-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathgeneopt.py
More file actions
270 lines (213 loc) · 9.44 KB
/
geneopt.py
File metadata and controls
270 lines (213 loc) · 9.44 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
# Copyright (C) 2019- Centre of Biological Engineering,
# University of Minho, Portugal
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Author: Vitor Pereira
CB model optimization test set that use the JMetalPy EA module
"""
import os
from collections import OrderedDict
from time import time
from reframed.io.sbml import load_cbmodel
from mewpy.optimization import EA, set_default_engine
from mewpy.optimization.evaluation import WYIELD, BPCY, ModificationType
from mewpy.simulation import SimulationMethod, get_simulator
ITERATIONS = 600
set_default_engine('jmetal')
def load_ec():
"""Loads a configuration for optimizations on E.coli model iJO1366SL,
including the model, environmental conditions, non targets for modifications,
the biomass equation ID, and wild type reference flux values.
Returns: A dictionary containing the configuration.
"""
DIR = os.path.dirname(os.path.realpath(__file__))
PATH = os.path.join(DIR, '../models/ec/')
DATA_FILE = os.path.join(PATH, "iJO1366SL.xml")
NON_TARGET_FILE = os.path.join(
PATH, "nontargets#RK#iJO1366SL#[lim-aerobic#glucose].txt")
BIOMASS_ID = 'R_Ec_biomass_iJO1366_core_53p95M'
O2 = 'R_EX_o2_LPAREN_e_RPAREN_'
GLC = 'R_EX_glc_LPAREN_e_RPAREN_'
model = load_cbmodel(DATA_FILE, flavor='cobra')
old_obj = model.get_objective().keys()
obj = {key: 0 for key in old_obj if key != BIOMASS_ID}
obj[BIOMASS_ID] = 1
model.set_objective(obj)
non_target = [O2, GLC, 'R_ATPM']
with open(NON_TARGET_FILE) as f:
line = f.readline()
while line:
non_target.append(line.strip())
line = f.readline()
envcond = OrderedDict()
envcond.update({GLC: (-10.0, 100000.0), O2: (-9.66, 100000.0)})
simulation = get_simulator(model, envcond=envcond)
res = simulation.simulate(method=SimulationMethod.pFBA)
reference = res.fluxes
return {'model': model, 'biomass': BIOMASS_ID, 'envcond': envcond, 'reference': reference, 'non_target': non_target}
def load_ec2():
"""Loads a configuration for optimizations on E.coli model iML1515,
including the model, environmental conditions, non targets for modifications,
the biomass equation ID, and wild type reference flux values.
Returns: A dictionary constaining the configuration.
"""
DIR = os.path.dirname(os.path.realpath(__file__))
PATH = os.path.join(DIR, '../models/ec/')
DATA_FILE = os.path.join(PATH, "iML1515.xml")
NON_TARGET_FILE = os.path.join(
PATH, "nontargets#RK#iJO1366SL#[lim-aerobic#glucose].txt")
BIOMASS_ID = 'R_BIOMASS_Ec_iML1515_core_75p37M'
O2 = 'R_EX_o2_e'
GLC = 'R_EX_glc__D_e'
model = load_cbmodel(DATA_FILE, flavor='cobra')
old_obj = model.get_objective().keys()
obj = {key: 0 for key in old_obj if key != BIOMASS_ID}
obj[BIOMASS_ID] = 1
model.set_objective(obj)
non_target = [O2, GLC, 'R_ATPM']
with open(NON_TARGET_FILE) as f:
line = f.readline()
while line:
non_target.append(line.strip())
line = f.readline()
envcond = OrderedDict()
envcond.update({GLC: (-10.0, 100000.0), O2: (-9.66, 100000.0)})
simulation = get_simulator(model, envcond=envcond)
res = simulation.simulate(method=SimulationMethod.pFBA)
reference = res.fluxes
res = simulation.simulate()
print(res)
return {'model': model, 'biomass': BIOMASS_ID, 'envcond': envcond, 'reference': reference, 'non_target': non_target}
def load_yeast():
"""Loads a configuration for optimizations on yeast model iMM904SL_v6,
including the model, environmental conditions, non targets for modifications,
the biomass equation ID, and wild type reference flux values.
Returns: A dictionary constaining the configuration.
"""
DIR = os.path.dirname(os.path.realpath(__file__))
PATH = os.path.join(DIR, '../models/yeast/')
DATA_FILE = os.path.join(PATH, "iMM904SL_v6.xml")
BIOMASS_ID = 'R_biomass_SC5_notrace'
O2 = 'R_EX_o2_e_'
GLC = 'R_EX_glc_e_'
model = load_cbmodel(DATA_FILE, flavor='cobra')
old_obj = model.get_objective().keys()
obj = {key: 0 for key in old_obj if key != BIOMASS_ID}
obj[BIOMASS_ID] = 1
model.set_objective(obj)
envcond = OrderedDict()
envcond.update({GLC: (-10.0, 999999.0), O2: (-12.25, 100000.0)})
simulation = get_simulator(model, envcond=envcond)
res = simulation.simulate(method=SimulationMethod.pFBA)
reference = res.fluxes
return {'model': model, 'biomass': BIOMASS_ID, 'envcond': envcond, 'reference': reference, 'non_target': []}
def cb_ou(product, chassis='ec', display=False, filename=None):
"""Defines and run a gene over/under expression problem.
Args:
product (str): the ID of the compound reaction exchange to be optimized
chassis (str, optional): The chassis, 'ec'(E.coli iJO1366Sl) , 'ec2' (E.coli iML1515) or 'ys' (yeast).
Defaults to 'ec'.
display (bool, optional): [description]. Defaults to False.
filename ([type], optional): [description]. Defaults to None.
"""
if chassis == 'ec2':
conf = load_ec2()
elif chassis == 'ys':
conf = load_yeast()
else:
conf = load_ec()
BIOMASS_ID = conf['biomass']
PRODUCT_ID = product
model = conf['model']
envcond = conf['envcond']
reference = conf['reference']
evaluator_1 = BPCY(BIOMASS_ID, PRODUCT_ID, uptake='R_EX_glc__D_e', method=SimulationMethod.lMOMA)
evaluator_2 = WYIELD(BIOMASS_ID, PRODUCT_ID)
# Favors deletion and under expression modifications
evaluator_3 = ModificationType()
from mewpy.problems import GOUProblem
problem = GOUProblem(model, fevaluation=[
evaluator_1, evaluator_2, evaluator_3], envcond=envcond, reference=reference, candidate_max_size=6,
operators=("lambda x,y: min(x,y)", "lambda x,y: max(x,y)"),
product=PRODUCT_ID)
ea = EA(problem, max_generations=ITERATIONS, visualizer=False, algorithm='NSGAIII')
final_pop = ea.run()
if display:
individual = max(final_pop)
best = list(problem.decode(individual.candidate).keys())
print('Best Solution: \n{0}'.format(str(best)))
if filename:
print("Saving solutions to file")
df = ea.dataframe()
df.to_csv(filename)
def cb_ko(product, chassis='ec', display=False, filename=None):
"""Defines and run a gene deletion problem.
Args:
product (str): the ID of the compound reaction exchange to be optimized
chassis (str, optional): The chassis, 'ec'(E.coli iJO1366Sl) , 'ec2' (E.coli iML1515) or 'ys' (yeast).
Defaults to 'ec'.
display (bool, optional): [description]. Defaults to False.
filename ([type], optional): [description]. Defaults to None.
"""
if chassis == 'ec':
conf = load_ec()
elif chassis == 'ys':
conf = load_yeast()
else:
raise ValueError
BIOMASS_ID = conf['biomass']
PRODUCT_ID = product
model = conf['model']
non_target = conf['non_target']
envcond = conf['envcond']
reference = conf['reference']
evaluator_1 = BPCY(BIOMASS_ID, PRODUCT_ID, method=SimulationMethod.lMOMA)
evaluator_2 = WYIELD(BIOMASS_ID, PRODUCT_ID)
from mewpy.problems.genes import GKOProblem
problem = GKOProblem(model, fevaluation=[
evaluator_1, evaluator_2], non_target=non_target, envcond=envcond, reference=reference)
ea = EA(problem, max_generations=ITERATIONS, mp=True)
final_pop = ea.run()
if display:
individual = max(final_pop)
best = list(problem.decode(individual.candidate).keys())
print('Best Solution: \n{0}'.format(str(best)))
if filename:
print("Saving solutions to file")
df = ea.dataframe()
df.to_csv(filename)
if __name__ == '__main__':
RUNS = 4
compounds_EC = { # "TYR": "R_EX_tyr_DASH_L_LPAREN_e_RPAREN_",
"PHE": "R_EX_phe_DASH_L_LPAREN_e_RPAREN_",
# "TRP": "R_EX_trp_DASH_L_LPAREN_e_RPAREN_"
}
compounds_YS = {"PHE": "R_EX_phe_L_e_",
"TYR": "R_EX_tyr_L_e_",
"TRY": "R_EX_trp_L_e_"
}
# for k, v in compounds_EC.items():
# for i in range(RUNS):
# millis = int(round(time() * 1000))
# cb_ko(v, filename="CBMODEL_{}_KO_{}.csv".format(k, millis))
# for k, v in compounds_EC.items():
# for i in range(RUNS):
# millis = int(round(time() * 1000))
# cb_ou(v, filename="CBMODEL_{}_OU_{}.csv".format(k, millis))
# for k, v in compounds_YS.items():
# for i in range(RUNS):
# millis = int(round(time() * 1000))
# cb_ko(v, chassis='ys', filename="CBMODEL_{}_KO_{}.csv".format(k, millis))
for k, v in compounds_EC.items():
for i in range(RUNS):
millis = int(round(time() * 1000))
cb_ou(v, chassis='ec', filename="CBMODEL_{}_OU_{}_.csv".format(k, millis))