-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathensemble_models.py
More file actions
282 lines (202 loc) · 8.54 KB
/
ensemble_models.py
File metadata and controls
282 lines (202 loc) · 8.54 KB
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
#!/usr/bin/env pyhton
# -*- coding: UTF-8 -*-
__author__ = 'Chao Wu'
__date__ = '10/19/2018'
__version__ = '1.0'
def generate_ensemble_models(S, enzymeInfo, Vss, nmodels, Ess = [], Css = []):
'''
Parameters
S: stoichiometric matrix, metabolite in rows, reaction in columns (including input and output reactions)
enzymeInfo: df, reaction in rows (same order with S)
Vss: ser, fluxes in steady state
nmodels: int, number of ensemble models
Css: ser, metabolite concentration in steady state
Ess: ser, enzyme concentration in steady state
Returns
ensembleModels: lst
NOTE kcats, ... are in order of enzymes
NOTE product concs, coes, Kms = [] and Keqs = 0 for irreversible and output reactions
'''
import numpy as np
import re
from numpy.random import rand
from constants import deftKm, deftKmRelBnds, deftKeqRelBnds
from common_rate_laws import v_expression
ensembleModels = []
for i in range(nmodels):
reverses, kcats, subConcss, subCoess, subKmss, proConcss, proCoess, proKmss, Keqs = [], [], [], [], [], [], [], [], []
for enzyme in S.columns:
subs = S.index[S.loc[:, enzyme] < 0]
pros = S.index[S.loc[:, enzyme] > 0]
reverse = 0 if enzyme not in enzymeInfo.index else enzymeInfo.loc[enzyme, 'rev']
# generate random Kms and Keq, metabolite concentrations set 1, then calculate kcat
if subs.size == 0: # no substrate indicates a input reaction (in form of X_in -> X)
if len(Css) > 0:
KmLBs = deftKm * deftKmRelBnds[0]
KmUBs = deftKm * deftKmRelBnds[1]
else:
KmLBs = deftKmRelBnds[0]
KmUBs = deftKmRelBnds[1]
subConcs = np.ones(1)
subCoes = np.ones(1)
subKms = np.power(10, np.log10(KmLBs) + (np.log10(KmUBs) - np.log10(KmLBs)) * rand(1))
elif pros.size == 0: # no product indicates a output reaction (in form of X -> X_out)
if len(Css) > 0:
subConcs = Css.loc[subs].values
KmLBs = deftKm * deftKmRelBnds[0]
KmUBs = deftKm * deftKmRelBnds[1]
else:
subConcs = np.ones(1)
KmLBs = deftKmRelBnds[0]
KmUBs = deftKmRelBnds[1]
subCoes = np.ones(1)
subKms = np.power(10, np.log10(KmLBs) + (np.log10(KmUBs) - np.log10(KmLBs)) * rand(1))
else:
if len(Css) > 0:
subConcs = Css.loc[subs].values
KmLBs = np.array([item[1] for item in enzymeInfo.loc[enzyme, 'subsKm'].loc[subs]])
KmUBs = np.array([item[2] for item in enzymeInfo.loc[enzyme, 'subsKm'].loc[subs]])
else:
subConcs = np.ones(len(subs))
KmLBs = deftKmRelBnds[0]
KmUBs = deftKmRelBnds[1]
subKms = np.power(10, np.log10(KmLBs) + (np.log10(KmUBs) - np.log10(KmLBs)) * rand(len(subs)))
subCoes = S.loc[subs, enzyme].abs().values
if reverse:
if len(Css) > 0:
proConcs = Css.loc[pros].values
KmLBs = np.array([item[1] for item in enzymeInfo.loc[enzyme, 'prosKm'].loc[pros]])
KmUBs = np.array([item[2] for item in enzymeInfo.loc[enzyme, 'prosKm'].loc[pros]])
KeqLBs = enzymeInfo.loc[enzyme, 'Keq'][1]
KeqUBs = enzymeInfo.loc[enzyme, 'Keq'][2]
else:
proConcs = np.ones(len(pros))
KmLBs = deftKmRelBnds[0]
KmUBs = deftKmRelBnds[1]
KeqLBs = deftKeqRelBnds[0]
KeqUBs = deftKeqRelBnds[1]
proKms = np.power(10, np.log10(KmLBs) + (np.log10(KmUBs) - np.log10(KmLBs)) * rand(len(pros)))
proCoes = S.loc[pros, enzyme].abs().values
Keq = np.power(10, np.log10(KeqLBs) + (np.log10(KeqUBs) - np.log10(KeqLBs)) * rand())
else:
proConcs = []
proCoes = []
proKms = []
Keq = 0
if len(Css) > 0:
kcat = Vss[enzyme] / Ess[enzyme] / v_expression(reverse, subConcs, subCoes, subKms, proConcs, proCoes, proKms, Keq) / 3600 # NOTE V in mmol/gCDW/h, E in mmol/gCDW, kcat should be in 1/s
else:
kcat = Vss[enzyme] / v_expression(reverse, subConcs, subCoes, subKms, proConcs, proCoes, proKms, Keq) # E = 1 in reference state for relative values
reverses.append(reverse)
kcats.append(kcat)
subConcss.append(subConcs)
subCoess.append(subCoes)
subKmss.append(subKms)
proConcss.append(proConcs)
proCoess.append(proCoes)
proKmss.append(proKms)
Keqs.append(Keq)
ensembleModels.append([reverses, kcats, subConcss, subCoess, subKmss, proConcss, proCoess, proKmss, Keqs])
return ensembleModels
def simulation_worker(i, ensembleModel, S, Smetab2rnx, E, Eini, X, Xini, enzymes, nsteps, enzymeLBs, enzymeUBs):
'''
Parameters
i: int, model #
ensembleModel: lst
S: df, stoichiometric matrix, metabolite in rows, reaction in columns
Smetab2rnx: df, transforme X to metabolites needed in each reaction
E: sym array, enzyme concentrations, in order of enzymes
Eini: array, initial enzyme concentrations, in order of enzymes
X: sym array, metabolites concentrations, in order of metabs
Xini: array, initial metabolites concentrations, in order of metabs
enzymes: lst, enzyme IDs
nsteps: int, # of integration steps
enzymeLBs: ser, lower bounds of enzyme level
enzymeUBs: ser, upper bounds of enzyme level
Returns
resultPerModel: dict
'''
import numpy as np
import pandas as pd
from scipy.linalg import eigvals
from constants import eigThreshold
from utilities import get_Jacobian, get_dVdE, solve_dXdE, get_lambdify_function
from common_rate_laws import v_expression
print('\nprocessing model %s ...' % (i + 1))
reverses, kcats, subConcss, subCoess, subKmss, proConcss, proCoess, proKmss, Keqs = ensembleModel
# calculate the Jacobian matrix of reference state and keep those model with all Jacobian eigenvalues real parts < 0
J = get_Jacobian(S, Smetab2rnx, v_expression, Eini, X, reverses, kcats, subCoess, subKmss, proCoess, proKmss, Keqs)
Jlam = get_lambdify_function(X, J)
Jss = np.matrix(Jlam(*Xini)).astype(np.float)
if np.any(eigvals(Jss).real >= eigThreshold):
print('Jacobian matrix singular, model abandoned')
return
# solve ODE to get relation of X ~ E
J = get_Jacobian(S, Smetab2rnx, v_expression, E, X, reverses, kcats, subCoess, subKmss, proCoess, proKmss, Keqs)
dVdE = get_dVdE(Smetab2rnx, v_expression, E, X, reverses, kcats, subCoess, subKmss, proCoess, proKmss, Keqs)
XE = np.concatenate((X, E))
Jlam = get_lambdify_function(XE, J)
dVdElam = get_lambdify_function(XE, dVdE)
resultPerModel = {}
for enzyme in enzymes:
# enzyme concentration increase
Espan1 = pd.DataFrame(np.array([Eini, Eini]).T, index = enzymes)
Espan1.loc[enzyme, 1] = enzymeUBs.loc[enzyme]
Eout1, Xout1 = solve_dXdE(Espan1, nsteps, Xini, Jlam, dVdElam, S)
Eout1 = Eout1.dropna(axis = 1)
Xout1 = Xout1.dropna(axis = 1)
# enzyme concentration decrease
Espan2 = pd.DataFrame(np.array([Eini, Eini]).T, index = enzymes)
Espan2.loc[enzyme, 1] = enzymeLBs.loc[enzyme]
Eout2, Xout2 = solve_dXdE(Espan2, nsteps, Xini, Jlam, dVdElam, S)
Eout2 = Eout2.dropna(axis = 1)
Xout2 = Xout2.dropna(axis = 1)
resultPerModel[enzyme] = [Eout2, Eout1, Xout2, Xout1]
return resultPerModel
def simulate_perturbation(ensembleModels, S, Smetab2rnx, enzymes, metabs, nsteps, enzymeLBs, enzymeUBs, nmodels, nprocess, Eini = [], Xini = []):
'''
Parameters
ensembleModels: lst
S: df, stoichiometric matrix, metabolite in rows, reaction in columns
Smetab2rnx: df, transforme X to metabolites needed in each reaction
enzymes: lst, enzyme IDs
metabs: lst, metabolite IDs
nsteps: int, # of integration steps
enzymeLBs: ser, lower bounds of enzyme level
enzymeUBs: ser, upper bounds of enzyme level
nmodels: int, number of ensemble models
nprocess: int, number of processes
Eini: ser, initial enzyme concentrations, if real values used
Xini: ser, initial enzyme concentrations if real values used
Returns
results: dict
'''
import numpy as np
from sympy import symbols
from multiprocessing import Pool
X = np.array(symbols(' '.join(metabs)))
E = np.array(symbols(' '.join(enzymes)))
if len(Eini) > 0:
Xini = Xini.loc[metabs]
Eini = Eini.loc[enzymes]
ifReal = 'yes'
else:
Xini = np.ones(len(metabs))
Eini = np.ones(len(enzymes))
ifReal = 'no'
# multiprocessing
pool = Pool(processes = nprocess)
tmp = []
for i in range(nmodels):
res = pool.apply_async(func = simulation_worker, args = (i, ensembleModels[i], S, Smetab2rnx, E, Eini, X, Xini, enzymes, nsteps, enzymeLBs, enzymeUBs))
tmp.append(res)
pool.close()
pool.join()
for i, res in enumerate(tmp): tmp[i] = res.get()
# get results
results = {enzyme: [] for enzyme in enzymes}
for i in range(nmodels):
if tmp[i]:
for enzyme in enzymes:
results[enzyme].append(tmp[i][enzyme])
return results