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

refactor: turn compartments into objects #725

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion cobra/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from cobra import flux_analysis, io
from cobra.core import (
DictList, Gene, Metabolite, Model, Object, Reaction, Species)
DictList, Gene, Metabolite, Model, Object, Reaction, Species, Compartment)
from cobra.util import show_versions

__version__ = "0.13.4"
Expand Down
1 change: 1 addition & 0 deletions cobra/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
from cobra.core.reaction import Reaction
from cobra.core.solution import Solution, LegacySolution, get_solution
from cobra.core.species import Species
from cobra.core.compartment import Compartment
76 changes: 76 additions & 0 deletions cobra/core/compartment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# -*- coding: utf-8 -*-

"""Provide a class for compartments."""

from __future__ import absolute_import

from copy import deepcopy
from cobra.util import format_long_string, is_not_sane
from six import string_types

from cobra.core.object import Object


class Compartment(Object):
"""
Compartment is a class for holding information regarding
a compartment in a cobra.Model object

Parameters
----------
id : string
An identifier for the compartment
name : string
A human readable name.

"""
def __init__(self, id=None, name=""):
super(Compartment, self).__init__(id=id, name=name)
self._id = None
self.id = id

def __contains__(self, metabolite):
return metabolite.compartment is self

def __eq__(self, other):
if self is other:
return True
if isinstance(other, string_types):
return self._id == other
if isinstance(other, Compartment):
return self._id == other.id
else:
return False

def __ne__(self, other):
return not self.__eq__(other)

def __hash__(self):
return hash(self._id)

def copy(self):
return deepcopy(self)

@property
def id(self):
return self._id

@id.setter
def id(self, value):
if is_not_sane(value):
raise TypeError("The compartment ID must be a non-empty string")
self._id = value

def _repr_html_(self):
return """
<table>
<tr>
<td><strong>Compartment identifier</strong></td><td>{id}</td>
</tr><tr>
<td><strong>Name</strong></td><td>{name}</td>
</tr><tr>
<td><strong>Memory address</strong></td>
<td>{address}</td>
</tr>
</table>""".format(id=self.id, name=format_long_string(self.name),
address='0x0%x' % id(self))
30 changes: 27 additions & 3 deletions cobra/core/metabolite.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from cobra.core.formula import elements_and_molecular_weights
from cobra.core.species import Species
from cobra.util.solver import check_solver_status
from cobra.util.util import format_long_string
from cobra.util.util import format_long_string, is_not_sane
from cobra.core.compartment import Compartment


# Numbers are not required because of the |(?=[A-Z])? block. See the
Expand All @@ -35,15 +36,16 @@ class Metabolite(Species):
A human readable name.
charge : float
The charge number of the metabolite
compartment: str or None
Compartment of the metabolite.
compartment: cobra.Compartment, str or None
Compartment or compartment ID that the the metabolite is in.
"""

def __init__(self, id=None, formula=None, name="",
charge=None, compartment=None):
Species.__init__(self, id, name)
self.formula = formula
# because in a Model a metabolite may participate in multiple Reactions
self._compartment = None
self.compartment = compartment
self.charge = charge

Expand All @@ -58,6 +60,28 @@ def _set_id_with_model(self, value):
self._id = value
self.model.metabolites._generate_index()

@property
def compartment(self):
return self._compartment

@compartment.setter
def compartment(self, value):
if value is None:
self._compartment = None
elif isinstance(value, Compartment):
if self._model and value.id in self._model.compartments:
self._compartment = \
self._model.compartments.get_by_id(value.id)
else:
self._compartment = value
elif not is_not_sane(value):
if self._model and value in self._model.compartments:
self._compartment = self._model.compartments.get_by_id(value)
else:
self._compartment = Compartment(value)
else:
raise TypeError("The compartment ID must be a non-empty string")

@property
def constraint(self):
"""Get the constraints associated with this metabolite from the solve
Expand Down
Loading