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__]): 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 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 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 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 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 prep_pdf_writer(): itf = oechem.OEInterface() 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) return popts, dopts, report
def main(argv=[__name__]): itf = oechem.OEInterface(InterfaceData) oedepict.OEConfigurePrepareDepictionOptions( itf, oedepict.OEPrepareDepictionSetup_SuppressHydrogens | oedepict.OEPrepareDepictionSetup_DepictOrientation) if not oechem.OEParseCommandLine(itf, argv): oechem.OEThrow.Fatal("Unable to interpret command line!") iname = itf.GetString("-in") ifs = oechem.oemolistream() if not ifs.open(iname): oechem.OEThrow.Fatal("Cannot open input file!") ofs = oechem.oemolostream(".sdf") if itf.HasString("-out"): oname = itf.GetString("-out") 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") 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) popts.SetClearCoords(True) for mol in ifs.GetOEGraphMols(): oedepict.OEPrepareDepiction(mol, popts) oechem.OEWriteMolecule(ofs, mol) return 0
def main(argv=[__name__]): itf = oechem.OEInterface(InterfaceData) oedepict.OEConfigureImageOptions(itf) oedepict.OEConfigurePrepareDepictionOptions(itf) oedepict.OEConfigure2DMolDisplayOptions(itf) oedepict.OEConfigureHighlightParams(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!") smarts = itf.GetString("-smarts") ss = oechem.OESubSearch() if not ss.Init(smarts): oechem.OEThrow.Fatal("Cannot parse smarts: %s" % smarts) 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) dopts.SetMargins(10.0) disp = oedepict.OE2DMolDisplay(mol, dopts) hstyle = oedepict.OEGetHighlightStyle(itf) hcolor = oedepict.OEGetHighlightColor(itf) oechem.OEPrepareSearch(mol, ss) unique = True for match in ss.Match(mol, unique): oedepict.OEAddHighlighting(disp, hcolor, hstyle, match) oedepict.OERenderMolecule(ofs, ext, disp) return 0
def highltigh_torsion_by_cluster(mapped_molecule, clustered_dihedrals, fname, width=600, height=400): """ Highlight torsion by cluster. This is used to visualize clustering output. Parameters ---------- mapped_molecule: oemol with map indices clustered_dihedrals fname width height Returns ------- """ mol = oechem.OEMol(mapped_molecule) atom_bond_sets = [] for cluster in clustered_dihedrals: atom_bond_set = oechem.OEAtomBondSet() for dihedral in clustered_dihedrals[cluster]: a = mol.GetAtom(oechem.OEHasMapIdx(dihedral[0] + 1)) atom_bond_set.AddAtom(a) for idx in dihedral[1:]: a2 = mol.GetAtom(oechem.OEHasMapIdx(idx + 1)) atom_bond_set.AddAtom(a2) bond = mol.GetBond(a, a2) atom_bond_set.AddBond(bond) a = a2 atom_bond_sets.append(atom_bond_set) dopt = oedepict.OEPrepareDepictionOptions() dopt.SetSuppressHydrogens(False) oedepict.OEPrepareDepiction(mol, dopt) opts = oedepict.OE2DMolDisplayOptions(width, height, oedepict.OEScale_AutoScale) opts.SetTitleLocation(oedepict.OETitleLocation_Hidden) disp = oedepict.OE2DMolDisplay(mol, opts) aroStyle = oedepict.OEHighlightStyle_Color aroColor = oechem.OEColor(oechem.OEBlack) oedepict.OEAddHighlighting(disp, aroColor, aroStyle, oechem.OEIsAromaticAtom(), oechem.OEIsAromaticBond()) hstyle = oedepict.OEHighlightStyle_BallAndStick # if color: # highlight = oechem.OEColor(color) # # combine all atom_bond_sets # atom_bond_set = oechem.OEAtomBondSet() # for ab_set in atom_bond_sets: # for a in ab_set.GetAtoms(): # atom_bond_set.AddAtom(a) # for b in ab_set.GetBonds(): # atom_bond_set.AddBond(b) # oedepict.OEAddHighlighting(disp, highlight, hstyle, atom_bond_set) # else: highlight = oedepict.OEHighlightOverlayByBallAndStick( oechem.OEGetContrastColors()) oedepict.OEAddHighlightOverlay(disp, highlight, atom_bond_sets) #hcolor = oechem.OEColor(oechem.OELightBlue) #oedepict.OEAddHighlighting(disp, hcolor, hstyle, atom_bond_sets) return oedepict.OERenderMolecule(fname, disp)
def highlight_torsion(mapped_molecule, dihedrals, fname, width=600, height=400, combine_central_bond=True, color=None): mol = oechem.OEMol(mapped_molecule) atom_bond_sets = [] if combine_central_bond: central_bonds = [(tor[1], tor[2]) for tor in dihedrals] eq_torsions = { cb: [ tor for tor in dihedrals if cb == (tor[1], tor[2]) or cb == (tor[2], tor[1]) ] for cb in central_bonds } for cb in eq_torsions: atom_bond_set = oechem.OEAtomBondSet() for dihedral in eq_torsions[cb]: a = mol.GetAtom(oechem.OEHasMapIdx(dihedral[0] + 1)) atom_bond_set.AddAtom(a) for idx in dihedral[1:]: a2 = mol.GetAtom(oechem.OEHasMapIdx(idx + 1)) atom_bond_set.AddAtom((a2)) bond = mol.GetBond(a, a2) atom_bond_set.AddBond(bond) a = a2 atom_bond_sets.append(atom_bond_set) if not combine_central_bond: for dihedral in dihedrals: atom_bond_set = oechem.OEAtomBondSet() a = mol.GetAtom(oechem.OEHasMapIdx(dihedral[0] + 1)) atom_bond_set.AddAtom(a) for idx in dihedral[1:]: a2 = mol.GetAtom(oechem.OEHasMapIdx(idx + 1)) atom_bond_set.AddAtom((a2)) bond = mol.GetBond(a, a2) atom_bond_set.AddBond(bond) a = a2 atom_bond_sets.append(atom_bond_set) dopt = oedepict.OEPrepareDepictionOptions() dopt.SetSuppressHydrogens(False) oedepict.OEPrepareDepiction(mol, dopt) opts = oedepict.OE2DMolDisplayOptions(width, height, oedepict.OEScale_AutoScale) opts.SetTitleLocation(oedepict.OETitleLocation_Hidden) disp = oedepict.OE2DMolDisplay(mol, opts) aroStyle = oedepict.OEHighlightStyle_Color aroColor = oechem.OEColor(oechem.OEBlack) oedepict.OEAddHighlighting(disp, aroColor, aroStyle, oechem.OEIsAromaticAtom(), oechem.OEIsAromaticBond()) hstyle = oedepict.OEHighlightStyle_BallAndStick if color: highlight = oechem.OEColor(color) # combine all atom_bond_sets atom_bond_set = oechem.OEAtomBondSet() for ab_set in atom_bond_sets: for a in ab_set.GetAtoms(): atom_bond_set.AddAtom(a) for b in ab_set.GetBonds(): atom_bond_set.AddBond(b) oedepict.OEAddHighlighting(disp, highlight, hstyle, atom_bond_set) else: highlight = oedepict.OEHighlightOverlayByBallAndStick( oechem.OEGetContrastColors()) oedepict.OEAddHighlightOverlay(disp, highlight, atom_bond_sets) #hcolor = oechem.OEColor(oechem.OELightBlue) #oedepict.OEAddHighlighting(disp, hcolor, hstyle, atom_bond_sets) return oedepict.OERenderMolecule(fname, disp)
def highlight_bonds(mol_copy, fname, conjugation=True, rotor=False, width=600, height=400, label=None): """ Generate image of molecule with highlighted bonds. The bonds can either be highlighted with a conjugation tag or if it is rotatable. Parameters ---------- mol_copy: OEMol fname: str Name of image file conjugation: Bool, optional, Default is True If True, the bonds with conjugation tag set to True will be highlighted rotor: Bool, optional, Default is False If True, the rotatable bonds will be highlighted. width: int height: int label: string. Optional, Default is None The bond order label. The options are WibergBondOrder, Wiberg_psi4, Mayer_psi4. """ mol = oechem.OEMol(mol_copy) bond_index_list = [] for bond in mol.GetBonds(): if conjugation: try: if bond.GetData('conjugated'): bond_index_list.append(bond.GetIdx()) except ValueError: pass if rotor: if bond.IsRotor(): bond_index_list.append(bond.GetIdx()) atomBondSet = oechem.OEAtomBondSet() for bond in mol.GetBonds(): if bond.GetIdx() in bond_index_list: atomBondSet.AddBond(bond) atomBondSet.AddAtom(bond.GetBgn()) atomBondSet.AddAtom(bond.GetEnd()) dopt = oedepict.OEPrepareDepictionOptions() dopt.SetSuppressHydrogens(True) oedepict.OEPrepareDepiction(mol, dopt) opts = oedepict.OE2DMolDisplayOptions(width, height, oedepict.OEScale_AutoScale) opts.SetTitleLocation(oedepict.OETitleLocation_Hidden) if label is not None: bond_label = { 'WibergBondOrder': LabelWibergBondOrder, 'Wiberg_psi4': LabelWibergPsiBondOrder, 'Mayer_psi4': LabelMayerPsiBondOrder } bondlabel = bond_label[label] opts.SetBondPropertyFunctor(bondlabel()) disp = oedepict.OE2DMolDisplay(mol, opts) aroStyle = oedepict.OEHighlightStyle_Color aroColor = oechem.OEColor(oechem.OEBlack) oedepict.OEAddHighlighting(disp, aroColor, aroStyle, oechem.OEIsAromaticAtom(), oechem.OEIsAromaticBond()) hstyle = oedepict.OEHighlightStyle_BallAndStick hcolor = oechem.OEColor(oechem.OELightBlue) oedepict.OEAddHighlighting(disp, hcolor, hstyle, atomBondSet) return oedepict.OERenderMolecule(fname, disp)