Muhamed-Kheir commited on
Commit
ebdb539
·
verified ·
1 Parent(s): afa23f8

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -0
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import math
4
+ from user_script import (
5
+ read_uploaded_fasta_or_zip,
6
+ derive_serotype_names_from_sources,
7
+ compute_unique_kmers_per_serotype,
8
+ classify_unknown_sequences,
9
+ parse_k_input,
10
+ plot_counts_by_serotype,
11
+ )
12
+
13
+ st.set_page_config(page_title="Serotype k-mer classifier", layout="wide")
14
+ st.title("🧬 Serotype k-mer classifier — STRICT uniques (Streamlit)")
15
+
16
+ st.markdown("""Upload **known serotype** sequences (FASTA or a ZIP with one FASTA per serotype) and **unknown** sequences,
17
+ choose parameters, then click **Run**. The app computes strictly unique k-mers per serotype across k and classifies unknowns.
18
+ """)
19
+
20
+ st.sidebar.header("Inputs")
21
+ known_file = st.sidebar.file_uploader("Known serotypes (FASTA or ZIP of FASTA files)", type=["fasta","fa","fas","fna","txt","zip"])
22
+ unknown_file = st.sidebar.file_uploader("Unknown sequences (FASTA or ZIP of FASTA files)", type=["fasta","fa","fas","fna","txt","zip"])
23
+
24
+ seqtype = st.sidebar.selectbox("Sequence type", ["DNA", "Protein"])
25
+ is_protein = (seqtype == "Protein")
26
+ default_k = 9 if is_protein else 21
27
+ k_input = st.sidebar.text_input("k values (e.g. 21 or 15-21 or 7,9,11)", value=str(default_k))
28
+ fdr_alpha = st.sidebar.number_input("FDR α", min_value=0.0, value=0.05, step=0.01)
29
+
30
+ run = st.sidebar.button("▶ Run analysis")
31
+
32
+ if run:
33
+ if not known_file or not unknown_file:
34
+ st.error("Please upload both known and unknown sequences.")
35
+ st.stop()
36
+
37
+ with st.spinner("Reading uploads..."):
38
+ known_records = read_uploaded_fasta_or_zip(known_file) # list of (src, header, seq)
39
+ unknown_records = read_uploaded_fasta_or_zip(unknown_file)
40
+
41
+ if not known_records:
42
+ st.error("No records found in the known upload."); st.stop()
43
+ if not unknown_records:
44
+ st.error("No records found in the unknown upload."); st.stop()
45
+
46
+ # Map headers to serotype names
47
+ name_map = derive_serotype_names_from_sources(known_records)
48
+ serotype_to_seq = {}
49
+ for src, header, seq in known_records:
50
+ sero = name_map.get(header, header.split()[0])
51
+ if sero not in serotype_to_seq:
52
+ serotype_to_seq[sero] = seq
53
+
54
+ k_values = parse_k_input(k_input, default_single=default_k)
55
+ k_values = sorted({k for k in k_values if k >= 3})
56
+ if not k_values:
57
+ st.error("No valid k values (>=3)."); st.stop()
58
+
59
+ st.info(f"Detected serotypes: {list(serotype_to_seq.keys())}")
60
+ st.info(f"k values: {k_values}")
61
+
62
+ with st.spinner("Computing strict-unique k-mers per serotype..."):
63
+ uniques = compute_unique_kmers_per_serotype(serotype_to_seq, is_protein=is_protein, k_values=k_values)
64
+
65
+ with st.spinner("Classifying unknown sequences..."):
66
+ df_full = classify_unknown_sequences(unknown_records, uniques, is_protein=is_protein, fdr_alpha=fdr_alpha)
67
+
68
+ def best_score(row):
69
+ g = row["Predicted_serotype"]
70
+ if g == "NoMatch":
71
+ return 0.0
72
+ q = row.get(f"FDR_{g}", 1.0)
73
+ return 0.0 if (q is None or q <= 0) else -math.log10(max(q, 1e-300))
74
+
75
+ df_full["Score_-log10FDR"] = df_full.apply(best_score, axis=1)
76
+
77
+ show_cols = ["Source","Sequence","Orientation_used","Predicted_serotype","Matches_total","Confidence_by_present","Confidence_by_serotype_vocab","Score_-log10FDR"]
78
+ st.subheader("Predictions")
79
+ st.dataframe(df_full[show_cols].sort_values("Score_-log10FDR", ascending=False), use_container_width=True)
80
+
81
+ fig = plot_counts_by_serotype(df_full)
82
+ st.subheader("Predicted serotype counts")
83
+ st.pyplot(fig)
84
+
85
+ st.subheader("Downloads")
86
+ csv = df_full.to_csv(index=False).encode("utf-8")
87
+ st.download_button("Download predictions_by_serotype.csv", data=csv, file_name="predictions_by_serotype.csv", mime="text/csv")
88
+ else:
89
+ st.info("Upload the two files on the left, set parameters, then click **Run analysis**.")