# --- bootstrap: force-install deps if Hugging Face didn't --- #import sys, subprocess #def _need(modname): # try: __import__(modname); return False # except ImportError: return True #def _pip(pkg_spec): # subprocess.check_call([sys.executable, "-m", "pip", "install", pkg_spec]) #PKGS = [ # ("joblib", "1.4.2", "joblib"), # ("numpy", "1.26.4","numpy"), # ("pandas", "2.2.2", "pandas"), # ("scikit-learn", "1.4.2", "sklearn"), # ("scipy", "1.11.4","scipy"), # ("shap", "0.46.0","shap"), # ("matplotlib", "3.8.4", "matplotlib"), # ("openpyxl", "3.1.5", "openpyxl"), # ("pillow", None, "PIL"), # ("gradio", "4.44.1","gradio"), #] #for pkg, ver, mod in PKGS: # if _need(mod): # _pip(f"{pkg}=={ver}" if ver else pkg) # now safe to import everything import os, json, io, gzip, joblib, traceback import numpy as np, pandas as pd import matplotlib; matplotlib.use("Agg") import matplotlib.pyplot as plt from PIL import Image import shap import gradio as gr import gradio as gr # --- PATCH: gradio_client json schema parser fails on boolean schemas --- try: import gradio_client.utils as _gcu # Patch the low-level parser used by json_schema_to_python_type if hasattr(_gcu, "_json_schema_to_python_type"): _orig = _gcu._json_schema_to_python_type def _patched(schema, defs=None): # JSON Schema allows boolean schemas (True/False). Treat them as "Any". if isinstance(schema, bool): return "Any" return _orig(schema, defs) _gcu._json_schema_to_python_type = _patched except Exception: pass # ---- artifacts in repo root ---- MODEL_PATH = "model_extratrees_ds4.joblib" # or "model_extratrees_ds4.gz" FEATS_PATH = "feature_names.json" # if you uploaded a .gz model, uncomment these two lines and delete the next joblib.load: # with gzip.open(MODEL_PATH, "rb") as f: # model = joblib.load(f) model = joblib.load(MODEL_PATH) with open(FEATS_PATH) as f: FEATURES = json.load(f) LABEL_NAME = {0: "Benign", 1: "Pathogenic"} THRESHOLD = 0.7 # accept many spellings, case-insensitive ID_CANDIDATES = [ "#Uploaded_Variation", "#Uploaded_variation", "uploaded_variation", "Uploaded_Variation", "Uploaded_variation", "variant_id", "Variant_ID", "variation_id", "Variant", "ID", "HGVSc", "HGVSp" ] # ---------- helpers ---------- def _clean_headers(df): df.columns = (df.columns.astype(str) .str.replace(r'[\u200b\ufeff]', '', regex=True) .str.strip()) return df def _read_table(file): name = getattr(file, "name", str(file)) if name.lower().endswith((".xlsx", ".xls")): df = pd.read_excel(name, engine="openpyxl") else: # NOTE: pandas python engine does not support low_memory try: df = pd.read_csv( name, engine="python", sep=None, # sniff delimiter dtype=str, na_values=["", "NA", "N/A"], quoting=3, on_bad_lines="skip", encoding_errors="ignore", ) except Exception: df = None for sep in [",", "\t", ";", "|"]: try: df = pd.read_csv( name, engine="python", sep=sep, dtype=str, na_values=["", "NA", "N/A"], quoting=3, on_bad_lines="skip", encoding_errors="ignore", ) break except Exception: pass if df is None: raise return _clean_headers(df) def _coerce_numeric(df): for c in df.columns: if df[c].dtype.kind in "OUS": df[c] = pd.to_numeric(df[c], errors="coerce") return df def _prepare(df): for c in FEATURES: if c not in df.columns: df[c] = np.nan return _coerce_numeric(df[FEATURES].copy()) def _validate_columns(df): missing = [c for c in FEATURES if c not in df.columns] extra = [c for c in df.columns if c not in FEATURES] return missing, extra def _pick_id_series(df): # case-insensitive lookup without altering original headers norm = {c.lower(): c for c in df.columns} for cand in ID_CANDIDATES: key = cand.lower() if key in norm: col = norm[key] return df[col].astype(str), col # fallback return pd.Series(df.index, index=df.index, name="row_index"), "row_index" # ---------- app fns ---------- def predict_csv(file): try: if file is None: return None, "No file." df = _read_table(file) missing, _ = _validate_columns(df) if missing: return None, f"Missing {len(missing)} feature(s). First 10: {missing[:10]}" X = _prepare(df) proba = model.predict_proba(X)[:, 1] pred = (proba > THRESHOLD).astype(int) id_series, id_name = _pick_id_series(df) id_label = "Variant ID" if id_name != "row_index" else "Variant ID (#Uploaded_variation not found)" out = pd.DataFrame({ id_label: id_series.values, "Predicted probability (pathogenic)": proba, "Predicted class": np.where(pred == 1, LABEL_NAME[1], LABEL_NAME[0]), }) n = len(out) n_path = int((out["Predicted class"] == "Pathogenic").sum()) n_ben = n - n_path txt = ( f"Processed {n} records at threshold {THRESHOLD:.2f}. " f"Pathogenic: {n_path} ({(n_path/n*100):.1f}%). " f"Benign: {n_ben} ({(n_ben/n*100):.1f}%). " "Model probabilities are supportive only; clinical reporting must follow ACMG/AMP." ) return out, txt except Exception: return None, "ERROR:\n" + traceback.format_exc() def explain_first_row(file): try: if file is None: return None, "No file." df = _read_table(file) missing, _ = _validate_columns(df) if missing: return None, f"Missing features for SHAP. First 10: {missing[:10]}" X = _prepare(df) if X.shape[0] == 0: return None, "No rows." X_imp = pd.DataFrame(model.named_steps["imputer"].transform(X), columns=FEATURES) clf = model.named_steps["clf"] bg = X_imp.sample(min(200, len(X_imp)), random_state=0) expl = shap.Explainer(clf.predict_proba, bg, feature_names=FEATURES, algorithm="permutation") ex = expl(X_imp.iloc[[0]]) vals = np.array(ex.values) vals_1d = vals[0, :, 1] if vals.ndim == 3 else vals[0] s = pd.Series(vals_1d, index=FEATURES).sort_values(key=np.abs, ascending=False).head(15) fig = plt.figure(figsize=(7,5)) ax = fig.add_subplot(111) s.iloc[::-1].plot(kind="barh", ax=ax, color="#d63384") ax.set_title("SHAP values (Sample #1)") fig.tight_layout() # clinical note: map top features to ACMG evidence “families” feats = [f.lower() for f in s.index.tolist()] acmg_tags = [] if any("af" in f or "gnomad" in f or "freq" in f for f in feats): acmg_tags.append("Population frequency → BA1/BS1 (benign thresholds) context") if any("score" in f or "rankscore" in f or "meta" in f or "deleterious" in f for f in feats): acmg_tags.append("Computational evidence → PP3 (supporting pathogenic) or BP4 (supporting benign)") if any("domain" in f or "hotspot" in f or "pfam" in f for f in feats): acmg_tags.append("Protein domain/hotspot → PM1 (moderate) if curated hotspot") if any("blosum" in f or "aminoacid" in f or "aa" in f for f in feats): acmg_tags.append("Same-residue considerations → PM5 possible with prior pathogenic missense") tag_txt = " | ".join(acmg_tags) if acmg_tags else "Top features map to PP3/BP4 computational evidence." note = ( "Clinical note for row #1: positive bars increase pathogenic probability; negative decrease it. " f"ACMG relevance: {tag_txt}. " "These signals are supportive only; final classification requires full ACMG/AMP evaluation with thresholds and curated evidence." ) return fig, note except Exception: return None, "ERROR:\n" + traceback.format_exc() # ---------- UI ---------- with gr.Blocks(title="Pathogenicity Predictor") as demo: gr.HTML("""