simonduerr commited on
Commit
fbcf420
·
1 Parent(s): c6d365f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py CHANGED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def get_pdb(pdb_code="", filepath=""):
2
+ if pdb_code is None or pdb_code == "":
3
+ try:
4
+ return filepath.name
5
+ except AttributeError as e:
6
+ return None
7
+ else:
8
+ os.system(f"wget -qnc https://files.rcsb.org/view/{pdb_code}.pdb")
9
+ return f"{pdb_code}.pdb"
10
+
11
+ def read_mol(molpath):
12
+ with open(molpath, "r") as fp:
13
+ lines = fp.readlines()
14
+ mol = ""
15
+ for l in lines:
16
+ mol += l
17
+ return mol
18
+
19
+
20
+ def molecule(input_pdb):
21
+
22
+ mol = read_mol(input_pdb)
23
+
24
+ x = (
25
+ """<!DOCTYPE html>
26
+ <html>
27
+ <head>
28
+ <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
29
+ <style>
30
+ body{
31
+ font-family:sans-serif
32
+ }
33
+ .mol-container {
34
+ width: 100%;
35
+ height: 700px;
36
+ position: relative;
37
+ }
38
+ .mol-container select{
39
+ background-image:None;
40
+ }
41
+ </style>
42
+ <script src="https://3Dmol.csb.pitt.edu/build/3Dmol-min.js"></script>
43
+ </head>
44
+ <body>
45
+
46
+ <div id="container" class="mol-container"></div>
47
+
48
+ <script>
49
+ let pdb = `"""
50
+ + mol
51
+ + """`
52
+
53
+ $(document).ready(function () {
54
+ let element = $("#container");
55
+ let config = { backgroundColor: "white" };
56
+ let viewer = $3Dmol.createViewer(element, config);
57
+ viewer.addModel(pdb, "pdb");
58
+ viewer.getModel(0).setStyle({}, { cartoon: { colorscheme:"whiteCarbon" } });
59
+ viewer.zoomTo();
60
+ viewer.render();
61
+ viewer.zoom(0.8, 2000);
62
+ })
63
+ </script>
64
+ </body></html>"""
65
+ )
66
+
67
+ return f"""<iframe style="width: 800px; height: 800px" name="result" allow="midi; geolocation; microphone; camera;
68
+ display-capture; encrypted-media;" sandbox="allow-modals allow-forms
69
+ allow-scripts allow-same-origin allow-popups
70
+ allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
71
+ allowpaymentrequest="" frameborder="0" srcdoc='{x}'></iframe>"""
72
+
73
+ def update(inp, file):
74
+ pdb_path = get_pdb(inp, file)
75
+ return molecule(pdb_path)
76
+
77
+ demo = gr.Blocks()
78
+
79
+ with demo:
80
+ gr.Markdown("#PDB viewer using 3Dmol.js")
81
+ inp = gr.Textbox(placeholder="PDB Code or upload file below", label="Input structure")
82
+ file = gr.File(file_count="single")
83
+ btn = gr.Button("View structure")
84
+ mol = gr.HTML()
85
+ btn.click(fn=update,inputs=[inp, file],outputs=mol]
86
+
87
+ demo.launch()