Skip to content

Commit ac69b93

Browse files
Merge pull request ModelSEED#93 from ModelSEED/rebalancing_reactions
Implemented removal of reagents of zero coefficient
2 parents c6eda9c + ea12abe commit ac69b93

4 files changed

Lines changed: 29 additions & 13 deletions

File tree

Libs/Python/BiochemPy/Reactions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ def balanceReaction(self, rgts_array):
227227

228228
def adjustCompound(self, rxn_cpds_array, compound, adjustment,
229229
compartment=0):
230+
230231
if (adjustment == 0):
231232
return rxn_cpds_array
232233

@@ -238,11 +239,14 @@ def adjustCompound(self, rxn_cpds_array, compound, adjustment,
238239

239240
# Check to see if it already exists
240241
cpd_exists = 0
242+
cpd_remove = {}
241243
for rgt in rxn_cpds_array:
242244
if (rgt["compound"] == compound and
243245
rgt["compartment"] == compartment):
244246
rgt["coefficient"] -= adjustment
245247
cpd_exists = 1
248+
if(rgt["coefficient"] == 0):
249+
cpd_remove=rgt
246250

247251
if (cpd_exists != 1):
248252
rgt_id = compound + "_" + str(compartment) + "0"
@@ -253,6 +257,10 @@ def adjustCompound(self, rxn_cpds_array, compound, adjustment,
253257
"name": self.Compounds_Dict[compound]["name"],
254258
"formula": self.Compounds_Dict[compound]["formula"],
255259
"charge": self.Compounds_Dict[compound]["charge"]})
260+
261+
if(len(cpd_remove.keys())>0):
262+
rxn_cpds_array.remove(cpd_remove)
263+
256264
return
257265

258266
def rebuildReaction(self, reaction_dict, stoichiometry):

Scripts/Biochemistry/Adjust_Protons.py renamed to Scripts/Biochemistry/Adjust_Reaction_Protons.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
Update_Reactions=0
1313
for rxn in sorted(Reactions_Dict.keys()):
14+
# if(rxn != "rxn00013"):
15+
# continue
16+
1417
if(Reactions_Dict[rxn]["status"] == "EMPTY"):
1518
continue
1619

@@ -36,7 +39,9 @@
3639

3740
old_stoichiometry=Reactions_Dict[rxn]["stoichiometry"]
3841
Rxn_Cpds_Array=ReactionsHelper.parseStoich(old_stoichiometry)
42+
print "1: ",Rxn_Cpds_Array
3943
ReactionsHelper.adjustCompound(Rxn_Cpds_Array,"cpd00067",float(number))
44+
print "2: ",Rxn_Cpds_Array
4045
new_status = ReactionsHelper.balanceReaction(Rxn_Cpds_Array)
4146
new_stoichiometry = ReactionsHelper.buildStoich(Rxn_Cpds_Array)
4247

@@ -49,8 +54,8 @@
4954
Reactions_Dict[rxn]["notes"]="HB"
5055
else:
5156
Reactions_Dict[rxn]["notes"]+="|HB"
52-
Update_Reactions=1
57+
Update_Reactions+=1
5358

54-
if(Update_Reactions==1):
55-
print "Saving reactions";
59+
if(Update_Reactions>0):
60+
print "Saving adjusted protons for "+str(Update_Reactions)+" reactions";
5661
ReactionsHelper.saveReactions(Reactions_Dict)

Scripts/Biochemistry/Rebalance_Reactions.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,25 @@
1111

1212
Update_Reactions=0
1313
for rxn in sorted(Reactions_Dict.keys()):
14+
# if(rxn != "rxn00013"):
15+
# continue
16+
1417
if(Reactions_Dict[rxn]["status"] == "EMPTY"):
1518
continue
1619

1720
Rxn_Cpds_Array=ReactionsHelper.parseStoich(Reactions_Dict[rxn]["stoichiometry"])
18-
Status = ReactionsHelper.balanceReaction(Rxn_Cpds_Array)
21+
new_status = ReactionsHelper.balanceReaction(Rxn_Cpds_Array)
1922

2023
#Need to handle reactions with polymers
21-
if("ERROR" in Status):
24+
if("ERROR" in new_status):
2225
continue
2326

2427
old_status=Reactions_Dict[rxn]["status"]
25-
if(Status != old_status and "CK" not in old_status):
26-
print "Changing Status for "+rxn+" from "+old_status+" to "+Status
27-
Reactions_Dict[rxn]["status"]=Status
28-
Update_Reactions=1
28+
if(new_status != old_status and "CK" not in old_status):
29+
print "Changing Status for "+rxn+" from "+old_status+" to "+new_status
30+
Reactions_Dict[rxn]["status"]=new_status
31+
Update_Reactions=+1
2932

30-
if(Update_Reactions==1):
31-
print "Saving reactions";
33+
if(Update_Reactions>0):
34+
print "Saving updated statuses for "+str(Update_Reactions)+" reactions";
3235
ReactionsHelper.saveReactions(Reactions_Dict)

Scripts/Biochemistry/Rebuild_Reactions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
Stoichiometry=ReactionsHelper.buildStoich(Rxn_Cpds_Array)
1919
if(Stoichiometry != Reactions_Dict[rxn]["stoichiometry"]):
2020
ReactionsHelper.rebuildReaction(Reactions_Dict[rxn],Stoichiometry)
21-
Update_Reactions=1
21+
Update_Reactions+=1
2222

23-
print "Saving reactions";
23+
print "Saving rebuilt equations for "+str(Update_Reactions)+" reactions";
2424
ReactionsHelper.saveReactions(Reactions_Dict)

0 commit comments

Comments
 (0)