Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions bin/fix_mnv_codons.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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."""
Expand All @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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),
)
Expand Down
69 changes: 69 additions & 0 deletions tests/test_fix_mnv_codons.py
Original file line number Diff line number Diff line change
@@ -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=<ID=chr1,length=21>
##INFO=<ID=AF,Number=A,Type=Float,Description="Allele frequency">
##INFO=<ID=ANN,Number=.,Type=String,Description="snpEff annotation">
#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")]
Loading