Пример #1
0
 def toMolFile(self,
               identifier,
               identifierType,
               molfilePath=None,
               fmt="mol",
               **kwargs):
     """Create molfile (fmt) from InChI, SMILES descriptors or PDB identifier."""
     try:
         molfilePath = molfilePath if molfilePath else self.__makeMolfilePath(
             fmt=fmt)
         oeio = OeIoUtils()
         if identifierType.lower() in ["smiles"]:
             oeMol = oeio.smilesToMol(identifier)
             oeMol.SetTitle("From SMILES")
         elif identifierType.lower() in ["inchi"]:
             oeMol = oeio.inchiToMol(identifier)
             oeMol.SetTitle("From InChI")
         elif identifierType.lower() in ["identifierpdb"]:
             ccsw = ChemCompSearchWrapper()
             oesmP = ccsw.getSearchMoleculeProvider()
             oeMol = oesmP.getMol(identifier)
         #
         ok = self.__toMolFile(oeMol, molfilePath, **kwargs)
         return molfilePath if ok else None
     except Exception as e:
         logger.exception("Failing with %s", str(e))
     return None
Пример #2
0
    def alignMoleculePair(self,
                          refIdentifier,
                          refIdentifierType,
                          fitIdentifier,
                          fitIdentifierType,
                          imagePath=None,
                          **kwargs):
        """Create aligned depiction for a target molecule InChI, SMILES descriptors or PDB identifier."""
        try:
            imagePath = imagePath if imagePath else self.__makeImagePath()
            oeio = OeIoUtils()
            ccsw = ChemCompSearchWrapper()
            oesmP = ccsw.getSearchMoleculeProvider()
            # ---
            if refIdentifierType.lower() in ["smiles"]:
                oeMolRef = oeio.smilesToMol(refIdentifier)
            elif refIdentifierType.lower() in ["inchi"]:
                oeMolRef = oeio.inchiToMol(refIdentifier)
            elif refIdentifierType.lower() in ["identifierpdb"]:
                oeMolRef = oesmP.getMol(refIdentifier)
            #
            if fitIdentifierType.lower() in ["smiles"]:
                oeMolFit = oeio.smilesToMol(fitIdentifier)
            elif fitIdentifierType.lower() in ["inchi"]:
                oeMolFit = oeio.inchiToMol(fitIdentifier)
            elif fitIdentifierType.lower() in ["identifierpdb"]:
                oeMolFit = oesmP.getMol(fitIdentifier)
            # ---
            logger.info("oeMolRef atoms %r", oeMolRef.NumAtoms())
            logger.info("oeMolFit atoms %r", oeMolFit.NumAtoms())

            displayIdRef = "Ref"
            displayIdFit = "Fit"
            ok = self.__depictAlignedPair(oeMolRef, displayIdRef, oeMolFit,
                                          displayIdFit, imagePath, **kwargs)
            return imagePath if ok else None
        except Exception as e:
            logger.exception("Failing with %s", str(e))
        return None
Пример #3
0
    def testDepictSMILES(self):
        """Test case -  create depiction from SMILES descriptor."""
        try:
            imagePath = os.path.join(self.__workPath, "benzene-from-smi.svg")
            oeio = OeIoUtils()
            oeMol = oeio.smilesToMol("c1ccccc1")

            oed = OeDepict()
            oed.setMolTitleList([("benzene", oeMol, "Title for benzene")])
            oed.setDisplayOptions(labelAtomName=False,
                                  labelAtomCIPStereo=True,
                                  labelBondCIPStereo=True,
                                  labelAtomIndex=False,
                                  labelBondIndex=False,
                                  bondDisplayWidth=1.0)
            oed.setGridOptions(rows=1, cols=1)
            oed.prepare()
            oed.write(imagePath)
        except Exception as e:
            logger.exception("Failing with %s", str(e))
            self.fail()
Пример #4
0
 def depictMolecule(self,
                    identifier,
                    identifierType,
                    imagePath=None,
                    **kwargs):
     """Create depiction from InChI, SMILES descriptors or PDB identifier."""
     try:
         imagePath = imagePath if imagePath else self.__makeImagePath()
         oeio = OeIoUtils()
         if identifierType.lower() in ["smiles"]:
             oeMol = oeio.smilesToMol(identifier)
         elif identifierType.lower() in ["inchi"]:
             oeMol = oeio.inchiToMol(identifier)
         elif identifierType.lower() in ["identifierpdb"]:
             ccsw = ChemCompSearchWrapper()
             oesmP = ccsw.getSearchMoleculeProvider()
             oeMol = oesmP.getMol(identifier)
         #
         ok = self.__depictOne(oeMol, imagePath, **kwargs)
         return imagePath if ok else None
     except Exception as e:
         logger.exception("Failing with %s", str(e))
     return None
