osanseviero commited on
Commit
19b831b
·
1 Parent(s): 3f3335c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import requests
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
+ return mol
13
+
14
+
15
+ def molecule(input_pdb):
16
+
17
+ mol = read_mol(input_pdb)
18
+
19
+ x = (
20
+ """<!DOCTYPE html>
21
+ <html>
22
+ <head>
23
+ <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
24
+ <style>
25
+ body{
26
+ font-family:sans-serif
27
+ }
28
+ .mol-container {
29
+ width: 100%;
30
+ height: 600px;
31
+ position: relative;
32
+ }
33
+ .mol-container select{
34
+ background-image:None;
35
+ }
36
+ </style>
37
+ <script src="https://3Dmol.csb.pitt.edu/build/3Dmol-min.js"></script>
38
+ </head>
39
+ <body>
40
+ <div id="container" class="mol-container"></div>
41
+
42
+ <script>
43
+ let pdb = `"""
44
+ + mol
45
+ + """`
46
+
47
+ $(document).ready(function () {
48
+ let element = $("#container");
49
+ let config = { backgroundColor: "white" };
50
+ let viewer = $3Dmol.createViewer(element, config);
51
+ viewer.addModel(pdb, "pdb");
52
+ viewer.getModel(0).setStyle({}, { cartoon: { colorscheme:"whiteCarbon" } });
53
+ viewer.zoomTo();
54
+ viewer.render();
55
+ viewer.zoom(0.8, 2000);
56
+ })
57
+ </script>
58
+ </body></html>"""
59
+ )
60
+
61
+ return f"""<iframe style="width: 100%; height: 600px" name="result" allow="midi; geolocation; microphone; camera;
62
+ display-capture; encrypted-media;" sandbox="allow-modals allow-forms
63
+ allow-scripts allow-same-origin allow-popups
64
+ allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
65
+ allowpaymentrequest="" frameborder="0" srcdoc='{x}'></iframe>"""
66
+
67
+
68
+ def update(sequence):
69
+ headers = {
70
+ 'Content-Type': 'application/x-www-form-urlencoded',
71
+ }
72
+
73
+ response = requests.post('https://api.esmatlas.com/foldSequence/v1/pdb/', headers=headers, data=sequence)
74
+ name = sequence[:3] + sequence[-3:]
75
+ pdb_filename = "test.pdb"
76
+ pdb_string = response.content.decode('utf-8')
77
+ with open(pdb_filename, "w") as out:
78
+ out.write(pdb_string)
79
+
80
+ return molecule(pdb_filename)
81
+
82
+
83
+ demo = gr.Blocks()
84
+
85
+ with demo:
86
+ gr.Markdown("# PDB viewer using 3Dmol.js")
87
+ with gr.Row():
88
+ with gr.Box():
89
+ inp = gr.Textbox(label="Protein sequence"), gr.Examples(["GENGEIPLEIRATTGAEVDTRAVTAVEMTEGTLGIFRLPEEDYTALENFRYNRVAGENWKPASTVIYVGGTYARLCAYAPYNSVEFKNSSLKTEAGLTMQTYAAEKDMRFAVSGGDEVWKKTPTANFELKRAYARLVLSVVRDATYPNTCKITKAKIEAFTGNIITANTVDISTGTEGSGTQTPQYIHTVTTGLKDGFAIGLPQQTFSGGVVLTLTVDGMEYSVTIPANKLSTFVRGTKYIVSLAVKGGKLTLMSDKILIDKDWAEVQTGTGGSGDDYDTSFN"])
90
+ btn = gr.Button("Predict structure")
91
+ mol = gr.HTML()
92
+ btn.click(fn=update, inputs=inp, outputs=mol)
93
+ demo.launch()