Skip to content

Commit 5f05a07

Browse files
authored
Add demo run scripts
1 parent 89234fd commit 5f05a07

3 files changed

Lines changed: 135 additions & 0 deletions

File tree

batch_demo.ipynb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"cells":[{"cell_type":"markdown","id":"c414b951-1124-47b9-8413-bf86e83037fc","metadata":{},"source":"## Instructions to setup a demo run"},{"cell_type":"markdown","id":"4ced3393-884f-4cfd-a41e-10847474a044","metadata":{},"source":"1. Prepare an input file similar to csv files given in ./data/demo/\n2. Define the path to the input file in below cell\n3. Define the parameter for prediction\n4. Run the cell to get output file\n5. The last five columns of the output csv file saved will have the prediction results"},{"cell_type":"code","execution_count":2,"id":"3c1841bb-8c35-4562-b86f-30777bb299bb","metadata":{},"outputs":[],"source":"parameter = 'kcat' # allowed values: [\"kcat\", \"Km\", \"Ki\"] \nparameter = parameter.lower()\n\nuse_cpu = 1 # set to 0 if you have GPU enabled\n\ninput_file_path = './data/demo/batch_kcat.csv'"},{"cell_type":"markdown","id":"db40ad26-0481-405b-8323-70d23f0ef44e","metadata":{},"source":"## Navigate to below cell and click \"Run->Run Selected Cell\" to get prediction"},{"cell_type":"markdown","id":"81de60cf-39b5-4546-83da-497c15e01b59","metadata":{},"source":"The result will be printed on the right column"},{"cell_type":"code","execution_count":21,"id":"be6cfa1f-9724-44ad-8ff9-ba8dcd51b887","metadata":{"jupyter":{"source_hidden":false}},"outputs":[{"name":"stdout","output_type":"stream","text":["Predicting.. This will take a while..\n","\n","Output saved to ./data/demo/batch_kcat_input_output.csv\n"]}],"source":"import time\nimport os\nimport pandas as pd\nimport numpy as np\nfrom IPython.display import Image, display\nfrom rdkit import Chem\nfrom IPython.display import display, Latex, Math\n\ndef create_csv_sh(parameter, input_file_path):\n df = pd.read_csv(input_file_path)\n smiles_list = df.SMILES\n seq_list = df.sequence\n smiles_list_new = []\n i=0\n for smi in smiles_list:\n try:\n mol = Chem.MolFromSmiles(smi)\n smi = Chem.MolToSmiles(mol)\n except:\n print(f'Invalid SMILES input in input row {i}')\n print('Correct your input! Exiting..')\n return\n if parameter=='kcat':\n if '.' in smi:\n x = smi.split('.')\n y = sorted(x)\n smi = '.'.join(y)\n smiles_list_new.append(smi)\n i+=1\n \n i=0\n valid_aas = list('ACDEFGHIKLMNPQRSTVWY')\n for seq in seq_list:\n for aa in seq:\n if not aa in valid_aas:\n print(f'Invalid Enzyme sequence input in row {i}!')\n print('Correct your input! Exiting..')\n return\n i+=1\n\n input_file_new_path = f'{input_file_path[:-4]}_input.csv'\n df['SMILES'] = smiles_list_new\n df.to_csv(input_file_new_path)\n \n f = open(f'predict.sh', 'w')\n f.write(f'''\n TEST_FILE_PREFIX={input_file_new_path[:-4]}\n RECORDS_FILE=${{TEST_FILE_PREFIX}}.json\n CHECKPOINT_DIR=./production_models/{parameter}/\n \n python ./scripts/create_pdbrecords.py --data_file ${{TEST_FILE_PREFIX}}.csv --out_file ${{RECORDS_FILE}}\n python predict.py --test_path ${{TEST_FILE_PREFIX}}.csv --preds_path ${{TEST_FILE_PREFIX}}_output.csv --checkpoint_dir $CHECKPOINT_DIR --uncertainty_method mve --smiles_column SMILES --individual_ensemble_predictions --protein_records_path $RECORDS_FILE\n ''')\n f.close()\n \n return input_file_new_path[:-4]+'_output.csv'\n\noutfile = create_csv_sh(parameter, input_file_path)\n\nprint('Predicting.. This will take a while..\\n')\n\nif use_cpu:\n os.system(\"export PROTEIN_EMBED_USE_CPU=1;./predict.sh >/dev/null 2>&1\")\nelse:\n os.system(\"export PROTEIN_EMBED_USE_CPU=0;./predict.sh >/dev/null 2>&1\")\n\ndef get_predictions(parameter, outfile):\n df = pd.read_csv(outfile)\n pred_col = []\n pred_logcol = []\n pred_sd_totcol = []\n pred_sd_aleacol = []\n pred_sd_epicol = []\n \n for ind, row in df.iterrows():\n unit = 'mM'\n if parameter=='kcat':\n parameter_print = 'k_{cat}'\n parameter_print_log = 'log_{10}(k_{cat})'\n target_col = 'log10kcat_max'\n unit = 's^(-1)'\n elif parameter=='km':\n target_col = 'log10km_mean'\n parameter_print = 'K_{m}'\n parameter_print_log = 'log_{10}(K_{m})'\n else:\n target_col = 'log10ki_mean'\n parameter_print = 'K_{i}'\n parameter_print_log = 'log_{10}(K_{i})'\n \n unc_col = f'{target_col}_mve_uncal_var'\n model_cols = [col for col in df.columns if col.startswith(target_col) and 'model_' in col]\n \n unc = df[unc_col].iloc[0]\n \n prediction = df[target_col].iloc[0]\n prediction_linear = np.power(10, prediction)\n \n model_out = df[target_col].iloc[0]\n model_outs = np.array([df[col].iloc[0] for col in model_cols])\n # print(model_outs)\n epi_unc = np.var(model_outs)#np.sum(np.power(2, model_outs))/10. - np.power(2, model_out)\n alea_unc = unc - epi_unc\n epi_unc = np.sqrt(epi_unc)\n alea_unc = np.sqrt(alea_unc)\n unc = np.sqrt(unc)\n \n # print(unc-epi_unc-alea_unc)\n # def display_outs(prediction_type, out, alea_output, epi_output, unit):\n pred_col.append(prediction_linear)\n pred_logcol.append(prediction)\n pred_sd_totcol.append(unc)\n pred_sd_aleacol.append(alea_unc)\n pred_sd_epicol.append(epi_unc)\n\n df[f'Prediction_({unit})'] = pred_col\n df['Prediction_log10'] = pred_logcol\n df['SD_total'] = pred_sd_totcol\n df['SD_aleatoric'] = pred_sd_aleacol\n df['SD_epistemic'] = pred_sd_epicol\n\n return df\n\noutput_final = get_predictions(parameter, outfile)\noutput_final.to_csv(f'{outfile}')\nprint('Output saved to', outfile)"},{"cell_type":"code","execution_count":null,"id":"3870129e-5fd5-4eae-b8b7-88bc727a8cf9","metadata":{},"outputs":[],"source":""}],"metadata":{"kernelspec":{"display_name":"Python 3 (ipykernel)","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.8.5"}},"nbformat":4,"nbformat_minor":5}

