From 2548da8f48c0147be0bfac86c2c87b39adabbd4b Mon Sep 17 00:00:00 2001
From: Nico Maillet <nicolas.maillet@pasteur.fr>
Date: Mon, 12 Apr 2021 16:52:18 +0200
Subject: [PATCH] Correct a major bug when a new enzyme is define and does not
 have anything before the cleaving site in the definition, and more than one
 following aa after the cleaving site, i.e. (,E)(E)(E). Closes #1

---
 rpg/rule.py | 47 +++++++++++++++++++++++++++++++----------------
 1 file changed, 31 insertions(+), 16 deletions(-)

diff --git a/rpg/rule.py b/rpg/rule.py
index 15af66b..8e16b29 100644
--- a/rpg/rule.py
+++ b/rpg/rule.py
@@ -582,25 +582,40 @@ def create_rules(all_rules):
             exceptions.append(dict_rule)
         # No exception
         else:
-            # Parse all after cleaving site first
-            for i in reversed(sorted(dict_rule_no_main.keys())):
-                # Find the deepest rule: left if exist, otherwise, right
+            # Find the deepest rule: left if exist, otherwise, right
+            if len(dict_rule_no_main.keys()) > 0:
                 min_key = min(dict_rule_no_main.keys()) # Deep on left
                 max_key = max(dict_rule_no_main.keys()) # Deep on right
-                if min_key < 0: # Something left, change it (leftest)
-                    if i == min_key:
-                        cleav = not cleav
+                # Something left
+                if min_key < 0:
+                    # Parse all after cleaving site first in reverse
+                    for i in reversed(sorted(dict_rule_no_main.keys())):
+                        # Deeper rule, change it (leftest)
+                        if i == min_key:
+                            cleav = not cleav
+                        # Create the rule
+                        this_rule = Rule(i, dict_rule_no_main[i], cleav, -1)
+                        # Add this rule to upper-rule
+                        if not previous_rule.equ(this_rule) and\
+                           not previous_rule.contains_any_level(this_rule):
+                            previous_rule.rules.append(this_rule)
+                        # Current rule is the new down-rule
+                        previous_rule = this_rule
+
                 else: # Nothing left, change deepest rule at right
-                    if i == max_key:
-                        cleav = not cleav
-                # Create the rule
-                this_rule = Rule(i, dict_rule_no_main[i], cleav, -1)
-                # Add this rule to upper-rule
-                if not previous_rule.equ(this_rule) and\
-                   not previous_rule.contains_any_level(this_rule):
-                    previous_rule.rules.append(this_rule)
-                # Current rule is the new down-rule
-                previous_rule = this_rule
+                    # Parse all after cleaving site first
+                    for i in sorted(dict_rule_no_main.keys()):
+                        # Deeper rule, change it (rightest)
+                        if i == max_key:
+                            cleav = not cleav
+                        # Create the rule
+                        this_rule = Rule(i, dict_rule_no_main[i], cleav, -1)
+                        # Add this rule to upper-rule
+                        if not previous_rule.equ(this_rule) and\
+                           not previous_rule.contains_any_level(this_rule):
+                            previous_rule.rules.append(this_rule)
+                        # Current rule is the new down-rule
+                        previous_rule = this_rule
             # Create the corresponding rule
             add_rule(correct_rules, cleaving_zone)
 
-- 
GitLab