Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
import hashlib
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from io import StringIO
|
| 8 |
+
from usalign_runner import USalignRunner
|
| 9 |
+
from utils import (calculate_md5, get_TM_mat_from_df, get_cluster_z_from_df, get_newick_str_from_Z,
|
| 10 |
+
build_graph_from_mat_df, fill_community_to_graph, get_graph_fig, run_community_analysis, run_usalign, save_pdb_files)
|
| 11 |
+
import matplotlib.pyplot as plt
|
| 12 |
+
os.environ['GRADIO_ANALYTICS_ENABLED'] = 'False'
|
| 13 |
+
|
| 14 |
+
# Create Gradio interface
|
| 15 |
+
with gr.Blocks() as demo:
|
| 16 |
+
gr.Markdown("# This is a Temp Title")
|
| 17 |
+
|
| 18 |
+
with gr.Row():
|
| 19 |
+
file_input = gr.File(
|
| 20 |
+
label="Upload PDB Files",
|
| 21 |
+
file_count="multiple",
|
| 22 |
+
file_types=[".pdb"],
|
| 23 |
+
height=200,
|
| 24 |
+
# lines=5, # 默认显示行数
|
| 25 |
+
# max_lines=10, # 最大可见行数(超过后自动滚动)
|
| 26 |
+
# container=True
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
output = gr.Textbox(label="Upload Results",
|
| 30 |
+
lines=5, # 默认显示行数
|
| 31 |
+
max_lines=5, # 最大可见行数(超过后自动滚动)
|
| 32 |
+
container=True )
|
| 33 |
+
|
| 34 |
+
threshold = gr.Slider(minimum=0, maximum=1, value=0.75, label="Threshold")
|
| 35 |
+
|
| 36 |
+
with gr.Row():
|
| 37 |
+
|
| 38 |
+
submit_btn = gr.Button("Upload Files")
|
| 39 |
+
run_usalign_btn = gr.Button("Run USalign")
|
| 40 |
+
community_btn = gr.Button("Run Community")
|
| 41 |
+
|
| 42 |
+
md5_hash = gr.State("")
|
| 43 |
+
with gr.Tab("USalign Results"):
|
| 44 |
+
results_df = gr.DataFrame(label="USalign Results",height=400,wrap=True,)
|
| 45 |
+
with gr.Tab("TM Matrix"):
|
| 46 |
+
# Add new output components for community analysis with height limits
|
| 47 |
+
tm_matrix_output = gr.DataFrame(label="TM Matrix",height=400,wrap=True,show_label=True)
|
| 48 |
+
with gr.Tab("Newick Tree"):
|
| 49 |
+
newick_output = gr.Textbox(label="Newick Tree",
|
| 50 |
+
lines=5, # 默认显示行数
|
| 51 |
+
max_lines=10, # 最大可见行数(超过后自动滚动)
|
| 52 |
+
container=True )
|
| 53 |
+
with gr.Tab("Structure Similarity Network"):
|
| 54 |
+
network_plot = gr.Plot(label="Structure Similarity Network")
|
| 55 |
+
|
| 56 |
+
# Combine download buttons into a single row
|
| 57 |
+
with gr.Row():
|
| 58 |
+
with gr.Column():
|
| 59 |
+
gr.Markdown("### Download Results")
|
| 60 |
+
download_tm = gr.File(label="Download Files")
|
| 61 |
+
|
| 62 |
+
submit_btn.click(
|
| 63 |
+
fn=save_pdb_files,
|
| 64 |
+
inputs=[file_input],
|
| 65 |
+
outputs=output
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
def update_md5_hash(files):
|
| 69 |
+
if files:
|
| 70 |
+
return calculate_md5(files)
|
| 71 |
+
return ""
|
| 72 |
+
|
| 73 |
+
file_input.change(
|
| 74 |
+
fn=update_md5_hash,
|
| 75 |
+
inputs=[file_input],
|
| 76 |
+
outputs=[md5_hash]
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
run_usalign_btn.click(
|
| 80 |
+
fn=run_usalign,
|
| 81 |
+
inputs=[md5_hash],
|
| 82 |
+
outputs=[results_df]
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
def process_community_analysis(results_df, md5_hash,threshold):
|
| 86 |
+
if results_df.empty:
|
| 87 |
+
return None, None, None, None, None, None
|
| 88 |
+
|
| 89 |
+
results = run_community_analysis(results_df, "./data", md5_hash,threshold)
|
| 90 |
+
|
| 91 |
+
if "Error" in results:
|
| 92 |
+
return None, None, None, None, None, None
|
| 93 |
+
|
| 94 |
+
# Prepare download files
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
return (
|
| 98 |
+
results["tm_matrix"],
|
| 99 |
+
results["newick_str"],
|
| 100 |
+
results["network_fig"],
|
| 101 |
+
results["files"]
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
community_btn.click(
|
| 105 |
+
fn=process_community_analysis,
|
| 106 |
+
inputs=[results_df, md5_hash,threshold],
|
| 107 |
+
outputs=[
|
| 108 |
+
tm_matrix_output,
|
| 109 |
+
newick_output,
|
| 110 |
+
network_plot,
|
| 111 |
+
download_tm,
|
| 112 |
+
]
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
if __name__ == "__main__":
|
| 116 |
+
demo.launch(server_name="0.0.0.0",server_port=7869)
|