-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmfsbSbml.py
228 lines (197 loc) · 5.29 KB
/
smfsbSbml.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python3
# smfsbSbml.py
# Import SBML models into an smfsb Spn
import libsbml
import sys
from jsmfsb import Spn
import jax.numpy as jnp
from sbmlsh import mod2sbml
def mod2Spn(filename, verb=False):
"""Convert an SBML-shorthand model into a Spn object
Read a file containing a model in SBML-shorthand and convert into
an Spn object for simulation and analysis.
Parameters
----------
filename: string
String name of file containing the model
verb: boolean
Output some debugging info
Returns
-------
An Spn object
Examples
--------
>>> import smfsb
>>> myMod = smfsb.mod2spn("myModel.mod")
>>> step = myMod.stepGillespie()
"""
try:
s = open(filename, "r")
except:
sys.stderr.write("Error: failed to open "+filename+"\n")
sys.exit(1)
p = mod2sbml.Parser()
d = p.parseStream(s)
m = d.getModel()
if (m == None):
sys.stderr.write("Error: can't extract SBML model\n")
sys.exit(1)
return(model2Spn(m, verb))
def sh2Spn(shString, verb=False):
"""Convert an SBML-shorthand model string into a Spn object
Parse a string containing a model in SBML-shorthand and convert into
an Spn object for simulation and analysis.
Parameters
----------
shString: string
String containing the model
verb: boolean
Output some debugging info
Returns
-------
An Spn object
Examples
--------
>>> import smfsb
>>> file = open('myModel.mod', 'r')
>>> myModStr = file.read()
>>> file.close()
>>> myMod = smfsb.sh2spn(myModStr)
>>> step = myMod.stepGillespie()
"""
p = mod2sbml.Parser()
d = p.parse(shString)
m = d.getModel()
if (m == None):
sys.stderr.write("Error: couldn't parse the shorthand string\n")
sys.exit(1)
return(model2Spn(m, verb))
def file2Spn(filename, verb=False):
"""Convert an SBML model into a Spn object
Read a file containing a model in SBML and convert into
an Spn object for simulation and analysis.
Parameters
----------
filename: string
String name of file containing the model
verb: boolean
Output some debugging info
Returns
-------
An Spn object
Examples
--------
>>> import smfsb
>>> myMod = smfsb.mod2spn("myModel.xml")
>>> step = myMod.stepGillespie()
"""
d = libsbml.readSBML(filename)
m = d.getModel()
if (m == None):
sys.stderr.write("Can't parse SBML file: "+filename+"\n")
sys.exit(1)
return(model2Spn(m, verb))
def model2Spn(m, verb=False):
"""Convert a libSBML model into a Spn object
Convert a libSBML model into a Spn object for simulation and analysis.
Parameters
----------
m: model
A libsbml model (not document) object
verb: boolean
Output some debugging info
Returns
-------
An Spn object
Examples
--------
>>> import smfsb
>>> import libsbml
>>> d = libsbml.readSBML("myModel.xml")
>>> m = d.getModel()
>>> myMod = smfsb.model2Spn(m)
>>> step = myMod.stepGillespie()
"""
# Species and initial amounts
ns = m.getNumSpecies()
if (verb):
print(str(ns)+" species")
ml = []
nl = []
for i in range(ns):
s = m.getSpecies(i)
nl += [s.getId()]
ml += [s.getInitialAmount()]
if (verb):
print(nl)
print(ml)
# Compartments
nc = m.getNumCompartments()
cd = {}
for i in range(nc):
comp = m.getCompartment(i)
cd[comp.getId()] = comp.getVolume()
if (verb):
print(cd)
# Global parameters
ngp = m.getNumParameters()
gpd = {}
for i in range(ngp):
param = m.getParameter(i)
gpd[param.getId()] = param.getValue()
if (verb):
print(gpd)
# Reactions
nr = m.getNumReactions()
if (verb):
print(str(nr)+" reactions")
pre = jnp.zeros((nr, ns))
post = jnp.zeros((nr, ns))
rn = []
kl = []
lpl = []
for i in range(nr):
r = m.getReaction(i)
rn += [r.getId()]
nPre = r.getNumReactants()
for j in range(nPre):
sr = r.getReactant(j)
sto = sr.getStoichiometry()
pre = pre.at[i, nl.index(sr.getSpecies())].set(sto)
nPost = r.getNumProducts()
for j in range(nPost):
sr = r.getProduct(j)
sto = sr.getStoichiometry()
post= post.at[i, nl.index(sr.getSpecies())].set(sto)
kli = r.getKineticLaw()
kl += [libsbml.formulaToString(kli.getMath())]
nlp = kli.getNumLocalParameters()
lpd = {}
for j in range(nlp):
param = kli.getLocalParameter(j)
lpd[param.getId()] = param.getValue()
lpl += [lpd]
if (verb):
print(rn)
print("Pre:")
print(pre)
print("Post:")
print(post)
print(kl)
print(lpl)
gpd.update(cd)
def haz(x, t):
h = jnp.zeros(nr)
xd = dict(zip(nl, x))
glob = gpd.copy()
glob.update(xd)
for i in range(nr):
h = h.at[i].set(eval(kl[i], glob, lpl[i]))
return(h)
spn = Spn(nl, rn, pre, post, haz, ml)
spn.comp = cd
spn.gp = gpd
spn.kl = kl
spn.lp = lpl
return(spn)
# eof