Skip to content

Commit

Permalink
add param min.maf to autoSVD
Browse files Browse the repository at this point in the history
  • Loading branch information
privefl committed Sep 10, 2024
1 parent ffd8246 commit e18d02a
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 22 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ Encoding: UTF-8
Package: bigsnpr
Type: Package
Title: Analysis of Massive SNP Arrays
Version: 1.12.13
Date: 2024-09-04
Version: 1.12.14
Date: 2024-09-10
Authors@R: c(
person("Florian", "Privé", email = "[email protected]", role = c("aut", "cre")),
person("Michael", "Blum", role = "ths"),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## bigsnpr 1.12.14

- Add a new `min.maf = 0.02` parameter to `snp_autoSVD()` and `bed_autoSVD()`. Then variants are now discarded when they have either a small MAC or a small MAF.

## bigsnpr 1.12.13

- Can now use matrix accessors for class `bed_light` as well.
Expand Down
35 changes: 20 additions & 15 deletions R/autoSVD.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ getIntervals <- function(x, n = 2) {
#' @param alpha.tukey Default is `0.1`. The type-I error rate in outlier
#' detection (that is further corrected for multiple testing).
#' @param min.mac Minimum minor allele count (MAC) for variants to be included.
#' Default is `10`.
#' Default is `10`. Can actually be higher because of `min.maf`.
#' @param min.maf Minimum minor allele frequency (MAF) for variants to be included.
#' Default is `0.02`. Can actually be higher because of `min.mac`.
#' @param max.iter Maximum number of iterations of outlier detection.
#' Default is `5`.
#'
Expand Down Expand Up @@ -75,6 +77,7 @@ snp_autoSVD <- function(G,
int.min.size = 20,
alpha.tukey = 0.05,
min.mac = 10,
min.maf = 0.02,
max.iter = 5,
is.size.in.bp = NULL,
ncores = 1,
Expand All @@ -90,15 +93,15 @@ snp_autoSVD <- function(G,
# Verbose?
printf2 <- function(...) if (verbose) printf(...)

if (min.mac > 0) {
if (min.mac > 0 && min.maf > 0) {
maf <- snp_MAF(G, ind.row, ind.col, ncores = ncores)
min.maf <- min.mac / (2 * length(ind.row))
mac.nok <- (maf < min.maf)
printf2("Discarding %d variant%s with MAC < %s.\n", sum(mac.nok),
`if`(sum(mac.nok) > 1, "s", ""), min.mac)
ind.keep <- ind.col[!mac.nok]
maf.nok <- (maf < max(min.maf, min.mac / (2 * length(ind.row))))
printf2("Discarding %d variant%s with MAC < %s or MAF < %s.\n",
sum(maf.nok), `if`(sum(maf.nok) > 1, "s", ""), min.mac, min.maf)
ind.keep <- ind.col[!maf.nok]
} else {
stop2("You cannot use variants with no variation; set min.mac > 0.")
stop2("You cannot use variants with no variation; %s",
"set min.mac > 0 and min.maf > 0.")
}

# First clumping
Expand Down Expand Up @@ -231,6 +234,7 @@ bed_autoSVD <- function(obj.bed,
int.min.size = 20,
alpha.tukey = 0.05,
min.mac = 10,
min.maf = 0.02,
max.iter = 5,
ncores = 1,
verbose = TRUE) {
Expand All @@ -243,14 +247,15 @@ bed_autoSVD <- function(obj.bed,
# Verbose?
printf2 <- function(...) if (verbose) printf(...)

if (min.mac > 0) {
mac <- bed_MAF(obj.bed, ind.row, ind.col, ncores = ncores)$mac
mac.nok <- (mac < min.mac)
printf2("Discarding %d variant%s with MAC < %s.\n", sum(mac.nok),
`if`(sum(mac.nok) > 1, "s", ""), min.mac)
ind.keep <- ind.col[!mac.nok]
if (min.mac > 0 && min.maf > 0) {
info <- bed_MAF(obj.bed, ind.row, ind.col, ncores = ncores)
maf.nok <- (info$mac < min.mac | info$maf < min.maf)
printf2("Discarding %d variant%s with MAC < %s or MAF < %s.\n",
sum(maf.nok), `if`(sum(maf.nok) > 1, "s", ""), min.mac, min.maf)
ind.keep <- ind.col[!maf.nok]
} else {
stop2("You cannot use variants with no variation; set min.mac > 0.")
stop2("You cannot use variants with no variation; %s",
"set min.mac > 0 and min.maf > 0.")
}

# First clumping
Expand Down
6 changes: 5 additions & 1 deletion docs/news/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/bed_projectPCA.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion man/snp_autoSVD.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 27 additions & 2 deletions tests/testthat/test-2-autoSVD.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ CHR <- test$map$chromosome
POS <- test$map$physical.pos / 10
POS2 <- round(POS + 1)

obj.bed <- bed(snp_writeBed(test, bedfile = tempfile(fileext = ".bed")))

################################################################################

test_that("snp_autoSVD() works", {
Expand Down Expand Up @@ -65,8 +67,6 @@ test_that("snp_autoSVD() works", {

test_that("bed_autoSVD() works", {

obj.bed <- bed(snp_writeBed(test, bedfile = tempfile(fileext = ".bed")))

expect_error(bed_autoSVD(obj.bed, min.mac = 0), "no variation; set min.mac > 0")
expect_output(bed_autoSVD(obj.bed, thr.r2 = NA), "Skipping clumping.")

Expand Down Expand Up @@ -96,3 +96,28 @@ test_that("bed_autoSVD() works", {
})

################################################################################

test_that("MAC/MAF thresholds work", {

info <- bed_MAF(obj.bed)

replicate(10, {

min.mac <- sample(1:40, size = 1)
min.maf <- runif(n = 1, min = 0.01, max = 0.1)

obj.svd1 <- snp_autoSVD(G, CHR, size = 5, min.mac = min.mac, min.maf = min.maf,
thr.r2 = NA, max.iter = 0, verbose = FALSE)
ind1 <- attr(obj.svd1, "subset")
expect_true(all(info$maf[ind1] >= min.maf))
expect_true(all(info$mac[ind1] >= min.mac))

obj.svd2 <- bed_autoSVD(obj.bed, min.mac = min.mac, min.maf = min.maf,
thr.r2 = NA, max.iter = 0, verbose = FALSE)
ind2 <- attr(obj.svd2, "subset")
expect_true(all(info$maf[ind2] >= min.maf))
expect_true(all(info$mac[ind2] >= min.mac))
})
})

################################################################################

0 comments on commit e18d02a

Please sign in to comment.