Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial attempt at compartments #1193

Draft
wants to merge 4 commits into
base: devel
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
some minor corrections
uri.akavia committed Mar 29, 2022
commit 5168db37683a984e2d4105d7e6378061ee411759
31 changes: 8 additions & 23 deletions src/cobra/core/compartment.py
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@

class Compartment(Group):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if inheriting from Group is really desirable or we should just have a similar class interface.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another point in favor is that .members is a bit awkward here. We rather want attributes .reactions and .metabolites.

"""
Manage groups via this implementation of the SBML group specification.
Manage compartments via this implementation of the SBML group specification.

`Compartment` is a class for holding information regarding a bounded space in
which species are located. You can think of it as a location in a model,
@@ -29,13 +29,14 @@ class Compartment(Group):
dimensions: float, optional
Compartments can have dimensions defined, if they are volume (3 dimensions) or
2 (a two-dimensional compartment, a surface, like a membrane). In theory, this
can be 1 or 0 dimensions, and even partial dimensions, but that will needlessly
complicate the math. The number of dimensions influences the size and units
used.
can be 1 or 0 dimensions, and even partial dimensions (fractal dimensions for
trees and vessels). For almost every application in constraint based modeling
this will be an integer. The number of dimensions influences the size and
units used.
"""
def __init__(self, id: str, name: str = "", members: Optional[Iterable] = None,
dimensions: Optional[float] = None):
"""Initialize the group object.
"""Initialize the Compartment object.

id : str
The identifier to associate with this group
@@ -57,8 +58,8 @@ def __init__(self, id: str, name: str = "", members: Optional[Iterable] = None,
super().__init__(id, name, members)
for x in members:
if not isinstance(x, (Metabolite, Reaction)):
raise(TypeError, f"Compartments should have only "
f"reactions or metabolites. {x} is a {type(x)}.")
raise(TypeError, f"Compartments should only have reactions or "
f"metabolites as members. {x} is a {type(x)}.")
self._members = DictList() if members is None else DictList(members)
self._compartment_type = None
self.__delattr__("kind") # Compartments don't have kind
@@ -163,22 +164,6 @@ def assigned_reactions(self) -> FrozenSet[Reaction]:
"""
return frozenset(self._members.query(lambda x: isinstance(x, Reaction)))

@property
def reactions(self) -> FrozenSet[Reaction]:
"""Return the reactions who belong to this compartment.

This is returned as a FrozenSet of reactions for each metabolite in the
compartment, if any, and the reactions that were assigned to this compartment
directly.

Returns
-------
FrozenSet of cobra.Reactions
Reactions that belong to this compartment, both assigned and inferred.
"""
assigned_reactions = set(self.assigned_reactions)
return frozenset(assigned_reactions.union(self.inferred_reactions))

def __contains__(self, member: Union[Metabolite, Reaction]):
return self.members.__contains__(member)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we keep members then more pythonic would be

Suggested change
return self.members.__contains__(member)
return member in self.members