Rocco Meli commited on
Commit
31f986f
·
1 Parent(s): 87bd350

simple gradio app to display protein and ligand

Browse files
Files changed (5) hide show
  1. app.py +93 -0
  2. conda.yml +12 -0
  3. html/ligand.html +36 -0
  4. html/pl.html +39 -0
  5. html/protein.html +36 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ import os
4
+
5
+ def load_html(html_file: str):
6
+ with open(os.path.join("html", html_file), "r") as f:
7
+ return f.read()
8
+
9
+ def load_protein_from_file(protein_file) -> str:
10
+ """
11
+ Parameters
12
+ ----------
13
+ protein_file: _TemporaryFileWrapper
14
+ GradIO file object
15
+
16
+ Returns
17
+ -------
18
+ str
19
+ Protein PDB file content
20
+ """
21
+ with open(protein_file.name, "r") as f:
22
+ return f.read()
23
+
24
+ def load_ligand_from_file(ligand_file):
25
+ with open(ligand_file.name, "r") as f:
26
+ return f.read()
27
+
28
+ def protein_html_from_file(protein_file):
29
+ protein = load_protein_from_file(protein_file)
30
+ protein_html = load_html("protein.html")
31
+
32
+ html = protein_html.replace("%%%PDB%%%", protein)
33
+
34
+ return f"""<iframe style="width: 100%; height: 600px" name="result" allow="midi; geolocation; microphone; camera;
35
+ display-capture; encrypted-media;" sandbox="allow-modals allow-forms
36
+ allow-scripts allow-same-origin allow-popups
37
+ allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
38
+ allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
39
+
40
+ def ligand_html_from_file(ligand_file):
41
+ ligand = load_ligand_from_file(ligand_file)
42
+ ligand_html = load_html("ligand.html")
43
+
44
+ html = ligand_html.replace("%%%SDF%%%", ligand)
45
+
46
+ return f"""<iframe style="width: 100%; height: 600px" name="result" allow="midi; geolocation; microphone; camera;
47
+ display-capture; encrypted-media;" sandbox="allow-modals allow-forms
48
+ allow-scripts allow-same-origin allow-popups
49
+ allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
50
+ allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
51
+
52
+ def protein_ligand_html_from_file(protein_file, ligand_file):
53
+ protein = load_protein_from_file(protein_file)
54
+ ligand = load_ligand_from_file(ligand_file)
55
+ protein_ligand_html = load_html("pl.html")
56
+
57
+ html = protein_ligand_html.replace("%%%PDB%%%", protein)
58
+ html = html.replace("%%%SDF%%%", ligand)
59
+
60
+ return f"""<iframe style="width: 100%; height: 600px" name="result" allow="midi; geolocation; microphone; camera;
61
+ display-capture; encrypted-media;" sandbox="allow-modals allow-forms
62
+ allow-scripts allow-same-origin allow-popups
63
+ allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
64
+ allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
65
+
66
+ demo = gr.Blocks()
67
+
68
+ with demo:
69
+ gr.Markdown("# Protein and Ligand")
70
+ with gr.Row():
71
+ with gr.Box():
72
+ pfile = gr.File(file_count="single")
73
+ pbtn = gr.Button("View")
74
+
75
+ protein = gr.HTML()
76
+ pbtn.click(fn=protein_html_from_file, inputs=[pfile], outputs=protein)
77
+
78
+ with gr.Box():
79
+ lfile = gr.File(file_count="single")
80
+ lbtn = gr.Button("View")
81
+
82
+ ligand = gr.HTML()
83
+ lbtn.click(fn=ligand_html_from_file, inputs=[lfile], outputs=ligand)
84
+
85
+ with gr.Row():
86
+ gr.Markdown("# Protein-Ligand Complex")
87
+ plcomplex = gr.HTML()
88
+
89
+ # TODO: Automatically display complex when both files are uploaded
90
+ plbtn = gr.Button("View")
91
+ plbtn.click(fn=protein_ligand_html_from_file, inputs=[pfile, lfile], outputs=plcomplex)
92
+
93
+ demo.launch()
conda.yml ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: gt-hf
2
+ channels:
3
+ - conda-forge
4
+ dependencies:
5
+ - python
6
+ - pip
7
+
8
+ - ipython
9
+
10
+ - pip:
11
+ - gninatorch
12
+ - gradio
html/ligand.html ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+
4
+ <head>
5
+ <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
6
+ <style>
7
+ .mol-container {
8
+ width: 100%;
9
+ height: 700px;
10
+ position: relative;
11
+ }
12
+
13
+ .mol-container select {
14
+ background-image: None;
15
+ }
16
+ </style>
17
+ <script src="https://3Dmol.csb.pitt.edu/build/3Dmol-min.js"></script>
18
+ </head>
19
+
20
+ <body>
21
+ <div id="container" class="mol-container"></div>
22
+ <script>
23
+ let sdf = `%%%SDF%%%`;
24
+ $(document).ready(function () {
25
+ let element = $("#container");
26
+ let config = { backgroundColor: "white" };
27
+ let viewer = $3Dmol.createViewer(element, config);
28
+ viewer.addModel(sdf, "sdf");
29
+ viewer.getModel(0).setStyle({}, { "stick": { "colorscheme": "lightgreyCarbon" } });
30
+ viewer.zoomTo();
31
+ viewer.render();
32
+ viewer.zoom(0.8, 2000);
33
+ })
34
+ </script>
35
+ </body>
36
+ </html>
html/pl.html ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ <!DOCTYPE html>
3
+ <html>
4
+ <head>
5
+ <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
6
+ <style>
7
+ .mol-container {
8
+ width: 100%;
9
+ height: 700px;
10
+ position: relative;
11
+ }
12
+
13
+ .mol-container select {
14
+ background-image: None;
15
+ }
16
+ </style>
17
+ <script src="https://3Dmol.csb.pitt.edu/build/3Dmol-min.js"></script>
18
+ </head>
19
+
20
+ <body>
21
+ <div id="container" class="mol-container"></div>
22
+ <script>
23
+ let sdf = `%%%SDF%%%`;
24
+ let pdb = `%%%PDB%%%`;
25
+ $(document).ready(function () {
26
+ let element = $("#container");
27
+ let config = { backgroundColor: "white" };
28
+ let viewer = $3Dmol.createViewer(element, config);
29
+ viewer.addModel(pdb, "pdb");
30
+ viewer.addModel(sdf, "sdf");
31
+ viewer.getModel(0).setStyle({}, { cartoon: { colorscheme: "whiteCarbon" } });
32
+ viewer.getModel(1).setStyle({}, { stick: { colorscheme: "lightgreyCarbon" } });
33
+ viewer.zoomTo();
34
+ viewer.render();
35
+ viewer.zoom(0.8, 2000);
36
+ })
37
+ </script>
38
+ </body>
39
+ </html>
html/protein.html ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ <!DOCTYPE html>
3
+ <html>
4
+ <head>
5
+ <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
6
+ <style>
7
+ .mol-container {
8
+ width: 100%;
9
+ height: 700px;
10
+ position: relative;
11
+ }
12
+
13
+ .mol-container select {
14
+ background-image: None;
15
+ }
16
+ </style>
17
+ <script src="https://3Dmol.csb.pitt.edu/build/3Dmol-min.js"></script>
18
+ </head>
19
+
20
+ <body>
21
+ <div id="container" class="mol-container"></div>
22
+ <script>
23
+ let pdb = `%%%PDB%%%`;
24
+ $(document).ready(function () {
25
+ let element = $("#container");
26
+ let config = { backgroundColor: "white" };
27
+ let viewer = $3Dmol.createViewer(element, config);
28
+ viewer.addModel(pdb, "pdb");
29
+ viewer.getModel(0).setStyle({}, { cartoon: { colorscheme: "whiteCarbon" } });
30
+ viewer.zoomTo();
31
+ viewer.render();
32
+ viewer.zoom(0.8, 2000);
33
+ })
34
+ </script>
35
+ </body>
36
+ </html>