Skip to content

Commit

Permalink
Validate qsvPCs is a prcomp object
Browse files Browse the repository at this point in the history
  • Loading branch information
HediaTnani committed Jan 12, 2024
1 parent 67113ab commit c11276f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
17 changes: 11 additions & 6 deletions R/get_qsvs.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@
#' get_qsvs(qsv, 2)
get_qsvs <- function(qsvPCs, k) {
# check that k isn't zero
if (k <= 0 | k > ncol(qsvPCs$x)) {
stop(paste("k must between 1 and",ncol(qsvPCs$x)))
}
if (k <= 0 | k > ncol(qsvPCs$x)) {
stop(paste("k must between 1 and",ncol(qsvPCs$x)))
}

qSVs <- qsvPCs$x[, seq_len(k), drop = FALSE]
colnames(qSVs) <- paste0("qSV", seq_len(k))
return(qSVs)
# Validate qsvPCs is a prcomp object
if (!is(qsvPCs, "prcomp")) {
stop("qsvPCs must be a prcomp object.", call. = FALSE)
}

qSVs <- qsvPCs$x[, seq_len(k), drop = FALSE]
colnames(qSVs) <- paste0("qSV", seq_len(k))
return(qSVs)
}
4 changes: 4 additions & 0 deletions tests/testthat/test-get_qsvs.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ test_that("k is 0 throws an error", {
test_that("k is higher than the number of columns throws an error", {
k = ncol(qsv$x) + 1000
expect_error(get_qsvs(qsv, k), paste("k must between 1 and",ncol(qsv$x)))
})

test_that("input has to be a prcomp", {
expect_error(get_qsvs(covComb_tx_deg, 3), "qsvPCs must be a prcomp object.")
})

0 comments on commit c11276f

Please sign in to comment.