Example #1
0
    def __swap_modelseed_compound(self, model_metabolite: Metabolite,
                                  new_compound_ontology_id: int):
        """
        Method to swap a metabolite in the Model SEED database format.
        This method tries to find the Model SEED format of the new compound. If it does not find, it will change into an
        arbitrary id with the Model SEED or the Ontology format.

        :param Model.Metabolite model_metabolite: metabolite to be replaced.
        :param int new_compound_ontology_id: ontology identifier of the metabolite that will replace the other
        :return:
        """

        old_inchikey = ""
        if "inchi_key" in model_metabolite.annotation.keys():
            old_inchikey = model_metabolite.annotation["inchi_key"]

        old_aliases = AnnotationUtils.get_annotation_from_cobra_annotation(
            model_metabolite)
        old_id = model_metabolite.id

        model_metabolite.annotation = {}
        compound_container = self.__compounds_ontology.get_node_by_ont_id(
            new_compound_ontology_id)

        new_aliases = self.__compoundsIdConverter.get_all_aliases_by_modelSeedID(
            compound_container.model_seed_id)
        annotation = AnnotationUtils.get_compound_annotation_format_by_aliases(
            new_aliases)

        model_metabolite.annotation = annotation

        new_inchikey = ""
        if not compound_container.generic:
            model_metabolite.annotation[
                "inchi_key"] = compound_container.inchikey
            new_inchikey = compound_container.inchikey

        model_metabolite.annotation["smiles"] = compound_container.smiles
        model_metabolite.formula = compound_container.formula
        model_metabolite.charge = compound_container.charge
        compartment = model_metabolite.compartment
        db_id = compound_container.model_seed_id
        model_metabolite.name = compound_container.name

        if db_id:

            self.__check_if_id_is_used_in_model_and_delete_it(
                compound_container.model_seed_id + "_" + compartment)

            model_metabolite.id = compound_container.model_seed_id + "_" + compartment

            self.mapper.update_maps(old_inchikey, new_inchikey, old_id,
                                    model_metabolite.id,
                                    new_compound_ontology_id, old_aliases,
                                    new_aliases)
        else:
            self.__change_boimmg_format_metabolite(model_metabolite,
                                                   compound_container)
Example #2
0
    def __change_boimmg_format_metabolite(self, model_metabolite: Metabolite,
                                          compound_container):
        """
        This method changes the id of the metabolite arbitrarily. The format of the metabolite is set for the
        ontology format.
        It is worth noting that the :param compound_container can be either a ModelSeedCompound or an OntologyCompound

        :param Model.Metabolite model_metabolite: metabolite to be replaced.
        :param CompoundContainer compound_container: compound that will replace the other.
        :return:
        """

        old_inchikey = ""
        if "inchi_key" in model_metabolite.annotation.keys():
            old_inchikey = model_metabolite.annotation["inchi_key"]

        old_aliases = AnnotationUtils.get_annotation_from_cobra_annotation(
            model_metabolite)
        old_id = model_metabolite.id

        aliases = self.__compoundsIdConverter.get_all_aliases_by_modelSeedID(
            compound_container.model_seed_id)
        annotation = {}
        if aliases:
            annotation = AnnotationUtils.get_compound_annotation_format_by_aliases(
                aliases)

        annotation["boimmg.compound"] = self.__compoundsAnnotationConfigs["BOIMMG_ID_CONSTRUCTION"]+\
                                        str(compound_container.id)

        new_aliases = {"BOIMMG": [annotation["boimmg.compound"]]}
        model_metabolite.annotation = annotation

        new_inchikey = ""
        if not compound_container.generic:
            model_metabolite.annotation[
                "inchi_key"] = compound_container.inchikey
            new_inchikey = compound_container.inchikey

        model_metabolite.annotation["smiles"] = compound_container.smiles
        model_metabolite.formula = compound_container.formula
        model_metabolite.charge = compound_container.charge
        model_metabolite.name = compound_container.name
        compartment = model_metabolite.compartment

        id = self.__compoundsAnnotationConfigs["BOIMMG_ID_CONSTRUCTION"] + str(
            compound_container.id)

        new_metabolite_id = id + "_" + compartment

        self.__check_if_id_is_used_in_model_and_delete_it(new_metabolite_id)

        self.report_material[model_metabolite.id] = new_metabolite_id
        model_metabolite.id = new_metabolite_id

        self.mapper.update_maps(old_inchikey, new_inchikey, old_id,
                                model_metabolite.id, compound_container.id,
                                old_aliases, new_aliases)
