File size: 35,728 Bytes
436f829
6973f9d
436f829
 
 
 
 
 
 
9d68891
 
436f829
5484f77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6973f9d
 
5484f77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
436f829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f4d482
436f829
 
 
 
 
 
 
 
 
30e4166
436f829
5484f77
436f829
5484f77
 
 
 
436f829
 
5484f77
 
 
 
 
 
 
 
 
 
 
436f829
 
5484f77
 
 
 
 
436f829
5484f77
 
 
 
 
 
 
 
 
 
 
436f829
5484f77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
436f829
 
5484f77
 
 
 
 
 
 
 
 
436f829
 
 
 
 
 
 
 
 
30e4166
436f829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d68891
 
 
 
 
436f829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d68891
 
 
 
 
 
 
 
 
 
 
 
436f829
 
 
9d68891
436f829
 
 
 
 
 
 
 
5484f77
436f829
5484f77
 
 
 
436f829
5484f77
 
 
 
 
 
 
436f829
 
5484f77
 
 
 
 
436f829
5484f77
 
 
 
436f829
5484f77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
436f829
 
5484f77
436f829
5484f77
 
 
 
 
 
 
 
436f829
5484f77
 
 
 
 
 
 
 
 
 
 
 
 
 
436f829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5484f77
 
 
436f829
5484f77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
436f829
5484f77
 
 
436f829
5484f77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
436f829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5484f77
 
436f829
 
5484f77
 
436f829
5484f77
 
436f829
5484f77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
436f829
5484f77
436f829
5484f77
436f829
5484f77
436f829
5484f77
 
 
436f829
5484f77
 
 
 
436f829
5484f77
436f829
5484f77
 
436f829
5484f77
 
 
 
 
436f829
5484f77
 
436f829
5484f77
 
 
 
 
436f829
 
 
5484f77
 
 
 
 
 
 
 
 
 
436f829
5484f77
 
 
 
 
 
 
436f829
 
5484f77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
436f829
5484f77
 
 
436f829
 
609dc5b
436f829
5484f77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
436f829
5484f77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
436f829
 
5484f77
 
 
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
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
import warnings
import os
import pandas as pd
import xgboost as xgb
import sys
sys.path.append('../codes')
import utils as utils
import gradio as gr
import pickle
import pandas as pd
import xgboost as xgb


def cleanup_files(uploaded_files=None):
    """
    Remove uploaded temporary files from disk without touching any session/model state.

    Args:
        uploaded_files (list | None): Optional list of Gradio `File` objects (or file-like objects)
            that have a `.name` attribute pointing to a local temporary filepath.

    Returns:
        None
    """
    # Nothing to do if caller didn't pass any files
    if not uploaded_files:
        return

    # Best-effort cleanup: never crash the app due to deletion failure.
    for f in uploaded_files:
        try:
            # Gradio may pass `None` for an unfilled upload component
            if f is None:
                continue

            # Gradio File objects expose the local temp path as `.name`
            if not hasattr(f, "name"):
                continue

            # Delete the temp file if it still exists
            if os.path.exists(f.name):
                os.remove(f.name)

        except Exception:
            # Never fail the entire UI due to a cleanup issue
            pass

def _reset_state(_):
    """
    Clear per-session cached inference state when upstream inputs change.

    Args:
        _ (Any): Placeholder input (e.g., the changed Gradio component value). Not used.

    Returns:
        None: Used to overwrite `gr.State` so the next Submit recomputes features/predictions.
    """
    # Returning None here is a simple "invalidate cache" signal
    return None

markdown_text = """
                **Feature Category Explanations**

                **Somatic mutations**: Single nucleotide variants (SNV) and indels. For each gene, the total count of a somatic mutation is encoded as a positive integer feature.

                **Copy number alterations (CNA)**: CNA event for each gene was encoded as a categorical variable with the following five levels:
                - `-2`: Deep loss
                - `-1`: Single-copy loss
                - `0`: No event
                - `1`: Low-level gain
                - `2`: High-level amplification.

                **Mutation signatures (single-base substitution (SBS))**: Mutation signatures were inferred by calculating the dot product of COSMIC-derived weights and 96 single-base substitutions in a trinucleotide context.
                - SBS4: Tobacco smoking
                - SBS7: Ultraviolet light exposure
                - SBS22: Aristolochic acid exposure
                - SBS24: Aflatoxin exposure
                - See more details in [COSMIC](https://cancer.sanger.ac.uk/signatures/sbs/).

                **Age/Sex**: Patient age at the time of sequencing and biological sex.

                **SHAP**: impact on OncoNPC prediction (larger magnitude indicates greater impact)
                """

