Update demo/modules/blocks.py
Browse files- demo/modules/blocks.py +66 -66
demo/modules/blocks.py
CHANGED
@@ -1,66 +1,66 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
|
3 |
-
from utils.foldseek_util import get_struc_seq
|
4 |
-
|
5 |
-
|
6 |
-
####################################################
|
7 |
-
# gradio blocks #
|
8 |
-
####################################################
|
9 |
-
def upload_pdb_button(visible: bool = True, chain_visible: bool = True):
|
10 |
-
"""
|
11 |
-
Provide an upload button to upload a pdb file
|
12 |
-
Args:
|
13 |
-
visible: Whether the block is visible or not
|
14 |
-
"""
|
15 |
-
|
16 |
-
with gr.Column(scale=0):
|
17 |
-
|
18 |
-
# Which chain to be extracted
|
19 |
-
chain_box = gr.Textbox(label="Chain (to be extracted from the pdb file)", value="A",
|
20 |
-
visible=chain_visible, interactive=True)
|
21 |
-
|
22 |
-
upload_btn = gr.UploadButton(label="Upload .pdb/.cif file", visible=visible)
|
23 |
-
|
24 |
-
return upload_btn, chain_box
|
25 |
-
|
26 |
-
|
27 |
-
####################################################
|
28 |
-
# Trigger functions #
|
29 |
-
####################################################
|
30 |
-
def parse_pdb_file(input_type: str, file: str, chain: str) -> str:
|
31 |
-
"""
|
32 |
-
Parse the uploaded structure file
|
33 |
-
|
34 |
-
Args:
|
35 |
-
input_type: Type of input. Must be one of ["protein sequence", "protein structure"]
|
36 |
-
|
37 |
-
file: Path to the uploaded file
|
38 |
-
|
39 |
-
chain: Chain to be extracted from the pdb file
|
40 |
-
|
41 |
-
Returns:
|
42 |
-
Protein sequence or Foldseek sequence
|
43 |
-
"""
|
44 |
-
try:
|
45 |
-
parsed_seqs = get_struc_seq("/tmp/foldseek", file, [chain])[chain]
|
46 |
-
if input_type == "sequence":
|
47 |
-
return parsed_seqs[0]
|
48 |
-
else:
|
49 |
-
return parsed_seqs[1].lower()
|
50 |
-
|
51 |
-
except Exception:
|
52 |
-
raise gr.Error(f"
|
53 |
-
|
54 |
-
|
55 |
-
def set_upload_visible(visible: bool) -> gr.Interface:
|
56 |
-
"""
|
57 |
-
Set the visibility of the upload button
|
58 |
-
|
59 |
-
Args:
|
60 |
-
visible: Whether the block is visible or not
|
61 |
-
|
62 |
-
Returns:
|
63 |
-
gr.Interface: Updated interface
|
64 |
-
"""
|
65 |
-
|
66 |
-
return gr.update(visible=visible)
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from utils.foldseek_util import get_struc_seq
|
4 |
+
|
5 |
+
|
6 |
+
####################################################
|
7 |
+
# gradio blocks #
|
8 |
+
####################################################
|
9 |
+
def upload_pdb_button(visible: bool = True, chain_visible: bool = True):
|
10 |
+
"""
|
11 |
+
Provide an upload button to upload a pdb file
|
12 |
+
Args:
|
13 |
+
visible: Whether the block is visible or not
|
14 |
+
"""
|
15 |
+
|
16 |
+
with gr.Column(scale=0):
|
17 |
+
|
18 |
+
# Which chain to be extracted
|
19 |
+
chain_box = gr.Textbox(label="Chain (to be extracted from the pdb file)", value="A",
|
20 |
+
visible=chain_visible, interactive=True)
|
21 |
+
|
22 |
+
upload_btn = gr.UploadButton(label="Upload .pdb/.cif file", visible=visible)
|
23 |
+
|
24 |
+
return upload_btn, chain_box
|
25 |
+
|
26 |
+
|
27 |
+
####################################################
|
28 |
+
# Trigger functions #
|
29 |
+
####################################################
|
30 |
+
def parse_pdb_file(input_type: str, file: str, chain: str) -> str:
|
31 |
+
"""
|
32 |
+
Parse the uploaded structure file
|
33 |
+
|
34 |
+
Args:
|
35 |
+
input_type: Type of input. Must be one of ["protein sequence", "protein structure"]
|
36 |
+
|
37 |
+
file: Path to the uploaded file
|
38 |
+
|
39 |
+
chain: Chain to be extracted from the pdb file
|
40 |
+
|
41 |
+
Returns:
|
42 |
+
Protein sequence or Foldseek sequence
|
43 |
+
"""
|
44 |
+
try:
|
45 |
+
parsed_seqs = get_struc_seq("/tmp/foldseek", file, [chain])[chain]
|
46 |
+
if input_type == "sequence":
|
47 |
+
return parsed_seqs[0]
|
48 |
+
else:
|
49 |
+
return parsed_seqs[1].lower()
|
50 |
+
|
51 |
+
except Exception as e:
|
52 |
+
raise gr.Error(f"{e}")
|
53 |
+
|
54 |
+
|
55 |
+
def set_upload_visible(visible: bool) -> gr.Interface:
|
56 |
+
"""
|
57 |
+
Set the visibility of the upload button
|
58 |
+
|
59 |
+
Args:
|
60 |
+
visible: Whether the block is visible or not
|
61 |
+
|
62 |
+
Returns:
|
63 |
+
gr.Interface: Updated interface
|
64 |
+
"""
|
65 |
+
|
66 |
+
return gr.update(visible=visible)
|