def AtomPathLength(ifs, ofs, itf, atm1, atm2): for mol in ifs.GetOEGraphMols(): oechem.OETriposAtomNames(mol) a1 = None a2 = None for atm in mol.GetAtoms(): if atm.GetName() == atm1: a1 = atm if atm.GetName() == atm2: a2 = atm if a1 is not None and a2 is not None: break if a1 is None or a2 is None: oechem.OEThrow.Warning( "Failed to find atoms %s and %s in molecule" % (atm1, atm2)) continue pathlen = oechem.OEGetPathLength(a1, a2) if itf.GetBool("-verbose") or not itf.HasString("-o"): print("Path length: %s in %s" % (pathlen, oechem.OEMolToSmiles(mol))) spath = oechem.OEShortestPath(a1, a2) spathmol = oechem.OEGraphMol() adjustHCount = True oechem.OESubsetMol(spathmol, mol, oechem.OEIsAtomMember(spath), adjustHCount) spathsmiles = oechem.OEMolToSmiles(spathmol) if itf.HasString("-o"): oechem.OEWriteMolecule(ofs, spathmol) elif itf.GetBool("-verbose"): print(spathsmiles)
def atom_bond_set_to_mol(frag, oemol, adjust_hcount=False, RGroup=True): from openeye import oechem import warnings fragatompred = oechem.OEIsAtomMember(frag.GetAtoms()) fragbondpred = oechem.OEIsBondMember(frag.GetBonds()) fragment_oemol = oechem.OEMol() adjustHCount = adjust_hcount oechem.OESubsetMol(fragment_oemol, oemol, fragatompred, fragbondpred, adjustHCount, RGroup) oechem.OEAddExplicitHydrogens(fragment_oemol) # sanity check that all atoms are bonded for atom in fragment_oemol.GetAtoms(): if not list(atom.GetBonds()): warnings.warn( "Yikes!!! An atom that is not bonded to any other atom in the fragment. " "You probably ran into a bug. Please report the input molecule to the issue tracker" ) # Perceive stereo and check that defined stereo did not change oechem.OEPerceiveChiral(fragment_oemol) oechem.OE3DToAtomStereo(fragment_oemol) oechem.OE3DToBondStereo(fragment_oemol) return fragment_oemol
def frag_to_smiles(frags, mol): """ Convert fragments (AtomBondSet) to smiles string Parameters ---------- frags mol Returns ------- smiles: list of smiles strings """ smiles = {} for frag in frags: fragatompred = oechem.OEIsAtomMember(frag.GetAtoms()) fragbondpred = oechem.OEIsBondMember(frag.GetBonds()) fragment = oechem.OEGraphMol() adjustHCount = True oechem.OESubsetMol(fragment, mol, fragatompred, fragbondpred, adjustHCount) s = oechem.OECreateIsoSmiString(fragment) if s not in smiles: smiles[s] = [] smiles[s].append(frag) return smiles
def SmartsPathLength(ifs, ofs, itf, ss1, ss2): for mol in ifs.GetOEGraphMols(): oechem.OEPrepareSearch(mol, ss1) oechem.OEPrepareSearch(mol, ss2) if not (ss1.SingleMatch(mol) and ss2.SingleMatch(mol)): oechem.OEThrow.Warning( "Unable to find SMARTS matches in %s, skipping" % mol.GetTitle()) continue unique = True allminlen = sys.maxsize for match1 in ss1.Match(mol, unique): for match2 in ss2.Match(mol, unique): minlen = sys.maxsize for atom1 in match1.GetTargetAtoms(): for atom2 in match2.GetTargetAtoms(): pathlen = oechem.OEGetPathLength(atom1, atom2) if minlen > pathlen: minlen = pathlen atompairs = [] atompairs.append([atom1, atom2]) elif minlen == pathlen: atompairs.append([atom1, atom2]) if minlen < allminlen: allminlen = minlen allatompairs = atompairs[:] elif minlen == allminlen: allatompairs += atompairs[:] if itf.GetBool("-verbose") or not itf.HasString("-o"): print("Shortest path length: %s in %s" % (allminlen, oechem.OEMolToSmiles(mol))) spathlist = set() for satom1, satom2, in allatompairs: spath = oechem.OEShortestPath(satom1, satom2) spathmol = oechem.OEGraphMol() oechem.OESubsetMol(spathmol, mol, oechem.OEIsAtomMember(spath)) spathsmiles = oechem.OEMolToSmiles(spathmol) if spathsmiles in spathlist: continue spathlist.add(spathsmiles) if itf.HasString("-o"): oechem.OEWriteMolecule(ofs, spathmol) elif itf.GetBool("-verbose"): print(spathsmiles) return
def GetFragmentCombinations(mol, fraglist, frag_number): fragments = [] fragcombs = GetFragmentAtomBondSetCombinations(mol, fraglist, frag_number) for f in fragcombs: fragatompred = oechem.OEIsAtomMember(f.GetAtoms()) fragbondpred = oechem.OEIsBondMember(f.GetBonds()) fragment = oechem.OEGraphMol() adjustHCount = True oechem.OESubsetMol(fragment, mol, fragatompred, fragbondpred, adjustHCount) fragments.append(fragment) return fragments
def BackBone(ifs, ofs): adjustHCount = True mol = oechem.OEGraphMol() backboneMol = oechem.OEGraphMol() while oechem.OEReadMolecule(ifs, mol): if not oechem.OEHasResidues(mol): oechem.OEPerceiveResidues(mol, oechem.OEPreserveResInfo_All) aiter = mol.GetAtoms(oechem.OEIsBackboneAtom()) member = oechem.OEIsAtomMember(aiter) oechem.OESubsetMol(backboneMol, mol, member, adjustHCount) oechem.OEWriteMolecule(ofs, backboneMol)
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 SubSetRes(ifs, ofs, chainid, resname, resnum): adjustHCount = True mol = oechem.OEGraphMol() while oechem.OEReadMolecule(ifs, mol): if not oechem.OEHasResidues(mol): oechem.OEPerceiveResidues(mol, oechem.OEPreserveResInfo_All) hv = oechem.OEHierView(mol) res = hv.GetResidue(chainid, resname, resnum) if res.GetOEResidue().GetName() is None: oechem.OEThrow.Fatal("Failed to find residue") atomiter = res.GetAtoms() member = oechem.OEIsAtomMember(atomiter) resmol = oechem.OEGraphMol() oechem.OESubsetMol(resmol, mol, member, adjustHCount) if chainid == " ": resmol.SetTitle("%s %d" % (resname, resnum)) else: resmol.SetTitle("%s %s %d" % (resname, chainid, resnum)) oechem.OEWriteMolecule(ofs, resmol)
def frag_to_smiles(frags, mol): """ Convert fragments (AtomBondSet) to canonical isomeric SMILES string Parameters ---------- frags: list mol: OEMol OESMILESFlag: str Either 'ISOMERIC' or 'DEFAULT'. This flag determines which OE function to use to generate SMILES string Returns ------- smiles: dict of smiles to frag """ smiles = {} for frag in frags: fragatompred = oechem.OEIsAtomMember(frag.GetAtoms()) fragbondpred = oechem.OEIsBondMember(frag.GetBonds()) #fragment = oechem.OEGraphMol() fragment = oechem.OEMol() adjustHCount = True oechem.OESubsetMol(fragment, mol, fragatompred, fragbondpred, adjustHCount) oechem.OEPerceiveChiral(fragment) # sanity check that all atoms are bonded for atom in fragment.GetAtoms(): if not list(atom.GetBonds()): raise Warning("Yikes!!! An atom that is not bonded to any other atom in the fragment. " "You probably ran into a bug. Please report the input molecule to the issue tracker") #s = oechem.OEMolToSmiles(fragment) #s2 = fragmenter.utils.create_mapped_smiles(fragment, tagged=False, explicit_hydrogen=False) s = mol_to_smiles(fragment, mapped=False, explicit_hydrogen=True, isomeric=True) if s not in smiles: smiles[s] = [] smiles[s].append(frag) return smiles
# 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. # @ <SNIPPET> from __future__ import print_function from openeye import oechem from openeye import oemedchem mol = oechem.OEGraphMol() oechem.OESmilesToMol(mol, "COc1ccc(cc1)CC(=O)N") for frag in oemedchem.OEGetRingChainFragments(mol): fragatompred = oechem.OEIsAtomMember(frag.GetAtoms()) fragbondpred = oechem.OEIsBondMember(frag.GetBonds()) fragment = oechem.OEGraphMol() adjustHCount = True oechem.OESubsetMol(fragment, mol, fragatompred, fragbondpred, adjustHCount) print(oechem.OEMolToSmiles(fragment)) # @ </SNIPPET>
def _extract_oe_fragment( molecule: Molecule, atom_indices: Set[int], bond_indices: Set[Tuple[int, int]] ) -> Molecule: from openeye import oechem oe_molecule = molecule.to_openeye() # Restore the map indices as to_openeye does not automatically add them. for atom_index, map_index in molecule.properties["atom_map"].items(): oe_atom = oe_molecule.GetAtom(oechem.OEHasAtomIdx(atom_index)) oe_atom.SetMapIdx(map_index) # Include any Hs bonded to the included atom set so we can retain their map # indices. for map_index in {*atom_indices}: oe_atom = oe_molecule.GetAtom(oechem.OEHasMapIdx(map_index)) for neighbour in oe_atom.GetAtoms(): if ( neighbour.GetAtomicNum() != 1 or neighbour.GetMapIdx() < 1 or neighbour.GetMapIdx() in atom_indices ): continue atom_indices.add(neighbour.GetMapIdx()) bond_indices.add((map_index, neighbour.GetMapIdx())) atom_bond_set = oechem.OEAtomBondSet() for map_index in atom_indices: atom = oe_molecule.GetAtom(oechem.OEHasMapIdx(map_index)) atom_bond_set.AddAtom(atom) for map_index_1, map_index_2 in bond_indices: atom_1 = oe_molecule.GetAtom(oechem.OEHasMapIdx(map_index_1)) atom_2 = oe_molecule.GetAtom(oechem.OEHasMapIdx(map_index_2)) bond = oe_molecule.GetBond(atom_1, atom_2) if not bond: raise ValueError(f"{(map_index_1, map_index_2)} is a disconnected bond") atom_bond_set.AddBond(bond) atom_predicate = oechem.OEIsAtomMember(atom_bond_set.GetAtoms()) bond_predicate = oechem.OEIsBondMember(atom_bond_set.GetBonds()) fragment = oechem.OEMol() oechem.OESubsetMol(fragment, oe_molecule, atom_predicate, bond_predicate, True) oechem.OEAddExplicitHydrogens(fragment) oechem.OEPerceiveChiral(fragment) # Always restore map? # if restore_maps: # In some cases (symmetric molecules) this changes the atom map so skip it # restore_atom_map(fragment) # atom map should be restored for combinatorial fragmentation # Perceive stereo and check that defined stereo did not change oechem.OEPerceiveChiral(fragment) oechem.OE3DToAtomStereo(fragment) oechem.OE3DToBondStereo(fragment) return Molecule.from_openeye(fragment, allow_undefined_stereo=True)
def DepictMoleculeWithFragmentCombinations(report, mol, frags, opts): #fragcombs, opts): """ This function was taken from https://docs.eyesopen.com/toolkits/cookbook/python/depiction/enumfrags.html with some modification """ stag = "fragment idx" itag = oechem.OEGetTag(stag) for fidx, frag in enumerate(frags): for bond in frags[frag].GetBonds(): bond.SetData(itag, fidx) # setup depiction styles nrfrags = len(frags) colors = [c for c in oechem.OEGetLightColors()] if len(colors) < nrfrags: colors = [c for c in oechem.OEGetColors(oechem.OEYellowTint, oechem.OEDarkOrange, nrfrags)] bondglyph = ColorBondByFragmentIndex(colors, itag) lineWidthScale = 0.75 fadehighlight = oedepict.OEHighlightByColor(oechem.OEGrey, lineWidthScale) # depict each fragment combinations for frag in frags: cell = report.NewCell() disp = oedepict.OE2DMolDisplay(mol, opts) fragatoms = oechem.OEIsAtomMember(frags[frag].GetAtoms()) fragbonds = oechem.OEIsBondMember(frags[frag].GetBonds()) notfragatoms = oechem.OENotAtom(fragatoms) notfragbonds = oechem.OENotBond(fragbonds) oedepict.OEAddHighlighting(disp, fadehighlight, notfragatoms, notfragbonds) bond = mol.GetBond(oechem.OEHasBondIdx(frag)) atomBondSet = oechem.OEAtomBondSet() atomBondSet.AddBond(bond) atomBondSet.AddAtom(bond.GetBgn()) atomBondSet.AddAtom(bond.GetEnd()) hstyle = oedepict.OEHighlightStyle_BallAndStick hcolor = oechem.OEColor(oechem.OELightBlue) oedepict.OEAddHighlighting(disp, hcolor, hstyle, atomBondSet) #oegrapheme.OEAddGlyph(disp, bondglyph, fragbonds) oedepict.OERenderMolecule(cell, disp) # depict original fragmentation in each header cellwidth, cellheight = report.GetHeaderWidth(), report.GetHeaderHeight() opts.SetDimensions(cellwidth, cellheight, oedepict.OEScale_AutoScale) opts.SetAtomColorStyle(oedepict.OEAtomColorStyle_WhiteMonochrome) bondlabel = LabelBondOrder() opts.SetBondPropertyFunctor(bondlabel) disp = oedepict.OE2DMolDisplay(mol, opts) #oegrapheme.OEAddGlyph(disp, bondglyph, oechem.IsTrueBond()) headerpen = oedepict.OEPen(oechem.OEWhite, oechem.OELightGrey, oedepict.OEFill_Off, 2.0) for header in report.GetHeaders(): oedepict.OERenderMolecule(header, disp) oedepict.OEDrawBorder(header, headerpen)