demo_run.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
parameter = 'kcat' # allowed values: ["kcat", "Km", "Ki"]
2+
parameter = parameter.lower()
3+
4+
use_cpu = 1 # set to 0 if you have GPU enabled
5+
6+
input_file_path = '../data/demo/batch_kcat.csv'
7+
8+
import time
9+
import os
10+
import pandas as pd
11+
import numpy as np
12+
from IPython.display import Image, display
13+
from rdkit import Chem
14+
from IPython.display import display, Latex, Math
15+
16+
print(os.getcwd())
17+
18+
def create_csv_sh(parameter, input_file_path):
19+
df = pd.read_csv(input_file_path)
20+
smiles_list = df.SMILES
21+
seq_list = df.sequence
22+
smiles_list_new = []
23+
i=0
24+
for smi in smiles_list:
25+
try:
26+
mol = Chem.MolFromSmiles(smi)
27+
smi = Chem.MolToSmiles(mol)
28+
except:
29+
print(f'Invalid SMILES input in input row {i}')
30+
print('Correct your input! Exiting..')
31+
return
32+
if parameter=='kcat':
33+
if '.' in smi:
34+
x = smi.split('.')
35+
y = sorted(x)
36+
smi = '.'.join(y)
37+
smiles_list_new.append(smi)
38+
i+=1
39+
40+
i=0
41+
valid_aas = list('ACDEFGHIKLMNPQRSTVWY')
42+
for seq in seq_list:
43+
for aa in seq:
44+
if not aa in valid_aas:
45+
print(f'Invalid Enzyme sequence input in row {i}!')
46+
print('Correct your input! Exiting..')
47+
return
48+
i+=1
49+
50+
input_file_new_path = f'{input_file_path[:-4]}_input.csv'
51+
df['SMILES'] = smiles_list_new
52+
df.to_csv(input_file_new_path)
53+
54+
f = open(f'predict.sh', 'w')
55+
f.write(f'''
56+
TEST_FILE_PREFIX={input_file_new_path[:-4]}
57+
RECORDS_FILE=${{TEST_FILE_PREFIX}}.json
58+
CHECKPOINT_DIR=./production_models/{parameter}/
59+
60+
python ./scripts/create_pdbrecords.py --data_file ${{TEST_FILE_PREFIX}}.csv --out_file ${{RECORDS_FILE}}
61+
python predict.py --test_path ${{TEST_FILE_PREFIX}}.csv --preds_path ${{TEST_FILE_PREFIX}}_output.csv --checkpoint_dir $CHECKPOINT_DIR --uncertainty_method mve --smiles_column SMILES --individual_ensemble_predictions --protein_records_path $RECORDS_FILE
62+
''')
63+
f.close()
64+
65+
return input_file_new_path[:-4]+'_output.csv'
66+
67+
outfile = create_csv_sh(parameter, input_file_path)
68+
69+
print('Predicting.. This will take a while..\n')
70+
71+
if use_cpu:
72+
os.system("export PROTEIN_EMBED_USE_CPU=1;./predict.sh")
73+
else:
74+
os.system("export PROTEIN_EMBED_USE_CPU=0;./predict.sh")
75+
76+
def get_predictions(parameter, outfile):
77+
df = pd.read_csv(outfile)
78+
pred_col = []
79+
pred_logcol = []
80+
pred_sd_totcol = []
81+
pred_sd_aleacol = []
82+
pred_sd_epicol = []
83+
84+
for ind, row in df.iterrows():
85+
unit = 'mM'
86+
if parameter=='kcat':
87+
parameter_print = 'k_{cat}'
88+
parameter_print_log = 'log_{10}(k_{cat})'
89+
target_col = 'log10kcat_max'
90+
unit = 's^(-1)'
91+
elif parameter=='km':
92+
target_col = 'log10km_mean'
93+
parameter_print = 'K_{m}'
94+
parameter_print_log = 'log_{10}(K_{m})'
95+
else:
96+
target_col = 'log10ki_mean'
97+
parameter_print = 'K_{i}'
98+
parameter_print_log = 'log_{10}(K_{i})'
99+
100+
unc_col = f'{target_col}_mve_uncal_var'
101+
model_cols = [col for col in row.columns if col.startswith(target_col) and 'model_' in col]
102+
103+
unc = row[unc_col].iloc[0]
104+
105+
prediction = row[target_col].iloc[0]
106+
prediction_linear = np.power(10, prediction)
107+
108+
model_out = row[target_col].iloc[0]
109+
model_outs = np.array([row[col].iloc[0] for col in model_cols])
110+
epi_unc = np.var(model_outs)
111+
alea_unc = unc - epi_unc
112+
epi_unc = np.sqrt(epi_unc)
113+
alea_unc = np.sqrt(alea_unc)
114+
unc = np.sqrt(unc)
115+
116+
pred_col.append(prediction_linear)
117+
pred_logcol.append(prediction)
118+
pred_sd_totcol.append(unc)
119+
pred_sd_aleacol.append(alea_unc)
120+
pred_sd_epicol.append(epi_unc)
121+
122+
df[f'Prediction_({unit})'] = pred_col
123+
df['Prediction_log10'] = pred_logcol
124+
df['SD_total'] = pred_sd_totcol
125+
df['SD_aleatoric'] = pred_sd_aleacol
126+
df['SD_epistemic'] = pred_sd_epicol
127+
128+
return df
129+
130+
output_final = get_predictions(parameter, outfile)
131+
filename = outfile.split('/')[-1]
132+
output_final.to_csv(f'../results/{filename}')
133+
print('Output saved to results/', filename)