Example #3
0
def generate_boimmg_metabolites_in_compartment(compound : CompoundNode,
                                compartment: str, compoundsIdConverter : CompoundsIDConverter,
                                compoundsAnnotationConfigs) -> Metabolite:
    """
    Generate an arbitrary format for the metabolites

    :param model:
    :param compound:
    :param database_format:
    :param compoundsIdConverter:
    :return:
    """

    modelseedid = compound.model_seed_id

    id = compoundsAnnotationConfigs["BOIMMG_ID_CONSTRUCTION"]+ str(compound.id)

    formula = compound.formula
    name = compound.name
    charge = compound.charge

    if charge:
        charge = int(charge)
    else:
        charge = 0

    new_metabolite = Metabolite(id=id+"_"+compartment, name=name, charge=charge, formula=formula)

    aliases = compound.aliases
    annotation = AnnotationUtils.get_compound_annotation_format_by_aliases(aliases)
    if modelseedid:
        aliases = compoundsIdConverter.get_all_aliases_by_modelSeedID(modelseedid)

        annotation = AnnotationUtils.get_compound_annotation_format_by_aliases(aliases)

    annotation["boimmg.compound"] = compoundsAnnotationConfigs["BOIMMG_ID_CONSTRUCTION"] + \
                                    str(compound.id)

    new_metabolite.annotation = annotation
    new_metabolite.annotation["smiles"] = compound.smiles
    new_metabolite.annotation["inchikey"] = compound.inchikey
    new_metabolite.compartment = compartment

    return new_metabolite
Example #4
0
def generate_bigg_reaction(modelseed_reaction,reaction_found_compounds,
                           compartment,reactionsIdConverter,
                           compoundsIdConverter):
    """
    This function generates a new BiGG format reaction in a given compartment.

    :param ModelSeedReaction modelseed_reaction: a Model SEED reaction container
    :param dict reaction_found_compounds: dictionary with model seed compounds identifiers as keys and the
    respective Model.Metabolite as value.
    :param Model.Compartment compartment:
    :param ReactionsIDConverter reactionsIdConverter: a reaction identifier converter
    :param CompoundsIDConverter compoundsIdConverter: a compounds identifier converter
    :return cobrapy.Reaction: the new model reaction
    """

    bigg_reaction_ids = \
    reactionsIdConverter.convert_modelSeedId_into_other_dbID(modelseed_reaction.getDbId(), "BiGG")

    bigg_reaction = None
    bigg_id = None
    found = False
    i = 0
    while not found and i < len(bigg_reaction_ids):
        try:
            bigg_reaction = bigg.get_bigg_reaction(bigg_reaction_ids[i])
            bigg_id = bigg_reaction_ids[i]
            found = True
        except:
            i += 1

    if bigg_reaction:
        bigg_metabolites = bigg_reaction.get("metabolites")

        new_stoichiometry = {}

        for compound in bigg_metabolites:
            compound_id = compound.get("bigg_id")
            modelseedid = compoundsIdConverter.convert_dbID_into_modelSeedId("BiGG", compound_id)[0]
            model_compound = reaction_found_compounds[modelseedid]
            new_stoichiometry[model_compound] = compound.get("stoichiometry")

        id = bigg_id + "_" + compartment
        name = bigg_reaction.get("name")
        reversibility = modelseed_reaction.getReversibility()

        if reversibility:
            new_reaction_model = Reaction(id, name, lower_bound=None)

        else:
            new_reaction_model = Reaction(id, name)

        new_reaction_model.add_metabolites(new_stoichiometry)
        aliases = reactionsIdConverter.get_all_aliases_by_modelSeedID(modelseed_reaction.getDbId())
        new_reaction_model.annotation = AnnotationUtils.get_reaction_annotation_format_by_aliases(aliases)
        return new_reaction_model
