diff --git a/python/obitools3/commands/grep.pyx b/python/obitools3/commands/grep.pyx index 96f7768..ff656f5 100644 --- a/python/obitools3/commands/grep.pyx +++ b/python/obitools3/commands/grep.pyx @@ -1,100 +1,96 @@ -# from obitools3.apps.progress cimport ProgressBar # @UnresolvedImport -# from obitools3.dms.dms import OBIDMS # TODO cimport doesn't work -# #, OBIView, OBIView_line_selection # TODO cimport doesn't work -# -# from functools import reduce -# import time -# -# __title__="Grep view lines that match the given predicates" -# +#cython: language_level=3 +from obitools3.apps.progress cimport ProgressBar # @UnresolvedImport +from obitools3.dms.dms import DMS # TODO cimport doesn't work +from obitools3.dms.view.view import View, Line_selection # TODO cimport doesn't work + +from functools import reduce +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="append", dest="grep:predicates", + metavar='', + default=None, + type=str, + help="Grep lines that match the given python expression on or .") + + def run(config): - pass - -# 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="append", dest="grep:predicates", -# 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 -# # selection = OBIView_line_selection(iview) -# # for i in range(len(iview)) : -# # pb(i) -# # line = iview[i] -# # -# # 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.clone_view_with_line_selection(config['obi']['outputview'], selection, comments="obi grep: "+str(config['grep']['predicates'])+"\n") -# # -# # #print("\n") -# # #print(repr(oview)) -# # -# # iview.close() -# # oview.close() -# d.close() -# -# -# -# -# -# -# -# -# \ No newline at end of file + + # Open DMS + d = DMS.open(config['obi']['defaultdms']) + + # Open input view 1 + iview = View.open(d, config['obi']['inputview']) + + # Initialize the progress bar + pb = ProgressBar(len(iview), config, seconde=5) + + # Apply filter + selection = Line_selection(iview) + for i in range(len(iview)) : + pb(i) + line = iview[i] + + 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 = selection.materialize(config['obi']['outputview'], comments="obi grep: "+str(config['grep']['predicates'])+"\n") + + print("\n") + print(repr(oview)) + + d.close() + + + + + + + + + \ No newline at end of file