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
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
import streamlit as st
import os
import tempfile
import subprocess
from pathlib import Path

import pipeline as pl
import json
import shutil

def render_igv(res):
    work_dir_name = res.get("work_dir_name")
    genome_version = res.get("genome_version", "hg38")
    if not work_dir_name:
        st.warning("No static files path found for IGV.js. Restart the pipeline to generate.")
        return
        
    igv_bed_path = Path(res["igv_bed_path"])
    variants = []
    if igv_bed_path.exists():
        with open(igv_bed_path, "r") as f:
            for line in f:
                if line.startswith("#") or not line.strip():
                    continue
                parts = line.strip().split("\t")
                if len(parts) >= 4:
                    chrom = parts[0]
                    start = int(parts[1])
                    end = int(parts[2])
                    label = parts[3]
                    locus = f"{chrom}:{max(1, start-50)}-{end+50}"
                    variants.append({
                        "locus": locus,
                        "name": label.split("_")[-1],
                        "label": label,
                        "pos_label": f"{chrom}:{start+1}"
                    })
                    
    variants_json = json.dumps(variants)
    
    bam_url = f"/app/static/{work_dir_name}/synthetic.sorted.bam"
    bai_url = f"/app/static/{work_dir_name}/synthetic.sorted.bam.bai"
    vcf_url = f"/app/static/{work_dir_name}/synthetic.vcf"
    navigator_url = f"/app/static/{work_dir_name}/igv_variant_navigator.bed"
    probes_url = f"/app/static/{work_dir_name}/fully_covered_exons.bed"
    mane_url = f"/app/static/{work_dir_name}/mane_transcripts.bed"
    
    probes_bed_exists = res.get("fully_covered_bed_path") is not None and Path(res["fully_covered_bed_path"]).exists()
    mane_transcripts_exists = res.get("mane_transcripts_bed_path") is not None and Path(res["mane_transcripts_bed_path"]).exists()
    
    tracks = [
        {
            "name": "Reference",
            "type": "sequence",
            "order": 1
        }
    ]
    if mane_transcripts_exists:
        tracks.append({
            "name": "MANE Transcripts",
            "type": "annotation",
            "format": "bed",
            "url": mane_url,
            "indexed": False,
            "order": 1.5,
            "color": "green",
            "displayMode": "EXPANDED"
        })
    if probes_bed_exists:
        tracks.append({
            "name": "Probes BED",
            "type": "annotation",
            "format": "bed",
            "url": probes_url,
            "indexed": False,
            "order": 2,
            "color": "blue"
        })
    tracks.extend([
        {
            "name": "Variant Navigator BED",
            "type": "annotation",
            "format": "bed",
            "url": navigator_url,
            "indexed": False,
            "order": 3,
            "color": "red"
        },
        {
            "name": "Synthetic VCF",
            "type": "variant",
            "format": "vcf",
            "url": vcf_url,
            "indexed": False,
            "order": 4
        },
        {
            "name": "Synthetic BAM",
            "type": "alignment",
            "format": "bam",
            "url": bam_url,
            "indexURL": bai_url,
            "order": 5,
            "height": 300
        }
    ])
    tracks_json = json.dumps(tracks)

    html_content = f"""
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
        <style>
            body {{
                font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
                margin: 0;
                padding: 0;
                display: flex;
                height: 600px;
                background-color: #ffffff;
            }}
            #sidebar {{
                width: 250px;
                border-right: 1px solid #e0e0e0;
                display: flex;
                flex-direction: column;
                height: 100%;
                background-color: #f8f9fa;
            }}
            #sidebar-header {{
                padding: 10px;
                background-color: #0e1117;
                color: white;
                font-weight: bold;
                font-size: 14px;
            }}
            #variant-list {{
                flex-grow: 1;
                overflow-y: auto;
                padding: 5px;
            }}
            .variant-item {{
                padding: 8px 10px;
                margin-bottom: 4px;
                border-radius: 4px;
                cursor: pointer;
                border: 1px solid #e9ecef;
                background-color: white;
                font-size: 12px;
                transition: background-color 0.2s;
            }}
            .variant-item:hover {{
                background-color: #e9ecef;
            }}
            .variant-name {{
                font-weight: bold;
                color: #ff4b4b;
            }}
            .variant-pos {{
                color: #6c757d;
                margin-top: 2px;
            }}
            #igv-container {{
                flex-grow: 1;
                height: 100%;
                overflow: hidden;
            }}
            #igv-div {{
                height: 600px;
                width: 100%;
            }}
        </style>
    </head>
    <body>
        <div id="sidebar">
            <div id="sidebar-header"><i class="fas fa-list"></i> Variant Navigator ({len(variants)})</div>
            <div id="variant-list"></div>
        </div>
        <div id="igv-container">
            <div id="igv-div"></div>
        </div>

        <script src="https://cdn.jsdelivr.net/npm/igv@2.15.5/dist/igv.min.js"></script>
        <script>
            var variants = {variants_json};
            
            var listContainer = document.getElementById("variant-list");
            variants.forEach(function(v, index) {{
                var item = document.createElement("div");
                item.className = "variant-item";
                item.innerHTML = '<div class="variant-name">' + v.name.toUpperCase() + '</div>' +
                                 '<div class="variant-pos">' + v.pos_label + '</div>';
                item.onclick = function() {{
                    if (window.igvBrowser) {{
                        window.igvBrowser.search(v.locus);
                    }}
                }};
                listContainer.appendChild(item);
            }});

            var options = {{
                genome: "{genome_version}",
                locus: variants.length > 0 ? variants[0].locus : "chr1:1787315-1787437",
                tracks: {tracks_json}
            }};

            var igvDiv = document.getElementById("igv-div");
            igv.createBrowser(igvDiv, options)
                .then(function (browser) {{
                    window.igvBrowser = browser;
                    console.log("IGV browser created successfully.");
                }})
                .catch(function(err) {{
                    console.error("Error creating IGV browser:", err);
                    document.getElementById("igv-div").innerHTML = 
                        "<div style='color:#721c24; background-color:#f8d7da; border:1px solid #f5c6cb; padding:20px; border-radius:4px; font-family:sans-serif; margin:20px;'>" +
                        "<h3>❌ Error Loading IGV Browser</h3>" +
                        "<p><b>Message:</b> " + err.toString() + "</p>" +
                        "<p>This usually indicates static file serving is not enabled or files are not accessible.</p>" +
                        "<p><b>Paths attempted:</b></p>" +
                        "<ul>" +
                        "<li>BAM: <code>" + options.tracks[4].url + "</code></li>" +
                        "<li>VCF: <code>" + options.tracks[3].url + "</code></li>" +
                        "<li>BED: <code>" + options.tracks[1].url + "</code></li>" +
                        "</ul>" +
                        "<p>Please verify that <code>enableStaticServing = true</code> is active and the Hugging Face Space has fully rebuilt.</p>" +
                        "</div>";
                }});
        </script>
    </body>
    </html>
    """
    st.components.v1.html(html_content, height=620, scrolling=False)


