Porthmeus / MeMoMe

Metabolic Model Merging - a semiautomated way to merge genome scale metabolic models
Apache License 2.0
0 stars 0 forks source link

Error during matching: KeyError: 'neutralized_charge_inchi' #157

Open Unaimend opened 3 months ago

Unaimend commented 3 months ago

The below code in MeMoModel crashes when sum(mask) is <= 0. If that happens neutralized_charge_inchi does not get set as a key and subsequently the code in the loop that tries to access this key crashes the program

MRE

from src.MeMoModel import MeMoModel
from src.MeMoMetabolite  import MeMoMetabolite
from pathlib import Path
#model = MeMoModel.fromPath(Path("/home/td/Projects/MeMoMe/tests/manually_merged_models/gapseq_recon3D/M2_bacterial_model.xml"))
#model

b = MeMoModel()

metabolite: MeMoMetabolite = MeMoMetabolite()
metabolite.set_id("A")
metabolite.set_inchi_string("")
metabolite.set_names(["Peter"])

metaboliteB: MeMoMetabolite = MeMoMetabolite()
metaboliteB.set_id("B")
metaboliteB.set_inchi_string("")
metaboliteB.set_names(["Peter"])

model1 = MeMoModel([metabolite])
model2 = MeMoModel([metaboliteB])

model1.annotate()
model2.annotate()

model1.match(model2)
        mask1 = ~pd.isna(mod1_inchis.Mol)
        print("MASK", mask1)
        if sum(mask1) > 0:
            mod1_inchis.loc[mask1,'charge'] = mod1_inchis.loc[mask1,'Mol'].apply(Chem.GetFormalCharge)
            mod1_inchis.loc[mask1,'neutralized_charge_inchi'] = mod1_inchis.loc[mask1,'Mol'].apply(NeutraliseCharges2Inchi)
        else:
            mod1_inchis["neutralized_charge_inchi"] = None
            mod1_inchis["charge"] = None

        mask2 = ~pd.isna(mod2_inchis.Mol)
        if sum(mask2) > 0:
            mod2_inchis.loc[mask2,'charge'] = mod2_inchis.loc[mask2,'Mol'].apply(Chem.GetFormalCharge)
            mod2_inchis.loc[mask2,'neutralized_charge_inchi'] = mod2_inchis.loc[mask2,'Mol'].apply(NeutraliseCharges2Inchi)
        else:
            mod2_inchis["neutralized_charge_mol"] = None
            mod2_inchis["charge"] = None

        # go through the inchis of the second model and find the corresponding inchi in self
        matches = {"met_id1" : [],
                "met_id2" : [],
                "inchi_score":[],
                "inchi_string":[],
                "charge_diff" : []}

        # loop over the first models metabolites
        for i in range(len(mod1_inchis)):
            # get the first inchi
            inchi1 = mod1_inchis.loc[i, "inchis"]
            # check if there is acutally an inchi, or whether the metabolite has none
            if inchi1 != None:
                # assign the precalculated values for the inchi
                mol1   = mod1_inchis.loc[i, "Mol"]
                nminchi1 = mod1_inchis.loc[i, "normalized_inchi"]
                ntchrinchi1 = mod1_inchis.loc[i, "neutralized_charge_inchi"]
                charge1 = mod1_inchis.loc[i,"charge"]
                id1 = mod1_inchis.loc[i,"met_id"]
Porthmeus commented 2 months ago

@Unaimend we did fix that in some branch, was that merged already?