Пример #5
0
    def testSssWithFingerPrintFromDescriptor(self):
        oemp = OeMoleculeProvider(**self.__myKwargs)
        ok = oemp.testCache()
        ccmP = ChemCompIndexProvider(**self.__myKwargs)
        ccIdxD = ccmP.getIndex()
        ok = ccmP.testCache(minCount=self.__minCount)
        self.assertTrue(ok)
        limitPerceptions = False
        # minFpScore = 0.5
        maxFpResults = 50
        matchOpts = "graph-relaxed"
        numMols = 20
        oeioU = OeIoUtils()
        oesU = OeSearchUtils(oemp, fpTypeList=self.__fpTypeList)
        missTupL = []
        missedD = {}
        missedFpD = {}
        # ----
        startTime = time.time()
        for ccId, ccD in list(ccIdxD.items())[:numMols]:
            for buildType in [
                    "oe-iso-smiles", "oe-smiles", "acdlabs-smiles",
                    "cactvs-iso-smiles", "cactvs-smiles", "inchi"
            ]:
                if buildType in ccD:
                    logger.debug("Search %s %r", ccId, ccD[buildType])
                    if buildType in ["inchi"]:
                        oemf = OeMoleculeFactory()
                        oemf.setDescriptor(ccD["inchi"], "inchi", ccId)
                        ok = oemf.build(molBuildType="inchi",
                                        limitPerceptions=limitPerceptions)
                        if not ok:
                            logger.info("%s build failed with InChI %r", ccId,
                                        ccD["inchi"])
                        else:
                            oeMol = oemf.getMol()
                            if oemf.getInChI() != ccD["inchi"]:
                                logger.info(
                                    "%s regenerated InChI differs\n%r\n%s",
                                    ccId, ccD["inchi"], oemf.getInChI())
                    else:
                        oeMol = oeioU.smilesToMol(
                            ccD[buildType], limitPerceptions=limitPerceptions)
                    if not oeMol:
                        continue
                    maxHits = 0
                    minHits = maxFpResults
                    selfHit = False
                    for fpType, minFpScore in self.__fpTypeCuttoffList:
                        retStatus, mL = oesU.searchSubStructureWithFingerPrint(
                            oeMol,
                            fpType,
                            minFpScore,
                            maxFpResults,
                            matchOpts=matchOpts)
                        self.assertTrue(retStatus)
                        logger.debug("%s fpType %r hits %d", ccId, fpType,
                                     len(mL))
                        maxHits = max(maxHits, len(mL))
                        minHits = min(minHits, len(mL))
                        matchedSelf = self.__resultContains(ccId, mL)
                        selfHit = selfHit or matchedSelf
                        if not matchedSelf:
                            missedFpD.setdefault(ccId, []).append(
                                (buildType, fpType, len(mL)))
                    if not selfHit:
                        missedD.setdefault(ccId, []).append(buildType)

                    logger.info("%s (%r) buildType %r min hits %d max hits %d",
                                ccId, selfHit, buildType, minHits, maxHits)
                else:
                    logger.info("%s missing descriptor %r", ccId, buildType)
        #
        for ccId, missL in missedD.items():
            logger.info("%s missed list %r", ccId, missL)
            if ccId in missedFpD:
                logger.info("%s unmatched for fpTypes %r", ccId,
                            missedFpD[ccId])
        # ----
        doDepict = False
        if doDepict:
            mD = {}
            for missTup in missTupL:
                mD.setdefault(missTup[0], []).append(missTup[1])

            for ccId, buildTypeL in mD.items():
                idxD = ccIdxD[ccId]
                if "oe-iso-smiles" in idxD:
                    for buildType in buildTypeL:
                        self.__displayAlignedDescriptorPair(
                            ccId,
                            idxD["oe-iso-smiles"],
                            "oe-iso-smiles",
                            idxD[buildType],
                            buildType,
                            title=None,
                            limitPerceptions=True)

        logger.info("%s fingerprints search on %d in (%.4f seconds)",
                    len(self.__fpTypeList), numMols,
                    time.time() - startTime)