-
Notifications
You must be signed in to change notification settings - Fork 0
/
Opt9d_calc.py
146 lines (135 loc) · 5.89 KB
/
Opt9d_calc.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Model-based optimization using the entire parameter ensemble.
Model: GDP-Fucose_v7XGSK_PE9XGSK_with_Opt9d_setup.cps
Method: Genetic Algorithm, default COPASI settings
The optimization is set in the COPASI model file (.cps)
Approach: Calculate the optimization result for each of the
estimated parameter sets from the random parameter sampling
(100 times). Export the result as serialized pickle (.pkl) file.
Package: basiCO - simplified Copasi Python API
<https://github.com/copasi/basico>
"""
import os
from basico import *
import matplotlib.pyplot as plt
import pandas as pd
from tqdm import tqdm
import pickle
import seaborn as sns
model = load_model('GDP-Fucose_v7XGSK_PE9XGSK_with_Opt9d_setup.cps')
# optimization parameters (= optimization variables):
# 1 0.00021 ≤ [E_FKP]_0 ≤ 0.05; Start Value = 0.006625024
# 2 0.002413333 ≤ [E_GMPK]_0 ≤ 0.50706; Start Value = 0.006358087
# 3 0.00319 ≤ [E_PPA]_0 ≤ 0.22363; Start Value = 0.03106716
# 4 0.00018 ≤ [E_PPK3]_0 ≤ 0.0129; Start Value = 0.002878526
# 5 0 ≤ [ATP]_0 ≤ 20; Start Value = 6
# 6 0 ≤ [Fuc]_0 ≤ 40; Start Value = 25.7
# 7 0 ≤ [GMP]_0 ≤ 40; Start Value = 25.7
# constraints: (# # = was not used)
# #1 0 ≤ Values[Enzyme Weighted Cost] ≤ XXX
# # -> global quantity 'Enzyme Weighted Cost': assignment:
# # 200.449*{[E_FKP]_0}*105.66+58.64511*{[E_GMPK]_0}*23.592+162.4384*{[E_PPA]_0}*19.313+54
# # 1.7541*{[E_PPK3]_0}*34.74; Initial Value: XXX
# #2 0 ≤ Values[Substrate Weighted Cost] ≤ XX
# # -> global quantity 'Substrate Weighted Cost': assignment:
# # 0.25*{[GMP]_0}*0.36322+5.66*{[Fuc]_0}*0.16416+0.51*{[ATP]_0}*0.50718; Initial
# # Value: XX
# #3 NO YIELD CONSTRAINT
# #4 X ≤ Values[GDP-Fucose at 24h] ≤ inf
# # -> global quantity 'GDP-Fucose at 24h', value: X <-> event: 'Save GDP-Fucose at 24h'
# with trigger: {Time} == 24, target: Values[GDP-Fucose at 24h], expression: #{[GDP_Fucose]}
# 5 0 ≤ [ADP] ≤ inf
# 6 0 ≤ [ATP] ≤ inf
# 7 0 ≤ [Fuc1P] ≤ inf
# 8 0 ≤ [Fuc] ≤ inf
# 9 0 ≤ [GDP] ≤ inf
# 10 0 ≤ [GDP_Fucose] ≤ inf
# 11 0 ≤ [GMP] ≤ inf
# 12 0 ≤ [GTP] ≤ inf
# 13 0 ≤ [PP] ≤ inf
# 14 0 ≤ [P] ≤ inf
# PREPARATION OF VARIABLES
# read the data of the parameter estimation sampling
# (the first column is all zeros, parameter columns start at .iloc[:,1])
path = os.getcwd()
parent_dir_path = os.path.abspath(os.path.join(path, os.pardir))
fits_data = pd.read_csv(parent_dir_path + '\\' + 'parameter_ensemble' + '\\' + 'sampling_output_EvoStrat_100runs.csv')
# setup optimization problem (it's already set in the .cps file but basiCO needs it here as well)
opt_vars = [{'name': '[E_FKP]_0',
'lower': 0.00021,
'upper': 0.05,
'start': 0.006625024},
{'name': '[E_GMPK]_0',
'lower': 0.002413333,
'upper': 0.50706,
'start': 0.006358087},
{'name': '[E_PPA]_0',
'lower': 0.00319,
'upper': 0.22363,
'start': 0.03106716},
{'name': '[E_PPK3]_0',
'lower': 0.00018,
'upper': 0.0129,
'start': 0.002878526},
{'name': '[ATP]_0',
'lower': 0,
'upper': 20,
'start': 6},
{'name': '[Fuc]_0',
'lower': 0,
'upper': 40,
'start': 25.7},
{'name': '[GMP]_0',
'lower': 0,
'upper': 40,
'start': 25.7}]
set_opt_parameters(opt_vars)
# there can be no spaces in the Copasi object name!
set_objective_function(expression='Values[Cost_per_Product]', minimize=True)
# constraints are set in the .cps file
set_opt_settings(settings={
'subtask': T.TIME_COURSE,
'method': {
'name': PE.GENETIC_ALGORITHM
}})
# OPTIMIZATION LOOP
# setup for repeated optimizations with all different estimated parameter sets
# from random parameter sampling result
opt_results = list()
# loop over result data frame (every row = one parameter set)
for index, param_set in tqdm(fits_data.iterrows()):
# current list of parameter values
curr_param_set_vals = list(param_set)
# current list of parameter names
curr_param_set_names = list(param_set.keys())
# list() to only get content (values or names) of pandas series param_set
# .keys() to get names of values in pandas series param_set
# trim both lists (get rid of first entries = name and last entries = obj_val of fit)
curr_param_set_vals = curr_param_set_vals[1:-1]
curr_param_set_names = curr_param_set_names[1:-1]
# set model parameter values to current estimated parameters from rand sampling result
for i in range(len(list(curr_param_set_vals))):
set_reaction_parameters(name = curr_param_set_names[i], value = curr_param_set_vals[i])
# optimize the model with these updated parameter values
opt_result = run_optimization() # opt_result is a pandas.core.frame.DataFrame
opt_stats = get_opt_statistic() # opt_stats is a dict
# get list of optimization variable start values
opt_vars_start_vals = [opt_vars[0]['start'],
opt_vars[1]['start'],
opt_vars[2]['start'],
opt_vars[3]['start'],
opt_vars[4]['start'],
opt_vars[5]['start'],
opt_vars[6]['start']]
# add start values of optimization variables to result data frame as column 3
opt_result.insert(2, "start", opt_vars_start_vals, True)
# store the optimization result dataframe and the opt_stats dict as a list
opt_results.append([opt_result, opt_stats])
# OUTPUT
# export opt_results as python object
file_name = "GDP-Fucose_v7XGSK_PE9XGSK_Opt9d_RandParamSampl_EvoStrat100x_list_res_stats.pkl"
open_file = open(file_name, "wb")
pickle.dump(opt_results, open_file)
open_file.close()