Example #5
0
def generate_kegg_compound(model,modelseedid,compoundsIdConverter,modelseedCompoundsDb, compartment=""):
    """
    Method to generate metabolites for each compartment of the model in the KEGG database format.
    This method tries to find the KEGG format of the new compounds. If it does not find, it will change it into the
    Model SEED format.

    :param cobrapy.Model model: cobrapy Model
    :param string modelseedid: model seed identifier of the compound
    :param CompoundsIDConverter compoundsIdConverter: a converter
    :param ModelSeedCompoundsDB modelseedCompoundsDb: the compounds Model SEED database
    :param (Optional) string compartment: compartment to introduce the new compound
    :return list: new compounds list
    """

    if modelseedid and "KEGG" in compoundsIdConverter.get_modelSeedIdToDb().get(modelseedid):
        kegg_id = compoundsIdConverter.convert_modelSeedId_into_other_dbID(modelseedid, "KEGG")[0]
        kegg_metabolite = KeggCompound(kegg_id)

        aliases = compoundsIdConverter.get_all_aliases_by_modelSeedID(modelseedid)
        annotation = AnnotationUtils.get_compound_annotation_format_by_aliases(aliases)

        compounds = []
        if not compartment:
            for compartment in model.compartments:

                model_metabolite = Metabolite(kegg_id + "_" + compartment)
                model_metabolite.annotation = annotation
                model_metabolite.annotation["inchikey"] = kegg_metabolite.get_inchikey()
                model_metabolite.annotation["smiles"] = kegg_metabolite.get_smiles()
                model_metabolite.formula = kegg_metabolite.get_formula()
                model_metabolite.name = kegg_metabolite.get_name()
                model_metabolite.charge = 0
                model_metabolite.compartment = compartment
                compounds.append(model_metabolite)

        else:
            model_metabolite = Metabolite(kegg_id + "_" + compartment)
            model_metabolite.annotation = annotation
            model_metabolite.annotation["inchikey"] = kegg_metabolite.get_inchikey()
            model_metabolite.annotation["smiles"] = kegg_metabolite.get_smiles()
            model_metabolite.formula = kegg_metabolite.get_formula()
            model_metabolite.name = kegg_metabolite.get_name()
            model_metabolite.charge = 0
            model_metabolite.compartment = compartment
            compounds.append(model_metabolite)

        return compounds



    else:
        return generate_modelseed_compound(model, modelseedid, compoundsIdConverter,modelseedCompoundsDb)
Example #6
0
def generate_modelseed_compound(model,model_seed_id,compoundsIdConverter,modelseedCompoundsDb, compartment=""):
    """
    Method to generate metabolites for each compartment of the model in the Model SEED database format.
    This method tries to find the Model SEED format of the new compounds.

    :param cobrapy.Model model: cobrapy Model
    :param string model_seed_id: model seed identifier of the compound
    :param CompoundsIDConverter compoundsIdConverter: a converter
    :param ModelSeedCompoundsDB modelseedCompoundsDb: the compounds Model SEED database
    :param (Optional) string compartment: compartment to introduce the new compound
    :return list: new compounds list
    """

    modelseed_compound = modelseedCompoundsDb.get_compound_by_id(model_seed_id)

    aliases = compoundsIdConverter.get_all_aliases_by_modelSeedID(model_seed_id)
    annotation = AnnotationUtils.get_compound_annotation_format_by_aliases(aliases)
    compounds = []

    if not compartment:

        for compartment in model.compartments:
            db_id = modelseed_compound.getDbId()
            model_metabolite = Metabolite(db_id + "_" + compartment)
            model_metabolite.annotation = annotation
            model_metabolite.annotation["inchikey"] = modelseed_compound.getInchikey()
            model_metabolite.formula = modelseed_compound.getFormula()
            model_metabolite.charge = modelseed_compound.getCharge()
            model_metabolite.name = modelseed_compound.getName()
            model_metabolite.compartment=compartment
            compounds.append(model_metabolite)

    else:
        db_id = modelseed_compound.getDbId()
        model_metabolite = Metabolite(db_id + "_" + compartment)
        model_metabolite.annotation = annotation
        model_metabolite.annotation["inchikey"] = modelseed_compound.getInchikey()
        model_metabolite.formula = modelseed_compound.getFormula()
        model_metabolite.charge = modelseed_compound.getCharge()
        model_metabolite.name = modelseed_compound.getName()
        model_metabolite.compartment = compartment
        compounds.append(model_metabolite)

    return compounds
