def main(argv=[__name__]): itf = oechem.OEInterface() oechem.OEConfigure(itf, InterfaceData) oegraphsim.OEConfigureFingerPrint( itf, oegraphsim.OEGetFPType(oegraphsim.OEFPType_Tree)) if not oechem.OEParseCommandLine(itf, argv): return 1 ifname = itf.GetString("-in") ffname = itf.GetString("-fpdb") if oechem.OEGetFileExtension(ffname) != "fpbin": oechem.OEThrow.Fatal( "Fingerprint database file should have '.fpbin' file extension!") idxfname = oechem.OEGetMolDatabaseIdxFileName(ifname) if not os.path.exists(idxfname): if not oechem.OECreateMolDatabaseIdx(ifname): oechem.OEThrow.Warning("Unable to create %s molecule index file" % idxfname) oechem.OEThrow.Info("Using %s index molecule file" % idxfname) moldb = oechem.OEMolDatabase() if not moldb.Open(ifname): oechem.OEThrow.Fatal("Cannot open molecule database file!") nrmols = moldb.GetMaxMolIdx() fptype = oegraphsim.OESetupFingerPrint(itf) oechem.OEThrow.Info("Using fingerprint type %s" % fptype.GetFPTypeString()) opts = oegraphsim.OECreateFastFPDatabaseOptions(fptype) opts.SetTracer(oechem.OEDots(100000, 1000, "fingerprints")) oechem.OEThrow.Info("Generating fingerprints with %d threads" % opts.GetNumProcessors()) timer = oechem.OEWallTimer() if not oegraphsim.OECreateFastFPDatabaseFile(ffname, ifname, opts): oechem.OEThrow.Fatal("Cannot create fingerprint database file!") oechem.OEThrow.Info("%5.2f secs to generate %d fingerprints" % (timer.Elapsed(), nrmols)) return 0
def GetFragments(mol, minbonds, maxbonds): from openeye import oegraphsim frags = [] fptype = oegraphsim.OEGetFPType("Tree,ver=2.0.0,size=4096,bonds=%d-%d,atype=AtmNum,btype=Order" % (minbonds, maxbonds)) for abset in oegraphsim.OEGetFPCoverage(mol, fptype, True): fragatompred = oechem.OEIsAtomMember(abset.GetAtoms()) frag = oechem.OEGraphMol() adjustHCount = True oechem.OESubsetMol(frag, mol, fragatompred, adjustHCount) oechem.OEFindRingAtomsAndBonds(frag) frags.append(oechem.OEGraphMol(frag)) return frags
def __createOeFastFingerPrintDatabase(self, oeMolDbFilePath, oeFpDbFilePath, fpType="TREE"): """Create fast search fingerprint database from the input molecular database. Args: oeMolDbFilePath (str): path to the input molecular database oeFpDbFilePath (str): path to the output fingerprint database fpType (str): finger print type Returns: bool: True for success or False otherwise Supports: OEFPType_Circular OEFPType_Path OEFPType_Tree Not currently supported by OE fp search - OEFPType_MACCS166 OEFPType_Lingo """ startTime = time.time() ok = False try: _ = fpType fpD = { "TREE": oegraphsim.OEFPType_Tree, "CIRCULAR": oegraphsim.OEFPType_Circular, "PATH": oegraphsim.OEFPType_Path } myFpType = fpD[ fpType] if fpType in fpD else oegraphsim.OEFPType_Tree opts = oegraphsim.OECreateFastFPDatabaseOptions( oegraphsim.OEGetFPType(myFpType)) ok = oegraphsim.OECreateFastFPDatabaseFile(oeFpDbFilePath, oeMolDbFilePath, opts) except Exception as e: logger.exception("Failing with %s", str(e)) endTime = time.time() logger.info("Completed operation at %s (%.4f seconds)", time.strftime("%Y %m %d %H:%M:%S", time.localtime()), endTime - startTime) return ok
def visualize_mols(smiles, fname, rows, cols, bond_idx, wbos, colors, align_to=0): """ Visualize molecules with highlighted bond and labeled with WBO Parameters ---------- smiles : list of SMILES to visualize. bond atoms should have map indices fname : str filename rows : int cols : int bond_idx : tuple of atom maps of bond to highlight. wbos : list of floats colors : list of hex values for colors align_to: int, optional, default 0 index for which molecule to align to. If zero, will align to first molecules in SMILES list """ itf = oechem.OEInterface() ropts = oedepict.OEReportOptions(rows, cols) ropts.SetHeaderHeight(25) ropts.SetFooterHeight(25) ropts.SetCellGap(2) ropts.SetPageMargins(10) report = oedepict.OEReport(ropts) cellwidth, cellheight = report.GetCellWidth(), report.GetCellHeight() opts = oedepict.OE2DMolDisplayOptions(cellwidth, cellheight, oedepict.OEScale_AutoScale) oedepict.OESetup2DMolDisplayOptions(opts, itf) # align to chosen molecule ref_mol = oechem.OEGraphMol() oechem.OESmilesToMol(ref_mol, smiles[align_to]) oedepict.OEPrepareDepiction(ref_mol) mols = [] minscale = float("inf") for s in smiles: mol = oechem.OEMol() oechem.OESmilesToMol(mol, s) mols.append(mol) oedepict.OEPrepareDepiction(mol, False, True) minscale = min(minscale, oedepict.OEGetMoleculeScale(mol, opts)) print(minscale) print(minscale) opts.SetScale(minscale) for i, mol in enumerate(mols): cell = report.NewCell() oedepict.OEPrepareDepiction(mol, False, True) bond = get_bond(mol, bond_idx) atom_bond_set = oechem.OEAtomBondSet() atom_bond_set.AddAtoms([bond.GetBgn(), bond.GetEnd()]) atom_bond_set.AddBond(bond) hstyle = oedepict.OEHighlightStyle_BallAndStick hcolor = oechem.OEColor(*colors[i]) overlaps = oegraphsim.OEGetFPOverlap( ref_mol, mol, oegraphsim.OEGetFPType(oegraphsim.OEFPType_Tree)) oedepict.OEPrepareMultiAlignedDepiction(mol, ref_mol, overlaps) #opts.SetBondPropLabelFontScale(4.0) disp = oedepict.OE2DMolDisplay(mol, opts) oedepict.OEAddHighlighting(disp, hcolor, hstyle, atom_bond_set) #font = oedepict.OEFont(oedepict.OEFontFamily_Default, oedepict.OEFontStyle_Bold, 12, # oedepict.OEAlignment_Default, oechem.OEBlack) bond_label = oedepict.OEHighlightLabel("{:.2f}".format((wbos[i])), hcolor) bond_label.SetFontScale(1.4) #bond_label.SetFont(font) oedepict.OEAddLabel(disp, bond_label, atom_bond_set) oedepict.OERenderMolecule(cell, disp) # oedepict.OEDrawCurvedBorder(cell, oedepict.OELightGreyPen, 10.0) return (oedepict.OEWriteReport(fname, report))
#!/usr/bin/env python # (C) 2017 OpenEye Scientific Software Inc. All rights reserved. # # TERMS FOR USE OF SAMPLE CODE The software below ("Sample Code") is # provided to current licensees or subscribers of OpenEye products or # SaaS offerings (each a "Customer"). # Customer is hereby permitted to use, copy, and modify the Sample Code, # subject to these terms. OpenEye claims no rights to Customer's # modifications. Modification of Sample Code is at Customer's sole and # exclusive risk. Sample Code may require Customer to have a then # current license or subscription to the applicable OpenEye offering. # THE SAMPLE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED. OPENEYE DISCLAIMS ALL WARRANTIES, INCLUDING, BUT # NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A # PARTICULAR PURPOSE AND NONINFRINGEMENT. In no event shall OpenEye be # liable for any damages or liability in connection with the Sample Code # or its use. from openeye import oegraphsim # @ <SNIPPET-PATH-DEFAULT-PARAMS> fptype = oegraphsim.OEGetFPType(oegraphsim.OEFPType_Path) print(fptype.GetFPTypeString()) # @ </SNIPPET-PATH-DEFAULT-PARAMS>
# THE SAMPLE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED. OPENEYE DISCLAIMS ALL WARRANTIES, INCLUDING, BUT # NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A # PARTICULAR PURPOSE AND NONINFRINGEMENT. In no event shall OpenEye be # liable for any damages or liability in connection with the Sample Code # or its use. from openeye import oechem from openeye import oegraphsim # @ <SNIPPET-FPOVERLAP> pmol = oechem.OEGraphMol() oechem.OESmilesToMol(pmol, "c1cnc2c(c1)CC(CC2O)CF") tmol = oechem.OEGraphMol() oechem.OESmilesToMol(tmol, "c1cc2c(cc1)CC(CCl)CC2N") fptype = oegraphsim.OEGetFPType("Tree,ver=2.0.0,size=4096,bonds=5-5," "atype=AtmNum|HvyDeg|EqHalo,btype=Order") for idx, match in enumerate(oegraphsim.OEGetFPOverlap(pmol, tmol, fptype)): ostring = "match %2d: " % (idx + 1) for mpair in match.GetAtoms(): p = mpair.pattern t = mpair.target ostring += "%d%s-%d%s " % ( p.GetIdx(), oechem.OEGetAtomicSymbol(p.GetAtomicNum()), t.GetIdx(), oechem.OEGetAtomicSymbol(t.GetAtomicNum())) print(ostring) # @ </SNIPPET-FPOVERLAP>
def to_pdf(molecules, bond_map_idx, fname, rows=3, cols=2, align=None): """ Generate PDF of list of oemols or SMILES Parameters ---------- molecules : list of OEMols These mols need to have map indices on bond of interest and WBO attached to that bond's data fname : str Name of PDF rows : int How many rows of molecules per page cols : int How many columns of molecule per page bond_map_idx : tuple of bond to highlight align: oemol molecule to align all other molecules in the list """ itf = oechem.OEInterface() ropts = oedepict.OEReportOptions(rows, cols) ropts.SetHeaderHeight(25) ropts.SetFooterHeight(25) ropts.SetCellGap(2) ropts.SetPageMargins(10) report = oedepict.OEReport(ropts) cellwidth, cellheight = report.GetCellWidth(), report.GetCellHeight() opts = oedepict.OE2DMolDisplayOptions(cellwidth, cellheight, oedepict.OEScale_AutoScale) oedepict.OESetup2DMolDisplayOptions(opts, itf) if align: if isinstance(align, str): ref_mol = oechem.OEGraphMol() oechem.OESmilesToMol(ref_mol, align) elif isinstance(align, (oechem.OEMol, oechem.OEMolBase, oechem.OEGraphMol)): ref_mol = align oedepict.OEPrepareDepiction(ref_mol) for i, mol in enumerate(molecules): cell = report.NewCell() mol_copy = oechem.OEMol(mol) oedepict.OEPrepareDepiction(mol_copy, False, True) atom_bond_set = oechem.OEAtomBondSet() a1 = mol_copy.GetAtom(oechem.OEHasMapIdx(bond_map_idx[0])) a2 = mol_copy.GetAtom(oechem.OEHasMapIdx(bond_map_idx[1])) b = mol_copy.GetBond(a1, a2) opts.SetBondPropertyFunctor(fragmenter.chemi.LabelWibergBondOrder()) atom_bond_set.AddAtom(a1) atom_bond_set.AddAtom(a2) atom_bond_set.AddBond(b) hstyle = oedepict.OEHighlightStyle_BallAndStick hcolor = oechem.OEColor(oechem.OELightBlue) overlaps = oegraphsim.OEGetFPOverlap( ref_mol, mol_copy, oegraphsim.OEGetFPType(oegraphsim.OEFPType_Tree)) oedepict.OEPrepareMultiAlignedDepiction(mol_copy, ref_mol, overlaps) disp = oedepict.OE2DMolDisplay(mol_copy, opts) oedepict.OEAddHighlighting(disp, hcolor, hstyle, atom_bond_set) oedepict.OERenderMolecule(cell, disp) oedepict.OEDrawCurvedBorder(cell, oedepict.OELightGreyPen, 10.0) oedepict.OEWriteReport(fname, report)
# @ <SNIPPET-FINGERPRINT-ALIGN> refmol = oechem.OEGraphMol() oechem.OESmilesToMol(refmol, "C[C@H](C(=O)N1CCC[C@H]1C(=O)O)OC(=O)[C@@H](Cc2ccccc2)S") oedepict.OEPrepareDepiction(refmol) fitmol = oechem.OEGraphMol() oechem.OESmilesToMol(fitmol, "C[C@H](C(=O)N1CCC[C@H]1C(=O)O)NC(=O)[C@@H](Cc2ccccn2)S") oedepict.OEPrepareDepiction(fitmol) image = oedepict.OEImage(500, 300) rows, cols = 1, 2 grid = oedepict.OEImageGrid(image, rows, cols) overlaps = oegraphsim.OEGetFPOverlap(refmol, fitmol, oegraphsim.OEGetFPType(oegraphsim.OEFPType_Tree)) oedepict.OEPrepareMultiAlignedDepiction(fitmol, refmol, overlaps) opts = oedepict.OE2DMolDisplayOptions(grid.GetCellWidth(), grid.GetCellHeight(), oedepict.OEScale_AutoScale) opts.SetTitleLocation(oedepict.OETitleLocation_Hidden) refscale = oedepict.OEGetMoleculeScale(refmol, opts) fitscale = oedepict.OEGetMoleculeScale(fitmol, opts) opts.SetScale(min(refscale, fitscale)) refdisp = oedepict.OE2DMolDisplay(refmol, opts) fitdisp = oedepict.OE2DMolDisplay(fitmol, opts) refcell = grid.GetCell(1, 1) oedepict.OERenderMolecule(refcell, refdisp)
# TERMS FOR USE OF SAMPLE CODE The software below ("Sample Code") is # provided to current licensees or subscribers of OpenEye products or # SaaS offerings (each a "Customer"). # Customer is hereby permitted to use, copy, and modify the Sample Code, # subject to these terms. OpenEye claims no rights to Customer's # modifications. Modification of Sample Code is at Customer's sole and # exclusive risk. Sample Code may require Customer to have a then # current license or subscription to the applicable OpenEye offering. # THE SAMPLE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED. OPENEYE DISCLAIMS ALL WARRANTIES, INCLUDING, BUT # NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A # PARTICULAR PURPOSE AND NONINFRINGEMENT. In no event shall OpenEye be # liable for any damages or liability in connection with the Sample Code # or its use. from openeye import oechem from openeye import oegraphsim # @ <SNIPPET-FPPATTERN> mol = oechem.OEGraphMol() oechem.OESmilesToMol(mol, "NCC(=O)[O-]") fptype = oegraphsim.OEGetFPType("Tree,ver=2.0.0,size=4096,bonds=0-4," "atype=AtmNum|Arom|FCharge|HvyDeg,btype=Order") for idx, pattern in enumerate(oegraphsim.OEGetFPPatterns(mol, fptype)): atomstr = " ".join([str(a) for a in pattern.GetAtoms()]) print("%2d %5d %50s %s" % ((idx + 1), pattern.GetBit(), pattern.GetSmarts(), atomstr)) # @ </SNIPPET-FPPATTERN>
#!/usr/bin/env python # (C) 2017 OpenEye Scientific Software Inc. All rights reserved. # # TERMS FOR USE OF SAMPLE CODE The software below ("Sample Code") is # provided to current licensees or subscribers of OpenEye products or # SaaS offerings (each a "Customer"). # Customer is hereby permitted to use, copy, and modify the Sample Code, # subject to these terms. OpenEye claims no rights to Customer's # modifications. Modification of Sample Code is at Customer's sole and # exclusive risk. Sample Code may require Customer to have a then # current license or subscription to the applicable OpenEye offering. # THE SAMPLE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED. OPENEYE DISCLAIMS ALL WARRANTIES, INCLUDING, BUT # NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A # PARTICULAR PURPOSE AND NONINFRINGEMENT. In no event shall OpenEye be # liable for any damages or liability in connection with the Sample Code # or its use. from openeye import oegraphsim # @ <SNIPPET-CIRCULAR-DEFAULT-PARAMS> fptype = oegraphsim.OEGetFPType(oegraphsim.OEFPType_Circular) print(fptype.GetFPTypeString()) # @ </SNIPPET-CIRCULAR-DEFAULT-PARAMS>
def main(argv=[__name__]): itf = oechem.OEInterface() oechem.OEConfigure(itf, InterfaceData) defopts = oegraphsim.OEFPDatabaseOptions(10, oegraphsim.OESimMeasure_Tanimoto) oegraphsim.OEConfigureFPDatabaseOptions(itf, defopts) oegraphsim.OEConfigureFingerPrint( itf, oegraphsim.OEGetFPType(oegraphsim.OEFPType_Tree)) if not oechem.OEParseCommandLine(itf, argv): return 0 qfname = itf.GetString("-query") mfname = itf.GetString("-molfname") ofname = itf.GetString("-out") # initialize databases timer = oechem.OEWallTimer() timer.Start() ifs = oechem.oemolistream() if not ifs.open(qfname): oechem.OEThrow.Fatal("Cannot open input file!") query = oechem.OEGraphMol() if not oechem.OEReadMolecule(ifs, query): oechem.OEThrow.Fatal("Cannot read query molecule!") moldb = oechem.OEMolDatabase() if not moldb.Open(mfname): oechem.OEThrow.Fatal("Cannot open molecule database!") ofs = oechem.oemolostream() if not ofs.open(ofname): oechem.OEThrow.Fatal("Cannot open output file!") fptype = oegraphsim.OESetupFingerPrint(itf) oechem.OEThrow.Info("Using fingerprint type %s" % fptype.GetFPTypeString()) fpdb = oegraphsim.OEFPDatabase(fptype) emptyfp = oegraphsim.OEFingerPrint() emptyfp.SetFPTypeBase(fptype) nrmols = moldb.GetMaxMolIdx() mol = oechem.OEGraphMol() for idx in range(0, nrmols): if moldb.GetMolecule(mol, idx): fpdb.AddFP(mol) else: fpdb.AddFP(emptyfp) nrfps = fpdb.NumFingerPrints() oechem.OEThrow.Info("%5.2f sec to initialize databases" % timer.Elapsed()) opts = oegraphsim.OEFPDatabaseOptions() oegraphsim.OESetupFPDatabaseOptions(opts, itf) # search fingerprint database timer.Start() scores = fpdb.GetSortedScores(query, opts) oechem.OEThrow.Info("%5.2f sec to search %d fingerprints" % (timer.Elapsed(), nrfps)) timer.Start() hit = oechem.OEGraphMol() for si in scores: if moldb.GetMolecule(hit, si.GetIdx()): oechem.OEWriteMolecule(ofs, hit) oechem.OEThrow.Info("%5.2f sec to write %d hits" % (timer.Elapsed(), opts.GetLimit())) return 0
from openeye import oegraphsim # @ <SNIPPET-SDF2FP> if len(sys.argv) != 2: oechem.OEThrow.Usage("%s <infile>" % sys.argv[0]) ifs = oechem.oemolistream() if not ifs.open(sys.argv[1]): oechem.OEThrow.Fatal("Unable to open %s for reading" % sys.argv[1]) if ifs.GetFormat() != oechem.OEFormat_SDF: oechem.OEThrow.Fatal("%s input file has to be an SDF file" % sys.argv[1]) molcounter = 0 fpcounter = 0 for mol in ifs.GetOEGraphMols(): molcounter += 1 for dp in oechem.OEGetSDDataPairs(mol): if oegraphsim.OEIsValidFPTypeString(dp.GetTag()): fpcounter += 1 fptypestr = dp.GetTag() fphexdata = dp.GetValue() fp = oegraphsim.OEFingerPrint() fptype = oegraphsim.OEGetFPType(fptypestr) fp.SetFPTypeBase(fptype) fp.FromHexString(fphexdata) print("Number of molecules = %d" % molcounter) print("Number of fingerprints = %d" % fpcounter) # @ </SNIPPET-SDF2FP>