From 82916933095374b552d33baa5a596113d3357904 Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Tue, 8 Nov 2016 11:19:12 +0100 Subject: [PATCH] obi grep: updated to work with the new line selection class and within the local sequence environment, and progress bar functioning --- python/obitools3/commands/grep.pyx | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/python/obitools3/commands/grep.pyx b/python/obitools3/commands/grep.pyx index e056dd1..2a75e86 100644 --- a/python/obitools3/commands/grep.pyx +++ b/python/obitools3/commands/grep.pyx @@ -1,7 +1,7 @@ from obitools3.apps.progress cimport ProgressBar # @UnresolvedImport -from obitools3.obidms._obidms import OBIDMS, OBIView, OBIView_NUC_SEQS # TODO cimport doesn't work - +from obitools3.obidms._obidms import OBIDMS, OBIView, OBIView_line_selection # TODO cimport doesn't work +from functools import reduce import time __title__="Grep view lines that match the given predicates" @@ -43,7 +43,7 @@ def addOptions(parser): group=parser.add_argument_group('obi grep specific options') group.add_argument('--predicate','-p', - action="store", dest="grep:predicate", + action="append", dest="grep:predicates", metavar='', default=None, type=str, @@ -59,29 +59,33 @@ def run(config): iview = d.open_view(config['obi']['inputview']) # Initialize the progress bar - #pb = ProgressBar(len(iview), config, seconde=5) + pb = ProgressBar(len(iview), config, seconde=5) # Apply filter - # TODO try both kinds of line selection functions to check efficiency - selection = [] + selection = OBIView_line_selection(iview) for i in range(len(iview)) : - #pb(i) + pb(i) line = iview[i] - if isinstance(iview, OBIView_NUC_SEQS) : # put this somewhere else for efficiency - sequence = line - if eval(config['grep']['predicate']) : + + loc_env = {'sequence': line, 'line': line} # TODO add taxonomy + + good = (reduce(lambda bint x, bint y: x and y, + (bool(eval(p, loc_env, line)) + for p in config['grep']['predicates']), True)) + + if good : selection.append(i) # Create output view with the line selection - oview = d.new_view(config['obi']['outputview'], view_to_clone=iview, line_selection=selection, comments="obi grep: "+config['grep']['predicate']+"\n") + oview = d.new_view(config['obi']['outputview'], line_selection=selection, comments="obi grep: "+str(config['grep']['predicates'])+"\n") - repr(oview) + #print("\n") + #print(repr(oview)) iview.close() oview.close() d.close() - print("Done.")