Example #7
0
def generate_modelseed_reaction(modelseed_reaction,reaction_found_compounds,compartment,reactionsIdConverter):
    """
    This function generates a new Model SEED format reaction in a given compartment.

    :param ModelSeedReaction modelseed_reaction: a Model SEED reaction container
    :param dict reaction_found_compounds: dictionary with model seed compounds identifiers as keys and the
    respective Model.Metabolite as value.
    :param Model.Compartment compartment:
    :param ReactionsIDConverter reactionsIdConverter: a reaction identifier converter
    :return cobrapy.Reaction: the new model reaction
    """

    stoichiometry = modelseed_reaction.getStoichiometry()
    new_stoichiometry = {}

    for compound_id in stoichiometry:
        if compound_id in reaction_found_compounds.keys():
            model_compound = reaction_found_compounds[compound_id]
            new_stoichiometry[model_compound] = stoichiometry[compound_id]

    id = modelseed_reaction.getDbId() + "_" + compartment
    name = modelseed_reaction.getName()
    reversibility = modelseed_reaction.getReversibility()
    direction = modelseed_reaction.getDirection()
    if direction == "<":
        for compound in new_stoichiometry:
            new_stoichiometry[compound] = new_stoichiometry[compound] * -1

    if reversibility:
        new_model_reaction = Reaction(id,name,lower_bound=None)

    else:
        new_model_reaction = Reaction(id,name)

    aliases = reactionsIdConverter.get_all_aliases_by_modelSeedID(modelseed_reaction.getDbId())

    if aliases:
        new_model_reaction.annotation = AnnotationUtils.get_reaction_annotation_format_by_aliases(aliases)
    new_model_reaction.add_metabolites(new_stoichiometry)

    return new_model_reaction
Example #8
0
    def __swap_kegg_metabolite(self, model_metabolite,
                               new_compound_ontology_id):
        """
        Method to swap a metabolite in the KEGG database format.
        This method tries to find the KEGG format of the new compound. If it does not find, it will change into the
        Model SEED format.

        :param Model.Metabolite model_metabolite: metabolite to be replaced.
        :param int new_compound_ontology_id: ontology identifier of the metabolite that will replace the other
        :return:
        """

        old_inchikey = ""
        if "inchi_key" in model_metabolite.annotation.keys():
            old_inchikey = model_metabolite.annotation["inchi_key"]

        old_aliases = AnnotationUtils.get_annotation_from_cobra_annotation(
            model_metabolite)
        old_id = model_metabolite.id

        model_metabolite.annotation = {}
        compound_container = self.__compounds_ontology.get_node_by_ont_id(
            new_compound_ontology_id)
        model_seed_id = compound_container.model_seed_id
        new_aliases = self.__compoundsIdConverter.get_all_aliases_by_modelSeedID(
            model_seed_id)

        if model_seed_id in self.__compoundsIdConverter.get_modelSeedIdToDb(
        ).keys():

            if "KEGG" in self.__compoundsIdConverter.get_modelSeedIdToDb().get(
                    model_seed_id):

                kegg_id = self.__compoundsIdConverter.convert_modelSeedId_into_other_dbID(
                    model_seed_id, "KEGG")[0]
                kegg_metabolite = KeggCompound(kegg_id)

                annotation = AnnotationUtils.get_compound_annotation_format_by_aliases(
                    new_aliases)
                model_metabolite.annotation = annotation

                new_inchikey = ""
                if not compound_container.generic:
                    model_metabolite.annotation[
                        "inchi_key"] = compound_container.inchikey
                    new_inchikey = compound_container.inchikey

                model_metabolite.annotation[
                    "smiles"] = compound_container.smiles
                model_metabolite.formula = kegg_metabolite.get_formula()
                model_metabolite.name = kegg_metabolite.get_name()
                model_metabolite.charge = 0
                compartment = model_metabolite.compartment

                self.__check_if_id_is_used_in_model_and_delete_it(kegg_id +
                                                                  "_" +
                                                                  compartment)

                model_metabolite.id = kegg_id + "_" + compartment

                self.mapper.update_maps(old_inchikey, new_inchikey, old_id,
                                        model_metabolite.id,
                                        compound_container.id, old_aliases,
                                        new_aliases)

            else:
                self.__change_boimmg_format_metabolite(model_metabolite,
                                                       compound_container)

        else:
            self.__change_boimmg_format_metabolite(model_metabolite,
                                                   compound_container)
