Spaces:
Sleeping
Sleeping
improved code
Browse files
app.py
CHANGED
@@ -5,6 +5,9 @@ from io import StringIO
|
|
5 |
|
6 |
# Simulates mutation detection
|
7 |
def analyze_sequences(input_df):
|
|
|
|
|
|
|
8 |
results = []
|
9 |
for _, row in input_df.iterrows():
|
10 |
seq = row['DNA_Sequence']
|
@@ -18,22 +21,27 @@ def analyze_sequences(input_df):
|
|
18 |
})
|
19 |
return pd.DataFrame(results)
|
20 |
|
21 |
-
#
|
22 |
def load_example_data():
|
23 |
-
|
24 |
-
"DNA_Sequence": [
|
|
|
|
|
|
|
|
|
|
|
25 |
})
|
26 |
-
return analyze_sequences(
|
27 |
|
28 |
-
#
|
29 |
-
def dataframe_to_csv(
|
30 |
-
if
|
31 |
return ""
|
32 |
csv_buffer = StringIO()
|
33 |
-
|
34 |
return csv_buffer.getvalue()
|
35 |
|
36 |
-
# Generate
|
37 |
def get_mutation_stats(result_df):
|
38 |
if result_df is None or result_df.empty:
|
39 |
return "No data available.", None
|
@@ -58,12 +66,27 @@ def get_mutation_stats(result_df):
|
|
58 |
|
59 |
return summary_text, chart
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
# Gradio Interface
|
62 |
-
with gr.Blocks() as demo:
|
63 |
-
gr.Markdown("
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
with gr.Row():
|
67 |
upload_btn = gr.File(label="π Upload CSV File", file_types=[".csv"])
|
68 |
example_btn = gr.Button("π§ͺ Load Example Data")
|
69 |
|
@@ -72,20 +95,12 @@ with gr.Blocks() as demo:
|
|
72 |
headers=["Sequence", "Predicted Mutation", "Confidence Score"]
|
73 |
)
|
74 |
|
75 |
-
download_btn = gr.File(label="β¬οΈ Download Results as CSV")
|
76 |
-
|
77 |
stats_text = gr.Textbox(label="Mutation Statistics Summary")
|
78 |
stats_chart = gr.Plot(label="Mutation Frequency Chart")
|
79 |
|
80 |
-
|
81 |
-
def process_and_get_stats(file=None):
|
82 |
-
if file is not None:
|
83 |
-
result_df = analyze_sequences(file)
|
84 |
-
else:
|
85 |
-
result_df = load_example_data()
|
86 |
-
summary, chart = get_mutation_stats(result_df)
|
87 |
-
return result_df, summary, chart
|
88 |
|
|
|
89 |
upload_btn.upload(fn=process_and_get_stats, inputs=upload_btn, outputs=[output_table, stats_text, stats_chart])
|
90 |
example_btn.click(fn=process_and_get_stats, inputs=None, outputs=[output_table, stats_text, stats_chart])
|
91 |
download_btn.upload(fn=dataframe_to_csv, inputs=output_table, outputs=download_btn)
|
|
|
5 |
|
6 |
# Simulates mutation detection
|
7 |
def analyze_sequences(input_df):
|
8 |
+
if input_df is None or input_df.empty:
|
9 |
+
return pd.DataFrame(columns=["Sequence", "Predicted Mutation", "Confidence Score"])
|
10 |
+
|
11 |
results = []
|
12 |
for _, row in input_df.iterrows():
|
13 |
seq = row['DNA_Sequence']
|
|
|
21 |
})
|
22 |
return pd.DataFrame(results)
|
23 |
|
24 |
+
# Loads example data and analyzes it
|
25 |
def load_example_data():
|
26 |
+
example_df = pd.DataFrame({
|
27 |
+
"DNA_Sequence": [
|
28 |
+
"AGCTAGCTA",
|
29 |
+
"GATCGATCG",
|
30 |
+
"TTAGCTAGCT",
|
31 |
+
"ATGCGTAGC"
|
32 |
+
]
|
33 |
})
|
34 |
+
return analyze_sequences(example_df)
|
35 |
|
36 |
+
# Converts DataFrame to CSV string
|
37 |
+
def dataframe_to_csv(df):
|
38 |
+
if df is None or df.empty:
|
39 |
return ""
|
40 |
csv_buffer = StringIO()
|
41 |
+
df.to_csv(csv_buffer, index=False)
|
42 |
return csv_buffer.getvalue()
|
43 |
|
44 |
+
# Generate mutation statistics summary and chart
|
45 |
def get_mutation_stats(result_df):
|
46 |
if result_df is None or result_df.empty:
|
47 |
return "No data available.", None
|
|
|
66 |
|
67 |
return summary_text, chart
|
68 |
|
69 |
+
# Unified function to process and return all outputs
|
70 |
+
def process_and_get_stats(file=None):
|
71 |
+
if file is not None:
|
72 |
+
result_df = analyze_sequences(file)
|
73 |
+
else:
|
74 |
+
result_df = load_example_data()
|
75 |
+
|
76 |
+
summary, chart = get_mutation_stats(result_df)
|
77 |
+
return result_df, summary, chart
|
78 |
+
|
79 |
# Gradio Interface
|
80 |
+
with gr.Blocks(theme="default") as demo:
|
81 |
+
gr.Markdown("""
|
82 |
+
# 𧬠MutateX β Liquid Biopsy Mutation Detection Tool
|
83 |
+
|
84 |
+
Upload a CSV file containing DNA sequences or use the example data to simulate mutation detection.
|
85 |
+
|
86 |
+
*Developed by [GradSyntax](https://www.gradsyntax.com )*
|
87 |
+
""")
|
88 |
|
89 |
+
with gr.Row(equal_height=True):
|
90 |
upload_btn = gr.File(label="π Upload CSV File", file_types=[".csv"])
|
91 |
example_btn = gr.Button("π§ͺ Load Example Data")
|
92 |
|
|
|
95 |
headers=["Sequence", "Predicted Mutation", "Confidence Score"]
|
96 |
)
|
97 |
|
|
|
|
|
98 |
stats_text = gr.Textbox(label="Mutation Statistics Summary")
|
99 |
stats_chart = gr.Plot(label="Mutation Frequency Chart")
|
100 |
|
101 |
+
download_btn = gr.File(label="β¬οΈ Download Results as CSV")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
+
# Function calls
|
104 |
upload_btn.upload(fn=process_and_get_stats, inputs=upload_btn, outputs=[output_table, stats_text, stats_chart])
|
105 |
example_btn.click(fn=process_and_get_stats, inputs=None, outputs=[output_table, stats_text, stats_chart])
|
106 |
download_btn.upload(fn=dataframe_to_csv, inputs=output_table, outputs=download_btn)
|