paulo-seixal's picture
Create app.py
357c3d2 verified
import gradio as gr
import pandas as pd
from rdkit import Chem
from rdkit.Chem import AllChem
import pickle
# Load cell lines data and top genes
cell_lines = pd.read_csv('gene_expression.csv', index_col=0)
with open('2128_genes.pkl', 'rb') as f:
top_genes = pickle.load(f)
# Load model
with open('xgboost.pkl', 'rb') as f:
model = pickle.load(f)
filtered_cell_lines = cell_lines[cell_lines.columns.intersection(top_genes)]
# Define the smiles_to_fingerprint function
def smiles_to_fingerprint(smiles):
mol = Chem.MolFromSmiles(smiles)
fp = AllChem.GetMorganFingerprintAsBitVect(mol, 2, nBits=1024)
return fp
# Define a function that will be called when the user makes a prediction
def predict(smiles_notation):
# Transform SMILES to fingerprint
fingerprint = smiles_to_fingerprint(smiles_notation)
# Convert the fingerprint to a DataFrame with one row and columns representing bits
fingerprint_df = pd.DataFrame([list(fingerprint)], columns=range(1024)).apply(lambda x: pd.Series({f'fp{str(i)}': val for i, val in enumerate(x)}), axis=1)
# Merge the fingerprint with each row of filtered_cell_lines
fingerprint_df['common_key'] = 1
filtered_cell_lines['common_key'] = 1
merged_data = pd.merge(filtered_cell_lines, fingerprint_df, on='common_key').drop('common_key', axis=1)
# Perform any additional processing or prediction based on the merged_data
predicts = model.predict(merged_data)
#merge predicts with cell lines
predicts = pd.DataFrame({'IC50': predicts,
'Cell_line': filtered_cell_lines.index})
#sort by IC50 (only lowest 20)
predicts = predicts.sort_values(by='IC50').head(10)
return predicts
# Define the Gradio interface
iface = gr.Interface(
fn=predict,
inputs=gr.Textbox(value="COc1cc(O)c2c(c1)C=CCC(O)C(O)C(=O)C=CCC(C)OC2=O", lines=1, label="Enter drug in SMILES notation"),
outputs=gr.Dataframe(headers=['IC50', 'Cell_line'], type="numpy",label = 'Top 10 Cell Lines with lowest IC50 (GDSC2 dataset)' , datatype="number", row_count=10, col_count=2),
title="Drug Response Prediction",
)
# Launch the Gradio interface
iface.launch(share=True)