コード例 #1
0
    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()
コード例 #2
0
 def testDepictCCIdList(self):
     """Test case -  single OE molecule depiction.
     labelAtomName=True, labelAtomCIPStereo=True, labelAtomIndex=False, labelBondIndex=False, abelBondCIPStereo=True, cellBorders=False, bondDisplayWidth=3.0
     """
     try:
         oeMolTitleList = self.__getMolDepictList(self.__ccIdList)
         for ccId, mol, title in oeMolTitleList:
             imagePath = os.path.join(self.__workPath, ccId + ".svg")
             oed = OeDepict()
             title = ""
             oed.setMolTitleList([(ccId, mol, title)])
             oed.setDisplayOptions(labelAtomName=False,
                                   labelAtomCIPStereo=True,
                                   labelAtomIndex=False,
                                   labelBondIndex=False,
                                   labelBondCIPStereo=True,
                                   cellBorders=False,
                                   bondDisplayWidth=8.0)
             oed.setGridOptions(rows=1, cols=1, cellBorders=False)
             oed.prepare()
             oed.write(imagePath)
     except Exception as e:
         logger.exception("Failing with %s", str(e))
         self.fail()
コード例 #3
0
 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()
コード例 #4
0
 def testDepictOneSDF(self):
     """Test case -  get, read, build OE molecule from SDF file, and depict the molecule."""
     try:
         imagePath = os.path.join(self.__workPath, "benzene-from-smi.svg")
         sdfPath = os.path.join(self.__dataPath, "ATP.sdf")
         oeio = OeIoUtils()
         oeMolL = oeio.fileToMols(sdfPath)
         #
         oed = OeDepict()
         oed.setMolTitleList([("ATP", oeMolL[0], "Title for ATP")])
         oed.setDisplayOptions(labelAtomName=True,
                               labelAtomCIPStereo=True,
                               labelBondCIPStereo=True,
                               labelAtomIndex=False,
                               labelBondIndex=False,
                               bondDisplayWidth=0.5)
         oed.setGridOptions(rows=1, cols=1)
         oed.prepare()
         oed.write(imagePath)
     except Exception as e:
         logger.exception("Failing with %s", str(e))
         self.fail()
コード例 #5
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()
コード例 #6
0
 def testDepictGrid(self):
     """Test case - depict molecule list  grid."""
     try:
         oeMolTitleList = self.__getMolDepictList(self.__ccIdList)
         oed = OeDepict()
         oed.setMolTitleList(oeMolTitleList)
         oed.setDisplayOptions(imageX=1000,
                               imageY=1000,
                               labelAtomName=True,
                               labelAtomCIPStereo=True,
                               labelBondCIPStereo=True,
                               labelAtomIndex=False,
                               labelBondIndex=False,
                               bondDisplayWidth=0.5)
         oed.setGridOptions(rows=2, cols=2)
         oed.prepare()
         oed.write(os.path.join(self.__workPath, "myIdListtest.png"))
     except Exception as e:
         logger.exception("Failing with %s", str(e))
         self.fail()
コード例 #7
0
    def testDepictCCIdListLabeled(self):
        """Test case -  single OE molecule depiction."""
        try:
            oeMolTitleList = self.__getMolDepictList(self.__ccIdList)
            for ccId, mol, title in oeMolTitleList:
                imagePath = os.path.join(self.__workPath,
                                         ccId + "-labeled.svg")
                oed = OeDepict()
                title = ""
                oed.setMolTitleList([(ccId, mol, title)])
                bondDisplayWidth = 10.0
                numAtoms = mol.NumAtoms()
                if numAtoms > 100 and numAtoms <= 200:
                    bondDisplayWidth = 6.0
                elif mol.NumAtoms() > 200:
                    bondDisplayWidth = 4.0

                oed.setDisplayOptions(
                    labelAtomName=True,
                    labelAtomCIPStereo=True,
                    labelAtomIndex=False,
                    labelBondIndex=False,
                    labelBondCIPStereo=True,
                    cellBorders=False,
                    bondDisplayWidth=bondDisplayWidth,
                )
                oed.setGridOptions(rows=1, cols=1, cellBorders=False)
                oed.prepare()
                oed.write(imagePath)
        except Exception as e:
            logger.exception("Failing with %s", str(e))
            self.fail()
