From 49aa7e019a32874a5102afa4165b023440e7d8bf Mon Sep 17 00:00:00 2001 From: jpalmer37 Date: Tue, 20 Oct 2026 13:48:33 -0400 Subject: [PATCH] Key MNV codon corrections by allele, not position A deletion or second allele sharing a POS with a corrected SNV inherited its codon correction or MNV_UNRESOLVED flag. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01A64XHGNZMSco3L8CbejfVw --- bin/fix_mnv_codons.py | 36 +++++++++++-------- tests/test_fix_mnv_codons.py | 69 ++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 15 deletions(-) create mode 100644 tests/test_fix_mnv_codons.py diff --git a/bin/fix_mnv_codons.py b/bin/fix_mnv_codons.py index a409541..63509ea 100755 --- a/bin/fix_mnv_codons.py +++ b/bin/fix_mnv_codons.py @@ -128,6 +128,9 @@ class ANN(IntEnum): # Data structures # --------------------------------------------------------------------------- +# (chrom, pos, ref, alt): the one VCF record a correction belongs to +VariantKey = Tuple[str, int, str, str] + @dataclass(slots=True) class CodonSNV: """One SNV contributing to a codon group for a specific transcript.""" @@ -141,9 +144,13 @@ class CodonSNV: ref_aa_1: Optional[str] hgvs_c_ref_nuc: Optional[str] + @property + def key(self) -> VariantKey: + return (self.chrom, self.vcf_pos, self.ref_nuc, self.alt_nuc) + @dataclass(slots=True) class AnnCorrection: - """Correction to apply to a specific (chrom, vcf_pos, feature_id) triplet.""" + """Correction to one transcript's ANN entry on a (chrom, pos, ref, alt) record.""" feature_id: str protein_pos: int ref_aa_1: str @@ -453,15 +460,14 @@ def collect_codon_groups(vcf_path: str, min_af: float) -> Dict[Tuple[str, str, i def compute_corrections( codon_groups: Dict[Tuple[str, str, int], List[CodonSNV]], ref_seqs: Optional[Dict[str, str]] = None, -) -> Tuple[Dict[Tuple[str, int], List[AnnCorrection]], Set[Tuple[str, int]]]: +) -> Tuple[Dict[VariantKey, List[AnnCorrection]], Set[VariantKey]]: """For each codon group with >=2 SNVs, resolve the true combined AA change. - Returns (corrections, unresolved_positions) where unresolved_positions is - the set of (chrom, vcf_pos) tuples belonging to codon groups that were - detected but could not be resolved. + Returns (corrections, unresolved) keyed by (chrom, pos, ref, alt), where unresolved + holds the SNVs of codon groups that were detected but could not be resolved. """ - corrections: Dict[Tuple[str, int], List[AnnCorrection]] = defaultdict(list) - unresolved: Set[Tuple[str, int]] = set() + corrections: Dict[VariantKey, List[AnnCorrection]] = defaultdict(list) + unresolved: Set[VariantKey] = set() n_groups = n_corrected = n_skipped = 0 for (chrom, feature_id, codon_number), snvs in codon_groups.items(): @@ -492,7 +498,7 @@ def compute_corrections( if resolved is None: n_skipped += 1 for snv in snvs: - unresolved.add((chrom, snv.vcf_pos)) + unresolved.add(snv.key) continue ref_codon, mut_codon, ref_aa_1, true_alt_aa_1 = resolved @@ -501,7 +507,7 @@ def compute_corrections( mut_codon, chrom, feature_id, codon_number) n_skipped += 1 for snv in snvs: - unresolved.add((chrom, snv.vcf_pos)) + unresolved.add(snv.key) continue protein_pos = snvs[0].protein_pos @@ -514,7 +520,7 @@ def compute_corrections( ref_aa_1=ref_aa_1, true_alt_aa_1=true_alt_aa_1, codon_group_id=codon_group_id) for snv in snvs: - corrections[(chrom, snv.vcf_pos)].append(correction) + corrections[snv.key].append(correction) n_corrected += 1 log.info("Codon groups: %d found, %d correctable, %d skipped", n_groups, n_corrected, n_skipped) @@ -546,8 +552,8 @@ def apply_corrections_to_ann(raw_ann: str, corrections: List[AnnCorrection]) -> def write_corrected_vcf( vcf_path: str, output_vcf_path: str, - corrections: Dict[Tuple[str, int], List[AnnCorrection]], - unresolved_positions: Set[Tuple[str, int]], + corrections: Dict[VariantKey, List[AnnCorrection]], + unresolved: Set[VariantKey], ) -> int: """Emit all records to a new VCF, correcting ANN fields for MNV groups. Returns the count of corrected records.""" @@ -572,7 +578,7 @@ def write_corrected_vcf( try: for variant in vcf: - key = (variant.CHROM, variant.POS) + key = (variant.CHROM, variant.POS, variant.REF.upper(), ",".join(variant.ALT).upper()) correction_list = corrections.get(key) if correction_list: raw_ann = variant.INFO.get("ANN") @@ -591,7 +597,7 @@ def write_corrected_vcf( if orig_hgvs: variant.INFO["MNV_ORIGINAL_HGVS_P"] = orig_hgvs n_corrected += 1 - elif key in unresolved_positions: + elif key in unresolved: variant.INFO["MNV_UNRESOLVED"] = True writer.write_record(variant) @@ -739,7 +745,7 @@ def main() -> None: corrections, unresolved = compute_corrections(mnv_candidates, ref_seqs=ref_seqs) log.info( - "%d positions to correct, %d positions flagged MNV_UNRESOLVED", + "%d records to correct, %d records flagged MNV_UNRESOLVED", len(corrections), len(unresolved), ) diff --git a/tests/test_fix_mnv_codons.py b/tests/test_fix_mnv_codons.py new file mode 100644 index 0000000..ea1732e --- /dev/null +++ b/tests/test_fix_mnv_codons.py @@ -0,0 +1,69 @@ +"""Unit tests for bin/fix_mnv_codons.py: corrections land only on the SNVs that make the codon. + +Run with: pytest tests/test_fix_mnv_codons.py + +Reference ATG AAA CGT TTT GGG CCC TAA on chr1, one CDS over all of it. Codon 2 (AAA, pos 4-6) +carries A4C + A5T, resolvable to p.K2L. Codon 3 (CGT, pos 7-9) carries C7A + G8A, which is +AA? and ambiguous (K or N) without --reference, so it is MNV_UNRESOLVED. A deletion anchored +on pos 4 and on pos 7, and a low-AF second allele at pos 5, share a POS with a grouped SNV. +""" + +import pytest + +pytest.importorskip("cyvcf2", reason="cyvcf2 is required by the script under test") + +import fix_mnv_codons as fmc # noqa: E402 (needs cyvcf2, checked above) + +HEADER = """##fileformat=VCFv4.2 +##contig= +##INFO= +##INFO= +#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO +""" + + +def _ann(alt, effect, impact, hgvs_c, hgvs_p, cds_pos, aa_pos): + return (f"{alt}|{effect}|{impact}|G|G|transcript|TX1|protein_coding|1/1|{hgvs_c}|{hgvs_p}|" + f"{cds_pos}/21|{cds_pos}/21|{aa_pos}/6||") + + +RECORDS = [ + (4, "A", "C", 1.0, _ann("C", "missense_variant", "MODERATE", "c.4A>C", "p.K2Q", 4, 2)), + (4, "AAAC", "A", 1.0, _ann("A", "frameshift_variant", "HIGH", "c.5_7delAAC", "p.K2fs", 5, 2)), + (5, "A", "T", 1.0, _ann("T", "missense_variant", "MODERATE", "c.5A>T", "p.K2I", 5, 2)), + (5, "A", "G", 0.2, _ann("G", "missense_variant", "MODERATE", "c.5A>G", "p.K2R", 5, 2)), + (7, "C", "A", 1.0, _ann("A", "missense_variant", "MODERATE", "c.7C>A", "p.R3S", 7, 3)), + (7, "CGTT", "C", 1.0, _ann("C", "frameshift_variant", "HIGH", "c.8_10delGTT", "p.R3fs", 8, 3)), + (8, "G", "A", 1.0, _ann("A", "missense_variant", "MODERATE", "c.8G>A", "p.R3H", 8, 3)), +] + + +@pytest.fixture +def written(tmp_path): + """Run both passes and return {(pos, ref, alt): INFO} from the output VCF.""" + vcf_in, vcf_out = tmp_path / "in.vcf", tmp_path / "out.vcf" + vcf_in.write_text(HEADER + "".join( + f"chr1\t{pos}\t.\t{ref}\t{alt}\t60\t.\tAF={af};ANN={ann}\n" for pos, ref, alt, af, ann in RECORDS)) + corrections, unresolved = fmc.compute_corrections(fmc.collect_codon_groups(str(vcf_in), 0.5)) + fmc.write_corrected_vcf(str(vcf_in), str(vcf_out), corrections, unresolved) + rows = [line.split("\t") for line in vcf_out.read_text().splitlines() if not line.startswith("#")] + return {(int(r[1]), r[3], r[4]): r[7] for r in rows} + + +def test_snvs_in_one_codon_get_the_combined_change(written): + for key in [(4, "A", "C"), (5, "A", "T")]: + assert "MNV_CORRECTED" in written[key] + assert "|p.K2L|" in written[key] + + +@pytest.mark.parametrize("key", [(4, "AAAC", "A"), (5, "A", "G")]) +def test_other_records_at_a_corrected_position_are_untouched(written, key): + original = next(ann for pos, ref, alt, _, ann in RECORDS if (pos, ref, alt) == key) + assert written[key].endswith(f"ANN={original}") + assert "MNV_" not in written[key] + + +def test_unresolved_flag_only_on_the_grouped_snvs(written): + assert "MNV_UNRESOLVED" in written[(7, "C", "A")] + assert "MNV_UNRESOLVED" in written[(8, "G", "A")] + assert "MNV_" not in written[(7, "CGTT", "C")]