From 4f6a4fcaae707ea583ce28039c564f8899f27f30 Mon Sep 17 00:00:00 2001 From: Luke Zappia <lazappi@users.noreply.github.com> Date: Tue, 30 Jan 2018 17:53:53 +1100 Subject: [PATCH] Add tests --- R/BASiCSParams-methods.R | 2 +- R/utils.R | 3 ++ tests/testthat/test-BASiCSParams.R | 15 +++++-- tests/testthat/test-Lun2Params.R | 8 +++- tests/testthat/test-LunParams.R | 17 ++++++++ tests/testthat/test-MFAEstimate.R | 9 +++++ tests/testthat/test-MFAParams.R | 7 ++++ tests/testthat/test-PhenoParams.R | 17 +++++--- tests/testthat/test-SCDDParams.R | 21 ++++++---- tests/testthat/test-SCE-functions.R | 22 ++++++++++- tests/testthat/test-SimpleParams.R | 7 ++++ tests/testthat/test-SplatParams.R | 31 ++++++++------- tests/testthat/test-ZINBParams.R | 9 ++++- tests/testthat/test-compare.R | 59 ++++++++++++++++++++++++++++ tests/testthat/test-listSims.R | 10 +++++ tests/testthat/test-lunEstimate.R | 9 +++++ tests/testthat/test-phenoEstimate.R | 9 +++++ tests/testthat/test-simpleEstimate.R | 9 +++++ tests/testthat/test-splat-simulate.R | 6 ++- tests/testthat/test-utils.R | 22 +++++++++++ 20 files changed, 252 insertions(+), 40 deletions(-) create mode 100644 tests/testthat/test-LunParams.R create mode 100644 tests/testthat/test-MFAEstimate.R create mode 100644 tests/testthat/test-MFAParams.R create mode 100644 tests/testthat/test-SimpleParams.R create mode 100644 tests/testthat/test-compare.R create mode 100644 tests/testthat/test-listSims.R create mode 100644 tests/testthat/test-lunEstimate.R create mode 100644 tests/testthat/test-phenoEstimate.R create mode 100644 tests/testthat/test-simpleEstimate.R create mode 100644 tests/testthat/test-utils.R diff --git a/R/BASiCSParams-methods.R b/R/BASiCSParams-methods.R index 90243b2..55bc5de 100644 --- a/R/BASiCSParams-methods.R +++ b/R/BASiCSParams-methods.R @@ -68,7 +68,7 @@ setValidity("BASiCSParams", function(object) { }) #' @rdname setParam -setMethod("setParam", "BASiCSParams",function(object, name, value) { +setMethod("setParam", "BASiCSParams", function(object, name, value) { checkmate::assertString(name) if (name == "nCells" || name == "nBatches") { diff --git a/R/utils.R b/R/utils.R index 39e770d..0a90987 100644 --- a/R/utils.R +++ b/R/utils.R @@ -23,6 +23,9 @@ logistic <- function(x, x0, k) { #' common columns. rbindMatched <- function(df1, df2) { common.names <- intersect(colnames(df1), colnames(df2)) + if (length(common.names) < 2) { + stop("There must be at least two columns in common") + } combined <- rbind(df1[, common.names], df2[, common.names]) return(combined) diff --git a/tests/testthat/test-BASiCSParams.R b/tests/testthat/test-BASiCSParams.R index 541abc1..97866e7 100644 --- a/tests/testthat/test-BASiCSParams.R +++ b/tests/testthat/test-BASiCSParams.R @@ -1,7 +1,12 @@ context("BASiCSParams") +params <- newBASiCSParams() + +test_that("printing works", { + expect_output(show(params), "A Params object of class BASiCSParams") +}) + test_that("gene.params checks work", { - params <- newBASiCSParams() expect_error(setParam(params, "gene.params", data.frame(A = 1, B = 1)), "gene.params: Incorrect column names") expect_error(setParam(params, "gene.params", @@ -10,7 +15,6 @@ test_that("gene.params checks work", { }) test_that("cell.params checks work", { - params <- newBASiCSParams() expect_error(setParam(params, "cell.params", data.frame(A = 1, B = 1)), "cell.params: Incorrect column names") expect_error(setParam(params, "cell.params", @@ -19,9 +23,14 @@ test_that("cell.params checks work", { }) test_that("nBatches checks work", { - params <- newBASiCSParams() expect_error(setParam(params, "nCells", 1), "nCells cannot be set directly, set batchCells instead") expect_error(setParam(params, "nBatches", 1), "nBatches cannot be set directly, set batchCells instead") }) + +test_that("batchCells checks work", { + pp <- setParam(params, "batchCells", c(10, 10)) + expect_equal(getParam(pp, "nCells"), 20) + expect_equal(getParam(pp, "nBatches"), 2) +}) diff --git a/tests/testthat/test-Lun2Params.R b/tests/testthat/test-Lun2Params.R index 5fb6062..98d424f 100644 --- a/tests/testthat/test-Lun2Params.R +++ b/tests/testthat/test-Lun2Params.R @@ -1,7 +1,12 @@ context("Lun2Params") +params <- newLun2Params() + +test_that("printing works", { + expect_output(show(params), "A Params object of class Lun2Params") +}) + test_that("nCells checks work", { - params <- newLun2Params() expect_error(setParam(params, "nCells", 1), "nCells cannot be set directly, set cell.plates instead") expect_error(setParam(params, "nPlates", 1), @@ -9,7 +14,6 @@ test_that("nCells checks work", { }) test_that("gene.params checks work", { - params <- newLun2Params() expect_error(setParam(params, "gene.params", data.frame(A = 1, B = 1)), "gene.params: Incorrect column names") expect_error(setParam(params, "gene.params", diff --git a/tests/testthat/test-LunParams.R b/tests/testthat/test-LunParams.R new file mode 100644 index 0000000..5f5720b --- /dev/null +++ b/tests/testthat/test-LunParams.R @@ -0,0 +1,17 @@ +context("LunParams") + +params <- newLunParams() + +test_that("printing works", { + expect_output(show(params), "A Params object of class LunParams") +}) + +test_that("nCells checks work", { + expect_error(setParam(params, "nCells", 1), + "nCells cannot be set directly, set groupCells instead") +}) + +test_that("nGroups checks work", { + expect_error(setParam(params, "nGroups", 1), + "nGroups cannot be set directly, set groupCells instead") +}) diff --git a/tests/testthat/test-MFAEstimate.R b/tests/testthat/test-MFAEstimate.R new file mode 100644 index 0000000..12cee5c --- /dev/null +++ b/tests/testthat/test-MFAEstimate.R @@ -0,0 +1,9 @@ +context("MFAEstimate") + +library(scater) +data("sc_example_counts") + +test_that("MFAEstimate works", { + params <- mfaEstimate(sc_example_counts) + expect_true(validObject(params)) +}) diff --git a/tests/testthat/test-MFAParams.R b/tests/testthat/test-MFAParams.R new file mode 100644 index 0000000..1cd9c19 --- /dev/null +++ b/tests/testthat/test-MFAParams.R @@ -0,0 +1,7 @@ +context("MFAParams") + +params <- newMFAParams() + +test_that("printing works", { + expect_output(show(params), "A Params object of class MFAParams") +}) diff --git a/tests/testthat/test-PhenoParams.R b/tests/testthat/test-PhenoParams.R index d5a252d..d806e99 100644 --- a/tests/testthat/test-PhenoParams.R +++ b/tests/testthat/test-PhenoParams.R @@ -1,15 +1,20 @@ context("PhenoParams") +params <- newPhenoParams() + test_that("constructor is valid", { - expect_true(validObject(newPhenoParams())) + expect_true(validObject(params)) +}) + +test_that("printing works", { + expect_output(show(params), "A Params object of class PhenoParams") }) test_that("nGenes checks work", { - params <- newPhenoParams() expect_error(setParam(params, "nGenes", 1), "nGenes cannot be set directly") - params <- setParam(params, "n.de", 0) - total <- getParam(params, "n.de") + getParam(params, "n.pst") + - getParam(params, "n.pst.beta") + getParam(params, "n.de.pst.beta") - expect_equal(getParam(params, "nGenes"), total) + pp <- setParam(params, "n.de", 0) + total <- getParam(pp, "n.de") + getParam(pp, "n.pst") + + getParam(pp, "n.pst.beta") + getParam(pp, "n.de.pst.beta") + expect_equal(getParam(pp, "nGenes"), total) }) diff --git a/tests/testthat/test-SCDDParams.R b/tests/testthat/test-SCDDParams.R index da4d061..05ff5ef 100644 --- a/tests/testthat/test-SCDDParams.R +++ b/tests/testthat/test-SCDDParams.R @@ -1,17 +1,22 @@ context("SCDDParams") +params <- newSCDDParams() + test_that("constructor is valid", { - expect_true(validObject(newSCDDParams())) + expect_true(validObject(params)) +}) + +test_that("printing works", { + expect_output(show(params), "A Params object of class SCDDParams") }) test_that("nGenes checks work", { - params <- newSCDDParams() expect_error(setParam(params, "nGenes", 1), paste("nGenes cannot be set directly, set nDE, nDP, nDM, nDB,", "nEE or nEP instead")) - params <- setParam(params, "nEE", 0) - total <- getParam(params, "nDE") + getParam(params, "nDP") + - getParam(params, "nDM") + getParam(params, "nDP") + - getParam(params, "nEE") + getParam(params, "nEP") - expect_equal(getParam(params, "nGenes"), total) -}) \ No newline at end of file + pp <- setParam(params, "nEE", 0) + total <- getParam(pp, "nDE") + getParam(pp, "nDP") + + getParam(pp, "nDM") + getParam(pp, "nDP") + + getParam(pp, "nEE") + getParam(pp, "nEP") + expect_equal(getParam(pp, "nGenes"), total) +}) diff --git a/tests/testthat/test-SCE-functions.R b/tests/testthat/test-SCE-functions.R index 83e002d..973cf54 100644 --- a/tests/testthat/test-SCE-functions.R +++ b/tests/testthat/test-SCE-functions.R @@ -1,7 +1,26 @@ context("SCE functions") +sce <- simpleSimulate() + +test_that("addFeatureStats works with counts", { + ss <- addFeatureStats(sce) + expect_true(all(c("MeanCounts", "VarCounts", "CVCounts", "MedCounts", + "MADCounts") %in% colnames(rowData(ss)))) + ss <- addFeatureStats(sce, log = TRUE) + expect_true(all(c("MeanLogCounts", "VarLogCounts", "CVLogCounts", + "MedLogCounts", "MADLogCounts") %in% + colnames(rowData(ss)))) + ss <- addFeatureStats(sce, no.zeros = TRUE) + expect_true(all(c("MeanCountsNo0", "VarCountsNo0", "CVCountsNo0", + "MedCountsNo0", "MADCountsNo0") %in% + colnames(rowData(ss)))) + ss <- addFeatureStats(sce, log = TRUE, no.zeros = TRUE) + expect_true(all(c("MeanLogCountsNo0", "VarLogCountsNo0", "CVLogCountsNo0", + "MedLogCountsNo0", "MADLogCountsNo0") %in% + colnames(rowData(ss)))) +}) + test_that("addGeneLengths generate method works", { - sce <- simpleSimulate() expect_silent(addGeneLengths(sce)) expect_error(addGeneLengths("a")) expect_error(addGeneLengths(sce, loc = "a")) @@ -10,7 +29,6 @@ test_that("addGeneLengths generate method works", { }) test_that("addGeneLength sample method works", { - sce <- simpleSimulate() lens <- round(runif(100, 100, 10000)) expect_silent(addGeneLengths(sce, method = "sample", lengths = lens)) expect_error(addGeneLengths(sce, method = "sample")) diff --git a/tests/testthat/test-SimpleParams.R b/tests/testthat/test-SimpleParams.R new file mode 100644 index 0000000..3fa78dc --- /dev/null +++ b/tests/testthat/test-SimpleParams.R @@ -0,0 +1,7 @@ +context("SimpleParams") + +params <- newSimpleParams() + +test_that("printing works", { + expect_output(show(params), "A Params object of class SimpleParams") +}) diff --git a/tests/testthat/test-SplatParams.R b/tests/testthat/test-SplatParams.R index 208f0df..91695df 100644 --- a/tests/testthat/test-SplatParams.R +++ b/tests/testthat/test-SplatParams.R @@ -1,7 +1,12 @@ context("SplatParams") +params <- newSplatParams() + +test_that("printing works", { + expect_output(show(params), "A Params object of class SplatParams") +}) + test_that("nBatches checks work", { - params <- newSplatParams() expect_error(setParam(params, "nCells", 1), "nCells cannot be set directly, set batchCells instead") expect_error(setParam(params, "nBatches", 1), @@ -9,22 +14,20 @@ test_that("nBatches checks work", { }) test_that("nGroups checks work", { - params <- newSplatParams() expect_error(setParam(params, "nGroups", 1), "nGroups cannot be set directly, set group.prob instead") }) test_that("path.from checks work", { - params <- newSplatParams() - params <- setParams(params, group.prob = c(0.5, 0.5)) - params <- setParamUnchecked(params, "path.from", c(0, 1)) - expect_silent(validObject(params)) - params <- setParamUnchecked(params, "path.from", c(0, 3)) - expect_error(validObject(params), "invalid class") - params <- setParamUnchecked(params, "path.from", c(1, 0)) - expect_error(validObject(params), "path cannot begin at itself") - params <- newSplatParams() - params <- setParams(params, group.prob = c(0.3, 0.3, 0.4)) - params <- setParamUnchecked(params, "path.from", c(2, 1, 1)) - expect_error(validObject(params), "origin must be specified in path.from") + pp <- setParams(params, group.prob = c(0.5, 0.5)) + pp <- setParamUnchecked(pp, "path.from", c(0, 1)) + expect_silent(validObject(pp)) + pp <- setParamUnchecked(pp, "path.from", c(0, 3)) + expect_error(validObject(pp), "invalid class") + pp <- setParamUnchecked(pp, "path.from", c(1, 0)) + expect_error(validObject(pp), "path cannot begin at itself") + pp <- newSplatParams() + pp <- setParams(pp, group.prob = c(0.3, 0.3, 0.4)) + pp <- setParamUnchecked(pp, "path.from", c(2, 1, 1)) + expect_error(validObject(pp), "origin must be specified in path.from") }) diff --git a/tests/testthat/test-ZINBParams.R b/tests/testthat/test-ZINBParams.R index bbb7abd..061bb8a 100644 --- a/tests/testthat/test-ZINBParams.R +++ b/tests/testthat/test-ZINBParams.R @@ -1,11 +1,16 @@ context("ZINBParams") +params <- newZINBParams() + test_that("constructor is valid", { - expect_true(validObject(newZINBParams())) + expect_true(validObject(params)) +}) + +test_that("printing works", { + expect_output(show(params), "A Params object of class ZINBParams") }) test_that("nGenes checks work", { - params <- newZINBParams() expect_error(setParam(params, "nGenes", 1), "nGenes cannot be set directly") expect_error(setParam(params, "nCells", 1), diff --git a/tests/testthat/test-compare.R b/tests/testthat/test-compare.R new file mode 100644 index 0000000..04d6520 --- /dev/null +++ b/tests/testthat/test-compare.R @@ -0,0 +1,59 @@ +context("Comparison functions") + +sim1 <- splatSimulate(nGenes = 1000, batchCells = 20) +sim2 <- simpleSimulate(nGenes = 1000, nCells = 20) + +comparison <- compareSCEs(list(Splat = sim1, Simple = sim2)) +difference <- diffSCEs(list(Splat = sim1, Simple = sim2), ref = "Simple") + +test_that("compareSCEs works", { + expect_length(comparison, 3) + expect_true(all(c("FeatureData", "PhenoData", "Plots") %in% + names(comparison))) + checkmate::expect_class(comparison$PhenoData, "data.frame") + checkmate::expect_class(comparison$FeatureData, "data.frame") + expect_length(comparison$Plots, 7) + expect_true(all(c("Means", "Variances", "MeanVar", "LibrarySizes", + "ZerosGene", "ZerosCell", "MeanZeros") %in% + names(comparison$Plots))) + for (plot in names(comparison$Plots)) { + checkmate::expect_class(comparison$Plots[[plot]], "ggplot") + } +}) + +test_that("diffSCEs works", { + expect_length(difference, 5) + expect_true(all(c("Reference", "FeatureData", "PhenoData", "Plots", + "QQPlots") %in% names(difference))) + checkmate::expect_class(difference$Reference, "SingleCellExperiment") + checkmate::expect_class(difference$PhenoData, "data.frame") + checkmate::expect_class(difference$FeatureData, "data.frame") + expect_length(difference$Plots, 7) + expect_true(all(c("Means", "Variances", "MeanVar", "LibrarySizes", + "ZerosGene", "ZerosCell", "MeanZeros") %in% + names(difference$Plots))) + for (plot in names(difference$Plots)) { + checkmate::expect_class(difference$Plots[[plot]], "ggplot") + } + expect_length(difference$QQPlots, 5) + expect_true(all(c("Means", "Variances", "LibrarySizes", "ZerosGene", + "ZerosCell") %in% names(difference$QQPlots))) + for (plot in names(difference$QQPlots)) { + checkmate::expect_class(difference$QQPlots[[plot]], "ggplot") + } +}) + +# test_that("makeCompPanel works", { +# panel <- makeCompPanel(comparison) +# checkmate::expect_class(panel, "ggplot") +# }) +# +# test_that("makeDiffPanel works", { +# panel <- makeDiffPanel(difference) +# checkmate::expect_class(panel, "ggplot") +# }) +# +# test_that("makeOverallPanel works", { +# panel <- makeOverallPanel(comparison, difference) +# checkmate::expect_class(panel, "ggplot") +# }) diff --git a/tests/testthat/test-listSims.R b/tests/testthat/test-listSims.R new file mode 100644 index 0000000..cf386c9 --- /dev/null +++ b/tests/testthat/test-listSims.R @@ -0,0 +1,10 @@ +context("listSims") + +test_that("listSims printing works", { + expect_output(listSims(), "Splatter currently contains") +}) + +test_that("listSims return works", { + ll <- listSims(print = FALSE) + checkmate::expect_class(ll, "data.frame") +}) diff --git a/tests/testthat/test-lunEstimate.R b/tests/testthat/test-lunEstimate.R new file mode 100644 index 0000000..010ae35 --- /dev/null +++ b/tests/testthat/test-lunEstimate.R @@ -0,0 +1,9 @@ +context("lunEstimate") + +library(scater) +data("sc_example_counts") + +test_that("lunEstimate works", { + params <- lunEstimate(sc_example_counts) + expect_true(validObject(params)) +}) diff --git a/tests/testthat/test-phenoEstimate.R b/tests/testthat/test-phenoEstimate.R new file mode 100644 index 0000000..402d781 --- /dev/null +++ b/tests/testthat/test-phenoEstimate.R @@ -0,0 +1,9 @@ +context("phenoEstimate") + +library(scater) +data("sc_example_counts") + +test_that("phenoEstimate works", { + params <- phenoEstimate(sc_example_counts) + expect_true(validObject(params)) +}) diff --git a/tests/testthat/test-simpleEstimate.R b/tests/testthat/test-simpleEstimate.R new file mode 100644 index 0000000..3b581e1 --- /dev/null +++ b/tests/testthat/test-simpleEstimate.R @@ -0,0 +1,9 @@ +context("simpleEstimate") + +library(scater) +data("sc_example_counts") + +test_that("simpleEstimate works", { + params <- simpleEstimate(sc_example_counts) + expect_true(validObject(params)) +}) diff --git a/tests/testthat/test-splat-simulate.R b/tests/testthat/test-splat-simulate.R index 9d99cf7..d5b16f5 100644 --- a/tests/testthat/test-splat-simulate.R +++ b/tests/testthat/test-splat-simulate.R @@ -4,9 +4,11 @@ test.params <- newSplatParams(nGenes = 100, batchCells = c(5, 5), group.prob = c(0.5, 0.5), lib.scale = 0) test_that("splatSimulate output is valid", { - expect_true(validObject(splatSimulate(test.params, method = "single"))) + expect_true(validObject(splatSimulate(test.params, method = "single", + dropout.present = TRUE))) expect_true(validObject(splatSimulate(test.params, method = "groups"))) - expect_true(validObject(splatSimulate(test.params, method = "paths"))) + expect_true(validObject(splatSimulate(test.params, method = "paths", + path.from = c(0, 1)))) }) test_that("one group switches to single mode", { diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R new file mode 100644 index 0000000..d4b7bdc --- /dev/null +++ b/tests/testthat/test-utils.R @@ -0,0 +1,22 @@ +context("utils") + +test_that("logistic function works", { + expect_equal(logistic(0, x0 = 0, k = 1), 0.5) +}) + +test_that("rbindMatched works", { + df1 <- data.frame(A = 1:3, B = 4:6, C = 7:9) + df2 <- data.frame(D = 0) + expect_error(rbindMatched(df1, df2), + "There must be at least two columns in common") + df2 <- data.frame(A = 1:3) + expect_error(rbindMatched(df1, df2), + "There must be at least two columns in common") + df2 <- data.frame(A = 1:3, C = 7:9, D = 0) + expect_equal(colnames(rbindMatched(df1, df2)), c("A", "C")) +}) + +test_that("winsorize works", { + x <- c(0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2) + expect_true(all(winsorize(x, q = 0.1) == 1)) +}) -- GitLab