Handle IUPAC ambiguity codes in the reference (#291) - #309
Merged
Conversation
GRCh38 and other assemblies carry IUPAC ambiguity codes (R, Y, S, W, K, M, B, D, H, V). NEAT previously had no handling for them: - they survived reference load (convert_masking only neutralized non-ACGT bases when an N was also present in the segment, and skipped bases before the first N), and - a surviving code at a sequencing-error site hit NUC_IND[base] and raised KeyError, crashing the run. Fix: resolve ambiguity codes to a concrete base at reference load. - common: add IUPAC_CODES map + resolve_iupac_bases(), which replaces each code with one of the bases it represents using the run's seeded RNG (reproducible). Vectorized; the no-codes common case is a single scan, so genome-scale references aren't penalized. 'N' is left for its existing low-quality masking. - split_inputs: resolve right after the existing upper(), so every downstream consumer (reads, BAM, golden VCF, error/trinucleotide lookups) only ever sees A/C/G/T/N. Logs a warning with the count. - error_models: defense-in-depth — NUC_IND lookup now skips a non-ACGT base instead of raising, so nothing can crash even if a code slips through. Tests: resolver unit tests (every code, counts, reproducibility, N left alone, length preserved) and an end-to-end runner test on a reference containing all ten codes plus an N run, asserting FASTQ output is clean. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Bump version to 4.5.3 and add the ChangeLog entry for the IUPAC ambiguity-code handling fix. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
joshfactorial
force-pushed
the
fix/291-iupac-handling
branch
from
June 8, 2026 21:51
fa28705 to
ee55d91
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #291. Release v4.5.3 (version bump + ChangeLog entry included).
Problem
GRCh38 and other assemblies carry IUPAC ambiguity codes (
R Y S W K M B D H V) alongsideA/C/G/T/N. NEAT had no handling:Read.convert_masking()only neutralized non-ACGT bases when the segment also contained anN, and skipped bases before the firstN.NUC_IND[snv_reference](error_models.py:256);NUC_INDonly has A/C/G/T →KeyError, aborting the run.(Mutation generation was already safe — skips non-
ALLOWED_NUCLpositions — and the BAM SEQ encoder already supports IUPAC nibbles. The crash was specific to the sequencing-error path.)Fix
Resolve ambiguity codes to a concrete base at reference load, so every downstream consumer (reads, BAM, golden VCF, error/trinucleotide lookups) only ever sees
A/C/G/T/N.common:IUPAC_CODESmap +resolve_iupac_bases()— replaces each code with one of the bases it represents using the run's seeded RNG (reproducible). Vectorized; the no-codes common case is a single scan, so genome-scale references aren't penalized.Nis left for its existing low-quality masking.split_inputs: resolves right after the existing.upper(); logs a warning with the count.error_models: defense-in-depth —NUC_INDlookup skips a non-ACGT base instead of raising, so nothing can crash even if a code slips through.Release
pyproject.toml:4.5.2 → 4.5.3ChangeLog.md: new# NEAT v4.5.3entry for the Deal with difficulties hg38 #291 fix(Branch is rebased on current
developat 4.5.2, so the bump applies cleanly.)Tests
Nleft alone, length preserved, lowercase passthrough.Full suite: 790 passed, 3 skipped. Verified end-to-end (single- and multi-threaded) on a synthetic IUPAC reference: no crash; FASTQ/BAM/VCF outputs contain only A/C/G/T/N.
Known limitation
The input-VCF ref-match check (
vcf_func.py:137) reads the original reference file, not the resolved chunks — it won't crash (string comparison), but at an ambiguous position an input variant is matched against the original code while reads show the resolved base. Rare edge case; left as follow-up.🤖 Generated with Claude Code