# defining global variables
xgb_onconpc = xgb.Booster()
xgb_onconpc.load_model('./model/xgboost_v1.7.6_OncoNPC_full.json')
cancer_types_to_consider = ['Acute Myeloid Leukemia', 'Bladder Urothelial Carcinoma', 'Cholangiocarcinoma',
                            'Colorectal Adenocarcinoma', 'Diffuse Glioma', 'Endometrial Carcinoma',
                            'Esophagogastric Adenocarcinoma', 'Gastrointestinal Neuroendocrine Tumors', 'Gastrointestinal Stromal Tumor',
                            'Head and Neck Squamous Cell Carcinoma',
                            'Invasive Breast Carcinoma', 'Melanoma', 'Meningothelial Tumor',
                            'Non-Hodgkin Lymphoma', 'Non-Small Cell Lung Cancer', 'Ovarian Epithelial Tumor', 'Pancreatic Adenocarcinoma',
                            'Pancreatic Neuroendocrine Tumor', 'Pleural Mesothelioma', 'Prostate Adenocarcinoma', 'Renal Cell Carcinoma',
                            'Well-Differentiated Thyroid Cancer']   

all_features = pd.read_csv('./data/onconpc_features.csv').drop('Unnamed: 0', axis=1).columns.tolist()

def get_preds(patients_file, samples_file, mutations_file, cna_file, tumor_id, state):
    """
    Run OncoNPC inference from uploaded CSV/TXT inputs and return:
      - top-3 predictions,
      - default explanation plot for the selected sample,
      - updated per-session state for later dropdown interactions.

    Args:
        patients_file (gradio.File | None): Uploaded patient file (CSV/TXT) with required columns `PATIENT_ID`, `SEX`.
        samples_file (gradio.File | None): Uploaded sample file (CSV/TXT) with required columns
            `PATIENT_ID`, `AGE_AT_SEQ_REPORT`, `SAMPLE_ID`.
        mutations_file (gradio.File | None): Uploaded mutations file (CSV/TXT) with required columns
            `Tumor_Sample_Barcode`, `Hugo_Symbol`, `Chromosome`, `Start_Position`, `Reference_Allele`, `Tumor_Seq_Allele2`.
        cna_file (gradio.File | None): Uploaded CNA file (CSV/TXT) with required column `Hugo_Symbol`.
        tumor_id (str | int | None): Sample identifier selected by the user.
        state (dict | None): Per-session cache (from `gr.State`). Expected keys:
            - "features": pd.DataFrame (rows=samples, cols=features)
            - "predictions": pd.DataFrame (rows=samples, cols=cancer types + metadata)
            - "sample_id": current sample being interpreted

    Returns:
        tuple:
            (top3_text: str,
             markdown: gr.Markdown,
             explanation_plot_path: str,
             state: dict)
    """
    # ------------------------------------------------------------------
    # 0) Decide whether to reuse cached computations
    # ------------------------------------------------------------------
    # If the user is only changing tumor_id (dropdown) and re-submitting,
    # we can reuse previously computed features/predictions and avoid:
    #   - re-reading files
    #   - recomputing features
    #   - recomputing predictions for all samples
    if state is not None and state.get("features") is not None and state.get("predictions") is not None:
        features = state["features"]
        predictions = state["predictions"]
    else:
        # ------------------------------------------------------------------
        # 1) First-run path: validate uploads exist
        # ------------------------------------------------------------------
        if patients_file is None or samples_file is None or cna_file is None or mutations_file is None:
            raise gr.Error("Please make sure all files have been properly uploaded")

        # ------------------------------------------------------------------
        # 2) Read uploaded files into DataFrames
        # ------------------------------------------------------------------
        # NOTE: Gradio stores uploads as temp files; access path via `.name`.
        if patients_file.name.endswith(".txt"):
            patients_df = pd.read_csv(patients_file.name, sep="\t")
        elif patients_file.name.endswith(".csv"):
            patients_df = pd.read_csv(patients_file.name)
        else:
            raise gr.Error("Patients file must be .txt or .csv")

        if samples_file.name.endswith(".txt"):
            samples_df = pd.read_csv(samples_file.name, sep="\t")
        elif samples_file.name.endswith(".csv"):
            samples_df = pd.read_csv(samples_file.name)  # BUGFIX: was patients_file in original code
        else:
            raise gr.Error("Samples file must be .txt or .csv")

        if mutations_file.name.endswith(".txt"):
            mutations_df = pd.read_csv(mutations_file.name, sep="\t")
        elif mutations_file.name.endswith(".csv"):
            mutations_df = pd.read_csv(mutations_file.name)
        else:
            raise gr.Error("Mutations file must be .txt or .csv")

        if cna_file.name.endswith(".txt"):
            cna_df = pd.read_csv(cna_file.name, sep="\t")
        elif cna_file.name.endswith(".csv"):
            cna_df = pd.read_csv(cna_file.name)
        else:
            raise gr.Error("CNA file must be .txt or .csv")

        # ------------------------------------------------------------------
        # 3) Validate the selected tumor_id
        # ------------------------------------------------------------------
        if tumor_id is None or str(tumor_id).strip() == "":
            raise gr.Error("Tumor Sample ID cannot be empty")

        # ------------------------------------------------------------------
        # 4) Validate expected columns (fail fast with friendly errors)
        # ------------------------------------------------------------------
        patients_columns = {"PATIENT_ID", "SEX"}
        if not patients_columns.issubset(set(patients_df.columns)):
            raise gr.Error(
                f"Patients File: Expected columns {sorted(patients_columns)}, "
                f"Received {patients_df.columns.to_list()}"
            )

        samples_columns = {"PATIENT_ID", "AGE_AT_SEQ_REPORT", "SAMPLE_ID"}
        if not samples_columns.issubset(set(samples_df.columns)):
            raise gr.Error(
                f"Samples File: Expected columns {sorted(samples_columns)}, "
                f"Received {samples_df.columns.to_list()}"
            )

        mutations_columns = {
            "Tumor_Sample_Barcode",
            "Hugo_Symbol",
            "Chromosome",
            "Start_Position",
            "Reference_Allele",
            "Tumor_Seq_Allele2",
        }
        if not mutations_columns.issubset(set(mutations_df.columns)):
            raise gr.Error(
                f"Mutations File: Expected columns {sorted(mutations_columns)}, "
                f"Received {mutations_df.columns.to_list()}"
            )

        cna_columns = {"Hugo_Symbol"}
        if not cna_columns.issubset(set(cna_df.columns)):
            raise gr.Error(
                f"CNA File: Expected columns {sorted(cna_columns)}, "
                f"Received {cna_df.columns.to_list()}"
            )

        # ------------------------------------------------------------------
        # 5) Build OncoNPC feature matrix from raw inputs
        # ------------------------------------------------------------------
        # `features` should end up indexed by sample IDs compatible with tumor_id dropdown.
        df_features_genie_final, _ = utils.get_onconpc_features_from_raw_data(
            patients_df,
            samples_df,
            mutations_df,
            cna_df,
            features_onconpc_path="./data/features_onconpc.pkl",
            combined_cohort_age_stats_path="./data/combined_cohort_age_stats.pkl",
            mut_sig_weights_filepath="./data/mutation_signatures/sigProfiler*.csv",
        )
        features = df_features_genie_final

        # ------------------------------------------------------------------
        # 6) Predict for all samples
        # ------------------------------------------------------------------
        # We assume `xgb_onconpc` is loaded at module scope.
        predictions = utils.get_xgboost_latest_cancer_type_preds(
            xgb_onconpc,
            features,
            cancer_types_to_consider,
        )

        # ------------------------------------------------------------------
        # 7) Cache these in session state so dropdown interactions work
        # ------------------------------------------------------------------
        state = {"features": features, "predictions": predictions}

        # ------------------------------------------------------------------
        # 8) Optional: delete temp uploaded files now that they're read into memory
        # ------------------------------------------------------------------
        # This prevents disk bloat on Spaces.
        cleanup_files([patients_file, samples_file, mutations_file, cna_file])

    # ------------------------------------------------------------------
    # 9) Set the sample being interpreted for this session
    # ------------------------------------------------------------------
    if tumor_id not in state["features"].index:
        raise gr.Error(f"Tumor Sample ID {tumor_id} not found in computed features index.")
    state["sample_id"] = tumor_id

    # ------------------------------------------------------------------
    # 10) Compute SHAP + generate a plot for the currently selected sample
    # ------------------------------------------------------------------
    # IMPORTANT: Only compute SHAP for one sample to keep this lightweight and fast.
    warnings.filterwarnings("ignore")
    features_one = state["features"].loc[[tumor_id]]
    shaps = utils.obtain_shap_values_with_latest_xgboost(xgb_onconpc, features_one)

    # Keep predictions consistent with `features_one`
    preds_one = state["predictions"].loc[[tumor_id]]

    # `results` is a dict keyed by query_id; each entry includes explanation_plot path
    results = utils.get_onconpc_prediction_explanations(
        [tumor_id],
        preds_one,
        shaps,
        features_one,
        cancer_types_to_consider,
        save_plot=True,
    )

    # ------------------------------------------------------------------
    # 11) Return UI outputs + updated state
    # ------------------------------------------------------------------
    return (
        get_top3(state["predictions"], tumor_id),
        gr.Markdown(markdown_text),
        results[tumor_id]["explanation_plot"],
        state,
    )

