Added mutation statistics summary and chart
Browse files
app.py
CHANGED
@@ -1,62 +1,93 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import random
|
|
|
4 |
|
5 |
-
# Simulates mutation
|
6 |
def analyze_sequences(input_df):
|
7 |
results = []
|
8 |
for _, row in input_df.iterrows():
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
confidence_score = round(random.uniform(0.7, 0.99), 2)
|
14 |
-
|
15 |
results.append({
|
16 |
-
"Sequence":
|
17 |
-
"Predicted Mutation":
|
18 |
-
"Confidence Score":
|
19 |
})
|
20 |
-
|
21 |
return pd.DataFrame(results)
|
22 |
|
23 |
-
#
|
24 |
def load_example_data():
|
25 |
-
|
26 |
-
"DNA_Sequence": [
|
27 |
-
"AGCTAGCTA",
|
28 |
-
"GATCGATCG",
|
29 |
-
"TTAGCTAGCT",
|
30 |
-
"ATGCGTAGC"
|
31 |
-
]
|
32 |
})
|
33 |
-
return analyze_sequences(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
# Gradio Interface
|
36 |
-
with gr.Blocks(
|
37 |
-
gr.Markdown(""
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
with gr.Row(equal_height=True):
|
46 |
-
upload_button = gr.File(label="π Upload CSV File", file_types=[".csv"])
|
47 |
-
example_button = gr.Button("π§ͺ Load Example Data")
|
48 |
-
|
49 |
-
gr.Markdown("### Results Table")
|
50 |
output_table = gr.DataFrame(
|
51 |
label="Analysis Results",
|
52 |
-
headers=["Sequence", "Predicted Mutation", "Confidence Score"]
|
53 |
-
wrap=True
|
54 |
)
|
55 |
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
|
|
61 |
|
62 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import random
|
4 |
+
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']
|
11 |
+
mutation_types = ['SNV', 'Insertion', 'Deletion', 'No Mutation']
|
12 |
+
mutation = random.choice(mutation_types)
|
13 |
+
confidence = round(random.uniform(0.7, 0.99), 2)
|
|
|
|
|
14 |
results.append({
|
15 |
+
"Sequence": seq,
|
16 |
+
"Predicted Mutation": mutation,
|
17 |
+
"Confidence Score": confidence
|
18 |
})
|
|
|
19 |
return pd.DataFrame(results)
|
20 |
|
21 |
+
# Load example data
|
22 |
def load_example_data():
|
23 |
+
df = pd.DataFrame({
|
24 |
+
"DNA_Sequence": ["AGCTAGCTA", "GATCGATCG", "TTAGCTAGCT", "ATGCGTAGC"]
|
|
|
|
|
|
|
|
|
|
|
25 |
})
|
26 |
+
return analyze_sequences(df)
|
27 |
+
|
28 |
+
# Convert DataFrame to CSV string
|
29 |
+
def dataframe_to_csv(dataframe):
|
30 |
+
if dataframe is None:
|
31 |
+
return ""
|
32 |
+
csv_buffer = StringIO()
|
33 |
+
dataframe.to_csv(csv_buffer, index=False)
|
34 |
+
return csv_buffer.getvalue()
|
35 |
+
|
36 |
+
# Generate Mutation Statistics Summary
|
37 |
+
def get_mutation_stats(result_df):
|
38 |
+
if result_df is None or result_df.empty:
|
39 |
+
return "No data available.", None
|
40 |
+
|
41 |
+
# Count mutations
|
42 |
+
mutation_counts = result_df["Predicted Mutation"].value_counts()
|
43 |
+
summary_text = "π Mutation Statistics:\n"
|
44 |
+
for mutation, count in mutation_counts.items():
|
45 |
+
summary_text += f"- {mutation}: {count}\n"
|
46 |
+
|
47 |
+
# Create bar chart
|
48 |
+
chart = gr.BarPlot(
|
49 |
+
mutation_counts.reset_index(),
|
50 |
+
x="Predicted Mutation",
|
51 |
+
y="count",
|
52 |
+
title="Mutation Frequency",
|
53 |
+
color="Predicted Mutation",
|
54 |
+
tooltip=["Predicted Mutation", "count"],
|
55 |
+
vertical=False,
|
56 |
+
height=200
|
57 |
+
)
|
58 |
+
|
59 |
+
return summary_text, chart
|
60 |
|
61 |
# Gradio Interface
|
62 |
+
with gr.Blocks() as demo:
|
63 |
+
gr.Markdown("## 𧬠MutateX β Liquid Biopsy Mutation Detection Tool")
|
64 |
+
gr.Markdown("Upload a CSV file with DNA sequences to simulate mutation detection.")
|
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 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
output_table = gr.DataFrame(
|
71 |
label="Analysis Results",
|
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 |
+
# Function calls
|
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)
|
92 |
|
93 |
demo.launch()
|