|
| 1 | +# Post processing script for sample and dock generated molecules |
| 2 | + |
| 3 | +import pandas as pd |
| 4 | +from rdkit import Chem |
| 5 | +from rdkit.Chem import AllChem, Draw |
| 6 | +import os |
| 7 | +from multiprocessing import Pool |
| 8 | +from itertools import repeat |
| 9 | + |
| 10 | +from rdkit.Chem.PropertyMol import PropertyMol # Allow pickle on mol props for multiprocessing |
| 11 | +from rdkit.Chem import RDConfig # Allow Contrib packages to be used |
| 12 | +from rdkit.Chem.Crippen import MolLogP as LogP # Lipophilicity |
| 13 | +from rdkit.Chem.QED import default as QED # Quantitiative Estimate of Drug-likeness |
| 14 | +from rdkit.Chem.Descriptors import MolWt # Mol Weight |
| 15 | +import sys |
| 16 | +sys.path.append(os.path.join(RDConfig.RDContribDir, 'SA_Score')) |
| 17 | +# add path for rdkit Contrib packages |
| 18 | +from sascorer import calculateScore as SAS # Sythetic Accessiblilty Score |
| 19 | + |
| 20 | +# Function for calculate mol properties for sd files in each folder for multiprocessing |
| 21 | +def process_by_folder(fd, inpath): |
| 22 | + cycle = fd.strip("cycle_") |
| 23 | + sd = inpath+'/'+fd+'/ranked_designs.sd' |
| 24 | + if os.path.exists(sd): |
| 25 | + cir_mols = [PropertyMol(m) for m in Chem.SDMolSupplier(sd)] |
| 26 | + for i,m in enumerate(cir_mols): |
| 27 | + # Calculate properties for each mol |
| 28 | + m.SetProp('Cycle',cycle) |
| 29 | + m.SetProp('MolWeight', str(MolWt(m))) |
| 30 | + m.SetProp('LogP', str(LogP(m))) |
| 31 | + m.SetProp('QED', str(QED(m))) |
| 32 | + m.SetProp('SAS', str(SAS(m))) |
| 33 | + if i == 0: |
| 34 | + # Select the highest score design in the cycle |
| 35 | + best_mol = m |
| 36 | + return cir_mols, best_mol |
| 37 | + |
| 38 | +# calculated mol properties from each cycle and combine mols in one sdf file |
| 39 | +def combine_designs(inpath, outpath): |
| 40 | + # list the folders in the directory for all cycles |
| 41 | + folders = [x for x in os.listdir(inpath) if x.startswith('cycle_')] |
| 42 | + # sort folder name |
| 43 | + folders.sort(key=lambda x: int(x.strip('cycle_'))) |
| 44 | + |
| 45 | + if len(folders) == 0: |
| 46 | + raise Exception('No "cycle_" folder found!') |
| 47 | + |
| 48 | + # Multiprocessing |
| 49 | + with Pool(processes = os.cpu_count()-1) as pool: |
| 50 | + results = pool.starmap(process_by_folder, zip(folders, repeat(inpath))) |
| 51 | + |
| 52 | + # Retrieve results |
| 53 | + mol_lists, best_mols = zip(*results) |
| 54 | + # Create the list of all mols |
| 55 | + all_mols = [] |
| 56 | + for l in mol_lists: |
| 57 | + all_mols.extend(l) |
| 58 | + # Convert tuple to list |
| 59 | + best_mols = list(best_mols) |
| 60 | + |
| 61 | + print(len(all_mols), "total molecules combined from", len(folders),"cycles in\n", inpath) |
| 62 | + print(len(best_mols), "best designs extracted.\n") |
| 63 | + sys.stdout.flush() |
| 64 | + |
| 65 | + # Save as sdf |
| 66 | + with open(outpath+'/All_Designs.sdf','w') as outfile: |
| 67 | + w = Chem.SDWriter(outfile) |
| 68 | + for m in all_mols: |
| 69 | + w.write(m) |
| 70 | + w.close() |
| 71 | + |
| 72 | + with open(outpath+'/Best_Designs.sdf','w') as outfile: |
| 73 | + w = Chem.SDWriter(outfile) |
| 74 | + for m in best_mols: |
| 75 | + w.write(m) |
| 76 | + w.close() |
| 77 | + print('Mols saved!') |
| 78 | + sys.stdout.flush() |
| 79 | + |
| 80 | + return all_mols, best_mols |
| 81 | + |
| 82 | +# Create dataframe with all the properties |
| 83 | +def create_df(mol_list): |
| 84 | + df = pd.DataFrame() |
| 85 | + |
| 86 | + df['Design'] = [m.GetProp('Name') for m in mol_list] |
| 87 | + df['Cycle'] = [int(m.GetProp('Cycle')) for m in mol_list] |
| 88 | + df['Score'] = [float(m.GetProp('SCORE.INTER')) for m in mol_list] |
| 89 | + df['SMILES'] = [m.GetProp('SMILES') for m in mol_list] |
| 90 | + df['Mol'] = [m for m in mol_list] |
| 91 | + df['LogP'] = [float(m.GetProp('LogP')) for m in mol_list] |
| 92 | + df['QED'] = [float(m.GetProp('QED')) for m in mol_list] |
| 93 | + df['MolWt'] = [float(m.GetProp('MolWeight')) for m in mol_list] |
| 94 | + df['SAS'] = [float(m.GetProp('SAS')) for m in mol_list] |
| 95 | + |
| 96 | + return df |
| 97 | + |
| 98 | +def mkdf(all_mols, best_mols, outpath): |
| 99 | + # Create dataframe from the lists |
| 100 | + allscores = create_df(all_mols) |
| 101 | + minscores = create_df(best_mols) |
| 102 | + |
| 103 | + # sort the dataframe based on docking scores |
| 104 | + sortedscores = minscores.sort_values('Score') |
| 105 | + # Drop dulicated entries |
| 106 | + sortedscores.drop_duplicates('SMILES', inplace = True, keep = 'first') |
| 107 | + |
| 108 | + # Save as csv |
| 109 | + allscores.drop(columns=['Mol']).to_csv(outpath+'/allscores.csv', index = False) |
| 110 | + sortedscores.drop(columns=['Mol']).to_csv(outpath+'/sortedscores.csv', index = False) |
| 111 | + print('Dataframes saved!') |
| 112 | + sys.stdout.flush() |
| 113 | + return allscores, minscores |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + import argparse |
| 117 | + parser = argparse.ArgumentParser(description="combine and the ranked_designs.sd in each "+ |
| 118 | + "'cycle_*' folder from Sample and Dock and calculate MolWeight, SAS, LogP, and QED.") |
| 119 | + parser.add_argument("-i","--input", help="input directory that contain folder by cycles") |
| 120 | + parser.add_argument("-o","--outpath", help="output directory for the combined sdf file,"+\ |
| 121 | + "default to ./processed_data") |
| 122 | + a = parser.parse_args() |
| 123 | + inpath = os.path.abspath(a.input) |
| 124 | + |
| 125 | + if a.outpath: |
| 126 | + outpath = os.path.abspath(a.outpath) |
| 127 | + else: outpath = inpath+"/All_Designs_Processed/" |
| 128 | + |
| 129 | + if not os.path.exists(outpath): |
| 130 | + os.makedirs(outpath) |
| 131 | + print("Directory Made:") |
| 132 | + print(outpath) |
| 133 | + sys.stdout.flush() |
| 134 | + allmols, bestmols = combine_designs(inpath, outpath) |
| 135 | + mkdf(allmols, bestmols, outpath) |
0 commit comments