Skip to content

Commit

Permalink
Merge pull request #38 from cdanielmachado/develop
Browse files Browse the repository at this point in the history
integrate development branch
  • Loading branch information
cdanielmachado committed Oct 9, 2019
2 parents 0027e67 + 6e00d79 commit 52047f2
Show file tree
Hide file tree
Showing 34 changed files with 1,236 additions and 542 deletions.
20 changes: 2 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1048261.svg)](https://doi.org/10.5281/zenodo.1048261) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![PyPI version](https://badge.fury.io/py/framed.svg)](https://badge.fury.io/py/framed) [![Documentation Status](http://readthedocs.org/projects/framed/badge/?version=latest)](http://framed.readthedocs.io/en/latest/?badge=latest)

### Note: this package will soon be deprecated! Please check the new [ReFramed](https://github.com/cdanielmachado/reframed) package.

![FRAMED](logo_300px.png)

## A python FRAmework for Metabolic Engineering and Design
Expand All @@ -21,24 +23,6 @@
* Calibration from metabolomics data
* Bioprocess modeling:
* Dynamic FBA (single and multi-species)
* Microbial community modeling:
* SMETANA

### Documentation

For documentation and API please check: http://framed.readthedocs.io/

### Instalation from PyPI (stable releases)

```
pip install framed
```

### Instalation from github (latest development release)

```
pip install https://github.com/cdanielmachado/framed/archive/master.zip
```


### Credits and License
Expand Down
146 changes: 73 additions & 73 deletions examples/models/cbmodel_copy2.xml

Large diffs are not rendered by default.

144 changes: 72 additions & 72 deletions examples/models/cbmodel_copy3.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion scripts/framed-deletions
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ from framed.cobra.deletion import deletion, deleted_genes_to_reactions
from framed.io.sbml import load_cbmodel as sbml_read
from framed import FBA, pFBA, MOMA, lMOMA, ROOM
from itertools import chain
from framed.solvers.solver import Status
from framed.solvers.solution import Status
import warnings
import argparse
import re
Expand Down
2 changes: 1 addition & 1 deletion src/framed/bioreactor/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from copy import deepcopy
from ..solvers import solver_instance
from ..solvers.solver import Status
from framed.solvers.solution import Status
from scipy.integrate import ode
import numpy
import collections
Expand Down
2 changes: 1 addition & 1 deletion src/framed/bioreactor/bioreactors.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
__author__ = 'kaizhuang'

from .base import Bioreactor
from ..solvers.solver import Status
from framed.solvers.solution import Status

# value definition for oxygen_availability flag
ANAEROBIC = 0
Expand Down
2 changes: 1 addition & 1 deletion src/framed/cobra/ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from framed import solver_instance
from framed.model.transformation import disconnected_metabolites
from framed.solvers.solver import Status
from framed.solvers.solution import Status
from framed import load_cbmodel, save_cbmodel
from framed import FBA, pFBA

Expand Down
2 changes: 1 addition & 1 deletion src/framed/cobra/essentiality.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from __future__ import absolute_import

from ..solvers import solver_instance
from ..solvers.solver import Status
from framed.solvers.solution import Status
from .simulation import FBA
from .deletion import gene_deletion, reaction_deletion

Expand Down
2 changes: 1 addition & 1 deletion src/framed/cobra/phaseplane.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from builtins import object
from .simulation import FBA
from ..solvers import solver_instance
from ..solvers.solver import Status
from framed.solvers.solution import Status
import numpy
import matplotlib.pyplot as plt

Expand Down
3 changes: 2 additions & 1 deletion src/framed/cobra/reconstruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from framed.io.sbml import _load_compartments, _load_metabolites, _load_reactions, _load_cobra_bounds, _load_cobra_objective
from libsbml import SBMLReader, SBMLDocument, XMLNode
from re import match
from framed.solvers.solver import VarType, Status
from framed.solvers.solver import VarType
from framed.solvers.solution import Status
from numpy import where, array


Expand Down
11 changes: 5 additions & 6 deletions src/framed/cobra/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"""

from ..solvers import solver_instance
from ..solvers.solver import Status, VarType
from ..solvers.solver import VarType
from framed.solvers.solution import Status
from warnings import warn
from collections import Iterable

Expand Down Expand Up @@ -42,7 +43,8 @@ def FBA(model, objective=None, minimize=False, constraints=None, solver=None, ge
return solution


def pFBA(model, objective=None, obj_frac=None, minimize=False, constraints=None, reactions=None, solver=None):
def pFBA(model, objective=None, obj_frac=None, minimize=False, constraints=None, reactions=None, solver=None,
cleanup=True):
""" Run a parsimonious Flux Balance Analysis (pFBA) simulation:
Arguments:
Expand Down Expand Up @@ -102,13 +104,10 @@ def pFBA(model, objective=None, obj_frac=None, minimize=False, constraints=None,
objective[r_id] = 1

solution = solver.solve(objective, minimize=True, constraints=constraints)

#post process

solver.remove_constraint('obj')
solution.pre_solution = pre_solution

if solution.status == Status.OPTIMAL:
if cleanup:
for r_id in reactions:
if model.reactions[r_id].reversible:
pos, neg = r_id + '+', r_id + '-'
Expand Down
2 changes: 1 addition & 1 deletion src/framed/cobra/strain_design.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .deletion import deletion
from .simulation import pFBA
from .essentiality import essentiality
from ..solvers.solver import Status
from framed.solvers.solution import Status
from ..solvers import solver_instance


Expand Down
3 changes: 2 additions & 1 deletion src/framed/cobra/thermodynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

from framed.solvers.solver import OptimizationWarning
from ..solvers import solver_instance
from ..solvers.solver import Status, VarType
from ..solvers.solver import VarType
from framed.solvers.solution import Status
import numpy as np
import scipy as sp
from collections import OrderedDict
Expand Down
2 changes: 1 addition & 1 deletion src/framed/cobra/variability.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from collections import OrderedDict
from ..solvers import solver_instance
from ..solvers.solver import Status
from framed.solvers.solution import Status
from .simulation import FBA
from .thermodynamics import looplessFBA
from numpy import linspace
Expand Down
20 changes: 9 additions & 11 deletions src/framed/community/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from collections import OrderedDict

from framed.model.cbmodel import CBModel, CBReaction
from framed.model.model import Compartment, Metabolite
from framed.model.model import Compartment, Metabolite, ReactionType
from ..model.model import AttrOrderedDict
from warnings import warn
from copy import deepcopy
Expand Down Expand Up @@ -262,7 +262,7 @@ def _name_pattern(object_name, organism_name):
return "{} ({})".format(object_name, organism_name)

def _copy_object(obj, org_id, compartment=None):
new_obj = obj.copy()
new_obj = deepcopy(obj)
new_obj.id = _id_pattern(obj.id, org_id)
new_obj.name = _name_pattern(obj.name, org_id)
if compartment:
Expand All @@ -281,7 +281,6 @@ def _copy_object(obj, org_id, compartment=None):
raise RuntimeError("Biomass reaction not found in models: {}".format("', '".join(models_missing_biomass)))

merged_model = CBModel(self.id)
merged_model.biomass_reaction = None

organisms_biomass_metabolites = {}
community_metabolite_exchange_lookup = {}
Expand Down Expand Up @@ -329,7 +328,7 @@ def _copy_object(obj, org_id, compartment=None):

exch_id = _id_pattern("R_EX_" + m_id, "pool_blacklist")
exch_name = _name_pattern(met.name, "pool (blacklist) exchange")
blk_rxn = CBReaction(exch_id, name=exch_name, reversible=False, is_exchange=False, is_sink=True)
blk_rxn = CBReaction(exch_id, name=exch_name, reversible=False, reaction_type=ReactionType.SINK)
blk_rxn.stoichiometry[pool_id] = -1.0
community_metabolite_exchange_lookup[new_met.id] = exch_id
merged_model.add_reaction(blk_rxn)
Expand All @@ -341,18 +340,17 @@ def _copy_object(obj, org_id, compartment=None):

exch_id = _id_pattern("R_EX_" + m_id, "pool")
exch_name = _name_pattern(met.name, "pool exchange")
new_rxn = CBReaction(exch_id, name=exch_name, reversible=True, is_exchange=True)
new_rxn = CBReaction(exch_id, name=exch_name, reversible=True, reaction_type=ReactionType.EXCHANGE)
new_rxn.stoichiometry[pool_id] = -1.0
community_metabolite_exchange_lookup[new_met.id] = exch_id
merged_model.add_reaction(new_rxn)

for r_id, rxn in model.reactions.items():

is_exchange = rxn.is_exchange
is_exchange = rxn.reaction_type == ReactionType.EXCHANGE

if not is_exchange or not self._merge_extracellular_compartments:
new_rxn = _copy_object(rxn, org_id)
new_rxn.is_exchange = False

for m_id, coeff in rxn.stoichiometry.items():
m_blacklisted = m_id in self._exchanged_metabolites_blacklist
Expand All @@ -376,14 +374,14 @@ def _copy_object(obj, org_id, compartment=None):
self._organisms_exchange_reactions[org_id][new_rxn.id] = cnm

if not self.interacting:
sink_rxn = CBReaction('Sink_{}'.format(new_id), is_exchange=False, is_sink=True, reversible=False)
sink_rxn = CBReaction('Sink_{}'.format(new_id), reaction_type=ReactionType.SINK, reversible=False)
sink_rxn.stoichiometry = {new_id: -1}
sink_rxn.lb = 0.0
merged_model.add_reaction(sink_rxn)
elif m_blacklisted:
pool_blacklist_id = _id_pattern(m_id, "pool_blacklist")
blacklist_export_rxn = CBReaction('R_EX_BLACKLIST_{}'.format(new_id),
is_exchange=False, is_sink=False,
reaction_type=ReactionType.OTHER,
reversible=False)
blacklist_export_rxn.stoichiometry = {new_id: -1, pool_blacklist_id: 1}
blacklist_export_rxn.lb = 0.0
Expand Down Expand Up @@ -426,7 +424,7 @@ def _copy_object(obj, org_id, compartment=None):
continue

new_rxn = deepcopy(rxn)
new_rxn.is_exchange = True
new_rxn.reaction_type = ReactionType.EXCHANGE
if rxn.id == model.biomass_reaction and self._create_biomass:
new_rxn.reversible = False
new_rxn.objective = False
Expand All @@ -446,7 +444,7 @@ def _copy_object(obj, org_id, compartment=None):

if self._create_biomass:
biomass_rxn = CBReaction('R_Community_Growth', name="Community Growth",
reversible=False, is_exchange=False, is_sink=True, objective=1.0)
reversible=False, reaction_type=ReactionType.SINK, objective=1.0)
for org_biomass in organisms_biomass_metabolites.values():
biomass_rxn.stoichiometry[org_biomass] = -1

Expand Down
130 changes: 130 additions & 0 deletions src/framed/community/model2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@

from ..model.model import AttrOrderedDict, ReactionType
from framed.model.cbmodel import CBModel, CBReaction
from framed.model.model import Compartment, Metabolite

from builtins import object


class Community(object):
def __init__(self, community_id, models, copy_models=False):
self.id = community_id
self.organisms = AttrOrderedDict()
self._merged_model = None
self.reaction_map = None
self.metabolite_map = None

for model in models:
self.organisms[model.id] = model.copy() if copy_models else model

def size(self):
return len(self.organisms)

@property
def merged_model(self):
if self._merged_model is None:
self._merged_model = self.merge_models()

return self._merged_model

def merge_models(self):
comm_model = CBModel(self.id)
old_ext_comps = []
ext_mets = []
self.reaction_map = {}
self.metabolite_map = {}

# default IDs
ext_comp_id = "ext"
biomass_id = "community_biomass"
comm_growth = "community_growth"

# create external compartment

comp = Compartment(ext_comp_id, "extracellular environment", external=True)
comm_model.add_compartment(comp)

# community biomass

met = Metabolite(biomass_id, "Total community biomass", ext_comp_id)
comm_model.add_metabolite(met, clear_tmp=False)

rxn = CBReaction(comm_growth, name="Community growth rate",
reversible=False, stoichiometry={biomass_id: -1},
lb=0, ub=None, objective=1)

comm_model.add_reaction(rxn, clear_tmp=False)

# add each organism

for org_id, model in self.organisms.items():

def rename(old_id):
return "{}_{}".format(old_id, org_id)

# add internal compartments

for c_id, comp in model.compartments.items():
if comp.external:
old_ext_comps.append(c_id)
else:
new_comp = Compartment(rename(c_id), comp.name)
comm_model.add_compartment(new_comp)

# add metabolites

for m_id, met in model.metabolites.items():
if met.compartment not in old_ext_comps: # if is internal
new_id = rename(m_id)
new_met = Metabolite(new_id, met.name, rename(met.compartment))
new_met.metadata = met.metadata.copy()
comm_model.add_metabolite(new_met)
self.metabolite_map[(org_id, m_id)] = new_id

elif m_id not in comm_model.metabolites: # if is external but was not added yet
new_met = Metabolite(m_id, met.name, ext_comp_id)
new_met.metadata = met.metadata.copy()
comm_model.add_metabolite(new_met, clear_tmp=False)
ext_mets.append(new_met.id)

# add internal reactions

for r_id, rxn in model.reactions.items():

if rxn.reaction_type == ReactionType.EXCHANGE:
continue

new_id = rename(r_id)
new_stoichiometry = {
m_id if m_id in ext_mets else rename(m_id): coeff
for m_id, coeff in rxn.stoichiometry.items()
}

if r_id == model.biomass_reaction:
new_stoichiometry[biomass_id] = 1

new_rxn = CBReaction(
new_id,
name=rxn.name,
reversible=rxn.reversible,
stoichiometry=new_stoichiometry,
reaction_type=rxn.reaction_type,
lb=rxn.lb,
ub=rxn.ub,
)

comm_model.add_reaction(new_rxn, clear_tmp=False)
new_rxn.metadata = rxn.metadata.copy()
self.reaction_map[(org_id, r_id)] = new_id

# Add exchange reactions

for m_id in ext_mets:
rxn = CBReaction("R_EX_{}".format(m_id),
reversible=True,
stoichiometry={m_id: -1},
reaction_type=ReactionType.EXCHANGE)
comm_model.add_reaction(rxn, clear_tmp=False)

return comm_model

0 comments on commit 52047f2

Please sign in to comment.