st.set_page_config(
    page_title="In Silico Controls Generator",
    page_icon="🧬",
    layout="wide",
)

st.title("🧬 In Silico Controls Generator")
st.caption(
    "Generate synthetic BAM + VCF files with realistic variants "
    "derived from your probe panel and MANE exon annotations."
)

# ── Sidebar: parameters ──────────────────────────────────────────────────────

uploaded_bed = st.session_state.get("uploaded_bed_file")

with st.sidebar:
    st.header("Pipeline Parameters")

    st.subheader("Sequencing Parameters")
    depth = st.number_input("Target read depth per variant", min_value=1, max_value=10000, value=100, step=10)
    vaf = st.slider("Variant allele frequency (VAF)", min_value=0.01, max_value=1.0, value=0.20, step=0.01, format="%.2f")
    read_length = st.number_input("Read length (bp)", min_value=50, max_value=300, value=150, step=10)

    st.subheader("Sequencing Technology")
    seq_mode = st.radio(
        "Sequencing assay style",
        options=["Hybrid Capture (Staggered reads)", "PCR Amplicon (Identical start/ends)"],
        index=0,
        help="Hybrid Capture simulates sheared fragments with staggered read start/end coordinates. PCR Amplicon simulates amplicon sequencing where all reads start and end exactly at the probe/target coordinate boundaries."
    )

    if seq_mode == "Hybrid Capture (Staggered reads)":
        st.subheader("Fragment Insert Size")
        insert_size = st.number_input("Mean insert size (bp)", min_value=100, max_value=1000, value=379, step=10)
        insert_std = st.number_input("Insert size std dev (bp)", min_value=0, max_value=200, value=20, step=5)
    else:
        insert_size = 250
        insert_std = 0

    st.subheader("Indel Parameters")
    indel_interval = st.number_input(
        "Indel interval (0 = SNVs only)",
        min_value=0, max_value=100, value=10, step=1,
        help="Make every Nth variant an indel. Set to 0 to generate only SNVs.",
    )

    st.divider()
    st.subheader("Reference Genome")
    genome_version = st.selectbox(
        "Genome assembly",
        options=["hg38", "hg19"],
        index=0,
        help="Choose the reference genome version (hg38 or hg19)."
    )
    ref_mode = st.radio(
        "FASTA source",
        options=["Use cached / download", "Custom path"],
        help="Downloads and caches the selected assembly, or lets you point to a custom local path.",
    )
    custom_ref_path = ""
    if ref_mode == "Custom path":
        custom_ref_path = st.text_input(
            f"Path to {genome_version}.fa",
            placeholder=f"/data/references/{genome_version}.fa",
            help="Must be an indexed FASTA (.fa + .fa.fai).",
        )

    st.divider()
    st.subheader("Targeting Mode")
    if genome_version == "hg19":
        st.info("ℹ️ MANE transcript annotations are hg38-only. Targeting mode is set to Direct Probe Coordinates for hg19.")
        target_mode = "Direct Probe Coordinates"
    else:
        target_mode = st.radio(
            "Variant targeting logic",
            options=["MANE Transcript Exons/Introns", "Direct Probe Coordinates"],
            help="MANE Transcripts Mode places variants in coding exons and flanking introns of protein-coding genes. Direct Probe Mode places a single variant inside each probe coordinate itself, completely ignoring gene annotations."
        )

    if target_mode == "MANE Transcript Exons/Introns":
        st.subheader("Variant Locations")
        include_cds = st.checkbox("Generate CDS variants", value=True, help="Place variants in the coding sequence (CDS) of MANE exons.")
        include_intron = st.checkbox("Generate flanking intronic variants", value=True, help="Place variants in the flanking introns of MANE exons.")
        include_offtarget = st.checkbox("Generate off-target (unused probe) variants", value=True, help="Place variants in the midpoint of probes with no MANE exon coverage.")
        direct_window_size = 0
    else:
        include_cds = False
        include_intron = False
        include_offtarget = False

        st.subheader("Direct Probe Settings")
        direct_variant_strategy = st.radio(
            "Variant placement strategy",
            options=["One variant per N bp window", "Single random variant per probe"],
            index=0,
            help="Choose whether to generate one variant per N bp window across the probe coordinates or a single random variant per probe."
        )
        if direct_variant_strategy == "One variant per N bp window":
            direct_window_size = st.number_input(
                "Window size (bp)",
                min_value=1, max_value=1000, value=10, step=1,
                help="Place one variant randomly inside each non-overlapping window of this size."
            )
        else:
            direct_window_size = 0

    st.subheader("Read Group")
    rg_id = st.text_input("Read Group ID", value="CPDV2510843-SEQ-251103")
    rg_sm = st.text_input("Sample Name", value="CPDV2510843-SEQ-251103")
    
    st.divider()
    st.subheader("πŸ› οΈ Debug Info")
    st.caption("Helpful diagnostics for troubleshooting deployment status.")
    st.write("Streamlit Version:", st.__version__)
    st.write("File Uploaded:", uploaded_bed is not None)
    if uploaded_bed:
        st.write("Filename:", uploaded_bed.name)
    st.write("probes_df in state:", "probes_df" in st.session_state)
    if "probes_df" in st.session_state:
        st.write("probes_df count:", len(st.session_state["probes_df"]))
    import os
    st.write("CWD:", os.getcwd())
    st.write("Script Path:", __file__)
    st.write("static/ exists:", os.path.exists("static"))
    st.write("src/static/ exists:", os.path.exists("src/static"))
    if os.path.exists("static"):
        st.write("static/ folders:", os.listdir("static")[:5])
    if os.path.exists("src/static"):
        st.write("src/static/ folders:", os.listdir("src/static")[:5])

