diff --git a/modelseedpy/community/commhelper.py b/modelseedpy/community/commhelper.py index 273b180d..3073b669 100644 --- a/modelseedpy/community/commhelper.py +++ b/modelseedpy/community/commhelper.py @@ -28,13 +28,14 @@ def correct_nonMSID(nonMSobject, output, model_index): name, compartment = output index = 0 if compartment == "e" else model_index nonMSobject.compartment = compartment + str(index) - comp = re.search(r"(_\w\d+$)", nonMSobject.id) - if comp is None: return nonMSobject.id.replace(rf"[{compartment}]", f"_{nonMSobject.compartment}") + comp = re.search(r"(_[a-z]\d+$)", nonMSobject.id) + if comp is None and rf"[{compartment}]" in nonMSobject.id: return nonMSobject.id.replace(rf"[{compartment}]", f"_{nonMSobject.compartment}") + elif comp is None: return nonMSobject.id + f"_{nonMSobject.compartment}" return "_".join([nonMSobject.id.replace(comp.group(), ""), nonMSobject.compartment]) def build_from_species_models(org_models, model_id=None, name=None, abundances=None, - standardize=False, copy_models=True, printing=False): + standardize=False, MSmodel = True, copy_models=True, printing=False): """Merges the input list of single species metabolic models into a community metabolic model Parameters @@ -65,6 +66,7 @@ def build_from_species_models(org_models, model_id=None, name=None, abundances=N model_util = MSModelUtil(org_model, copy=copy_models) model_reaction_ids = [rxn.id for rxn in model_util.model.reactions] model_index += 1 + # if MSmodel: # Rename metabolites for met in model_util.model.metabolites: # Renaming compartments @@ -112,8 +114,7 @@ def build_from_species_models(org_models, model_id=None, name=None, abundances=N output = MSModelUtil.parse_id(rxn) if output is None: if printing: print(f"The {rxn.id} ({output}; {hasattr(rxn, 'compartment')}) is unpredictable.") - try: - rxn.id = correct_nonMSID(rxn, (rxn.id, "c"), model_index) + try: rxn.id = correct_nonMSID(rxn, (rxn.id, "c"), model_index) except ValueError: pass elif len(output) == 2: rxn.id = correct_nonMSID(rxn, output, model_index) elif len(output) == 3: @@ -128,6 +129,9 @@ def build_from_species_models(org_models, model_id=None, name=None, abundances=N if string_diff and not all(FBAHelper.isnumber(x) for x in string_diff): print(f"The ID {initialID} is changed with {string_diff} to create the final ID {finalID}") new_reactions.add(rxn) + # else: + # # TODO develop a method for compartmentalizing models without editing all reaction IDs or assuming their syntax + # pass # adds only unique reactions and metabolites to the community model newmodel = Model(model_id or "+".join([model.id for model in models]), name or "+".join([model.name for model in models])) diff --git a/modelseedpy/community/commscores.py b/modelseedpy/community/commscores_old.py similarity index 100% rename from modelseedpy/community/commscores.py rename to modelseedpy/community/commscores_old.py diff --git a/modelseedpy/community/mscommunity.py b/modelseedpy/community/mscommunity.py index 7ef9d1d9..6c30722b 100644 --- a/modelseedpy/community/mscommunity.py +++ b/modelseedpy/community/mscommunity.py @@ -103,7 +103,7 @@ def __init__(self, model=None, member_models: list = None, ids=None, abundances= if "cpd11416" not in msid_cobraid_hash: raise KeyError("Could not find biomass compound for the model.") other_biomass_cpds = [] for self.biomass_cpd in msid_cobraid_hash["cpd11416"]: - if self.biomass_cpd.compartment == "c0": + if "c" in self.biomass_cpd.compartment: for rxn in self.util.model.reactions: if self.biomass_cpd not in rxn.metabolites: continue print(self.biomass_cpd, rxn, end=";\t") diff --git a/modelseedpy/core/msminimalmedia.py b/modelseedpy/core/msminimalmedia.py index 0082f066..5baaab38 100644 --- a/modelseedpy/core/msminimalmedia.py +++ b/modelseedpy/core/msminimalmedia.py @@ -36,7 +36,7 @@ def _var_to_ID(var): def _compatibilize(org_models, printing=False): -from commscores import GEMCompatibility + from commscores import GEMCompatibility return GEMCompatibility.standardize(org_models, conflicts_file_name="standardization_corrections.json", printing=printing) @@ -61,7 +61,7 @@ def minimizeFlux_withGrowth(model_util, min_growth, obj): model_util.add_objective(obj, "min") # print(model_util.model.objective) # print([(cons.lb, cons.expression) for cons in model_util.model.constraints if "min" in cons.name]) - sol = model_util.model.optimize() + sol = model_util.model.optimize() # print(sol.objective_value) sol_dict = bioFlux_check(model_util.model, sol, min_growth=min_growth) return sol, sol_dict