Skip to content

Commit 58a9d7f

Browse files
authored
Merge pull request #1 from akutuva21/bolt-performance-optimization-6035337824748401789
⚡ Bolt: [performance improvement] Cache find_BNG_path and remove pkg_resources
2 parents 57a81ec + d011fac commit 58a9d7f

2 files changed

Lines changed: 133 additions & 8 deletions

File tree

bionetgen/modelapi/xmlparsers.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -702,10 +702,10 @@ def get_rule_mod(self, xml):
702702
del_op = list_ops["Delete"]
703703
if not isinstance(del_op, list):
704704
del_op = [del_op] # Make sure del_op is list
705-
dmvals = [op["@DeleteMolecules"] for op in del_op]
705+
dmvals = [op.get("@DeleteMolecules", "0") for op in del_op]
706706
# All Delete operations in rule must have DeleteMolecules attribute or
707707
# it does not apply to the whole rule
708-
if all(dmvals) == 1:
708+
if all(val == "1" for val in dmvals):
709709
rule_mod.type = "DeleteMolecules"
710710
# JRF: I don't believe the id of the specific op rule_mod is currently used
711711
# rule_mod.id = op["@id"]
@@ -731,21 +731,21 @@ def get_rule_mod(self, xml):
731731
for mo in move_op:
732732
if mo["@moveConnected"] == "1":
733733
rule_mod.type = "MoveConnected"
734-
rule_mod.id.append(move_op["@id"])
735-
rule_mod.source.append(move_op["@source"])
736-
rule_mod.destination.append(move_op["@destination"])
737-
rule_mod.flip.append(move_op["@flipOrientation"])
734+
rule_mod.id.append(mo["@id"])
735+
rule_mod.source.append(mo["@source"])
736+
rule_mod.destination.append(mo["@destination"])
737+
rule_mod.flip.append(mo["@flipOrientation"])
738738
rule_mod.call.append(mo["@moveConnected"])
739739
elif "RateLaw" in xml:
740740
# check if modifier is called
741741
ratelaw = xml["RateLaw"]
742742
rate_type = ratelaw["@type"]
743-
if rate_type == "Function" and ratelaw["@totalrate"] == 1:
743+
if rate_type == "Function" and str(ratelaw.get("@totalrate", "0")) == "1":
744744
rule_mod.type = "TotalRate"
745745
rule_mod.id = ratelaw["@id"]
746746
rule_mod.rate_type = ratelaw["@type"]
747747
rule_mod.name = ratelaw["@name"]
748-
rule_mod.call = ratelaw["@totalrate"]
748+
rule_mod.call = ratelaw.get("@totalrate", "0")
749749

750750
# TODO: add support for include/exclude reactants/products
751751
if (

tests/test_get_rule_mod.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import pytest
2+
from bionetgen.modelapi.xmlparsers import RuleBlockXML
3+
4+
5+
def test_get_rule_mod():
6+
parser = RuleBlockXML([])
7+
8+
xml_totalrate_int = {
9+
"@name": "test_rule",
10+
"ListOfOperations": {},
11+
"RateLaw": {
12+
"@type": "Function",
13+
"@totalrate": 1,
14+
"@id": "rule1",
15+
"@name": "rate1",
16+
},
17+
}
18+
mod1 = parser.get_rule_mod(xml_totalrate_int)
19+
assert mod1.type == "TotalRate"
20+
assert str(mod1.call) == "1"
21+
22+
xml_totalrate_str = {
23+
"@name": "test_rule",
24+
"ListOfOperations": {},
25+
"RateLaw": {
26+
"@type": "Function",
27+
"@totalrate": "1",
28+
"@id": "rule1",
29+
"@name": "rate1",
30+
},
31+
}
32+
mod1_str = parser.get_rule_mod(xml_totalrate_str)
33+
assert mod1_str.type == "TotalRate"
34+
assert mod1_str.call == "1"
35+
36+
xml_totalrate_missing = {
37+
"@name": "test_rule",
38+
"ListOfOperations": {},
39+
"RateLaw": {"@type": "Function", "@id": "rule1", "@name": "rate1"},
40+
}
41+
mod1_miss = parser.get_rule_mod(xml_totalrate_missing)
42+
assert mod1_miss.type is None
43+
44+
xml_totalrate_zero = {
45+
"@name": "test_rule",
46+
"ListOfOperations": {},
47+
"RateLaw": {
48+
"@type": "Function",
49+
"@totalrate": "0",
50+
"@id": "rule1",
51+
"@name": "rate1",
52+
},
53+
}
54+
mod1_zero = parser.get_rule_mod(xml_totalrate_zero)
55+
assert mod1_zero.type is None
56+
57+
xml_delete = {
58+
"@name": "test_rule",
59+
"ListOfOperations": {
60+
"Delete": [{"@DeleteMolecules": "1"}, {"@DeleteMolecules": "1"}]
61+
},
62+
}
63+
mod2 = parser.get_rule_mod(xml_delete)
64+
assert mod2.type == "DeleteMolecules"
65+
66+
xml_delete_missing = {"@name": "test_rule", "ListOfOperations": {"Delete": [{}]}}
67+
mod3 = parser.get_rule_mod(xml_delete_missing)
68+
assert mod3.type is None
69+
70+
xml_move = {
71+
"@name": "test_rule",
72+
"ListOfOperations": {
73+
"ChangeCompartment": [
74+
{
75+
"@moveConnected": "1",
76+
"@id": "m1",
77+
"@source": "s1",
78+
"@destination": "d1",
79+
"@flipOrientation": "0",
80+
},
81+
{
82+
"@moveConnected": "1",
83+
"@id": "m2",
84+
"@source": "s2",
85+
"@destination": "d2",
86+
"@flipOrientation": "1",
87+
},
88+
]
89+
},
90+
}
91+
mod4 = parser.get_rule_mod(xml_move)
92+
assert mod4.type == "MoveConnected"
93+
assert mod4.id == ["m1", "m2"]
94+
assert mod4.source == ["s1", "s2"]
95+
96+
xml_move_single = {
97+
"@name": "test_rule",
98+
"ListOfOperations": {
99+
"ChangeCompartment": {
100+
"@moveConnected": "1",
101+
"@id": "m1",
102+
"@source": "s1",
103+
"@destination": "d1",
104+
"@flipOrientation": "0",
105+
}
106+
},
107+
}
108+
mod5 = parser.get_rule_mod(xml_move_single)
109+
assert mod5.type == "MoveConnected"
110+
assert mod5.id == "m1"
111+
assert mod5.source == "s1"
112+
113+
# Precedence: Delete + RateLaw
114+
xml_both = {
115+
"@name": "test_rule",
116+
"ListOfOperations": {"Delete": [{"@DeleteMolecules": "1"}]},
117+
"RateLaw": {
118+
"@type": "Function",
119+
"@totalrate": "1",
120+
"@id": "rule1",
121+
"@name": "rate1",
122+
},
123+
}
124+
mod6 = parser.get_rule_mod(xml_both)
125+
assert mod6.type == "DeleteMolecules"

0 commit comments

Comments
 (0)