onconpc-visualization / codes /gradio_utils.py
itmoon7's picture
Fix Gradio cache cleanup breaking update_image
5484f77 verified
Raw History Blame Contribute Delete
35.7 kB
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)