def draw_frag_pdf(frag_dict, scaffold=None, pdf_filename='fragments.pdf'): from openeye import oechem, oedepict import re itf = oechem.OEInterface() PageByPage = True suppress_h = True rows = 7 cols = 5 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_Default * 0.5) opts.SetAromaticStyle(oedepict.OEAromaticStyle_Circle) pen = oedepict.OEPen(oechem.OEBlack, oechem.OEBlack, oedepict.OEFill_On, 1.0) opts.SetDefaultBondPen(pen) oedepict.OESetup2DMolDisplayOptions(opts, itf) if scaffold: oemol = oechem.OEGraphMol() scaffold_smi = re.sub(r"\(\[R([1-9])+]\)", r"([*])", scaffold.smiles) oechem.OESmilesToMol(oemol, scaffold_smi) cell = report.NewCell() mol = oechem.OEMol(oemol) mol.SetTitle(f'{scaffold.smiles}') oedepict.OEPrepareDepiction(mol, False, suppress_h) disp = oedepict.OE2DMolDisplay(mol, opts) oedepict.OERenderMolecule(cell, disp) headerpen = oedepict.OEPen(oechem.OEWhite, oechem.OELightGrey, oedepict.OEFill_Off, 1.0) oedepict.OEDrawBorder(cell, headerpen) for rx, smis in frag_dict.items(): for idx, smi in enumerate(smis): if smi != None: # Create oemol oemol = oechem.OEGraphMol() oechem.OESmilesToMol(oemol, smi) # Render molecule cell = report.NewCell() mol = oechem.OEMol(oemol) mol.SetTitle(f'R{rx} #{idx+1}') oedepict.OEPrepareDepiction(mol, False, suppress_h) disp = oedepict.OE2DMolDisplay(mol, opts) oedepict.OERenderMolecule(cell, disp) oedepict.OEWriteReport(pdf_filename, report)
def png_atoms_labeled(smiles, fname): """Write out png file of molecule with atoms labeled with their index. Parameters ---------- smiles: str SMILES fname: str absolute path and filename for png """ mol = oechem.OEGraphMol() oechem.OESmilesToMol(mol, smiles) oedepict.OEPrepareDepiction(mol) width, height = 300, 200 opts = oedepict.OE2DMolDisplayOptions(width, height, oedepict.OEScale_AutoScale) opts.SetAtomPropertyFunctor(oedepict.OEDisplayAtomIdx()) opts.SetAtomPropLabelFont(oedepict.OEFont(oechem.OEDarkGreen)) disp = oedepict.OE2DMolDisplay(mol, opts) return oedepict.OERenderMolecule(fname, disp)
def png_wiberg_labels(mol, fname, width=600, height=400): """ Generate png figure of molecule. Bonds are labeled with Wiberg bond order Parameters ---------- mol: OpenEye OEMol fname: str filename for png width: int height: int Returns ------- bool: """ oedepict.OEPrepareDepiction(mol) opts = oedepict.OE2DMolDisplayOptions(width, height, oedepict.OEScale_AutoScale) # opts.SetAtomPropertyFunctor(oedepict.OEDisplayAtomIdx()) # opts.SetAtomPropLabelFont(oedepict.OEFont(oechem.OEDarkGreen)) bondlabel = LabelBondOrder() opts.SetBondPropertyFunctor(bondlabel) disp = oedepict.OE2DMolDisplay(mol, opts) return oedepict.OERenderMolecule(fname, disp)
def ToPdf(mol, oname, frags):#, fragcombs): """ Parameters ---------- mol: charged OEMolGraph oname: str Output file name Returns ------- """ itf = oechem.OEInterface() oedepict.OEPrepareDepiction(mol) ropts = oedepict.OEReportOptions() oedepict.OESetupReportOptions(ropts, itf) ropts.SetFooterHeight(25.0) ropts.SetHeaderHeight(ropts.GetPageHeight() / 4.0) report = oedepict.OEReport(ropts) # setup decpiction options opts = oedepict.OE2DMolDisplayOptions() oedepict.OESetup2DMolDisplayOptions(opts, itf) cellwidth, cellheight = report.GetCellWidth(), report.GetCellHeight() opts.SetDimensions(cellwidth, cellheight, oedepict.OEScale_AutoScale) opts.SetTitleLocation(oedepict.OETitleLocation_Hidden) opts.SetAtomColorStyle(oedepict.OEAtomColorStyle_WhiteMonochrome) opts.SetAtomLabelFontScale(1.2) DepictMoleculeWithFragmentCombinations(report, mol, frags, opts) return oedepict.OEWriteReport(oname, report)
def main(argv=[__name__]): itf = oechem.OEInterface(InterfaceData) oedepict.OEConfigureReportOptions(itf) oedepict.OEConfigurePrepareDepictionOptions(itf) oedepict.OEConfigure2DMolDisplayOptions(itf) if not oechem.OEParseCommandLine(itf, argv): return 1 iname = itf.GetString("-in") ifs = oechem.oemolistream() ifs.SetConfTest(oechem.OEAbsoluteConfTest()) # VTL if not ifs.open(iname): oechem.OEThrow.Fatal("Cannot open input file!") oname = itf.GetString("-out") ext = oechem.OEGetFileExtension(oname) if ext != "pdf": oechem.OEThrow.Fatal("Output must be PDF format.") ofs = oechem.oeofstream() if not ofs.open(oname): oechem.OEThrow.Fatal("Cannot open output file!") if itf.HasString("-ringdict"): rdfname = itf.GetString("-ringdict") if not oechem.OEInit2DRingDictionary(rdfname): oechem.OEThrow.Warning("Cannot use user-defined ring dictionary!") ropts = oedepict.OEReportOptions() oedepict.OESetupReportOptions(ropts, itf) ropts.SetFooterHeight(25.0) report = oedepict.OEReport(ropts) popts = oedepict.OEPrepareDepictionOptions() oedepict.OESetupPrepareDepictionOptions(popts, itf) dopts = oedepict.OE2DMolDisplayOptions() oedepict.OESetup2DMolDisplayOptions(dopts, itf) dopts.SetDimensions(report.GetCellWidth(), report.GetCellHeight(), oedepict.OEScale_AutoScale) for mol in ifs.GetOEMols(): # VTL ignore confs; dont use GetOEGraphMols print(mol.GetTitle()) # VTL cell = report.NewCell() oedepict.OEPrepareDepiction(mol, popts) disp = oedepict.OE2DMolDisplay(mol, dopts) oedepict.OERenderMolecule(cell, disp) font = oedepict.OEFont(oedepict.OEFontFamily_Default, oedepict.OEFontStyle_Bold, 12, oedepict.OEAlignment_Center, oechem.OEBlack) for pagenum, footer in enumerate(report.GetFooters()): text = "Page %d of %d" % (pagenum + 1, report.NumPages()) oedepict.OEDrawTextToCenter(footer, text, font) oedepict.OEWriteReport(ofs, ext, report) return 0
def main(argv=[__name__]): # import configuration file itf = oechem.OEInterface() oechem.OEConfigure(itf, InterfaceData) # add configuration for image size and display options oedepict.OEConfigureImageOptions(itf) oedepict.OEConfigure2DMolDisplayOptions(itf) if not oechem.OEParseCommandLine(itf, argv): oechem.OEThrow.Fatal("Unable to interpret command line!") ifname = itf.GetString("-in") ofname = itf.GetString("-out") ifs = oechem.oemolistream(ifname) mol = oechem.OEGraphMol() oechem.OEReadMolecule(ifs, mol) oedepict.OEPrepareDepiction(mol) width, height = oedepict.OEGetImageWidth(itf), oedepict.OEGetImageHeight(itf) opts = oedepict.OE2DMolDisplayOptions(width, height, oedepict.OEScale_AutoScale) # set up display options from command line parameters oedepict.OESetup2DMolDisplayOptions(opts, itf) disp = oedepict.OE2DMolDisplay(mol, opts) oedepict.OERenderMolecule(ofname, disp)
def Draw2DSurfacePartialCharge(image, mol): oedepict.OEPrepareDepiction(mol) oechem.OEMMFFAtomTypes(mol) oechem.OEMMFF94PartialCharges(mol) opts = oedepict.OE2DMolDisplayOptions(image.GetWidth(), image.GetHeight(), oedepict.OEScale_AutoScale) opts.SetTitleLocation(oedepict.OETitleLocation_Hidden) opts.SetScale(oegrapheme.OEGetMoleculeSurfaceScale(mol, opts)) coloranion = oechem.OEColorStop(-1.0, oechem.OEColor(oechem.OEDarkRed)) colorcation = oechem.OEColorStop(+1.0, oechem.OEColor(oechem.OEDarkBlue)) colorg = oechem.OELinearColorGradient(coloranion, colorcation) colorg.AddStop(oechem.OEColorStop(0.0, oechem.OEColor(oechem.OEWhite))) arcfxn = AtomPartialChargeArcFxn(colorg) for atom in mol.GetAtoms(): oegrapheme.OESetSurfaceArcFxn(mol, atom, arcfxn) disp = oedepict.OE2DMolDisplay(mol, opts) oegrapheme.OEDraw2DSurface(disp) oedepict.OERenderMolecule(image, disp)
def prepare(self): """[summary] # OESuppressHydrogens(self.__oeMol, retainPolar=False,retainStereo=True,retainIsotope=True) oechem.OESuppressHydrogens(self.__oeMol) """ self.__setupImage() for idx, cell in enumerate(self.__grid.GetCells()): ccId, oeMol, title = self._molTitleList[idx] logger.debug("Preparing %s %r", ccId, title) # if self._params["suppressHydrogens"]: # mol = oeMol.getGraphMolSuppressH() # OESuppressHydrogens(self.__oeMol, retainPolar=False,retainStereo=True,retainIsotope=True) mol = oechem.OESuppressHydrogens(oechem.OEGraphMol(oeMol)) else: mol = oeMol # if self.__useTitle and title: mol.SetTitle(title) self._opts.SetTitleHeight(5.0) else: mol.SetTitle("") # # oedepict.OEPrepareDepiction(mol) self._opts.SetDimensions(cell.GetWidth(), cell.GetHeight(), oedepict.OEScale_AutoScale) self._assignDisplayOptions() disp = oedepict.OE2DMolDisplay(mol, self._opts) oedepict.OERenderMolecule(cell, disp) if self._params["cellBorders"]: oedepict.OEDrawBorder(cell, oedepict.OEPen(oedepict.OEBlackPen))
def smiles_to_png(smiles, file_path, image_size=200): """Creates a png image of the 2D representation of a given smiles pattern. Parameters ---------- smiles: str The smiles pattern to generate the png of. file_path: str The path of the output png file. image_size: int The size in pixels of the square image. """ from openeye import oedepict from openforcefield.topology import Molecule if os.path.isfile(file_path): return off_molecule = Molecule.from_smiles(smiles) oe_molecule = off_molecule.to_openeye() # oe_molecule.SetTitle(off_molecule.to_smiles()) oedepict.OEPrepareDepiction(oe_molecule) options = oedepict.OE2DMolDisplayOptions(image_size, image_size, oedepict.OEScale_AutoScale) display = oedepict.OE2DMolDisplay(oe_molecule, options) oedepict.OERenderMolecule(file_path, display)
def main(argv=[__name__]): itf = oechem.OEInterface() oechem.OEConfigure(itf, InterfaceData) oedepict.OEConfigureImageWidth(itf, 400.0) oedepict.OEConfigureImageHeight(itf, 400.0) oedepict.OEConfigureImageGridParams(itf) oedepict.OEConfigurePrepareDepictionOptions(itf) oedepict.OEConfigure2DMolDisplayOptions(itf) if not oechem.OEParseCommandLine(itf, argv): oechem.OEThrow.Fatal("Unable to interpret command line!") oname = itf.GetString("-out") ext = oechem.OEGetFileExtension(oname) if not oedepict.OEIsRegisteredImageFile(ext): oechem.OEThrow.Fatal("Unknown image type!") ofs = oechem.oeofstream() if not ofs.open(oname): oechem.OEThrow.Fatal("Cannot open output file!") width, height = oedepict.OEGetImageWidth(itf), oedepict.OEGetImageHeight( itf) image = oedepict.OEImage(width, height) rows = oedepict.OEGetImageGridNumRows(itf) cols = oedepict.OEGetImageGridNumColumns(itf) grid = oedepict.OEImageGrid(image, rows, cols) popts = oedepict.OEPrepareDepictionOptions() oedepict.OESetupPrepareDepictionOptions(popts, itf) dopts = oedepict.OE2DMolDisplayOptions() oedepict.OESetup2DMolDisplayOptions(dopts, itf) dopts.SetDimensions(grid.GetCellWidth(), grid.GetCellHeight(), oedepict.OEScale_AutoScale) celliter = grid.GetCells() for iname in itf.GetStringList("-in"): ifs = oechem.oemolistream() if not ifs.open(iname): oechem.OEThrow.Warning("Cannot open %s input file!" % iname) continue for mol in ifs.GetOEGraphMols(): if not celliter.IsValid(): break oedepict.OEPrepareDepiction(mol, popts) disp = oedepict.OE2DMolDisplay(mol, dopts) oedepict.OERenderMolecule(celliter.Target(), disp) celliter.Next() oedepict.OEWriteImage(ofs, ext, image) return 0
def smiles_to_image_grid_mod( smiles: set, output_path: str, cols: int = 8, cell_width: int = 200, cell_height: int = 200, ): from openeye import oechem, oedepict itf = oechem.OEInterface() PageByPage = True suppress_h = True rows = math.ceil(len(smiles) / cols) image = oedepict.OEImage(cell_width * cols, cell_height * rows) grid = oedepict.OEImageGrid(image, rows, cols) opts = oedepict.OE2DMolDisplayOptions( grid.GetCellWidth(), grid.GetCellHeight(), oedepict.OEScale_AutoScale ) opts.SetAromaticStyle(oedepict.OEAromaticStyle_Circle) opts.SetTitleLocation(oedepict.OETitleLocation_Bottom) for i, (smi, cell) in enumerate(zip(smiles, grid.GetCells())): mol = oechem.OEGraphMol() oechem.OESmilesToMol(mol, smi) center_bond = [] for atom in mol.GetAtoms(): if atom.GetMapIdx() >0: center_bond.append(atom.GetIdx()) # print(smi, center_bond) assert len(center_bond) == 2 oedepict.OEPrepareDepiction(mol, False, suppress_h) disp = oedepict.OE2DMolDisplay(mol, opts) # Highlight element of interest class NoAtom(oechem.OEUnaryAtomPred): def __call__(self, atom): return False class NoBond(oechem.OEUnaryBondPred): def __call__(self, bond): return False class CentralBondInTorsion(oechem.OEUnaryBondPred): def __call__(self, bond): return (bond.GetBgn().GetIdx() in center_bond) and (bond.GetEnd().GetIdx() in center_bond) atoms = mol.GetAtoms(NoAtom()) bonds = mol.GetBonds(CentralBondInTorsion()) abset = oechem.OEAtomBondSet(atoms, bonds) oedepict.OEAddHighlighting(disp, oechem.OEColor(oechem.OEMandarin), oedepict.OEHighlightStyle_BallAndStick, abset) oedepict.OERenderMolecule(cell, disp) oedepict.OEWriteImage(output_path, image)
def generate_ligands_figure(molecules, figsize=None, filename='ligands.png'): """ Plot an image with all of the ligands passed in Parameters ---------- molecules : list list of openeye.oemol objects figsize : list or tuple list or tuple of len() == 2 of the horizontal and vertical lengths of image filename : string name of file to save the image Returns ------- """ from openeye import oechem, oedepict to_draw = [] for lig in molecules: oedepict.OEPrepareDepiction(lig) to_draw.append(oechem.OEGraphMol(lig)) dim = int(np.ceil(len(to_draw)**0.5)) if figsize is None: x_len = 1000 * dim y_len = 500 * dim image = oedepict.OEImage(x_len, y_len) else: assert (len(figsize) == 2 ), "figsize arguement should be a tuple or list of length 2" image = oedepict.OEImage(figsize[0], figsize[1]) rows, cols = dim, dim grid = oedepict.OEImageGrid(image, rows, cols) opts = oedepict.OE2DMolDisplayOptions(grid.GetCellWidth(), grid.GetCellHeight(), oedepict.OEScale_AutoScale) minscale = float("inf") for mol in to_draw: minscale = min(minscale, oedepict.OEGetMoleculeScale(mol, opts)) # print(mol.GetTitle()) opts.SetScale(minscale) for idx, cell in enumerate(grid.GetCells()): mol = to_draw[idx] disp = oedepict.OE2DMolDisplay(mol, opts) oedepict.OERenderMolecule(cell, disp) oedepict.OEWriteImage(filename, image) return
def show_oemol_struc(oemol, torsions=False, atom_indices=[], width=500, height=300): from IPython.display import Image from openeye import oechem, oedepict # Highlight element of interest class NoAtom(oechem.OEUnaryAtomPred): def __call__(self, atom): return False class AtomInTorsion(oechem.OEUnaryAtomPred): def __call__(self, atom): return atom.GetIdx() in atom_indices class NoBond(oechem.OEUnaryBondPred): def __call__(self, bond): return False class BondInTorsion(oechem.OEUnaryBondPred): def __call__(self, bond): return (bond.GetBgn().GetIdx() in atom_indices) and (bond.GetEnd().GetIdx() in atom_indices) class CentralBondInTorsion(oechem.OEUnaryBondPred): def __call__(self, bond): return (bond.GetBgn().GetIdx() in atom_indices[1:3]) and (bond.GetEnd().GetIdx() in atom_indices[1:3]) opts = oedepict.OE2DMolDisplayOptions(width, height, oedepict.OEScale_AutoScale) opts.SetAtomPropertyFunctor(oedepict.OEDisplayAtomIdx()) oedepict.OEPrepareDepiction(oemol) img = oedepict.OEImage(width, height) display = oedepict.OE2DMolDisplay(oemol, opts) if torsions: atoms = oemol.GetAtoms(AtomInTorsion()) bonds = oemol.GetBonds(NoBond()) abset = oechem.OEAtomBondSet(atoms, bonds) oedepict.OEAddHighlighting( display, oechem.OEColor(oechem.OEYellow), oedepict.OEHighlightStyle_BallAndStick, abset, ) oedepict.OERenderMolecule(img, display) png = oedepict.OEWriteImageToString("png", img) return Image(png)
def render_molecule( smiles: str, path: str, width: int = 320, height: int = 240, file_format: str = "svg", clearbackground: bool = False, force_regenerate: bool = False, ) -> None: """ Render the molecule (from SMILES) to an image Parameters ---------- smiles : str The SMILES string filename : str Output filename, with image format (pdf, png, jpg) detected from filename width : int, optional, default=320 Default image width height : int, optional, default=240 Default image height clearbackground : bool, optional, default=False For PNG, whether background should be clear """ # Import the openeye toolkit from openeye import oechem, oedepict output_name = get_image_filename(smiles) output_path = os.path.join(path, os.extsep.join([output_name, file_format])) if not force_regenerate and os.path.exists(output_path): logging.info("Skipping already-rendered molecule: %s", smiles) return # Generate OpenEye OEMol object from SMILES # see https://docs.eyesopen.com/toolkits/python/oechemtk/molctordtor.html?highlight=smiles#construction-from-smiles mol = oechem.OEGraphMol() if not oechem.OESmilesToMol(mol, smiles): raise ValueError( f"Failed to convert SMILES string to molecule: {smiles}") # Configure options (lots more are available) # see https://docs.eyesopen.com/toolkits/python/depicttk/OEDepictClasses/OE2DMolDisplayOptions.html opts = oedepict.OE2DMolDisplayOptions() opts.SetWidth(width) opts.SetHeight(height) # Render image oedepict.OEPrepareDepiction(mol) disp = oedepict.OE2DMolDisplay(mol, opts) oedepict.OERenderMolecule(output_path, disp, clearbackground)
def mol2svg(self, mol, svg_file): oechem.OEAssignImplicitHydrogens(mol) oechem.OEAddExplicitHydrogens(mol) oedepict.OEPrepareDepiction(mol) width = 1000 height = 1000 opts = oedepict.OE2DMolDisplayOptions(width, height, oedepict.OEScale_AutoScale) pens = opts.GetDefaultBondPen() pens.SetLineWidth(10) disp = oedepict.OE2DMolDisplay(mol, opts) oedepict.OERenderMolecule(svg_file, disp)
def depict(mol, width=500, height=200): from IPython.display import Image dopt = oedepict.OEPrepareDepictionOptions() dopt.SetDepictOrientation(oedepict.OEDepictOrientation_Horizontal) oedepict.OEPrepareDepiction(mol, dopt) opts = oedepict.OE2DMolDisplayOptions(width, height, oedepict.OEScale_AutoScale) disp = oedepict.OE2DMolDisplay(mol, opts) ofs = oechem.oeosstream() oedepict.OERenderMolecule(ofs, 'png', disp) ofs.flush() return Image(data="".join(ofs.str()))
def plot_indices(mol2, width=200, height=200): mol = mol2.CreateCopy() opts = oedepict.OE2DMolDisplayOptions(width, height, oedepict.OEScale_AutoScale) opts.SetAtomPropertyFunctor(oedepict.OEDisplayAtomIdx()) oedepict.OEPrepareDepiction(mol) disp = oedepict.OE2DMolDisplay(mol, opts) img = oedepict.OEImage(width, height) oedepict.OERenderMolecule(img, disp) return img
def DepictPeptide(opts, smiles, basefilename): mol = oechem.OEGraphMol() oechem.OESmilesToMol(mol, smiles) oedepict.OEPrepareDepiction(mol) image = oedepict.OEImage(400, 200) oegrapheme.OEDrawPeptide(image, mol, opts) oedepict.OEWriteImage(basefilename + ".svg", image) oedepict.OEWriteImage(basefilename + ".pdf", image) oedepict.OEWriteImage(basefilename + ".png", image)
def genHBIndexGuide(inpmol): """ For each mol, generate depiction of molecule with labeled indices as read in by oechem. Saved as _____ """ mol = oechem.OEGraphMol(inpmol) dopt = oedepict.OEPrepareDepictionOptions() dopt.SetDepictOrientation(oedepict.OEDepictOrientation_Horizontal) oedepict.OEPrepareDepiction(mol, dopt) opts = oedepict.OE2DMolDisplayOptions(width, height, oedepict.OEScale_AutoScale) opts.SetAtomPropertyFunctor(oedepict.OEDisplayAtomIdx()) oedepict.OERenderMolecule(outfn, mol)
def oedepict_pdf(all_probe_mols, subdir): """ Generate a PDF report of all molecules together, color-coded by parameter ID and labeled with parameter ID and SMILES tag. Parameters ---------- all_probe_mols: key is string of a parameter id to be probed; value is a list of oegraphmols with this parameter id subdir : string Name of subdirectory in which to save results.pdf file """ multi = oedepict.OEMultiPageImageFile(oedepict.OEPageOrientation_Landscape, oedepict.OEPageSize_US_Letter) image = multi.NewPage() opts = oedepict.OE2DMolDisplayOptions() rows, cols = 4, 4 grid = oedepict.OEImageGrid(image, rows, cols) grid.SetCellGap(20) grid.SetMargins(20) citer = grid.GetCells() colors = list(oechem.OEGetContrastColors()) for i, (param, mol_list) in enumerate(all_probe_mols.items()): pen = oedepict.OEPen(oechem.OEWhite, colors[i], oedepict.OEFill_Off, 4.0) for mol in mol_list: # go to next page if not citer.IsValid(): image = multi.NewPage() grid = oedepict.OEImageGrid(image, rows, cols) grid.SetCellGap(20) grid.SetMargins(20) citer = grid.GetCells() cell = citer.Target() mol.SetTitle(f"{param} {oechem.OEGetSDData(mol, 'SMILES QCArchive')}") oedepict.OEPrepareDepiction(mol) opts.SetDimensions(cell.GetWidth(), cell.GetHeight(), oedepict.OEScale_AutoScale) disp = oedepict.OE2DMolDisplay(mol, opts) oedepict.OERenderMolecule(cell, disp) oedepict.OEDrawBorder(cell, pen) citer.Next() oedepict.OEWriteMultiPageImage(f"{subdir}/results.pdf", multi)
def align2d(file1, file2): atomexpr = oechem.OEExprOpts_AtomicNumber | oechem.OEExprOpts_RingMember bondexpr = oechem.OEExprOpts_RingMember ifs1 = oechem.oemolistream(file1) ifs2 = oechem.oemolistream(file2) ifs1.SetConfTest(oechem.OEAbsCanonicalConfTest()) ifs2.SetConfTest(oechem.OEAbsCanonicalConfTest()) popts, dopts, report = prep_pdf_writer() for mol1, mol2 in zip(ifs1.GetOEMols(), ifs2.GetOEMols()): oechem.OESuppressHydrogens(mol1) oechem.OESuppressHydrogens(mol2) oechem.OEGenerate2DCoordinates(mol2) ss = oechem.OESubSearch(mol2, atomexpr, bondexpr) oechem.OEPrepareSearch(mol1, ss) alignres = oedepict.OEPrepareAlignedDepiction(mol1, ss) if not alignres.IsValid(): oechem.OEThrow.Error( "Substructure is not found in input molecule!") cell1 = report.NewCell() cell2 = report.NewCell() oedepict.OEPrepareDepiction(mol1, popts) oedepict.OEPrepareDepiction(mol2, popts) disp1 = oedepict.OE2DMolDisplay(mol1, dopts) disp2 = oedepict.OE2DMolDisplay(mol2, dopts) oedepict.OERenderMolecule(cell1, disp1) oedepict.OERenderMolecule(cell2, disp2) ofs = oechem.oeofstream() if not ofs.open('output.pdf'): oechem.OEThrow.Fatal("Cannot open output file!") oedepict.OEWriteReport(ofs, "pdf", report)
def highlight_atoms_in_mol(mol2, dihedralAtomIndices, width=200, height=200): mol = mol2.CreateCopy() opts = oedepict.OE2DMolDisplayOptions(width, height, oedepict.OEScale_AutoScale) oedepict.OEPrepareDepiction(mol) disp = oedepict.OE2DMolDisplay(mol, opts) img = oedepict.OEImage(width, height) hstyle = oedepict.OEHighlightByBallAndStick(oechem.OEBlueTint) for atom_idx in dihedralAtomIndices: oedepict.OEAddHighlighting(disp, hstyle, oechem.OEHasAtomIdx(atom_idx)) oedepict.OERenderMolecule(img, disp) return img
def main(argv=[__name__]): itf = oechem.OEInterface(InterfaceData) oedepict.OEConfigureImageOptions(itf) oedepict.OEConfigurePrepareDepictionOptions(itf) oedepict.OEConfigure2DMolDisplayOptions(itf) if not oechem.OEParseCommandLine(itf, argv): oechem.OEThrow.Fatal("Unable to interpret command line!") iname = itf.GetString("-in") oname = itf.GetString("-out") ext = oechem.OEGetFileExtension(oname) if not oedepict.OEIsRegisteredImageFile(ext): oechem.OEThrow.Fatal("Unknown image type!") ifs = oechem.oemolistream() if not ifs.open(iname): oechem.OEThrow.Fatal("Cannot open input file!") ofs = oechem.oeofstream() if not ofs.open(oname): oechem.OEThrow.Fatal("Cannot open output file!") mol = oechem.OEGraphMol() if not oechem.OEReadMolecule(ifs, mol): oechem.OEThrow.Fatal("Cannot read input file!") if itf.HasString("-ringdict"): rdfname = itf.GetString("-ringdict") if not oechem.OEInit2DRingDictionary(rdfname): oechem.OEThrow.Warning("Cannot use user-defined ring dictionary!") popts = oedepict.OEPrepareDepictionOptions() oedepict.OESetupPrepareDepictionOptions(popts, itf) oedepict.OEPrepareDepiction(mol, popts) width, height = oedepict.OEGetImageWidth(itf), oedepict.OEGetImageHeight( itf) dopts = oedepict.OE2DMolDisplayOptions(width, height, oedepict.OEScale_AutoScale) oedepict.OESetup2DMolDisplayOptions(dopts, itf) disp = oedepict.OE2DMolDisplay(mol, dopts) oedepict.OERenderMolecule(ofs, ext, disp) return 0
def DepictMolecules(opts, smiles, basefilename, drawborder=False): mol = oechem.OEGraphMol() oechem.OESmilesToMol(mol, smiles) oedepict.OEPrepareDepiction(mol) disp = oedepict.OE2DMolDisplay(mol, opts) image = oedepict.OEImage(disp.GetWidth(), disp.GetHeight()) oedepict.OERenderMolecule(image, disp) if drawborder: oedepict.OEDrawBorder(image, oedepict.OELightGreyPen) oedepict.OEWriteImage(basefilename + ".png", image) oedepict.OEWriteImage(basefilename + ".pdf", image)
def Draw2DSurface(image, mol): oedepict.OEPrepareDepiction(mol) opts = oedepict.OE2DMolDisplayOptions(image.GetWidth(), image.GetHeight(), oedepict.OEScale_AutoScale) opts.SetTitleLocation(oedepict.OETitleLocation_Hidden) opts.SetScale(oegrapheme.OEGetMoleculeSurfaceScale(mol, opts)) arcfxn = AtomColorArcFxn() for atom in mol.GetAtoms(): oegrapheme.OESetSurfaceArcFxn(mol, atom, arcfxn) disp = oedepict.OE2DMolDisplay(mol, opts) oegrapheme.OEDraw2DSurface(disp) oedepict.OERenderMolecule(image, disp)
def depictMatch(mol, match, width=500, height=200): from IPython.display import Image dopt = oedepict.OEPrepareDepictionOptions() dopt.SetDepictOrientation(oedepict.OEDepictOrientation_Horizontal) dopt.SetSuppressHydrogens(True) oedepict.OEPrepareDepiction(mol, dopt) opts = oedepict.OE2DMolDisplayOptions(width, height, oedepict.OEScale_AutoScale) disp = oedepict.OE2DMolDisplay(mol, opts) hstyle = oedepict.OEHighlightStyle_Color hcolor = oechem.OEColor(oechem.OELightBlue) oedepict.OEAddHighlighting(disp, hcolor, hstyle, match) ofs = oechem.oeosstream() oedepict.OERenderMolecule(ofs, 'png', disp) ofs.flush() return Image(data="".join(ofs.str()))
def main(argv=[__name__]): itf = oechem.OEInterface(InterfaceData) if not oechem.OEParseCommandLine(itf, argv): oechem.OEThrow.Fatal("Unable to interpret command line!") rname = itf.GetString("-ref") fname = itf.GetString("-fit") oname = itf.GetString("-out") rifs = oechem.oemolistream() if not rifs.open(rname): oechem.OEThrow.Fatal("Cannot open reference molecule file!") refmol = oechem.OEGraphMol() if not oechem.OEReadMolecule(rifs, refmol): oechem.OEThrow.Fatal("Cannot read reference molecule!") fifs = oechem.oemolistream() if not fifs.open(fname): oechem.OEThrow.Fatal("Cannot open align molecule file!") ofs = oechem.oemolostream() if not ofs.open(oname): oechem.OEThrow.Fatal("Cannot open output file!") if not oechem.OEIs2DFormat(ofs.GetFormat()): oechem.OEThrow.Fatal("Invalid output format for 2D coordinates") oedepict.OEPrepareDepiction(refmol) mcss = oechem.OEMCSSearch(oechem.OEMCSType_Approximate) atomexpr = oechem.OEExprOpts_DefaultAtoms bondexpr = oechem.OEExprOpts_DefaultBonds mcss.Init(refmol, atomexpr, bondexpr) mcss.SetMCSFunc(oechem.OEMCSMaxBondsCompleteCycles()) oechem.OEWriteConstMolecule(ofs, refmol) for fitmol in fifs.GetOEGraphMols(): alignres = oedepict.OEPrepareAlignedDepiction(fitmol, mcss) if alignres.IsValid(): oechem.OEThrow.Info("%s mcs size: %d" % (fitmol.GetTitle(), alignres.NumAtoms())) oechem.OEWriteMolecule(ofs, fitmol) return 0
def DepictMoleculesWithHighlight(smiles, ss, highlight, basefilename): mol = oechem.OEGraphMol() oechem.OESmilesToMol(mol, smiles) oedepict.OEPrepareDepiction(mol) opts = oedepict.OE2DMolDisplayOptions(220, 160, oedepict.OEScale_AutoScale) disp = oedepict.OE2DMolDisplay(mol, opts) unique = True for match in ss.Match(mol, unique): oedepict.OEAddHighlighting(disp, highlight, match) image = oedepict.OEImage(disp.GetWidth(), disp.GetHeight()) oedepict.OERenderMolecule(image, disp) oedepict.OEWriteImage(basefilename + ".png", image) oedepict.OEWriteImage(basefilename + ".pdf", image)
def render_single_molecule(filename, molecule, width=1200, height=600): """ simple function to create an oemol image Parameters ---------- filename : str The PDF filename to write to. molecule : openeye.oechem.OEMol molecule width : int, optional, default=1200 Width in pixels height : int, optional, default=1200 Height in pixels """ from openeye import oechem, oedepict oedepict.OEPrepareDepiction(molecule) oedepict.OERenderMolecule(filename, molecule)
def DepictMoleculesWithHighlight(smiles, ss, highlight, basefilename): mol = oechem.OEGraphMol() oechem.OESmilesToMol(mol, smiles) oedepict.OEPrepareDepiction(mol) opts = oedepict.OE2DMolDisplayOptions(250, 160, oedepict.OEScale_AutoScale) opts.SetTitleLocation(oedepict.OETitleLocation_Hidden) disp = oedepict.OE2DMolDisplay(mol, opts) unique = True oedepict.OEAddHighlightOverlay(disp, highlight, ss.Match(mol, unique)) image = oedepict.OEImage(disp.GetWidth(), disp.GetHeight()) oedepict.OERenderMolecule(image, disp) oedepict.OEWriteImage(basefilename + ".png", image) oedepict.OEWriteImage(basefilename + ".pdf", image)