def parse_inputs(age, gender, CNA_events, mutations):
    # Normalization of age input
    age = age 
    try:
        int(age)
    except:
        raise gr.Error('Age should be a positive numerical value')
    combined_cohort_age_stats = None
    combined_cohort_age_stats_path = './data/combined_cohort_age_stats.pkl'
    with open(combined_cohort_age_stats_path, "rb") as fp:
        combined_cohort_age_stats = pickle.load(fp)
    age =  (age - combined_cohort_age_stats['Age_mean']) / combined_cohort_age_stats['Std_mean']

    # Convert gender to numerical value: male as 1, otherwise -1
    gender = 1.0 if gender == 'Male' else -1.0
    
    data = {'Age': age, 'Sex': gender} # TODO: Check how this is normalized 

    # Process CNA events: Expected format [[CNA, val], [CNA, val]]
    if len(CNA_events) > 0:
        CNA_events = CNA_events.split('|')
        for i in range(len(CNA_events)):
            # Split each event into CNA and value, and cast the value to integer
            try:
                CNA, val = CNA_events[i].split()
                CNA_events[i] = [CNA + ' CNA', float(val)] # Cast val to float
            except Exception as e:
                raise gr.Error(f'Error in CNA events. {e}')

    else:
        CNA_events = []

    # Process mutations: Expected format [mut1, mut2, etc.]
    if len(mutations) > 0:
        mutations = mutations.split('| ')
        for i in range(len(mutations)):
            # Split each mutation entry and strip white space
            mutations[i] = ['manual input'] + mutations[i].split(', ')
            mutations[i] = [m.strip() for m in mutations[i]] # Strip white space
            for j in range(len(mutations[i])):
                try:
                     mutations[i][j] = int(mutations[i][j])
                except:
                     pass
            #TODO: clean this up
            try:
                data[mutations[i][1]] += 1.0
            except:
                try:
                    data[mutations[i][1]] = 1.0
                except:
                    mutations = []
    else:
        mutations = []

    # Define mutation columns for DataFrame and create mutation DataFrame
    try:
        mutation_columns = ["UNIQUE_SAMPLE_ID", "HUGO_SYMBOL", "CHROMOSOME", "POSITION", "REF_ALLELE", "ALT_ALLELE"]
        mutation_full_df = pd.DataFrame(mutations, columns=mutation_columns) if mutations else pd.DataFrame(columns=mutation_columns)
        mutation_df = mutation_full_df.drop('HUGO_SYMBOL', axis=1, inplace=False)
    except Exception as e:
        raise gr.Error(f'Error in somatic mutations. {e}')
    
    mutation_signatures = pd.read_csv('./mutation_signatures_blank.csv').columns
    if len(mutations) == 0:
         for column in mutation_signatures:
              data[column] = 0
    else:
        df_trinuc_feats = utils.get_snv_in_trinuc_context(mutation_df, 'UNIQUE_SAMPLE_ID', 'CHROMOSOME', 'POSITION', 'REF_ALLELE', 'ALT_ALLELE', None)
        mutation_signatures = utils.obtain_mutation_signatures(df_trinuc_feats)

    # # Initialize data dictionary and populate with age and mutation signatures
        for column in mutation_signatures.columns:
            data[column] = mutation_signatures.loc['manual input'][column]

    # Add CNA events to data dictionary
    for CNA, val in CNA_events:
        data[CNA] = val

    # Add zero values for missing features in data dictionary
    for column in all_features:
        if column not in data.keys():
            data[column] = 0

    # Return the data as a DataFrame
    return pd.DataFrame([data])

