forked from opencobra/cobrapy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
1229 lines (1037 loc) · 46 KB
/
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
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import types
import logging
from copy import copy, deepcopy
from functools import partial
from warnings import warn
import optlang
from optlang.symbolics import Basic, Zero
import six
from six import iteritems, string_types
from cobra.exceptions import SolverNotFound
from cobra.core.dictlist import DictList
from cobra.core.object import Object
from cobra.core.reaction import separate_forward_and_reverse_bounds, Reaction
from cobra.core.metabolite import Metabolite
from cobra.core.gene import Gene
from cobra.core.group import Group
from cobra.core.solution import get_solution
from cobra.util.context import HistoryManager, resettable, get_context
from cobra.util.solver import (
get_solver_name, interface_to_str, set_objective, solvers,
add_cons_vars_to_problem, remove_cons_vars_from_problem, assert_optimal)
from cobra.util.util import AutoVivification, format_long_string
from cobra.medium import find_boundary_types
LOGGER = logging.getLogger(__name__)
class Model(Object):
"""Class representation for a cobra model
Parameters
----------
id_or_model : Model, string
Either an existing Model object in which case a new model object is
instantiated with the same properties as the original model,
or a the identifier to associate with the model as a string.
name : string
Human readable name for the model
Attributes
----------
reactions : DictList
A DictList where the key is the reaction identifier and the value a
Reaction
metabolites : DictList
A DictList where the key is the metabolite identifier and the value a
Metabolite
genes : DictList
A DictList where the key is the gene identifier and the value a
Gene
groups : DictList
A DictList where the key is the group identifier and the value a
Group
solution : Solution
The last obtained solution from optimizing the model.
"""
def __setstate__(self, state):
"""Make sure all cobra.Objects in the model point to the model.
"""
self.__dict__.update(state)
for y in ['reactions', 'genes', 'metabolites']:
for x in getattr(self, y):
x._model = self
if not hasattr(self, "name"):
self.name = None
def __getstate__(self):
"""Get state for serialization.
Ensures that the context stack is cleared prior to serialization,
since partial functions cannot be pickled reliably.
"""
odict = self.__dict__.copy()
odict['_contexts'] = []
return odict
def __init__(self, id_or_model=None, name=None):
if isinstance(id_or_model, Model):
Object.__init__(self, name=name)
self.__setstate__(id_or_model.__dict__)
if not hasattr(self, "name"):
self.name = None
self._solver = id_or_model.solver
else:
Object.__init__(self, id_or_model, name=name)
self._trimmed = False
self._trimmed_genes = []
self._trimmed_reactions = {}
self.genes = DictList()
self.reactions = DictList() # A list of cobra.Reactions
self.metabolites = DictList() # A list of cobra.Metabolites
self.groups = DictList() # A list of cobra.Groups
# genes based on their ids {Gene.id: Gene}
self._compartments = dict()
self._contexts = []
# from cameo ...
# if not hasattr(self, '_solver'): # backwards compatibility
# with older cobrapy pickles?
interface = solvers[get_solver_name()]
self._solver = interface.Model()
self._solver.objective = interface.Objective(Zero)
self._populate_solver(self.reactions, self.metabolites)
@property
def solver(self):
"""Get or set the attached solver instance.
The associated the solver object, which manages the interaction with
the associated solver, e.g. glpk.
This property is useful for accessing the optimization problem
directly and to define additional non-metabolic constraints.
Examples
--------
>>> import cobra.test
>>> model = cobra.test.create_test_model("textbook")
>>> new = model.problem.Constraint(model.objective.expression,
>>> lb=0.99)
>>> model.solver.add(new)
"""
return self._solver
@solver.setter
@resettable
def solver(self, value):
not_valid_interface = SolverNotFound(
'%s is not a valid solver interface. Pick from %s.' % (
value, list(solvers)))
if isinstance(value, six.string_types):
try:
interface = solvers[interface_to_str(value)]
except KeyError:
raise not_valid_interface
elif isinstance(value, types.ModuleType) and hasattr(value, 'Model'):
interface = value
elif isinstance(value, optlang.interface.Model):
interface = value.interface
else:
raise not_valid_interface
# Do nothing if the solver did not change
if self.problem == interface:
return
self._solver = interface.Model.clone(self._solver)
@property
def description(self):
warn("description deprecated", DeprecationWarning)
return self.name if self.name is not None else ""
@description.setter
def description(self, value):
self.name = value
warn("description deprecated", DeprecationWarning)
def get_metabolite_compartments(self):
"""Return all metabolites' compartments."""
warn('use Model.compartments instead', DeprecationWarning)
return {met.compartment for met in self.metabolites
if met.compartment is not None}
@property
def compartments(self):
return {met.compartment: self._compartments.get(met.compartment, '')
for met in self.metabolites if met.compartment is not None}
@compartments.setter
def compartments(self, value):
"""Get or set the dictionary of current compartment descriptions.
Assigning a dictionary to this property updates the model's
dictionary of compartment descriptions with the new values.
Parameters
----------
value : dict
Dictionary mapping compartments abbreviations to full names.
Examples
--------
>>> import cobra.test
>>> model = cobra.test.create_test_model("textbook")
>>> model.compartments = {'c': 'the cytosol'}
{'c': 'the cytosol', 'e': 'extracellular'}
"""
self._compartments.update(value)
@property
def medium(self):
def is_active(reaction):
"""Determine if a boundary reaction permits flux towards creating
metabolites
"""
return ((bool(reaction.products) and (reaction.upper_bound > 0)) or
(bool(reaction.reactants) and (reaction.lower_bound < 0)))
def get_active_bound(reaction):
"""For an active boundary reaction, return the relevant bound"""
if reaction.reactants:
return -reaction.lower_bound
elif reaction.products:
return reaction.upper_bound
return {rxn.id: get_active_bound(rxn) for rxn in self.exchanges
if is_active(rxn)}
@medium.setter
def medium(self, medium):
"""Get or set the constraints on the model exchanges.
`model.medium` returns a dictionary of the bounds for each of the
boundary reactions, in the form of `{rxn_id: bound}`, where `bound`
specifies the absolute value of the bound in direction of metabolite
creation (i.e., lower_bound for `met <--`, upper_bound for `met -->`)
Parameters
----------
medium: dictionary-like
The medium to initialize. medium should be a dictionary defining
`{rxn_id: bound}` pairs.
"""
def set_active_bound(reaction, bound):
if reaction.reactants:
reaction.lower_bound = -bound
elif reaction.products:
reaction.upper_bound = bound
# Set the given media bounds
media_rxns = list()
exchange_rxns = frozenset(self.exchanges)
for rxn_id, bound in iteritems(medium):
rxn = self.reactions.get_by_id(rxn_id)
if rxn not in exchange_rxns:
LOGGER.warn("%s does not seem to be an"
" an exchange reaction. Applying bounds anyway.",
rxn.id)
media_rxns.append(rxn)
set_active_bound(rxn, bound)
media_rxns = frozenset(media_rxns)
# Turn off reactions not present in media
for rxn in (exchange_rxns - media_rxns):
set_active_bound(rxn, 0)
def __add__(self, other_model):
"""Add the content of another model to this model (+).
The model is copied as a new object, with a new model identifier,
and copies of all the reactions in the other model are added to this
model. The objective is the sum of the objective expressions for the
two models.
"""
warn('use model.merge instead', DeprecationWarning)
return self.merge(other_model, objective='sum', inplace=False)
def __iadd__(self, other_model):
"""Incrementally add the content of another model to this model (+=).
Copies of all the reactions in the other model are added to this
model. The objective is the sum of the objective expressions for the
two models.
"""
warn('use model.merge instead', DeprecationWarning)
return self.merge(other_model, objective='sum', inplace=True)
def copy(self):
"""Provides a partial 'deepcopy' of the Model. All of the Metabolite,
Gene, and Reaction objects are created anew but in a faster fashion
than deepcopy
"""
new = self.__class__()
do_not_copy_by_ref = {"metabolites", "reactions", "genes", "notes",
"annotation", "groups"}
for attr in self.__dict__:
if attr not in do_not_copy_by_ref:
new.__dict__[attr] = self.__dict__[attr]
new.notes = deepcopy(self.notes)
new.annotation = deepcopy(self.annotation)
new.metabolites = DictList()
do_not_copy_by_ref = {"_reaction", "_model"}
for metabolite in self.metabolites:
new_met = metabolite.__class__()
for attr, value in iteritems(metabolite.__dict__):
if attr not in do_not_copy_by_ref:
new_met.__dict__[attr] = copy(
value) if attr == "formula" else value
new_met._model = new
new.metabolites.append(new_met)
new.genes = DictList()
for gene in self.genes:
new_gene = gene.__class__(None)
for attr, value in iteritems(gene.__dict__):
if attr not in do_not_copy_by_ref:
new_gene.__dict__[attr] = copy(
value) if attr == "formula" else value
new_gene._model = new
new.genes.append(new_gene)
new.reactions = DictList()
do_not_copy_by_ref = {"_model", "_metabolites", "_genes"}
for reaction in self.reactions:
new_reaction = reaction.__class__()
for attr, value in iteritems(reaction.__dict__):
if attr not in do_not_copy_by_ref:
new_reaction.__dict__[attr] = copy(value)
new_reaction._model = new
new.reactions.append(new_reaction)
# update awareness
for metabolite, stoic in iteritems(reaction._metabolites):
new_met = new.metabolites.get_by_id(metabolite.id)
new_reaction._metabolites[new_met] = stoic
new_met._reaction.add(new_reaction)
for gene in reaction._genes:
new_gene = new.genes.get_by_id(gene.id)
new_reaction._genes.add(new_gene)
new_gene._reaction.add(new_reaction)
new.groups = DictList()
do_not_copy_by_ref = {"_model", "_members"}
for group in self.groups:
new_group = group.__class__()
for attr, value in iteritems(group.__dict__):
if attr not in do_not_copy_by_ref:
new_group.__dict__[attr] = copy(value)
new_group._model = new
new.groups.append(new_group)
# update awareness, as in the reaction copies
new_objects = []
for member in group.members:
if isinstance(member, Metabolite):
new_object = new.metabolites.get_by_id(member.id)
elif isinstance(member, Reaction):
new_object = new.reactions.get_by_id(member.id)
elif isinstance(member, Gene):
new_objext = new.genes.get_by_id(member.id)
new_objects.append(new_object)
new_group.add_members(new_objects)
try:
new._solver = deepcopy(self.solver)
# Cplex has an issue with deep copies
except Exception: # pragma: no cover
new._solver = copy(self.solver) # pragma: no cover
# it doesn't make sense to retain the context of a copied model so
# assign a new empty context
new._contexts = list()
return new
def add_metabolites(self, metabolite_list):
"""Will add a list of metabolites to the model object and add new
constraints accordingly.
The change is reverted upon exit when using the model as a context.
Parameters
----------
metabolite_list : A list of `cobra.core.Metabolite` objects
"""
if not hasattr(metabolite_list, '__iter__'):
metabolite_list = [metabolite_list]
if len(metabolite_list) == 0:
return None
# First check whether the metabolites exist in the model
metabolite_list = [x for x in metabolite_list
if x.id not in self.metabolites]
bad_ids = [m for m in metabolite_list
if not isinstance(m.id, string_types) or len(m.id) < 1]
if len(bad_ids) != 0:
raise ValueError('invalid identifiers in {}'.format(repr(bad_ids)))
for x in metabolite_list:
x._model = self
self.metabolites += metabolite_list
# from cameo ...
to_add = []
for met in metabolite_list:
if met.id not in self.constraints:
constraint = self.problem.Constraint(
Zero, name=met.id, lb=0, ub=0)
to_add += [constraint]
self.add_cons_vars(to_add)
context = get_context(self)
if context:
context(partial(self.metabolites.__isub__, metabolite_list))
for x in metabolite_list:
# Do we care?
context(partial(setattr, x, '_model', None))
def remove_metabolites(self, metabolite_list, destructive=False):
"""Remove a list of metabolites from the the object.
The change is reverted upon exit when using the model as a context.
Parameters
----------
metabolite_list : list
A list with `cobra.Metabolite` objects as elements.
destructive : bool
If False then the metabolite is removed from all
associated reactions. If True then all associated
reactions are removed from the Model.
"""
if not hasattr(metabolite_list, '__iter__'):
metabolite_list = [metabolite_list]
# Make sure metabolites exist in model
metabolite_list = [x for x in metabolite_list
if x.id in self.metabolites]
for x in metabolite_list:
x._model = None
# remove reference to the metabolite in all groups
associated_groups = self.get_associated_groups(x)
for group in associated_groups:
group.remove(x)
if not destructive:
for the_reaction in list(x._reaction):
the_coefficient = the_reaction._metabolites[x]
the_reaction.subtract_metabolites({x: the_coefficient})
else:
for x in list(x._reaction):
x.remove_from_model()
self.metabolites -= metabolite_list
to_remove = [self.solver.constraints[m.id] for m in metabolite_list]
self.remove_cons_vars(to_remove)
context = get_context(self)
if context:
context(partial(self.metabolites.__iadd__, metabolite_list))
for x in metabolite_list:
context(partial(setattr, x, '_model', self))
def add_reaction(self, reaction):
"""Will add a cobra.Reaction object to the model, if
reaction.id is not in self.reactions.
Parameters
----------
reaction : cobra.Reaction
The reaction to add
Deprecated (0.6). Use `~cobra.Model.add_reactions` instead
"""
warn("add_reaction deprecated. Use add_reactions instead",
DeprecationWarning)
self.add_reactions([reaction])
def add_boundary(self, metabolite, type="exchange", reaction_id=None,
lb=None, ub=1000.0):
"""Add a boundary reaction for a given metabolite.
There are three different types of pre-defined boundary reactions:
exchange, demand, and sink reactions.
An exchange reaction is a reversible, imbalanced reaction that adds
to or removes an extracellular metabolite from the extracellular
compartment.
A demand reaction is an irreversible reaction that consumes an
intracellular metabolite.
A sink is similar to an exchange but specifically for intracellular
metabolites.
If you set the reaction `type` to something else, you must specify the
desired identifier of the created reaction along with its upper and
lower bound. The name will be given by the metabolite name and the
given `type`.
Parameters
----------
metabolite : cobra.Metabolite
Any given metabolite. The compartment is not checked but you are
encouraged to stick to the definition of exchanges and sinks.
type : str, {"exchange", "demand", "sink"}
Using one of the pre-defined reaction types is easiest. If you
want to create your own kind of boundary reaction choose
any other string, e.g., 'my-boundary'.
reaction_id : str, optional
The ID of the resulting reaction. Only used for custom reactions.
lb : float, optional
The lower bound of the resulting reaction. Only used for custom
reactions.
ub : float, optional
The upper bound of the resulting reaction. For the pre-defined
reactions this default value determines all bounds.
Returns
-------
cobra.Reaction
The created boundary reaction.
Examples
--------
>>> import cobra.test
>>> model = cobra.test.create_test_model("textbook")
>>> demand = model.add_boundary(model.metabolites.atp_c, type="demand")
>>> demand.id
'DM_atp_c'
>>> demand.name
'ATP demand'
>>> demand.bounds
(0, 1000.0)
>>> demand.build_reaction_string()
'atp_c --> '
"""
types = dict(exchange=("EX", -ub, ub), demand=("DM", 0, ub),
sink=("SK", -ub, ub))
if type in types:
prefix, lb, ub = types[type]
reaction_id = "{}_{}".format(prefix, metabolite.id)
if reaction_id in self.reactions:
raise ValueError('boundary %s already exists' % reaction_id)
name = "{} {}".format(metabolite.name, type)
rxn = Reaction(id=reaction_id, name=name, lower_bound=lb,
upper_bound=ub)
rxn.add_metabolites({metabolite: -1})
self.add_reactions([rxn])
return rxn
def add_reactions(self, reaction_list):
"""Add reactions to the model.
Reactions with identifiers identical to a reaction already in the
model are ignored.
The change is reverted upon exit when using the model as a context.
Parameters
----------
reaction_list : list
A list of `cobra.Reaction` objects
"""
def existing_filter(rxn):
if rxn.id in self.reactions:
LOGGER.warning(
"Ignoring reaction '%s' since it already exists.", rxn.id)
return False
return True
# First check whether the reactions exist in the model.
pruned = DictList(filter(existing_filter, reaction_list))
context = get_context(self)
# Add reactions. Also take care of genes and metabolites in the loop.
for reaction in pruned:
reaction._model = self
# Build a `list()` because the dict will be modified in the loop.
for metabolite in list(reaction.metabolites):
# TODO: Should we add a copy of the metabolite instead?
if metabolite not in self.metabolites:
self.add_metabolites(metabolite)
# A copy of the metabolite exists in the model, the reaction
# needs to point to the metabolite in the model.
else:
# FIXME: Modifying 'private' attributes is horrible.
stoichiometry = reaction._metabolites.pop(metabolite)
model_metabolite = self.metabolites.get_by_id(
metabolite.id)
reaction._metabolites[model_metabolite] = stoichiometry
model_metabolite._reaction.add(reaction)
if context:
context(partial(
model_metabolite._reaction.remove, reaction))
for gene in list(reaction._genes):
# If the gene is not in the model, add it
if not self.genes.has_id(gene.id):
self.genes += [gene]
gene._model = self
if context:
# Remove the gene later
context(partial(self.genes.__isub__, [gene]))
context(partial(setattr, gene, '_model', None))
# Otherwise, make the gene point to the one in the model
else:
model_gene = self.genes.get_by_id(gene.id)
if model_gene is not gene:
reaction._dissociate_gene(gene)
reaction._associate_gene(model_gene)
self.reactions += pruned
if context:
context(partial(self.reactions.__isub__, pruned))
# from cameo ...
self._populate_solver(pruned)
def remove_reactions(self, reactions, remove_orphans=False):
"""Remove reactions from the model.
The change is reverted upon exit when using the model as a context.
Parameters
----------
reactions : list
A list with reactions (`cobra.Reaction`), or their id's, to remove
remove_orphans : bool
Remove orphaned genes and metabolites from the model as well
"""
if isinstance(reactions, string_types) or hasattr(reactions, "id"):
warn("need to pass in a list")
reactions = [reactions]
context = get_context(self)
for reaction in reactions:
# Make sure the reaction is in the model
try:
reaction = self.reactions[self.reactions.index(reaction)]
except ValueError:
warn('%s not in %s' % (reaction, self))
else:
forward = reaction.forward_variable
reverse = reaction.reverse_variable
if context:
context(partial(self._populate_solver, [reaction]))
context(partial(setattr, reaction, '_model', self))
context(partial(self.reactions.add, reaction))
self.remove_cons_vars([forward, reverse])
self.reactions.remove(reaction)
reaction._model = None
for met in reaction._metabolites:
if reaction in met._reaction:
met._reaction.remove(reaction)
if context:
context(partial(met._reaction.add, reaction))
if remove_orphans and len(met._reaction) == 0:
self.remove_metabolites(met)
for gene in reaction._genes:
if reaction in gene._reaction:
gene._reaction.remove(reaction)
if context:
context(partial(gene._reaction.add, reaction))
if remove_orphans and len(gene._reaction) == 0:
self.genes.remove(gene)
if context:
context(partial(self.genes.add, gene))
# remove reference to the reaction in all groups
associated_groups = self.get_associated_groups(reaction)
for group in associated_groups:
group.remove(reaction)
def add_groups(self, group_list):
"""Add groups to the model.
Groups with identifiers identical to a group already in the model are
ignored.
If any group contains members that are not in the model, these members
are added to the model as well. Only metabolites, reactions, and genes
can have groups.
Parameters
----------
group_list : list
A list of `cobra.Group` objects to add to the model.
"""
def existing_filter(group):
if group.id in self.groups:
LOGGER.warning(
"Ignoring group '%s' since it already exists.", group.id)
return False
return True
if isinstance(group_list, string_types) or \
hasattr(group_list, "id"):
warn("need to pass in a list")
group_list = [group_list]
pruned = DictList(filter(existing_filter, group_list))
for group in pruned:
group._model = self
for member in group.members:
# If the member is not associated with the model, add it
if isinstance(member, Metabolite):
if member not in self.metabolites:
self.add_metabolites([member])
if isinstance(member, Reaction):
if member not in self.reactions:
self.add_reactions([member])
# TODO(midnighter): `add_genes` method does not exist.
# if isinstance(member, Gene):
# if member not in self.genes:
# self.add_genes([member])
self.groups += [group]
def remove_groups(self, group_list):
"""Remove groups from the model.
Members of each group are not removed
from the model (i.e. metabolites, reactions, and genes in the group
stay in the model after any groups containing them are removed).
Parameters
----------
group_list : list
A list of `cobra.Group` objects to remove from the model.
"""
if isinstance(group_list, string_types) or \
hasattr(group_list, "id"):
warn("need to pass in a list")
group_list = [group_list]
for group in group_list:
# make sure the group is in the model
if group.id not in self.groups:
LOGGER.warning("%r not in %r. Ignored.", group, self)
else:
self.groups.remove(group)
group._model = None
def get_associated_groups(self, element):
"""Returns a list of groups that an element (reaction, metabolite, gene)
is associated with.
Parameters
----------
element: `cobra.Reaction`, `cobra.Metabolite`, or `cobra.Gene`
Returns
-------
list of `cobra.Group`
All groups that the provided object is a member of
"""
# check whether the element is associated with the model
return [g for g in self.groups if element in g.members]
def add_cons_vars(self, what, **kwargs):
"""Add constraints and variables to the model's mathematical problem.
Useful for variables and constraints that can not be expressed with
reactions and simple lower and upper bounds.
Additions are reversed upon exit if the model itself is used as
context.
Parameters
----------
what : list or tuple of optlang variables or constraints.
The variables or constraints to add to the model. Must be of
class `optlang.interface.Variable` or
`optlang.interface.Constraint`.
**kwargs : keyword arguments
Passed to solver.add()
"""
add_cons_vars_to_problem(self, what, **kwargs)
def remove_cons_vars(self, what):
"""Remove variables and constraints from the model's mathematical
problem.
Remove variables and constraints that were added directly to the
model's underlying mathematical problem. Removals are reversed
upon exit if the model itself is used as context.
Parameters
----------
what : list or tuple of optlang variables or constraints.
The variables or constraints to add to the model. Must be of
class `optlang.interface.Variable` or
`optlang.interface.Constraint`.
"""
remove_cons_vars_from_problem(self, what)
@property
def problem(self):
"""The interface to the model's underlying mathematical problem.
Solutions to cobra models are obtained by formulating a mathematical
problem and solving it. Cobrapy uses the optlang package to
accomplish that and with this property you can get access to the
problem interface directly.
Returns
-------
optlang.interface
The problem interface that defines methods for interacting with
the problem and associated solver directly.
"""
return self.solver.interface
@property
def variables(self):
"""The mathematical variables in the cobra model.
In a cobra model, most variables are reactions. However,
for specific use cases, it may also be useful to have other types of
variables. This property defines all variables currently associated
with the model's problem.
Returns
-------
optlang.container.Container
A container with all associated variables.
"""
return self.solver.variables
@property
def constraints(self):
"""The constraints in the cobra model.
In a cobra model, most constraints are metabolites and their
stoichiometries. However, for specific use cases, it may also be
useful to have other types of constraints. This property defines all
constraints currently associated with the model's problem.
Returns
-------
optlang.container.Container
A container with all associated constraints.
"""
return self.solver.constraints
@property
def boundary(self):
"""Boundary reactions in the model.
Reactions that either have no substrate or product.
"""
return [rxn for rxn in self.reactions if rxn.boundary]
@property
def exchanges(self):
"""Exchange reactions in model.
Reactions that exchange mass with the exterior. Uses annotations
and heuristics to exclude non-exchanges such as sink reactions.
"""
return find_boundary_types(self, "exchange", None)
@property
def demands(self):
"""Demand reactions in model.
Irreversible reactions that accumulate or consume a metabolite in
the inside of the model.
"""
return find_boundary_types(self, "demand", None)
@property
def sinks(self):
"""Sink reactions in model.
Reversible reactions that accumulate or consume a metabolite in
the inside of the model.
"""
return find_boundary_types(self, "sink", None)
def _populate_solver(self, reaction_list, metabolite_list=None):
"""Populate attached solver with constraints and variables that
model the provided reactions.
"""
constraint_terms = AutoVivification()
to_add = []
if metabolite_list is not None:
for met in metabolite_list:
to_add += [self.problem.Constraint(
Zero, name=met.id, lb=0, ub=0)]
self.add_cons_vars(to_add)
for reaction in reaction_list:
if reaction.id not in self.variables:
reverse_lb, reverse_ub, forward_lb, forward_ub = \
separate_forward_and_reverse_bounds(*reaction.bounds)
forward_variable = self.problem.Variable(
reaction.id, lb=forward_lb, ub=forward_ub)
reverse_variable = self.problem.Variable(
reaction.reverse_id, lb=reverse_lb, ub=reverse_ub)
self.add_cons_vars([forward_variable, reverse_variable])
else:
reaction = self.reactions.get_by_id(reaction.id)
forward_variable = reaction.forward_variable
reverse_variable = reaction.reverse_variable
for metabolite, coeff in six.iteritems(reaction.metabolites):
if metabolite.id in self.constraints:
constraint = self.constraints[metabolite.id]
else:
constraint = self.problem.Constraint(
Zero,
name=metabolite.id,
lb=0, ub=0)
self.add_cons_vars(constraint, sloppy=True)
constraint_terms[constraint][forward_variable] = coeff
constraint_terms[constraint][reverse_variable] = -coeff
self.solver.update()
for constraint, terms in six.iteritems(constraint_terms):
constraint.set_linear_coefficients(terms)
def slim_optimize(self, error_value=float('nan'), message=None):
"""Optimize model without creating a solution object.
Creating a full solution object implies fetching shadow prices and
flux values for all reactions and metabolites from the solver
object. This necessarily takes some time and in cases where only one
or two values are of interest, it is recommended to instead use this
function which does not create a solution object returning only the
value of the objective. Note however that the `optimize()` function
uses efficient means to fetch values so if you need fluxes/shadow
prices for more than say 4 reactions/metabolites, then the total
speed increase of `slim_optimize` versus `optimize` is expected to
be small or even negative depending on how you fetch the values
after optimization.
Parameters
----------
error_value : float, None
The value to return if optimization failed due to e.g.
infeasibility. If None, raise `OptimizationError` if the
optimization fails.
message : string
Error message to use if the model optimization did not succeed.
Returns
-------
float
The objective value.
"""
self.solver.optimize()
if self.solver.status == optlang.interface.OPTIMAL:
return self.solver.objective.value
elif error_value is not None:
return error_value
else:
assert_optimal(self, message)
def optimize(self, objective_sense=None, raise_error=False):
"""
Optimize the model using flux balance analysis.
Parameters
----------
objective_sense : {None, 'maximize' 'minimize'}, optional
Whether fluxes should be maximized or minimized. In case of None,
the previous direction is used.
raise_error : bool
If true, raise an OptimizationError if solver status is not
optimal.
Notes
-----
Only the most commonly used parameters are presented here. Additional
parameters for cobra.solvers may be available and specified with the
appropriate keyword argument.
"""
original_direction = self.objective.direction
self.objective.direction = \
{"maximize": "max", "minimize": "min"}.get(