Skip to content

Tolerate flip-twin collapse in .ldFromSketch instead of erroring - #572

Open
Yining97 wants to merge 1 commit into
StatFunGen:mainfrom
Yining97:rss-ldsketch-flip-twin-tolerant
Open

Tolerate flip-twin collapse in .ldFromSketch instead of erroring#572
Yining97 wants to merge 1 commit into
StatFunGen:mainfrom
Yining97:rss-ldsketch-flip-twin-tolerant

Conversation

@Yining97

Copy link
Copy Markdown
Contributor

Problem

On an LD panel that stores a variant in both ref/alt orientations (e.g. ADSP R5: 6,365 such records on chr19; R4 has none), RAISS imputes both orientations — each is a distinct panel variant_id absent from the observed GWAS — so a region's variant set ends up carrying a variant and its own ref/alt-swapped twin. .ldFromSketch calls matchVariants(allowFlip = TRUE), which treats a variant and its swap as the same variant and merges the pair to one match. The old code then computed nMissing = length(requested) - length(matched) > 0 and stopped with "N variant id(s) not present in the LD sketch panel" — but nothing is absent; the pair merged. Two real regions died this way (APOE in the kriging prefilter; chr11 in the fine-mapping LD build), making R5 unusable end to end.

Fix

.ldFromSketch now distinguishes a collapsed flip-twin (a requested id whose (chrom, pos) IS on the panel but merged onto its sibling) from a genuine absence (position not on the panel at all). Only genuine absences count toward the onMissing = "error" stop; the LD matrix is built on the deduplicated set and the survivors are exposed via keptVariantIds (now always, not just under "drop"). The callers that use the matrix alongside z/N realign to the survivors: a new .fmAlignToKeptLd helper at both fine-mapping sites, and a df row-drop at the kriging prefilter and zMismatch QC. No panel/data change — this makes pecotmr tolerant of a panel that carries both orientations.

Preserved: the genuine-absence safety check still errors, and distinct multi-allelic ALT alleles at one position are never merged (verified) so they still enter the credible set. The dropped twin is the same variant (its z is the sign-flip of the kept one), so results are unchanged.

Scope: covers the GWAS RSS path (fine-mapping both methods, kriging, zMismatch). jointEngine/causalInference/twasWeights are out of that path with different vector shapes; their z/N realignment is deferred (the central fix already removes the misleading error there).

Validation

  • flip-twin request under onMissing="error" → no stop, deduplicated LD matrix with keptVariantIds; genuine absence still errors; distinct ALTs don't collapse.
  • .fmAlignToKeptLd realigns z/n/af correctly (no-op otherwise); end-to-end fineMappingPipeline(GwasSumStats) on a flip-twin region fits on the deduplicated set (z len 3, R 3x3 from 4 requested).
  • Regression: 0 new failures across every .ldFromSketch consumer (test_ld 217, sumstatsQc 325, fineMapping 185, colocboost 42, cip 69, twas 166, jointEngine 57, coloc 36, variantId 100). The 6 test_ctwasPipeline.R errors are pre-existing on main (confirmed by stash comparison), unrelated.

Tests added: test_ld.R +3, test_fineMappingPipeline.R +2.

🤖 Generated with Claude Code

When the requested set carries a variant and its ref/alt-swapped twin (e.g. a
panel that stores both orientations, imputed by RAISS as two), matchVariants
merges the pair and .ldFromSketch mislabeled the drop as a panel absence and
stopped. Count only genuine absences (position not on the panel) toward that
error; build LD on the deduplicated set and realign z/N at the fine-mapping,
kriging, and zMismatch callers. Genuine absences still error; distinct
multi-allelic ALTs are unaffected.
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.

1 participant