Repository navigation
Expand file tree
/
Copy pathpredict.py
More file actions
542 lines (474 loc) · 17.8 KB
/
Copy pathpredict.py
File metadata and controls
542 lines (474 loc) · 17.8 KB
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
"""
Simple prediction script for pVACml.
This script:
- Merges pVACtools-style MHC class I and class II outputs for a single case
- Applies the published imputer and label encoders
- Uses the trained Random Forest model to score each epitope
- Writes a single aggregated TSV with updated ML predictions for MHC class I
Inputs (from manuscript/data/predict_new_case_data):
- <sample>.MHC_I.all_epitopes.aggregated.tsv
- <sample>.MHC_I.all_epitopes.tsv
- <sample>.MHC_II.all_epitopes.aggregated.tsv
Usage (from repository root):
python manuscript/scripts/predict.py
Default paths (see ``SAMPLE_NAME``, ``ARTIFACTS_DIR``, ``OUTPUT_DIR`` at bottom of file):
- Inputs: ``manuscript/data/predict_new_case_data/<sample>.*.tsv``
- Artifacts: ``manuscript/manuscript_model/`` (``rf_downsample_model.pkl``, ``trained_imputer.joblib``, ``label_encoders.pkl``)
- Output: ``manuscript/data/manuscript_prediction_results/``
Environment: install ``manuscript/requirements.txt`` (Python 3.13.x and scikit-learn version should match the environment used to train the pickles). Unpickling the RF can require ``imbalanced-learn`` if the model is a ``BalancedRandomForestClassifier``.
This implementation is adapted from pVACtools' ml_predictor module, but restricted to the pVACml repository layout.
"""
from __future__ import annotations
from pathlib import Path
from typing import Tuple
import joblib
import numpy as np
import pandas as pd
import pickle
def _get_repo_paths() -> Tuple[Path, Path, Path]:
"""Return base, data, and default artifacts directories for this repo."""
# predict.py lives at manuscript/scripts/ → repo root is parents[2]
base_dir = Path(__file__).resolve().parents[2]
data_dir = base_dir / "manuscript" / "data" / "predict_new_case_data"
artifacts_dir = base_dir / "manuscript" / "manuscript_model"
return base_dir, data_dir, artifacts_dir
def _resolve_artifact_paths(
artifacts_dir: Path,
artifacts_version: str = "",
) -> Tuple[Path, Path, Path]:
"""
Resolve paths to model, imputer, and label encoders under ``artifacts_dir``.
Layouts:
(1) Flat (manuscript default): ``rf_downsample_model.pkl``, ``trained_imputer.joblib``,
``label_encoders.pkl`` directly in ``artifacts_dir``.
(2) Versioned filenames: ``rf_downsample_model_{version}.pkl``, etc. when ``version`` is set.
(3) Nested folder: ``artifacts_dir / version /`` with the same flat filenames inside.
"""
version = (artifacts_version or "").strip()
if version:
v_flat = (
artifacts_dir / f"rf_downsample_model_{version}.pkl",
artifacts_dir / f"trained_imputer_{version}.joblib",
artifacts_dir / f"label_encoders_{version}.pkl",
)
if v_flat[0].is_file():
return v_flat
nested = artifacts_dir / version
if (nested / "rf_downsample_model.pkl").is_file():
return (
nested / "rf_downsample_model.pkl",
nested / "trained_imputer.joblib",
nested / "label_encoders.pkl",
)
flat = (
artifacts_dir / "rf_downsample_model.pkl",
artifacts_dir / "trained_imputer.joblib",
artifacts_dir / "label_encoders.pkl",
)
if flat[0].is_file():
return flat
raise FileNotFoundError(
f"No model pickle found under {artifacts_dir!s} "
f"(tried flat layout and version {version!r})."
)
def merge_and_prepare_data(
class1_aggregated_path: Path,
class1_all_epitopes_path: Path,
class2_aggregated_path: Path,
) -> pd.DataFrame:
"""
Merge class I / class II files and engineer features for ML prediction.
This is a trimmed version of pvactools' merge_and_prepare_data,
but preserves the model's expected feature space.
"""
print("Reading input files...")
mhc1_agg_columns = [
"ID",
"Index",
"Best Peptide",
"IC50 MT",
"IC50 WT",
"%ile MT",
"%ile WT",
"Allele",
"Allele Expr",
"DNA VAF",
"TSL",
"Num Passing Peptides",
"Pos",
"Prob Pos",
"RNA Depth",
"RNA Expr",
"RNA VAF",
"Ref Match",
"Evaluation",
]
mhc2_agg_columns = [
"ID",
"%ile MT",
"%ile WT",
"IC50 MT",
"IC50 WT",
]
mhc1_allepi_columns = [
"Index",
"MT Epitope Seq",
"HLA Allele",
"Gene of Interest",
"Corresponding Fold Change",
"Variant Type",
"Best MT IC50 Score",
"Best MT Percentile",
"Corresponding WT IC50 Score",
"Corresponding WT Percentile",
"Biotype",
"cysteine_count",
]
predictor_columns = [
"MHCflurry MT IC50 Score",
"MHCflurry MT Percentile",
"MHCflurry WT IC50 Score",
"MHCflurry WT Percentile",
"MHCflurryEL Presentation MT Percentile",
"MHCflurryEL Presentation MT Score",
"MHCflurryEL Presentation WT Percentile",
"MHCflurryEL Presentation WT Score",
"MHCflurryEL Processing MT Score",
"MHCflurryEL Processing WT Score",
"MHCnuggetsI MT IC50 Score",
"MHCnuggetsI MT Percentile",
"MHCnuggetsI WT IC50 Score",
"MHCnuggetsI WT Percentile",
"Median Fold Change",
"NetMHC MT IC50 Score",
"NetMHC MT Percentile",
"NetMHC WT IC50 Score",
"NetMHC WT Percentile",
"NetMHCcons MT IC50 Score",
"NetMHCcons MT Percentile",
"NetMHCcons WT IC50 Score",
"NetMHCcons WT Percentile",
"NetMHCpan MT IC50 Score",
"NetMHCpan MT Percentile",
"NetMHCpan WT IC50 Score",
"NetMHCpan WT Percentile",
"NetMHCpanEL MT Presentation Score",
"NetMHCpanEL MT Percentile",
"NetMHCpanEL WT Presentation Score",
"NetMHCpanEL WT Percentile",
"Peptide Length",
"PickPocket MT IC50 Score",
"PickPocket MT Percentile",
"PickPocket WT IC50 Score",
"PickPocket WT Percentile",
"SMM MT IC50 Score",
"SMM MT Percentile",
"SMM WT IC50 Score",
"SMM WT Percentile",
"SMMPMBEC MT IC50 Score",
"SMMPMBEC MT Percentile",
"SMMPMBEC WT IC50 Score",
"SMMPMBEC WT Percentile",
]
def get_column_info(file_path: Path, requested_columns):
available_cols = pd.read_csv(file_path, sep="\t", nrows=0).columns.tolist()
filtered_cols = [col for col in requested_columns if col in available_cols]
missing_cols = [col for col in requested_columns if col not in available_cols]
if missing_cols:
print(
f"Caution: missing columns in {file_path.name} will be filled with NA: {missing_cols}"
)
return filtered_cols, missing_cols
def add_missing_columns(df: pd.DataFrame, missing_cols) -> pd.DataFrame:
for col in missing_cols:
df[col] = np.nan
return df
mhc1_agg_cols_available, mhc1_agg_cols_missing = get_column_info(
class1_aggregated_path, mhc1_agg_columns
)
mhc1_agg_df = pd.read_csv(
class1_aggregated_path,
sep="\t",
na_values=["NA", "NaN", ""],
keep_default_na=False,
usecols=mhc1_agg_cols_available,
)
mhc1_agg_df = add_missing_columns(mhc1_agg_df, mhc1_agg_cols_missing)
mhc1_allepi_cols_available, mhc1_allepi_cols_missing = get_column_info(
class1_all_epitopes_path, mhc1_allepi_columns + predictor_columns
)
mhc1_allepi_df = pd.read_csv(
class1_all_epitopes_path,
sep="\t",
na_values=["NA", "NaN", ""],
keep_default_na=False,
usecols=mhc1_allepi_cols_available,
)
mhc1_allepi_df = add_missing_columns(mhc1_allepi_df, mhc1_allepi_cols_missing)
mhc2_agg_cols_available, mhc2_agg_cols_missing = get_column_info(
class2_aggregated_path, mhc2_agg_columns
)
mhc2_agg_df = pd.read_csv(
class2_aggregated_path,
sep="\t",
na_values=["NA", "NaN", ""],
keep_default_na=False,
usecols=mhc2_agg_cols_available,
)
mhc2_agg_df = add_missing_columns(mhc2_agg_df, mhc2_agg_cols_missing)
mhc1_agg_df.rename(
columns={
"IC50 MT": "IC50 MT class1",
"IC50 WT": "IC50 WT class1",
"%ile MT": "%ile MT class1",
"%ile WT": "%ile WT class1",
},
inplace=True,
)
mhc2_agg_df.rename(
columns={
"IC50 MT": "IC50 MT class2",
"IC50 WT": "IC50 WT class2",
"%ile MT": "%ile MT class2",
"%ile WT": "%ile WT class2",
},
inplace=True,
)
if mhc1_agg_df.shape[0] != mhc2_agg_df.shape[0]:
print(
"Warning: Class I and Class II aggregated files have different row counts; "
"this may cause NA predictions for some rows."
)
merged_df = pd.merge(mhc1_agg_df, mhc2_agg_df, on="ID")
merged_all = pd.merge(
merged_df,
mhc1_allepi_df,
how="inner",
left_on=["Index", "Best Peptide", "Allele"],
right_on=["Index", "MT Epitope Seq", "HLA Allele"],
)
# Pos parsing
if merged_all["Pos"].dtype == "object":
pos_extracted = merged_all["Pos"].astype(str).str.extract(r"^(\d+)")
merged_all["Pos"] = pos_extracted[0].astype("Int64").astype(float)
else:
merged_all["Pos"] = merged_all["Pos"].astype(float)
# Prob Pos parsing
merged_all["Prob Pos"] = (
merged_all["Prob Pos"]
.fillna("0")
.astype(str)
.str.split(",")
.apply(
lambda x: int(float(x[0])) if x[0].replace(".", "", 1).isdigit() else 0
)
)
merged_all["Prob match"] = merged_all.apply(
lambda row: "True"
if pd.notna(row["Pos"])
and row["Pos"]
in [
int(x)
for x in str(row["Prob Pos"]).split(",")
if x.replace(".", "", 1).isdigit()
]
else "False",
axis=1,
)
merged_all["Prob match"] = merged_all["Prob match"].map(
{"True": True, "False": False}
).astype(bool)
merged_all["Gene of Interest"] = (
merged_all["Gene of Interest"]
.fillna("False")
.map({"True": True, "False": False})
.astype(bool)
)
def convert_tsl_to_int(value):
if pd.isna(value):
return 6
if isinstance(value, str) and value.lower() in ["not supported", "na", "n/a", ""]:
return 6
try:
return int(float(value))
except (ValueError, TypeError):
return 6
merged_all["TSL"] = merged_all["TSL"].apply(convert_tsl_to_int).astype(int)
rename_map_v7 = {
"NetMHCpanEL MT Presentation Score": "NetMHCpanEL MT IC50 Score",
"NetMHCpanEL WT Presentation Score": "NetMHCpanEL WT IC50 Score",
}
merged_all = merged_all.rename(columns=rename_map_v7)
merged_all = merged_all.drop(
columns=["Index", "MT Epitope Seq", "HLA Allele", "Best Peptide", "Allele"]
)
return merged_all
def clean_and_impute_data(
merged_all: pd.DataFrame,
artifacts_dir: Path,
artifacts_version: str = "",
) -> pd.DataFrame:
"""Apply label encoders and imputer from the saved artifacts."""
print("Loading imputer and label encoders...")
_model_path, imputer_path, encoders_path = _resolve_artifact_paths(
artifacts_dir, artifacts_version
)
imputer = joblib.load(imputer_path)
with open(encoders_path, "rb") as f:
label_encoders = pickle.load(f)
exclude_columns = ["ID", "Evaluation"]
columns_to_impute = merged_all.columns.difference(exclude_columns)
excluded_data = merged_all[exclude_columns].copy()
data_to_impute = merged_all[columns_to_impute].copy()
categorical_columns = data_to_impute.select_dtypes(include=["category", "object"]).columns
for col in categorical_columns:
if col in label_encoders:
le = label_encoders[col]
data_to_impute.loc[:, col] = data_to_impute[col].map(
lambda x: le.transform([x])[0]
if x in le.classes_
else le.transform([le.classes_[0]])[0]
)
imputed_data = imputer.transform(data_to_impute)
imputed_data = pd.DataFrame(imputed_data, columns=columns_to_impute)
post_imputed_data = pd.concat(
[excluded_data.reset_index(drop=True), imputed_data.reset_index(drop=True)],
axis=1,
)
return post_imputed_data
def make_ml_predictions(
post_imputed_data: pd.DataFrame,
artifacts_dir: Path,
artifacts_version: str = "",
ml_threshold_accept: float = 0.55,
ml_threshold_reject: float = 0.30,
) -> pd.DataFrame:
"""Load RF model and add prediction columns."""
print("Loading model and making predictions...")
model_path, _imputer_path, _encoders_path = _resolve_artifact_paths(
artifacts_dir, artifacts_version
)
rf_model = joblib.load(model_path)
predict_cols = [
col
for col in post_imputed_data.columns
if col not in ["ID", "Evaluation", "patient_id"]
]
probs = rf_model.predict_proba(post_imputed_data[predict_cols])[:, 1]
post_imputed_data["Accept_pred_prob"] = probs
post_imputed_data["Evaluation_pred"] = np.where(
post_imputed_data["Accept_pred_prob"].isna(),
"Pending",
np.where(
post_imputed_data["Accept_pred_prob"] >= ml_threshold_accept,
"Accept",
np.where(
post_imputed_data["Accept_pred_prob"] > ml_threshold_reject,
"Review",
"Reject",
),
),
)
return post_imputed_data
def create_final_output(
post_imputed_data: pd.DataFrame,
original_agg_file_path: Path,
output_dir: Path,
sample_name: str,
) -> Path:
"""Merge predictions back into the original class I aggregated file."""
print("Writing final prediction file...")
output_dir.mkdir(parents=True, exist_ok=True)
orig_df = pd.read_csv(
original_agg_file_path,
sep="\t",
dtype=str,
keep_default_na=False,
na_values=[],
)
original_columns = list(orig_df.columns)
cols_to_preserve = [c for c in original_columns if c != "Evaluation"]
base_df = orig_df.drop(columns=["Evaluation"])
merged_df = base_df.merge(
post_imputed_data[["ID", "Evaluation_pred", "Accept_pred_prob"]],
on="ID",
how="left",
).rename(columns={"Evaluation_pred": "Evaluation"})
merged_df["Evaluation"] = merged_df["Evaluation"].fillna("Pending")
merged_df["ML Prediction (score)"] = merged_df.apply(
lambda row: "NA"
if (
pd.isna(row["Evaluation"])
or row["Evaluation"] == "Pending"
or pd.isna(row["Accept_pred_prob"])
)
else f"{row['Evaluation']} ({round(row['Accept_pred_prob'], 2)})",
axis=1,
)
merged_df.loc[merged_df["Evaluation"] == "Review", "Evaluation"] = "Pending"
final_df = merged_df.drop(columns=["Accept_pred_prob"])
final_df[cols_to_preserve] = orig_df[cols_to_preserve]
output_file = output_dir / f"{sample_name}.MHC_I.all_epitopes.aggregated.ML_predict.tsv"
final_df.to_csv(output_file, float_format="%.3f", sep="\t", index=False, na_rep="NA")
print(f"ML predictions saved to: {output_file}")
return output_file
def run_predictions(
sample_name: str,
artifacts_dir: Path | None = None,
artifacts_version: str = "",
output_dir: Path | None = None,
ml_threshold_accept: float = 0.55,
ml_threshold_reject: float = 0.30,
) -> Path:
"""End-to-end prediction pipeline for a single case."""
if ml_threshold_reject > ml_threshold_accept:
raise ValueError(
f"ml_threshold_reject ({ml_threshold_reject}) must be <= ml_threshold_accept ({ml_threshold_accept})."
)
_, data_dir, default_artifacts_dir = _get_repo_paths()
artifacts_dir = artifacts_dir or default_artifacts_dir
artifacts_dir = Path(artifacts_dir)
if output_dir is None:
output_dir = Path("results") / "predictions"
output_dir = Path(output_dir)
class1_agg = data_dir / f"{sample_name}.MHC_I.all_epitopes.aggregated.tsv"
class1_all = data_dir / f"{sample_name}.MHC_I.all_epitopes.tsv"
class2_agg = data_dir / f"{sample_name}.MHC_II.all_epitopes.aggregated.tsv"
if not class1_agg.exists():
raise FileNotFoundError(f"Class I aggregated file not found: {class1_agg}")
if not class1_all.exists():
raise FileNotFoundError(f"Class I all epitopes file not found: {class1_all}")
if not class2_agg.exists():
raise FileNotFoundError(f"Class II aggregated file not found: {class2_agg}")
print(f"Starting prediction pipeline using artifacts in directory '{artifacts_dir}'...")
merged_all = merge_and_prepare_data(class1_agg, class1_all, class2_agg)
post_imputed = clean_and_impute_data(merged_all, artifacts_dir, artifacts_version)
post_imputed = make_ml_predictions(
post_imputed,
artifacts_dir,
artifacts_version,
ml_threshold_accept=ml_threshold_accept,
ml_threshold_reject=ml_threshold_reject,
)
output_file = create_final_output(post_imputed, class1_agg, output_dir, sample_name)
print("Prediction pipeline completed successfully.")
return output_file
# Hard-coded configuration for manuscript reproducibility
SAMPLE_NAME = "Test"
ARTIFACTS_DIR = Path(__file__).resolve().parents[2] / "manuscript" / "manuscript_model"
OUTPUT_DIR = (
Path(__file__).resolve().parents[2]
/ "manuscript"
/ "data"
/ "manuscript_prediction_results"
)
ML_THRESHOLD_ACCEPT = 0.55
ML_THRESHOLD_REJECT = 0.30
if __name__ == "__main__":
run_predictions(
sample_name=SAMPLE_NAME,
artifacts_dir=ARTIFACTS_DIR,
output_dir=OUTPUT_DIR,
ml_threshold_accept=ML_THRESHOLD_ACCEPT,
ml_threshold_reject=ML_THRESHOLD_REJECT,
)