Spaces:
Runtime error
Runtime error
Simon Duerr
commited on
Commit
·
98be481
1
Parent(s):
b101dd1
my first gradio app
Browse files- app.py +70 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
import py3Dmol
|
4 |
+
import torch
|
5 |
+
import pydssp
|
6 |
+
|
7 |
+
import os
|
8 |
+
|
9 |
+
|
10 |
+
def get_pdb(pdb_code="", filepath=""):
|
11 |
+
try:
|
12 |
+
return filepath.name
|
13 |
+
except AttributeError as e:
|
14 |
+
if pdb_code is None or pdb_code == "":
|
15 |
+
return None
|
16 |
+
else:
|
17 |
+
os.system(f"wget -qnc https://files.rcsb.org/view/{pdb_code}.pdb")
|
18 |
+
return f"{pdb_code}.pdb"
|
19 |
+
|
20 |
+
|
21 |
+
def get_offset(pdb):
|
22 |
+
pdb_multiline = pdb.split("\n")
|
23 |
+
for line in pdb_multiline:
|
24 |
+
if line.startswith("ATOM"):
|
25 |
+
return int(line[22:27])
|
26 |
+
|
27 |
+
|
28 |
+
def run(pdb_id, pdb_file, helix, sheet, loop):
|
29 |
+
path_to_pdb = get_pdb(pdb_code=pdb_id, filepath=pdb_file)
|
30 |
+
pdb = open(path_to_pdb, "r").read()
|
31 |
+
|
32 |
+
offset = get_offset(pdb)
|
33 |
+
coord = torch.tensor(pydssp.read_pdbtext(pdb))
|
34 |
+
secondary_struct = pydssp.assign(coord)
|
35 |
+
|
36 |
+
view = py3Dmol.view(width=400, height=400)
|
37 |
+
view.addModel(pdb, "pdb")
|
38 |
+
colormap = {"H": helix, "-": loop, "E": sheet}
|
39 |
+
colors = {i + offset: colormap[resi] for i, resi in enumerate(secondary_struct)}
|
40 |
+
view.setStyle({"cartoon": {"colorscheme": {"prop": "resi", "map": colors}}})
|
41 |
+
view.zoomTo()
|
42 |
+
output = view._make_html().replace("'", '"')
|
43 |
+
|
44 |
+
x = f"""<!DOCTYPE html><html> {output} </html>""" # do not use ' in this input
|
45 |
+
return f"""<iframe style="width: 100%; height:420px" name="result" allow="midi; geolocation; microphone; camera;
|
46 |
+
display-capture; encrypted-media;" sandbox="allow-modals allow-forms
|
47 |
+
allow-scripts allow-same-origin allow-popups
|
48 |
+
allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
|
49 |
+
allowpaymentrequest="" frameborder="0" srcdoc='{x}'></iframe>"""
|
50 |
+
|
51 |
+
|
52 |
+
with gr.Blocks() as demo:
|
53 |
+
pdb_id = gr.Textbox(label="pdb code")
|
54 |
+
pdb_file = gr.File(label="pdb file")
|
55 |
+
with gr.Row():
|
56 |
+
helix = gr.ColorPicker(label="Helix")
|
57 |
+
sheet = gr.ColorPicker(label="Sheet")
|
58 |
+
loop = gr.ColorPicker(label="Loop")
|
59 |
+
btn = gr.Button(label="run")
|
60 |
+
|
61 |
+
html = gr.HTML()
|
62 |
+
gr.Examples(
|
63 |
+
[["1QYS", "#ff0000", "#00ff00", "#0000ff"]],
|
64 |
+
inputs=[pdb_id, helix, sheet, loop],
|
65 |
+
outputs=[html],
|
66 |
+
fn=run,
|
67 |
+
)
|
68 |
+
btn.click(fn=run, inputs=[pdb_id, pdb_file, helix, sheet, loop], outputs=[html])
|
69 |
+
|
70 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
gradio
|
3 |
+
py3Dmol
|
4 |
+
git+https://github.com/ShintaroMinami/PyDSSP.git
|