Skip to content

Commit

Permalink
Add unit test to throw an error if assayname is in assayNames
Browse files Browse the repository at this point in the history
  • Loading branch information
HediaTnani committed Jan 12, 2024
1 parent 4c86f4b commit 4e7323b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
50 changes: 28 additions & 22 deletions R/k_qsvs.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,33 @@
#' set.seed(20230621)
#' k_qsvs(covComb_tx_deg, mod, "tpm")
k_qsvs <- function(rse_tx, mod, assayname) {
if (qr(mod)$rank != ncol(mod)) {
stop("The 'mod' matrix is not full rank.", call. = FALSE)

# Check if assayname is in assayNames
if (!assayname %in% assayNames(rse_tx)) {
stop(sprintf("'%s' is not in assayNames(rse_tx).", assayname), call. = FALSE)
}

if (qr(mod)$rank != ncol(mod)) {
stop("The 'mod' matrix is not full rank.", call. = FALSE)
}
if (nrow(mod) != ncol(rse_tx)) {
stop("The number of rows in 'mod' does not match the number of input 'rse_tx' columns.", call. = FALSE)
}

expr <- log2(assays(rse_tx)[[assayname]] + 1)
k <- tryCatch(
num.sv(expr, mod),
error = function(e) {

if (grepl("only 0's may be mixed with negative subscripts", e$message)) {
warning("Could not run sva::num.sv(). Likely due to transcripts being not expressed in most samples.", call. = FALSE)
} else if (grepl("system is computationally singular", e$message)) {
warning("Could not run sva::num.sv(). Likely due to having highly correlated variables in your 'mod'.", call. = FALSE)
} else {
warning("Could not run sva::num.sv().", call. = FALSE)
}
stop(e)
}
if (nrow(mod) != ncol(rse_tx)) {
stop("The number of rows in 'mod' does not match the number of input 'rse_tx' columns.", call. = FALSE)
}

expr <- log2(assays(rse_tx)[[assayname]] + 1)
k <- tryCatch(
num.sv(expr, mod),
error = function(e) {

if (grepl("only 0's may be mixed with negative subscripts", e$message)) {
warning("Could not run sva::num.sv(). Likely due to transcripts being not expressed in most samples.", call. = FALSE)
} else if (grepl("system is computationally singular", e$message)) {
warning("Could not run sva::num.sv(). Likely due to having highly correlated variables in your 'mod'.", call. = FALSE)
} else {
warning("Could not run sva::num.sv().", call. = FALSE)
}
stop(e)
}
)
return(k)
)
return(k)
}
5 changes: 5 additions & 0 deletions tests/testthat/test-k_qsvs.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ test_that("test that full rank matrix works correctly", {
# expect_warning(k_qsvs(rse_tx_low, mod, "tpm"), "Likely due to transcripts being not expressed in most samples")
expect_silent(k_qsvs(covComb_tx_deg, mod, "tpm"))
})

# Test for assayname not in assayNames
test_that("k_qsvs throws an error when assayname is not in assayNames", {
expect_error(k_qsvs(covComb_tx_deg, assayname = "not_in_assayNames"), "'not_in_assayNames' is not in assayNames\\(rse_tx\\).")
})

0 comments on commit 4e7323b

Please sign in to comment.