Skip to content

Instantly share code, notes, and snippets.

@phantomas1234
Created September 2, 2016 07:46
Show Gist options
  • Select an option

  • Save phantomas1234/87a800dbc58f5f23123a2b9ce4366734 to your computer and use it in GitHub Desktop.

Select an option

Save phantomas1234/87a800dbc58f5f23123a2b9ce4366734 to your computer and use it in GitHub Desktop.
Decompartmentalize models
from cameo import Model, Reaction
from re import sub
def decompartmentalize(model, rules=None):
if rules is None:
rules = {}
compartmentless_model = Model()
for reaction in model.reactions:
new_reaction = reaction.copy()
new_reaction.clear_metabolites()
new_metabolites = {}
for metabolite, coeff in reaction.metabolites.items():
new_metabolite = metabolite.copy()
try:
new_compartment = rules[metabolite.compartment]
except KeyError:
pass
else:
# for bigg models
new_metabolite.id = sub('_{}$'.format(metabolite.compartment), '_' + new_compartment, metabolite.id)
new_metabolite.compartment = new_compartment
new_metabolites[new_metabolite] = coeff
new_reaction.add_metabolites(new_metabolites)
compartmentless_model.add_reaction(new_reaction)
return compartmentless_model
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment