paulo-seixal commited on
Commit
357c3d2
·
verified ·
1 Parent(s): aad238d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ from rdkit import Chem
4
+ from rdkit.Chem import AllChem
5
+ import pickle
6
+
7
+ # Load cell lines data and top genes
8
+ cell_lines = pd.read_csv('gene_expression.csv', index_col=0)
9
+ with open('2128_genes.pkl', 'rb') as f:
10
+ top_genes = pickle.load(f)
11
+
12
+ # Load model
13
+ with open('xgboost.pkl', 'rb') as f:
14
+ model = pickle.load(f)
15
+
16
+ filtered_cell_lines = cell_lines[cell_lines.columns.intersection(top_genes)]
17
+
18
+ # Define the smiles_to_fingerprint function
19
+ def smiles_to_fingerprint(smiles):
20
+ mol = Chem.MolFromSmiles(smiles)
21
+ fp = AllChem.GetMorganFingerprintAsBitVect(mol, 2, nBits=1024)
22
+ return fp
23
+
24
+ # Define a function that will be called when the user makes a prediction
25
+ def predict(smiles_notation):
26
+ # Transform SMILES to fingerprint
27
+ fingerprint = smiles_to_fingerprint(smiles_notation)
28
+
29
+ # Convert the fingerprint to a DataFrame with one row and columns representing bits
30
+ 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)
31
+
32
+ # Merge the fingerprint with each row of filtered_cell_lines
33
+ fingerprint_df['common_key'] = 1
34
+ filtered_cell_lines['common_key'] = 1
35
+ merged_data = pd.merge(filtered_cell_lines, fingerprint_df, on='common_key').drop('common_key', axis=1)
36
+
37
+ # Perform any additional processing or prediction based on the merged_data
38
+ predicts = model.predict(merged_data)
39
+
40
+ #merge predicts with cell lines
41
+ predicts = pd.DataFrame({'IC50': predicts,
42
+ 'Cell_line': filtered_cell_lines.index})
43
+
44
+ #sort by IC50 (only lowest 20)
45
+ predicts = predicts.sort_values(by='IC50').head(10)
46
+
47
+ return predicts
48
+
49
+ # Define the Gradio interface
50
+ iface = gr.Interface(
51
+ fn=predict,
52
+ 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"),
53
+ 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),
54
+ title="Drug Response Prediction",
55
+ )
56
+
57
+ # Launch the Gradio interface
58
+ iface.launch(share=True)