Skip to content

Commit

Permalink
Add checks and unit tests of rse_tx as an rse and mod as a matrix
Browse files Browse the repository at this point in the history
Co-authored-by: Nick Eagles <nickeagles77@gmail.com>
  • Loading branch information
HediaTnani and Nick-Eagles committed Jan 16, 2024
1 parent 9c7a2a3 commit 2eaa657
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
14 changes: 14 additions & 0 deletions R/k_qsvs.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,33 @@
#' k_qsvs(covComb_tx_deg, mod, "tpm")
k_qsvs <- function(rse_tx, mod, assayname) {

# Validate rse_tx is a RangedSummarizedExperiment object
if (!is(rse_tx, "RangedSummarizedExperiment")) {
stop("'rse_tx' must be a RangedSummarizedExperiment object.", 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)
}

# Check if mod is a matrix
if (!is(mod, "matrix")) {
stop("'mod' must be a matrix.", call. = FALSE)
}

# Check if mod is full rank
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)
}

# Get expression data normalized by log2
expr <- log2(assays(rse_tx)[[assayname]] + 1)

# Run num.sv
k <- tryCatch(
num.sv(expr, mod),
error = function(e) {
Expand Down
23 changes: 17 additions & 6 deletions tests/testthat/test-k_qsvs.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,22 @@ test_that("output is an numeric", {
expect_equal(class(k_res), "numeric")
})

# Test for input is an rse object
test_that("getDegTx throws an error when input is not a RangedSummarizedExperiment object", {
qsv <- list(x = matrix(seq_len(9), ncol = 3))
expect_error(getDegTx(qsv, assayname = "tpm"), "'rse_tx' must be a RangedSummarizedExperiment object.")
})

# 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\\).")
})

# Test when number of rows in 'mod' does not match number of columns in 'rse_tx'
test_that("Number of rows in 'mod' does not match number of columns in 'rse_tx'", {
mod_not_matching <- mod
mod_not_matching <- mod_not_matching[-1, ]
expect_error(k_qsvs(covComb_tx_deg, mod_not_matching, "tpm"),
"The number of rows in 'mod' does not match the number of input 'rse_tx' columns.")
expect_error(k_qsvs(covComb_tx_deg, mod_not_matching, "tpm"), "The number of rows in 'mod' does not match the number of input 'rse_tx' columns.")
})

test_that("non-full rank data throws error", {
Expand All @@ -44,7 +54,8 @@ test_that("test that full rank matrix works correctly", {
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\\).")
})
test_that("test that mod is a matrix", {
set.seed(20230621)
expect_error(k_qsvs(covComb_tx_deg, mod = "mod", assayname = "tpm"), "'mod' must be a matrix.")
})

0 comments on commit 2eaa657

Please sign in to comment.