From 64a745ce0bbae40df13079f1db52f293542c4409 Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Thu, 11 Aug 2016 17:32:08 +0200 Subject: [PATCH] First very basic version of obi grep command --- python/obitools3/commands/grep.pyx | 93 ++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 python/obitools3/commands/grep.pyx diff --git a/python/obitools3/commands/grep.pyx b/python/obitools3/commands/grep.pyx new file mode 100644 index 0000000..ffcbe3e --- /dev/null +++ b/python/obitools3/commands/grep.pyx @@ -0,0 +1,93 @@ +from obitools3.apps.progress cimport ProgressBar # @UnresolvedImport +from obitools3.obidms._obidms import OBIDMS, OBIView, OBIView_NUC_SEQS # TODO cimport doesn't work + + +import time + +__title__="Grep view lines that match the given predicates" + + +default_config = { 'inputview' : None, + 'outputview' : None + } + +def addOptions(parser): + + # TODO put this common group somewhere else but I don't know where + group=parser.add_argument_group('DMS and view options') + + group.add_argument('--default-dms','-d', + action="store", dest="obi:defaultdms", + metavar='', + default=None, + type=str, + help="Name of the default DMS for reading and writing data.") + + group.add_argument('--input-view','-i', + action="store", dest="obi:inputview", + metavar='', + default=None, + type=str, + help="Name of the input view, either raw if the view is in the default DMS," + " or in the form 'dms:view' if it is in another DMS.") + + group.add_argument('--output-view','-o', + action="store", dest="obi:outputview", + metavar='', + default=None, + type=str, + help="Name of the output view, either raw if the view is in the default DMS," + " or in the form 'dms:view' if it is in another DMS.") + + + group=parser.add_argument_group('obi grep specific options') + + group.add_argument('--predicate','-p', + action="store", dest="grep:predicate", + metavar='', + default=None, + type=str, + help="Grep lines that match the given python expression on or .") + + +def run(config): + + # Open DMS + d = OBIDMS(config['obi']['defaultdms']) + + # Open input view 1 + iview = d.open_view(config['obi']['inputview']) + + # Initialize the progress bar + #pb = ProgressBar(len(iview), config, seconde=5) + + # Apply filter + # TODO try both kinds of line selection functions to check efficiency + selection = [] + for i in range(len(iview)) : + #pb(i) + line = iview[i] + if isinstance(iview, OBIView_NUC_SEQS) : # put this somewhere else for efficiency + sequence = line + if eval(config['grep']['predicate']) : + selection.append(i) + + # Create output view with the line selection + if isinstance(iview, OBIView_NUC_SEQS) : # TODO make view type automatic when cloning + oview = d.new_view(config['obi']['outputview'], view_type="NUC_SEQS_VIEW", view_to_clone=iview, line_selection=selection, comments="obi grep: "+config['grep']['predicate']+'\n') + else : + oview = d.new_view(config['obi']['outputview'], view_to_clone=iview, line_selection=selection, comments="obi grep: "+config['grep']['predicate']) + + iview.save_and_close() + oview.save_and_close() + d.close() + + print("Done.") + + + + + + + + \ No newline at end of file