From 33a3762fdecf5ab74123694508c4b32f7aff20c0 Mon Sep 17 00:00:00 2001 From: Chen-Yang Su Date: Fri, 27 Nov 2026 09:49:36 -0800 Subject: [PATCH] Fix HWEzscore sign in snpSummary --- R/methods-snpSummary.R | 2 +- inst/unitTests/test_snpSummary.R | 39 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/R/methods-snpSummary.R b/R/methods-snpSummary.R index 55c0414..0ca2d36 100644 --- a/R/methods-snpSummary.R +++ b/R/methods-snpSummary.R @@ -61,7 +61,7 @@ setMethod("snpSummary", "CollapsedVCF", expt <- rowSums(alleleCounts) * cbind(a0^2/2, a0*a1, a1^2/2) chisq <- rowSums((genoCounts - expt)^2 / expt) z <- rep(NA, nrow(genoCounts)) - zsign <- sign(genoCounts[,"g01"] - expt[,2] * rowSums(genoCounts)) + zsign <- sign(genoCounts[,"g01"] - expt[,2]) zsign * sqrt(chisq) } diff --git a/inst/unitTests/test_snpSummary.R b/inst/unitTests/test_snpSummary.R index ab60d64..62f232e 100644 --- a/inst/unitTests/test_snpSummary.R +++ b/inst/unitTests/test_snpSummary.R @@ -38,3 +38,42 @@ test_snpSummary_output <- function() checkEquals(round(res[["a1Freq"]], 2), c(0.50, 0.17, NA, NA, NA)) } +test_snpSummary_HWEzscore <- function() +{ + ## ex2.vcf: row 1 has (1, 1, 1) -> E[g01] = 1.5 > 1 (deficit, z < 0) + ## row 2 has (2, 1, 0) -> E[g01] = 5/6 < 1 (excess, z > 0) + fl <- system.file("extdata", "ex2.vcf", package="VariantAnnotation") + res <- snpSummary(readVcf(fl, "hg19")) + checkEqualsNumeric(res$HWEzscore[1], -sqrt(1 / 3)) + checkEqualsNumeric(res$HWEzscore[2], sqrt(0.12)) + + ## Synthetic VCF covering heterozygote excess (including N = 2), + ## heterozygote deficit, and exact HWE + gt <- rbind( + c("0/1", "0/1", "./.", "./.", "./.", "./.", "./.", "./.", "./.", "./."), + rep("0/1", 10L), + c(rep("0/0", 5L), rep("1/1", 5L)), + c("0/0", "0/1", "0/1", "1/1", rep("./.", 6L)) + ) + rownames(gt) <- paste0("snp", 1:4) + colnames(gt) <- paste0("s", 1:10) + rr <- GRanges("1", IRanges(1:4, width=1L), + paramRangeID=factor(rep(NA, 4L))) + names(rr) <- rownames(gt) + fixed <- DataFrame( + REF=DNAStringSet(rep("A", 4L)), + ALT=DNAStringSetList("G", "G", "G", "G"), + QUAL=rep(NA_real_, 4L), + FILTER=rep("PASS", 4L) + ) + vcf <- VCF(rowRanges=rr, fixed=fixed, geno=SimpleList(GT=gt), + colData=DataFrame(Samples=seq_len(ncol(gt)), + row.names=colnames(gt)), + collapsed=TRUE) + s <- snpSummary(vcf) + + checkEqualsNumeric(s$HWEzscore, c(sqrt(2), sqrt(10), -sqrt(10), 0)) + checkEqualsNumeric(s$HWEpvalue, + pchisq(c(2, 10, 10, 0), df=1, lower.tail=FALSE)) +} +