def stringToMols(self, txt, sType="mol2", use3D=False): """Parse the input string as input format type (sType) returning a list of molecule objects (OeGraphMol) Args: txt (str): string text of molecule data sType (str, optional): string data format (mol2, sdf, smiles) . Defaults to "mol2". Returns: list: list of OeGraphMol() objects """ # mL = [] oemf = OeMoleculeFactory() try: if sType not in ["mol2", "sdf", "smiles"]: logger.error("Unsupported string data format") return None fD = { "mol2": oechem.OEFormat_MOL2, "sdf": oechem.OEFormat_SDF, "smiles": oechem.OEFormat_SMI } ifs = oechem.oemolistream() ifs.SetFormat(fD["sType"]) if not ifs.openstring(txt): logger.error("Unable open string data for molecule reader") return None for tMol in ifs.GetOEGraphMols(): oeMol = oechem.OEGraphMol(tMol) if use3D: mL.append( oemf.updateOePerceptions3D( oeMol, aromaticModel=oechem.OEAroModelOpenEye)) else: mL.append( oemf.updateOePerceptions2D( oeMol, aromaticModel=oechem.OEAroModelOpenEye)) except Exception as e: logger.exception("Failing with %s", str(e)) return mL
def fileToMols(self, filePath, use3D=False, largestPart=False): """Parse the input path returning a list of molecule objects (OeGraphMol). Args: filePath (str): file path must have strandard recognized extension ('mol', 'sdf', 'smi', 'oeb'). Returns: list : list of OeGraphMol() objects """ mL = [] oemf = OeMoleculeFactory() try: ifs = oechem.oemolistream() if ifs.open(filePath): for tMol in ifs.GetOEGraphMols(): oeMol = oechem.OEGraphMol(tMol) # if oechem.OEReadMolecule(ifs, oeMol): if largestPart: molL = oemf.getParts(oeMol) if len(molL) > 0: oeMol = molL[0] logger.info( "Using largest bonded molecule part (%d/%d)", len(molL), oeMol.NumAtoms()) if use3D: mL.append( oemf.updateOePerceptions3D( oeMol, aromaticModel=oechem.OEAroModelOpenEye)) else: mL.append( oemf.updateOePerceptions2D( oeMol, aromaticModel=oechem.OEAroModelOpenEye)) except Exception as e: logger.exception("Failing with %s", str(e)) return mL