def get_preds_min_info(age, gender, CNA_events, mutations, state, output="Top Prediction"):
    """
    Run OncoNPC inference from manual inputs and return:
      - top-3 predictions,
      - default explanation plot (top prediction) for the manual sample,
      - updated per-session state.

    Args:
        age (int | float): Age at sequencing/report; standardized using cohort stats.
        gender (str): "Male" or "Female".
        CNA_events (str): CNA event string like "RAF1 2 | PPARG 2".
        mutations (str): Mutation string like "ERBB2, chr17, 378..., C, T | ...".
        state (dict | None): Per-session cache (from `gr.State`). Will be overwritten.
        output (str): Reserved for future output selection. Currently unused.

    Returns:
        tuple:
            (top3_text: str,
             markdown: gr.Markdown,
             explanation_plot_path: str,
             state: dict)
    """
    # ------------------------------------------------------------------
    # 1) Parse manual inputs into a single-row feature frame
    # ------------------------------------------------------------------
    # parse_inputs() should produce a DataFrame with one row (the "manual input" sample)
    features = parse_inputs(age, gender, CNA_events, mutations)

    # ------------------------------------------------------------------
    # 2) Ensure feature columns match training features exactly (same set + same order)
    # ------------------------------------------------------------------
    all_features_local = (
        pd.read_csv("./data/onconpc_features.csv")
        .drop("Unnamed: 0", axis=1)
        .columns.tolist()
    )

    # Drop any extra columns not in training feature list
    for c in list(features.columns):
        if c not in all_features_local:
            features = features.drop(c, axis=1)

    # Add missing columns as zeros
    missing_cols = [c for c in all_features_local if c not in features.columns]
    if missing_cols:
        features = pd.concat([features, pd.DataFrame({c: [0] for c in missing_cols})], axis=1)

    # Reorder columns to match training
    features = features[all_features_local]

    # ------------------------------------------------------------------
    # 3) Predict + explain
    # ------------------------------------------------------------------
    predictions = utils.get_xgboost_latest_cancer_type_preds(
        xgb_onconpc, features, cancer_types_to_consider
    )

    warnings.filterwarnings("ignore")
    shaps = utils.obtain_shap_values_with_latest_xgboost(xgb_onconpc, features)

    # Manual sample id convention: index 0
    query_ids = [0]
    results = utils.get_onconpc_prediction_explanations(
        query_ids,
        predictions,
        shaps,
        features,
        cancer_types_to_consider,
        save_plot=True,
    )

    # ------------------------------------------------------------------
    # 4) Store per-session state used by update_image()
    # ------------------------------------------------------------------
    state = {"features": features, "predictions": predictions, "sample_id": 0}

    # ------------------------------------------------------------------
    # 5) Return UI outputs + state
    # ------------------------------------------------------------------
    return (
        get_top3(predictions, 0),
        gr.Markdown(markdown_text, elem_classes="markdown-box"),
        results[0]["explanation_plot"],
        state,
    )

