-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathequations.py
375 lines (254 loc) · 8 KB
/
equations.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
'''
Defines the equations for the modeled dynamical system. Most of the arguments
to these functions are matrices, generated by structure.py.
Call pattern:
(v, dc_dt, dglc_dt) = equations.compute_all(parameter_values, *equations.args)
'''
from __future__ import division
import numpy as np
def reaction_rates(
gibbs_energies,
mu,
k_star,
RT,
forward_reaction_potential,
reverse_reaction_potential,
forward_binding_potential,
reverse_binding_potential,
reaction_forward_binding_association,
reaction_reverse_binding_association,
stoich,
glc_association,
):
'''
Computes net reaction rates.
'''
frp = forward_reaction_potential.dot(gibbs_energies)
rrp = reverse_reaction_potential.dot(gibbs_energies)
fbp = forward_binding_potential.dot(gibbs_energies)
rbp = reverse_binding_potential.dot(gibbs_energies)
denom = (
1
+ reaction_forward_binding_association.dot(np.exp(-fbp/RT))
+ reaction_reverse_binding_association.dot(np.exp(-rbp/RT))
)
return k_star * (np.exp(-frp/RT) - np.exp(-rrp/RT)) / denom
def dc_dt(
gibbs_energies,
mu,
k_star,
RT,
forward_reaction_potential,
reverse_reaction_potential,
forward_binding_potential,
reverse_binding_potential,
reaction_forward_binding_association,
reaction_reverse_binding_association,
stoich,
glc_association,
):
'''
Computes dc/dt, where 'c' is the metabolite concentrations.
'''
frp = forward_reaction_potential.dot(gibbs_energies)
rrp = reverse_reaction_potential.dot(gibbs_energies)
fbp = forward_binding_potential.dot(gibbs_energies)
rbp = reverse_binding_potential.dot(gibbs_energies)
denom = (
1
+ reaction_forward_binding_association.dot(np.exp(-fbp/RT))
+ reaction_reverse_binding_association.dot(np.exp(-rbp/RT))
)
v = k_star * (np.exp(-frp/RT) - np.exp(-rrp/RT)) / denom
glc = glc_association.dot(gibbs_energies)
c = np.exp(glc/RT)
return stoich.dot(v) - mu * c
def dglc_dt(
gibbs_energies,
mu,
k_star,
RT,
forward_reaction_potential,
reverse_reaction_potential,
forward_binding_potential,
reverse_binding_potential,
reaction_forward_binding_association,
reaction_reverse_binding_association,
stoich,
glc_association,
):
'''
Computes dglc/dt, where 'glc' is the Gibbs energy from the logarithm on the
metabolite concentrations.
'''
frp = forward_reaction_potential.dot(gibbs_energies)
rrp = reverse_reaction_potential.dot(gibbs_energies)
fbp = forward_binding_potential.dot(gibbs_energies)
rbp = reverse_binding_potential.dot(gibbs_energies)
denom = (
1
+ reaction_forward_binding_association.dot(np.exp(-fbp/RT))
+ reaction_reverse_binding_association.dot(np.exp(-rbp/RT))
)
v = k_star * (np.exp(-frp/RT) - np.exp(-rrp/RT)) / denom
glc = glc_association.dot(gibbs_energies)
return RT * np.exp(-glc/RT) * stoich.dot(v) - RT * mu
def compute_all(
gibbs_energies,
mu,
k_star,
RT,
forward_reaction_potential,
reverse_reaction_potential,
forward_binding_potential,
reverse_binding_potential,
reaction_forward_binding_association,
reaction_reverse_binding_association,
stoich,
glc_association,
):
'''
Computes all the terms needed for the optimization. This is done in one
function rather than calling the individual functions because it can reuse
several intermediate calculations.
'''
frp = forward_reaction_potential.dot(gibbs_energies)
rrp = reverse_reaction_potential.dot(gibbs_energies)
fbp = forward_binding_potential.dot(gibbs_energies)
rbp = reverse_binding_potential.dot(gibbs_energies)
denom = (
1
+ reaction_forward_binding_association.dot(np.exp(-fbp/RT))
+ reaction_reverse_binding_association.dot(np.exp(-rbp/RT))
)
v = k_star * (np.exp(-frp/RT) - np.exp(-rrp/RT)) / denom
glc = glc_association.dot(gibbs_energies)
c = np.exp(glc/RT)
dc = stoich.dot(v) - mu * c
dglc = RT * np.exp(-glc/RT) * stoich.dot(v) - RT * mu
return (v, dc, dglc)
def forward_reaction_rates(
gibbs_energies,
mu,
k_star,
RT,
forward_reaction_potential,
reverse_reaction_potential,
forward_binding_potential,
reverse_binding_potential,
reaction_forward_binding_association,
reaction_reverse_binding_association,
stoich,
glc_association,
):
'''
Computes the forward rates of reaction.
'''
frp = forward_reaction_potential.dot(gibbs_energies)
# rrp = reverse_reaction_potential.dot(gibbs_energies)
fbp = forward_binding_potential.dot(gibbs_energies)
rbp = reverse_binding_potential.dot(gibbs_energies)
denom = (
1
+ reaction_forward_binding_association.dot(np.exp(-fbp/RT))
+ reaction_reverse_binding_association.dot(np.exp(-rbp/RT))
)
return k_star * np.exp(-frp/RT) / denom
def reverse_reaction_rates(
gibbs_energies,
mu,
k_star,
RT,
forward_reaction_potential,
reverse_reaction_potential,
forward_binding_potential,
reverse_binding_potential,
reaction_forward_binding_association,
reaction_reverse_binding_association,
stoich,
glc_association,
):
'''
Computes the reverse rates of reaction.
'''
# frp = forward_reaction_potential.dot(gibbs_energies)
rrp = reverse_reaction_potential.dot(gibbs_energies)
fbp = forward_binding_potential.dot(gibbs_energies)
rbp = reverse_binding_potential.dot(gibbs_energies)
denom = (
1
+ reaction_forward_binding_association.dot(np.exp(-fbp/RT))
+ reaction_reverse_binding_association.dot(np.exp(-rbp/RT))
)
return k_star * np.exp(-rrp/RT) / denom
def build_jacobian():
'''
This function uses Theano, a symbolic math library, to analytically
evaluate the Jacobian of the dynamical system. In the spirit of minimizing
dependencies, and due to some performance issues, I opted to not use Theano
for this work. However I retained this function for legacy purposes.
'''
import theano as th
import theano.tensor as tn
gibbs_energies = tn.dvector('Molar Gibbs energies')
mu = tn.dscalar('Growth rate')
k_star = tn.dscalar('Fundamental catalytic rate constant')
RT = tn.dscalar('RT')
forward_reaction_potential = tn.dmatrix('Forward reaction potential matrix')
reverse_reaction_potential = tn.dmatrix('Reverse reaction potential matrix')
forward_binding_potential = tn.dmatrix('Forward binding potential matrix')
reverse_binding_potential = tn.dmatrix('Reverse binding potential matrix')
reaction_forward_binding_association = tn.dmatrix('Reaction forward binding association matrix')
reaction_reverse_binding_association = tn.dmatrix('Reaction reverse binding association matrix')
stoich = tn.dmatrix('Stoichiometry matrix')
glc_association = tn.dmatrix('Molar Gibbs energies from log-concentration association matrix')
frp = forward_reaction_potential.dot(gibbs_energies)
rrp = reverse_reaction_potential.dot(gibbs_energies)
fbp = forward_binding_potential.dot(gibbs_energies)
rbp = reverse_binding_potential.dot(gibbs_energies)
denom = (
1
+ reaction_forward_binding_association.dot(tn.exp(-fbp/RT))
+ reaction_reverse_binding_association.dot(tn.exp(-rbp/RT))
)
v = k_star * (tn.exp(-frp/RT) - tn.exp(-rrp/RT)) / denom
glc = glc_association.dot(gibbs_energies)
inputs = (
gibbs_energies,
mu,
k_star,
RT,
forward_reaction_potential,
reverse_reaction_potential,
forward_binding_potential,
reverse_binding_potential,
reaction_forward_binding_association,
reaction_reverse_binding_association,
stoich,
glc_association,
)
dglc_dt = RT * tn.exp(-glc/RT) * stoich.dot(v) - RT * mu
jac_dglc_dt = tn.jacobian(dglc_dt, gibbs_energies).dot(
glc_association.T
)
f_jac_dglc_dt = th.function(inputs, jac_dglc_dt)
return f_jac_dglc_dt
import constants
import structure
'''
args is the set of arguments passed to the equations above that aren't
parameter values.
'''
args = (
constants.MU,
constants.K_STAR,
constants.RT,
structure.forward_reaction_potential_matrix,
structure.reverse_reaction_potential_matrix,
structure.forward_binding_potential_matrix,
structure.reverse_binding_potential_matrix,
structure.reaction_forward_binding_association_matrix,
structure.reaction_reverse_binding_association_matrix,
structure.stoich,
structure.glc_association_matrix
)