# ── Main area ────────────────────────────────────────────────────────────────

col_upload, col_info = st.columns([2, 1])

with col_upload:
    st.header("1 Β· Upload Probes BED")
    uploaded_bed = st.file_uploader(
        "Upload your probes BED file",
        type=["bed"],
        key="uploaded_bed_file",
        help="Standard BED3+ format (chrom, start, end, ...)",
    )

with col_info:
    st.header("Cache Status")
    mane_cached = pl.MANE_BED12.exists()
    ref_cached = (pl.HG38_FA.exists() and pl.HG38_FAI.exists()) if genome_version == "hg38" else (pl.HG19_FA.exists() and pl.HG19_FAI.exists())
    bigbed_cached = pl.BIGBEDTOBED_PATH.exists()

    st.markdown(f"{'βœ…' if bigbed_cached else '⬜'} bigBedToBed")
    st.markdown(f"{'βœ…' if mane_cached else '⬜'} MANE annotation")
    st.markdown(f"{'βœ…' if ref_cached else '⬜'} {genome_version} reference")

    if not ref_cached and ref_mode == "Use cached / download":
        st.warning(f"{genome_version} not cached. First run will download and index the assembly, which may take 5–10 minutes.")

# ── Step 1.5: Customize Probes ────────────────────────────────────────────────
if uploaded_bed:
    if "probes_df" not in st.session_state or st.session_state.get("uploaded_file_name") != uploaded_bed.name:
        import pandas as pd
        import io
        
        try:
            uploaded_bed.seek(0)
            content = uploaded_bed.read().decode("utf-8", errors="ignore")
            # Strip comments and headers
            lines = [line for line in content.splitlines() if line.strip() and not line.startswith("#") and not line.startswith("track")]
            
            if lines:
                df = pd.read_csv(io.StringIO("\n".join(lines)), sep="\t", header=None)
                cols = ["chrom", "start", "end"]
                if len(df.columns) > 3:
                    cols += [f"col_{i}" for i in range(3, len(df.columns))]
                df.columns = cols[:len(df.columns)]
                df.insert(0, "Select", True)
                st.session_state["probes_df"] = df
                st.session_state["uploaded_file_name"] = uploaded_bed.name
                st.session_state.pop("sample_seed", None)
                st.session_state.pop("prev_frac", None)
            else:
                st.error("Uploaded BED file appears to be empty or contains only comments.")
        except Exception as e:
            st.error(f"Error parsing BED file: {e}")

    if "probes_df" in st.session_state:
        df = st.session_state["probes_df"]
        
        st.header("1.5 Β· Customize Probes")
        st.caption(f"Loaded {len(df):,} probes from {uploaded_bed.name}. Customize which regions will be processed below.")
        
        col_mode, col_rand = st.columns([1, 1])
        
        with col_mode:
            subset_mode = st.radio(
                "Selection mode",
                options=["All Probes", "Manual Selection (below)", "Random Sampling"],
                index=0,
                help="Choose whether to run all probes, manually check/uncheck probes in the list, or select a random fraction of the probes."
            )
            
        with col_rand:
            if subset_mode == "Random Sampling":
                sample_frac = st.slider("Fraction of probes to keep", min_value=0.01, max_value=1.00, value=0.10, step=0.01)
                resample_btn = st.button("🎲 Resample")
                
                if "sample_seed" not in st.session_state or resample_btn or st.session_state.get("prev_frac") != sample_frac:
                    import random
                    st.session_state["sample_seed"] = random.randint(0, 100000)
                    st.session_state["prev_frac"] = sample_frac
                
                sampled_df = df.sample(frac=sample_frac, random_state=st.session_state["sample_seed"])
                df["Select"] = df.index.isin(sampled_df.index)
            elif subset_mode == "All Probes":
                df["Select"] = True
                
        # Render table editor
        st.markdown("#### πŸ“‹ Probes List")
        st.caption("Double-click a cell to search, or check/uncheck boxes to filter targets.")
        
        edited_df = st.data_editor(
            df,
            use_container_width=True,
            hide_index=True,
            disabled=[col for col in df.columns if col != "Select"],
            column_config={
                "Select": st.column_config.CheckboxColumn(
                    "Select",
                    help="Uncheck to exclude this region from variant generation",
                    default=True
                )
            }
        )
        st.session_state["probes_df"] = edited_df
        
        total_selected = len(edited_df[edited_df["Select"] == True])
        st.info(f"Selected {total_selected:,} of {len(df):,} probes ({total_selected/len(df)*100:.1f}%) for variant generation.")

