-
Notifications
You must be signed in to change notification settings - Fork 2
/
fitting.py
661 lines (440 loc) · 12.9 KB
/
fitting.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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
'''
Builds the vectors and matrices used in the misfit cost function, utilized by
the optimization problem.
'''
from __future__ import division
import numpy as np
from data import kb
import constants
import structure
def find_weight(rules_and_weights, entry):
'''
Find the right weight for a data entry, given certain rules (filters) and
corresponding weights.
'''
for rule, weight in rules_and_weights:
if rule(entry):
return weight
else:
raise Exception('Entry {} was not assigned a weight.'.format(entry))
def field_value_rule(**fields_and_values):
'''
Convenience metafunction for generation a rule based on field values.
'''
def rule(entry):
return all(
hasattr(entry, field) and (getattr(entry, field) in values)
for field, values in fields_and_values.viewitems()
)
return rule
def build_fitting_tensors(*rules_and_weights):
'''
Builds standard fitting problem matrices, vectors, and metadata.
'''
if len(rules_and_weights) == 0:
rules_and_weights = ((lambda entry: True, 1.0),)
if any((weight < 0) for rule, weight in rules_and_weights):
raise Exception('Weights must be non-negative.')
# standard molar Gibbs energies of formation
gs_weights = []
gs_indices = []
gs_values = []
gs_entries = []
for entry in kb.standard_energy_of_formation:
try:
i = structure.parameters.index(
structure.GS.format(entry.compound)
)
except ValueError:
continue
w = find_weight(rules_and_weights, entry)
if w == 0:
continue
gs_weights.append(w)
v = entry.standard_energy_of_formation
gs_indices.append(i)
gs_values.append(v)
gs_entries.append(entry)
gs_weights = np.array(gs_weights)
gs_indices = np.array(gs_indices)
gs_values = np.array(gs_values)
gs_mat = np.zeros((gs_values.size, structure.n_parameters))
for i, j in enumerate(gs_indices):
gs_mat[i, j] = 1
gs_values = gs_values * gs_weights
gs_mat = gs_mat * gs_weights[:, None]
# molar Gibbs energies of log concentration
glc_weights = []
glc_indices = []
glc_values = []
glc_entries = []
for entry in kb.concentration:
try:
i = structure.parameters.index(
structure.GLC.format(entry.compound)
)
except ValueError:
continue
w = find_weight(rules_and_weights, entry)
if w == 0:
continue
glc_weights.append(w)
v = np.log(entry.concentration) * constants.RT
glc_indices.append(i)
glc_values.append(v)
glc_entries.append(entry)
glc_weights = np.array(glc_weights)
glc_indices = np.array(glc_indices)
glc_values = np.array(glc_values)
glc_mat = np.zeros((glc_values.size, structure.n_parameters))
for i, j in enumerate(glc_indices):
glc_mat[i, j] = 1
glc_values = glc_values * glc_weights
glc_mat = glc_mat * glc_weights[:, None]
# forward catalytic rates
kcat_f_weights = []
kcat_f_rows = []
kcat_f_values = []
kcat_f_entries = []
for entry in kb.forward_catalytic_rate:
if entry.reaction not in structure.reactions:
continue
w = find_weight(rules_and_weights, entry)
if w == 0:
continue
kcat_f_weights.append(w)
row = np.zeros(structure.n_parameters)
gt_ind = structure.parameters.index(
structure.GTE.format(entry.reaction)
)
row[gt_ind] = -1
for reactant in structure.reactants_by_reaction[entry.reaction]:
for s in xrange(reactant.stoichiometry):
gs_ind = structure.parameters.index(
structure.GS.format(
reactant.compound,
)
)
gb_ind = structure.parameters.index(
structure.GBER.format(
reactant.compound,
s+1,
reactant.reaction,
)
)
row[gs_ind] += 1
row[gb_ind] += 1
kcat_f_rows.append(row)
kcat_f_values.append(
constants.RT * np.log(entry.k_cat / constants.K_STAR)
)
kcat_f_entries.append(entry)
kcat_f_weights = np.array(kcat_f_weights)
kcat_f_values = np.array(kcat_f_values)
kcat_f_mat = np.zeros((len(kcat_f_rows), structure.n_parameters))
for i, r in enumerate(kcat_f_rows):
kcat_f_mat[i, :] += r
kcat_f_values = kcat_f_values * kcat_f_weights
kcat_f_mat = kcat_f_mat * kcat_f_weights[:, None]
# reverse catalytic rates
kcat_r_weights = []
kcat_r_rows = []
kcat_r_values = []
kcat_r_entries = []
for entry in kb.reverse_catalytic_rate:
if entry.reaction not in structure.reactions:
continue
w = find_weight(rules_and_weights, entry)
if w == 0:
continue
kcat_r_weights.append(w)
row = np.zeros(structure.n_parameters)
gt_ind = structure.parameters.index(
structure.GTE.format(entry.reaction)
)
row[gt_ind] = -1
for product in structure.products_by_reaction[entry.reaction]:
for s in xrange(product.stoichiometry):
gs_ind = structure.parameters.index(
structure.GS.format(
product.compound,
)
)
gb_ind = structure.parameters.index(
structure.GBEP.format(
product.compound,
s+1,
product.reaction,
)
)
row[gs_ind] += 1
row[gb_ind] += 1
kcat_r_rows.append(row)
kcat_r_values.append(
constants.RT * np.log(entry.k_cat / constants.K_STAR)
)
kcat_r_entries.append(entry)
kcat_r_weights = np.array(kcat_r_weights)
kcat_r_values = np.array(kcat_r_values)
kcat_r_mat = np.zeros((len(kcat_r_rows), structure.n_parameters))
for i, r in enumerate(kcat_r_rows):
kcat_r_mat[i, :] += r
kcat_r_values = kcat_r_values * kcat_r_weights
kcat_r_mat = kcat_r_mat * kcat_r_weights[:, None]
# forward saturation constants (reactants)
KM_f_weights = []
KM_f_rows = []
KM_f_values = []
KM_f_entries = []
for entry in kb.reactant_saturation:
if entry.reaction not in structure.reactions:
continue
w = find_weight(rules_and_weights, entry)
if w == 0:
continue
KM_f_weights.append(w)
gb_ind = structure.parameters.index(structure.GBER.format(
entry.compound,
entry.index,
entry.reaction,
))
row = np.zeros(structure.n_parameters)
row[gb_ind] = 1
KM_f_rows.append(row)
KM_f_values.append(constants.RT * np.log(entry.K_M))
KM_f_entries.append(entry)
KM_f_weights = np.array(KM_f_weights)
KM_f_values = np.array(KM_f_values)
KM_f_mat = np.zeros((len(KM_f_rows), structure.n_parameters))
for i, r in enumerate(KM_f_rows):
KM_f_mat[i] += r
KM_f_values = KM_f_values * KM_f_weights
KM_f_mat = KM_f_mat * KM_f_weights[:, None]
# reverse saturation constants (products)
KM_r_weights = []
KM_r_rows = []
KM_r_values = []
KM_r_entries = []
for entry in kb.product_saturation:
if entry.reaction not in structure.reactions:
continue
w = find_weight(rules_and_weights, entry)
if w == 0:
continue
KM_r_weights.append(w)
gb_ind = structure.parameters.index(structure.GBEP.format(
entry.compound,
entry.index,
entry.reaction
))
row = np.zeros(structure.n_parameters)
row[gb_ind] = 1
KM_r_rows.append(row)
KM_r_values.append(constants.RT * np.log(entry.K_M))
KM_r_entries.append(entry)
KM_r_weights = np.array(KM_r_weights)
KM_r_values = np.array(KM_r_values)
KM_r_mat = np.zeros((len(KM_r_rows), structure.n_parameters))
for i, r in enumerate(KM_r_rows):
KM_r_mat[i] += r
KM_r_values = KM_r_values * KM_r_weights
KM_r_mat = KM_r_mat * KM_r_weights[:, None]
# chemical equilibrium constants
Keq_weights = []
Keq_rows = []
Keq_values = []
Keq_entries = []
for entry in kb.equilibrium:
# if entry.reaction not in structure.reactions:
# continue
w = find_weight(rules_and_weights, entry)
if w == 0:
continue
Keq_weights.append(w)
row = np.zeros(structure.n_parameters)
for reactant in structure.reactants_by_reaction[entry.reaction]:
gs_ind = structure.parameters.index(structure.GS.format(
reactant.compound
))
row[gs_ind] += reactant.stoichiometry
for product in structure.products_by_reaction[entry.reaction]:
gs_ind = structure.parameters.index(structure.GS.format(
product.compound
))
row[gs_ind] -= product.stoichiometry
Keq_rows.append(row)
Keq_values.append(constants.RT * np.log(entry.equilibrium_constant))
Keq_entries.append(entry)
Keq_weights = np.array(Keq_weights)
Keq_values = np.array(Keq_values)
Keq_mat = np.zeros((len(Keq_rows), structure.n_parameters))
for i, r in enumerate(Keq_rows):
Keq_mat[i] += r
Keq_values = Keq_values * Keq_weights
Keq_mat = Keq_mat * Keq_weights[:, None]
fitting_values = np.concatenate([
gs_values,
glc_values,
kcat_f_values,
kcat_r_values,
KM_f_values,
KM_r_values,
Keq_values,
])
fitting_mat = np.concatenate([
gs_mat,
glc_mat,
kcat_f_mat,
kcat_r_mat,
KM_f_mat,
KM_r_mat,
Keq_mat,
])
fitting_entries = sum([
gs_entries,
glc_entries,
kcat_f_entries,
kcat_r_entries,
KM_f_entries,
KM_r_entries,
Keq_entries
], [])
return fitting_mat, fitting_values, fitting_entries
def build_upper_fitting_tensors(*rules_and_weights):
'''
Same as build_fitting_tensors, except these rules will only be used to
penalize for quantities above some threshold.
'''
if len(rules_and_weights) == 0:
rules_and_weights = ((lambda entry: True, 1.0),)
if any((weight < 0) for rule, weight in rules_and_weights):
raise Exception('Weights must be non-negative.')
# forward upper saturation limits (reactants)
sat_f_weights = []
sat_f_rows = []
sat_f_values = []
sat_f_entries = []
for entry in kb.upper_reactant_saturation_limit:
if entry.reaction not in structure.reactions:
continue
w = find_weight(rules_and_weights, entry)
if w == 0:
continue
sat_f_weights.append(w)
glc_ind = structure.parameters.index(
structure.GLC.format(entry.compound)
)
gb_ind = structure.parameters.index(structure.GBER.format(
entry.compound,
entry.index,
entry.reaction,
))
row = np.zeros(structure.n_parameters)
row[glc_ind] = +1
row[gb_ind] = -1
sat_f_rows.append(row)
sat_f_values.append(constants.RT * np.log(entry.ratio))
sat_f_entries.append(entry)
sat_f_weights = np.array(sat_f_weights)
sat_f_values = np.array(sat_f_values)
sat_f_mat = np.zeros((len(sat_f_rows), structure.n_parameters))
for i, r in enumerate(sat_f_rows):
sat_f_mat[i] += r
sat_f_values = sat_f_values * sat_f_weights
sat_f_mat = sat_f_mat * sat_f_weights[:, None]
# reverse upper saturation limits (products)
sat_r_weights = []
sat_r_rows = []
sat_r_values = []
sat_r_entries = []
for entry in kb.upper_product_saturation_limit:
if entry.reaction not in structure.reactions:
continue
w = find_weight(rules_and_weights, entry)
if w == 0:
continue
sat_r_weights.append(w)
glc_ind = structure.parameters.index(
structure.GLC.format(entry.compound)
)
gb_ind = structure.parameters.index(structure.GBEP.format(
entry.compound,
entry.index,
entry.reaction,
))
row = np.zeros(structure.n_parameters)
row[glc_ind] = +1
row[gb_ind] = -1
sat_r_rows.append(row)
sat_r_values.append(constants.RT * np.log(entry.ratio))
sat_r_entries.append(entry)
sat_r_weights = np.array(sat_r_weights)
sat_r_values = np.array(sat_r_values)
sat_r_mat = np.zeros((len(sat_r_rows), structure.n_parameters))
for i, r in enumerate(sat_r_rows):
sat_r_mat[i] += r
sat_r_values = sat_r_values * sat_r_weights
sat_r_mat = sat_r_mat * sat_r_weights[:, None]
fitting_values = np.concatenate([
sat_f_values,
sat_r_values
])
fitting_mat = np.concatenate([
sat_f_mat,
sat_r_mat
])
fitting_entries = sum([
sat_f_entries,
sat_r_entries
], [])
return fitting_mat, fitting_values, fitting_entries
def build_relative_fitting_tensor_sets(*rules_and_weights):
'''
Same as build_fitting_tensors, except these will be used to fit data with
some degree of freedom (e.g. a set of values on an arbitrary but shared
basis).
'''
if len(rules_and_weights) == 0:
rules_and_weights = ((lambda entry: True, 1.0),)
if any((weight < 0) for rule, weight in rules_and_weights):
raise Exception('Weights must be non-negative.')
# relative enzyme concentrations
relative_protein_count_sets = structure.gather(
kb.relative_protein_count,
'source'
)
tensor_sets = []
for source, entries in relative_protein_count_sets.viewitems():
gelc_weights = []
gelc_indices = []
gelc_values = []
gelc_entries = []
for entry in entries:
if entry.reaction not in structure.reactions:
continue
w = find_weight(rules_and_weights, entry)
if w == 0:
continue
gelc_weights.append(w)
i = structure.parameters.index(
structure.GELC.format(entry.reaction)
)
gelc_indices.append(i)
v = constants.RT * np.log(entry.count)
gelc_values.append(v)
gelc_entries.append(entry)
if len(gelc_values) == 0:
continue
gelc_weights = np.array(gelc_weights)
if not np.all(gelc_weights[0] == gelc_weights):
raise NotImplementedError(
'Non-trivial weighted relative values not implemented - need to develop the math and methods'
)
gelc_indices = np.array(gelc_indices)
gelc_values = np.array(gelc_values) * gelc_weights
gelc_mat = np.zeros((gelc_values.size, structure.n_parameters))
for (i, j) in enumerate(gelc_indices):
gelc_mat[i, j] = 1
gelc_mat = gelc_mat * gelc_weights[:, None]
tensor_sets.append((gelc_mat, gelc_values, gelc_entries))
return tensor_sets