-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.py
408 lines (334 loc) · 18.5 KB
/
core.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 5 23:56:16 2019
@author: kirichoi
"""
import os, sys
import tellurium as te
import roadrunner
import numpy as np
import antimony
import scipy.optimize
import networkGenerator as ng
import time
import copy
def f1(k_list, *args):
global counts
global countf
args[0].reset()
args[0].setValues(args[0].getGlobalParameterIds(), k_list)
try:
args[0].steadyStateApproximate()
objCCC = args[0].getScaledConcentrationControlCoefficientMatrix()
objCCC[np.abs(objCCC) < 1e-12] = 0 # Set small values to zero
if np.isnan(objCCC).any():
dist_obj = 10000
else:
if args[3]:
objFlux = args[0].getReactionRates()
objFlux[np.abs(objFlux) < 1e-12] = 0 # Set small values to zero
# objFCC = args[0].getScaledFluxControlCoefficientMatrix()
# objFCC[np.abs(objFCC) < 1e-12] = 0 # Set small values to zero
objCCC_row = objCCC.rownames
objCCC_col = objCCC.colnames
objCCC = objCCC[np.argsort(objCCC_row)]
objCCC = objCCC[:,np.argsort(objCCC_col)]
if args[3]:
objFlux = objFlux[np.argsort(objCCC_col)]
dist_obj = (((np.linalg.norm(args[1] - objCCC)) + (np.linalg.norm(args[2] - objFlux))) *
((1 + np.sum(np.equal(np.sign(np.array(args[1])), np.sign(np.array(objCCC))))) +
(1 + np.sum(np.equal(np.sign(np.array(args[2])), np.sign(np.array(objFlux)))))))
else:
dist_obj = ((np.linalg.norm(args[1] - objCCC))*(1 +
np.sum(np.not_equal(np.sign(np.array(args[1])),
np.sign(np.array(objCCC))))))
except:
countf += 1
dist_obj = 10000
counts += 1
return dist_obj
def callbackF(X, convergence=0.):
global counts
global countf
print(str(counts) + ", " + str(countf))
return False
def initialize(Parameters):
global countf
global counts
numBadModels = 0
numGoodModels = 0
numIter = 0
ens_dist = np.empty(Parameters.ens_size)
ens_model = np.empty(Parameters.ens_size, dtype='object')
ens_rl = np.empty(Parameters.ens_size, dtype='object')
rl_track = []
rl_track.append(Parameters.knownReactionList)
# Initial Random generation
while (numGoodModels < Parameters.ens_size):
# Ensure no redundant model
rl = ng.generateReactionList(Parameters)
st = ng.getFullStoichiometryMatrix(rl, Parameters.ns).tolist()
stt = ng.removeBoundaryNodes(np.array(st))
while rl in rl_track:
rl = ng.generateReactionList(Parameters)
st = ng.getFullStoichiometryMatrix(rl, Parameters.ns).tolist()
stt = ng.removeBoundaryNodes(np.array(st))
antStr = ng.generateAntimony(Parameters.realFloatingIds, Parameters.realBoundaryIds, stt[1],
stt[2], rl, boundary_init=Parameters.realBoundaryVal)
try:
r = te.loada(antStr)
counts = 0
countf = 0
r.steadyStateApproximate()
p_bound = ng.generateParameterBoundary(r.getGlobalParameterIds())
res = scipy.optimize.differential_evolution(f1,
args=(r, Parameters.realConcCC, Parameters.realFlux, Parameters.FLUX),
bounds=p_bound,
maxiter=Parameters.optiMaxIter,
tol=Parameters.optiTol,
polish=Parameters.optiPolish,
seed=Parameters.r_seed)
if not res.success:
numBadModels += 1
else:
# TODO: Might be able to cut the bottom part by simply using
# the obj func value from optimizer
r = te.loada(antStr)
r.setValues(r.getGlobalParameterIds(), res.x)
r.steadyStateApproximate()
SS_i = r.getFloatingSpeciesConcentrations()
r.steadyStateApproximate()
if np.any(SS_i < 1e-5) or np.any(SS_i > 1e5):
numBadModels += 1
else:
concCC_i = r.getScaledConcentrationControlCoefficientMatrix()
if Parameters.FLUX:
flux_i = r.getReactionRates()
if np.isnan(concCC_i).any():
numBadModels += 1
else:
concCC_i[np.abs(concCC_i) < 1e-12] = 0 # Set small values to zero
if Parameters.FLUX:
flux_i[np.abs(flux_i) < 1e-12] = 0 # Set small values to zero
concCC_i_row = concCC_i.rownames
concCC_i_col = concCC_i.colnames
concCC_i = concCC_i[np.argsort(concCC_i_row)]
concCC_i = concCC_i[:,np.argsort(concCC_i_col)]
if Parameters.FLUX:
flux_i = flux_i[np.argsort(concCC_i_col)]
dist_i = (((np.linalg.norm(Parameters.realConcCC - concCC_i)) +
(np.linalg.norm(Parameters.realFlux - flux_i))) *
((1 + np.sum(np.not_equal(np.sign(np.array(Parameters.realConcCC)),
np.sign(np.array(concCC_i))))) +
(1 + np.sum(np.not_equal(np.sign(np.array(Parameters.realFlux)),
np.sign(np.array(flux_i)))))))
else:
dist_i = ((np.linalg.norm(Parameters.realConcCC - concCC_i))*(1 +
np.sum(np.not_equal(np.sign(np.array(Parameters.realConcCC)),
np.sign(np.array(concCC_i))))))
ens_dist[numGoodModels] = dist_i
r.reset()
ens_model[numGoodModels] = r.getAntimony(current=True)
ens_rl[numGoodModels] = rl
rl_track.append(rl)
numGoodModels = numGoodModels + 1
except:
numBadModels = numBadModels + 1
antimony.clearPreviousLoads()
numIter = numIter + 1
if int(numIter/1000) == (numIter/1000):
print("Number of iterations = " + str(numIter))
if int(numIter/10000) == (numIter/10000):
print("Number of good models = " + str(numGoodModels))
print("In generation: 1")
print("Number of total iterations = " + str(numIter))
print("Number of bad models = " + str(numBadModels))
return ens_dist, ens_model, ens_rl, rl_track
def mutate_and_evaluate(Parameters, listantStr, listdist, listrl, rl_track):
global countf
global counts
eval_dist = np.empty(Parameters.mut_size)
eval_model = np.empty(Parameters.mut_size, dtype='object')
eval_rl = np.empty(Parameters.mut_size, dtype='object')
for m in Parameters.mut_range:
o = 0
rl = ng.generateMutation(Parameters, listrl[m], listantStr[m])
st = ng.getFullStoichiometryMatrix(rl, Parameters.ns).tolist()
stt = ng.removeBoundaryNodes(np.array(st))
while ((rl in rl_track) and (o < Parameters.maxIter_mut)):
rl = ng.generateMutation(Parameters, listrl[m], listantStr[m])
st = ng.getFullStoichiometryMatrix(rl, Parameters.ns).tolist()
stt = ng.removeBoundaryNodes(np.array(st))
o += 1
if o >= Parameters.maxIter_mut:
eval_dist[m] = listdist[m]
eval_model[m] = listantStr[m]
eval_rl[m] = listrl[m]
else:
antStr = ng.generateAntimony(Parameters.realFloatingIds, Parameters.realBoundaryIds,
stt[1], stt[2], rl,
boundary_init=Parameters.realBoundaryVal)
try:
r = te.loada(antStr)
r.steadyStateApproximate()
p_bound = ng.generateParameterBoundary(r.getGlobalParameterIds())
res = scipy.optimize.differential_evolution(f1,
args=(r, Parameters.realConcCC, Parameters.realFlux, Parameters.FLUX),
bounds=p_bound,
maxiter=Parameters.optiMaxIter,
tol=Parameters.optiTol,
polish=Parameters.optiPolish,
seed=Parameters.r_seed)
if not res.success:
eval_dist[m] = listdist[m]
eval_model[m] = listantStr[m]
eval_rl[m] = listrl[m]
else:
r = te.loada(antStr)
r.setValues(r.getGlobalParameterIds(), res.x)
r.steadyStateApproximate()
SS_i = r.getFloatingSpeciesConcentrations()
r.steadyStateApproximate()
if np.any(SS_i < 1e-5) or np.any(SS_i > 1e5):
eval_dist[m] = listdist[m]
eval_model[m] = listantStr[m]
eval_rl[m] = listrl[m]
else:
concCC_i = r.getScaledConcentrationControlCoefficientMatrix()
if Parameters.FLUX:
flux_i = r.getReactionRates()
if np.isnan(concCC_i).any():
eval_dist[m] = listdist[m]
eval_model[m] = listantStr[m]
eval_rl[m] = listrl[m]
else:
concCC_i[np.abs(concCC_i) < 1e-12] = 0 # Set small values to zero
if Parameters.FLUX:
flux_i[np.abs(flux_i) < 1e-12] = 0 # Set small values to zero
concCC_i_row = concCC_i.rownames
concCC_i_col = concCC_i.colnames
concCC_i = concCC_i[np.argsort(concCC_i_row)]
concCC_i = concCC_i[:,np.argsort(concCC_i_col)]
if Parameters.FLUX:
flux_i = flux_i[np.argsort(concCC_i_col)]
dist_i = (((np.linalg.norm(Parameters.realConcCC - concCC_i)) +
(np.linalg.norm(Parameters.realFlux - flux_i))) *
((1 + np.sum(np.not_equal(np.sign(np.array(Parameters.realConcCC)),
np.sign(np.array(concCC_i))))) +
(1 + np.sum(np.not_equal(np.sign(np.array(Parameters.realFlux)),
np.sign(np.array(flux_i)))))))
else:
dist_i = ((np.linalg.norm(Parameters.realConcCC - concCC_i))*(1 +
np.sum(np.not_equal(np.sign(np.array(Parameters.realConcCC)),
np.sign(np.array(concCC_i))))))
if dist_i < listdist[m]:
eval_dist[m] = dist_i
r.reset()
eval_model[m] = r.getAntimony(current=True)
eval_rl[m] = rl
rl_track.append(rl)
else:
eval_dist[m] = listdist[m]
eval_model[m] = listantStr[m]
eval_rl[m] = listrl[m]
except:
eval_dist[m] = listdist[m]
eval_model[m] = listantStr[m]
eval_rl[m] = listrl[m]
antimony.clearPreviousLoads()
return eval_dist, eval_model, eval_rl, rl_track
def random_gen(Parameters, listAntStr, listDist, listrl, rl_track):
global countf
global counts
rndSize = len(listDist)
rnd_dist = np.empty(rndSize)
rnd_model = np.empty(rndSize, dtype='object')
rnd_rl = np.empty(rndSize, dtype='object')
for l in range(rndSize):
d = 0
rl = ng.generateReactionList(Parameters)
st = ng.getFullStoichiometryMatrix(rl, Parameters.ns).tolist()
stt = ng.removeBoundaryNodes(np.array(st))
# Ensure no redundant models
while ((rl in rl_track) and (d < Parameters.maxIter_gen)):
rl = ng.generateReactionList(Parameters)
st = ng.getFullStoichiometryMatrix(rl, Parameters.ns).tolist()
stt = ng.removeBoundaryNodes(np.array(st))
d += 1
if d >= Parameters.maxIter_gen:
rnd_dist[l] = listDist[l]
rnd_model[l] = listAntStr[l]
rnd_rl[l] = listrl[l]
else:
antStr = ng.generateAntimony(Parameters.realFloatingIds, Parameters.realBoundaryIds,
stt[1], stt[2], rl, boundary_init=Parameters.realBoundaryVal)
try:
r = te.loada(antStr)
r.steadyStateApproximate()
p_bound = ng.generateParameterBoundary(r.getGlobalParameterIds())
res = scipy.optimize.differential_evolution(f1,
args=(r, Parameters.realConcCC, Parameters.realFlux, Parameters.FLUX),
bounds=p_bound,
maxiter=Parameters.optiMaxIter,
tol=Parameters.optiTol,
polish=Parameters.optiPolish,
seed=Parameters.r_seed)
# Failed to find solution
if not res.success:
rnd_dist[l] = listDist[l]
rnd_model[l] = listAntStr[l]
rnd_rl[l] = listrl[l]
else:
r = te.loada(antStr)
r.setValues(r.getGlobalParameterIds(), res.x)
r.steadyStateApproximate()
SS_i = r.getFloatingSpeciesConcentrations()
r.steadyStateApproximate()
if np.any(SS_i < 1e-5) or np.any(SS_i > 1e5):
rnd_dist[l] = listDist[l]
rnd_model[l] = listAntStr[l]
rnd_rl[l] = listrl[l]
else:
concCC_i = r.getScaledConcentrationControlCoefficientMatrix()
if Parameters.FLUX:
flux_i = r.getReactionRates()
if np.isnan(concCC_i).any():
rnd_dist[l] = listDist[l]
rnd_model[l] = listAntStr[l]
rnd_rl[l] = listrl[l]
else:
concCC_i[np.abs(concCC_i) < 1e-12] = 0 # Set small values to zero
if Parameters.FLUX:
flux_i[np.abs(flux_i) < 1e-12] = 0 # Set small values to zero
concCC_i_row = concCC_i.rownames
concCC_i_col = concCC_i.colnames
concCC_i = concCC_i[np.argsort(concCC_i_row)]
concCC_i = concCC_i[:,np.argsort(concCC_i_col)]
if Parameters.FLUX:
flux_i = flux_i[np.argsort(concCC_i_col)]
dist_i = (((np.linalg.norm(Parameters.realConcCC - concCC_i)) +
(np.linalg.norm(Parameters.realFlux - flux_i))) *
((1 + np.sum(np.not_equal(np.sign(np.array(Parameters.realConcCC)),
np.sign(np.array(concCC_i))))) +
(1 + np.sum(np.not_equal(np.sign(np.array(Parameters.realFlux)),
np.sign(np.array(flux_i)))))))
else:
dist_i = ((np.linalg.norm(Parameters.realConcCC - concCC_i))*(1 +
np.sum(np.not_equal(np.sign(np.array(Parameters.realConcCC)),
np.sign(np.array(concCC_i))))))
if dist_i < listDist[l]:
rnd_dist[l] = dist_i
r.reset()
rnd_model[l] = r.getAntimony(current=True)
rnd_rl[l] = rl
rl_track.append(rl)
else:
rnd_dist[l] = listDist[l]
rnd_model[l] = listAntStr[l]
rnd_rl[l] = listrl[l]
except:
rnd_dist[l] = listDist[l]
rnd_model[l] = listAntStr[l]
rnd_rl[l] = listrl[l]
antimony.clearPreviousLoads()
return rnd_dist, rnd_model, rnd_rl, rl_track