st.divider()

# ── Step 2: Run pipeline ──────────────────────────────────────────────────────

st.header("2 Β· Run Pipeline")

if not uploaded_bed:
    st.info("Upload a probes BED file to enable the pipeline.")
    st.stop()

run_btn = st.button("β–Ά Run Pipeline", type="primary", use_container_width=True)

# Clear results when a new run is requested
if run_btn:
    st.session_state.pop("results", None)
    st.session_state.pop("log_lines", None)

# ── Execute pipeline ──────────────────────────────────────────────────────────

if run_btn:
    fasta_path = (pl.HG38_FA if genome_version == "hg38" else pl.HG19_FA) if ref_mode == "Use cached / download" else Path(custom_ref_path)

    if ref_mode == "Custom path":
        if not custom_ref_path:
            st.error("Please provide a path to your hg38.fa file.")
            st.stop()
        if not fasta_path.exists():
            st.error(f"FASTA file not found: {fasta_path}")
            st.stop()
        fai = Path(str(fasta_path) + ".fai")
        if not fai.exists():
            st.warning("No .fai index found. Attempting to index with samtools faidx...")
            subprocess.run(f"samtools faidx {fasta_path}", shell=True, capture_output=True)

    # Filter for selected probes
    if "probes_df" in st.session_state:
        df = st.session_state["probes_df"]
        selected_df = df[df["Select"] == True]
    else:
        st.error("No probe data found in session state.")
        st.stop()
        
    if len(selected_df) == 0:
        st.error("No probes selected! Please select at least one probe in Step 1.5.")
        st.stop()
        
    # Convert back to BED format (tab-separated, without the 'Select' column)
    bed_cols = [col for col in selected_df.columns if col != "Select"]
    bed_text = selected_df[bed_cols].to_csv(sep="\t", header=False, index=False)

    work_dir = Path(tempfile.mkdtemp(prefix="insilicocontrols_"))
    probes_bed = work_dir / "probes.bed"
    probes_bed.write_text(bed_text)

    log_expander = st.expander("Pipeline log", expanded=True)
    log_area = log_expander.empty()
    log_lines = []

    def append_log(msg):
        log_lines.append(str(msg))
        log_area.code("\n".join(log_lines[-80:]), language=None)

    progress_bar = st.progress(0.0, text="Starting...")

    def update_progress(fraction, label=""):
        progress_bar.progress(min(fraction, 1.0), text=label)

    try:
        # Pre-ensure reference genome
        if ref_mode == "Use cached / download":
            update_progress(0.08, f"Ensuring {genome_version} reference...")
            append_log(f"\n=== {genome_version} Reference ===")
            pl.ensure_reference(genome_version=genome_version, log_func=append_log)

        if target_mode == "MANE Transcript Exons/Introns":
            update_progress(0.02, "Setting up tools...")
            append_log("=== Setting up tools ===")
            pl.ensure_bigbedtobed(append_log)

            update_progress(0.05, "Ensuring MANE annotation...")
            append_log("\n=== MANE Annotation ===")
            pl.ensure_mane(append_log)

            update_progress(0.15, "Parsing MANE exons...")
            append_log("\n=== Parsing MANE Exons ===")
            exons_bed = pl.parse_mane_exons(work_dir, append_log)

            update_progress(0.25, "Analyzing probe coverage...")
            append_log("\n=== Coverage Analysis ===")
            stats, fully_bed, partial_bed, unused_bed = pl.analyze_coverage(
                work_dir, probes_bed, exons_bed, append_log
            )

            append_log("\n============================================")
            append_log("           COVERAGE SUMMARY               ")
            append_log("============================================")
            append_log(f"Exons with >95% coverage (USED):    {stats['fully_covered']}")
            append_log(f"Exons with partial coverage (USED): {stats['partially_covered']}")
            append_log(f"Probes with no exon coverage:       {stats['probes_no_exons']}")
            append_log(f"Unused contiguous probes (ADDED):   {stats['unused_probes']}")
            append_log("============================================")

            # Subset MANE transcripts intersecting with target probes
            merged_probes = work_dir / "merged_probes.bed"
            mane_transcripts_bed = work_dir / "mane_transcripts.bed"
            update_progress(0.30, "Subsetting MANE transcripts...")
            append_log("\n=== Subsetting MANE Transcripts ===")
            pl.run_cmd(f"bedtools intersect -a {pl.MANE_BED12} -b {merged_probes} -wa -u > {mane_transcripts_bed}", append_log)

            update_progress(0.35, "Generating target SNVs...")
            append_log("\n=== Generating Target SNVs ===")
            snvs_bed, total_snvs = pl.generate_target_snvs(
                work_dir=work_dir,
                fully_bed=fully_bed,
                partial_bed=partial_bed,
                unused_bed=unused_bed,
                include_cds=include_cds,
                include_intron=include_intron,
                include_offtarget=include_offtarget,
                mode="mane",
                log_func=append_log
            )
        else: # Direct Probe Coordinates mode
            stats = {
                "fully_covered": 0,
                "partially_covered": 0,
                "probes_no_exons": 0,
                "unused_probes": 0,
            }
            fully_bed = None
            partial_bed = None
            unused_bed = None
            mane_transcripts_bed = None

            update_progress(0.35, "Generating target SNVs...")
            append_log("\n=== Generating Target SNVs (Direct BED Mode) ===")
            snvs_bed, total_snvs = pl.generate_target_snvs(
                work_dir=work_dir,
                fully_bed=None,
                partial_bed=None,
                unused_bed=None,
                mode="direct_bed",
                probes_bed=probes_bed,
                direct_window_size=direct_window_size,
                log_func=append_log
            )

        append_log("\n============================================")
        append_log("           VARIANT SUMMARY                ")
        append_log("============================================")
        append_log(f"Total SNVs generated for BAM:       {total_snvs}")
        append_log("============================================")

        update_progress(0.40, "Generating synthetic BAM...")
        append_log("\n=== Generating Synthetic BAM ===")

        def bam_progress(fraction, label):
            update_progress(0.40 + fraction * 0.55, label)

        sorted_bam, output_vcf = pl.generate_synthetic_bam(
            work_dir=work_dir,
            snvs_bed=snvs_bed,
            fasta_path=fasta_path,
            depth=depth,
            vaf=vaf,
            rg_id=rg_id,
            rg_sm=rg_sm,
            insert_size=insert_size,
            insert_std=insert_std,
            indel_interval=indel_interval,
            read_length=read_length,
            sequencing_mode="pcr_amplicon" if seq_mode.startswith("PCR Amplicon") else "hybrid_capture",
            log_func=append_log,
            progress_func=bam_progress,
        )

        update_progress(1.0, "Done!")
        append_log("\nβœ… Pipeline complete.")

        bai_path = Path(str(sorted_bam) + ".bai")
        vcf_path = Path(output_vcf) if not isinstance(output_vcf, Path) else output_vcf
        igv_bed_path = Path(snvs_bed) if not isinstance(snvs_bed, Path) else snvs_bed
        fully_bed_path = Path(fully_bed) if fully_bed and not isinstance(fully_bed, Path) else fully_bed

        # Copy to static directories for IGV.js visualization (both root and script-relative)
        work_dir_name = work_dir.name
        static_dest_cwd = Path("static") / work_dir_name
        static_dest_script = Path(__file__).parent / "static" / work_dir_name
        
        for dest in [static_dest_cwd, static_dest_script]:
            dest.mkdir(parents=True, exist_ok=True)
            shutil.copy(sorted_bam, dest / "synthetic.sorted.bam")
            if bai_path.exists():
                shutil.copy(bai_path, dest / "synthetic.sorted.bam.bai")
            shutil.copy(vcf_path, dest / "synthetic.vcf")
            shutil.copy(igv_bed_path, dest / "igv_variant_navigator.bed")
            if fully_bed_path and fully_bed_path.exists():
                shutil.copy(fully_bed_path, dest / "fully_covered_exons.bed")
            if mane_transcripts_bed and mane_transcripts_bed.exists():
                shutil.copy(mane_transcripts_bed, dest / "mane_transcripts.bed")

        # Store paths only β€” never load large files into session_state memory
        st.session_state["results"] = {
            "stats": stats,
            "total_snvs": total_snvs,
            "bam_path": str(sorted_bam),
            "bai_path": str(bai_path) if bai_path.exists() else None,
            "vcf_path": str(vcf_path),
            "igv_bed_path": str(igv_bed_path),
            "fully_covered_bed_path": str(fully_bed_path) if fully_bed_path else None,
            "mane_transcripts_bed_path": str(mane_transcripts_bed) if mane_transcripts_bed else None,
            "work_dir_name": work_dir_name,
            "genome_version": genome_version,
        }
        st.session_state["log_lines"] = log_lines[:]

    except Exception as e:
        st.error(f"Pipeline failed: {e}")
        append_log(f"\n❌ ERROR: {e}")
        raise

