Ejemplo n.º 1
0
 def __buildChemCompSearchIndex(self, procName, ccIdList, descrD, limitPerceptions=False, quietFlag=False):
     """Internal method return a dictionary of extracted chemical component descriptors and formula."""
     rL = []
     fL = []
     try:
         for ccId in ccIdList:
             if ccId not in self.__ccObjD:
                 logger.error("%s missing chemical definition for %s", procName, ccId)
                 fL.append(ccId)
                 continue
             dataContainer = self.__ccObjD[ccId]
             # ----
             oemf = OeMoleculeFactory()
             if quietFlag:
                 oemf.setQuiet()
             tId = oemf.setChemCompDef(dataContainer)
             if tId != ccId:
                 logger.error("%s %s chemical component definition import error", procName, ccId)
                 fL.append(ccId)
                 continue
             # ----
             oemf.clearExternalDescriptors()
             for smi in descrD[ccId] if ccId in descrD else []:
                 oemf.addExternalDescriptor("smiles", smi, "chemaxon-smiles")
             # ----
             relD = oemf.buildRelated(limitPerceptions=limitPerceptions)
             logger.debug("%s %s related molecular forms %d", procName, ccId, len(relD))
             if relD:
                 rL.extend([relD[v] for v in relD])
             else:
                 fL.append(ccId)
     except Exception as e:
         logger.exception("%s failing with %s", procName, str(e))
     return rL, fL
Ejemplo n.º 2
0
    def __buildChemCompSearchIndex(self, ccObjD, descrD, limitPerceptions=False, molLimit=None):
        """Internal method return a dictionary of extracted chemical component descriptors and formula."""
        rD = {}
        try:
            for ii, ccId in enumerate(ccObjD, 1):
                if molLimit and ii > molLimit:
                    break
                # ----
                oemf = OeMoleculeFactory()
                oemf.setQuiet()
                tId = oemf.setChemCompDef(ccObjD[ccId])
                if tId != ccId:
                    logger.error("%s chemical component definition import error", ccId)
                # ----
                oemf.clearExternalDescriptors()
                for smi in descrD[ccId] if ccId in descrD else []:
                    oemf.addExternalDescriptor("smiles", smi, "chemaxon-smiles")
                # ----
                smiD = oemf.buildRelated(limitPerceptions=limitPerceptions)
                logger.debug("%s related molecular forms %d", ccId, len(smiD))
                rD.update(smiD)
        except Exception as e:
            logger.exception("Failing with %s", str(e))

        return rD
Ejemplo n.º 3
0
    def testRoundTripOps(self):
        """Test IO operation on generated related molecules"""
        try:
            oeIoU = OeIoUtils()
            mU = MarshalUtil()
            mU.mkdir(self.__molfileDirPath)
            ccMolD = self.__getChemCompDefs()
            oemf = OeMoleculeFactory()
            for ccId, ccObj in list(ccMolD.items())[:10]:
                # ----
                tId = oemf.setChemCompDef(ccObj)
                self.assertEqual(tId, ccId)
                relatedIdxD = oemf.buildRelated(limitPerceptions=False)
                logger.info("%s generated %d molecular forms", ccId,
                            len(relatedIdxD))
                for sId, idxD in relatedIdxD.items():
                    logger.info("sId %r smiles %r", sId, idxD["smiles"])
                    mol2Path = os.path.join(self.__molfileDirPath,
                                            sId + ".mol2")
                    oeMol = oeIoU.descriptorToMol(idxD["smiles"],
                                                  "oe-iso-smiles",
                                                  limitPerceptions=False,
                                                  messageTag=None)
                    oeIoU.write(mol2Path,
                                oeMol,
                                constantMol=True,
                                addSdTags=True)
                    tMolL = oeIoU.fileToMols(mol2Path)
                    #
                    nextMol2Path = os.path.join(self.__molfileDirPath,
                                                sId + "-next.mol2")
                    oeIoU.write(nextMol2Path,
                                tMolL[0],
                                constantMol=True,
                                addSdTags=True)

                    sdfPath = os.path.join(self.__molfileDirPath, sId + ".mol")
                    oeMol = oeIoU.descriptorToMol(idxD["smiles"],
                                                  "oe-iso-smiles",
                                                  limitPerceptions=False,
                                                  messageTag=None)
                    oeIoU.write(sdfPath,
                                oeMol,
                                constantMol=True,
                                addSdTags=True)
                    #
                    tMolL = oeIoU.fileToMols(sdfPath)
                    nextSdfPath = os.path.join(self.__molfileDirPath,
                                               sId + "-next.sdf")
                    oeIoU.write(nextSdfPath,
                                tMolL[0],
                                constantMol=True,
                                addSdTags=True)
                # ----
        except Exception as e:
            logger.exception("Failing with %s", str(e))
            self.fail()
 def testBuildRelated(self):
     """Test build molecules chemical definitions -"""
     try:
         logger.info("Starting")
         ccMolD = self.__getChemCompDefs()
         oemf = OeMoleculeFactory(quietMode=True)
         relD = {}
         for ccId, ccObj in list(ccMolD.items())[:100]:
             # ----
             tId = oemf.setChemCompDef(ccObj)
             self.assertEqual(tId, ccId)
             tD = oemf.buildRelated(limitPerceptions=False)
             logger.info("%s related molecular forms %d", ccId, len(tD))
             relD.update(tD)
         logger.info("Total molecular forms (%d)", len(relD))
         # ----
     except Exception as e:
         logger.exception("Failing with %s", str(e))
         self.fail()
    def testBuildRelatedExtra(self):
        """Test build molecules chemical definitions including extra chemaxon descriptors"""

        try:
            caxP = ChemAxonDescriptorProvider(
                ccUrlTarget=self.__ccUrlTarget,
                birdUrlTarget=self.__birdUrlTarget,
                cachePath=self.__cachePath,
                useCache=True,
                ccFileNamePrefix="cc-abbrev")
            ok = caxP.testCache()
            self.assertTrue(ok)
            descrD = caxP.getDescriptorIndex()
            #
            ccMolD = self.__getChemCompDefs()
            oemf = OeMoleculeFactory(quietMode=True)
            relD = {}
            for ccId, ccObj in list(ccMolD.items())[:100]:
                # ----
                tId = oemf.setChemCompDef(ccObj)
                self.assertEqual(tId, ccId)
                #
                oemf.clearExternalDescriptors()
                for smi in descrD[ccId] if ccId in descrD else []:
                    oemf.addExternalDescriptor("smiles", smi,
                                               "chemaxon-smiles")
                #
                tD = oemf.buildRelated(limitPerceptions=False)
                logger.info("%s related molecular forms %d", ccId, len(tD))
                relD.update(tD)
            logger.info("Total molecular forms (%d)", len(relD))

            # ----
        except Exception as e:
            logger.exception("Failing with %s", str(e))
            self.fail()