rahafahmad89 commited on
Commit
f2302fc
·
verified ·
1 Parent(s): ed6aa9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -117
app.py CHANGED
@@ -1,106 +1,96 @@
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):
@@ -115,119 +105,136 @@ def _validate_columns(df):
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)
 
 
 
 
 
1
+ import os, json, traceback, gzip, joblib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import numpy as np, pandas as pd
3
+
4
+ import matplotlib
5
+ matplotlib.use("Agg")
6
  import matplotlib.pyplot as plt
7
+
8
  import shap
9
  import gradio as gr
10
 
11
+ # =========================
12
+ # Paths / constants
13
+ # =========================
14
+ MODEL_PATH = "model_extratrees_ds4.joblib" # or .gz
15
  FEATS_PATH = "feature_names.json"
16
 
 
 
 
 
 
 
 
 
17
  LABEL_NAME = {0: "Benign", 1: "Pathogenic"}
18
  THRESHOLD = 0.7
19
+
20
  ID_CANDIDATES = [
21
  "#Uploaded_Variation", "#Uploaded_variation", "uploaded_variation",
22
  "Uploaded_Variation", "Uploaded_variation", "variant_id",
23
  "Variant_ID", "variation_id", "Variant", "ID", "HGVSc", "HGVSp"
24
  ]
25
 
26
+ # =========================
27
+ # Load model
28
+ # =========================
29
+ if MODEL_PATH.endswith(".gz"):
30
+ with gzip.open(MODEL_PATH, "rb") as f:
31
+ model = joblib.load(f)
32
+ else:
33
+ model = joblib.load(MODEL_PATH)
34
+
35
+ with open(FEATS_PATH) as f:
36
+ FEATURES = json.load(f)
37
+
38
+ IS_PIPELINE = hasattr(model, "named_steps")
39
+
40
+ # =========================
41
+ # Helpers
42
+ # =========================
43
  def _clean_headers(df):
44
+ df.columns = (
45
+ df.columns.astype(str)
46
+ .str.replace(r'[\u200b\ufeff]', '', regex=True)
47
+ .str.strip()
48
+ )
49
  return df
50
 
51
  def _read_table(file):
52
+ if file is None:
53
+ raise ValueError("No file provided")
54
+
55
+ path = file.name if hasattr(file, "name") else file
56
+
57
+ if path.lower().endswith((".xlsx", ".xls")):
58
+ df = pd.read_excel(path, engine="openpyxl")
59
  else:
 
60
  try:
61
  df = pd.read_csv(
62
+ path,
63
  engine="python",
64
+ sep=None,
65
  dtype=str,
 
66
  quoting=3,
67
  on_bad_lines="skip",
68
+ encoding_errors="ignore"
69
  )
70
  except Exception:
71
  df = None
72
  for sep in [",", "\t", ";", "|"]:
73
  try:
74
  df = pd.read_csv(
75
+ path,
76
  engine="python",
77
  sep=sep,
78
  dtype=str,
 
79
  quoting=3,
80
  on_bad_lines="skip",
81
+ encoding_errors="ignore"
82
  )
83
  break
84
  except Exception:
85
  pass
86
  if df is None:
87
  raise
 
88
 
89
+ return _clean_headers(df)
90
 
91
  def _coerce_numeric(df):
92
  for c in df.columns:
93
+ df[c] = pd.to_numeric(df[c], errors="coerce")
 
94
  return df
95
 
96
  def _prepare(df):
 
105
  return missing, extra
106
 
107
  def _pick_id_series(df):
 
108
  norm = {c.lower(): c for c in df.columns}
109
  for cand in ID_CANDIDATES:
110
+ if cand.lower() in norm:
111
+ col = norm[cand.lower()]
 
112
  return df[col].astype(str), col
 
113
  return pd.Series(df.index, index=df.index, name="row_index"), "row_index"
114
 
115
+ # =========================
116
+ # Prediction
117
+ # =========================
118
  def predict_csv(file):
119
  try:
 
120
  df = _read_table(file)
121
  missing, _ = _validate_columns(df)
122
+ if missing:
123
+ return None, f"Missing {len(missing)} features. First 10: {missing[:10]}"
124
+
125
  X = _prepare(df)
126
 
127
  proba = model.predict_proba(X)[:, 1]
128
  pred = (proba > THRESHOLD).astype(int)
129
 
