Skip to content

Commit ff7ec4e

Browse files
committed
fix(gwas): make adjustPval and manhatan plot handle missing p values
1 parent 23ee1bd commit ff7ec4e

4 files changed

Lines changed: 79 additions & 5 deletions

File tree

src/gwas.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ adjustPval <- function(p, adj_method, thresh_p = NULL) {
146146

147147
# check p
148148
logger$log("Check p values ...")
149-
if (any(p < 0) || any(p > 1)) {
149+
if (any(p < 0, na.rm = TRUE) || any(p > 1, na.rm = TRUE)) {
150150
# The `p.adjust` function will proceed to the calculation even if the p-values
151151
# are invalid. To avoid unexpected behaviour, I'd prefer this function
152152
# to crash in such case.

src/plot.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ manPlot <- function(gwas,
3737
all(is.na(x))
3838
})
3939
gwas <- gwas[!allNaLines, ]
40+
gwas <- gwas[!is.na(gwas$p), ]
4041
logger$log("Remove NAs DONE")
4142

4243
# P-Values adjustment ----
@@ -81,7 +82,7 @@ manPlot <- function(gwas,
8182
# get significant SNP ----
8283
if (highlightSinif) {
8384
logger$log("Extract significant SNP ...")
84-
significantSNP <- gwas[gwas$p_adj <= thresh_p, "id"]
85+
significantSNP <- gwas[ifelse(is.na(gwas$p_adj), FALSE, gwas$p_adj <= thresh_p), "id"]
8586
if (length(significantSNP) == 0) {
8687
significantSNP <- NULL
8788
}

tests/testthat/test_2_gwas.R

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,36 @@ capture.output({
155155
}
156156

157157
# adjustPval with edge case
158-
test_that("adjustPval handle gwas with missing p values", {
158+
test_that("adjustPval handle gwas with some missing p values", {
159159
n_p <- 100
160160
p <- runif(n_p)
161-
p[sample.int(n_p, 5)] <- runif(5, min = 0, max = 0.05)
162-
p[sample.int(n_p, 5)] <- NA
161+
162+
missing_pval_indices <- sample.int(n_p, 5)
163+
p[missing_pval_indices] <- NA
164+
thresh_p <- 0.05
165+
166+
for (adjMeth in c(
167+
"holm",
168+
"hochberg",
169+
"bonferroni",
170+
"BH",
171+
"BY",
172+
"fdr",
173+
"none"
174+
)) {
175+
expect_no_error({
176+
adj <- adjustPval(
177+
p = p,
178+
adj_method = adjMeth,
179+
thresh_p = thresh_p
180+
)
181+
})
182+
expect_true(all(is.na(adj$p_adj[missing_pval_indices])))
183+
}
184+
})
185+
186+
test_that("adjustPval handle gwas with all missing p values", {
187+
p <- rep(NA, 100)
163188
thresh_p <- 0.05
164189

165190
for (adjMeth in c(
@@ -178,6 +203,7 @@ capture.output({
178203
thresh_p = thresh_p
179204
)
180205
})
206+
expect_true(all(is.na(adj$p_adj)))
181207
}
182208
})
183209
})

tests/testthat/test_4_plots.R

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,53 @@ capture_output({
7171
expect_true(is.null(mP))
7272
}
7373
})
74+
75+
test_that(paste(testName, "with some missing p values"), {
76+
resGwas$p[c(2, 4, 10)] <- NA
77+
expect_no_error({
78+
mP <- manPlot(
79+
gwas = resGwas,
80+
adj_method = "bonferroni",
81+
thresh_p = 0.05,
82+
chr = chr,
83+
interactive = interactive,
84+
filter_pAdj = 1,
85+
filter_nPoints = Inf,
86+
filter_quant = 1,
87+
title = "test manPlot"
88+
)
89+
})
90+
if (interactive) {
91+
expect_true(all.equal(class(mP), c("plotly", "htmlwidget")))
92+
} else {
93+
expect_true(is.null(mP))
94+
}
95+
})
96+
97+
test_that(paste(testName, "with all missing p values"), {
98+
resGwas$p <- NA
99+
expect_warning(
100+
{
101+
mP <- manPlot(
102+
gwas = resGwas,
103+
adj_method = "bonferroni",
104+
thresh_p = 0.05,
105+
chr = chr,
106+
interactive = interactive,
107+
filter_pAdj = 1,
108+
filter_nPoints = Inf,
109+
filter_quant = 1,
110+
title = "test manPlot"
111+
)
112+
},
113+
regexp = "There is no points to display"
114+
)
115+
if (interactive) {
116+
expect_true(all.equal(class(mP), c("plotly", "htmlwidget")))
117+
} else {
118+
expect_true(is.null(mP))
119+
}
120+
})
74121
}
75122
}
76123
}

0 commit comments

Comments
 (0)