generated from readthedocs/tutorial-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
inst_fit.py
618 lines (495 loc) · 21.2 KB
/
inst_fit.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
'''Define the InstFitter class.'''
__author__ = 'Chao Wu'
__date__ = '06/14/2022'
from functools import partial
from collections.abc import Iterable
from copy import deepcopy
from math import ceil
import numpy as np
from multiprocessing import Pool
from ..io.inputs import read_measurements_from_file, read_initial_values
from ..io.results import InstFitResults, InstFitMCResults
from .inst_simulate import InstSimulator
from .fit import Fitter
from ..solver.nlpsolver import InstMFAModel
from ..utils.progress import Progress
class InstFitter(Fitter, InstSimulator):
'''
Estimated fluxes are in the unit of umol/gCDW/s if concentrations in the unit of
umol/gCDW and timepoints in the unit of s.
'''
def set_measured_MDVs(self, fragmentid, timepoints, means, sds):
'''
Set measured MDVs at various timepoints.
Parameters
----------
fragmentid: str
Metabolite ID + '_' + atom NOs, e.g., 'Glu_12345'.
timepoints: float or list of float
Timepoint(s).
means: array or list of array
Mean of measured MDV(s). len(means) should be equal to len(timepoints).
sds: array or list of array
Standard deviation of measured MDV(s). len(sds) should be equal to len(timepoints).
'''
if not isinstance(timepoints, Iterable):
timepoints = [timepoints]
means = [means]
sds = [sds]
for timepoint, mean, sd in zip(timepoints, means, sds):
self.model.measured_inst_MDVs.setdefault(fragmentid, {})[timepoint] = [
np.array(mean),
np.array(sd)
]
if self.contexts:
context = self.contexts[-1]
context.add_undo(partial(self._unset_measured_MDVs, {fragmentid: timepoints}))
def set_measured_MDVs_from_file(self, file):
'''
Read measured MDVs at various timepoints from file.
Parameters
----------
file: file path
Path of tsv or excel file with fields "fragment_ID", "time", "mean" and "sd".
"fragment_ID" is metabolite ID + '_' + atom NOs, e.g., 'Glu_12345';
"time" is timepoint when MDVs are measured (while some timepoints could be missing);
"mean" and "sd" are the mean and standard deviation of MDV with element seperated by ','.
Header line starts with "#", and will be skiped.
'''
measMDVs = read_measurements_from_file(file, inst_data = True)
fragmentid_tpoints = {}
for [emuid, timepoint], [mean, sd] in measMDVs.iterrows():
timepoint = float(timepoint)
self.model.measured_inst_MDVs.setdefault(emuid, {})[timepoint] = [
np.array(list(map(float, mean.split(',')))),
np.array(list(map(float, sd.split(','))))
]
fragmentid_tpoints.setdefault(emuid, []).append(timepoint)
if self.contexts:
context = self.contexts[-1]
context.add_undo(partial(self._unset_measured_MDVs, fragmentid_tpoints))
def _unset_measured_MDVs(self, fragmentid_tpoints):
'''
Parameters
----------
fragmentid_tpoints: dict
measured MDV ID => list of timepoints.
'''
for fragmentid, timepoints in fragmentid_tpoints.items():
if fragmentid in self.model.measured_inst_MDVs:
for timepoint in timepoints:
if timepoint in self.model.measured_inst_MDVs[fragmentid]:
self.model.measured_inst_MDVs[fragmentid].pop(timepoint)
if not self.model.measured_inst_MDVs[fragmentid]:
self.model.measured_inst_MDVs.pop(fragmentid)
def set_concentration_bounds(self, metabid, bounds):
'''
Set lower and upper bounds for concentration in unit of umol/gCDW.
Parameters
----------
metabid: str or 'all'
Metabolite ID. If 'all', all concentrations will be set to the range.
bounds: 2-list
[lower bound, upper bound]. Lower bound is not allow to equal upper bound.
'''
bounds = list(map(float, bounds))
metabids = []
if bounds[0] < bounds[1]:
if metabid == 'all':
for metabid in self.model.metabolites:
self.model.concentrations_bounds[metabid] = [max(0.0, bounds[0]), bounds[1]]
metabids.append(metabid)
elif metabid in self.model.metabolites:
self.model.concentrations_bounds[metabid] = [max(0.0, bounds[0]), bounds[1]]
metabids = [metabid]
else:
raise ValueError(f'concentration range set to nonexistent metabolite {metabid}')
else:
raise ValueError('concentration lower bound should be less than upper bound')
if self.contexts:
context = self.contexts[-1]
context.add_undo(partial(self._unset_concentration_bounds, metabids))
def _set_default_concentration_bounds(self):
'''
This method assign bounds of [0.01, 100] (umol/gCDW) for concentrations
not set by set_concentration_bounds
'''
defBnds = [0.0001, 1]
metabids = []
for metabid in self.model.metabolites:
if metabid not in self.model.net_fluxes_bounds:
self.model.concentrations_bounds[metabid] = defBnds
metabids.append(metabid)
if self.contexts:
context = self.contexts[-1]
context.add_undo(partial(self._unset_concentration_bounds, metabids))
def _unset_concentration_bounds(self, metabids):
'''
Parameters
----------
metabids: str or list of str
Metabolite ID(s).
'''
if not isinstance(metabids, Iterable):
metabids = [metabids]
for metabid in metabids:
if metabid in self.model.concentrations_bounds:
self.model.concentrations_bounds.pop(metabid)
def _decompose_network(self, n_jobs):
'''
Parameters
----------
n_jobs: int
# of jobs to run in parallel.
'''
if not self.model.measured_inst_MDVs:
raise ValueError('call set_measured_MDV or set_measured_MDVs_from_file first')
if not self.model.EAMs:
if n_jobs <= 0:
raise ValueError('n_jobs should be a positive value')
else:
self.model.target_EMUs = list(self.model.measured_inst_MDVs.keys())
metabids = []
atom_nos = []
for emuid in self.model.target_EMUs:
metabid, atomNOs = emuid.split('_')
metabids.append(metabid)
atom_nos.append(atomNOs)
EAMs = self.model._decompose_network(
metabids, atom_nos,
lump = False,
n_jobs = n_jobs
)
for size, EAM in EAMs.items():
self.model.EAMs[size] = EAM
if self.contexts:
context = self.contexts[-1]
context.add_undo(self._unset_decomposition)
def _set_timepoints(self):
if not self.model.timepoints:
self.calculator._set_timepoints()
if self.contexts:
context = self.contexts[-1]
context.add_undo(self._unset_timepoints)
def _calculate_matrix_Ms_derivatives_p(self):
if not self.model.matrix_Ms_der_p:
self.calculator._calculate_matrix_Ms_derivatives_p()
if self.contexts:
context = self.contexts[-1]
context.add_undo(self._unset_matrix_Ms_derivatives_p)
def _unset_matrix_Ms_derivatives_p(self):
self.model.matrix_Ms_der_p.clear()
def _calculate_measured_inst_MDVs_inversed_covariance_matrix(self):
if not self.model.measured_inst_MDVs_inv_cov:
self.calculator._calculate_measured_inst_MDVs_inversed_covariance_matrix()
if self.contexts:
context = self.contexts[-1]
context.add_undo(self._unset_measured_inst_MDVs_inversed_covariance_matrix)
def _unset_measured_inst_MDVs_inversed_covariance_matrix(self):
self.model.measured_inst_MDVs_inv_cov = None
def _calculate_initial_matrix_Xs_derivatives_p(self):
if not self.model.initial_matrix_Xs_der_p:
self.calculator._calculate_initial_matrix_Xs_derivatives_p()
if self.contexts:
context = self.contexts[-1]
context.add_undo(self._unset_initial_matrix_Xs_derivatives_p)
def _unset_initial_matrix_Xs_derivatives_p(self):
self.model.initial_matrix_Xs_der_p.clear()
def _calculate_initial_matrix_Ys_derivatives_p(self):
if not self.model.initial_matrix_Ys_der_p:
self.calculator._calculate_initial_matrix_Ys_derivatives_p()
if self.contexts:
context = self.contexts[-1]
context.add_undo(self._unset_initial_matrix_Ys_derivatives_p)
def _unset_initial_matrix_Ys_derivatives_p(self):
self.model.initial_matrix_Ys_der_p.clear()
def _estimate_concentrations_range(self):
if not self.model.concentrations_range:
for metabid in self.model.concids:
self.model.concentrations_range[metabid] = self.model.concentrations_bounds[metabid]
if self.contexts:
context = self.contexts[-1]
context.add_undo(partial(self._unset_concentrations_range, self.model.concids))
def _unset_concentrations_range(self, metabids):
'''
Parameters
----------
metabids: str or list of str
Metabolite ID(s).
'''
if not isinstance(metabids, Iterable):
metabids = [metabids]
for metabid in metabids:
if metabid in self.model.concentrations_range:
self.model.concentrations_range.pop(metabid)
def prepare(self, dilution_from = None, n_jobs = 1):
'''
Parameters
----------
dilution_from: str or list of str
ID(s) of unlabeled (inactive) metabolite leading to dilution effect.
These metabolites have zero stoichiometric coefficients in reaction network.
n_jobs: int
If n_jobs > 1, decomposition job will run in parallel.
'''
self._decompose_network(n_jobs)
self._set_timepoints()
self._calculate_null_space()
self._calculate_transform_matrix()
self._lambdify_matrix_As_and_Bs()
self._calculate_matrix_As_and_Bs_derivatives_p('inst', n_jobs)
self._lambdify_matrix_Ms()
self._calculate_matrix_Ms_derivatives_p()
self._calculate_substrate_MDVs(dilution_from)
self._calculate_substrate_MDV_derivatives_p('inst', dilution_from)
self._calculate_measured_inst_MDVs_inversed_covariance_matrix()
self._calculate_measured_fluxes_inversed_covariance_matrix()
self._calculate_measured_fluxes_derivative_p('inst')
self._calculate_initial_matrix_Xs()
self._calculate_initial_matrix_Ys()
self._calculate_initial_matrix_Xs_derivatives_p()
self._calculate_initial_matrix_Ys_derivatives_p()
self._set_default_flux_bounds()
self._estimate_fluxes_range(self.model.unbalanced_metabolites)
self._set_default_concentration_bounds()
self._estimate_concentrations_range()
def _check_dependencies(self, fit_measured_fluxes):
'''
Parameters
----------
fit_measured_fluxes: bool
Whether to fit measured fluxes.
'''
if not self.model.net_fluxes_bounds:
raise ValueError('call set_flux_bounds first')
if not self.model.concentrations_bounds:
raise ValueError('call set_concentration_bounds first')
if not self.model.measured_inst_MDVs:
raise ValueError('call call set_measured_MDV or set_measured_MDVs_from_file first')
if not self.model.measured_fluxes:
raise ValueError('call set_measured_flux or set_measured_fluxes_from_file first')
if not self.model.labeling_strategy:
raise ValueError('call labeling_strategy first')
checklist = [
not self.model.target_EMUs,
self.model.transform_matrix is None,
self.model.null_space is None,
self.model.measured_inst_MDVs_inv_cov is None,
not self.model.matrix_As,
not self.model.matrix_Bs,
not self.model.matrix_Ms,
not self.model.matrix_As_der_p,
not self.model.matrix_Bs_der_p,
not self.model.matrix_Ms_der_p,
not self.model.substrate_MDVs,
not self.model.substrate_MDVs_der_p,
self.model.measured_fluxes_der_p is None,
not self.model.initial_matrix_Xs,
not self.model.initial_matrix_Ys,
not self.model.initial_matrix_Xs_der_p,
not self.model.initial_matrix_Ys_der_p,
not self.model.timepoints
]
if fit_measured_fluxes:
checklist.append(self.model.measured_fluxes_inv_cov is None)
if any(checklist):
raise ValueError('call prepare first')
def solve(
self,
fit_measured_fluxes = True,
ini_fluxes = None,
ini_concs = None,
solver = 'slsqp',
tol = 1e-6,
max_iters = 400,
show_progress = True
):
'''
Parameters
----------
fit_measured_fluxes: bool
Whether to fit measured fluxes.
ini_fluxes: ser or file in .tsv or .xlsx
Initial values of net fluxes.
ini_concs: ser or file in .tsv or .xlsx
Initial values of concentrations.
solvor: {"slsqp", "ralg"}
* If "slsqp", scipy.optimize.minimze will be used.
* If "ralg", openopt NLP solver will be used.
tol: float
Tolerance for termination.
max_iters: int
Maximum # of iterations.
show_progress: bool
Whether to show the progress bar.
'''
self._check_dependencies(fit_measured_fluxes)
if ini_fluxes is not None:
iniFluxes = read_initial_values(ini_fluxes, self.model.netfluxids)
else:
iniFluxes = ini_fluxes
if ini_concs is not None:
iniConcs = read_initial_values(ini_concs, self.model.concids)
else:
iniConcs = ini_concs
optModel = InstMFAModel(self.model, fit_measured_fluxes, solver)
optModel.build_objective()
optModel.build_gradient()
optModel.build_flux_and_conc_bound_constraints()
optModel.build_initial_flux_and_conc_values(
ini_netfluxes = iniFluxes,
ini_concs = iniConcs
)
with Progress('INST fitting', silent = not show_progress):
res = optModel.solve_flux(tol, max_iters)
return InstFitResults(
*res[:8],
deepcopy(res[8]),
res[9],
deepcopy(res[10]),
*res[11:]
)
def _solve_with_confidence_intervals(
self,
fit_measured_fluxes,
ini_fluxes,
ini_concs,
solver,
tol,
max_iters,
nruns
):
'''
Parameters
----------
fit_measured_fluxes: bool
Whether to fit measured fluxes.
ini_fluxes: ser or file in .tsv or .xlsx or None
Initial values of net fluxes.
ini_concs: ser or file in .tsv or .xlsx or None
Initial values of concentrations.
solvor: {"slsqp", "ralg"}
* If "slsqp", scipy.optimize.minimze will be used.
* If "ralg", openopt NLP solver will be used.
tol: float
Tolerance for termination.
max_iters: int
Maximum # of iterations.
nruns: int
# of estimations in each worker.
'''
import platform
if platform.system() == 'Linux':
import os
os.sched_setaffinity(os.getpid(), range(os.cpu_count()))
self._lambdify_matrix_As_and_Bs()
self._lambdify_matrix_Ms()
if ini_fluxes is not None:
iniFluxes = read_initial_values(ini_fluxes, self.model.netfluxids)
else:
iniFluxes = ini_fluxes
# if ini_concs is not None:
# iniConcs = read_initial_values(ini_concs, self.model.concids)
# else:
# iniConcs = ini_concs
optTotalfluxesSet = []
optNetfluxesSet = []
optConcsSet = []
for _ in range(nruns):
self.calculator._generate_random_fluxes()
self.calculator._generate_random_inst_MDVs()
optModel = InstMFAModel(self.model, fit_measured_fluxes, solver)
optModel.build_objective()
optModel.build_gradient()
optModel.build_flux_and_conc_bound_constraints()
optModel.build_initial_flux_and_conc_values(ini_netfluxes = iniFluxes)
try:
while True:
(optTotalfluxes,
optNetfluxes,
optConcs,
*_,
isSuccess
) = optModel.solve_flux(tol, max_iters)
if isSuccess:
break
except:
continue
optTotalfluxesSet.append(optTotalfluxes)
optNetfluxesSet.append(optNetfluxes)
optConcsSet.append(optConcs)
self.calculator._reset_measured_fluxes()
self.calculator._reset_measured_inst_MDVs()
return optTotalfluxesSet, optNetfluxesSet, optConcsSet
def solve_with_confidence_intervals(
self,
fit_measured_fluxes = True,
ini_fluxes = None,
ini_concs = None,
solver = 'slsqp',
tol = 1e-6,
max_iters = 400,
n_runs = 100,
n_jobs = 1,
show_progress = True
):
'''
Parameters
----------
fit_measured_fluxes: bool
Whether to fit measured fluxes.
ini_fluxes: ser or file in .tsv or .xlsx
Initial values of net fluxes.
ini_concs: ser or file in .tsv or .xlsx
Initial values of concentrations.
solvor: {"slsqp", "ralg"}
* If "slsqp", scipy.optimize.minimze will be used.
* If "ralg", openopt NLP solver will be used.
tol: float
Tolerance for termination.
max_iters: int
Max # of iterations.
show_progress: bool
Whether to show the progress bar.
n_runs: int
# of runs to estimate confidence intervals.
n_jobs: int
# of jobs to run in parallel.
'''
self._check_dependencies(fit_measured_fluxes)
self._unset_matrix_As_and_Bs()
self._unset_matrix_Ms()
if n_runs <= n_jobs:
nruns_worker = 1
else:
nruns_worker = ceil(n_runs/n_jobs)
pool = Pool(processes = n_jobs)
with Progress('INST fitting with CIs', silent = not show_progress):
resSet = []
for _ in range(n_jobs):
res = pool.apply_async(
func = self._solve_with_confidence_intervals,
args = (
fit_measured_fluxes,
ini_fluxes,
ini_concs,
solver,
tol,
max_iters,
nruns_worker
)
)
resSet.append(res)
pool.close()
pool.join()
resSet = [res.get() for res in resSet]
totalFluxesSet = []
netFluxesSet = []
concsSet = []
for totalFluxesSubset, netFluxesSubset, concsSubset in resSet:
totalFluxesSet.extend(totalFluxesSubset)
netFluxesSet.extend(netFluxesSubset)
concsSet.extend(concsSubset)
self._lambdify_matrix_As_and_Bs()
self._lambdify_matrix_Ms()
return InstFitMCResults(totalFluxesSet, netFluxesSet, concsSet)