130
  id_series, id_name = _pick_id_series(df)
131
+ id_label = "Variant ID" if id_name != "row_index" else "Variant ID (row index)"
132
 
133
  out = pd.DataFrame({
134
  id_label: id_series.values,
135
  "Predicted probability (pathogenic)": proba,
136
+ "Predicted class": np.where(pred == 1, "Pathogenic", "Benign"),
137
  })
138
 
139
  n = len(out)
140
  n_path = int((out["Predicted class"] == "Pathogenic").sum())
141
+
142
  txt = (
143
  f"Processed {n} records at threshold {THRESHOLD:.2f}. "
144
+ f"Pathogenic: {n_path} ({(n_path/n*100):.1f}%).\n\n"
145
+ "⚠️ Model output is *supportive only*. "
146
+ "Final classification must follow ACMG/AMP guidelines."
147
  )
148
+
149
  return out, txt
150
+
151
  except Exception:
152
+ return None, "ERROR:\n```text\n" + traceback.format_exc() + "\n```"
153
 
154
+ # =========================
155
+ # SHAP (safe & bounded)
156
+ # =========================
157
  def explain_first_row(file):
158
  try:
 
159
  df = _read_table(file)
160
  missing, _ = _validate_columns(df)
161
+ if missing:
162
+ return None, f"Missing features for SHAP. First 10: {missing[:10]}"
163
+
164
  X = _prepare(df)
165
+ if len(X) == 0:
166
+ return None, "No rows in file."
167
 
168
+ if IS_PIPELINE:
169
+ X_imp = pd.DataFrame(
170
+ model.named_steps["imputer"].transform(X),
171
+ columns=FEATURES
172
+ )
173
+ clf = model.named_steps["clf"]
174
+ else:
175
+ X_imp = X
176
+ clf = model
177
+
178
+ bg = X_imp.sample(min(50, len(X_imp)), random_state=0)
179
+
180
+ explainer = shap.Explainer(
181
+ clf.predict_proba,
182
+ bg,
183
+ feature_names=FEATURES,
184
+ algorithm="permutation"
185
+ )
186
 
187
+ explanation = explainer(X_imp.iloc[[0]])
188
+ vals = explanation.values
 
 
189
  vals_1d = vals[0, :, 1] if vals.ndim == 3 else vals[0]
 
190
 
191
+ s = (
192
+ pd.Series(vals_1d, index=FEATURES)
193
+ .sort_values(key=np.abs, ascending=False)
194
+ .head(15)
195
+ )
196
+
197
+ fig = plt.figure(figsize=(7, 5))
198
  ax = fig.add_subplot(111)
199
+ s.iloc[::-1].plot(kind="barh", ax=ax)
200
  ax.set_title("SHAP values (Sample #1)")
201
  fig.tight_layout()
202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  note = (
204
+ "Positive bars increase pathogenic probability; negative bars decrease it.\n\n"
205
+ "SHAP features correspond primarily to ACMG PP3/BP4 (computational evidence).\n"
206
+ "Do not use in isolation for clinical reporting."
207
  )
208
+
209
  return fig, note
210
+
211
  except Exception:
212
+ return None, "ERROR:\n```text\n" + traceback.format_exc() + "\n```"
213
 
214
+ # =========================
215
+ # UI
216
+ # =========================
217
  with gr.Blocks(title="Pathogenicity Predictor") as demo:
218
+ gr.HTML(
219
+ "<h1 style='text-align:center; color:#d63384;'>"
220
+ "Pathogenicity Predictor</h1>"
221
+ )
 
 
222
 
223
+ inp = gr.File(label="Upload CSV / XLSX with trained features")
224
 
 
 
225
  with gr.Row():
226
  btn_pred = gr.Button("Predict")
227
+ btn_expl = gr.Button("Explain Sample #1 (SHAP)")
228
+
229
  out_df = gr.Dataframe(label="Predictions")
230
+ out_plot = gr.Plot(label="SHAP Explanation")
231
  out_txt = gr.Markdown()
 
 
232
 
233
+ btn_pred.click(predict_csv, inp, [out_df, out_txt])
234
+ btn_expl.click(explain_first_row, inp, [out_plot, out_txt])
235
+
236
+ # =========================
237
+ # Launch (HF SAFE)
238
+ # =========================
239
+ demo.queue(concurrency_count=1)
240
+ demo.launch()