def testDepictByBuildType(self): """Compare depictions constructed molecules with various builds from chemical defintions -""" try: ccIdList = self.__ccIdList ccMolD = self.__getChemCompDefs() # limitPerceptions = True molBuildTypeL = ["model-xyz", "ideal-xyz", "connection-table", "oe-iso-smiles"] # startTime = time.time() oefm = OeMoleculeFactory() for molBuildType in molBuildTypeL: for ccId in ccIdList: ccObj = ccMolD[ccId] # ---- tId = oefm.setChemCompDef(ccObj) self.assertEqual(tId, ccId) ok = oefm.build(molBuildType=molBuildType, limitPerceptions=limitPerceptions) if not ok: logger.info("Build using %r failed for %s", molBuildType, ccId) continue # oeMol = oefm.getGraphMol() pS = "-limited" if limitPerceptions else "" imagePath = os.path.join(self.__workPath, ccId + "-%s%s.svg" % (molBuildType, pS)) oed = OeDepict() title = "" oed.setMolTitleList([(ccId, oeMol, title)]) oed.setDisplayOptions(labelAtomName=False, labelAtomCIPStereo=True, labelAtomIndex=False, labelBondIndex=False, cellBorders=False, bondDisplayWidth=0.5) oed.setGridOptions(rows=1, cols=1) oed.prepare() oed.write(imagePath) logger.info("Completed depictions on %d molecules (%.4f seconds)", len(ccIdList) * len(molBuildTypeL), time.time() - startTime) # except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def testCompareByBuildType(self): """Compare depictions constructed molecules with various builds from chemical defintions - all build types 8769 (all) connect - smiles 6743 model vs iso smiles 5937 ideal va iso smiles 7047 """ doDepict = False ccResultD = {} genResultD = {} smilesByBuildTypeD = {} try: ccMolD, ccIdxD = self.__getChemCompDefs() # limitPerceptions = True # molBuildTypeL = ["model-xyz", "ideal-xyz", "connection-table", "oe-iso-smiles"] molBuildTypeL = ["ideal-xyz", "oe-iso-smiles"] # startTime = time.time() oefm = OeMoleculeFactory() oefm.setQuiet() for molBuildType in molBuildTypeL: for ccId, idxD in ccIdxD.items(): ccObj = ccMolD[ccId] # ---- ccIsoSmiles = idxD["oe-iso-smiles"] ccSmiles = idxD["oe-smiles"] # ---- tId = oefm.setChemCompDef(ccObj) if not tId: logger.info("Skipping bad component %r", ccId) continue self.assertEqual(tId, ccId) ok = oefm.build(molBuildType=molBuildType, limitPerceptions=limitPerceptions) if not ok: logger.info("Build using %r failed for %s", molBuildType, ccId) continue # ------ oeMol = oefm.getGraphMol() oeIsoSmiles = oefm.getIsoSMILES() oeSmiles = oefm.getCanSMILES() ccEq = oeIsoSmiles == ccIsoSmiles and oeSmiles == ccSmiles # oefmR = OeMoleculeFactory() oefmR.setQuiet() ccIdGen = ccId + "_gen" oefmR.setDescriptor(oeIsoSmiles, "oe-iso-smiles", ccIdGen) ok = oefmR.build(molBuildType="oe-iso-smiles", limitPerceptions=limitPerceptions) if not ok: logger.info("Build using %r failed for %s", molBuildType, ccIdGen) continue # ------ # # oeMolGen = oefmR.getGraphMol() oeIsoSmilesGen = oefmR.getIsoSMILES() oeSmilesGen = oefmR.getCanSMILES() genEq = oeIsoSmiles == oeIsoSmilesGen and oeSmiles == oeSmilesGen smilesByBuildTypeD.setdefault(ccId, {}).setdefault( molBuildType, []).append(oeIsoSmilesGen) # logger.debug("%s buildType %s ccEq %r genEq %r", ccId, molBuildType, ccEq, genEq) if not ccEq: ccResultD.setdefault(molBuildType, []).append(ccId) if not genEq: genResultD.setdefault(molBuildType, []).append(ccId) if doDepict: pS = "-limited" if limitPerceptions else "" imagePath = os.path.join( self.__workPath, ccId + "-%s%s.svg" % (molBuildType, pS)) oed = OeDepict() title = "" oed.setMolTitleList([(ccId, oeMol, title)]) oed.setDisplayOptions(labelAtomName=False, labelAtomCIPStereo=True, labelAtomIndex=False, labelBondIndex=False, cellBorders=False, bondDisplayWidth=0.5) oed.setGridOptions(rows=1, cols=1) oed.prepare() oed.write(imagePath) logger.info( "Completed comparing %d molecules in %d builds (%.4f seconds)", len(ccIdxD), len(molBuildTypeL), time.time() - startTime) # # for molBuildType in molBuildTypeL: if molBuildType in genResultD: logger.info("GEN %s (%d) %r", molBuildType, len(genResultD[molBuildType]), genResultD[molBuildType]) numDiff = 0 for ccId, btD in smilesByBuildTypeD.items(): tS = set() for molBuildType, sL in btD.items(): tS.add(sL[0]) if len(tS) > 1: numDiff += 1 logger.debug("%s diff smiles (%d) %r", ccId, len(tS), tS) logger.info("Components with inconsistent SMILES %d", numDiff) # except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()
def testRoundTrip(self): """Round trip smiles comparisons -""" try: ccMolD = self.__getChemCompDefs() # useCache = True # quietFlag = False # molBuildTypeL = ["model-xyz", "ideal-xyz", None] # molBuildTypeL = [None] buildTypeRef = "oe-iso-smiles" oemf1 = OeMoleculeFactory() oemf2 = OeMoleculeFactory() # for ccId, ccObj in ccMolD.items(): # ---- ccIt = iter(PdbxChemCompIt(ccObj)) cc = next(ccIt) formula = cc.getFormulaWithCharge() # ccId = cc.getId() ccName = cc.getName() ifCharge = cc.getFormalChargeAsInt() isAmbiguous = cc.getAmbiguousFlag() in ["Y", "y"] isCurrent = cc.getReleaseStatus() in ["REL"] logger.debug("%s name %r formula %r charge %d", ccId, ccName, formula, ifCharge) # ---- ccId = oemf1.setChemCompDef(ccObj) ok = oemf1.build(molBuildType=buildTypeRef, limitPerceptions=False) if not ok: logger.info( "Build using %r failed for %s (ambiguous flag %r current %r)", buildTypeRef, ccId, isAmbiguous, isCurrent) # isDiff = False # if isDiff: genIsoSmi = oemf1.getCanSMILES() oemf2 = OeMoleculeFactory() oemf2.setDescriptor(genIsoSmi, "oe-iso-smiles", ccId) oemf2.build(molBuildType="oe-iso-smiles", limitPerceptions=False) regenIsoSmi = oemf2.getIsoSMILES() if genIsoSmi != regenIsoSmi: logger.info( "%s regenerated ISOSMILES differ \n -- INP: %s\n -- OUT: %s", ccId, genIsoSmi, regenIsoSmi) oed = OeDepictMCSAlignPage() oed.setDisplayOptions( labelAtomName=True, labelAtomCIPStereo=True, labelAtomIndex=False, labelBondIndex=False, labelBondCIPStereo=True, highlightStyleFit="ballAndStickInverse", highLightNotMatchColorRef="pink", bondDisplayWidth=0.5, ) oed.setRefMol(oemf1.getGraphMol(), ccId) oed.setFitMol(oemf2.getGraphMol(), ccId) imgPath = os.path.join( self.__workPath, "compare-assigned-" + ccId + "-calc-" + ccId + ".svg") logger.info("Using image path %r", imgPath) aML = oed.alignPair(imagePath=imgPath) if aML: for (rCC, rAt, tCC, tAt) in aML: logger.info("%5s %-5s %5s %-5s", rCC, rAt, tCC, tAt) else: logger.debug("%s matched all cases", ccId) except Exception as e: logger.exception("Failing with %s", str(e)) self.fail()