Skip to content

fix: enricher assigned samples to the wrong ALT allele on multiallelic records - #44

Merged
lucapinello merged 1 commit into
masterfrom
fix/enricher-gt-allele-parsing
Oct 8, 2026
Merged

lucapinello merged 1 commit into
masterfrom
fix/enricher-gt-allele-parsing

Conversation

@anncir1

@anncir1 anncir1 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

The enricher matched a sample to an ALT allele by searching the raw GT text for the allele number and stopping at the first hit, resulting in two failures. First, the search is a substring test, so any record with ten or more ALTs can bind a sample to an allele it does not carry (e.g., GT "0|12" matches allele 1). Second, the break drops the second allele of a sample carrying two different ALTs ("2|1"), leaving that allele with no carrier.

Proposed fix: parse the GT into actual allele integers instead, splitting on '|' and '/' so phased, unphased, haploid ("1") and partially-missing (".|1") genotypes are all handled, and skipping '.' and the reference allele. Then, test membership on those integers and record the sample under every ALT it carries. Applied to the multiallelic SNP path and the indel path, in both the C++ enricher and the Python fallback (crispritz.py falls back to enricher.py when the compiled binary is absent). The biallelic path is untouched (its gt.find('1') is correct because a biallelic GT holds only 0, 1 or '.').

When tested on chr22 of an HPRC pangenome subset, 53 SNP positions change sample assignment and the indel log increases (both real indels the sample carries that were previously dropped were added, and indels it does not carry that were previously fabricated were dropped). Two cases were verified against the raw genotypes, biallelic datasets are unaffected. The enriched FASTA is unchanged in every test, so the SNP search space does not move.

…c records

The enricher matched a sample to an ALT allele by searching the raw GT text
for the allele number and stopping at the first hit:

    if (gt.find(std::to_string(idx)) != npos) { ...; break; }

Two failures follow. First, the search is a substring test, so GT "0|12"
matches allele 1, "32|32" matches allele 2, and "86|135" matches allele 1 --
any record with ten or more ALTs can bind a sample to an allele it does not
carry. Second, the break drops the second allele of a sample carrying two
different ALTs ("2|1"), leaving that allele with no carrier.

Parse the GT into actual allele integers instead -- splitting on '|' and '/'
so phased, unphased, haploid ("1") and partially-missing (".|1") genotypes
are all handled, and skipping '.' and the reference allele -- then test
membership on those integers and record the sample under every ALT it
carries. Applied to the multiallelic SNP path and the indel path, in both the
C++ enricher and the Python fallback (crispritz.py falls back to enricher.py
when the compiled binary is absent). The biallelic path is untouched: its
gt.find('1') is sound because a biallelic GT holds only 0, 1 or '.'.

Biallelic datasets are unaffected: on chr22 of a 1000G subset (0 multiallelic
records of 60,094) the SNP dictionary, indel log and enriched FASTA are all
byte-identical. On chr22 of an HPRC pangenome subset, 53 SNP positions change
sample assignment and the indel log grows from 18,596 to 20,272 rows -- real
indels the sample carries that were previously dropped, less indels it does
not carry that were previously fabricated. Two cases verified against the raw
genotypes: at chr22:10,515,074 (GT 1|2) both carried indels are now recorded
where only the first was before; at chr22:10,571,582 (GT 32|32, 54 ALTs) the
sample moves from allele 2 to allele 32. The enriched FASTA is unchanged in
every test, so the SNP search space does not move.

@lucapinello lucapinello left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified the bug and the patch before merge.

Bug reproduced (logic-level, both failure modes):

  • Substring false-match: GT 0|12 with gt.find("1") wrongly binds allele 1 (the "1" in "12"). Any record with ≥10 ALTs is exposed.
  • break drops the 2nd ALT: GT 2|1 recorded only the first allele hit, leaving the other with no carrier.

Patch confirmed correct:

  • gtAltAlleles/_gt_alt_alleles tested on 14 edge cases (phased/unphased/haploid/.|1/././multi-digit 10,12/empty) — all correct.
  • Set-membership + no-break assigns the sample to every ALT it carries; 0|12 → {12} (no false allele-1), 2|1 → {1,2}.
  • Biallelic path verified untouched: all biallelic GTs (0|1,1|0,1|1,0|0,.|1,./.) give identical old/new results.
  • C++ mirrors the Python exactly; CI is green on the compiled binary (build + golden search regression + ASan #105 guard + arm64), and the author's HPRC chr22 test shows 53 SNP positions corrected with the enriched FASTA unchanged.

Impact for CRISPRme+: shipped indices are bcftools norm -m -any pre-split to biallelic so they're unaffected; this corrects per-sample carrier assignment for user-supplied multiallelic VCFs. LGTM.

@lucapinello
lucapinello merged commit 8241a49 into master Oct 8, 2026
10 checks passed
lucapinello added a commit that referenced this pull request Oct 8, 2026
Bundles the #44 enricher multiallelic ALT-allele-assignment fix.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
lucapinello added a commit that referenced this pull request Oct 30, 2026
…one contig

indel_to_fasta extracts one reference window per VCF record, then loops over the
record's carried indel alleles emitting one fake contig each. The substitution
result was assigned back into the window, so allele 2 was applied on top of
allele 1's output: its contig carried BOTH insertions -- a haplotype no sample
need carry -- and because refseq and end_fake_pos both derive from the window's
length, the logged reference sequence and the fake->real coordinate mapping
drifted with it.

Measured on a synthetic site G -> GA, GAA. The second allele's contig was
...CCG AAA GCTTCG (three A's, for an allele that inserts two) against ...CCG AA
GCTTCG, with FAKEPOS 56 bases against 55.

Not reachable through CRISPRme's own VCFs, which are split: 0 of 1,998,332 chr22
records in the shipped HGDP+1kGP panel carry a comma in ALT, so the loop runs
once per record and the reassignment cannot compound. `gnomAD-converter
--multiallelic` deliberately merges variants into multiallelic sites, and those
VCFs feed add-variants. Dropping the `break` in the carrier scan (#44) raised the
exposure, since more alleles now reach the emit gate. Across the shipped index's
own logs, 781,032 of 14,641,550 sites have two or more carried indel alleles,
which would be 2,550,831 affected contigs -- 14.8% of all indel windows.

Each allele is now derived from the reference window. For a single-allele record
the two are byte-identical, so no existing index moves.

Also: start_position = POS - 26 had no floor, and the window is anchored with the
REF base at index 25, which a negative start cannot satisfy. Python sliced from
the far end of the chromosome and returned an EMPTY window; C++ passed a negative
offset to substr, which converts to size_t and throws. So the ports did not even
agree on it. Both now skip the site with a warning on stdout -- not stderr, which
is fatal to the CRISPRme stage that calls this. No shipped index is affected: 0
of 17,192,381 log rows have a negative window start, hg38 chromosomes opening
with a telomeric N run.

verify_enricher.sh covered a "multiallelic indel" that was one SNP allele plus
one insertion, so its indels list had a single entry and the per-allele loop ran
once -- it could not catch this. Added a site with two carried indel alleles, and
wired the script into CI: the packaged install falls back to enricher.py when the
binary is missing, so the two ports must not drift.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants