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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# --- bootstrap: force-install deps if Hugging Face didn't ---
|
| 2 |
+
#import sys, subprocess
|
| 3 |
+
#def _need(modname):
|
| 4 |
+
# try: __import__(modname); return False
|
| 5 |
+
# except ImportError: return True
|
| 6 |
+
#def _pip(pkg_spec):
|
| 7 |
+
# subprocess.check_call([sys.executable, "-m", "pip", "install", pkg_spec])
|
| 8 |
+
|
| 9 |
+
#PKGS = [
|
| 10 |
+
# ("joblib", "1.4.2", "joblib"),
|
| 11 |
+
# ("numpy", "1.26.4","numpy"),
|
| 12 |
+
# ("pandas", "2.2.2", "pandas"),
|
| 13 |
+
# ("scikit-learn", "1.4.2", "sklearn"),
|
| 14 |
+
# ("scipy", "1.11.4","scipy"),
|
| 15 |
+
# ("shap", "0.46.0","shap"),
|
| 16 |
+
# ("matplotlib", "3.8.4", "matplotlib"),
|
| 17 |
+
# ("openpyxl", "3.1.5", "openpyxl"),
|
| 18 |
+
# ("pillow", None, "PIL"),
|
| 19 |
+
# ("gradio", "4.44.1","gradio"),
|
| 20 |
+
#]
|
| 21 |
+
#for pkg, ver, mod in PKGS:
|
| 22 |
+
# if _need(mod):
|
| 23 |
+
# _pip(f"{pkg}=={ver}" if ver else pkg)
|
| 24 |
+
# now safe to import everything
|
| 25 |
+
import os, json, io, gzip, joblib, traceback
|
| 26 |
+
import numpy as np, pandas as pd
|
| 27 |
+
import matplotlib; matplotlib.use("Agg")
|
| 28 |
+
import matplotlib.pyplot as plt
|
| 29 |
+
from PIL import Image
|
| 30 |
+
import shap
|
| 31 |
+
import gradio as gr
|
| 32 |
+
|
| 33 |
+
# ---- artifacts in repo root ----
|
| 34 |
+
MODEL_PATH = "model_extratrees_ds4.joblib" # or "model_extratrees_ds4.gz"
|
| 35 |
+
FEATS_PATH = "feature_names.json"
|
| 36 |
+
|
| 37 |
+
# if you uploaded a .gz model, uncomment these two lines and delete the next joblib.load:
|
| 38 |
+
# with gzip.open(MODEL_PATH, "rb") as f:
|
| 39 |
+
# model = joblib.load(f)
|
| 40 |
+
model = joblib.load(MODEL_PATH)
|
| 41 |
+
|
| 42 |
+
with open(FEATS_PATH) as f:
|
| 43 |
+
FEATURES = json.load(f)
|
| 44 |
+
|
| 45 |
+
LABEL_NAME = {0: "Benign", 1: "Pathogenic"}
|
| 46 |
+
THRESHOLD = 0.7
|
| 47 |
+
# accept many spellings, case-insensitive
|
| 48 |
+
ID_CANDIDATES = [
|
| 49 |
+
"#Uploaded_Variation", "#Uploaded_variation", "uploaded_variation",
|
| 50 |
+
"Uploaded_Variation", "Uploaded_variation", "variant_id",
|
| 51 |
+
"Variant_ID", "variation_id", "Variant", "ID", "HGVSc", "HGVSp"
|
| 52 |
+
]
|
| 53 |
+
|
| 54 |
+
# ---------- helpers ----------
|
| 55 |
+
def _clean_headers(df):
|
| 56 |
+
df.columns = (df.columns.astype(str)
|
| 57 |
+
.str.replace(r'[\u200b\ufeff]', '', regex=True)
|
| 58 |
+
.str.strip())
|
| 59 |
+
return df
|
| 60 |
+
|
| 61 |
+
def _read_table(file):
|
| 62 |
+
name = getattr(file, "name", str(file))
|
| 63 |
+
if name.lower().endswith((".xlsx", ".xls")):
|
| 64 |
+
df = pd.read_excel(name, engine="openpyxl")
|
| 65 |
+
else:
|
| 66 |
+
# NOTE: pandas python engine does not support low_memory
|
| 67 |
+
try:
|
| 68 |
+
df = pd.read_csv(
|
| 69 |
+
name,
|
| 70 |
+
engine="python",
|
| 71 |
+
sep=None, # sniff delimiter
|
| 72 |
+
dtype=str,
|
| 73 |
+
na_values=["", "NA", "N/A"],
|
| 74 |
+
quoting=3,
|
| 75 |
+
on_bad_lines="skip",
|
| 76 |
+
encoding_errors="ignore",
|
| 77 |
+
)
|
| 78 |
+
except Exception:
|
| 79 |
+
df = None
|
| 80 |
+
for sep in [",", "\t", ";", "|"]:
|
| 81 |
+
try:
|
| 82 |
+
df = pd.read_csv(
|
| 83 |
+
name,
|
| 84 |
+
engine="python",
|
| 85 |
+
sep=sep,
|
| 86 |
+
dtype=str,
|
| 87 |
+
na_values=["", "NA", "N/A"],
|
| 88 |
+
quoting=3,
|
| 89 |
+
on_bad_lines="skip",
|
| 90 |
+
encoding_errors="ignore",
|
| 91 |
+
)
|
| 92 |
+
break
|
| 93 |
+
except Exception:
|
| 94 |
+
pass
|
| 95 |
+
if df is None:
|
| 96 |
+
raise
|
| 97 |
+
return _clean_headers(df)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def _coerce_numeric(df):
|
| 101 |
+
for c in df.columns:
|
| 102 |
+
if df[c].dtype.kind in "OUS":
|
| 103 |
+
df[c] = pd.to_numeric(df[c], errors="coerce")
|
| 104 |
+
return df
|
| 105 |
+
|
| 106 |
+
def _prepare(df):
|
| 107 |
+
for c in FEATURES:
|
| 108 |
+
if c not in df.columns:
|
| 109 |
+
df[c] = np.nan
|
| 110 |
+
return _coerce_numeric(df[FEATURES].copy())
|
| 111 |
+
|
| 112 |
+
def _validate_columns(df):
|
| 113 |
+
missing = [c for c in FEATURES if c not in df.columns]
|
| 114 |
+
extra = [c for c in df.columns if c not in FEATURES]
|
| 115 |
+
return missing, extra
|
| 116 |
+
|
| 117 |
+
def _pick_id_series(df):
|
| 118 |
+
# case-insensitive lookup without altering original headers
|
| 119 |
+
norm = {c.lower(): c for c in df.columns}
|
| 120 |
+
for cand in ID_CANDIDATES:
|
| 121 |
+
key = cand.lower()
|
| 122 |
+
if key in norm:
|
| 123 |
+
col = norm[key]
|
| 124 |
+
return df[col].astype(str), col
|
| 125 |
+
# fallback
|
| 126 |
+
return pd.Series(df.index, index=df.index, name="row_index"), "row_index"
|
| 127 |
+
|
| 128 |
+
# ---------- app fns ----------
|
| 129 |
+
def predict_csv(file):
|
| 130 |
+
try:
|
| 131 |
+
if file is None: return None, "No file."
|
| 132 |
+
df = _read_table(file)
|
| 133 |
+
missing, _ = _validate_columns(df)
|
| 134 |
+
if missing: return None, f"Missing {len(missing)} feature(s). First 10: {missing[:10]}"
|
| 135 |
+
X = _prepare(df)
|
| 136 |
+
|
| 137 |
+
proba = model.predict_proba(X)[:, 1]
|
| 138 |
+
pred = (proba > THRESHOLD).astype(int)
|
| 139 |
+
|
| 140 |
+
id_series, id_name = _pick_id_series(df)
|
| 141 |
+
id_label = "Variant ID" if id_name != "row_index" else "Variant ID (#Uploaded_variation not found)"
|
| 142 |
+
|
| 143 |
+
out = pd.DataFrame({
|
| 144 |
+
id_label: id_series.values,
|
| 145 |
+
"Predicted probability (pathogenic)": proba,
|
| 146 |
+
"Predicted class": np.where(pred == 1, LABEL_NAME[1], LABEL_NAME[0]),
|
| 147 |
+
})
|
| 148 |
+
|
| 149 |
+
n = len(out)
|
| 150 |
+
n_path = int((out["Predicted class"] == "Pathogenic").sum())
|
| 151 |
+
n_ben = n - n_path
|
| 152 |
+
txt = (
|
| 153 |
+
f"Processed {n} records at threshold {THRESHOLD:.2f}. "
|
| 154 |
+
f"Pathogenic: {n_path} ({(n_path/n*100):.1f}%). "
|
| 155 |
+
f"Benign: {n_ben} ({(n_ben/n*100):.1f}%). "
|
| 156 |
+
"Model probabilities are supportive only; clinical reporting must follow ACMG/AMP."
|
| 157 |
+
)
|
| 158 |
+
return out, txt
|
| 159 |
+
except Exception:
|
| 160 |
+
return None, "ERROR:\n" + traceback.format_exc()
|
| 161 |
+
|
| 162 |
+
def explain_first_row(file):
|
| 163 |
+
try:
|
| 164 |
+
if file is None: return None, "No file."
|
| 165 |
+
df = _read_table(file)
|
| 166 |
+
missing, _ = _validate_columns(df)
|
| 167 |
+
if missing: return None, f"Missing features for SHAP. First 10: {missing[:10]}"
|
| 168 |
+
X = _prepare(df)
|
| 169 |
+
if X.shape[0] == 0: return None, "No rows."
|
| 170 |
+
|
| 171 |
+
X_imp = pd.DataFrame(model.named_steps["imputer"].transform(X), columns=FEATURES)
|
| 172 |
+
clf = model.named_steps["clf"]
|
| 173 |
+
|
| 174 |
+
bg = X_imp.sample(min(200, len(X_imp)), random_state=0)
|
| 175 |
+
expl = shap.Explainer(clf.predict_proba, bg, feature_names=FEATURES, algorithm="permutation")
|
| 176 |
+
ex = expl(X_imp.iloc[[0]])
|
| 177 |
+
vals = np.array(ex.values)
|
| 178 |
+
vals_1d = vals[0, :, 1] if vals.ndim == 3 else vals[0]
|
| 179 |
+
s = pd.Series(vals_1d, index=FEATURES).sort_values(key=np.abs, ascending=False).head(15)
|
| 180 |
+
|
| 181 |
+
fig = plt.figure(figsize=(7,5))
|
| 182 |
+
ax = fig.add_subplot(111)
|
| 183 |
+
s.iloc[::-1].plot(kind="barh", ax=ax, color="#d63384")
|
| 184 |
+
ax.set_title("SHAP values (Sample #1)")
|
| 185 |
+
fig.tight_layout()
|
| 186 |
+
|
| 187 |
+
# clinical note: map top features to ACMG evidence “families”
|
| 188 |
+
feats = [f.lower() for f in s.index.tolist()]
|
| 189 |
+
acmg_tags = []
|
| 190 |
+
if any("af" in f or "gnomad" in f or "freq" in f for f in feats):
|
| 191 |
+
acmg_tags.append("Population frequency → BA1/BS1 (benign thresholds) context")
|
| 192 |
+
if any("score" in f or "rankscore" in f or "meta" in f or "deleterious" in f for f in feats):
|
| 193 |
+
acmg_tags.append("Computational evidence → PP3 (supporting pathogenic) or BP4 (supporting benign)")
|
| 194 |
+
if any("domain" in f or "hotspot" in f or "pfam" in f for f in feats):
|
| 195 |
+
acmg_tags.append("Protein domain/hotspot → PM1 (moderate) if curated hotspot")
|
| 196 |
+
if any("blosum" in f or "aminoacid" in f or "aa" in f for f in feats):
|
| 197 |
+
acmg_tags.append("Same-residue considerations → PM5 possible with prior pathogenic missense")
|
| 198 |
+
|
| 199 |
+
tag_txt = " | ".join(acmg_tags) if acmg_tags else "Top features map to PP3/BP4 computational evidence."
|
| 200 |
+
note = (
|
| 201 |
+
"Clinical note for row #1: positive bars increase pathogenic probability; negative decrease it. "
|
| 202 |
+
f"ACMG relevance: {tag_txt}. "
|
| 203 |
+
"These signals are supportive only; final classification requires full ACMG/AMP evaluation with thresholds and curated evidence."
|
| 204 |
+
)
|
| 205 |
+
return fig, note
|
| 206 |
+
except Exception:
|
| 207 |
+
return None, "ERROR:\n" + traceback.format_exc()
|
| 208 |
+
|
| 209 |
+
# ---------- UI ----------
|
| 210 |
+
with gr.Blocks(title="Pathogenicity Predictor") as demo:
|
| 211 |
+
gr.HTML("""
|
| 212 |
+
<h1 style='text-align:center; color:#d63384; font-size:28px; margin-top:10px;'>
|
| 213 |
+
Pathogenicity Predictor
|
| 214 |
+
</h1>
|
| 215 |
+
""")
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
inp = gr.File(label="Upload CSV/XLSX containing the trained feature columns")
|
| 221 |
+
with gr.Row():
|
| 222 |
+
btn_pred = gr.Button("Predict")
|
| 223 |
+
btn_expl = gr.Button("Explain Sample #1")
|
| 224 |
+
out_df = gr.Dataframe(label="Predictions")
|
| 225 |
+
out_plot = gr.Plot(label="SHAP (Sample #1)")
|
| 226 |
+
out_txt = gr.Markdown()
|
| 227 |
+
btn_pred.click(fn=predict_csv, inputs=inp, outputs=[out_df, out_txt])
|
| 228 |
+
btn_expl.click(fn=explain_first_row, inputs=inp, outputs=[out_plot, out_txt])
|
| 229 |
+
|
| 230 |
+
if __name__ == "__main__":
|
| 231 |
+
port_env = os.environ.get("GRADIO_SERVER_PORT", "")
|
| 232 |
+
port = int(port_env) if port_env.isdigit() else None
|
| 233 |
+
demo.launch(server_name="0.0.0.0", server_port=port, share=False, debug=True)
|