Example #9
0
    def __swap_bigg_metabolite(self, model_metabolite,
                               new_compound_ontology_id):
        """
        Method to swap a metabolite in the BiGG database format.
        This method tries to find the BiGG format of the new compound. If it does not find, it will change into the
        Model SEED format.

        :param Model.Metabolite model_metabolite: metabolite to be replaced.
        :param int new_compound_ontology_id: ontology identifier of the metabolite that will replace the other
        :return:
        """

        model_metabolite.annotation = {}
        compound_container = self.__compounds_ontology.get_node_by_ont_id(
            new_compound_ontology_id)
        model_seed_id = compound_container.model_seed_id

        if model_seed_id:
            bigg_ids = None
            aliases = self.__compoundsIdConverter.get_modelSeedIdToDb().get(
                model_seed_id)
            if "BiGG" in aliases:
                bigg_ids = self.__compoundsIdConverter.convert_modelSeedId_into_other_dbID(
                    model_seed_id, "BiGG")
            elif "BiGG1" in aliases:
                bigg_ids = self.__compoundsIdConverter.convert_modelSeedId_into_other_dbID(
                    model_seed_id, "BiGG1")

            if bigg_ids:

                bigg_metabolite = None
                bigg_id = None
                found = False
                i = 0
                while not found and i < len(bigg_ids):
                    try:
                        bigg_id = bigg_ids[i]
                        bigg_metabolite = bigg.get_bigg_metabolite(bigg_id)
                        found = True
                    except:
                        i += 1
                if bigg_metabolite:

                    old_inchikey = ""
                    if "inchi_key" in model_metabolite.annotation.keys():
                        old_inchikey = model_metabolite.annotation["inchi_key"]

                    old_aliases = AnnotationUtils.get_annotation_from_cobra_annotation(
                        model_metabolite)
                    old_id = model_metabolite.id

                    new_aliases = self.__compoundsIdConverter.get_all_aliases_by_modelSeedID(
                        compound_container.model_seed_id)
                    annotation = AnnotationUtils.get_compound_annotation_format_by_aliases(
                        new_aliases)
                    model_metabolite.annotation = annotation

                    new_inchikey = ""
                    if not compound_container.generic:
                        new_inchikey = compound_container.inchikey
                        model_metabolite.annotation[
                            "inchi_key"] = compound_container.inchikey

                    model_metabolite.annotation[
                        "smiles"] = compound_container.smiles
                    model_metabolite.formula = bigg_metabolite.get(
                        "formulae")[0]

                    charges = bigg_metabolite.get("charges")
                    if charges: model_metabolite.charge = charges[0]

                    compartment = model_metabolite.compartment

                    if compartment is None:
                        model_metabolite.compartment = "c"

                    model_metabolite.name = bigg_metabolite.get("name")

                    self.__check_if_id_is_used_in_model_and_delete_it(
                        bigg_id + "_" + model_metabolite.compartment)

                    model_metabolite.id = bigg_id + "_" + model_metabolite.compartment

                    self.mapper.update_maps(old_inchikey, new_inchikey, old_id,
                                            model_metabolite.id,
                                            new_compound_ontology_id,
                                            old_aliases, new_aliases)

                else:
                    self.__change_boimmg_format_metabolite(
                        model_metabolite, compound_container)

            else:
                self.__change_boimmg_format_metabolite(model_metabolite,
                                                       compound_container)

        else:
            self.__change_boimmg_format_metabolite(model_metabolite,
                                                   compound_container)
