/
mnm_repr.py
executable file
·392 lines (287 loc) · 10.4 KB
/
mnm_repr.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
#! /usr/bin/env python3
# I, Robert Rozanski, the copyright holder of this work, release this work into the public domain. This applies worldwide. In some countries this may not be legally possible; if so: I grant anyone the right to use this work for any purpose, without any conditions, unless such conditions are required by law.
class Element:
def __init__(self, ID, name):
self.ID = ID
self.name = name
class Entity(Element):
def __init__(self, ID, name, version, properties):
Element.__init__(self, ID, name)
self.version = version
self.properties = frozenset(properties)
self.add_cost = 1
self.remove_cost = 1
self.detection_cost = 1
self.localisation_cost = 1
def __hash__(self):
return hash((self.ID, self.version))
def __eq__(self, other):
return ((hash(self) == hash(other)) and (type(self) == type(other)))
class Gene(Entity):
def __init__(self, ID, name=None, version='none', properties=[]):
Entity.__init__(self, ID, name, version, properties)
class Metabolite(Entity):
def __init__(self, ID, name=None, version='none', properties=[]):
Entity.__init__(self, ID, name, version, properties)
class Protein(Entity):
def __init__(self, ID, name=None, version='none', properties=[]):
Entity.__init__(self, ID, name, version, properties)
class Complex(Entity):
def __init__(self, ID, name=None, version='none', properties=[]):
Entity.__init__(self, ID, name, version, properties)
class Activity(Element):
def __init__(self, ID, name, required_conditions, changes):
Element.__init__(self, ID, name)
self.required_conditions = frozenset(required_conditions)
self.changes = frozenset(changes)
self.detection_cost = 1
self.base_reconstruction_cost = 1
self.add_cost = 1
self.remove_cost = 1
def check_if_is_product(self, ent):
return ent in [con.entity for con in self.changes if isinstance(con, PresentEntity)]
def return_substrates(self):
return [con.entity for con in self.required_conditions if isinstance(con, PresentEntity)]
def return_products(self):
return [con.entity for con in self.changes if isinstance(con, PresentEntity)]
def __hash__(self):
return hash((self.required_conditions, self.changes))
def __eq__(self, other):
return ((hash(self) == hash(other)) and (type(self) == type(other)))
class Growth(Activity):
def __init__(self, ID, required_conditions, name=None):
Activity.__init__(self, ID, name, required_conditions, [])
class Expression(Activity):
def __init__(self, ID, required_conditions, changes, name=None):
Activity.__init__(self, ID, name, required_conditions, changes)
class Reaction(Activity):
def __init__(self, ID, required_conditions, changes, name=None):
Activity.__init__(self, ID, name, required_conditions, changes)
class Transport(Activity):
def __init__(self, ID, required_conditions, changes, name=None):
Activity.__init__(self, ID, name, required_conditions, changes)
class ComplexFormation(Activity):
def __init__(self, ID, required_conditions, changes, name=None):
Activity.__init__(self, ID, name, required_conditions, changes)
class Property:
def __init__(self):
pass
class Catalyses(Property):
def __init__(self, activity):
Property.__init__(self)
self.activity = activity
class Transports(Property):
def __init__(self, activity):
Property.__init__(self)
self.activity = activity
class Condition:
def __init__(self):
pass
class PresentEntity(Condition):
def __init__(self, entity, compartment):
self.entity = entity
self.compartment = compartment
def __hash__(self):
return hash((self.entity, self.compartment))
def __eq__(self, other):
return ((hash(self) == hash(other)) and (type(self) == type(other)))
class PresentCatalyst(Condition):
def __init__(self, compartment):
self.compartment = compartment
def __hash__(self):
return hash(self.compartment)
def __eq__(self, other):
return ((hash(self) == hash(other)) and (type(self) == type(other)))
class PresentTransporter(Condition):
def __init__(self, compartment):
self.compartment = compartment
def __hash__(self):
return hash(self.compartment)
def __eq__(self, other):
return ((hash(self) == hash(other)) and (type(self) == type(other)))
class Compartment:
def __init__(self):
self.ID = None
def __hash__(self):
return hash(self.ID)
def __eq__(self, other):
return ((hash(self) == hash(other)) and (type(self) == type(other)))
class Medium(Compartment):
def __init__(self):
self.ID = "c_01"
class CellMembrane(Compartment):
def __init__(self):
self.ID = "c_02"
class CellMembraneOuterSide(Compartment):
def __init__(self):
self.ID = "c_03"
class CellMembraneInnerSide(Compartment):
def __init__(self):
self.ID = "c_04"
class Cytosol(Compartment):
def __init__(self):
self.ID = "c_05"
class Mitochondrium(Compartment):
def __init__(self):
self.ID = "c_06"
class MitochMatrix(Compartment):
def __init__(self):
self.ID = "c_07"
class MitochOuterMembrane(Compartment):
def __init__(self):
self.ID = "c_08"
class MitochOuterMembraneOuterSide(Compartment):
def __init__(self):
self.ID = "c_09"
class MitochOuterMembraneInnerSide(Compartment):
def __init__(self):
self.ID = "c_10"
class MitochInnerMembrane(Compartment):
def __init__(self):
self.ID = "c_11"
class MitochInnerMembraneOuterSide(Compartment):
def __init__(self):
self.ID = "c_12"
class MitochInnerMembraneInnerSide(Compartment):
def __init__(self):
self.ID = "c_13"
class GolgiMembrane(Compartment):
def __init__(self):
self.ID = "c_14"
class GolgiMembraneOuterSide(Compartment):
def __init__(self):
self.ID = "c_15"
class GolgiMembraneInnerSide(Compartment):
def __init__(self):
self.ID = "c_16"
class GolgiApparatus(Compartment):
def __init__(self):
self.ID = "c_17"
class Nucleus(Compartment):
def __init__(self):
self.ID = "c_18"
class NuclearMembrane(Compartment):
def __init__(self):
self.ID = "c_19"
class NuclearMembraneOuterSide(Compartment):
def __init__(self):
self.ID = "c_20"
class NuclearMembraneInnerSide(Compartment):
def __init__(self):
self.ID = "c_21"
class EndoplasmicReticulum(Compartment):
def __init__(self):
self.ID = "c_22"
class ERMembrane(Compartment):
def __init__(self):
self.ID = "c_23"
class ERMembraneOuterSide(Compartment):
def __init__(self):
self.ID = "c_24"
class ERMembraneInnerSide(Compartment):
def __init__(self):
self.ID = "c_25"
class Vacuole(Compartment):
def __init__(self):
self.ID = "c_26"
class VacuolarMembrane(Compartment):
def __init__(self):
self.ID = "c_27"
class VacuolarMembraneMediumSide(Compartment):
def __init__(self):
self.ID = "c_28"
class VacuolarMembraneCytosolSide(Compartment):
def __init__(self):
self.ID = "c_29"
class VacuolarMembraneInnerSide(Compartment):
def __init__(self):
self.ID = "c_30"
class PeroxisomalMembrane(Compartment):
def __init__(self):
self.ID = "c_31"
class PeroxisomalMembraneInnerSide(Compartment):
def __init__(self):
self.ID = "c_32"
class PeroxisomalMembraneOuterSide(Compartment):
def __init__(self):
self.ID = "c_33"
class Peroxisome(Compartment):
def __init__(self):
self.ID = "c_34"
class LipidParticle(Compartment):
def __init__(self):
self.ID = "c_35"
class Intervention:
def __init__(self, condition_or_activity):
self.condition_or_activity = condition_or_activity
def __hash__(self):
return hash(self.condition_or_activity)
def __eq__(self, other):
return ((hash(self) == hash(other)) and (type(self) == type(other)))
class Add(Intervention):
def __init__(self, condition_or_activity):
Intervention.__init__(self, condition_or_activity)
def __hash__(self):
return hash(self.condition_or_activity)
def __eq__(self, other):
return ((hash(self) == hash(other)) and (type(self) == type(other)))
class Remove(Intervention):
def __init__(self, condition_or_activity):
Intervention.__init__(self, condition_or_activity)
def __hash__(self):
return hash(self.condition_or_activity)
def __eq__(self, other):
return ((hash(self) == hash(other)) and (type(self) == type(other)))
class Model:
def __init__(self, ID, setup_conditions, intermediate_activities, termination_conditions, status='Active'):
self.ID = ID
self.setup_conditions = frozenset(setup_conditions)
self.intermediate_activities = frozenset(intermediate_activities)
self.termination_conditions = frozenset(termination_conditions)
self.status = status
self.score = None
self.quality = 1
self.results_covered = frozenset([])
self.ignored_results = frozenset([])
def __copy__(self):
setup = frozenset(list(self.setup_conditions))
activ = frozenset(list(self.intermediate_activities))
termi = frozenset(list(self.termination_conditions))
new_model = Model(None, setup, activ, termi)
return new_model
def __hash__(self):
return hash((self.setup_conditions, self.intermediate_activities, self.termination_conditions))
def __eq__(self, other):
return ((hash(self) == hash(other)) and (type(self) == type(other)))
def update_ignored_results(self, ignored_results):
self.ignored_results = frozenset(ignored_results)
def update_covered_results(self, results_covered):
self.results_covered = frozenset(results_covered)
def apply_interventions(self, interventions):
for intervention in interventions:
self.apply_intervention(intervention)
def apply_intervention(self, intervention):
if type(intervention) == Add:
if isinstance(intervention.condition_or_activity, Condition):
new_set = set(self.setup_conditions)
new_set.add(intervention.condition_or_activity)
self.setup_conditions = frozenset(new_set)
elif isinstance(intervention.condition_or_activity, Activity):
new_set = set(self.intermediate_activities)
new_set.add(intervention.condition_or_activity)
self.intermediate_activities = frozenset(new_set)
else:
raise TypeError('intervention is neither condition nor activity: %s' % type(intervention.condition_or_activity))
elif type(intervention) == Remove:
if isinstance(intervention.condition_or_activity, Condition):
new_set = set(self.setup_conditions)
new_set.remove(intervention.condition_or_activity)
self.setup_conditions = frozenset(new_set)
elif isinstance(intervention.condition_or_activity, Activity):
new_set = set(self.intermediate_activities)
new_set.remove(intervention.condition_or_activity)
self.intermediate_activities = frozenset(new_set)
else:
raise TypeError('intervention is neither condition nor activity: %s' % type(intervention.condition_or_activity))
else:
raise TypeError('type of intervention not recognised: %s' % type(intervention))