huhlim commited on
Commit
fb9e841
1 Parent(s): f470b2d

added 3Dmol viewer to app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -16
app.py CHANGED
@@ -2,23 +2,134 @@ import gradio as gr
2
  import cg2all
3
  import os
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  def runner(in_pdb, model_type):
6
  out_fn = in_pdb.name[:-4] + "-all.pdb"
7
  ckpt_fn = f"model/{model_type}.ckpt"
8
  cg2all.convert_cg2all(in_pdb.name, out_fn, model_type=model_type, ckpt_fn=ckpt_fn)
9
- return out_fn
10
-
11
- demo = gr.Interface(fn=runner,
12
- inputs=["file", \
13
- gr.Radio(["CalphaBasedModel", "ResidueBasedModel", "CalphaCMModel", "BackboneModel",
14
- "MainchainModel", "Martini", "PRIMO"])],
15
- outputs=["file"],
16
- examples=[["inputs/1ab1_A.calpha.pdb", "CalphaBasedModel"],
17
- ["inputs/1ab1_A.residue.pdb", "ResidueBasedModel"],
18
- ["inputs/1ab1_A.cacm.pdb", "CalphaCMModel"],
19
- ["inputs/1ab1_A.bb.pdb", "BackboneModel"],
20
- ["inputs/1ab1_A.mc.pdb", "MainchainModel"],
21
- ["inputs/1ab1_A.martini.pdb", "Martini"],
22
- ["inputs/1ab1_A.primo.pdb", "PRIMO"],
23
- ])
24
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import cg2all
3
  import os
4
 
5
+
6
+ def read_mol(molpath):
7
+ with open(molpath, "r") as fp:
8
+ lines = fp.readlines()
9
+ mol = ""
10
+ for l in lines:
11
+ mol += l
12
+ #
13
+ mol = mol.replace("OT1", "O ")
14
+ mol = mol.replace("OT2", "OXT")
15
+ return mol
16
+
17
+
18
+ def molecule(input_pdb):
19
+ mol = read_mol(input_pdb)
20
+ x = (
21
+ """<!DOCTYPE html>
22
+ <html>
23
+ <head>
24
+ <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
25
+ <style>
26
+ body{
27
+ font-family:sans-serif
28
+ }
29
+ .mol-container {
30
+ width: 100%;
31
+ height: 600px;
32
+ position: relative;
33
+ }
34
+ .mol-container select{
35
+ background-image:None;
36
+ }
37
+ </style>
38
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js" integrity="sha512-STof4xm1wgkfm7heWqFJVn58Hm3EtS31XFaagaa8VMReCXAkQnJZ+jEy8PCC/iT18dFy95WcExNHFTqLyp72eQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
39
+ <script src="https://3Dmol.csb.pitt.edu/build/3Dmol-min.js"></script>
40
+ </head>
41
+ <body>
42
+
43
+ <div id="container" class="mol-container"></div>
44
+
45
+ <script>
46
+ let pdb = `"""
47
+ + mol
48
+ + """`
49
+
50
+ $(document).ready(function () {
51
+ let element = $("#container");
52
+ let config = { backgroundColor: "white" };
53
+ let viewer = $3Dmol.createViewer(element, config);
54
+ viewer.addModel(pdb, "pdb");
55
+ viewer.setStyle({}, {cartoon: { color:"spectrum"}});
56
+ viewer.addStyle({"and":[{resn:["GLY","PRO"], invert:true}, {atom:["N","C","O"], invert:true}]}, {stick: {radius:0.2, colorscheme:"WhiteCarbon"}});
57
+ viewer.addStyle({"and":[{resn:"GLY"}, {atom:"CA"}]}, {stick: {radius:0.2, colorscheme:"WhiteCarbon"}});
58
+ viewer.addStyle({"and":[{resn:"PRO"}, {atom:["C","O"], invert:true}]}, {stick: {radius:0.2, 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: 100%; height: 600px" 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
+
74
  def runner(in_pdb, model_type):
75
  out_fn = in_pdb.name[:-4] + "-all.pdb"
76
  ckpt_fn = f"model/{model_type}.ckpt"
77
  cg2all.convert_cg2all(in_pdb.name, out_fn, model_type=model_type, ckpt_fn=ckpt_fn)
78
+ view = molecule(out_fn)
79
+ return out_fn, view
80
+
81
+
82
+ with gr.Blocks() as app:
83
+ gr.Markdown("# cg2all: conversion of coarse-grained protein structure model to all-atom structure")
84
+ with gr.Row():
85
+ with gr.Column():
86
+ input_pdb = gr.File(file_count="single", label="Input CG structure")
87
+ model_type = gr.Radio(
88
+ [
89
+ "CalphaBasedModel",
90
+ "ResidueBasedModel",
91
+ "CalphaCMModel",
92
+ "BackboneModel",
93
+ "MainchainModel",
94
+ "Martini",
95
+ "PRIMO",
96
+ ],
97
+ label="Input CG model type",
98
+ )
99
+ #
100
+ button = gr.Button("Run")
101
+ #
102
+ gr.Examples(
103
+ [
104
+ ["inputs/1ab1_A.calpha.pdb", "CalphaBasedModel"],
105
+ ["inputs/1ab1_A.residue.pdb", "ResidueBasedModel"],
106
+ ["inputs/1ab1_A.cacm.pdb", "CalphaCMModel"],
107
+ ["inputs/1ab1_A.bb.pdb", "BackboneModel"],
108
+ ["inputs/1ab1_A.mc.pdb", "MainchainModel"],
109
+ ["inputs/1ab1_A.martini.pdb", "Martini"],
110
+ ["inputs/1ab1_A.primo.pdb", "PRIMO"],
111
+ ],
112
+ [input_pdb, model_type],
113
+ )
114
+
115
+ with gr.Column():
116
+ output_pdb = gr.File(file_count="single", label="Output structure")
117
+ viewer = gr.HTML()
118
+
119
+ button.click(fn=runner, inputs=[input_pdb, model_type], outputs=[output_pdb, viewer])
120
+ #
121
+ gr.Markdown("---")
122
+ gr.Markdown("### GitHub repository: [https://github.com/huhlim/cg2all](https://github.com/huhlim/cg2all)")
123
+ gr.Markdown("### Local installation: `pip install git+http://github.com/huhlim/cg2all`")
124
+ gr.Markdown("### Supported coarse-grained models")
125
+ gr.Markdown("- CalphaBasedModel: CA-trace")
126
+ gr.Markdown("- ResidueBasedModel: Residue center-of-mass")
127
+ gr.Markdown("- CalphaCMModel: CA-trace + Residue center-of-mass")
128
+ gr.Markdown("- BackboneModel: Backbone N, CA, and C atoms")
129
+ gr.Markdown("- MainchainModel: Backbone N, CA, C, and O atoms")
130
+ gr.Markdown("- Martini: [Martini model](http://cgmartini.nl)")
131
+ gr.Markdown("- PRIMO: [PRIMO model](https://dx.doi.org/10.1002/prot.22645)")
132
+
133
+ gr.Markdown("### Cite: TODO")
134
+
135
+ app.launch()