Spaces:
Runtime error
Runtime error
simonduerr
commited on
Commit
•
f303e27
1
Parent(s):
3a2fa2f
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
+
import os
|
7 |
+
|
8 |
+
def get_pdb(pdb_code="", filepath=""):
|
9 |
+
if pdb_code is None or pdb_code == "":
|
10 |
+
try:
|
11 |
+
return filepath.name
|
12 |
+
except AttributeError as e:
|
13 |
+
return None
|
14 |
+
else:
|
15 |
+
os.system(f"wget -qnc https://files.rcsb.org/view/{pdb_code}.pdb")
|
16 |
+
return f"{pdb_code}.pdb"
|
17 |
+
|
18 |
+
|
19 |
+
def read_mol(molpath):
|
20 |
+
with open(molpath, "r") as fp:
|
21 |
+
lines = fp.readlines()
|
22 |
+
mol = ""
|
23 |
+
for l in lines:
|
24 |
+
mol += l
|
25 |
+
return mol
|
26 |
+
|
27 |
+
|
28 |
+
def molecule(input_pdb):
|
29 |
+
|
30 |
+
mol = read_mol(input_pdb)
|
31 |
+
|
32 |
+
html ="""<!DOCTYPE html>
|
33 |
+
<html lang="en">
|
34 |
+
<head>
|
35 |
+
<meta charset="utf-8" />
|
36 |
+
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
|
37 |
+
<title>PDBe Molstar - Helper functions</title>
|
38 |
+
|
39 |
+
<!-- Molstar CSS & JS -->
|
40 |
+
<link rel="stylesheet" type="text/css" href="https://www.ebi.ac.uk/pdbe/pdb-component-library/css/pdbe-molstar-light-3.1.0.css">
|
41 |
+
<script type="text/javascript" src="https://www.ebi.ac.uk/pdbe/pdb-component-library/js/pdbe-molstar-plugin-3.1.0.js"></script>
|
42 |
+
<style>
|
43 |
+
* {
|
44 |
+
margin: 0;
|
45 |
+
padding: 0;
|
46 |
+
box-sizing: border-box;
|
47 |
+
}
|
48 |
+
.msp-plugin ::-webkit-scrollbar-thumb {
|
49 |
+
background-color: #474748 !important;
|
50 |
+
}
|
51 |
+
.viewerSection {
|
52 |
+
margin: 120px 0 0 50px;
|
53 |
+
}
|
54 |
+
#myViewer{
|
55 |
+
float:left;
|
56 |
+
width:400px;
|
57 |
+
height: 400px;
|
58 |
+
position:relative;
|
59 |
+
}
|
60 |
+
</style>
|
61 |
+
</head>
|
62 |
+
|
63 |
+
<body>
|
64 |
+
<h4>PDBe Mol* AlphaFold Demo</h4>
|
65 |
+
<div class="viewerSection">
|
66 |
+
<!-- Molstar container -->
|
67 |
+
<div id="myViewer"></div>
|
68 |
+
|
69 |
+
</div>
|
70 |
+
<script>
|
71 |
+
|
72 |
+
//Create plugin instance
|
73 |
+
var viewerInstance = new PDBeMolstarPlugin();
|
74 |
+
|
75 |
+
//Set options (Checkout available options list in the documentation)
|
76 |
+
var options = {
|
77 |
+
customData: {
|
78 |
+
url: "https://alphafold.ebi.ac.uk/files/AF-O15552-F1-model_v1.cif",
|
79 |
+
format: "cif"
|
80 |
+
},
|
81 |
+
alphafoldView: true,
|
82 |
+
bgColor: {r:255, g:255, b:255},
|
83 |
+
hideCanvasControls: ["selection", "animation", "controlToggle", "controlInfo"]
|
84 |
+
}
|
85 |
+
|
86 |
+
//Get element from HTML/Template to place the viewer
|
87 |
+
var viewerContainer = document.getElementById("myViewer");
|
88 |
+
|
89 |
+
//Call render method to display the 3D view
|
90 |
+
viewerInstance.render(viewerContainer, options);
|
91 |
+
|
92 |
+
</script>
|
93 |
+
</body>
|
94 |
+
|
95 |
+
</html>"""
|
96 |
+
|
97 |
+
return f"""<iframe style="width: 100%; height: 600px" name="result" allow="midi; geolocation; microphone; camera;
|
98 |
+
display-capture; encrypted-media;" sandbox="allow-modals allow-forms
|
99 |
+
allow-scripts allow-same-origin allow-popups
|
100 |
+
allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
|
101 |
+
allowpaymentrequest="" frameborder="0" srcdoc='{x}'></iframe>"""
|
102 |
+
|
103 |
+
|
104 |
+
def update(inp, file, request: gr.Request):
|
105 |
+
pdb_path = get_pdb(inp, file)
|
106 |
+
print("Request headers dictionary:", request.headers)
|
107 |
+
print("IP address:", request.client.host)
|
108 |
+
return molecule(pdb_path)
|
109 |
+
|
110 |
+
|
111 |
+
demo = gr.Blocks()
|
112 |
+
|
113 |
+
with demo:
|
114 |
+
gr.Markdown("# PDB viewer using Molstar")
|
115 |
+
gr.Markdown("""If using please cite
|
116 |
+
> David Sehnal, Sebastian Bittrich, Mandar Deshpande, Radka Svobodová, Karel Berka, Václav Bazgier, Sameer Velankar, Stephen K Burley, Jaroslav Koča, Alexander S Rose: Mol* Viewer: modern web app for 3D visualization and analysis of large biomolecular structures, Nucleic Acids Research, 2021; 10.1093/nar/gkab31.""")
|
117 |
+
with gr.Row():
|
118 |
+
with gr.Box():
|
119 |
+
inp = gr.Textbox(
|
120 |
+
placeholder="PDB Code or upload file below", label="Input structure"
|
121 |
+
)
|
122 |
+
file = gr.File(file_count="single")
|
123 |
+
gr.Examples(["2CBA", "6VXX"], inp)
|
124 |
+
btn = gr.Button("View structure")
|
125 |
+
mol = gr.HTML()
|
126 |
+
btn.click(fn=update, inputs=[inp, file], outputs=mol)
|
127 |
+
demo.launch()
|