Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from igfold import IgFoldRunner
|
3 |
import os
|
@@ -5,177 +8,114 @@ import random
|
|
5 |
import base64
|
6 |
import socket
|
7 |
import re
|
8 |
-
|
9 |
|
10 |
def read_mol(molpath):
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
|
19 |
def molecule(input_pdb, h_seq, l_seq):
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
height: 600px;
|
37 |
-
position: relative;
|
38 |
-
}
|
39 |
-
.mol-container select{
|
40 |
-
background-image:None;
|
41 |
-
}
|
42 |
-
</style>
|
43 |
-
<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>
|
44 |
-
<script src="https://3Dmol.csb.pitt.edu/build/3Dmol-min.js"></script>
|
45 |
-
</head>
|
46 |
-
<body>
|
47 |
-
<div id="container" class="mol-container"></div>
|
48 |
-
|
49 |
<script>
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
return f"""<iframe style="width: 100%; height: 600px" name="result" allow="midi; geolocation; microphone; camera;
|
79 |
-
display-capture; encrypted-media;" sandbox="allow-modals allow-forms
|
80 |
-
allow-scripts allow-same-origin allow-popups
|
81 |
-
allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
|
82 |
-
allowpaymentrequest="" frameborder="0" srcdoc='{x}'></iframe>
|
83 |
-
<div style="position: absolute; top: 10px; right: 10px; background-color: white; padding: 10px; border: 1px solid black;">
|
84 |
-
<div style="width: 20px; height: 20px; background-color: red; display: inline-block;"></div>
|
85 |
-
<span style="margin-left: 3px;">Heavy chain</span><br>
|
86 |
-
<div style="width: 20px; height: 20px; background-color: blue; display: inline-block;"></div>
|
87 |
-
<span style="margin-left: 3px;">Light chain</span>
|
88 |
-
<div style="display: flex; justify-content: space-between; margin-top: 5px;">
|
89 |
-
<a href="data:application/octet-stream;base64,{base64_content}" download="structure.pdb">Download File</a>
|
90 |
-
</div>
|
91 |
-
</div>
|
92 |
-
"""
|
93 |
|
94 |
def validate(seq):
|
95 |
alphabet = set('ACDEFGHIKLMNPQRSTVWY')
|
96 |
-
|
97 |
-
return not leftover
|
98 |
|
99 |
def clean_sequence(seq):
|
100 |
return re.sub(r'\s+', '', seq)
|
101 |
|
102 |
def pred_seq(h_seq, l_seq):
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
-
print("Logs","___"*10)
|
107 |
-
print(f"Heavy chain: {h_seq}")
|
108 |
-
print(f"Light chain: {l_seq}")
|
109 |
-
hostname = socket.gethostname()
|
110 |
-
ip_address = socket.gethostbyname(hostname)
|
111 |
-
print(f"Hostname: {hostname}")
|
112 |
-
print(f"IP Address: {ip_address}")
|
113 |
-
print("___"*10)
|
114 |
-
|
115 |
-
h_is_valid = validate(h_seq)
|
116 |
-
l_is_valid = validate(l_seq)
|
117 |
-
if h_is_valid and l_is_valid:
|
118 |
-
sequences = {
|
119 |
-
"H": h_seq,
|
120 |
-
"L": l_seq
|
121 |
-
}
|
122 |
-
|
123 |
-
f_name = ''.join([random.choice("ACDEFGHIKLMNPQRSTVWY") for _ in range(15)])
|
124 |
-
|
125 |
-
pred_pdb = f"{f_name}.pdb"
|
126 |
-
|
127 |
-
igfold = IgFoldRunner(num_models = 1)
|
128 |
-
igfold.fold(
|
129 |
-
pred_pdb,
|
130 |
-
sequences=sequences,
|
131 |
-
do_refine=False,
|
132 |
-
do_renum=False
|
133 |
-
)
|
134 |
-
|
135 |
-
html = molecule(pred_pdb, h_seq, l_seq)
|
136 |
-
|
137 |
-
else:
|
138 |
-
html = "<p>ERROR! Not valid sequence. Please use only standard amino acid letters (ACDEFGHIKLMNPQRSTVWY)</p>"
|
139 |
-
return html
|
140 |
-
|
141 |
-
# Примеры последовательностей
|
142 |
examples = [
|
143 |
-
[
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
[
|
148 |
-
"EVQLLESGGGLVQPGGSLRLSCAASGFTFSLYWMGWVRQAPGKGLEWVSSISSSGGVTPYADSVKGRFTISRDNSKNTLYLQMNSLRAEDTAVYYCAKLGELGWFDPWGQGTLVTVSS",
|
149 |
-
"DIQMTQSPSSLSASVGDRVTITCRASQGISSYLNWYQQKPGKAPKLLIYYASNLQNGVPSRFSGSGSGTDFTLTISSLQPEDFATYYCQQSYSTPLTFGGGTKVEIK"
|
150 |
-
],
|
151 |
-
[
|
152 |
-
"EVQLVQSGPEVKKPGTSVKVSCKASGFTFMSSAVQWVRQARGQRLEWIGWIVIGSGNTNYAQKFQERVTITRDMSTSTAYMELSSLRSEDTAVYYCAAPYCSSISCNDGFDIWGQGTMVTVS",
|
153 |
-
"EIVLTQSPATLSLSPGERATLSCRASQSVSSYLAWYQQKPGQAPRLLIYDASNRATGIPARFSGSGSGTDFTLTISSLEPEDFAVYYCQQRSNWPITFGQGTKLEIK"
|
154 |
-
]
|
155 |
]
|
156 |
|
157 |
with gr.Blocks() as demo:
|
158 |
gr.Markdown('# Antibody Structure Prediction')
|
159 |
-
gr.Markdown("## Examples")
|
160 |
-
|
161 |
with gr.Row():
|
162 |
-
h_text = gr.Textbox(lines=5, label="Heavy chain",
|
163 |
-
|
164 |
-
l_text = gr.Textbox(lines=5, label="Light chain",
|
165 |
-
placeholder="Enter light chain sequence (e.g. DVVMTQTPL...)")
|
166 |
-
|
167 |
-
# Создаем примеры с помощью gr.Examples
|
168 |
-
gr.Examples(
|
169 |
-
examples=examples,
|
170 |
-
inputs=[h_text, l_text],
|
171 |
-
label="Click on any example to paste it into the input fields",
|
172 |
-
fn=pred_seq,
|
173 |
-
outputs=gr.HTML()
|
174 |
-
)
|
175 |
|
176 |
-
|
|
|
177 |
output_html = gr.HTML()
|
178 |
btn.click(pred_seq, inputs=[h_text, l_text], outputs=output_html)
|
179 |
|
180 |
if __name__ == "__main__":
|
181 |
-
demo.launch()
|
|
|
1 |
+
import csv
|
2 |
+
csv.field_size_limit(131072 * 10)
|
3 |
+
|
4 |
import gradio as gr
|
5 |
from igfold import IgFoldRunner
|
6 |
import os
|
|
|
8 |
import base64
|
9 |
import socket
|
10 |
import re
|
11 |
+
from pathlib import Path
|
12 |
|
13 |
def read_mol(molpath):
|
14 |
+
try:
|
15 |
+
with open(molpath, "r") as fp:
|
16 |
+
return fp.read()
|
17 |
+
except Exception as e:
|
18 |
+
print(f"Error reading PDB file: {e}")
|
19 |
+
return ""
|
|
|
20 |
|
21 |
def molecule(input_pdb, h_seq, l_seq):
|
22 |
+
try:
|
23 |
+
mol = read_mol(input_pdb)
|
24 |
+
if not mol:
|
25 |
+
return "<p>Error: Failed to read PDB file</p>"
|
26 |
+
|
27 |
+
byte_content = mol.encode('utf-8')
|
28 |
+
base64_content = base64.b64encode(byte_content).decode('utf-8')
|
29 |
+
|
30 |
+
x = f"""<!DOCTYPE html><html><head>
|
31 |
+
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
|
32 |
+
<style>body{{font-family:sans-serif}}
|
33 |
+
.mol-container {{width: 100%; height: 600px; position: relative;}}
|
34 |
+
.mol-container select{{background-image:None;}}</style>
|
35 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
|
36 |
+
<script src="https://3Dmol.csb.pitt.edu/build/3Dmol-min.js"></script>
|
37 |
+
</head><body><div id="container" class="mol-container"></div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
<script>
|
39 |
+
$(document).ready(function () {{
|
40 |
+
let element = $("#container");
|
41 |
+
let config = {{ backgroundColor: "white" }};
|
42 |
+
let viewer = $3Dmol.createViewer(element, config);
|
43 |
+
viewer.addModel(`{mol}`, "pdb");
|
44 |
+
viewer.getModel(0).setStyle({{chain: "H"}}, {{cartoon:{{color:"red"}}}});
|
45 |
+
viewer.getModel(0).setStyle({{chain: "L"}}, {{cartoon:{{color:"blue"}}}});
|
46 |
+
viewer.addSurface($3Dmol.SurfaceType.VDW, {{opacity: 0.4, color: "lightblue"}});
|
47 |
+
viewer.zoomTo();
|
48 |
+
viewer.render();
|
49 |
+
viewer.zoom(0.8, 2000);
|
50 |
+
}})
|
51 |
+
</script></body></html>"""
|
52 |
+
|
53 |
+
return f"""<iframe style="width: 100%; height: 600px" srcdoc='{x}'></iframe>
|
54 |
+
<div style="position: absolute; top: 10px; right: 10px; background-color: white; padding: 10px; border: 1px solid black;">
|
55 |
+
<div style="width: 20px; height: 20px; background-color: red; display: inline-block;"></div>
|
56 |
+
<span>Heavy chain</span><br>
|
57 |
+
<div style="width: 20px; height: 20px; background-color: blue; display: inline-block;"></div>
|
58 |
+
<span>Light chain</span>
|
59 |
+
<div style="margin-top: 5px;">
|
60 |
+
<a href="data:application/octet-stream;base64,{base64_content}" download="structure.pdb">Download PDB</a>
|
61 |
+
</div>
|
62 |
+
</div>"""
|
63 |
+
except Exception as e:
|
64 |
+
print(f"Error in molecule visualization: {e}")
|
65 |
+
return f"<p>Error generating visualization: {str(e)}</p>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
def validate(seq):
|
68 |
alphabet = set('ACDEFGHIKLMNPQRSTVWY')
|
69 |
+
return not (set(seq.upper()) - alphabet)
|
|
|
70 |
|
71 |
def clean_sequence(seq):
|
72 |
return re.sub(r'\s+', '', seq)
|
73 |
|
74 |
def pred_seq(h_seq, l_seq):
|
75 |
+
try:
|
76 |
+
h_seq = clean_sequence(h_seq).upper()
|
77 |
+
l_seq = clean_sequence(l_seq).upper()
|
78 |
+
|
79 |
+
if not (validate(h_seq) and validate(l_seq)):
|
80 |
+
return "<p>Error: Invalid amino acid characters detected</p>"
|
81 |
+
|
82 |
+
sequences = {"H": h_seq, "L": l_seq}
|
83 |
+
f_name = f"temp_{random.randint(0, 100000)}.pdb"
|
84 |
+
|
85 |
+
try:
|
86 |
+
igfold = IgFoldRunner(num_models=1)
|
87 |
+
igfold.fold(f_name, sequences=sequences, do_refine=False, do_renum=False)
|
88 |
+
|
89 |
+
if not os.path.exists(f_name):
|
90 |
+
return "<p>Error: Failed to generate PDB file</p>"
|
91 |
+
|
92 |
+
html = molecule(f_name, h_seq, l_seq)
|
93 |
+
finally:
|
94 |
+
if os.path.exists(f_name):
|
95 |
+
os.remove(f_name)
|
96 |
+
|
97 |
+
return html
|
98 |
+
except Exception as e:
|
99 |
+
print(f"Error in prediction: {e}")
|
100 |
+
return f"<p>Error: {str(e)}</p>"
|
101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
examples = [
|
103 |
+
["QVQLKESGPGLVAPSQSLSITCTVSGFSLSSYGVSWVRQPPGKGLEWLGVIWGDGSTNYHPNLMSRLSISKDISKSQVLFKLNSLQTDDTATYYCVTLDYWGQGTSVTVSS",
|
104 |
+
"DVVMTQTPLSLPVSLGDQASISCRSSQSLVHRNGNTYLHWYLQKPGQSPKLLIYKVSNRFSGVPDRFSGSGSGTDFTLKISRVEAEDLGLYFCFQTTYVPNTFGGGTKLEIK"],
|
105 |
+
["EVQLLESGGGLVQPGGSLRLSCAASGFTFSLYWMGWVRQAPGKGLEWVSSISSSGGVTPYADSVKGRFTISRDNSKNTLYLQMNSLRAEDTAVYYCAKLGELGWFDPWGQGTLVTVSS",
|
106 |
+
"DIQMTQSPSSLSASVGDRVTITCRASQGISSYLNWYQQKPGKAPKLLIYYASNLQNGVPSRFSGSGSGTDFTLTISSLQPEDFATYYCQQSYSTPLTFGGGTKVEIK"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
]
|
108 |
|
109 |
with gr.Blocks() as demo:
|
110 |
gr.Markdown('# Antibody Structure Prediction')
|
|
|
|
|
111 |
with gr.Row():
|
112 |
+
h_text = gr.Textbox(lines=5, label="Heavy chain", placeholder="Enter heavy chain sequence...")
|
113 |
+
l_text = gr.Textbox(lines=5, label="Light chain", placeholder="Enter light chain sequence...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
+
gr.Examples(examples=examples, inputs=[h_text, l_text], label="Example sequences")
|
116 |
+
btn = gr.Button("Predict Structure")
|
117 |
output_html = gr.HTML()
|
118 |
btn.click(pred_seq, inputs=[h_text, l_text], outputs=output_html)
|
119 |
|
120 |
if __name__ == "__main__":
|
121 |
+
demo.launch(show_error=True, server_name="0.0.0.0")
|