Skip to content

Commit

Permalink
rebase, integrated suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
maureencarey authored and Midnighter committed Aug 31, 2018
1 parent 54709f1 commit d1b086e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 43 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ branches:
only:
- master
- devel
- bug/prune_unused_metabolites
- /^[0-9]+\.[0-9]+\.[0-9]+[.0-9ab]*$/

env:
Expand Down
51 changes: 23 additions & 28 deletions cobra/manipulation/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,50 @@


def prune_unused_metabolites(cobra_model):
"""Remove metabolites that are not involved in any reactions
"""Remove metabolites that are not involved in any reactions and
returns pruned model
Parameters
----------
cobra_model: cobra.Model
the model to remove unused metabolites from
cobra_model: class:`~cobra.core.Model.Model` object
the model to remove unused metabolites from
Returns
-------
model
input model with unused metabolites removed
list
list of metabolites that were removed
output_model: class:`~cobra.core.Model.Model` object
input model with unused metabolites removed
inactive_metabolites: list of class:`~cobra.core.reaction.Reaction`
list of metabolites that were removed
"""
inactive_metabolites = []
active_metabolites = []

output_model = cobra_model.copy()
for the_metabolite in output_model.metabolites:
if len(the_metabolite._reaction) == 0:
inactive_metabolites.append(the_metabolite)
else:
active_metabolites.append(the_metabolite)
inactive_metabolites = [m for m in output_model.metabolites
if len(m.reactions) == 0]
output_model.remove_metabolites(inactive_metabolites)
return output_model, inactive_metabolites


def prune_unused_reactions(cobra_model):
"""Remove reactions that have no assigned metabolites
"""Remove reactions with no assigned metabolites, returns pruned model
Parameters
----------
cobra_model: cobra.Model
the model to remove unused reactions from
cobra_model: class:`~cobra.core.Model.Model` object
the model to remove unused reactions from
Returns
-------
model
input model with unused reactions removed
list
list of reactions that were removed
output_model: class:`~cobra.core.Model.Model` object
input model with unused reactions removed
reactions_to_prune: list of class:`~cobra.core.reaction.Reaction`
list of reactions that were removed
"""
pruned_reactions = []

output_model = cobra_model.copy()
reactions_to_prune = [x for x in output_model.reactions
if len(x._metabolites) == 0]
for the_reaction in reactions_to_prune:
pruned_reactions.append(the_reaction)
output_model.remove_reactions(pruned_reactions)
return output_model, pruned_reactions
reactions_to_prune = [r for r in output_model.reactions
if len(r.metabolites) == 0]
output_model.remove_reactions(reactions_to_prune)
return output_model, reactions_to_prune


def undelete_model_genes(cobra_model):
Expand Down
29 changes: 15 additions & 14 deletions cobra/test/test_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from cobra.core import Metabolite, Model, Reaction
from cobra.manipulation import *
from itertools import chain


class TestManipulation:
Expand Down Expand Up @@ -215,8 +216,7 @@ def test_validate_mass_balance(self, model):
with pytest.raises(ValueError), pytest.warns(UserWarning):
r1.check_mass_balance()

def test_prune_unused(self, model):

def test_prune_unused_mets_output_type(self, model):
# test that the output contains metabolite objects
metabolite = model.metabolites.ru5p__D_c
[model.reactions.get_by_id(x).remove_from_model() for x in
Expand All @@ -225,16 +225,15 @@ def test_prune_unused(self, model):
assert isinstance(model_pruned, Model)
assert isinstance(unused[0], Metabolite)

def test_prune_unused_mets_functionality(self, model):
# test that the unused metabolites are not used in the model
metabolite1 = model.metabolites.ru5p__D_c
metabolite2 = model.metabolites.akg_e
metabolite3 = model.metabolites.akg_c
reactions = list(set([x for sublist in
[metabolite1._reaction,
metabolite2._reaction,
metabolite3._reaction]
for x in sublist]))
[rxn.remove_from_model() for rxn in reactions]
reactions = set(chain(metabolite1.reactions,
metabolite2.reactions,
metabolite3.reactions))
model.remove_reactions(reactions)
model_pruned, unused = delete.prune_unused_metabolites(model)
assert metabolite1 in model.metabolites
assert metabolite2 in model.metabolites
Expand All @@ -243,20 +242,22 @@ def test_prune_unused(self, model):
assert metabolite2 not in model_pruned.metabolites
assert metabolite3 not in model_pruned.metabolites

def test_prune_unused_rxns_output_type(self, model):
# test that the output contains reaction objects
reaction = Reaction('foo')
model.add_reaction(reaction)
model_pruned, unused = delete.prune_unused_reactions(model)
assert isinstance(model_pruned, Model)
assert isinstance(unused[0], Reaction)

def test_prune_unused_rxns_functionality(self, model):
# test that the unused reactions are not used in the model
for x in ['foo1', 'foo2', 'foo3']:
model.add_reaction(Reaction(x))
model_pruned, unused = delete.prune_unused_reactions(model)
assert 'foo1' in [x.id for x in model.reactions]
assert 'foo2' in [x.id for x in model.reactions]
assert 'foo3' in [x.id for x in model.reactions]
assert 'foo1' not in [x.id for x in model_pruned.reactions]
assert 'foo2' not in [x.id for x in model_pruned.reactions]
assert 'foo3' not in [x.id for x in model_pruned.reactions]
assert 'foo1' in model.reactions
assert 'foo2' in model.reactions
assert 'foo3' in model.reactions
assert 'foo1' not in model_pruned.reactions
assert 'foo2' not in model_pruned.reactions
assert 'foo3' not in model_pruned.reactions

0 comments on commit d1b086e

Please sign in to comment.