single_demo-2.ipynb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"cells":[{"cell_type":"markdown","id":"49b08bbc-db31-4aa4-89b4-2313c0030642","metadata":{},"source":"## Define inputs"},{"cell_type":"code","execution_count":11,"id":"3c1841bb-8c35-4562-b86f-30777bb299bb","metadata":{},"outputs":[],"source":"parameter = 'Km' # allowed values: [\"kcat\", \"Km\", \"Ki\"] \nparameter = parameter.lower()\n\nuse_cpu = 1 # set to 0 if you have GPU enabled\n\nuniprot_id = \"P35557\" \n# If you do not have a uniprot-id, enter some name (for eg: \"enzyme1\")\nsequence = 'MLDDRARMEAAKKEKVEQILAEFQLQEEDLKKVMRRMQKEMDRGLRLETHEEASVKMLPTYVRSTPEGSEVGDFLSLDLGGTNFRVMLVKVGEGEEGQWSVKTKHQMYSIPEDAMTGTAEMLFDYISECISDFLDKHQMKHKKLPLGFTFSFPVRHEDIDKGILLNWTKGFKASGAEGNNVVGLLRDAIKRRGDFEMDVVAMVNDTVATMISCYYEDHQCEVGMIVGTGCNACYMEEMQNVELVEGDEGRMCVNTEWGAFGDSGELDEFLLEYDRLVDESSANPGQQLYEKLIGGKYMGELVRLVLLRLVDENLLFHGEASEQLRTRGAFETRFVSQVESDTGDRKQIYNILSTLGLRPSTTDCDIVRRACESVSTRAAHMCSAGLAGVINRMRESRSEDVMRITVGVDGSVYKLHPSFKERFHASVRRLTPSCEITFIESEEGSGRGAALVSAVACKKACMLGQ'\nSMILES = \"C([C@@H]1[C@H]([C@@H]([C@H](C(O1)O)O)O)O)O\" "},{"cell_type":"markdown","id":"db40ad26-0481-405b-8323-70d23f0ef44e","metadata":{},"source":"## Navigate to below cell and click \"Run->Run Selected Cell\" to get prediction"},{"cell_type":"markdown","id":"81de60cf-39b5-4546-83da-497c15e01b59","metadata":{},"source":"The result will be printed on the right column"},{"cell_type":"code","execution_count":16,"id":"be6cfa1f-9724-44ad-8ff9-ba8dcd51b887","metadata":{"jupyter":{"source_hidden":true}},"outputs":[{"name":"stdout","output_type":"stream","text":["Input success!\n","Enzyme sequence length: 465\n","Substrate structure:\n","Predicting.. This will take a while..\n","\n"]},{"data":{"text/latex":["$\\displaystyle K_{m} = 5.57977 mM$"],"text/plain":["<IPython.core.display.Math object>"]},"metadata":{},"output_type":"display_data"},{"name":"stdout","output_type":"stream","text":["\n","\n"]},{"data":{"text/latex":["$\\displaystyle log_{10}(K_{m}) = 0.74662$"],"text/plain":["<IPython.core.display.Math object>"]},"metadata":{},"output_type":"display_data"},{"data":{"text/latex":["$\\displaystyle SD_{total} = 0.69207$"],"text/plain":["<IPython.core.display.Math object>"]},"metadata":{},"output_type":"display_data"},{"data":{"text/latex":["$\\displaystyle SD_{aleatoric} = 0.64138$"],"text/plain":["<IPython.core.display.Math object>"]},"metadata":{},"output_type":"display_data"},{"data":{"text/latex":["$\\displaystyle SD_{epistemic} = 0.25999$"],"text/plain":["<IPython.core.display.Math object>"]},"metadata":{},"output_type":"display_data"}],"source":"import time\nimport os\nimport pandas as pd\nimport numpy as np\nfrom IPython.display import Image, display\nfrom rdkit import Chem\nfrom IPython.display import display, Latex, Math\n\ndef create_csv_sh(parameter, uni, seq, smi):\n try:\n mol = Chem.MolFromSmiles(smi)\n smi = Chem.MolToSmiles(mol)\n except:\n print('Invalid SMILES input!')\n print('Correct your input! Exiting..')\n return\n valid_aas = list('ACDEFGHIKLMNPQRSTVWY')\n for aa in seq:\n if not aa in valid_aas:\n print('Invalid Enzyme sequence input!')\n print('Correct your input! Exiting..')\n return\n if parameter=='kcat':\n if '.' in smi:\n x = smi.split('.')\n y = sorted(x)\n smi = '.'.join(y)\n f = open(f'{uni}_{parameter}_input.csv', 'w')\n f.write('name,sequence,SMILES,pdbpath\\n')\n f.write(f'{uni},{seq},{smi},{uni}.pdb\\n')\n f.close()\n\n f = open(f'predict.sh', 'w')\n f.write(f'''\nTEST_FILE_PREFIX={uni}_{parameter}\nRECORDS_FILE=${{TEST_FILE_PREFIX}}.json\nCHECKPOINT_DIR=./production_models/{parameter}/\n\npython ./scripts/create_pdbrecords.py --data_file ${{TEST_FILE_PREFIX}}_input.csv --out_file ${{RECORDS_FILE}}\npython predict.py --test_path ${{TEST_FILE_PREFIX}}_input.csv --preds_path ${{TEST_FILE_PREFIX}}_output.csv --checkpoint_dir $CHECKPOINT_DIR --uncertainty_method mve --smiles_column SMILES --individual_ensemble_predictions --protein_records_path $RECORDS_FILE\n''')\n f.close()\n\n print('Input success!')\n print('Enzyme sequence length:', len(sequence))\n print('Substrate structure:')\n # display(ShowMols([mol]))\n\n return seq, smi\n\nseq, smi = create_csv_sh(parameter, uniprot_id, sequence, SMILES)\n\nprint('Predicting.. This will take a while..\\n')\n\ndef get_predictions(parameter, uniprot_id):\n df = pd.read_csv(f'{uniprot_id}_{parameter}_output.csv')\n unit = ' mM'\n if parameter=='kcat':\n parameter_print = 'k_{cat}'\n parameter_print_log = 'log_{10}(k_{cat})'\n target_col = 'log10kcat_max'\n unit = ' s^{-1}'\n elif parameter=='km':\n target_col = 'log10km_mean'\n parameter_print = 'K_{m}'\n parameter_print_log = 'log_{10}(K_{m})'\n else:\n target_col = 'log10ki_mean'\n parameter_print = 'K_{i}'\n parameter_print_log = 'log_{10}(K_{i})'\n\n unc_col = f'{target_col}_mve_uncal_var'\n model_cols = [col for col in df.columns if col.startswith(target_col) and 'model_' in col]\n\n unc = df[unc_col].iloc[0]\n\n prediction = df[target_col].iloc[0]\n prediction_linear = np.power(10, prediction)\n\n model_out = df[target_col].iloc[0]\n model_outs = np.array([df[col].iloc[0] for col in model_cols])\n # print(model_outs)\n epi_unc = np.var(model_outs)#np.sum(np.power(2, model_outs))/10. - np.power(2, model_out)\n alea_unc = unc - epi_unc\n epi_unc = np.sqrt(epi_unc)\n alea_unc = np.sqrt(alea_unc)\n unc = np.sqrt(unc)\n\n # print(unc-epi_unc-alea_unc)\n # def display_outs(prediction_type, out, alea_output, epi_output, unit):\n display(Math((parameter_print + f' = {prediction_linear:.5f}'+ unit)))\n print('\\n')\n display(Math((parameter_print_log + f' = {prediction:.5f}')))\n display(Math(('SD_{total}'+f' = {unc:.5f}')))\n display(Math(('SD_{aleatoric}'+f' = {alea_unc:.5f}')))\n display(Math(('SD_{epistemic}'+f' = {epi_unc:.5f}')))\n\nif use_cpu:\n os.system(\"export PROTEIN_EMBED_USE_CPU=1;./predict.sh >/dev/null 2>&1\")\nelse:\n os.system(\"export PROTEIN_EMBED_USE_CPU=0;./predict.sh >/dev/null 2>&1\")\n\nget_predictions(parameter,uniprot_id)"}],"metadata":{"kernelspec":{"display_name":"Python 3 (ipykernel)","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.8.5"}},"nbformat":4,"nbformat_minor":5}

0 commit comments

Comments
 (0)