From 36a8aaa92e29b97f7addae3259685c241bc63843 Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Wed, 10 Jun 2020 16:57:42 +0200 Subject: [PATCH] grep: now creating empty views instead of displaying an error when selecting on an unexisting column/tag --- python/obitools3/commands/grep.pyx | 44 +++++++++++++++++++----------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/python/obitools3/commands/grep.pyx b/python/obitools3/commands/grep.pyx index 357a690..c8bd68c 100644 --- a/python/obitools3/commands/grep.pyx +++ b/python/obitools3/commands/grep.pyx @@ -161,8 +161,7 @@ def obi_eval(compiled_expr, loc_env, line): return obi_eval_result -def Filter_generator(options, tax_filter): - #taxfilter = taxonomyFilterGenerator(options) +def Filter_generator(options, tax_filter, i_view): # Initialize conditions predicates = None @@ -171,6 +170,9 @@ def Filter_generator(options, tax_filter): attributes = None if "attributes" in options and len(options["attributes"]) > 0: attributes = options["attributes"] + for attribute in attributes: + if attribute not in i_view: + return None lmax = None if "lmax" in options: lmax = options["lmax"] @@ -196,6 +198,8 @@ def Filter_generator(options, tax_filter): if "attribute_patterns" in options and len(options["attribute_patterns"]) > 0: for p in options["attribute_patterns"]: attribute, pattern = p.split(":", 1) + if attribute not in i_view: + return None attribute_patterns[tobytes(attribute)] = re.compile(tobytes(pattern)) def filter(line, loc_env): @@ -324,21 +328,29 @@ def run(config): # Apply filter tax_filter = Taxonomy_filter_generator(taxo, config["grep"]) - filter = Filter_generator(config["grep"], tax_filter) + filter = Filter_generator(config["grep"], tax_filter, i_view) selection = Line_selection(i_view) - for i in range(len(i_view)): - PyErr_CheckSignals() - pb(i) - line = i_view[i] - - loc_env = {"sequence": line, "line": line, "taxonomy": taxo, "obi_eval_result": False} - - good = filter(line, loc_env) - - if good : - selection.append(i) - - pb(i, force=True) + + if filter is None and config["grep"]["invert_selection"]: # all sequences are selected: filter is None if no line will be selected because some columns don't exist + for i in range(len(i_view)): + PyErr_CheckSignals() + pb(i) + selection.append(i) + + elif filter is not None : # filter is None if no line will be selected because some columns don't exist + for i in range(len(i_view)): + PyErr_CheckSignals() + pb(i) + line = i_view[i] + + loc_env = {"sequence": line, "line": line, "taxonomy": taxo, "obi_eval_result": False} + + good = filter(line, loc_env) + + if good : + selection.append(i) + + pb(len(i_view), force=True) print("", file=sys.stderr) # Create output view with the line selection