File size: 3,449 Bytes
52da96f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc7f17f
52da96f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
import gradio as gr
import os

from .blocks import upload_pdb_button
from utils.downloader import download_pdb, download_af2


root_dir = __file__.rsplit("/", 3)[0]
structure_types = ["AlphaFoldDB", "PDB"]


def upload_structure(file: str):
    return file


def get_structure_path(structure: str, structure_type: str) -> str:
    # If the structure is manually uploaded
    if structure[0] == "/":
        return structure
    
    # If the structure is a Uniprot ID, download the structure from AlphaFoldDB
    elif structure_type == "AlphaFoldDB":
        save_path = f"{root_dir}/demo/cache/{structure}.pdb"
        if not os.path.exists(save_path):
            download_af2(structure, "pdb", save_path)
        return save_path
    
    # If the structure is a PDB ID, download the structure from PDB
    elif structure_type == "PDB":
        save_path = f"{root_dir}/demo/cache/{structure}.cif"
        if not os.path.exists(save_path):
            download_pdb(structure, "cif", save_path)
        return save_path
    
    
def tmalign(structure_1: str, structure_type_1: str, structure_2: str, structure_type_2: str):
    structure_path_1 = get_structure_path(structure_1, structure_type_1)
    structure_path_2 = get_structure_path(structure_2, structure_type_2)
    
    cmd = f"/tmp/TMalign {structure_path_1} {structure_path_2}"
    
    r = os.popen(cmd)
    text = r.read()
    return text


# Build the block for computing protein-text similarity
def build_TMalign():
    gr.Markdown(f"# Calculate TM-score between two protein structures")
    with gr.Row(equal_height=True):
        with gr.Column():
            # Compute similarity score between sequence and text
            with gr.Row():
                structure_1 = gr.Textbox(label="Protein structure 1 (input Uniprot ID or PDB ID or upload a pdb file)")
                
                structure_type_1 = gr.Dropdown(structure_types, label="Structure type (if the structure is manually uploaded, ignore this field)",
                                               value="AlphaFoldDB", interactive=True, visible=True)
    
                # Provide an upload button to upload a pdb file
                upload_btn_1, _ = upload_pdb_button(visible=True, chain_visible=False)
                upload_btn_1.upload(upload_structure, inputs=[upload_btn_1], outputs=[structure_1])
            
            with gr.Row():
                structure_2 = gr.Textbox(label="Protein structure 2 (input Uniprot ID or PDB ID or upload a pdb file)")
                
                structure_type_2 = gr.Dropdown(structure_types, label="Structure type (if the structure is manually uploaded, ignore this field)",
                                               value="AlphaFoldDB", interactive=True, visible=True)
                
                # Provide an upload button to upload a pdb file
                upload_btn_2, _ = upload_pdb_button(visible=True, chain_visible=False)
                upload_btn_2.upload(upload_structure, inputs=[upload_btn_2], outputs=[structure_2])
            
            compute_btn = gr.Button(value="Compute TM-score")
            tmscore = gr.TextArea(label="TM-score", interactive=False)
            
            compute_btn.click(tmalign, inputs=[structure_1, structure_type_1, structure_2, structure_type_2],
                              outputs=[tmscore])