def get_top3(predictions, tumor_sample_id):
    """
    Extracts and formats the top three cancer type predictions for a given tumor sample.

    Args:
        predictions: A DataFrame containing the cancer type predictions for various samples.
        tumor_sample_id: The ID of the tumor sample for which to extract the top three predictions.

    Returns:
        A string that lists the top three predicted cancer types and their probabilities.
    """
    # select the row corresponding to the tumor sample ID
    result = predictions.loc[tumor_sample_id]

    # transpose the row for easier processing, each row has columns cancer type, cancer probability 
    transposed_row = result.transpose()

    # remove unnecessary rows
    transposed_row = transposed_row.drop(['cancer_type', 'max_posterior'])

    # convert the series to a DataFrame and rename the column
    transposed_row = transposed_row.to_frame()
    transposed_row.columns = ['probability']

    # make sure the probability column is numeric
    transposed_row['probability'] = pd.to_numeric(transposed_row['probability'], errors='coerce')

    # get the top 3 predictions and their probabilities
    top3df = transposed_row.nlargest(3, columns=['probability'])
    top3 = top3df.index.tolist() # cancer types are indices
    top3probs = top3df['probability'].tolist()

    # build a formatted string with the top 3 predictions
    build = ''
    for cancer, prob in zip(top3, top3probs):
        build += f'{cancer}: {prob:.2f}\n'
    build = build.rstrip('\n')

    return build

