diff --git a/rpg/digest.py b/rpg/digest.py index 9d8bcf4d5b7a2e7428f385eb72ba6ea54b22559d..ca946215e0db8a90c996bca889834d9d2043414d 100644 --- a/rpg/digest.py +++ b/rpg/digest.py @@ -243,13 +243,14 @@ def one_digest(pep, enz): ret.inc_nb_cleavage() # current position previous_pos = pos - # Default: do not cut this position - cut = False before = True # Check each rules for rul in enz.rules: + # Default: do not cut this position + cut = None # Apply the rule: if we need to cut - if rule.handle_rule(pep.sequence, pos, rul, cut): + cut = rule.handle_rule(pep.sequence, pos, rul, cut) + if cut is True: # Random to handle miscleavage tmp_rand = random.random() * 100 # Rand > ratio_miscleavage, no miscleavage occurs diff --git a/rpg/rule.py b/rpg/rule.py index 8c1312dfd006d821aa391158dd286a7479c694d9..6dcf802c9fd33a0e31dc6a3b6a820197af8627e0 100644 --- a/rpg/rule.py +++ b/rpg/rule.py @@ -786,7 +786,7 @@ def handle_rule(seq, pos, a_rule, cut): :type cut: bool :return: `True` if sequence must be cutted - :rtype: bool + :rtype: bool or None """ # return of the function: should we cut this? @@ -799,11 +799,28 @@ def handle_rule(seq, pos, a_rule, cut): # If the rule applies, i.e. the letter to watch is the good one if (pos + a_rule.index) >= 0 and \ seq[pos + a_rule.index] == a_rule.letter: - ret = a_rule.cut - # Handle the rules (exceptions) - for rul in a_rule.rules: - # Apply the rule: do we need to cut? - ret = handle_rule(seq, pos, rul, ret) + # If no previous 'False' and this is cutting + if a_rule.cut and ret is not False: + ret = True + # Handle the sub-rules (exceptions) + for rul in a_rule.rules: + # Apply the rule: do we need to cut? + ret = handle_rule(seq, pos, rul, ret) + # Is is not cutting + elif not a_rule.cut: + # Reinit + ret = None + # Handle sub-rules + if a_rule.rules: + # Handle the rules (exceptions) + for rul in a_rule.rules: + # Apply the rule: do we need to cut? + ret = handle_rule(seq, pos, rul, ret) + # No sub-rules and not cutting + else: + # We are at the end a of rule that applies + # and say to NOT cut. So it will never cut. + ret = False # Doesn't work: begin or end of sequence, don't change cut value except IndexError: pass