def main(argv=[__name__]): itf = oechem.OEInterface(InterfaceData) if not oechem.OEParseCommandLine(itf, argv): oechem.OEThrow.Fatal("Unable to interpret command line!") iname = itf.GetString("-in") oname = itf.GetString("-out") smarts = itf.GetString("-smarts") qmol = oechem.OEQMol() if not oechem.OEParseSmarts(qmol, smarts): oechem.OEThrow.Fatal("Invalid SMARTS: %s" % smarts) oechem.OEGenerate2DCoordinates(qmol) ss = oechem.OESubSearch(qmol) if not ss.IsValid(): oechem.OEThrow.Fatal("Unable to initialize substructure search!") ifs = oechem.oemolistream() if not ifs.open(iname): oechem.OEThrow.Fatal("Cannot open input 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") for mol in ifs.GetOEGraphMols(): oechem.OEPrepareSearch(mol, ss) alignres = oedepict.OEPrepareAlignedDepiction(mol, ss) if not alignres.IsValid(): oechem.OEThrow.Warning( "Substructure is not found in input molecule!") oedepict.OEPrepareDepiction(mol) oechem.OEWriteMolecule(ofs, mol) return 0
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 render_atom_mapping(filename, molecule1, molecule2, new_to_old_atom_map, width=1200, height=600): """ Render the atom mapping to a PDF file. Parameters ---------- filename : str The PDF filename to write to. molecule1 : openeye.oechem.OEMol Initial molecule molecule2 : openeye.oechem.OEMol Final molecule new_to_old_atom_map : dict of int new_to_old_atom_map[molecule2_atom_index] is the corresponding molecule1 atom index width : int, optional, default=1200 Width in pixels height : int, optional, default=1200 Height in pixels """ from openeye import oechem, oedepict # Make copies of the input molecules molecule1, molecule2 = oechem.OEGraphMol(molecule1), oechem.OEGraphMol(molecule2) oechem.OEGenerate2DCoordinates(molecule1) oechem.OEGenerate2DCoordinates(molecule2) # Add both to an OEGraphMol reaction rmol = oechem.OEGraphMol() rmol.SetRxn(True) def add_molecule(mol): # Add atoms new_atoms = list() old_to_new_atoms = dict() for old_atom in mol.GetAtoms(): new_atom = rmol.NewAtom(old_atom.GetAtomicNum()) new_atoms.append(new_atom) old_to_new_atoms[old_atom] = new_atom # Add bonds for old_bond in mol.GetBonds(): rmol.NewBond(old_to_new_atoms[old_bond.GetBgn()], old_to_new_atoms[old_bond.GetEnd()], old_bond.GetOrder()) return new_atoms, old_to_new_atoms [new_atoms_1, old_to_new_atoms_1] = add_molecule(molecule1) [new_atoms_2, old_to_new_atoms_2] = add_molecule(molecule2) # Label reactant and product for atom in new_atoms_1: atom.SetRxnRole(oechem.OERxnRole_Reactant) for atom in new_atoms_2: atom.SetRxnRole(oechem.OERxnRole_Product) core1 = oechem.OEAtomBondSet() core2 = oechem.OEAtomBondSet() # add all atoms to the set core1.AddAtoms(new_atoms_1) core2.AddAtoms(new_atoms_2) # Label mapped atoms core_change = oechem.OEAtomBondSet() index =1 for (index2, index1) in new_to_old_atom_map.items(): new_atoms_1[index1].SetMapIdx(index) new_atoms_2[index2].SetMapIdx(index) # now remove the atoms that are core, so only uniques are highlighted core1.RemoveAtom(new_atoms_1[index1]) core2.RemoveAtom(new_atoms_2[index2]) if new_atoms_1[index1].GetAtomicNum() != new_atoms_2[index2].GetAtomicNum(): # this means the element type is changing core_change.AddAtom(new_atoms_1[index1]) core_change.AddAtom(new_atoms_2[index2]) index += 1 # Set up image options itf = oechem.OEInterface() oedepict.OEConfigureImageOptions(itf) ext = oechem.OEGetFileExtension(filename) if not oedepict.OEIsRegisteredImageFile(ext): raise Exception('Unknown image type for filename %s' % filename) ofs = oechem.oeofstream() if not ofs.open(filename): raise Exception('Cannot open output file %s' % filename) # Setup depiction options oedepict.OEConfigure2DMolDisplayOptions(itf, oedepict.OE2DMolDisplaySetup_AromaticStyle) opts = oedepict.OE2DMolDisplayOptions(width, height, oedepict.OEScale_AutoScale) oedepict.OESetup2DMolDisplayOptions(opts, itf) opts.SetBondWidthScaling(True) opts.SetAtomPropertyFunctor(oedepict.OEDisplayAtomMapIdx()) opts.SetAtomColorStyle(oedepict.OEAtomColorStyle_WhiteMonochrome) # Depict reaction with component highlights oechem.OEGenerate2DCoordinates(rmol) rdisp = oedepict.OE2DMolDisplay(rmol, opts) oedepict.OEAddHighlighting(rdisp, oechem.OEColor(oechem.OEPink),oedepict.OEHighlightStyle_Stick, core1) oedepict.OEAddHighlighting(rdisp, oechem.OEColor(oechem.OEPurple),oedepict.OEHighlightStyle_Stick, core2) oedepict.OEAddHighlighting(rdisp, oechem.OEColor(oechem.OEGreen),oedepict.OEHighlightStyle_Stick, core_change) oedepict.OERenderMolecule(ofs, ext, rdisp) ofs.close()