def check_esp(conf_dict, qm_dir=".", logfile="check_esp.log"): check_log = logger(logfile) check_log.log("# Logfile QM ESP check") for frag_i, conf_N in conf_dict.items(): check_log.log("Checking frag%s..." % frag_i) mainpath = qm_dir + "/" + "frag%d" % frag_i if not os.path.exists(mainpath): check_log.log(" ! Warning ! %s not found." % mainpath) continue for conf_i in range(conf_N): check_log.log("Checking frag%d-conf%d..." % (frag_i, conf_i)) fragname = 'frag%d-conf%d' % (frag_i, conf_i) fragpath = mainpath + '/' + 'conf%d' % conf_i logpath = fragpath + "/" + fragname + "_esp.log" if not os.path.exists(fragpath): check_log.log(" ! Warning ! %s not found." % fragpath) continue if not os.path.exists(logpath): check_log.log(" ! Warning ! %s not found" % logpath) continue esp_vals_list = list() esp_crds_list = list() if get_esp(logpath, esp_vals_list, esp_crds_list): n_crds = len(esp_crds_list) n_vals = len(esp_vals_list) if n_crds == n_vals: check_log.log(" Found %d esp fit centers." % n_vals) else: check_log.log( "! Warning ! Found %d fit centers but only %d fit values." % (n_crds, n_vals)) if is_normal_terminate(logpath): check_log.log(" Normal termination of Gaussian.") else: check_log.log( " ! Warning ! An error occured. Check Gaussian log.") check_log.close()
def datadump(database, dumpdir): db = pickle.load(open(database, "rb")) if os.path.exists(dumpdir): raise Warning( "Caution, %s already exists. Already existing data may be overwritten." ) else: os.mkdir(dumpdir) os.mkdir(dumpdir + "/png") frag2mol = db.get_frag2mol() frag2lcapconn = db.get_frag2lcapconn() frag2rcapconn = db.get_frag2rcapconn() mol2frag = db.get_mol2frag() mol2conn = db.get_mol2conn() frag_log = logger(dumpdir + "/frag.dat") frag_log.log("### datadump of database %s" % database) frag_log.log("### timestamp %s" % time.asctime(time.localtime(time.time()))) frag_log.log("### written by run_fragresp.py datadump routine.") frag_log.log("###") frag_log.log("### ----------------- ###") frag_log.log("### FRAGMENT DATA LOG ###") frag_log.log("### ----------------- ###") frag_log.log("###") frag_log.log( "# id smiles mol_id lcap_id rcap_id Natoms Nbonds Nnonhatoms Chg Nhbd Nhba Nrotbonds Nrings" ) for frag_i in range(db.get_frag_count()): frag = db.get_frag(frag_i) Chem.SanitizeMol(frag) log_str = list() ### id log_str.append(str(frag_i) + " ") ### smiles log_str.append(str(Chem.MolToSmiles(frag, isomericSmiles=True)) + " ") ### mol_id mol_count = len(frag2mol[frag_i]) if mol_count == 0: log_str.append("-1 ") else: for i in range(mol_count): mol_i = frag2mol[frag_i][i] if i < mol_count - 1: log_str.append(str(mol_i) + ",") else: log_str.append(str(mol_i) + " ") ### lcap_id lcap_count = len(frag2lcapconn[frag_i]) if lcap_count == 0: log_str.append("-1 ") else: for i in range(lcap_count): cap_i = frag2lcapconn[frag_i][i] if i < lcap_count - 1: log_str.append(str(cap_i) + ",") else: log_str.append(str(cap_i) + " ") ### rcap_id rcap_count = len(frag2rcapconn[frag_i]) if rcap_count == 0: log_str.append("-1 ") else: for i in range(rcap_count): cap_i = frag2rcapconn[frag_i][i] if i < rcap_count - 1: log_str.append(str(cap_i) + ",") else: log_str.append(str(cap_i) + " ") ### N_atoms log_str.append(str(frag.GetNumAtoms()) + " ") ### N_bonds log_str.append(str(frag.GetNumBonds()) + " ") ### Nnonhatoms log_str.append(str(frag.GetNumHeavyAtoms()) + " ") ### Chg log_str.append(str(rdmolops.GetFormalCharge(frag)) + " ") ### Nhbd log_str.append(str(rdMolDescriptors.CalcNumHBD(frag)) + " ") ### Nhba log_str.append(str(rdMolDescriptors.CalcNumHBA(frag)) + " ") ### Nrotbonds log_str.append(str(rdMolDescriptors.CalcNumRotatableBonds(frag)) + " ") ### Nrings log_str.append(str(rdMolDescriptors.CalcNumRings(frag)) + " ") frag_log.log("".join(log_str)) png_path = dumpdir + "/png/" + "frag_%d.png" % frag_i try: Chem.SanitizeMol(frag) AllChem.Compute2DCoords(frag) Draw.MolToFile(frag, png_path, size=(500, 500)) except: #Chem.Kekulize(frag) print("Could not save frag %d to disk." % frag_i) frag_log.close() mol_log = logger(dumpdir + "/mol.dat") mol_log.log("### datadump of database %s" % database) mol_log.log("### timestamp %s" % time.asctime(time.localtime(time.time()))) mol_log.log("### written by run_fragresp.py datadump routine.") mol_log.log("###") mol_log.log("### ----------------- ###") mol_log.log("### MOLECULE DATA LOG ###") mol_log.log("### ----------------- ###") mol_log.log("###") mol_log.log( "# id name smiles frag_id Natoms Nbonds Nnonhatoms Chg Nhbd Nhba Nrotbonds Nrings" ) for mol_i in range(db.get_mol_count()): mol = db.get_mol(mol_i) Chem.SanitizeMol(mol) name = db.get_name(mol_i) decomp = db.get_decompose(mol_i) log_str = list() log_str.append(str(mol_i) + " ") log_str.append(name + " ") log_str.append(str(Chem.MolToSmiles(mol, isomericSmiles=True)) + " ") frag_count = decomp.get_frag_count() if frag_count == 0: log_str.append("-1 ") else: for i in range(frag_count): frag_i = mol2frag[mol_i][i] if i < frag_count - 1: log_str.append(str(frag_i) + ",") else: log_str.append(str(frag_i) + " ") log_str.append(str(mol.GetNumAtoms()) + " ") log_str.append(str(mol.GetNumBonds()) + " ") log_str.append(str(mol.GetNumHeavyAtoms()) + " ") log_str.append(str(rdmolops.GetFormalCharge(mol)) + " ") log_str.append(str(rdMolDescriptors.CalcNumHBD(mol)) + " ") log_str.append(str(rdMolDescriptors.CalcNumHBA(mol)) + " ") log_str.append(str(rdMolDescriptors.CalcNumRotatableBonds(mol)) + " ") log_str.append(str(rdMolDescriptors.CalcNumRings(mol)) + " ") mol_log.log("".join(log_str)) png_path = dumpdir + "/png/" + "mol_%d.png" % mol_i AllChem.Compute2DCoords(mol) Chem.Kekulize(mol) Draw.MolToFile(mol, png_path, size=(500, 500)) mol_log.close() surr_log = logger(dumpdir + "/surr.dat") surr_log.log("### datadump of database %s" % database) surr_log.log("### timestamp %s" % time.asctime(time.localtime(time.time()))) surr_log.log("### written by run_fragresp.py datadump routine.") surr_log.log("###") surr_log.log("### ----------------- ###") surr_log.log("### SURROGATE DATA LOG ###") surr_log.log("### ------------------ ###") surr_log.log("###") surr_log.log( "# id name smiles mol_id Natoms Nbonds Nnonhatoms Chg Nhbd Nhba Nrotbonds Nrings" ) for conn_i, conn in enumerate(db.get_conn_list()): if conn.get_terminal(): continue name = conn.get_name() conn_cap = conn.get_surrogate_cap() Chem.SanitizeMol(conn_cap) log_str = list() log_str.append(str(conn_i) + " ") log_str.append(name + " ") log_str.append( str(Chem.MolToSmiles(conn_cap, isomericSmiles=True)) + " ") conn2mol = db.get_conn2mol()[conn_i] mol_count = len(conn2mol) if mol_count == 0: log_str.append("-1 ") else: for i in range(mol_count): mol_i = conn2mol[i] if i < mol_count - 1: log_str.append(str(mol_i) + ",") else: log_str.append(str(mol_i) + " ") log_str.append(str(conn_cap.GetNumAtoms()) + " ") log_str.append(str(conn_cap.GetNumBonds()) + " ") log_str.append(str(conn_cap.GetNumHeavyAtoms()) + " ") log_str.append(str(rdmolops.GetFormalCharge(conn_cap)) + " ") log_str.append(str(rdMolDescriptors.CalcNumHBD(conn_cap)) + " ") log_str.append(str(rdMolDescriptors.CalcNumHBA(conn_cap)) + " ") log_str.append( str(rdMolDescriptors.CalcNumRotatableBonds(conn_cap)) + " ") log_str.append(str(rdMolDescriptors.CalcNumRings(conn_cap)) + " ") surr_log.log("".join(log_str)) png_path = dumpdir + "/png/" + "surr_%s.png" % (conn_i) AllChem.Compute2DCoords(conn_cap) Chem.Kekulize(conn_cap) Draw.MolToFile(conn_cap, png_path, size=(500, 500)) surr_log.close()
def prep_qm(frag_list, overwrite, limit, percentage, write_mol2=True, nproc=2, mem=1900, queue='marc2', qm_dir=".", opt_batch="submit_opt_g09.sh", esp_batch="submit_esp_g09.sh", logfile="prep_qm.log", include_list=None): queue_choices = ['marc2', 'slurm', 'condor', 'none'] if queue not in queue_choices: print("queue %s not known. Using queue 'none'.") queue = 'none' conf_list = list() if include_list == None: include_list = range(len(frag_list)) prep_log = logger(logfile) prep_log.log("### OpenEye conformer pipeline") if not os.path.exists(qm_dir): os.mkdir(qm_dir) if queue == 'marc2': g09_cmd = "subg09 -p %d -m %d -cc" % (nproc, mem) elif queue == 'slurm': g09_cmd = 'subg09_slurm.sh %d' % nproc elif queue == 'condor': g09_cmd = 'subg09_condor.sh 1' else: g09_cmd = 'g09' opt_batch_file = logger(qm_dir + "/" + opt_batch) esp_batch_file = logger(qm_dir + "/" + esp_batch) opt_batch_file.log("#!/bin/bash") opt_batch_file.log() esp_batch_file.log("#!/bin/bash") esp_batch_file.log() for frag_i in include_list: frag = frag_list[frag_i] mainpath = qm_dir + "/" + "frag%d" % frag_i if not os.path.exists(mainpath): os.mkdir(mainpath) elif not overwrite: conf_list.append(-1) continue ### Always isomericSmiles=True, otherwise omega does not ### know what to do. smi = Chem.MolToSmiles(frag, isomericSmiles=True) prep_log.log("Fragment %d" % frag_i) prep_log.log("Smi %s" % smi) mol = OEGraphMol() OESmilesToMol(mol, smi) mol = OEMol(mol) generate_conformers(mol) prep_log.log("initial conformers: %d" % mol.NumConfs()) filter_conformers(mol, limit, percentage) prep_log.log("filtered conformers:%d" % mol.NumConfs()) conf_list.append(mol.NumConfs()) for conf_i, conf in enumerate(mol.GetConfs()): fragname = 'frag%d-conf%d' % (frag_i, conf_i) fragpath = mainpath + '/' + 'conf%d' % conf_i subpath = "frag%d" % frag_i + '/' + 'conf%d' % conf_i if not os.path.exists(fragpath): os.mkdir(fragpath) com_opt, com_esp = make_g09(fragname, conf, nproc, mem, queue) f = logger(fragpath + "/" + fragname + "_opt.com") f.log(com_opt) f.close() opt_batch_file.log( "%s %s" % (g09_cmd, subpath + "/" + fragname + "_opt.com")) del f f = logger(fragpath + "/" + fragname + "_esp.com") f.log(com_esp) f.close() esp_batch_file.log( "%s %s" % (g09_cmd, subpath + "/" + fragname + "_esp.com")) del f if write_mol2: ofs = oemolostream() ofs.open(mainpath + "/" + "f%d.mol2" % frag_i) OEWriteMolecule(ofs, mol) del ofs prep_log.log() prep_log.close() opt_batch_file.close() esp_batch_file.close() del opt_batch_file del esp_batch_file return conf_list
def check_opt(conf_dict, mol_list, qm_dir=".", logfile="check_qm.log", stdout=None, stderr=None): if stdout == None: stdout = open(os.devnull, 'w') if stderr == None: stderr = open(os.devnull, 'w') check_log = logger(logfile) check_log.log("# Logfile QM check") for frag_i, conf_N in conf_dict.items(): check_log.log("Checking frag%s..." % frag_i) mainpath = qm_dir + "/" + "frag%d" % frag_i ante_args_general = [ "-fi", "gout", "-pf", "y", "-at", "sybyl", "-dr", "no", ] if not os.path.exists(mainpath): check_log.log(" ! Warning ! %s not found." % mainpath) continue for conf_i in range(conf_N): check_log.log("Checking frag%d-conf%d..." % (frag_i, conf_i)) fragname = 'frag%d-conf%d' % (frag_i, conf_i) fragpath = mainpath + '/' + 'conf%d' % conf_i logpath = fragpath + "/" + fragname + "_opt.log" if not os.path.exists(fragpath): check_log.log(" ! Warning ! %s not found." % fragpath) continue if not os.path.exists(logpath): check_log.log(" ! Warning ! %s not found" % logpath) continue freq_list = list() if get_freq(logpath, freq_list): freq_str = 'Frequencies [cm**-1]: ' freq_ok = True for freq_i, freq in enumerate(freq_list): if freq < 0.: check_log.log( " ! Warning ! Imaginary frequency %f cm**-1 for mode %d" % (freq, freq_i)) freq_ok = False freq_str += str(freq) freq_str += ' ' if freq_ok: check_log.log(" Frequencies OK!") #check_log.log(freq_str) else: check_log.log(" ! Warning ! No frequencies found in %s" % logpath) del freq_list if is_normal_terminate(logpath): check_log.log(" Normal termination of Gaussian.") else: check_log.log( " ! Warning ! An error occured. Check Gaussian log.") mol2path = fragpath + "/" + fragname + "_opt.mol2" ante_args = [ ante_exe, "-i", logpath, "-o", mol2path, "-fo", "mol2" ] + ante_args_general call(ante_args, stdout=stdout, stderr=stderr) mol_opt = Chem.MolFromMol2File(mol2path, removeHs=False) matches = mol_opt.GetSubstructMatches(mol_list[frag_i]) if len(matches) > 1: check_log.log( " ! Warning ! Found more than one substructure match.") if len(matches) == 0: check_log.log( " ! Warning ! Did not find any substructure match.") check_log.close()
def prep_qm(frag_list, overwrite, limit, percentage, write_mol2=True, nproc=2, mem=1900, queue='marc2', qm_dir=".", opt_batch="submit_opt_g09.sh", esp_batch="submit_esp_g09.sh", logfile="prep_qm.log", include_list=None): conf_list = list() if include_list == None: include_list = range(len(frag_list)) prep_log = logger(logfile) prep_log.log("### Openbabel conformer pipeline") if not os.path.exists(qm_dir): os.mkdir(qm_dir) if queue == 'marc2': g09_cmd = "subg09 -p %d -m %d -cc" % (nproc, mem) elif queue == 'slurm': g09_cmd = 'subg09_slurm.sh %d' % nproc elif queue == 'condor': g09_cmd = 'subg09_condor.sh 1' else: g09_cmd = 'g09' opt_batch_file = logger(qm_dir + "/" + opt_batch) esp_batch_file = logger(qm_dir + "/" + esp_batch) opt_batch_file.log("#!/bin/bash") opt_batch_file.log() opt_batch_file.log("_pwd=$PWD") opt_batch_file.log() esp_batch_file.log("#!/bin/bash") esp_batch_file.log() esp_batch_file.log("_pwd=$PWD") esp_batch_file.log() obConversion = ob.OBConversion() obConversion.SetInFormat("smi") obConversion.SetOutFormat("mol2") for frag_i in include_list: frag = frag_list[frag_i] mainpath = qm_dir + "/" + "frag%d" % frag_i if not os.path.exists(mainpath): os.mkdir(mainpath) elif not overwrite: conf_list.append(-1) continue ### Always isomericSmiles=True, otherwise omega does not ### know what to do. smi = Chem.MolToSmiles(frag, isomericSmiles=True) prep_log.log("Fragment %d" % frag_i) prep_log.log("Smi %s" % smi) pbmol = pb.readstring("smi", smi) pbmol.make3D() mol = pbmol.OBMol generate_conformers(mol) prep_log.log("initial conformers: %d" % mol.NumConformers()) filter_conformers(mol, limit, percentage) prep_log.log("filtered conformers:%d" % mol.NumConformers()) conf_list.append(mol.NumConformers()) for conf_i in range(mol.NumConformers()): mol.SetConformer(conf_i) fragname = 'frag%d-conf%d' % (frag_i, conf_i) fragpath = mainpath + '/' + 'conf%d' % conf_i subpath = "frag%d" % frag_i + '/' + 'conf%d' % conf_i if not os.path.exists(fragpath): os.mkdir(fragpath) com_opt, com_esp = make_g09(fragname, mol, nproc, mem, queue) f = logger(fragpath + "/" + fragname + "_opt.com") f.log(com_opt) f.close() opt_batch_file.log("cd %s" % subpath) opt_batch_file.log("%s %s" % (g09_cmd, fragname + "_opt.com")) opt_batch_file.log("cd $_pwd") del f f = logger(fragpath + "/" + fragname + "_esp.com") f.log(com_esp) f.close() esp_batch_file.log("cd %s" % subpath) esp_batch_file.log("%s %s" % (g09_cmd, fragname + "_esp.com")) esp_batch_file.log("cd $_pwd") del f if write_mol2: obConversion.WriteFile(mol, mainpath + "/" + "f%d.mol2" % frag_i) prep_log.log() prep_log.close() opt_batch_file.close() esp_batch_file.close() del opt_batch_file del esp_batch_file return conf_list