-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from cdanielmachado/develop
integrate development branch
- Loading branch information
Showing
34 changed files
with
1,236 additions
and
542 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
Oops, something went wrong.