|
import gradio as gr |
|
import pandas as pd |
|
import random |
|
from io import StringIO |
|
|
|
|
|
def analyze_sequences(input_df): |
|
if input_df is None or input_df.empty: |
|
return pd.DataFrame(columns=["Sequence", "Predicted Mutation", "Confidence Score"]) |
|
|
|
results = [] |
|
for _, row in input_df.iterrows(): |
|
seq = row['DNA_Sequence'] |
|
mutation_types = ['SNV', 'Insertion', 'Deletion', 'No Mutation'] |
|
mutation = random.choice(mutation_types) |
|
confidence = round(random.uniform(0.7, 0.99), 2) |
|
results.append({ |
|
"Sequence": seq, |
|
"Predicted Mutation": mutation, |
|
"Confidence Score": confidence |
|
}) |
|
return pd.DataFrame(results) |
|
|
|
|
|
def load_example_data(): |
|
example_df = pd.DataFrame({ |
|
"DNA_Sequence": [ |
|
"AGCTAGCTA", |
|
"GATCGATCG", |
|
"TTAGCTAGCT", |
|
"ATGCGTAGC" |
|
] |
|
}) |
|
return analyze_sequences(example_df) |
|
|
|
|
|
def dataframe_to_csv(df): |
|
if df is None or df.empty: |
|
return "" |
|
csv_buffer = StringIO() |
|
df.to_csv(csv_buffer, index=False) |
|
return csv_buffer.getvalue() |
|
|
|
|
|
def get_mutation_stats(result_df): |
|
if result_df is None or result_df.empty: |
|
return "No data available.", None |
|
|
|
|
|
mutation_counts = result_df["Predicted Mutation"].value_counts() |
|
summary_text = "π Mutation Statistics:\n" |
|
for mutation, count in mutation_counts.items(): |
|
summary_text += f"- {mutation}: {count}\n" |
|
|
|
|
|
chart = gr.BarPlot( |
|
mutation_counts.reset_index(), |
|
x="Predicted Mutation", |
|
y="count", |
|
title="Mutation Frequency", |
|
color="Predicted Mutation", |
|
tooltip=["Predicted Mutation", "count"], |
|
vertical=False, |
|
height=200 |
|
) |
|
|
|
return summary_text, chart |
|
|
|
|
|
def process_and_get_stats(file=None): |
|
if file is not None: |
|
result_df = analyze_sequences(file) |
|
else: |
|
result_df = load_example_data() |
|
|
|
summary, chart = get_mutation_stats(result_df) |
|
return result_df, summary, chart |
|
|
|
|
|
with gr.Blocks(theme="default") as demo: |
|
gr.Markdown(""" |
|
# 𧬠MutateX β Liquid Biopsy Mutation Detection Tool |
|
|
|
Upload a CSV file containing DNA sequences or use the example data to simulate mutation detection. |
|
|
|
*Developed by [GradSyntax](https://www.gradsyntax.com )* |
|
""") |
|
|
|
with gr.Row(equal_height=True): |
|
upload_btn = gr.File(label="π Upload CSV File", file_types=[".csv"]) |
|
example_btn = gr.Button("π§ͺ Load Example Data") |
|
|
|
output_table = gr.DataFrame( |
|
label="Analysis Results", |
|
headers=["Sequence", "Predicted Mutation", "Confidence Score"] |
|
) |
|
|
|
stats_text = gr.Textbox(label="Mutation Statistics Summary") |
|
stats_chart = gr.Plot(label="Mutation Frequency Chart") |
|
|
|
download_btn = gr.File(label="β¬οΈ Download Results as CSV") |
|
|
|
|
|
upload_btn.upload(fn=process_and_get_stats, inputs=upload_btn, outputs=[output_table, stats_text, stats_chart]) |
|
example_btn.click(fn=process_and_get_stats, inputs=None, outputs=[output_table, stats_text, stats_chart]) |
|
download_btn.upload(fn=dataframe_to_csv, inputs=output_table, outputs=download_btn) |
|
|
|
demo.launch() |