def extract_sample_ids(samples_file):
    # Read the file into a DataFrame
    if samples_file is None:
        return []
    
    if samples_file.name[-4:] == '.txt':
        df = pd.read_csv(samples_file.name, sep='\t')
    elif samples_file.name[-4:] == '.csv':
        df = pd.read_csv(samples_file.name)
    else:
        raise ValueError('samples file must be .txt or .csv')

    # Assuming the column containing the sample IDs is named 'SampleID'
    sample_ids = df['SAMPLE_ID'].unique().tolist()
    return  gr.Dropdown(choices=sample_ids)

def update_image(target, state):
    """
    Regenerate the explanation plot for a different target cancer type using cached state.

    Args:
        target (str): Cancer type selected from dropdown; must be in `cancer_types_to_consider`.
        state (dict | None): Per-session state produced by get_preds()/get_preds_min_info().

    Returns:
        str: Filesystem path to the newly generated explanation plot image for (sample_id, target).
    """
    # ------------------------------------------------------------------
    # 0) Validate state
    # ------------------------------------------------------------------
    if state is None or state.get("features") is None or state.get("predictions") is None:
        raise gr.Error("Please click Submit first.")

    features = state["features"]
    predictions = state["predictions"]
    sample_id = state.get("sample_id")

    if sample_id is None:
        raise gr.Error("Please click Submit first.")

    if target not in cancer_types_to_consider:
        raise gr.Error(f"Unknown target cancer type: {target}")

    # ------------------------------------------------------------------
    # 1) Compute SHAP for only the selected sample (fast)
    # ------------------------------------------------------------------
    features_one = features.loc[[sample_id]]
    shaps_cup = utils.obtain_shap_values_with_latest_xgboost(xgb_onconpc, features_one)

    # In multiclass setting, SHAP is typically indexed by class position
    target_idx = cancer_types_to_consider.index(target)

    # ------------------------------------------------------------------
    # 2) Build per-feature SHAP vector for this sample and target
    # ------------------------------------------------------------------
    shap_pred_cancer_df = pd.DataFrame(
        shaps_cup[target_idx],                 # shape ~ (1, n_features) for this single sample
        index=features_one.index,              # [sample_id]
        columns=features_one.columns,          # feature names
    )
    shap_pred_sample_df = shap_pred_cancer_df.loc[sample_id]
    feature_sample_df = features_one.loc[sample_id]

    # Predicted probability for selected target class
    probability = predictions.loc[sample_id][target]

    # ------------------------------------------------------------------
    # 3) Generate and save plot
    # ------------------------------------------------------------------
    sample_info = f"Prediction: {target}\nPrediction probability: {probability:.3f}"

    # Partition features into human-friendly groups for the plot legend
    feature_group_to_features_dict, feature_to_feature_group_dict = utils.partition_feature_names_by_group(
        features_one.columns
    )

    explanation_plot = utils.get_individual_pred_interpretation(
        shap_pred_sample_df,
        feature_sample_df,
        feature_group_to_features_dict,
        feature_to_feature_group_dict,
        sample_info=sample_info,
        filename=f"{sample_id}_{target}_plot",
        filepath="./others_prediction_explanation",
        save_plot=True,
    )

    return explanation_plot

