-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathspecies.py
104 lines (80 loc) · 2.77 KB
/
species.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
"""Define the Species class, used as a base for Gene and Metabolite."""
from copy import deepcopy
from typing import TYPE_CHECKING, FrozenSet, Optional, Union
from ..core.object import Object
if TYPE_CHECKING:
from .. import Model
class Species(Object):
"""Species is a base class in Cobrapy.
Species is a class for holding information regarding
a chemical Species
Parameters
----------
id : string
An identifier for the chemical species
name : string
A human readable name.
"""
# noinspection PyShadowingBuiltins
def __init__(
self, id: Optional[str] = None, name: Optional[str] = None, **kwargs
) -> None:
"""Initialize a Species.
Parameters
----------
id : string, optional, default None
An identifier for the chemical species
name : string, optional, default None
A human readable name.
A species also contains a _model, reference to a cobra.model (initialized as
None) and a self._reaction, a set of cobra.reactions (initialized as empty set).
"""
super().__init__(id=id, name=name, **kwargs)
self._model = None
# references to reactions that operate on this species
self._reaction = set()
self._comparment = None
@property
def reactions(self) -> FrozenSet:
"""Return a frozenset of reactions.
Returns
-------
FrozenSet
A frozenset that includes the reactions of the species.
"""
return frozenset(self._reaction)
def __getstate__(self) -> dict:
"""Return the state of the species.
Remove the references to container reactions when serializing to
avoid problems associated with recursion.
Returns
-------
dict
A dictionary describing the state, without the self._reaction to avoid
recursion.
"""
state = Object.__getstate__(self)
state["_reaction"] = set()
return state
def copy(self) -> "Species":
"""Copy a species.
When copying a reaction, it is necessary to deepcopy the
components so the list references aren't carried over.
Additionally, a copy of a reaction is no longer in a cobra.Model.
This should be fixed with self.__deepcopy__ if possible
Returns
-------
Species
A copy of the species.
"""
return deepcopy(self)
@property
def model(self) -> Optional["Model"]:
"""Return the model.
Returns
-------
model
Returns the cobra model that the species is associated with. None if there
is no model associated with this species.
"""
return self._model