Skip to content

Commit

Permalink
fix: skip check of boundary types without such reactions (#733)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianLieven authored and Midnighter committed Jul 20, 2018
1 parent 93bf0eb commit 5cc0bbf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
23 changes: 19 additions & 4 deletions cobra/medium/boundary_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,23 @@ def find_external_compartment(model):
str
The putative external compartment.
"""
if not model.boundary:
LOGGER.error("The heuristic for discovering an external compartment "
"relies on boundary reactions. Yet, there are no "
"boundary reactions in this model.")
raise RuntimeError(
"The external compartment cannot be identified. "
"The heuristic for discovering an external compartment "
"relies on boundary reactions. Yet, there are no "
"boundary reactions in this model.")
counts = Counter(tuple(r.compartments)[0] for r in model.boundary)
most = counts.most_common(1)[0][0]
if "e" in model.compartments:
if most == "e":
return "e"
else:
LOGGER.warn("There is an `e` compartment but it does not look "
"like it is the actual external compartment.")
LOGGER.warning("There is an `e` compartment but it does not look "
"like it is the actual external compartment.")
return most
return most

Expand Down Expand Up @@ -105,7 +114,7 @@ def is_boundary_type(reaction, boundary_type, external_compartment):


def find_boundary_types(model, boundary_type, external_compartment=None):
"""Find exchange reactions.
"""Find specific boundary reactions.
Arguments
---------
Expand All @@ -121,8 +130,14 @@ def find_boundary_types(model, boundary_type, external_compartment=None):
Returns
-------
list of cobra.reaction
A list of likely exchange reactions.
A list of likely boundary reactions of a user defined type.
"""
if not model.boundary:
LOGGER.warning("There are no boundary reactions in this model. "
"Therefore specific types of boundary reactions such "
"as 'exchanges', 'demands' or 'sinks' cannot be "
"identified.")
return []
if external_compartment is None:
external_compartment = find_external_compartment(model)
return model.reactions.query(
Expand Down
10 changes: 10 additions & 0 deletions cobra/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ def data_directory():
return data_dir


@pytest.fixture(scope="session")
def empty_once():
return Model()


@pytest.fixture(scope="function")
def empty_model(empty_once):
return empty_once.copy()


@pytest.fixture(scope="session")
def small_model():
return create_test_model("textbook")
Expand Down
10 changes: 10 additions & 0 deletions cobra/test/test_medium.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,13 @@ def test_open_exchanges(self, model):
assert len(med) >= 3
med = medium.minimal_medium(model, 0.8, open_exchanges=100)
assert len(med) >= 3


class TestErrorsAndExceptions:

def test_no_boundary_reactions(self, empty_model):
assert medium.find_boundary_types(empty_model, 'e', None) == []

def test_no_boundary_reactions(self, empty_model):
with pytest.raises(RuntimeError):
medium.find_external_compartment(empty_model)

0 comments on commit 5cc0bbf

Please sign in to comment.