Skip to content

Commit

Permalink
add tests for ExpressionSet
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiAragaki committed Jul 21, 2024
1 parent 8b1a072 commit ac8ac04
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
7 changes: 4 additions & 3 deletions R/clanc-fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ clanc.ExpressionSet <- function(x,
pd_names <- colnames(Biobase::pData(x))

if (!spec_in_cd(classes, pd_names) &&
(spec_in_cd(priors, pd_names) || spec_in_cd(active, pd_names))) {
((!priors %in% c("equal", "class") && (spec_in_cd(priors, pd_names))) ||
spec_in_cd(active, pd_names))) {
cli::cli_abort(
"`classes` must be specified as a column name in pData if `active` or `priors` are." #nolint
)
Expand All @@ -253,11 +254,11 @@ clanc.ExpressionSet <- function(x,

if (spec_in_cd(classes, pd_names) &&
nrow(unique(metadata)) > length(unique(metadata$class))) {
cli::cli_abort(
cli::cli_abort(c(
"More than one prior and/or active for each class",
"i" = "Each class must have exactly 1 prior and active",
"i" = "To test across many `active`, use `tune::tune_grid`"
)
))
} else {
metadata <- unique(metadata)
classes <- x[[classes]]
Expand Down
44 changes: 41 additions & 3 deletions tests/testthat/test-clanc-fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,60 @@ test_that("catch unsupported types", {
expect_error(clanc(numeric()))
})

test_that("catch bad args", {
test_that("SummarizedExperiment catch bad args", {
library(SummarizedExperiment)
assay <- matrix(1:4, nrow = 2)
cd <- DataFrame(a = c("one", "two"), active = c(3, 4), prior = c(0.5, 0.5))
cd <- DataFrame(
a = c("one", "two"),
active = c(3, 4),
prior = c(0.5, 0.5)
)
rd <- DataFrame(gene = c("g1", "g2"))
se <- SummarizedExperiment(assay, rowData = rd, colData = cd)
expect_error(clanc(se, classes = "b"))
expect_error(clanc(se, active = "active", classes = c("one", "two")))
expect_error(clanc(se, priors = "prior", classes = c("one", "two")))

# Too many attributes per class:
assay <- matrix(1:6, nrow = 2)
cd <- DataFrame(a = c("one", "two", "one"), active = c(3, 4, 2), prior = c(0.5, 0.5, 0.5))
cd <- DataFrame(
a = c("one", "two", "one"),
active = c(3, 4, 2),
prior = c(0.5, 0.5, 0.5)
)
rd <- DataFrame(gene = c("g1", "g2"))
se <- SummarizedExperiment(assay, rowData = rd, colData = cd)
expect_error(
clanc(se, classes = "a", active = "active", priors = "prior"),
"More than one prior"
)
})

test_that("SummarizedExperiment catch bad args", {
library(Biobase)
assay <- matrix(1:4, nrow = 2)
pdata <- AnnotatedDataFrame(
data.frame(a = c("one", "two"), active = c(3, 4), prior = c(0.5, 0.5))
)
fdata <- AnnotatedDataFrame(data.frame(gene = c("g1", "g2")))
es <- ExpressionSet(assay, pdata, fdata)
expect_error(clanc(es, classes = "b"))
expect_error(clanc(es, active = "active", classes = c("one", "two")))
expect_error(clanc(es, priors = "prior", classes = c("one", "two")))

# Too many attributes per class:
assay <- matrix(1:6, nrow = 2)
pdata <- AnnotatedDataFrame(
data.frame(
a = c("one", "two", "one"),
active = c(3, 4, 2),
prior = c(0.5, 0.5, 0.5)
)
)
fdata <- AnnotatedDataFrame(data.frame(gene = c("g1", "g2")))
es <- ExpressionSet(assay, pdata, fdata)
expect_error(
clanc(es, classes = "a", active = "active", priors = "prior"),
"More than one prior"
)
})

0 comments on commit ac8ac04

Please sign in to comment.