|
| 1 | +"""Split an MSK panel MAF into per-cancer-type sub-cohort MAFs for DIALECT. |
| 2 | +
|
| 3 | +MSK-IMPACT/MSK-CHORD pool many cancer types in one cohort; running DIALECT on the pooled |
| 4 | +conflates a cancer-type-composition confound with real CO. This splits the MAF by the |
| 5 | +clinical ``CANCER_TYPE`` (broad level; ONCOTREE_CODE is far too granular -- 502 |
| 6 | +codes for IMPACT) into one MAF per type with at least ``--min-samples`` mutated samples, |
| 7 | +mirroring the TCGA per-cohort design. |
| 8 | +
|
| 9 | +Usage:: |
| 10 | +
|
| 11 | + python scripts/split_msk_by_cancer_type.py \ |
| 12 | + --maf data/mafs_msk/MSK_IMPACT_2026.maf \ |
| 13 | + --clinical data/mafs_msk/MSK_IMPACT_2026.clinical.txt \ |
| 14 | + --out data/mafs_msk_split/IMPACT2026 --min-samples 100 |
| 15 | +""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import argparse |
| 20 | +import io |
| 21 | +import re |
| 22 | +from pathlib import Path |
| 23 | + |
| 24 | +import pandas as pd |
| 25 | + |
| 26 | + |
| 27 | +def slug(name: str) -> str: |
| 28 | + """Filesystem-safe cohort label from a cancer-type string.""" |
| 29 | + s = re.sub(r"[^0-9A-Za-z]+", "_", str(name)).strip("_") |
| 30 | + return s or "UNKNOWN" |
| 31 | + |
| 32 | + |
| 33 | +def load_sample_to_type(clinical: Path, field: str) -> dict: |
| 34 | + """Map SAMPLE_ID -> cancer type, skipping cBioPortal's leading '#' comment lines.""" |
| 35 | + with clinical.open() as fh: |
| 36 | + lines = [ln for ln in fh if not ln.startswith("#")] |
| 37 | + df = pd.read_csv(io.StringIO("".join(lines)), sep="\t", dtype=str) |
| 38 | + return dict(zip(df["SAMPLE_ID"], df[field], strict=False)) |
| 39 | + |
| 40 | + |
| 41 | +def main() -> None: |
| 42 | + """Write one per-cancer-type MAF for each type with enough mutated samples.""" |
| 43 | + ap = argparse.ArgumentParser(description=__doc__) |
| 44 | + ap.add_argument("--maf", type=Path, required=True) |
| 45 | + ap.add_argument("--clinical", type=Path, required=True) |
| 46 | + ap.add_argument("--out", type=Path, required=True) |
| 47 | + ap.add_argument("--field", default="CANCER_TYPE") |
| 48 | + ap.add_argument("--min-samples", type=int, default=100) |
| 49 | + args = ap.parse_args() |
| 50 | + args.out.mkdir(parents=True, exist_ok=True) |
| 51 | + |
| 52 | + sample_to_type = load_sample_to_type(args.clinical, args.field) |
| 53 | + maf = pd.read_csv(args.maf, sep="\t", low_memory=False, comment="#") |
| 54 | + matched = maf["Tumor_Sample_Barcode"].isin(sample_to_type) |
| 55 | + print(f"MAF rows: {len(maf)}; sample-barcode match rate: {matched.mean():.1%}") |
| 56 | + maf = maf[matched].copy() |
| 57 | + maf["_TYPE"] = maf["Tumor_Sample_Barcode"].map(sample_to_type) |
| 58 | + |
| 59 | + kept, dropped = [], [] |
| 60 | + for ctype, grp in maf.groupby("_TYPE"): |
| 61 | + n_samp = grp["Tumor_Sample_Barcode"].nunique() |
| 62 | + label = slug(ctype) |
| 63 | + if n_samp < args.min_samples: |
| 64 | + dropped.append((label, n_samp)) |
| 65 | + continue |
| 66 | + out_fn = args.out / f"{label}.maf" |
| 67 | + grp.drop(columns="_TYPE").to_csv(out_fn, sep="\t", index=False) |
| 68 | + kept.append((label, n_samp, len(grp))) |
| 69 | + |
| 70 | + kept.sort(key=lambda x: -x[1]) |
| 71 | + print(f"\nKept {len(kept)} cohorts (>= {args.min_samples} samples):") |
| 72 | + for label, n_samp, n_mut in kept: |
| 73 | + print(f" {label:<45} {n_samp:>6} samples {n_mut:>8} muts") |
| 74 | + print(f"\nDropped {len(dropped)} types below threshold " |
| 75 | + f"({sum(n for _, n in dropped)} samples total).") |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + main() |
0 commit comments