Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
633 lines (551 loc) · 20.7 KB
/
Copy pathapp.py
File metadata and controls
633 lines (551 loc) · 20.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
"""
============================================================
TP53 RAG Platform - Production Web App
============================================================
Streamlit web interface with:
- Fast multi-agent analysis (via dispatcher)
- Voice input transcription (Whisper)
- Real-time streaming responses
- HIPAA-compliant output
- Enterprise dossier export
Run: streamlit run app.py
============================================================
"""
import os
import sys
import json
import logging
from pathlib import Path
from typing import Dict, List, Optional, Any
from datetime import datetime
import streamlit as st
try:
from streamlit_option_menu import option_menu
except ImportError:
option_menu = None
# Add project root to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
# Initialise pipeline logger before importing functions
import main_tp53_analysis as _pipeline
import logging as _logging
_pipeline.logger = _logging.getLogger("TP53Pipeline")
_pipeline.logger.setLevel(_logging.DEBUG)
_pipeline.logger.addHandler(_logging.NullHandler())
from cancer_heatmap import plot_mutation_heatmap, KNOWN_HOTSPOTS
from main_tp53_analysis import (
fetch_sequence,
analyze_protein,
find_mutation_positions,
run_alignment,
find_orfs,
codon_usage,
amino_acid_frequency,
fetch_multiple_sequences,
build_multiple_alignment,
build_phylogenetic_tree,
save_phylogenetic_tree,
export_distance_matrix_csv,
annotate_protein_domains,
export_domains_csv,
export_mutations_csv,
export_orfs_csv,
plot_all,
TP53_HOMOLOGS,
)
# ── Page config ────────────────────────────────────────────────────────────
st.set_page_config(
page_title="TP53 Bioinformatics Pipeline",
page_icon="🧬",
layout="wide",
initial_sidebar_state="expanded",
)
# ── Custom CSS ─────────────────────────────────────────────────────────────
st.markdown("""
<style>
@import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;700&family=Syne:wght@400;600;800&display=swap');
html, body, [class*="css"] {
font-family: 'Syne', sans-serif;
}
.stApp {
background: #0D1117;
color: #E6EDF3;
}
h1, h2, h3 {
font-family: 'Syne', sans-serif !important;
font-weight: 800 !important;
}
code, .stCode {
font-family: 'JetBrains Mono', monospace !important;
}
.metric-card {
background: #161B22;
border: 1px solid #30363D;
border-radius: 8px;
padding: 16px 20px;
margin: 6px 0;
}
.metric-value {
font-size: 2rem;
font-weight: 800;
color: #2196F3;
font-family: 'JetBrains Mono', monospace;
}
.metric-label {
font-size: 0.75rem;
color: #8B949E;
text-transform: uppercase;
letter-spacing: 0.1em;
}
.domain-card {
background: #161B22;
border-left: 3px solid #4CAF50;
border-radius: 0 6px 6px 0;
padding: 10px 14px;
margin: 4px 0;
font-family: 'JetBrains Mono', monospace;
font-size: 0.8rem;
}
.domain-db {
color: #4CAF50;
font-weight: 700;
font-size: 0.7rem;
}
.mutation-badge {
background: #1C2128;
border: 1px solid #F44336;
border-radius: 4px;
padding: 2px 8px;
font-family: 'JetBrains Mono', monospace;
font-size: 0.75rem;
color: #F44336;
display: inline-block;
margin: 2px;
}
.orf-row {
background: #161B22;
border: 1px solid #30363D;
border-radius: 6px;
padding: 8px 12px;
margin: 3px 0;
font-family: 'JetBrains Mono', monospace;
font-size: 0.78rem;
}
.step-header {
background: linear-gradient(90deg, #2196F3 0%, #0D47A1 100%);
border-radius: 6px;
padding: 8px 16px;
margin: 12px 0 6px 0;
font-weight: 700;
font-size: 0.85rem;
letter-spacing: 0.05em;
}
.success-box {
background: #0D2818;
border: 1px solid #4CAF50;
border-radius: 8px;
padding: 12px 16px;
margin: 8px 0;
color: #4CAF50;
font-family: 'JetBrains Mono', monospace;
font-size: 0.85rem;
}
.warning-box {
background: #1A1200;
border: 1px solid #FF9800;
border-radius: 8px;
padding: 12px 16px;
margin: 8px 0;
color: #FF9800;
font-size: 0.85rem;
}
.sidebar-info {
background: #161B22;
border-radius: 8px;
padding: 12px;
font-size: 0.8rem;
color: #8B949E;
margin: 8px 0;
}
div[data-testid="stSidebar"] {
background: #0D1117;
border-right: 1px solid #30363D;
}
.stButton > button {
background: linear-gradient(135deg, #2196F3, #0D47A1);
color: white;
border: none;
border-radius: 6px;
font-family: 'Syne', sans-serif;
font-weight: 700;
font-size: 1rem;
padding: 0.6rem 2rem;
width: 100%;
transition: opacity 0.2s;
}
.stButton > button:hover {
opacity: 0.85;
}
.stTextInput > div > div > input,
.stSelectbox > div > div > select {
background: #161B22;
border: 1px solid #30363D;
color: #E6EDF3;
border-radius: 6px;
font-family: 'JetBrains Mono', monospace;
}
</style>
""", unsafe_allow_html=True)
# ── Sidebar ────────────────────────────────────────────────────────────────
with st.sidebar:
st.markdown("## 🧬 Pipeline Config")
st.markdown("---")
email = st.text_input(
"NCBI Email",
value=os.environ.get("ENTREZ_EMAIL", ""),
placeholder="your@email.com",
help="Required by NCBI Entrez API. Never stored or shared."
)
accession = st.text_input(
"NCBI Accession",
value="NM_000546",
help="Any NCBI nucleotide accession. Default: human TP53"
)
st.markdown("**Analysis Options**")
run_phylo = st.checkbox("Multi-species phylogenetic tree", value=True)
run_domains = st.checkbox("Protein domain annotation", value=True)
st.markdown("**Parameters**")
mutation_window = st.slider("Mutation window (bp)", 10, 200, 50)
gc_window = st.slider("GC content window (bp)", 50, 300, 100)
orf_min_length = st.slider("Min ORF length (bp)", 50, 500, 100)
st.markdown("---")
run_button = st.button("🚀 Run Analysis", use_container_width=True)
st.markdown("""
<div class="sidebar-info">
<b>About</b><br>
Built by Samuel Mbote<br>
General Surgery Resident<br>
& Bioinformatics Developer<br><br>
<a href="https://github.com/mbote-droid/tp53_analysis"
style="color:#2196F3;">GitHub Repository</a>
</div>
""", unsafe_allow_html=True)
# ── Header ─────────────────────────────────────────────────────────────────
st.markdown("""
# 🧬 TP53 Bioinformatics Pipeline
### Genomic Analysis for Tumour Suppressor Genes
""")
tab1, tab2 = st.tabs(["🔬 Pipeline Analysis", "🔥 Cancer Heatmap"])
with tab2:
st.markdown("## 🔥 TP53 Hotspot Mutations Across Cancer Types")
st.markdown(
"<div style='color:#8B949E;font-size:0.9rem;margin-bottom:16px;'>"
"12 known TP53 hotspot mutations across 11 cancer types, based on the "
"<b>IARC TP53 Database</b> and published somatic mutation literature."
"</div>", unsafe_allow_html=True)
heatmap_path = "results/cancer_mutation_heatmap.png"
if st.button("Generate Heatmap", key="hm_btn"):
with st.spinner("Generating..."):
plot_mutation_heatmap(output_path=heatmap_path)
st.success("Done!")
if os.path.exists(heatmap_path):
st.image(heatmap_path, use_container_width=True,
caption="TP53 Hotspot Mutation Frequency — IARC TP53 Database")
with open(heatmap_path, "rb") as f:
st.download_button("Download PNG", f.read(),
"cancer_mutation_heatmap.png", "image/png")
else:
st.info("Click 'Generate Heatmap' above.")
import pandas as pd
st.markdown("### Hotspot Reference Table")
rows = [{"Mutation": h["aa"], "Codon": h["codon"],
"Change": h["change"], "Cancer Types": ", ".join(h["cancers"])}
for h in KNOWN_HOTSPOTS]
st.dataframe(pd.DataFrame(rows), use_container_width=True, hide_index=True)
with tab1:
st.markdown("""
<div style="color:#8B949E; font-size:0.9rem; margin-bottom:24px;">
TP53 is mutated in ~50% of all human cancers. This pipeline fetches, analyzes,
and visualizes the gene across species — from sequence to protein domains.
Enter your NCBI email in the sidebar and click <b>Run Analysis</b>.
</div>
""", unsafe_allow_html=True)
# ── Pre-run info cards ──────────────────────────────────────────────────────
if not run_button:
col1, col2, col3, col4 = st.columns(4)
with col1:
st.markdown("""
<div class="metric-card">
<div class="metric-value">9</div>
<div class="metric-label">Pipeline Steps</div>
</div>""", unsafe_allow_html=True)
with col2:
st.markdown("""
<div class="metric-card">
<div class="metric-value">6</div>
<div class="metric-label">Reading Frames Scanned</div>
</div>""", unsafe_allow_html=True)
with col3:
st.markdown("""
<div class="metric-card">
<div class="metric-value">5</div>
<div class="metric-label">Species Compared</div>
</div>""", unsafe_allow_html=True)
with col4:
st.markdown("""
<div class="metric-card">
<div class="metric-value">63</div>
<div class="metric-label">Passing Unit Tests</div>
</div>""", unsafe_allow_html=True)
st.markdown("""
<div class="warning-box">
⚡ <b>Quick start:</b> Set your NCBI email in the sidebar, then click
<b>Run Analysis</b>. Use <b>--skip phylo/domains</b> checkboxes for a
faster ~30 second run. Full analysis takes 2–5 minutes.
</div>
""", unsafe_allow_html=True)
st.stop()
# ── Validation ─────────────────────────────────────────────────────────────
if not email or "@" not in email:
st.error("❌ Please enter a valid email address in the sidebar.")
st.stop()
if not accession.strip():
st.error("❌ Please enter an NCBI accession number.")
st.stop()
clean_accession = accession.strip().replace(" ", "")
os.makedirs("results", exist_ok=True)
# ── Run Pipeline ───────────────────────────────────────────────────────────
st.markdown("---")
# Step 1 — Fetch
st.markdown('<div class="step-header">STEP 1 / 9 — Fetching Sequence from NCBI</div>',
unsafe_allow_html=True)
with st.spinner(f"Fetching {clean_accession} from NCBI..."):
record = fetch_sequence(clean_accession, email)
if not record:
st.error(f"❌ Failed to fetch accession '{clean_accession}'. Check the accession ID and try again.")
st.stop()
dna_seq = record.seq
st.markdown(f"""
<div class="success-box">
✓ Fetched: <b>{record.id}</b> | Length: <b>{len(dna_seq):,} bp</b>
</div>""", unsafe_allow_html=True)
# Step 2 — Translate
st.markdown('<div class="step-header">STEP 2 / 9 — Translating DNA to Protein</div>',
unsafe_allow_html=True)
protein_seq = analyze_protein(dna_seq)
if protein_seq:
st.markdown(f"""
<div class="success-box">
✓ Protein translated: <b>{len(protein_seq)} amino acids</b>
</div>""", unsafe_allow_html=True)
else:
st.warning("⚠️ Translation produced no protein. Continuing with remaining steps.")
# Step 3 — Mutations
st.markdown('<div class="step-header">STEP 3 / 9 — Simulating & Detecting Mutations</div>',
unsafe_allow_html=True)
compare_len = 1000
mutant_dna = Seq("A" * mutation_window) + dna_seq[mutation_window:compare_len]
mutations = find_mutation_positions(str(dna_seq[:compare_len]), str(mutant_dna))
export_mutations_csv(mutations)
st.markdown(f"""
<div class="success-box">
✓ Mutations detected: <b>{len(mutations)}</b>
(first {mutation_window} bases replaced with 'A')
</div>""", unsafe_allow_html=True)
if mutations:
with st.expander(f"View all {len(mutations)} mutations"):
cols = st.columns(6)
for i, m in enumerate(mutations):
cols[i % 6].markdown(
f'<span class="mutation-badge">'
f'pos {m["position"]}: {m["original"]}→{m["mutant"]}'
f'</span>',
unsafe_allow_html=True
)
# Step 4 — Alignment
st.markdown('<div class="step-header">STEP 4 / 9 — Pairwise Alignment</div>',
unsafe_allow_html=True)
score = run_alignment(dna_seq[:compare_len], mutant_dna)
st.markdown(f"""
<div class="success-box">
✓ Alignment score: <b>{score:.2f}</b> (original vs mutant, {compare_len} bp window)
</div>""", unsafe_allow_html=True)
# Step 5 — ORFs
st.markdown('<div class="step-header">STEP 5 / 9 — ORF Discovery (All 6 Reading Frames)</div>',
unsafe_allow_html=True)
with st.spinner("Scanning all 6 reading frames..."):
orfs = find_orfs(dna_seq, min_length=orf_min_length)
export_orfs_csv(orfs)
st.markdown(f"""
<div class="success-box">
✓ ORFs found (≥{orf_min_length} bp): <b>{len(orfs)}</b>
</div>""", unsafe_allow_html=True)
if orfs:
with st.expander(f"View top 10 ORFs"):
for orf in orfs[:10]:
st.markdown(
f'<div class="orf-row">'
f'Frame <b>{orf["frame"]}</b> | '
f'Position {orf["start"]}–{orf["end"]} | '
f'Length <b>{orf["length"]} nt</b> | '
f'Protein: {orf["protein"][:30]}...'
f'</div>',
unsafe_allow_html=True
)
# Step 6 — Codon Usage
st.markdown('<div class="step-header">STEP 6 / 9 — Codon Usage Bias</div>',
unsafe_allow_html=True)
codon_freq = codon_usage(dna_seq)
top5 = sorted(codon_freq.items(), key=lambda x: x[1], reverse=True)[:5]
st.markdown(f"""
<div class="success-box">
✓ Codon usage calculated | Top codon:
<b>{top5[0][0]}</b> ({top5[0][1]:.4f})
</div>""", unsafe_allow_html=True)
codon_cols = st.columns(5)
for i, (codon, freq) in enumerate(top5):
codon_cols[i].markdown(f"""
<div class="metric-card" style="text-align:center">
<div class="metric-value" style="font-size:1.4rem">{codon}</div>
<div class="metric-label">{freq:.4f}</div>
</div>""", unsafe_allow_html=True)
# Step 7 — Phylogenetics
st.markdown('<div class="step-header">STEP 7 / 9 — Multi-Species Phylogenetic Analysis</div>',
unsafe_allow_html=True)
if run_phylo:
with st.spinner("Fetching homologs and building phylogenetic tree (this takes ~30s)..."):
homologs = fetch_multiple_sequences(TP53_HOMOLOGS, email)
if len(homologs) >= 3:
alignment = build_multiple_alignment(homologs)
tree = build_phylogenetic_tree(alignment)
save_phylogenetic_tree(tree)
export_distance_matrix_csv(alignment)
st.markdown(f"""
<div class="success-box">
✓ Phylogenetic tree built |
{len(homologs)} species × {len(alignment[0].seq):,} bp alignment
</div>""", unsafe_allow_html=True)
# Show tree image
if os.path.exists("results/phylo_tree.png"):
st.image("results/phylo_tree.png",
caption="Neighbour-Joining Phylogenetic Tree — TP53 Orthologs",
use_container_width=True)
# Show distance matrix
if os.path.exists("results/distance_matrix.csv"):
import pandas as pd
with st.expander("View species distance matrix"):
df = pd.read_csv("results/distance_matrix.csv", index_col=0)
st.dataframe(df.style.background_gradient(cmap="Blues"),
use_container_width=True)
else:
st.warning("⚠️ Fewer than 3 homologs fetched. Skipping tree.")
else:
st.info("⏭️ Phylogenetic analysis skipped.")
# Step 8 — Domain Annotation
st.markdown('<div class="step-header">STEP 8 / 9 — Protein Domain Annotation</div>',
unsafe_allow_html=True)
if run_domains and protein_seq:
with st.spinner("Querying EMBL-EBI InterPro API..."):
domains = annotate_protein_domains(protein_seq, email)
export_domains_csv(domains)
if domains:
st.markdown(f"""
<div class="success-box">
✓ Protein domains annotated: <b>{len(domains)} hits</b>
</div>""", unsafe_allow_html=True)
for d in domains[:15]:
st.markdown(
f'<div class="domain-card">'
f'<span class="domain-db">[{d["database"]}]</span> '
f'<b>{d["accession"]}</b> — {d["name"]} '
f'<span style="color:#8B949E">| aa {d["start"]}–{d["end"]}</span>'
f'</div>',
unsafe_allow_html=True
)
else:
st.warning("⚠️ No domain annotations returned. EBI may be temporarily unavailable.")
else:
st.info("⏭️ Domain annotation skipped.")
# Step 9 — Plots & Exports
st.markdown('<div class="step-header">STEP 9 / 9 — Visualisation & Exports</div>',
unsafe_allow_html=True)
with st.spinner("Generating plots..."):
if protein_seq:
plot_all(dna_seq, protein_seq, gc_window=gc_window)
st.markdown("""
<div class="success-box">
✓ All results exported to results/ folder
</div>""", unsafe_allow_html=True)
if os.path.exists("results/tp53_analysis.png"):
st.image("results/tp53_analysis.png",
caption="GC Content & Amino Acid Frequency Analysis",
use_container_width=True)
# ── Summary metrics ────────────────────────────────────────────────────────
st.markdown("---")
st.markdown("## 📊 Analysis Summary")
c1, c2, c3, c4, c5 = st.columns(5)
with c1:
st.markdown(f"""
<div class="metric-card">
<div class="metric-value">{len(dna_seq):,}</div>
<div class="metric-label">Sequence Length (bp)</div>
</div>""", unsafe_allow_html=True)
with c2:
st.markdown(f"""
<div class="metric-card">
<div class="metric-value">{len(protein_seq) if protein_seq else 0}</div>
<div class="metric-label">Protein Length (aa)</div>
</div>""", unsafe_allow_html=True)
with c3:
st.markdown(f"""
<div class="metric-card">
<div class="metric-value">{len(mutations)}</div>
<div class="metric-label">Mutations Detected</div>
</div>""", unsafe_allow_html=True)
with c4:
st.markdown(f"""
<div class="metric-card">
<div class="metric-value">{len(orfs)}</div>
<div class="metric-label">ORFs Discovered</div>
</div>""", unsafe_allow_html=True)
with c5:
st.markdown(f"""
<div class="metric-card">
<div class="metric-value">{score:.0f}</div>
<div class="metric-label">Alignment Score</div>
</div>""", unsafe_allow_html=True)
# ── Download buttons ───────────────────────────────────────────────────────
st.markdown("## 📥 Download Results")
dl1, dl2, dl3, dl4 = st.columns(4)
def read_file(path):
if os.path.exists(path):
with open(path, "rb") as f:
return f.read()
return None
with dl1:
data = read_file("results/mutations.csv")
if data:
st.download_button("⬇️ Mutations CSV", data,
"mutations.csv", "text/csv")
with dl2:
data = read_file("results/orfs.csv")
if data:
st.download_button("⬇️ ORFs CSV", data,
"orfs.csv", "text/csv")
with dl3:
data = read_file("results/protein_domains.csv")
if data:
st.download_button("⬇️ Domains CSV", data,
"protein_domains.csv", "text/csv")
with dl4:
data = read_file("results/distance_matrix.csv")
if data:
st.download_button("⬇️ Distance Matrix", data,
"distance_matrix.csv", "text/csv")
st.markdown("---")
st.markdown("""
<div style="text-align:center; color:#8B949E; font-size:0.8rem; padding:16px 0">
Built by <b>Samuel Mbote</b> — General Surgery Resident & Bioinformatics Developer<br>
<a href="https://github.com/mbote-droid/tp53_analysis"
style="color:#2196F3;">github.com/mbote-droid/tp53_analysis</a>
</div>
""", unsafe_allow_html=True)