def generate_conformers(lig_file, init='generate_conformers_init'): # option pdb_mol """Performs the following tasks: > Creates the receptor folders within binding_ligands and decoy_ligands > Converts all PDB crystal ligands into mol for future use > Generates conformers and saves them to crystal_ligands folder""" init = eval(init) lig_name = lig_file[len(init.lig_path)+1: ] # use rdkit to get a mol object from the PDB pdb_file = os.path.join(init.out_lig_path, lig_name) mol_file = os.path.join(init.mol_path, lig_name).replace('.pdb', '.sdf') # write the mol to a mol file for future use mol = Chem.MolFromPDBFile(lig_file) writer = SDWriter(mol_file) writer.write(mol) # generate conformers and get the number of atoms of the molecule mol2 = Chem.AddHs(mol) # addHs pdb_writer = PDBWriter(pdb_file) conf_ids = AllChem.EmbedMultipleConfs(mol2, init.num_conformers) # PDB has hydrogens for cid in conf_ids: AllChem.MMFFOptimizeMolecule(mol2, confId=cid) mol = Chem.RemoveHs(mol2) pdb_writer.write(mol) num_atoms = Mol.GetNumAtoms(mol) pdb_writer.close() # also has hydrogens print 'Generated conformers for one ligand' return [[pdb_file, mol_file, num_atoms]]
def convert_conf_to_sdf_i(_i): f = file_pattern.format(_i) lowest_e = np.inf selected_mol = None suppl = rdkit.Chem.SDMolSupplier(f, removeHs=False) for mol in suppl: prop_dict = mol.GetPropsAsDict() if lowest_e > prop_dict["energy_abs"]: selected_mol = mol w = SDWriter(dst_folder + "/{}.mmff.sdf".format(osp.basename(f).split("_")[0])) w.write(selected_mol)
def spectrophores(mols, accuracy=20, resolution=3.0): from copy import deepcopy import os import subprocess import tempfile import numpy as np from rdkit.Chem.rdmolfiles import SDWriter assert accuracy in (1, 2, 5, 10, 15, 20, 30, 36, 45, 60) assert isinstance(resolution, float) assert resolution > 0. _mols = [] names = np.array([str(i) for i in range(len(mols))]) for m, name in zip(mols, names): if m.GetNumConformers() == 1: _mols.append(deepcopy(m)) _mols[-1].SetProp("_Name", name) elif m.GetNumConformers() == 0: _mols.append(None) else: raise ValueError("every molecule must have at most 1 conformer") mols = np.array(_mols, dtype=np.object) del _mols temp_dir = tempfile.mkdtemp() fname = os.path.join(temp_dir, "mols.sdf") writer = SDWriter(fname) mask_nan = np.array([m is None for m in mols], dtype=np.bool) [writer.write(m, confId=0) for m in mols[np.logical_not(mask_nan)]] result = subprocess.run(["obspectrophore", "-i", fname, "-a", str(accuracy), "-r", str(resolution)], stdout=subprocess.PIPE).stdout.decode("utf-8").split('\n') assert not any(['\t' in line for line in result[:11]]) assert not '\t' in result[:-1] arr = np.zeros((len(mols), 48), dtype=np.float32) out_names = [] for i, line in enumerate(result[11:-1]): row = line.split('\t') assert len(row) == 50 out_names.append(row[0]) assert row[-1] == "" arr[i,:] = np.array([float(s) for s in row[1:-1]]) arr[mask_nan] = np.nan assert all([a==b for a, b in zip(names[np.logical_not(mask_nan)], out_names)]) os.remove(fname) return arr, np.array(["{:02d}".format(i+1) for i in range(48)], dtype=np.str)
def write_frame(host_coords, host_mol, guest_coords, guest_mol, guest_name, outdir, step, stage): if not os.path.exists(os.path.join(outdir, guest_name)): os.mkdir(os.path.join(outdir, guest_name)) host_frame = host_mol.GetConformer() for i in range(host_mol.GetNumAtoms()): x, y, z = host_coords[i] host_frame.SetAtomPosition(i, Point3D(x, y, z)) conf_id = host_mol.AddConformer(host_frame) writer = PDBWriter( os.path.join( outdir, guest_name, f"{guest_name}_{stage}_{step}_host.pdb", )) writer.write(host_mol, conf_id) writer.close() host_mol.RemoveConformer(conf_id) guest_frame = guest_mol.GetConformer() for i in range(guest_mol.GetNumAtoms()): x, y, z = guest_coords[i] guest_frame.SetAtomPosition(i, Point3D(x, y, z)) conf_id = guest_mol.AddConformer(guest_frame) guest_mol.SetProp("_Name", f"{guest_name}_{stage}_{step}_guest") writer = SDWriter( os.path.join( outdir, guest_name, f"{guest_name}_{stage}_{step}_guest.sdf", )) writer.write(guest_mol, conf_id) writer.close() guest_mol.RemoveConformer(conf_id)
def write_sdf_file(self, c_id="%", output_file=""): list_compounds = self.get_list_by(c_id=c_id) writer = SDWriter(output_file) for compound in list_compounds: try: mol = chemical.read_string("mol", compound.mol) mol.SetProp("_Name", compound.name) mol.SetProp("_Compound_id", compound.c_id) mol.SetProp("_Scaffold", compound.scaffold if compound.scaffold else "") mol.SetProp("_SMILES", compound.smiles) mol.SetProp("_INCHI", compound.inchi) mol.SetProp("_INCHIKEY", compound.inchikey) mol.SetProp("_Molecular_weight", str(compound.molecular_weight)) mol.SetProp("_Molecular_formula", compound.molecular_formula) writer.write(mol) except: print("c_id: {} has an error!".format(compound.c_id))
def pdb2mol(uid, lig_file, init='pdb2mol_init'): """ Convert .pdb file to .mol file :param lig_file: string (relative path to the ligand file) :param init: string (init function) :return: """ init = eval(init) mol = Chem.MolFromPDBFile(os.path.join(init.db_root, lig_file)) mol_writer = SDWriter(os.path.join(init.molfile_path, uid + ".mol")) mol_writer.write(mol) mol_writer.close() return [[uid, os.path.join(init.molfile_dir, uid + ".mol")]]
from rdkit.Chem.rdchem import Mol from rdkit.Chem.rdmolfiles import SDWriter repo = COCONUT() # print(repo.count()) i = 0 converted, not_converted = 0, 0 converted_list_file = open("out/inchi_valid_check/converted.txt", mode="w", encoding="utf-8") converted_not_match_file = open( "out/inchi_valid_check/converted_not_match.txt", mode="w", encoding="utf-8") w = SDWriter("out/inchi_valid_check/converted.sdf") np: Unique_NP for np in repo.get_unique_stream(): mol = Mol() try: mol = Chem.MolFromInchi(inchi=np.inchi, treatWarningAsError=True) mol.SetProp("coconut_id", np.coconut_id) except: not_converted += 1 if mol: mol_inchikey = inchi.MolToInchiKey(mol) if np.inchikey == mol_inchikey: converted_list_file.write(np.inchi + "\n") w.write(mol)