def show_row(value):
    """
    Toggle the visibility of UI elements based on the selected input method.

    This function is designed to work with a graphical user interface (GUI).
    It changes the visibility of elements depending on the user's choice of input method.

    Parameters:
    value (str): The input method selected by the user. Expected values are "CSV File" or "Manual Inputs".

    Returns:
    tuple: A pair of commands to update the visibility of GUI elements.
           Each command is a tuple itself, containing a method to update an element and a visibility state.
    """

    # If the user selects "CSV File" as the input method
    if value == "CSV File":
        # Make the CSV file upload element visible and hide the manual input fields
        return (gr.update(visible=True), gr.update(visible=False))

    # If the user selects "Manual Inputs" as the input method
    if value == "Manual Inputs":
        # Hide the CSV file upload element and make the manual input fields visible
        return (gr.update(visible=False), gr.update(visible=True))

    # If the user's selection does not match any expected input method
    return (gr.update(visible=False), gr.update(visible=False))  # Hide both elements


def launch_gradio(server_name, server_port):
    """
    Create and launch the Gradio UI, wiring callbacks with per-session `gr.State`
    so interpretation dropdown changes work without recomputing or relying on deleted temp files.

    Args:
        server_name (str): Server interface to bind (e.g., "0.0.0.0" on Spaces).
        server_port (int): Port number to bind.

    Returns:
        None
    """
    manual_input_description = (
        "In this option, you will manually enter patient and tumor data including age at sequencing, sex, "
        "copy number alterations (CNA) events, and somatic mutations."
    )

    csv_description = (
        "In this option, you can directly upload relevant CSV files, including clinical patient, clinical sample, "
        "mutations, and CNA data from [cBioPortal](https://www.cbioportal.org) or "
        "[AACR GENIE](https://www.aacr.org/professionals/research/aacr-project-genie/). "
        "Please see the example data in this [link](https://github.com/itmoon7/onconpc/tree/main/data/mock_n_3_data) <br> "
        "Note that column names `Tumor_Sample_Barcode` and `SAMPLE_ID` both refer to the sample identifier column and should be the same."
    )

    with gr.Blocks(
        css=(
            ".markdown-box { border: 1px solid #E0E0E0; border-radius: 8px; "
            "box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05); padding: 15px; margin-bottom: 20px; }"
        )
    ) as demo:

        # Main selector: choose input mode
        d = gr.Dropdown(
            label="Select your preferred input method for tumor data to visualize predictions made by OncoNPC",
            choices=["Manual Inputs", "CSV File"],
        )

        # Helper text rows (toggled with the same dropdown)
        with gr.Row(visible=False) as r3:
            gr.Markdown(csv_description, elem_classes="markdown-box")
        with gr.Row(visible=False) as r4:
            gr.Markdown(manual_input_description, elem_classes="markdown-box")

        # ------------------------------------------------------------------
        # CSV MODE UI (files + sample dropdown)
        # ------------------------------------------------------------------
        with gr.Row(visible=False) as r1:
            # Per-session cached state for CSV mode.
            # This is what makes dropdown plot changes possible without recomputing.
            state_csv = gr.State(value=None)

            with gr.Column():
                gr.Markdown("**Clinical Patient Data File:** Required columns: `PATIENT_ID`, `SEX`.")
                patients_file = gr.File(label="Upload clinical patients data")

                gr.Markdown("**Clinical Sample Data File:** Required columns: `PATIENT_ID`, `AGE_AT_SEQ_REPORT`, `SAMPLE_ID`.")
                samples_file = gr.File(label="Upload clinical samples data")

                gr.Markdown(
                    "**Mutations Data File:** Required columns: `Tumor_Sample_Barcode`, `Hugo_Symbol`, `Chromosome`, "
                    "`Start_Position`, `Reference_Allele`, `Tumor_Seq_Allele2`."
                )
                mutations_file = gr.File(label="Upload mutations data")

                gr.Markdown("**CNA Data File:** Required column: `Hugo_Symbol`.")
                cna_file = gr.File(label="Upload CNA data")

                # Sample ID dropdown is populated dynamically when samples_file changes
                tumor_sample_id = gr.Dropdown(choices=[], label="Tumor Sample ID")

                submit_button_csv = gr.Button("Submit")

            with gr.Column():
                predictions_output = gr.Textbox(label="Top 3 Predicted Cancer Types")
                category_explanation = gr.Markdown(markdown_text, elem_classes="markdown-box")

                image = gr.Image(label="Image Display")

                # User selects alternate target cancer type for interpretation plot
                output_selector = gr.Dropdown(
                    choices=cancer_types_to_consider,
                    label="Output Options",
                    filterable=True,
                )

                gr.Markdown(
                    "For more details regarding OncoNPC feature processing, OncoNPC performance, and clinical implications of OncoNPC predictions, "
                    "please see our [manuscript](https://www.nature.com/articles/s41591-023-02482-6).",
                    elem_classes="markdown-box",
                )

            # When samples file changes, parse IDs and populate tumor_sample_id dropdown
            samples_file.change(extract_sample_ids, inputs=samples_file, outputs=tumor_sample_id)

            # If any upstream input changes, invalidate cached state (forces recompute on next submit)
            patients_file.change(_reset_state, inputs=patients_file, outputs=state_csv)
            samples_file.change(_reset_state, inputs=samples_file, outputs=state_csv)
            mutations_file.change(_reset_state, inputs=mutations_file, outputs=state_csv)
            cna_file.change(_reset_state, inputs=cna_file, outputs=state_csv)

            # Submit: computes features/preds (or reuses cached), generates default plot for selected tumor_id,
            # and returns updated state_csv for later dropdown interactions.
            submit_button_csv.click(
                get_preds,
                inputs=[patients_file, samples_file, mutations_file, cna_file, tumor_sample_id, state_csv],
                outputs=[predictions_output, category_explanation, image, state_csv],
            )

            # Dropdown: regenerates plot using cached state only (no file reads)
            output_selector.change(
                update_image,
                inputs=[output_selector, state_csv],
                outputs=[image],
            )

        # ------------------------------------------------------------------
        # MANUAL MODE UI (text inputs)
        # ------------------------------------------------------------------
        with gr.Row(visible=False) as r2:
            # Per-session cached state for manual mode (separate from CSV mode).
            state_manual = gr.State(value=None)

            with gr.Column():
                age = gr.Number(label="Age")
                gender = gr.Radio(choices=["Male", "Female"], label="Sex")

                cna_events = gr.Textbox(
                    lines=7,
                    placeholder="Copy Number Alterations\n\nExample: RAF1 2 | PPARG 2",
                    label="Genes with CNA Events",
                )

                mutations = gr.Textbox(
                    lines=7,
                    placeholder=(
                        "Somatic Mutations\n\nExample: ERBB2, chr17, 37868208, C, T | "
                        "CDKN1A, chr6, 36651880, -, GTCAGAACCGGCTGGGGATGTCC"
                    ),
                    label="Mutations",
                )

                submit_button_manual = gr.Button("Submit")

            with gr.Column():
                predictions_output_m = gr.Textbox(label="Top 3 Predicted Cancer Types")
                category_explanation_m = gr.Markdown(markdown_text, elem_classes="markdown-box")

                image_m = gr.Image(label="Image Display")

                output_selector_m = gr.Dropdown(
                    choices=cancer_types_to_consider,
                    label="Output Options",
                    filterable=True,
                )

                gr.Markdown(
                    "For more details regarding OncoNPC feature processing, OncoNPC performance, and clinical implications of OncoNPC predictions, "
                    "please see our [manuscript](https://www.nature.com/articles/s41591-023-02482-6).",
                    elem_classes="markdown-box",
                )

            # Manual submit: compute features/preds for a single sample (id 0) and store in state_manual
            submit_button_manual.click(
                get_preds_min_info,
                inputs=[age, gender, cna_events, mutations, state_manual],
                outputs=[predictions_output_m, category_explanation_m, image_m, state_manual],
            )

            # Manual dropdown: regenerate plot using manual cached state
            output_selector_m.change(
                update_image,
                inputs=[output_selector_m, state_manual],
                outputs=[image_m],
            )

        # ------------------------------------------------------------------
        # Toggle which UI rows are visible based on input mode dropdown
        # ------------------------------------------------------------------
        d.change(show_row, d, [r1, r2])
        d.change(show_row, d, [r3, r4])

    # Launch Gradio app
    demo.launch(debug=True, share=False, server_name=server_name, server_port=server_port)