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 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 prepare(self): self.__setupImage() rows = self._params["gridRows"] cols = self._params["gridCols"] grid = oedepict.OEImageGrid(self.__image, rows, cols) citer = grid.GetCells() for ccId, oeMol, title in self._molTitleList: logger.debug("Preparing %s %r", ccId, title) if not citer.IsValid(): # go to next page self.__image = self.__multi.NewPage() grid = oedepict.OEImageGrid(self.__image, rows, cols) grid.SetCellGap(self._params["cellGap"]) grid.SetMargins(self._params["cellMargin"]) citer = grid.GetCells() cell = citer.Target() # 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) oedepict.OEDrawBorder(cell, oedepict.OEPen(oedepict.OEBlackPen)) citer.Next()
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 CreateGrid(image): rows, cols = 3, 3 grid = oedepict.OEImageGrid(image, rows, cols) grid.SetCellGap(5) grid.SetMargins(0) for cell in grid.GetCells(): oedepict.OEDrawBorder(cell, oedepict.OERedPen) return grid
def __setupImage(self): """Internal method to configure a single page image.""" # self.__image = oedepict.OEImage(self._params["imageSizeX"], self._params["imageSizeY"]) self.__grid = oedepict.OEImageGrid(self.__image, self._params["gridRows"], self._params["gridCols"]) self.__grid.SetCellGap(self._params["cellGap"]) self.__grid.SetMargins(self._params["cellMargin"]) self._opts = oedepict.OE2DMolDisplayOptions( self.__grid.GetCellWidth(), self.__grid.GetCellHeight(), oedepict.OEScale_AutoScale) # logger.debug("Num columns %d", self.__grid.NumCols()) logger.debug("Num rows %d", self.__grid.NumRows())
if len(sys.argv) != 3: oechem.OEThrow.Usage("%s <rxnfile> <imagefile>" % sys.argv[0]) ifs = oechem.oemolistream(sys.argv[1]) mol = oechem.OEGraphMol() if not oechem.OEReadRxnFile(ifs, mol): oechem.OEThrow.Fatal("%s error reading reaction file" % sys.argv[0]) oedepict.OEPrepareDepiction(mol) image = oedepict.OEImage(900, 100) rows, cols = 1, 3 grid = oedepict.OEImageGrid(image, rows, cols) opts = oedepict.OE2DMolDisplayOptions(grid.GetCellWidth(), grid.GetCellHeight(), oedepict.OEScale_AutoScale) cell = grid.GetCell(1, 1) mol.SetTitle("Reaction display") opts.SetAtomVisibilityFunctor( oechem.OEIsTrueAtom()) # explicitly set the default disp = oedepict.OE2DMolDisplay(mol, opts) rxnscale = disp.GetScale() oedepict.OERenderMolecule(cell, disp) cell = grid.GetCell(1, 2) mol.SetTitle("Reactant display")
def gen_pdf( tid_clusters_list: list, output_path: str, cols: int = 8, cell_width: int = 200, cell_height: int = 200, ): from openeye import oechem, oedepict from openforcefield.topology import Molecule itf = oechem.OEInterface() PageByPage = True suppress_h = True n = sum([len(clusters) for tid, clusters in tid_clusters_list.items()]) rows = math.ceil(n / 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) count = 0 for tid, clusters in tid_clusters_list.items(): for cluster in clusters: torsions = cluster['torsions'] label = cluster['cluster_label'] torsions = cluster['torsions'] for torsion in torsions: cell = grid.GetCell(count // cols + 1, count % cols + 1) smi = torsion['mol_index'] atom_indices = torsion['indices'] # mol = oechem.OEGraphMol() # oechem.OESmilesToMol(mol, smi) off_mol = Molecule.from_smiles(smi, allow_undefined_stereo=True) off_mol = off_mol.canonical_order_atoms() mol = Molecule.to_openeye(off_mol) title = '{} ({})'.format(tid, set(torsion['covered_tids'])) mol.SetTitle(title) 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 AtomInTorsion(oechem.OEUnaryAtomPred): def __call__(self, atom): return atom.GetIdx() in atom_indices class NoBond(oechem.OEUnaryBondPred): def __call__(self, bond): return False 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]) atoms = mol.GetAtoms(AtomInTorsion()) bonds = mol.GetBonds(NoBond()) abset = oechem.OEAtomBondSet(atoms, bonds) oedepict.OEAddHighlighting( disp, oechem.OEColor(oechem.OEYellow), oedepict.OEHighlightStyle_BallAndStick, abset) 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) count += 1 oedepict.OEWriteImage(output_path, image)
def gen_pdf( cluster_dic: list, 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 n = sum([len(dic['torsions']) for dic in cluster_dic]) rows = math.ceil(n / 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, (dic, cell) in enumerate(zip(cluster_dic, grid.GetCells())): count = 0 for i, dic in enumerate(cluster_dic): if 'cluster_label' in dic: label = dic['cluster_label'] torsions = dic['torsions'] for torsion in torsions: cell = grid.GetCell(count // cols + 1, count % cols + 1) smi = torsion['mol_index'] atom_indices = torsion['indices'] mol = oechem.OEGraphMol() oechem.OESmilesToMol(mol, smi) title = 'label:{} {}'.format(label, torsion['covered_tids']) mol.SetTitle(title) 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 AtomInTorsion(oechem.OEUnaryAtomPred): def __call__(self, atom): return atom.GetIdx() in atom_indices class NoBond(oechem.OEUnaryBondPred): def __call__(self, bond): return False 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]) atoms = mol.GetAtoms(AtomInTorsion()) bonds = mol.GetBonds(NoBond()) abset = oechem.OEAtomBondSet(atoms, bonds) oedepict.OEAddHighlighting( disp, oechem.OEColor(oechem.OEYellow), oedepict.OEHighlightStyle_BallAndStick, abset) 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) count += 1 elif 'mol_index' in dic: cell = grid.GetCell(count // cols + 1, count % cols + 1) smi = dic['mol_index'] atom_indices = dic['indices'] mol = oechem.OEGraphMol() oechem.OESmilesToMol(mol, smi) 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 AtomInTorsion(oechem.OEUnaryAtomPred): def __call__(self, atom): return atom.GetIdx() in atom_indices class NoBond(oechem.OEUnaryBondPred): def __call__(self, bond): return False 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]) atoms = mol.GetAtoms(AtomInTorsion()) bonds = mol.GetBonds(NoBond()) abset = oechem.OEAtomBondSet(atoms, bonds) oedepict.OEAddHighlighting(disp, oechem.OEColor(oechem.OEYellow), oedepict.OEHighlightStyle_BallAndStick, abset) 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) count += 1 else: for cluster in cluster_dic[dic]: label = cluster['cluster_label'] torsions = cluster['torsions'] for torsion in torsions: cell = grid.GetCell(count // cols + 1, count % cols + 1) smi = torsion['mol_index'] atom_indices = torsion['indices'] mol = oechem.OEGraphMol() oechem.OESmilesToMol(mol, smi) title = 'tid: {}, label:{}'.format(dic, label) mol.SetTitle(title) 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 AtomInTorsion(oechem.OEUnaryAtomPred): def __call__(self, atom): return atom.GetIdx() in atom_indices class NoBond(oechem.OEUnaryBondPred): def __call__(self, bond): return False 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]) atoms = mol.GetAtoms(AtomInTorsion()) bonds = mol.GetBonds(NoBond()) abset = oechem.OEAtomBondSet(atoms, bonds) oedepict.OEAddHighlighting( disp, oechem.OEColor(oechem.OEYellow), oedepict.OEHighlightStyle_BallAndStick, abset) 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) count += 1 oedepict.OEWriteImage(output_path, image)