/
initalize_model.py
302 lines (250 loc) · 14 KB
/
initalize_model.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
""" Initalize the construxction of wc_lang formatted models from wc_kb knowledge base.
:Author: Balazs Szigeti <balazs.szigeti@mssm.edu>
:Date: 2018-01-21
:Copyright: 2018, Karr Lab
:License: MIT
TODO:
- read cytosol volume from DB; currently there is onyl fractional volume?!
"""
from wc_lang import Species, Observable, ExpressionMethods
import wc_model_gen
import wc_lang
import wc_kb
class InitalizeModel(wc_model_gen.ModelComponentGenerator):
""" Generate compartments """
def run(self):
self.gen_compartments()
self.gen_parameters()
self.clean_and_validate_options()
options = self.options
if options['gen_metabolic_species']:
self.gen_metabolic_species()
if options['gen_rna']:
self.gen_rna()
if options['gen_protein']:
self.gen_protein()
if options['gen_complexes']:
self.gen_complexes()
if options['gen_concentrations']:
self.gen_concentrations()
if options['gen_observables']:
self.gen_observables()
def clean_and_validate_options(self):
options = self.options
gen_metabolic_species = options.get('gen_metabolic_species', True)
assert(isinstance(gen_metabolic_species,bool))
options['gen_metabolic_species'] = gen_metabolic_species
gen_rna = options.get('gen_rna', True)
assert(isinstance(gen_rna,bool))
options['gen_rna'] = gen_rna
gen_protein = options.get('gen_protein', True)
assert(isinstance(gen_protein,bool))
options['gen_protein'] = gen_protein
gen_complexes = options.get('gen_complexes', True)
assert(isinstance(gen_complexes,bool))
options['gen_complexes'] = gen_complexes
gen_concentrations = options.get('gen_concentrations', True)
assert(isinstance(gen_concentrations,bool))
options['gen_concentrations'] = gen_concentrations
gen_observables = options.get('gen_observables', True)
assert(isinstance(gen_observables,bool))
options['gen_observables'] = gen_observables
def gen_compartments(self):
cell = self.knowledge_base.cell
model = self.model
# Create default compartments
self.model.compartments.create(id='c', name='Cytosol', initial_volume=1E-15)
self.model.compartments.create(id='e', name='Extracellular space', initial_volume=1E-10)
# Generate the compartments that are defined in the knowledge base
# TODO: currently no volume info in KB, talk to YH
# for compartment_kb in self.knowledge_base.cell.compartments:
# compartment_model = self.model.compartments.get_or_create(id=kb_comp.id)
# compartment_model.name = compartment_kb.name
# compartment_model.initial_volume = compartment_kb.properties.get_one(id='volume').value
def gen_parameters(self):
# Create parameters
self.model.parameters.create(id='fraction_dry_weight',
value = self.knowledge_base.cell.properties.get_one(id='fraction_dry_weight').value)
self.model.parameters.get_or_create(id='fraction_dry_weight').units = 'dimensionless'
self.model.parameters.create(id='cell_cycle_length',
value = self.knowledge_base.cell.properties.get_one(id='cell_cycle_length').value)
self.model.parameters.get_or_create(id='cell_cycle_length').units = 's'
self.model.parameters.create(id='fractionDryWeight',
value = self.knowledge_base.cell.properties.get_one(id='fraction_dry_weight').value)
self.model.parameters.get_or_create(id='fractionDryWeight').units = 'dimensionless'
self.model.parameters.create(id='cellCycleLength',
value = self.knowledge_base.cell.properties.get_one(id='cell_cycle_length').value)
self.model.parameters.get_or_create(id='cellCycleLength').units = 's'
def gen_metabolic_species(self):
""" Generate all metabolic species in the cytosol """
cytosol = self.model.compartments.get_one(id='c')
xtracell = self.model.compartments.get_one(id='e')
metabolites = self.knowledge_base.cell.species_types.get(
__type=wc_kb.core.MetaboliteSpeciesType)
# get or create metabolite species
for kb_met in metabolites:
self.gen_a_specie(kb_met, cytosol)
self.gen_a_specie(kb_met, xtracell)
def gen_rna(self):
'''Generate RNAs in wc_lang model from knowledge base '''
cell = self.knowledge_base.cell
model = self.model
cytosol = model.compartments.get(id='c')[0]
# get or create RNA species
rnas = cell.species_types.get(__type=wc_kb.prokaryote_schema.RnaSpeciesType)
for rna in rnas:
species_type = model.species_types.get_or_create(id=rna.id)
species_type.name = rna.name
species_type.type = wc_lang.SpeciesTypeType.rna
species_type.structure = rna.get_seq()
species_type.empirical_formula = rna.get_empirical_formula()
species_type.molecular_weight = rna.get_mol_wt()
species_type.charge = rna.get_charge()
species_type.comments = rna.comments
species = species_type.species.get_or_create(compartment=cytosol)
def gen_protein(self):
'''Generate proteins in wc_lang model from knowledge base '''
cell = self.knowledge_base.cell
model = self.model
cytosol = model.compartments.get(id='c')[0]
for protein in self.knowledge_base.cell.species_types.get(__type=wc_kb.prokaryote_schema.ProteinSpeciesType):
species_type = self.model.species_types.get_or_create(id=protein.id)
species_type.name = protein.name
species_type.type = wc_lang.SpeciesTypeType.protein
species_type.structure = protein.get_seq()
species_type.empirical_formula = protein.get_empirical_formula()
species_type.molecular_weight = protein.get_mol_wt()
species_type.charge = protein.get_charge()
species_type.comments = protein.comments
species = species_type.species.get_or_create(compartment=cytosol)
def gen_complexes(self):
'''Generate complexes in wc_lang model from knowledge base '''
cell = self.knowledge_base.cell
model = self.model
cytosol = model.compartments.get(id='c')[0]
for comp in self.knowledge_base.cell.species_types.get(__type=wc_kb.core.ComplexSpeciesType):
species_type = model.species_types.get_or_create(id=comp.id)
species_type.name = comp.name
species_type.type = wc_lang.SpeciesTypeType.pseudo_species
species_type.empirical_formula = comp.get_empirical_formula()
species_type.molecular_weight = comp.get_mol_wt()
species_type.charge = comp.get_charge()
species = species_type.species.get_or_create(compartment=cytosol)
def gen_concentrations(self):
'''Generate concentrations in wc_lang model from knowledge base '''
cell = self.knowledge_base.cell
model = self.model
cytosol = model.compartments.get_one(id='c')
for conc in self.knowledge_base.cell.concentrations:
species_comp_model = model.compartments.get_one(id=conc.species.compartment.id)
species_type = model.species_types.get_or_create(id=conc.species.species_type.id)
species = species_type.species.get_or_create(compartment=species_comp_model)
species.concentration = wc_lang.Concentration(
value=conc.value, units=wc_lang.ConcentrationUnit.M,
comments=conc.comments, references=conc.references)
def gen_observables(self):
'''Generate observables in wc_lang model from knowledge base '''
cell = self.knowledge_base.cell
model = self.model
cytosol = model.compartments.get(id='c')[0]
observable_references = {Species:{}, Observable:{}}
for kb_observable in self.knowledge_base.cell.observables:
model_observable = self.model.observables.get_or_create(id=kb_observable.id)
obs_expr_parts = []
model_observable.name = kb_observable.name
for kb_species_coefficient in kb_observable.species:
kb_species = kb_species_coefficient.species
kb_species_type = kb_species.species_type
kb_compartment = kb_species.compartment
model_species_type = model.species_types.get_one(
id=kb_species_type.id)
model_species = model_species_type.species.get_one(
compartment=model.compartments.get_one(id=kb_compartment.id))
observable_references[Species][model_species.get_id()] = model_species
model_coefficient = kb_species_coefficient.coefficient
obs_expr_parts.append("{}*{}".format(model_coefficient, model_species.get_id()))
for kb_observable_observable in kb_observable.observables:
model_observable_observable = model.observables.get_or_create(
id=kb_observable_observable.id)
obs_expr_parts.append("{}*{}".format(kb_observable_observable.coefficient, kb_observable_observable.id))
observable_references[Observable][model_observable_observable.id] = model_observable_observable
obs_expr, e = ExpressionMethods.make_expression_obj(Observable,
' + '.join(obs_expr_parts), observable_references)
assert e is None, "cannot deserialize ObservableExpression: {}".format(e)
model_observable.expression = obs_expr
def gen_a_specie(self, kb_metabolite, lang_compartment):
""" Generate a species in a particular compartment
Args:
kb_metabolite (:obj:`wc_kb.MetaboliteSpeciesType`): a knowledgebase metabolite
lang_compartment (:obj:`wc_lang.Compartment`): the wc_lang compartment containing the species
Returns:
:obj:`wc_lang.Species`: the species that was found or created
"""
species_type = self.model.species_types.get_or_create(id=kb_metabolite.id)
species_type.name = kb_metabolite.name
species_type.type = wc_lang.SpeciesTypeType.metabolite
species_type.structure = kb_metabolite.structure
species_type.empirical_formula = kb_metabolite.get_empirical_formula()
species_type.molecular_weight = kb_metabolite.get_mol_wt()
species_type.charge = kb_metabolite.get_charge()
species_type.comments = kb_metabolite.comments
species = species_type.species.get_or_create(compartment=lang_compartment)
return species
"""
The functions below are not supported at the moment.
KB needs to be refractored before these can work.
GH issue: github.com/KarrLab/wc_model_gen/issues/14
def gen_kb_reactions(self):
" Generate reactions encoded within KB ""
for kb_rxn in self.knowledge_base.cell.reactions:
# if species are metabolites, create lang reaction in metabolism
# todo: generalize to all submodels
if self.get_species_type_types(kb_rxn) == set([wc_kb.core.MetaboliteSpeciesType]):
lang_rxn = self.submodel.reactions.create(
id=kb_rxn.id,
name=kb_rxn.name,
reversible=kb_rxn.reversible,
comments=kb_rxn.comments)
for participant in kb_rxn.participants:
kb_species = participant.species
lang_species_type = self.model.species_types.get_one(
id=kb_species.species_type.id)
lang_compartment = self.model.compartments.get_one(
id=kb_species.compartment.id)
lang_species = lang_species_type.species.get_one(
compartment=lang_compartment)
# ensure that species are present in extracellular space
if lang_species is None:
lang_species = self.gen_a_specie(kb_species.species_type, lang_compartment)
lang_rxn.participants.add(
lang_species.species_coefficients.get_or_create(
coefficient=participant.coefficient))
def gen_kb_rate_laws(self):
"" Generate rate laws for reactions encoded in KB ""
model = self.model
cell = self.knowledge_base.cell
submodel = model.submodels.get_one(id='metabolism')
c = model.compartments.get_one(id='c')
e = model.compartments.get_one(id='e')
for kb_rxn in self.knowledge_base.cell.reactions:
if self.get_species_type_types(kb_rxn) == set([wc_kb.core.MetaboliteSpeciesType]):
lang_rxn = self.submodel.reactions.get_one(id=kb_rxn.id)
for kb_rate_law in kb_rxn.rate_laws:
lang_rate_law = wc_lang.RateLaw(k_cat=kb_rate_law.k_cat,
k_m=kb_rate_law.k_m,
comments=kb_rate_law.comments)
lang_rxn.rate_laws.add(lang_rate_law)
if kb_rate_law.direction == wc_kb.RateLawDirection.forward:
lang_rate_law.direction = wc_lang.RateLawDirection.forward
elif kb_rate_law.direction == wc_kb.RateLawDirection.backward: # pragma branch not covered
lang_rate_law.direction = wc_lang.RateLawDirection.backward
lang_rate_law.equation = wc_lang.RateLawEquation(
expression=kb_rate_law.equation.expression
)
for kb_modifier in kb_rate_law.equation.modifiers:
lang_species_type = self.model.species_types.get_one(
id=kb_modifier.species_type.id)
lang_species = lang_species_type.species.get_one(
compartment=c)
lang_rate_law.equation.modifiers.add(lang_species)
"""