Example #10
0
    def check_compounds_representation_and_balance_reactions(self, reactions:list):

        res = []
        self.__get_hydrogen_from_model()

        compounds_ontology = CompoundsDBAccessor()

        to_remove = []
        for reaction in reactions:

            reaction_ontology_reactants = []
            reaction_ontology_products = []

            for compound in reaction.reactants:
                aliases = AnnotationUtils.get_annotation_from_cobra_annotation(compound)
                found = False
                for key in aliases:
                    for alias in aliases[key]:
                        new_key = self.compoundsAnnotationConfigs.get(key)

                        if key == "BOIMMG":
                            id = self.get_boimmg_id_from_annotation(aliases[key])
                            container = compounds_ontology.get_node_by_ont_id(id)

                            reaction_ontology_reactants.append(container)
                            found = True
                            break

                        elif new_key in self.__mapper.compounds_aliases_indexation.keys():
                            entities = self.__mapper.compounds_aliases_indexation.get(new_key).get(alias)
                            if entities:
                                for entity in entities:
                                    entity_id = entity.id

                                    if entity_id in self.__mapper.boimmg_db_model_map:

                                        ont_id = self.__mapper.boimmg_db_model_map.get(entity_id)
                                        entity_container = compounds_ontology.get_node_by_ont_id(ont_id)
                                        reaction_ontology_reactants.append(entity_container)
                                        found = True
                                        break
                    if found:
                        break


            for compound in reaction.products:

                found = False
                aliases = AnnotationUtils.get_annotation_from_cobra_annotation(compound)
                for key in aliases:
                    new_key = self.compoundsAnnotationConfigs.get(key)
                    for alias in aliases[key]:

                        if key == "BOIMMG":

                            id = self.get_boimmg_id_from_annotation(aliases[key])

                            container = compounds_ontology.get_node_by_ont_id(id)

                            reaction_ontology_products.append(container)
                            found = True
                            break


                        elif new_key in self.__mapper.compounds_aliases_indexation.keys():

                            entities = self.__mapper.compounds_aliases_indexation.get(new_key).get(alias)
                            if entities:
                                for entity in entities:
                                    entity_id = entity.id

                                    if entity_id in self.__mapper.boimmg_db_model_map:

                                        ont_id = self.__mapper.boimmg_db_model_map.get(entity_id)
                                        entity_container = compounds_ontology.get_node_by_ont_id(ont_id)
                                        reaction_ontology_products.append(entity_container)
                                        found = True
                                        break

                    if found:
                        break

            # self.__check_generic_complete_relationship(reaction_ontology_reactants,reaction_ontology_products,reaction)

            # if reaction_ontology_reactants and reaction_ontology_products:
            balanced = self.check_balanced_reaction(reaction)

            if not balanced:
                to_remove.append(reaction)

            else:
                res.append(reaction)

                # if generalization:
                #     self.__check_if_generic_complete_relationship()

        # print("reactions to remove: ")
        # print(to_remove)
        self.__model.remove_reactions(to_remove)
        return res
Example #11
0
def generate_kegg_reaction(modelseed_reaction,reaction_found_compounds,
                             compartment,reactionsIdConverter,compoundsIdConverter):
    """
    This function generates a new KEGG format reaction in a given compartment.

    :param ModelSeedReaction modelseed_reaction: a Model SEED reaction container
    :param dict reaction_found_compounds: dictionary with model seed compounds identifiers as keys and the
    respective Model.Metabolite as value.
    :param Model.Compartment compartment:
    :param ReactionsIDConverter reactionsIdConverter: a reaction identifier converter
    :param CompoundsIDConverter compoundsIdConverter: a compounds identifier converter
    :return cobrapy.Reaction: the new model reaction
    """

    kegg_reaction_id = reactionsIdConverter.convert_modelSeedId_into_other_dbID(modelseed_reaction.getDbId(),"KEGG")[0]
    try:
        kegg_reaction = KeggReaction(kegg_reaction_id)

        stoichiometry = kegg_reaction.get_stoichiometry()
        new_stoichiometry = {}

        for compound_id in stoichiometry:
            modelseedids = compoundsIdConverter.convert_dbID_into_modelSeedId("KEGG",compound_id)

            found = False
            i=0
            found_compound = None
            while not found and i<len(modelseedids):
                if modelseedids[i] in reaction_found_compounds.keys():
                    found = True
                    found_compound = modelseedids[i]

                i+=1

            if found:
                model_compound = reaction_found_compounds[found_compound]
                new_stoichiometry[model_compound] = stoichiometry[compound_id]

            else:
                return None

        id = kegg_reaction.get_id() + "_" + compartment

        name = kegg_reaction.get_name()
        if not name:
            name = modelseed_reaction.getName()

        reversibility = kegg_reaction.get_reversibility()
        direction = kegg_reaction.get_direction()
        if direction == "<=":
            for compound in new_stoichiometry:
                new_stoichiometry[compound] = new_stoichiometry[compound] * -1

        if reversibility:
            new_model_reaction = Reaction(id, name, lower_bound=None)

        else:
            new_model_reaction = Reaction(id, name)

        aliases = reactionsIdConverter.get_all_aliases_by_modelSeedID(modelseed_reaction.getDbId())
        new_model_reaction.annotation = AnnotationUtils.get_reaction_annotation_format_by_aliases(aliases)
        new_model_reaction.add_metabolites(new_stoichiometry)
        return new_model_reaction

    except:
        return None
