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 __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 #4
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 #5
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