def get_mols_from_frags(this_smiles, old_smiles=None): if old_smiles is None: old_smiles = [] fragfunc = GetFragmentationFunction() mol = oechem.OEGraphMol() oechem.OESmilesToMol(mol, this_smiles) frags = [f for f in fragfunc(mol)] len_frags = len(frags) for smile in old_smiles: mol2 = oechem.OEGraphMol() oechem.OESmilesToMol(mol2, smile) frags += [f for f in fragfunc(mol2)] oechem.OEThrow.Info("%d number of fragments generated" % len(frags)) fragcombs = GetFragmentCombinations(mol, frags, frag_number=len_frags) oechem.OEThrow.Info("%d number of fragment combinations generated" % len(fragcombs)) smiles = set() for frag in fragcombs: if oechem.OEDetermineComponents(frag)[0] == 1: smiles = smiles.union(oechem.OEMolToSmiles(frag)) return smiles
def __iter__(self): molA = oechem.OEGraphMol() molB = oechem.OEGraphMol() oechem.OESmilesToMol(molA, 'c1cccc1 A') oechem.OESmilesToMol(molB, 'c1cccc1 B') act_list = [molA, molB] baitset = (0, [0]) ranking = list() dataset_infos = (0, {"A": 0, "B": 1}) yield (act_list, baitset, ranking, dataset_infos)
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 atomic_charge(smiles): mol = oechem.OEMol() oechem.OESmilesToMol(mol, smiles) for a in mol.GetAtoms(): if a.GetFormalCharge() != 0: return False return True
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 __call__(self, smiles: list): """ Calculate the scores for FRED given a list of SMILES strings. :param smiles: List of SMILES strings. :return: List of dicts i.e. [{'smiles': smi, 'metric': 'value', ...}, ...] """ results = [] for smi in smiles: result = {'smiles': smi} mol = oechem.OEMol() if not oechem.OESmilesToMol(mol, smi): results.append({ 'smiles': smi, f'{self.prefix}_fred_energy': 0.0 }) continue if self.omega(mol): dockedMol = oechem.OEGraphMol() self.oedock.DockMultiConformerMolecule(dockedMol, mol) score = dockedMol.GetEnergy() results.append({ 'smiles': smi, f'{self.prefix}_fred_energy': score }) return results
def test_oemol_nhfcl(): """Test coordinates for NHFCl read in as OEMol.""" import openeye.oechem as oechem import openeye.oeomega as oeomega # coordinates for N, F, H, Cl respectively # generated after minimization with improper phase of 150 degrees coordlist = [ 0.155, -0.088, -0.496, -1.054, -0.776, -0.340, -0.025, 0.906, -0.516, 1.689, -0.635, -1.263 ] # create OEMol mol = oechem.OEMol() oechem.OESmilesToMol(mol, 'FNCl') omega = oeomega.OEOmega() omega.SetMaxConfs(1) omega.SetIncludeInput(False) omega.SetStrictStereo(False) status = omega(mol) oechem.OETriposAtomTypes(mol) oechem.OETriposAtomNames(mol) oechem.OEAddExplicitHydrogens(mol) # set provided coordinates mol.SetCoords(oechem.OEFloatArray(coordlist)) # calculate and check improper angle crds, names = find_improper_angles(mol) ang = calc_improper_angle(crds[0][0], crds[0][1], crds[0][2], crds[0][3]) if abs(ang - 15.0) > 0.1 and abs(ang - 165.0) > 0.1: raise Exception( "Error calculating improper of test OEMol. Calculated {} degrees, but should be 15 or 165 degrees." .format(ang))
def normalize_wbo(smiles, timeout=15): mol = oechem.OEMol() oechem.OESmilesToMol(mol, smiles) pool = Pool(processes=1) result = pool.apply_async(chemi.get_charges, kwds={ 'molecule': mol, 'strict_stereo': False, 'strict_types': False }) try: charged = result.get(timeout=timeout) except: print('process timed out') pool.terminate() return None wbo = 0 bonds = 0 for b in charged.GetBonds(): if 'WibergBondOrder' in b.GetData(): wbo += b.GetData('WibergBondOrder') bonds += 1 else: return None pool.terminate() return (wbo / bonds)
def smiles_to_topology(smiles): """ Convert a SMILES string to an OpenMM Topology Parameters ---------- smiles : str smiles to be made into topology Returns ------- topology : simtk.openmm.topology.app topology of the smiles mol : OEMol OEMol with explicit hydrogens """ from openmoltools.forcefield_generators import generateTopologyFromOEMol mol = oechem.OEMol() oechem.OESmilesToMol(mol, smiles) oechem.OEAddExplicitHydrogens(mol) oechem.OETriposAtomNames(mol) oechem.OETriposBondTypeNames(mol) topology = generateTopologyFromOEMol(mol) return topology, mol
def createOEMolFromSMILES(smiles='CC', title='MOL'): """ Generate an oemol with a geometry """ from openeye import oechem, oeiupac, oeomega # Create molecule mol = oechem.OEMol() oechem.OESmilesToMol(mol, smiles) # Set title. mol.SetTitle(title) # Assign aromaticity and hydrogens. oechem.OEAssignAromaticFlags(mol, oechem.OEAroModelOpenEye) oechem.OEAddExplicitHydrogens(mol) # Create atom names. oechem.OETriposAtomNames(mol) # Assign geometry omega = oeomega.OEOmega() omega.SetMaxConfs(1) omega.SetIncludeInput(False) omega.SetStrictStereo(True) omega(mol) return mol
def test_expand_tautomers(): from openeye import oechem imidazol_smiles = 'CC1=CN=CN1' oemol = oechem.OEMol() oechem.OESmilesToMol(oemol, imidazol_smiles) tautomers = fragmenter.states._enumerate_tautomers(oemol) assert len(tautomers) == 2 for tau in tautomers: assert chemi.get_charge(tau) == 0 salsalate = 'OC(=O)C1=CC=CC=C1OC(=O)C1=CC=CC=C1O' oemol = oechem.OEMol() oechem.OESmilesToMol(oemol, salsalate) tautomers = fragmenter.states._enumerate_tautomers(oemol) assert len(tautomers) == 1 assert chemi.get_charge(tautomers[0]) == -1
def test_tag_fgroups(): from openeye import oechem import itertools smiles = '[H:40][c:3]1[c:8]([c:20]2[n:30][c:12]([c:14]([n:32]2[n:31][c:11]1[H:48])[C:2]#[C:1][c:13]3[c:9]([c:15]([c:4]([c:5]([c:16]3[C:26]' \ '([H:58])([H:59])[H:60])[H:42])[H:41])[C:21](=[O:36])[N:35]([H:66])[c:19]4[c:7]([c:6]([c:17]([c:18]([c:10]4[H:47])[C:29]([F:37])([F:38])' \ '[F:39])[C:28]([H:64])([H:65])[N:34]5[C:24]([C:22]([N:33]([C:23]([C:25]5([H:56])[H:57])([H:52])[H:53])[C:27]([H:61])([H:62])[H:63])([H:50])' \ '[H:51])([H:54])[H:55])[H:43])[H:44])[H:46])[H:49])[H:45]' mol = oechem.OEMol() oechem.OESmilesToMol(mol, smiles) frags = fragmenter.fragment.CombinatorialFragmenter(mol) fgroups = {} fgroups['alkyne_0'] = [1, 2] fgroups['carbonyl_0'] = [21, 36] fgroups['amide_0'] = [35] fgroups['tri_halide_0'] = [29, 37, 38, 39] for group in fgroups: for i in fgroups[group]: a = frags.molecule.GetAtom(oechem.OEHasMapIdx(i)) assert a.GetData('fgroup') == group for group in fgroups: atoms = [ frags.molecule.GetAtom(oechem.OEHasMapIdx(i)) for i in fgroups[group] ] for atom in itertools.combinations(atoms, 2): # Check for bond b = frags.molecule.GetBond(atom[0], atom[1]) if b: assert b.GetData('fgroup') == group
def test_expand_states(smiles, tautomers, stereoisomers, max_stereo_return, filter_nitro, output): from openeye import oechem mol = oechem.OEMol() oechem.OESmilesToMol(mol, smiles) states = fragmenter.states.enumerate_states(mol, tautomers=tautomers, stereoisomers=stereoisomers, max_stereo_returns=max_stereo_return, filter_nitro=filter_nitro, verbose=False) assert len(states) == output
def _from_csv(): df = pd.read_csv(path) df_smiles = df.iloc[:, smiles_col] df_y = df.iloc[:, y_cols] if toolkit == "rdkit": from rdkit import Chem mols = [Chem.MolFromSmiles(smiles) for smiles in df_smiles] gs = [esp.HomogeneousGraph(mol) for mol in mols] elif toolkit == "openeye": from openeye import oechem mols = [ oechem.OESmilesToMol(oechem.OEGraphMol(), smiles) for smiles in df_smiles ] gs = [esp.HomogeneousGraph(mol) for mol in mols] ds = list(zip(gs, list(torch.tensor(df_y.values)))) random.seed(seed) random.shuffle(ds) return ds
def molecule_from_record(record: MoleculeESPRecord) -> Molecule: """Converts an ``openff-recharge`` ESP record to to an Open Force Field molecule.""" oe_molecule = oechem.OEMol() oechem.OESmilesToMol(oe_molecule, record.tagged_smiles) ordered_conformer = reorder_conformer(oe_molecule, record.conformer) # Clear the records index map. for atom in oe_molecule.GetAtoms(): atom.SetMapIdx(0) oe_molecule.DeleteConfs() oe_molecule.NewConf(oechem.OEFloatArray(ordered_conformer.flatten())) with NamedTemporaryFile(suffix=".mol2") as file: # Workaround for stereochemistry being incorrectly perceived. molecule = Molecule.from_openeye(oe_molecule, allow_undefined_stereo=True) molecule.to_file(file.name, "mol2") molecule = molecule.from_file(file.name) return molecule
def from_smiles(smi:str) -> Mol: """ Create a molecule object from a smiles """ mol = oechem.OEGraphMol() oechem.OESmilesToMol(mol, smi) return Mol(mol)
def test_get_bond(): from openeye import oechem smiles = '[H:5][C:1]([H:6])([H:7])[C:3]([H:11])([H:12])[C:4]([H:13])([H:14])[C:2]([H:8])([H:9])[H:10]' mol = oechem.OEMol() oechem.OESmilesToMol(mol, smiles) f = fragmenter.fragment.WBOFragmenter(mol) bond = f.get_bond(bond_tuple=(3, 4)) assert bond.IsRotor()
def test_normalize_molecule(): from openeye import oechem mol = oechem.OEMol() oechem.OESmilesToMol(mol, 'CCCC') assert mol.GetTitle() == '' normalized_mol = chemi.normalize_molecule(mol) assert normalized_mol.GetTitle() == 'butane'
def calculate_molecule_costs( molecules: "iterable of SMILES strings") -> {"SMILES"}: """Calculates the cost associated with each molecule""" costs = {} for smi in molecules: mol = oechem.OEMol() oechem.OESmilesToMol(mol, smi) costs[smi] = mol.NumAtoms() return costs
def test_get_torsion_quartet(input, output): from openeye import oechem mol = oechem.OEMol() oechem.OESmilesToMol(mol, input) f = fragmenter.fragment.PfizerFragmenter(mol) atoms, bonds = f._get_torsion_quartet((3, 5)) # This also includes explicit hydrogen assert len(atoms) == output[0] assert len(bonds) == output[1]
def test_expand_stereoisomers(smiles, forceflip, enum_n, output): from openeye import oechem oemol = oechem.OEMol() oechem.OESmilesToMol(oemol, smiles) stereo = fragmenter.states._enumerate_stereoisomers(oemol, force_flip=forceflip, enum_nitrogen=enum_n, verbose=False) assert len(stereo) == output
def triple(smiles): mol = oechem.OEMol() oechem.OESmilesToMol(mol, smiles) for b in mol.GetBonds(): if b.IsInRing(): continue if b.GetOrder() > 2: return True return False
def test_compare_wbo(): from openeye import oechem smiles = '[H:5][C:1]([H:6])([H:7])[C:3]([H:11])([H:12])[C:4]([H:13])([H:14])[C:2]([H:8])([H:9])[H:10]' mol = oechem.OEMol() oechem.OESmilesToMol(mol, smiles) f = fragmenter.fragment.WBOFragmenter(mol) f.calculate_wbo() f._get_rotor_wbo() assert f._compare_wbo(fragment=mol, bond_tuple=(3, 4)) == 0.0
def circular_wrapper(smi, num_bits=1024, min_radius=2, max_radius=2): mol = oechem.OEGraphMol() oechem.OESmilesToMol(mol, smi) fp = oegraphsim.OEFingerPrint() oegraphsim.OEMakeCircularFP(fp, mol, num_bits, min_radius, max_radius, oegraphsim.OEFPAtomType_DefaultPathAtom, oegraphsim.OEFPBondType_DefaultPathBond) return get_bit_string(fp)
def test_openeye_canonical(smiles_input, oe_can_expected): """Testing rdkit canonical smiles""" for i, o in zip(smiles_input, oe_can_expected): oe_mol = oechem.OEMol() oechem.OESmilesToMol(oe_mol, i) assert cmiles.utils.mol_to_smiles(oe_mol, isomeric=False, mapped=False, explicit_hydrogen=False) == o
def test_openeye_explicit_h(smiles_input, oe_h_expected): """Testing openeye explicit hydrogen""" for i, o in zip(smiles_input, oe_h_expected): oe_mol = oechem.OEMol() oechem.OESmilesToMol(oe_mol, i) assert cmiles.utils.mol_to_smiles(oe_mol, isomeric=True, mapped=False, explicit_hydrogen=True) == o
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 maccs_wrapper(smi, ): mol = oechem.OEGraphMol() oechem.OESmilesToMol(mol, smi) fp = oegraphsim.OEFingerPrint() oegraphsim.OEMakeMACCS166FP( fp, mol, ) return get_bit_string(fp)
def n_heavy_atoms(smiles): """ """ mol = oechem.OEMol() oechem.OESmilesToMol(mol, smiles) n = 0 for a in mol.GetAtoms(): if not a.IsHydrogen(): n += 1 return n
def setup_smi(self, smiles: str): """ Load SMILES string into OE mol object :param smiles: SMILES string :return: """ self.fitmol = oechem.OEMol() oechem.OESmilesToMol(self.fitmol, smiles) return self