-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_sbml_boundaries.py
67 lines (56 loc) · 2.33 KB
/
add_sbml_boundaries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from argparse import ArgumentParser
import cobra
import getpass
from datetime import datetime
ext_suffix = '_e' # or '_b' or '_p', note: in iAF1260 '_e' still is not the final external meta delimiter
new_ext_suffix = '_b' # or '_xt', new suffix to distinguish from the previous suffix (ie. ext_suffix)
replace_suffix = lambda n: n[:-len(ext_suffix)] + new_ext_suffix
add_suffix = lambda n: n + new_ext_suffix
add_history = False
boundary_correct = True
def correct_boundary(rb, apply_suffix=replace_suffix):
"""
Corrects a certain type of COBRA Model boundary reaction
Adds a boundary metabolite to the COBRA Model
Params:
rb: string representing the boundary reaction
apply_suffix: lambda function determining what should be done with the suffix
Returns:
rb: string representing the updated boundary reaction
Raises:
AssertionError: if the COBRA Model boundary reaction is badly formed
"""
tab = rb.split()
assert(len(tab) == 2)
assert(tab[1] == '-->' or tab[1] == '<=>' or tab[1] == '<--')
return tab[0]+ ' ' + tab[1] + ' ' + apply_suffix(tab[0])
def cobra_to_sbml(infname, outfname):
"""
Converts a COBRA SBML to an up-to-date SBML with standard boundary metabolites
Uses Python library COBRA
Params:
infname: name of the input COBRA SBML file
outfname: name of the output SBML file
"""
m = cobra.io.read_sbml_model(infname)
if boundary_correct:
for x in m.boundary:
x.reaction = correct_boundary(x.reaction)
no_comp = [x for x in m.metabolites if x.compartment is None]
for x in no_comp:
x.compartment = 'b'
cobra.io.sbml.write_sbml_model(m, outfname)
if add_history:
username = getpass.getuser()
today = datetime.today().strftime("%Y-%m-%d at %H:%M:%S")
with open(outfname, "r+") as fp:
lines = fp.readlines()
lines.insert(0, f"<!-- last edited by {username} on {today} -->\n")
fp.seek(0)
fp.writelines(lines)
if __name__== "__main__":
parser = ArgumentParser()
parser.add_argument('infile', help='Input filename: Original COBRA SBML')
parser.add_argument('outfile', help='Output filename: Updated SBML')
opts = parser.parse_args()
cobra_to_sbml(opts.infile, opts.outfile)