Spaces:
Build error
Build error
Rocco Meli
commited on
Commit
·
0e0c2ea
1
Parent(s):
7b26682
add drop-down menu to select different ensembles
Browse files
app.py
CHANGED
|
@@ -1,9 +1,20 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
|
| 5 |
|
| 6 |
-
def load_html(html_file: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
with open(os.path.join("html", html_file), "r") as f:
|
| 8 |
return f.read()
|
| 9 |
|
|
@@ -24,12 +35,38 @@ def load_protein_from_file(protein_file) -> str:
|
|
| 24 |
return f.read()
|
| 25 |
|
| 26 |
|
| 27 |
-
def load_ligand_from_file(ligand_file):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
with open(ligand_file.name, "r") as f:
|
| 29 |
return f.read()
|
| 30 |
|
| 31 |
|
| 32 |
-
def protein_html_from_file(protein_file):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
protein = load_protein_from_file(protein_file)
|
| 34 |
protein_html = load_html("protein.html")
|
| 35 |
|
|
@@ -40,7 +77,21 @@ def protein_html_from_file(protein_file):
|
|
| 40 |
return wrapper.replace("%%%HTML%%%", html)
|
| 41 |
|
| 42 |
|
| 43 |
-
def ligand_html_from_file(ligand_file):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
ligand = load_ligand_from_file(ligand_file)
|
| 45 |
ligand_html = load_html("ligand.html")
|
| 46 |
|
|
@@ -64,7 +115,24 @@ def protein_ligand_html_from_file(protein_file, ligand_file):
|
|
| 64 |
return wrapper.replace("%%%HTML%%%", html)
|
| 65 |
|
| 66 |
|
| 67 |
-
def predict(protein_file, ligand_file, cnn="default"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
import molgrid
|
| 69 |
from gninatorch import gnina, dataloaders
|
| 70 |
import torch
|
|
@@ -85,6 +153,7 @@ def predict(protein_file, ligand_file, cnn="default"):
|
|
| 85 |
iteration_scheme=molgrid.IterationScheme.SmallEpoch,
|
| 86 |
)
|
| 87 |
|
|
|
|
| 88 |
with open("data.in", "w") as f:
|
| 89 |
f.write(protein_file.name)
|
| 90 |
f.write(" ")
|
|
@@ -124,40 +193,66 @@ def predict(protein_file, ligand_file, cnn="default"):
|
|
| 124 |
)
|
| 125 |
|
| 126 |
|
| 127 |
-
|
|
|
|
| 128 |
|
| 129 |
-
|
| 130 |
-
gr.Markdown("# Protein and Ligand")
|
| 131 |
-
with gr.Row():
|
| 132 |
-
with gr.Box():
|
| 133 |
-
pfile = gr.File(file_count="single")
|
| 134 |
-
pbtn = gr.Button("View")
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
ligand = gr.HTML()
|
| 144 |
-
lbtn.click(fn=ligand_html_from_file, inputs=[lfile], outputs=ligand)
|
| 145 |
-
|
| 146 |
-
gr.Markdown("# Protein-Ligand Complex")
|
| 147 |
-
with gr.Row():
|
| 148 |
-
plcomplex = gr.HTML()
|
| 149 |
-
|
| 150 |
-
# TODO: Automatically display complex when both files are uploaded
|
| 151 |
-
plbtn = gr.Button("View")
|
| 152 |
-
plbtn.click(
|
| 153 |
-
fn=protein_ligand_html_from_file, inputs=[pfile, lfile], outputs=plcomplex
|
| 154 |
)
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
|
| 3 |
|
| 4 |
+
def load_html(html_file: str) -> str:
|
| 5 |
+
"""
|
| 6 |
+
Load file from HTML directory.
|
| 7 |
+
|
| 8 |
+
Parameters
|
| 9 |
+
----------
|
| 10 |
+
html_file: str
|
| 11 |
+
HTML file name
|
| 12 |
+
|
| 13 |
+
Returns
|
| 14 |
+
-------
|
| 15 |
+
str
|
| 16 |
+
HTML file content
|
| 17 |
+
"""
|
| 18 |
with open(os.path.join("html", html_file), "r") as f:
|
| 19 |
return f.read()
|
| 20 |
|
|
|
|
| 35 |
return f.read()
|
| 36 |
|
| 37 |
|
| 38 |
+
def load_ligand_from_file(ligand_file) -> str:
|
| 39 |
+
"""
|
| 40 |
+
Load ligand from file.
|
| 41 |
+
|
| 42 |
+
Parameters
|
| 43 |
+
----------
|
| 44 |
+
ligand_file: _TemporaryFileWrapper
|
| 45 |
+
GradIO file object
|
| 46 |
+
|
| 47 |
+
Returns
|
| 48 |
+
-------
|
| 49 |
+
str
|
| 50 |
+
Ligand SDF file content
|
| 51 |
+
"""
|
| 52 |
with open(ligand_file.name, "r") as f:
|
| 53 |
return f.read()
|
| 54 |
|
| 55 |
|
| 56 |
+
def protein_html_from_file(protein_file) -> str:
|
| 57 |
+
"""
|
| 58 |
+
Wrap 3Dmol.js code around protein PDB file.
|
| 59 |
+
|
| 60 |
+
Parameters
|
| 61 |
+
----------
|
| 62 |
+
protein_file: _TemporaryFileWrapper
|
| 63 |
+
GradIO file object
|
| 64 |
+
|
| 65 |
+
Returns
|
| 66 |
+
-------
|
| 67 |
+
str
|
| 68 |
+
3Dmol.js HTML code for displaying a PDB file
|
| 69 |
+
"""
|
| 70 |
protein = load_protein_from_file(protein_file)
|
| 71 |
protein_html = load_html("protein.html")
|
| 72 |
|
|
|
|
| 77 |
return wrapper.replace("%%%HTML%%%", html)
|
| 78 |
|
| 79 |
|
| 80 |
+
def ligand_html_from_file(ligand_file) -> str:
|
| 81 |
+
"""
|
| 82 |
+
Wrap 3Dmol.js code around ligand SDF file.
|
| 83 |
+
|
| 84 |
+
Parameters
|
| 85 |
+
----------
|
| 86 |
+
ligand_file: _TemporaryFileWrapper
|
| 87 |
+
GradIO file object
|
| 88 |
+
|
| 89 |
+
Returns
|
| 90 |
+
-------
|
| 91 |
+
str
|
| 92 |
+
3Dmol.js HTML code for displaying a SDF file
|
| 93 |
+
"""
|
| 94 |
+
|
| 95 |
ligand = load_ligand_from_file(ligand_file)
|
| 96 |
ligand_html = load_html("ligand.html")
|
| 97 |
|
|
|
|
| 115 |
return wrapper.replace("%%%HTML%%%", html)
|
| 116 |
|
| 117 |
|
| 118 |
+
def predict(protein_file, ligand_file, cnn: str = "default"):
|
| 119 |
+
"""
|
| 120 |
+
Run gnina-torch on protein-ligand complex.
|
| 121 |
+
|
| 122 |
+
Parameters
|
| 123 |
+
----------
|
| 124 |
+
protein_file: _TemporaryFileWrapper
|
| 125 |
+
GradIO file object
|
| 126 |
+
ligand_file: _TemporaryFileWrapper
|
| 127 |
+
GradIO file object
|
| 128 |
+
cnn: str
|
| 129 |
+
CNN model to use
|
| 130 |
+
|
| 131 |
+
Returns
|
| 132 |
+
-------
|
| 133 |
+
dict[str, float]
|
| 134 |
+
CNNscore, CNNaffinity, and CNNvariance
|
| 135 |
+
"""
|
| 136 |
import molgrid
|
| 137 |
from gninatorch import gnina, dataloaders
|
| 138 |
import torch
|
|
|
|
| 153 |
iteration_scheme=molgrid.IterationScheme.SmallEpoch,
|
| 154 |
)
|
| 155 |
|
| 156 |
+
# FIXME: Do this properly... =( [Might require light gnina-torch refactoring]
|
| 157 |
with open("data.in", "w") as f:
|
| 158 |
f.write(protein_file.name)
|
| 159 |
f.write(" ")
|
|
|
|
| 193 |
)
|
| 194 |
|
| 195 |
|
| 196 |
+
if __name__ == "__main__":
|
| 197 |
+
import gradio as gr
|
| 198 |
|
| 199 |
+
demo = gr.Blocks()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
+
with demo:
|
| 202 |
+
gr.Markdown("# Gnina-Torch")
|
| 203 |
+
gr.Markdown(
|
| 204 |
+
"Score your protein-ligand compex and predict the binding affinity with [Gnina]"
|
| 205 |
+
+ "(https://github.com/gnina/gnina)'s scoring function. Poewerd by [gnina-torch]"
|
| 206 |
+
+ "(https://github.com/RMeli/gnina-torch), a PyTorch implementation of Gnina's"
|
| 207 |
+
+ " scoring function."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
)
|
| 209 |
|
| 210 |
+
gr.Markdown("## Protein and Ligand")
|
| 211 |
+
gr.Markdown(
|
| 212 |
+
"Upload your protein and ligand files in PDB and SDF format, respectively."
|
| 213 |
+
)
|
| 214 |
+
with gr.Row():
|
| 215 |
+
with gr.Box():
|
| 216 |
+
pfile = gr.File(file_count="single", label="Protein file (PDB)")
|
| 217 |
+
pbtn = gr.Button("View")
|
| 218 |
+
|
| 219 |
+
protein = gr.HTML()
|
| 220 |
+
pbtn.click(fn=protein_html_from_file, inputs=[pfile], outputs=protein)
|
| 221 |
+
|
| 222 |
+
with gr.Box():
|
| 223 |
+
lfile = gr.File(file_count="single", label="Ligand file (SDF)")
|
| 224 |
+
lbtn = gr.Button("View")
|
| 225 |
+
|
| 226 |
+
ligand = gr.HTML()
|
| 227 |
+
lbtn.click(fn=ligand_html_from_file, inputs=[lfile], outputs=ligand)
|
| 228 |
+
|
| 229 |
+
gr.Markdown("## Protein-Ligand Complex")
|
| 230 |
+
with gr.Row():
|
| 231 |
+
plcomplex = gr.HTML()
|
| 232 |
+
|
| 233 |
+
# TODO: Automatically display complex when both files are uploaded
|
| 234 |
+
plbtn = gr.Button("View")
|
| 235 |
+
plbtn.click(
|
| 236 |
+
fn=protein_ligand_html_from_file,
|
| 237 |
+
inputs=[pfile, lfile],
|
| 238 |
+
outputs=plcomplex,
|
| 239 |
+
)
|
| 240 |
+
|
| 241 |
+
gr.Markdown("## Gnina-Torch")
|
| 242 |
+
with gr.Row():
|
| 243 |
+
dd = gr.Dropdown(
|
| 244 |
+
choices=[
|
| 245 |
+
"default",
|
| 246 |
+
"redock_default2018_ensemble",
|
| 247 |
+
"general_default2018_ensemble",
|
| 248 |
+
"crossdock_default2018_ensemble",
|
| 249 |
+
],
|
| 250 |
+
value="default",
|
| 251 |
+
label="CNN model",
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
df = gr.Dataframe()
|
| 255 |
+
btn = gr.Button("Score!")
|
| 256 |
+
btn.click(fn=predict, inputs=[pfile, lfile, dd], outputs=df)
|
| 257 |
+
|
| 258 |
+
demo.launch()
|
data.in
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
/tmp/4w52d_6apeph.pdb /tmp/4w52_B_BNZ9h06xhcz.sdf
|