# ── Results section (persists across reruns via session_state) ────────────────

if "results" in st.session_state:
    res = st.session_state["results"]
    stats = res["stats"]
    total_snvs = res["total_snvs"]

    st.success("Pipeline completed successfully!")

    # Show log if available and pipeline didn't just run
    if not run_btn and "log_lines" in st.session_state:
        with st.expander("Pipeline log", expanded=False):
            st.code("\n".join(st.session_state["log_lines"][-80:]), language=None)

    st.header("3 Β· Results")
    m1, m2, m3, m4 = st.columns(4)
    m1.metric("Fully Covered Exons", f"{stats['fully_covered']:,}")
    m2.metric("Partially Covered Exons", f"{stats['partially_covered']:,}")
    m3.metric("Off-target Probes", f"{stats['probes_no_exons']:,}")
    m4.metric("Total SNVs Generated", f"{total_snvs:,}")

    st.header("πŸ” Interactive Variant Browser")
    st.caption("Inspect the generated synthetic alignments and mutations directly in the browser. Click on a variant in the navigator panel to jump to its locus.")
    render_igv(res)

    st.header("4 Β· Download Outputs")

    dl1, dl2, dl3 = st.columns(3)

    bam_path = Path(res["bam_path"])
    bai_path = Path(res["bai_path"]) if res["bai_path"] else None
    vcf_path = Path(res["vcf_path"])
    igv_bed_path = Path(res["igv_bed_path"])
    fully_bed_path = Path(res["fully_covered_bed_path"]) if res.get("fully_covered_bed_path") else None

    with dl1:
        st.markdown("**Synthetic BAM**")
        if bam_path.exists():
            with open(bam_path, "rb") as f:
                st.download_button(
                    "⬇ Download BAM",
                    data=f,
                    file_name="synthetic.sorted.bam",
                    mime="application/octet-stream",
                    use_container_width=True,
                )

    with dl2:
        st.markdown("**BAM Index (.bai)**")
        if bai_path and bai_path.exists():
            with open(bai_path, "rb") as f:
                st.download_button(
                    "⬇ Download BAI",
                    data=f,
                    file_name="synthetic.sorted.bam.bai",
                    mime="application/octet-stream",
                    use_container_width=True,
                )

    with dl3:
        st.markdown("**Synthetic VCF**")
        if vcf_path.exists():
            with open(vcf_path, "rb") as f:
                st.download_button(
                    "⬇ Download VCF",
                    data=f,
                    file_name="synthetic.vcf",
                    mime="text/plain",
                    use_container_width=True,
                )

    dl4, dl5, dl6 = st.columns(3)

    with dl4:
        st.markdown("**IGV Variant Navigator BED**")
        if igv_bed_path.exists():
            with open(igv_bed_path, "rb") as f:
                st.download_button(
                    "⬇ Download IGV BED",
                    data=f,
                    file_name="igv_variant_navigator.bed",
                    mime="text/plain",
                    use_container_width=True,
                )

    with dl5:
        st.markdown("**Fully Covered Exons BED**")
        if fully_bed_path and fully_bed_path.exists():
            with open(fully_bed_path, "rb") as f:
                st.download_button(
                    "⬇ Download Fully Covered Exons",
                    data=f,
                    file_name="fully_covered_exons.bed",
                    mime="text/plain",
                    use_container_width=True,
                )

    with dl6:
        st.markdown("**MANE Transcripts BED12**")
        mane_transcripts_bed_path = Path(res["mane_transcripts_bed_path"]) if res.get("mane_transcripts_bed_path") else None
        if mane_transcripts_bed_path and mane_transcripts_bed_path.exists():
            with open(mane_transcripts_bed_path, "rb") as f:
                st.download_button(
                    "⬇ Download MANE Transcripts",
                    data=f,
                    file_name="mane_transcripts.bed",
                    mime="text/plain",
                    use_container_width=True,
                )

# ── Footer ────────────────────────────────────────────────────────────────────
st.divider()
st.caption(
    "**How it works:** Your probe BED is intersected with MANE CDS exons. "
    "For each covered exon, synthetic SNVs are placed in the CDS and flanking "
    "intronic positions. For unused probes, a variant is placed at the midpoint. "
    "Paired-end reads are generated at the target depth and VAF, then written to "
    "a sorted, indexed BAM alongside a matching VCF."
)