Example #12
0
def generate_bigg_compound(model,model_seed_id,compoundsIdConverter,modelseedCompoundsDb,compartment=""):
    """
    Method to generate metabolites for each compartment of the model in the BiGG database format.
    This method tries to find the BiGG format of the new compounds. If it does not find, it will change it into the
    Model SEED format.

    :param cobrapy.Model model: cobrapy Model
    :param string model_seed_id: model seed identifier of the compound
    :param CompoundsIDConverter compoundsIdConverter: a converter
    :param ModelSeedCompoundsDB modelseedCompoundsDb: the compounds Model SEED database
    :param (Optional) string compartment: compartment to introduce the new compound
    :return list: new compounds list
    """

    if model_seed_id and "BiGG" in compoundsIdConverter.get_modelSeedIdToDb().get(model_seed_id)\
            or "BiGG1" in compoundsIdConverter.get_modelSeedIdToDb().get(model_seed_id):
        if "BiGG" in compoundsIdConverter.get_modelSeedIdToDb().get(model_seed_id):
            bigg_ids = compoundsIdConverter.convert_modelSeedId_into_other_dbID(model_seed_id, "BiGG")
        else:
            bigg_ids = compoundsIdConverter.convert_modelSeedId_into_other_dbID(model_seed_id, "BiGG1")

        bigg_metabolite  = None
        bigg_id = None
        found = False
        i = 0
        while not found and i < len(bigg_ids):
            try:
                bigg_id = bigg_ids[i]
                bigg_metabolite = bigg.get_bigg_metabolite(bigg_id)
                found = True
            except:
                i += 1

        if bigg_metabolite:
            compounds = []
            if not compartment:
                for compartment in model.compartments:
                    model_metabolite = Metabolite(bigg_id + "_" + compartment)
                    aliases = compoundsIdConverter.get_all_aliases_by_modelSeedID(model_seed_id)
                    annotation = AnnotationUtils.get_compound_annotation_format_by_aliases(aliases)
                    model_metabolite.annotation = annotation
                    if bigg_metabolite.get("inchikey"):
                        bigg_inchikey = bigg_metabolite.get("inchikey")
                        if bigg_inchikey:
                            model_metabolite.annotation["inchikey"] = bigg_inchikey

                    model_metabolite.formula = bigg_metabolite.get("formulae")[0]
                    model_metabolite.charge =  bigg_metabolite.get("charges")[0]
                    model_metabolite.name = bigg_metabolite.get("name")
                    model_metabolite.compartment = compartment
                    compounds.append(model_metabolite)

            else:
                model_metabolite = Metabolite(bigg_id + "_" + compartment)
                aliases = compoundsIdConverter.get_all_aliases_by_modelSeedID(model_seed_id)
                annotation = AnnotationUtils.get_compound_annotation_format_by_aliases(aliases)
                model_metabolite.annotation = annotation
                if bigg_metabolite.get("inchikey"):
                    model_metabolite.annotation["inchikey"] = bigg_metabolite.get("inchikey")

                model_metabolite.formula = bigg_metabolite.get("formulae")[0]
                model_metabolite.charge = bigg_metabolite.get("charges")[0]
                model_metabolite.name = bigg_metabolite.get("name")
                model_metabolite.compartment = compartment
                compounds.append(model_metabolite)


            return compounds

        else:
            return generate_modelseed_compound(model,model_seed_id,compoundsIdConverter,modelseedCompoundsDb)

    else:
        return generate_modelseed_compound(model,model_seed_id,compoundsIdConverter,modelseedCompoundsDb)