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

Fixing and adding test for combine kwarg in reaction.add_metabolites #503

Merged
merged 1 commit into from May 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 15 additions & 5 deletions cobra/core/reaction.py
Expand Up @@ -10,7 +10,7 @@
from operator import attrgetter
from warnings import warn

from six import iteritems, string_types
from six import iteritems, iterkeys, string_types
from future.utils import raise_from, raise_with_traceback

from cobra.exceptions import OptimizationError
Expand Down Expand Up @@ -799,10 +799,20 @@ def add_metabolites(self, metabolites_to_add, combine=True,

context = get_context(self)
if context and reversibly:
# Just subtract the metabolites that were added
context(partial(
self.subtract_metabolites, metabolites_to_add, combine=True,
reversibly=False))
if combine:
# Just subtract the metabolites that were added
context(partial(
self.subtract_metabolites, metabolites_to_add,
combine=True, reversibly=False))
else:
# Reset them with add_metabolites
mets_to_reset = {
key: old_coefficients[model.metabolites.get_by_any(key)[0]]
for key in iterkeys(metabolites_to_add)}

context(partial(
self.add_metabolites, mets_to_reset,
combine=False, reversibly=False))

def subtract_metabolites(self, metabolites, combine=True, reversibly=True):
"""This function will 'subtract' metabolites from a reaction, which
Expand Down
12 changes: 12 additions & 0 deletions cobra/test/test_model.py
Expand Up @@ -142,6 +142,18 @@ def test_add_metabolite(self, model):
model.metabolites.get_by_id("g6p_c")] == -1
assert model.metabolites.h_c not in reaction._metabolites

# Test combine=False
reaction = model.reactions.get_by_id("ATPM")
old_stoich = reaction._metabolites[
model.metabolites.get_by_id("h2o_c")]
with model:
reaction.add_metabolites({'h2o_c': 2.5}, combine=False)
assert reaction._metabolites[
model.metabolites.get_by_id("h2o_c")] == 2.5

assert reaction._metabolites[
model.metabolites.get_by_id("h2o_c")] == old_stoich

# test adding to a new Reaction
reaction = Reaction("test")
assert len(reaction._metabolites) == 0
Expand Down