コード例 #8
0
    def __depictOne(self, oeMol, imagePath, **kwargs):
        """Single

        Args:
            oeMol (object): instance of an OE graph molecule
            imagePath (string): file path for image

        Returns:
            bool: True for success or False otherwise
        """
        try:
            title = kwargs.get("title", None)
            oed = OeDepict()
            oed.setMolTitleList([("Target", oeMol, title)])

            # ---
            bondDisplayWidth = 10.0
            numAtoms = oeMol.NumAtoms()
            if numAtoms > 100 and numAtoms <= 200:
                bondDisplayWidth = 6.0
            elif numAtoms > 200:
                bondDisplayWidth = 4.0
            # ---
            oed.setDisplayOptions(
                imageSizeX=kwargs.get("imageSizeX", 2500),
                imageSizeY=kwargs.get("imageSizeX", 2500),
                labelAtomName=kwargs.get("labelAtomName", False),
                labelAtomCIPStereo=kwargs.get("labelAtomCIPStereo", True),
                labelAtomIndex=kwargs.get("labelAtomIndex", False),
                labelBondIndex=kwargs.get("labelBondIndex", False),
                labelBondCIPStereo=kwargs.get("labelBondCIPStereo", True),
                cellBorders=kwargs.get("cellBorders", True),
                bondDisplayWidth=bondDisplayWidth,
            )
            oed.setGridOptions(rows=1, cols=1, cellBorders=False)
            oed.prepare()
            oed.write(imagePath)
            self.__imageCount += 1
            return True
        except Exception as e:
            logger.exception("Failing with %s", str(e))
        return False
コード例 #9
0
 def makeImages(self):
     """ Create images for all public chemical components with and without atom labels.
     """
     try:
         if not self.__setLicense(self.__licensePath):
             logger.error("Invalid license details - exiting")
             return False
         for ccId, oeMol in self.__oeMolD.items():
             imagePath = os.path.join(self.__imagePath, "image", ccId[0],
                                      ccId + ".svg")
             oed = OeDepict()
             title = ""
             oed.setMolTitleList([(ccId, oeMol, title)])
             # ---
             bondDisplayWidth = 10.0
             numAtoms = oeMol.NumAtoms()
             if numAtoms > 100 and numAtoms <= 200:
                 bondDisplayWidth = 6.0
             elif numAtoms > 200:
                 bondDisplayWidth = 4.0
             # ---
             oed.setDisplayOptions(
                 labelAtomName=False,
                 labelAtomCIPStereo=True,
                 labelAtomIndex=False,
                 labelBondIndex=False,
                 labelBondCIPStereo=True,
                 cellBorders=False,
                 bondDisplayWidth=bondDisplayWidth,
             )
             oed.setGridOptions(rows=1, cols=1, cellBorders=False)
             oed.prepare()
             oed.write(imagePath)
         for ccId, oeMol in self.__oeMolD.items():
             imagePath = os.path.join(self.__imagePath, "image_labeled",
                                      ccId[0], ccId + ".svg")
             oed = OeDepict()
             title = ""
             oed.setMolTitleList([(ccId, oeMol, title)])
             # ---
             bondDisplayWidth = 10.0
             numAtoms = oeMol.NumAtoms()
             if numAtoms > 100 and numAtoms <= 200:
                 bondDisplayWidth = 6.0
             elif numAtoms > 200:
                 bondDisplayWidth = 4.0
             # ---
             oed.setDisplayOptions(
                 labelAtomName=True,
                 labelAtomCIPStereo=True,
                 labelAtomIndex=False,
                 labelBondIndex=False,
                 labelBondCIPStereo=True,
                 cellBorders=False,
                 bondDisplayWidth=bondDisplayWidth,
             )
             oed.setGridOptions(rows=1, cols=1, cellBorders=False)
             oed.prepare()
             oed.write(imagePath)
         return True
     except Exception as e:
         logger.exception("Failing with %s", str(e))
     return False
コード例 #10
0
 def testDepictAll(self):
     """Test case -  single OE molecule depiction."""
     try:
         for ccId, oeMol in self.__oeMolD.items():
             imagePath = os.path.join(self.__workPath, "image", ccId[0],
                                      ccId + ".svg")
             oed = OeDepict()
             title = ""
             oed.setMolTitleList([(ccId, oeMol, title)])
             oed.setDisplayOptions(labelAtomName=False,
                                   labelAtomCIPStereo=True,
                                   labelAtomIndex=False,
                                   labelBondIndex=False,
                                   labelBondCIPStereo=True,
                                   cellBorders=False,
                                   bondDisplayWidth=0.5)
             oed.setGridOptions(rows=1, cols=1)
             oed.prepare()
             oed.write(imagePath)
         for ccId, oeMol in self.__oeMolD.items():
             imagePath = os.path.join(self.__workPath, "image_labeled",
                                      ccId[0], ccId + ".svg")
             oed = OeDepict()
             title = ""
             oed.setMolTitleList([(ccId, oeMol, title)])
             oed.setDisplayOptions(labelAtomName=True,
                                   labelAtomCIPStereo=True,
                                   labelAtomIndex=False,
                                   labelBondIndex=False,
                                   abelBondCIPStereo=True,
                                   cellBorders=False,
                                   bondDisplayWidth=0.5)
             oed.setGridOptions(rows=1, cols=1)
             oed.prepare()
             oed.write(imagePath)
     except Exception as e:
         logger.exception("Failing with %s", str(e))
         self.fail()