diff --git a/tests/testthat/test-CytoPipeline.R b/tests/testthat/test-CytoPipeline.R index 4691bf5..3099945 100644 --- a/tests/testthat/test-CytoPipeline.R +++ b/tests/testthat/test-CytoPipeline.R @@ -14,1149 +14,1149 @@ # GNU General Public License for more details (). -# outputDir <- base::tempdir() -# -# if (!interactive()) pdf(NULL) -# -# test_that("CytoPipeline default creation raises no error", { -# expect_error(pipL0 <- CytoPipeline(), NA) -# }) -# -# test_that("Cytopipeline add/remove/clean processing step works", { -# rawDataDir <- system.file("extdata", package = "CytoPipeline") -# experimentName <- "OMIP021_PeacoQC" -# sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, -# pattern = "Donor" -# )) -# transListPath <- file.path(system.file("extdata", -# package = "CytoPipeline"), -# "OMIP021_TransList.rds") -# -# -# # main parameters : sample files and experiment name -# pipelineParams <- list() -# pipelineParams$experimentName <- experimentName -# -# pipL <- CytoPipeline(pipelineParams) -# expect_error(show(pipL), NA) -# -# pipelineParams$sampleFiles <- sampleFiles -# -# pipL <- CytoPipeline(pipelineParams) -# expect_error(show(pipL), NA) -# -# pipL <- addProcessingStep(pipL, -# whichQueue = "scale transform", -# CytoProcessingStep( -# name = "scale_transform_read", -# FUN = "readRDS", -# ARGS = list(file = transListPath) -# ) -# ) -# -# expect_equal(getNbProcessingSteps(pipL, "scale transform"), 1) -# -# pipL <- addProcessingStep(pipL, -# whichQueue = "scale transform", -# CytoProcessingStep( -# name = "scale_transform_sum", -# FUN = "sum", -# ARGS = list() -# ) -# ) -# -# expect_equal(getNbProcessingSteps(pipL, "scale transform"), 2) -# -# pipL <- removeProcessingStep(pipL, -# whichQueue = "scale transform", -# index = 2 -# ) -# expect_equal(getNbProcessingSteps(pipL, "scale transform"), 1) -# pS <- getProcessingStep(pipL, whichQueue = "scale transform", index = 1) -# expect_equal(pS@FUN, "readRDS") -# -# pipL <- addProcessingStep(pipL, -# whichQueue = "pre-processing", -# CytoProcessingStep( -# name = "pre-processing_sum", -# FUN = "sum", -# ARGS = list() -# ) -# ) -# expect_equal(getNbProcessingSteps(pipL, "scale transform"), 1) -# expect_equal(getNbProcessingSteps(pipL, "pre-processing"), 1) -# -# expect_error(pipL <- addProcessingStep(pipL, -# whichQueue = "pre-processing", -# CytoProcessingStep( -# name = "pre-processing_sum", -# FUN = "mean", -# ARGS = list() -# ) -# ), regexp = "There already exist a step") -# -# pipL <- cleanProcessingSteps(pipL) -# expect_equal(getNbProcessingSteps(pipL, "scale transform"), 0) -# expect_equal(getNbProcessingSteps(pipL, "pre-processing"), 0) -# -# newExp <- "newExperiment" -# experimentName(pipL) <- newExp -# expect_equal(experimentName(pipL), newExp) -# -# newPhenoData <- data.frame(name = c("Donor1", "Donor2"), -# donor = c(1,2)) -# expect_error(pData(pipL) <- "invalidCharacterType", -# regexp = "is not TRUE") -# -# expect_error(pData(pipL) <- newPhenoData, -# regexp = "should contain all sample file basenames") -# -# }) -# -# test_that("CytoPipeline with reading scale transfo only raises no error", { -# expect_error( -# { -# rawDataDir <- -# system.file("extdata", package = "CytoPipeline") -# experimentName <- "OMIP021_PeacoQC" -# sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, -# pattern = "Donor" -# )) -# transListPath <- file.path(system.file("extdata", -# package = "CytoPipeline"), -# "OMIP021_TransList.rds") -# -# # main parameters : sample files and output files -# pipelineParams <- list() -# pipelineParams$experimentName <- experimentName -# pipelineParams$sampleFiles <- sampleFiles -# -# pipL <- CytoPipeline(pipelineParams) -# -# pipL <- -# addProcessingStep(pipL, -# whichQueue = "scale transform", -# CytoProcessingStep( -# name = "scale_transform_read", -# FUN = "readRDSObject", -# ARGS = list(RDSFile = transListPath) -# ) -# ) -# -# suppressWarnings(execute(pipL, -# rmCache = TRUE, -# path = outputDir -# )) -# }, -# NA -# ) -# }) -# -# test_that("CytoPipeline with no sample raises an execution error", { -# expect_error( -# { -# rawDataDir <- -# system.file("extdata", package = "CytoPipeline") -# experimentName <- "OMIP021_PeacoQC" -# sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, -# pattern = "NotGood" -# )) -# -# # main parameters : sample files and output files -# pipelineParams <- list() -# pipelineParams$experimentName <- experimentName -# pipelineParams$sampleFiles <- sampleFiles -# -# pipL <- CytoPipeline(pipelineParams) -# -# pipL <- -# addProcessingStep(pipL, -# whichQueue = "scale transform", -# CytoProcessingStep( -# name = "flowframe_read", -# FUN = "readSampleFiles", -# ARGS = list( -# whichSamples = "all", -# truncate_max_range = FALSE, -# min.limit = NULL -# ) -# ) -# ) -# -# suppressWarnings(execute(pipL, -# rmCache = TRUE, -# path = outputDir -# )) -# }, -# "Can't execute CytoPipeline object with no sample file" -# ) -# }) -# -# test_that("Creation of CytoPipeline with wrong phenoData raises an error", { -# expect_error( -# { -# rawDataDir <- -# system.file("extdata", package = "CytoPipeline") -# experimentName <- "OMIP021_PeacoQC" -# sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, -# pattern = "Donor" -# )) -# -# phenoData <- data.frame(row.names = c("wrong1", "wrong2"), -# donor = c(1,2), -# group = c("G1", "G1")) -# -# pipL <- CytoPipeline(experimentName = experimentName, -# sampleFiles = sampleFiles, -# pData = phenoData) -# }, "Row names of non-null @pData slot should contain all sample file") -# }) -# -# test_that("Execution of CytoPipeline with correct phenoData raises no error", { -# expect_error( -# { -# rawDataDir <- -# system.file("extdata", package = "CytoPipeline") -# experimentName <- "OMIP021_PeacoQC" -# sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, -# pattern = "Donor" -# )) -# -# phenoData <- data.frame(row.names = basename(sampleFiles), -# donor = c(1,2), -# group = c("G1", "G1")) -# -# # main parameters : sample files and output files -# pipelineParams <- list() -# pipelineParams$experimentName <- experimentName -# pipelineParams$sampleFiles <- sampleFiles -# pipelineParams$pData <- phenoData -# -# pipL <- CytoPipeline(pipelineParams) -# -# pipL <- -# addProcessingStep(pipL, -# whichQueue = "scale transform", -# CytoProcessingStep( -# name = "flowframe_read", -# FUN = "readSampleFiles", -# ARGS = list( -# whichSamples = "all", -# truncate_max_range = FALSE, -# min.limit = NULL -# ) -# ) -# ) -# -# suppressWarnings(execute(pipL, -# rmCache = TRUE, -# path = outputDir -# )) -# -# # re-execute un second time to test behaviour with pData -# execute(pipL, -# rmCache = FALSE, -# path = outputDir -# ) -# }, -# NA -# ) -# -# newPipL <- buildCytoPipelineFromCache(experimentName, -# path = outputDir) -# -# newPData <- pData(newPipL) -# expect_true(all.equal(phenoData, newPData)) -# }) -# -# -# test_that("CytoPipeline with complex flows raises no error", { -# expect_error( -# { -# rawDataDir <- -# system.file("extdata", package = "CytoPipeline") -# experimentName <- "OMIP021_PeacoQC" -# sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, -# pattern = "Donor" -# )) -# -# # main parameters : sample files and output files -# pipL <- CytoPipeline(experimentName = experimentName, -# sampleFiles = sampleFiles) -# -# ### SCALE TRANSFORMATION STEPS ### -# -# pipL <- -# addProcessingStep(pipL, -# whichQueue = "scale transform", -# CytoProcessingStep( -# name = "flowframe_read", -# FUN = "readSampleFiles", -# ARGS = list( -# whichSamples = "all", -# truncate_max_range = FALSE, -# min.limit = NULL -# ) -# ) -# ) -# -# pipL <- -# addProcessingStep(pipL, -# whichQueue = "scale transform", -# CytoProcessingStep( -# name = "remove_margins", -# FUN = "removeMarginsPeacoQC", -# ARGS = list() -# ) -# ) -# -# pipL <- -# addProcessingStep(pipL, -# whichQueue = "scale transform", -# CytoProcessingStep( -# name = "compensate", -# FUN = "compensateFromMatrix", -# ARGS = list(matrixSource = "fcs") -# ) -# ) -# -# pipL <- -# addProcessingStep(pipL, -# whichQueue = "scale transform", -# CytoProcessingStep( -# name = "flowframe_aggregate", -# FUN = "aggregateAndSample", -# ARGS = list( -# nTotalEvents = 10000, -# seed = 0 -# ) -# ) -# ) -# -# pipL <- -# addProcessingStep(pipL, -# whichQueue = "scale transform", -# CytoProcessingStep( -# name = "scale_transform_estimate", -# FUN = "estimateScaleTransforms", -# ARGS = list( -# fluoMethod = "estimateLogicle", -# scatterMethod = "linear", -# scatterRefMarker = "BV785 - CD3" -# ) -# ) -# ) -# -# ### PRE-PROCESSING STEPS ### -# -# pipL <- -# addProcessingStep(pipL, -# whichQueue = "pre-processing", -# CytoProcessingStep( -# name = "flowframe_read", -# FUN = "readSampleFiles", -# ARGS = list( -# truncate_max_range = FALSE, -# min.limit = NULL -# ) -# ) -# ) -# -# -# pipL <- -# addProcessingStep(pipL, -# whichQueue = "pre-processing", -# CytoProcessingStep( -# name = "remove_margins", -# FUN = "removeMarginsPeacoQC", -# ARGS = list() -# ) -# ) -# -# pipL <- -# addProcessingStep(pipL, -# whichQueue = "pre-processing", -# CytoProcessingStep( -# name = "compensate", -# FUN = "compensateFromMatrix", -# ARGS = list(matrixSource = "fcs") -# ) -# ) -# -# pipL <- -# addProcessingStep( -# pipL, -# whichQueue = "pre-processing", -# CytoProcessingStep( -# name = "remove_doublets", -# FUN = "removeDoubletsCytoPipeline", -# ARGS = list( -# areaChannels = c("FSC-A", "SSC-A"), -# heightChannels = c("FSC-H", "SSC-H"), -# nmads = c(3, 5)) -# ) -# ) -# -# pipL <- -# addProcessingStep(pipL, -# whichQueue = "pre-processing", -# CytoProcessingStep( -# name = "remove_debris", -# FUN = "removeDebrisManualGate", -# ARGS = list( -# FSCChannel = "FSC-A", -# SSCChannel = "SSC-A", -# gateData = c(73615, 110174, 213000, 201000, 126000, -# 47679, 260500, 260500, 113000, 35000) -# ) -# ) -# ) -# -# pipL <- -# addProcessingStep(pipL, -# whichQueue = "pre-processing", -# CytoProcessingStep( -# name = "remove_dead_cells", -# FUN = "removeDeadCellsManualGate", -# ARGS = list( -# FSCChannel = "FSC-A", -# LDMarker = "L/D Aqua - Viability", -# gateData = c(0, 0, 250000, 250000, -# 0, 650, 650, 0) -# ) -# ) -# ) -# -# pipL <- -# addProcessingStep( -# pipL, -# whichQueue = "pre-processing", -# CytoProcessingStep( -# name = "perform_QC", -# FUN = "qualityControlPeacoQC", -# ARGS = list( -# preTransform = TRUE, -# min_cells = 150, # default -# max_bins = 500, # default -# step = 500, # default, -# MAD = 6, # default -# IT_limit = 0.55, # default -# force_IT = 150, # default -# peak_removal = 0.3333, # default -# min_nr_bins_peakdetection = 10 # default -# ) -# ) -# ) -# -# pipL <- -# addProcessingStep(pipL, -# whichQueue = "pre-processing", -# CytoProcessingStep( -# name = "transform", -# FUN = "applyScaleTransforms", -# ARGS = list() -# ) -# ) -# -# suppressWarnings(execute(pipL, -# rmCache = TRUE, -# path = outputDir -# )) -# -# suppressWarnings(execute(pipL, -# rmCache = FALSE, -# path = outputDir, -# saveLastStepFF = FALSE, -# saveScaleTransforms = TRUE -# )) -# }, -# NA -# ) -# }) -# -# test_that("CytoPipeline with json input raises no error", { -# expect_error( -# { -# rawDataDir <- -# system.file("extdata", package = "CytoPipeline") -# experimentName <- "OMIP021_PeacoQC" -# sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, -# pattern = "Donor")) -# jsonDir <- system.file("extdata", package = "CytoPipeline") -# jsonPath <- file.path(jsonDir, "pipelineParams.json") -# -# pipL2 <- CytoPipeline(jsonPath, -# experimentName = experimentName, -# sampleFiles = sampleFiles) -# -# suppressWarnings(execute(pipL2, -# rmCache = TRUE, -# path = outputDir)) -# }, -# NA -# ) -# }) -# -# test_that("CytoPipeline with Biocparallel::Serial (by default) raises no error", -# { -# expect_error( -# { -# rawDataDir <- -# system.file("extdata", package = "CytoPipeline") -# sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, -# pattern = "Donor")) -# -# jsonDir <- system.file("extdata", package = "CytoPipeline") -# jsonPath <- file.path(jsonDir, "pipelineParams.json") -# -# pipL2 <- CytoPipeline(jsonPath, -# sampleFiles = sampleFiles) -# -# # testing changing the experiment name on the fly -# experimentName(pipL2) <- "BPSerial_Experiment" -# -# bp <- BiocParallel::SerialParam() -# BiocParallel::register(bp, default = TRUE) -# suppressWarnings(execute(pipL2, path = outputDir, -# useBiocParallel = TRUE)) -# }, -# NA -# ) -# }) -# -# test_that("CytoPipeline with Biocparallel::SnowParam raises no error", { -# expect_error( -# { -# rawDataDir <- -# system.file("extdata", package = "CytoPipeline") -# sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, -# pattern = "Donor")) -# -# jsonDir <- system.file("extdata", package = "CytoPipeline") -# jsonPath <- file.path(jsonDir, "pipelineParams.json") -# -# pipL2 <- CytoPipeline(jsonPath, -# experimentName = "BPSNOW_Experiment", -# sampleFiles = sampleFiles) -# -# logDir <- file.path(outputDir, "BiocParallel", "log") -# -# suppressWarnings(dir.create(logDir, recursive = TRUE)) -# bp <- BiocParallel::SnowParam(workers = 2, log = TRUE, -# logdir = logDir, -# progressbar = TRUE) -# suppressWarnings(execute(pipL2, path = outputDir, -# useBiocParallel = TRUE, -# BPPARAM = bp, rmCache = TRUE)) -# -# }, -# NA -# ) -# }) -# -# test_that("CytoPipeline export as list works", { -# jsonDir <- system.file("extdata", package = "CytoPipeline") -# jsonPath <- file.path(jsonDir, "pipelineParams.json") -# -# pipL1 <- CytoPipeline(jsonPath) -# pipList <- as.list(pipL1) -# -# pipL2 <- CytoPipeline(pipList) -# expect_identical(pipL1, pipL2) -# -# }) -# -# test_that("CytoPipeline rebuilt from cache raises no error", { -# expect_error( -# { -# experimentName <- "OMIP021_PeacoQC" -# pipL3 <- buildCytoPipelineFromCache( -# experimentName = experimentName, -# path = outputDir -# ) -# suppressWarnings(execute(pipL3, -# rmCache = FALSE, -# path = outputDir -# )) -# }, -# NA -# ) -# }) -# -# -# test_that("CytoPipeline not in cache with warning", { -# expect_warning( -# pipL4 <- buildCytoPipelineFromCache( -# experimentName = "non_existent", -# path = outputDir -# ), -# regexp = "no cache directory found" -# ) -# }) -# -# -# test_that("Check consistency with cache works", { -# rawDataDir <- system.file("extdata", package = "CytoPipeline") -# sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, -# pattern = "Donor" -# )) -# -# pipL5 <- CytoPipeline(experimentName = "DummyExperiment") -# -# sampleFiles(pipL5) <- sampleFiles -# deleteCytoPipelineCache(pipL5, path = outputDir) -# -# pipL5 <- addProcessingStep( -# pipL5, -# "scale transform", -# CytoProcessingStep( -# name = "flowframe_read", -# FUN = "readSampleFiles", -# ARGS = list( -# whichSamples = "all", -# truncate_max_range = FALSE, -# min.limit = NULL -# ) -# ) -# ) -# res <- checkCytoPipelineConsistencyWithCache(pipL5, path = outputDir) -# -# expect_error(suppressWarnings(execute(pipL5, -# rmCache = TRUE, -# path = outputDir -# )), NA) -# -# res <- checkCytoPipelineConsistencyWithCache(pipL5, -# path = outputDir -# ) -# expect_equal(res$isConsistent, TRUE) -# expect_equal(unname(res$scaleTransformStepStatus[1]), "run") -# -# pipL5 <- -# addProcessingStep(pipL5, -# whichQueue = "pre-processing", -# CytoProcessingStep( -# name = "flowframe_read", -# FUN = "readSampleFiles", -# ARGS = list( -# truncate_max_range = FALSE, -# min.limit = NULL -# ) -# ) -# ) -# res <- checkCytoPipelineConsistencyWithCache(pipL5, -# path = outputDir -# ) -# expect_equal(res$isConsistent, TRUE) -# expect_equal(unname(res$scaleTransformStepStatus[1]), "run") -# expect_equal(unname(res$preProcessingStepStatus[1, 1]), "not_run") -# expect_equal(unname(res$preProcessingStepStatus[1, 2]), "not_run") -# expect_equal(res$scaleTransformStepOutputObjNames, c("flowframe_read_obj")) -# expect_equal(res$scaleTransformStepOutputClasses, c("flowSet")) -# expect_equal(res$preProcessingStepOutputObjNames, c("unknown")) -# expect_equal(res$preProcessingStepOutputClasses, c("unknown")) -# -# expect_error(execute(pipL5, -# rmCache = FALSE, -# path = outputDir -# ), NA) -# res <- checkCytoPipelineConsistencyWithCache(pipL5, -# path = outputDir -# ) -# expect_equal(res$isConsistent, TRUE) -# expect_equal(unname(res$scaleTransformStepStatus[1]), "run") -# expect_equal(unname(res$preProcessingStepStatus[1, 1]), "run") -# expect_equal(unname(res$preProcessingStepStatus[1, 2]), "run") -# expect_equal(res$scaleTransformStepOutputObjNames, c("flowframe_read_obj")) -# expect_equal(res$scaleTransformStepOutputClasses, c("flowSet")) -# expect_equal(res$preProcessingStepOutputObjNames, c("flowframe_read_obj")) -# expect_equal(res$preProcessingStepOutputClasses, c("flowFrame")) -# -# pipL5 <- -# addProcessingStep(pipL5, -# whichQueue = "pre-processing", -# CytoProcessingStep( -# name = "remove_margins", -# FUN = "removeMarginsPeacoQC", -# ARGS = list() -# ) -# ) -# -# res <- checkCytoPipelineConsistencyWithCache(pipL5, -# path = outputDir -# ) -# expect_equal(res$isConsistent, TRUE) -# expect_equal(unname(res$scaleTransformStepStatus[1]), "run") -# expect_equal(unname(res$preProcessingStepStatus[1, 1]), "run") -# expect_equal(unname(res$preProcessingStepStatus[1, 2]), "run") -# expect_equal(unname(res$preProcessingStepStatus[2, 1]), "not_run") -# expect_equal(unname(res$preProcessingStepStatus[2, 2]), "not_run") -# expect_equal(res$scaleTransformStepOutputObjNames, c("flowframe_read_obj")) -# expect_equal(res$scaleTransformStepOutputClasses, c("flowSet")) -# expect_equal( -# res$preProcessingStepOutputObjNames, -# c("flowframe_read_obj", "unknown") -# ) -# expect_equal( -# res$preProcessingStepOutputClasses, -# c("flowFrame", "unknown") -# ) -# -# -# expect_error(suppressWarnings(execute(pipL5, -# rmCache = FALSE, -# path = outputDir -# )), NA) -# res <- checkCytoPipelineConsistencyWithCache(pipL5, -# path = outputDir -# ) -# expect_equal(res$isConsistent, TRUE) -# expect_equal(unname(res$scaleTransformStepStatus[1]), "run") -# expect_equal(unname(res$preProcessingStepStatus[1, 1]), "run") -# expect_equal(unname(res$preProcessingStepStatus[1, 2]), "run") -# expect_equal(unname(res$preProcessingStepStatus[2, 1]), "run") -# expect_equal(unname(res$preProcessingStepStatus[2, 2]), "run") -# expect_equal(res$scaleTransformStepOutputObjNames, c("flowframe_read_obj")) -# expect_equal(res$scaleTransformStepOutputClasses, c("flowSet")) -# expect_equal( -# res$preProcessingStepOutputObjNames, -# c("flowframe_read_obj", "remove_margins_obj") -# ) -# expect_equal( -# res$preProcessingStepOutputClasses, -# c("flowFrame", "flowFrame") -# ) -# -# pipL5_bad <- pipL5 -# -# pipL5_bad@flowFramesPreProcessingQueue[[2]]@name <- "aaaaaa" -# res <- checkCytoPipelineConsistencyWithCache(pipL5_bad, -# path = outputDir -# ) -# expect_equal(res$isConsistent, FALSE) -# expect_equal( -# res$inconsistencyMsg, -# paste0( -# "inconsistent pre-processing step #2 for sample file ", -# "Donor1.fcs (different in cache)" -# ) -# ) -# expect_equal(unname(res$scaleTransformStepStatus[1]), "run") -# expect_equal(unname(res$preProcessingStepStatus[1, 1]), "run") -# expect_equal(unname(res$preProcessingStepStatus[2, 1]), "inconsistent") -# -# expect_error(suppressWarnings(execute(pipL5_bad, -# rmCache = FALSE, -# path = outputDir -# )), -# regexp = "inconsistent pre-processing step" -# ) -# -# pipL5_bad2 <- pipL5 -# pipL5_bad2@flowFramesPreProcessingQueue[[1]]@ARGS$truncate_max_range <- -# TRUE -# res <- checkCytoPipelineConsistencyWithCache(pipL5_bad2, -# path = outputDir -# ) -# expect_equal(res$isConsistent, FALSE) -# expect_equal( -# res$inconsistencyMsg, -# paste0( -# "inconsistent pre-processing step #1 for sample file ", -# "Donor1.fcs (different in cache)" -# ) -# ) -# expect_equal(unname(res$scaleTransformStepStatus[1]), "run") -# expect_equal(unname(res$preProcessingStepStatus[1, 1]), "inconsistent") -# expect_equal(unname(res$preProcessingStepStatus[2, 1]), "not_run") -# -# -# pipL5 <- removeProcessingStep(pipL5, -# whichQueue = "pre-processing", -# index = 2 -# ) -# -# res <- checkCytoPipelineConsistencyWithCache(pipL5, -# path = outputDir -# ) -# -# expect_equal(res$isConsistent, FALSE) -# expect_equal( -# res$inconsistencyMsg, -# paste0( -# "more pre-processing steps in cache than in CytoPipeline object" -# ) -# ) -# }) -# -# -# test_that("plotCytoPipelineProcessingQueue works", { -# pipL6 <- CytoPipeline(experimentName = "DummyExperiment") -# -# deleteCytoPipelineCache(pipL6, path = outputDir) -# -# rawDataDir <- system.file("extdata", package = "CytoPipeline") -# sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, -# pattern = "Donor" -# )) -# -# # put only second sample file for the time being -# sampleFiles(pipL6) <- sampleFiles[2] -# pipL6 <- addProcessingStep( -# pipL6, -# "scale transform", -# CytoProcessingStep( -# name = "flowframe_read", -# FUN = "readSampleFiles", -# ARGS = list( -# whichSamples = "all", -# truncate_max_range = FALSE, -# min.limit = NULL -# ) -# ) -# ) -# -# expect_error(plotCytoPipelineProcessingQueue( -# pipL6, -# whichQueue = "scale transform", -# path = outputDir -# ), NA) -# -# expect_error(suppressWarnings(execute(pipL6, -# rmCache = TRUE, -# path = outputDir -# )), NA) -# -# expect_error(plotCytoPipelineProcessingQueue( -# pipL6, -# whichQueue = "scale transform", -# path = outputDir -# ), NA) -# expect_message(plotCytoPipelineProcessingQueue( -# pipL6, -# whichQueue = "pre-processing", -# path = outputDir -# ), -# regexp = "no sample file passed" -# ) -# -# pipL6 <- -# addProcessingStep(pipL6, -# whichQueue = "pre-processing", -# CytoProcessingStep( -# name = "flowframe_read", -# FUN = "readSampleFiles", -# ARGS = list( -# truncate_max_range = FALSE, -# min.limit = NULL -# ) -# ) -# ) -# -# expect_error(plotCytoPipelineProcessingQueue( -# pipL6, -# whichQueue = "scale transform", -# path = outputDir -# ), NA) -# expect_message(plotCytoPipelineProcessingQueue( -# pipL6, -# whichQueue = "pre-processing", -# path = outputDir -# ), -# regexp = "no sample file passed" -# ) -# -# execute(pipL6, rmCache = FALSE, path = outputDir) -# -# plotCytoPipelineProcessingQueue(pipL6, -# whichQueue = "scale transform", -# path = outputDir -# ) -# expect_error(plotCytoPipelineProcessingQueue(pipL6, -# sampleFile = 1, -# whichQueue = "pre-processing", -# path = outputDir -# ), NA) -# expect_error(plotCytoPipelineProcessingQueue(pipL6, -# sampleFile = 2, -# whichQueue = "pre-processing", -# path = outputDir -# ), -# regexp = "out of bounds" -# ) -# -# -# pipL6 <- -# addProcessingStep(pipL6, -# whichQueue = "pre-processing", -# CytoProcessingStep( -# name = "remove_margins", -# FUN = "removeMarginsPeacoQC", -# ARGS = list() -# ) -# ) -# plotCytoPipelineProcessingQueue(pipL6, -# whichQueue = "scale transform", -# path = outputDir -# ) -# plotCytoPipelineProcessingQueue(pipL6, -# sampleFile = 1, -# whichQueue = "pre-processing", -# path = outputDir -# ) -# -# suppressWarnings(execute(pipL6, -# rmCache = FALSE, -# path = outputDir -# )) -# -# plotCytoPipelineProcessingQueue(pipL6, -# whichQueue = "scale transform", -# path = outputDir -# ) -# plotCytoPipelineProcessingQueue(pipL6, -# sampleFile = 1, -# whichQueue = "pre-processing", -# path = outputDir -# ) -# -# # add first sample file to see the impact -# sampleFiles(pipL6) <- sampleFiles -# -# plotCytoPipelineProcessingQueue(pipL6, -# whichQueue = "scale transform", -# path = outputDir -# ) -# # following should show yellow boxes -# plotCytoPipelineProcessingQueue(pipL6, -# whichQueue = "pre-processing", -# path = outputDir -# ) -# # following should show bow in green -# plotCytoPipelineProcessingQueue(pipL6, -# sampleFile = 2, -# whichQueue = "pre-processing", -# path = outputDir -# ) -# -# suppressWarnings(execute(pipL6, -# rmCache = FALSE, -# path = outputDir -# )) -# -# plotCytoPipelineProcessingQueue(pipL6, -# whichQueue = "scale transform", -# path = outputDir -# ) -# # following should now show the green box -# plotCytoPipelineProcessingQueue(pipL6, -# whichQueue = "pre-processing", -# path = outputDir -# ) -# # following as well -# plotCytoPipelineProcessingQueue(pipL6, -# sampleFile = sampleFiles[2], -# whichQueue = "pre-processing", -# path = outputDir -# ) -# -# -# pipL6@flowFramesPreProcessingQueue[[2]]@name <- "aaaaaa" -# -# expect_error(plotCytoPipelineProcessingQueue( -# pipL6, -# whichQueue = "scale transform", -# path = outputDir -# ), -# NA -# ) -# -# expect_warning(plotCytoPipelineProcessingQueue( -# pipL6, -# sampleFile = 1, -# whichQueue = "pre-processing", -# path = outputDir -# ), -# regexp = "CytoPipeline object not consistent with cache" -# ) -# -# -# expect_error(execute(pipL6, -# rmCache = FALSE, -# path = outputDir -# ), regexp = "inconsistent") -# }) -# -# test_that("getCytoPipelineObject works", { -# -# expect_error( -# { -# experimentName <- "OMIP021_PeacoQC" -# -# rawDataDir <- -# system.file("extdata", package = "CytoPipeline") -# sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, -# pattern = "Donor" -# )) -# -# pipL7 <- buildCytoPipelineFromCache( -# experimentName = experimentName, -# path = outputDir -# ) -# -# plotCytoPipelineProcessingQueue(pipL7, -# sampleFile = 1, -# whichQueue = "pre-processing", -# path = outputDir -# ) -# -# getCytoPipelineObjectInfos(pipL7, -# whichQueue = "pre-processing", -# sampleFile = sampleFiles[1], -# path = outputDir -# ) -# getCytoPipelineObjectInfos(pipL7, -# whichQueue = "pre-processing", -# sampleFile = 1, -# path = outputDir -# ) -# getCytoPipelineObjectInfos(pipL7, -# whichQueue = "scale transform", -# sampleFile = sampleFiles[1], -# path = outputDir -# ) -# -# ffFrom <- getCytoPipelineFlowFrame(pipL7, -# whichQueue = "pre-processing", -# sampleFile = sampleFiles[1], -# objectName = "compensate_obj", -# path = outputDir -# ) -# -# ffTo <- getCytoPipelineFlowFrame(pipL7, -# whichQueue = "pre-processing", -# sampleFile = sampleFiles[1], -# objectName = "remove_doublets_obj", -# path = outputDir -# ) -# -# ggplotFilterEvents(ffFrom, ffTo, -# xChannel = "FSC-A", yChannel = "FSC-H" -# ) -# -# plotCytoPipelineProcessingQueue(pipL7, -# whichQueue = "scale transform", -# path = outputDir -# ) -# }, -# NA -# ) -# -# expect_error(getCytoPipelineObjectInfos(pipL7, -# whichQueue = "pre-processing", -# sampleFile = 3, -# path = outputDir), -# "out of bounds") -# -# -# expect_error( -# getCytoPipelineScaleTransform( -# pipL7, -# whichQueue = "scale transform", -# objectName = "flowframe_aggregate_obj", -# path = outputDir -# ), -# regexp = "does not appear to be a transformList" -# ) -# -# -# expect_error( -# transList <- -# getCytoPipelineScaleTransform( -# pipL7, -# whichQueue = "scale transform", -# objectName = "scale_transform_estimate_obj", -# path = outputDir -# ), -# NA -# ) -# -# expect_error( -# ffAgg <- -# getCytoPipelineFlowFrame(pipL7, -# whichQueue = "scale transform", -# objectName = "flowframe_aggregate_obj", -# path = outputDir -# ), -# NA -# ) -# -# -# }) -# -# test_that("collectNbOfRetainedEvents works", { -# -# experimentName <- "OMIP021_PeacoQC" -# -# rawDataDir <- -# system.file("extdata", package = "CytoPipeline") -# sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, -# pattern = "Donor" -# )) -# -# stepNames <- c("flowframe_read", "remove_margins", "compensate", -# "remove_doublets", "remove_debris", "remove_dead_cells", -# "perform_QC", "transform") -# -# # with missing whichSampleFiles argument -# nbEventsDF1 <- collectNbOfRetainedEvents( -# experimentName = experimentName, -# path = outputDir -# ) -# -# expect_equal(unname(colnames(nbEventsDF1)), stepNames) -# expect_equal(unname(rownames(nbEventsDF1)), basename(sampleFiles)) -# expect_equal(nbEventsDF1[1,1], 5000) -# expect_equal(nbEventsDF1[1,2], 4494) -# expect_equal(nbEventsDF1[1,3], 4494) -# expect_equal(nbEventsDF1[1,4], 3541) -# expect_equal(nbEventsDF1[1,5], 2983) -# expect_equal(nbEventsDF1[1,6], 2888) -# expect_equal(nbEventsDF1[1,7], 2000) -# expect_equal(nbEventsDF1[1,8], 2000) -# expect_equal(nbEventsDF1[2,1], 5000) -# expect_equal(nbEventsDF1[2,2], 4700) -# expect_equal(nbEventsDF1[2,3], 4700) -# expect_equal(nbEventsDF1[2,4], 3809) -# expect_equal(nbEventsDF1[2,5], 3347) -# expect_equal(nbEventsDF1[2,6], 3306) -# expect_equal(nbEventsDF1[2,7], 2500) -# expect_equal(nbEventsDF1[2,8], 2500) -# -# # with explicit whichSampleFiles argument -# nbEventsDF2 <- collectNbOfRetainedEvents( -# experimentName = experimentName, -# path = outputDir, -# whichSampleFiles = sampleFiles[1] -# ) -# -# expect_equal(unname(colnames(nbEventsDF2)), stepNames) -# expect_equal(unname(rownames(nbEventsDF2)), basename(sampleFiles[1])) -# expect_equal(nbEventsDF2[1,1], 5000) -# expect_equal(nbEventsDF2[1,2], 4494) -# expect_equal(nbEventsDF2[1,3], 4494) -# expect_equal(nbEventsDF2[1,4], 3541) -# expect_equal(nbEventsDF2[1,5], 2983) -# expect_equal(nbEventsDF2[1,6], 2888) -# expect_equal(nbEventsDF2[1,7], 2000) -# expect_equal(nbEventsDF2[1,8], 2000) -# -# # with wrong whichSampleFiles argument -# expect_error( -# collectNbOfRetainedEvents( -# experimentName = experimentName, -# path = outputDir, -# whichSampleFiles = 3 -# ), regexp = "whichSampleFiles out of bounds") -# -# }) +outputDir <- base::tempdir() + +if (!interactive()) pdf(NULL) + +test_that("CytoPipeline default creation raises no error", { + expect_error(pipL0 <- CytoPipeline(), NA) +}) + +test_that("Cytopipeline add/remove/clean processing step works", { + rawDataDir <- system.file("extdata", package = "CytoPipeline") + experimentName <- "OMIP021_PeacoQC" + sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, + pattern = "Donor" + )) + transListPath <- file.path(system.file("extdata", + package = "CytoPipeline"), + "OMIP021_TransList.rds") + + + # main parameters : sample files and experiment name + pipelineParams <- list() + pipelineParams$experimentName <- experimentName + + pipL <- CytoPipeline(pipelineParams) + expect_error(show(pipL), NA) + + pipelineParams$sampleFiles <- sampleFiles + + pipL <- CytoPipeline(pipelineParams) + expect_error(show(pipL), NA) + + pipL <- addProcessingStep(pipL, + whichQueue = "scale transform", + CytoProcessingStep( + name = "scale_transform_read", + FUN = "readRDS", + ARGS = list(file = transListPath) + ) + ) + + expect_equal(getNbProcessingSteps(pipL, "scale transform"), 1) + + pipL <- addProcessingStep(pipL, + whichQueue = "scale transform", + CytoProcessingStep( + name = "scale_transform_sum", + FUN = "sum", + ARGS = list() + ) + ) + + expect_equal(getNbProcessingSteps(pipL, "scale transform"), 2) + + pipL <- removeProcessingStep(pipL, + whichQueue = "scale transform", + index = 2 + ) + expect_equal(getNbProcessingSteps(pipL, "scale transform"), 1) + pS <- getProcessingStep(pipL, whichQueue = "scale transform", index = 1) + expect_equal(pS@FUN, "readRDS") + + pipL <- addProcessingStep(pipL, + whichQueue = "pre-processing", + CytoProcessingStep( + name = "pre-processing_sum", + FUN = "sum", + ARGS = list() + ) + ) + expect_equal(getNbProcessingSteps(pipL, "scale transform"), 1) + expect_equal(getNbProcessingSteps(pipL, "pre-processing"), 1) + + expect_error(pipL <- addProcessingStep(pipL, + whichQueue = "pre-processing", + CytoProcessingStep( + name = "pre-processing_sum", + FUN = "mean", + ARGS = list() + ) + ), regexp = "There already exist a step") + + pipL <- cleanProcessingSteps(pipL) + expect_equal(getNbProcessingSteps(pipL, "scale transform"), 0) + expect_equal(getNbProcessingSteps(pipL, "pre-processing"), 0) + + newExp <- "newExperiment" + experimentName(pipL) <- newExp + expect_equal(experimentName(pipL), newExp) + + newPhenoData <- data.frame(name = c("Donor1", "Donor2"), + donor = c(1,2)) + expect_error(pData(pipL) <- "invalidCharacterType", + regexp = "is not TRUE") + + expect_error(pData(pipL) <- newPhenoData, + regexp = "should contain all sample file basenames") + +}) + +test_that("CytoPipeline with reading scale transfo only raises no error", { + expect_error( + { + rawDataDir <- + system.file("extdata", package = "CytoPipeline") + experimentName <- "OMIP021_PeacoQC" + sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, + pattern = "Donor" + )) + transListPath <- file.path(system.file("extdata", + package = "CytoPipeline"), + "OMIP021_TransList.rds") + + # main parameters : sample files and output files + pipelineParams <- list() + pipelineParams$experimentName <- experimentName + pipelineParams$sampleFiles <- sampleFiles + + pipL <- CytoPipeline(pipelineParams) + + pipL <- + addProcessingStep(pipL, + whichQueue = "scale transform", + CytoProcessingStep( + name = "scale_transform_read", + FUN = "readRDSObject", + ARGS = list(RDSFile = transListPath) + ) + ) + + suppressWarnings(execute(pipL, + rmCache = TRUE, + path = outputDir + )) + }, + NA + ) +}) + +test_that("CytoPipeline with no sample raises an execution error", { + expect_error( + { + rawDataDir <- + system.file("extdata", package = "CytoPipeline") + experimentName <- "OMIP021_PeacoQC" + sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, + pattern = "NotGood" + )) + + # main parameters : sample files and output files + pipelineParams <- list() + pipelineParams$experimentName <- experimentName + pipelineParams$sampleFiles <- sampleFiles + + pipL <- CytoPipeline(pipelineParams) + + pipL <- + addProcessingStep(pipL, + whichQueue = "scale transform", + CytoProcessingStep( + name = "flowframe_read", + FUN = "readSampleFiles", + ARGS = list( + whichSamples = "all", + truncate_max_range = FALSE, + min.limit = NULL + ) + ) + ) + + suppressWarnings(execute(pipL, + rmCache = TRUE, + path = outputDir + )) + }, + "Can't execute CytoPipeline object with no sample file" + ) +}) + +test_that("Creation of CytoPipeline with wrong phenoData raises an error", { + expect_error( + { + rawDataDir <- + system.file("extdata", package = "CytoPipeline") + experimentName <- "OMIP021_PeacoQC" + sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, + pattern = "Donor" + )) + + phenoData <- data.frame(row.names = c("wrong1", "wrong2"), + donor = c(1,2), + group = c("G1", "G1")) + + pipL <- CytoPipeline(experimentName = experimentName, + sampleFiles = sampleFiles, + pData = phenoData) + }, "Row names of non-null @pData slot should contain all sample file") +}) + +test_that("Execution of CytoPipeline with correct phenoData raises no error", { + expect_error( + { + rawDataDir <- + system.file("extdata", package = "CytoPipeline") + experimentName <- "OMIP021_PeacoQC" + sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, + pattern = "Donor" + )) + + phenoData <- data.frame(row.names = basename(sampleFiles), + donor = c(1,2), + group = c("G1", "G1")) + + # main parameters : sample files and output files + pipelineParams <- list() + pipelineParams$experimentName <- experimentName + pipelineParams$sampleFiles <- sampleFiles + pipelineParams$pData <- phenoData + + pipL <- CytoPipeline(pipelineParams) + + pipL <- + addProcessingStep(pipL, + whichQueue = "scale transform", + CytoProcessingStep( + name = "flowframe_read", + FUN = "readSampleFiles", + ARGS = list( + whichSamples = "all", + truncate_max_range = FALSE, + min.limit = NULL + ) + ) + ) + + suppressWarnings(execute(pipL, + rmCache = TRUE, + path = outputDir + )) + + # re-execute un second time to test behaviour with pData + execute(pipL, + rmCache = FALSE, + path = outputDir + ) + }, + NA + ) + + newPipL <- buildCytoPipelineFromCache(experimentName, + path = outputDir) + + newPData <- pData(newPipL) + expect_true(all.equal(phenoData, newPData)) +}) + + +test_that("CytoPipeline with complex flows raises no error", { + expect_error( + { + rawDataDir <- + system.file("extdata", package = "CytoPipeline") + experimentName <- "OMIP021_PeacoQC" + sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, + pattern = "Donor" + )) + + # main parameters : sample files and output files + pipL <- CytoPipeline(experimentName = experimentName, + sampleFiles = sampleFiles) + + ### SCALE TRANSFORMATION STEPS ### + + pipL <- + addProcessingStep(pipL, + whichQueue = "scale transform", + CytoProcessingStep( + name = "flowframe_read", + FUN = "readSampleFiles", + ARGS = list( + whichSamples = "all", + truncate_max_range = FALSE, + min.limit = NULL + ) + ) + ) + + pipL <- + addProcessingStep(pipL, + whichQueue = "scale transform", + CytoProcessingStep( + name = "remove_margins", + FUN = "removeMarginsPeacoQC", + ARGS = list() + ) + ) + + pipL <- + addProcessingStep(pipL, + whichQueue = "scale transform", + CytoProcessingStep( + name = "compensate", + FUN = "compensateFromMatrix", + ARGS = list(matrixSource = "fcs") + ) + ) + + pipL <- + addProcessingStep(pipL, + whichQueue = "scale transform", + CytoProcessingStep( + name = "flowframe_aggregate", + FUN = "aggregateAndSample", + ARGS = list( + nTotalEvents = 10000, + seed = 0 + ) + ) + ) + + pipL <- + addProcessingStep(pipL, + whichQueue = "scale transform", + CytoProcessingStep( + name = "scale_transform_estimate", + FUN = "estimateScaleTransforms", + ARGS = list( + fluoMethod = "estimateLogicle", + scatterMethod = "linear", + scatterRefMarker = "BV785 - CD3" + ) + ) + ) + + ### PRE-PROCESSING STEPS ### + + pipL <- + addProcessingStep(pipL, + whichQueue = "pre-processing", + CytoProcessingStep( + name = "flowframe_read", + FUN = "readSampleFiles", + ARGS = list( + truncate_max_range = FALSE, + min.limit = NULL + ) + ) + ) + + + pipL <- + addProcessingStep(pipL, + whichQueue = "pre-processing", + CytoProcessingStep( + name = "remove_margins", + FUN = "removeMarginsPeacoQC", + ARGS = list() + ) + ) + + pipL <- + addProcessingStep(pipL, + whichQueue = "pre-processing", + CytoProcessingStep( + name = "compensate", + FUN = "compensateFromMatrix", + ARGS = list(matrixSource = "fcs") + ) + ) + + pipL <- + addProcessingStep( + pipL, + whichQueue = "pre-processing", + CytoProcessingStep( + name = "remove_doublets", + FUN = "removeDoubletsCytoPipeline", + ARGS = list( + areaChannels = c("FSC-A", "SSC-A"), + heightChannels = c("FSC-H", "SSC-H"), + nmads = c(3, 5)) + ) + ) + + pipL <- + addProcessingStep(pipL, + whichQueue = "pre-processing", + CytoProcessingStep( + name = "remove_debris", + FUN = "removeDebrisManualGate", + ARGS = list( + FSCChannel = "FSC-A", + SSCChannel = "SSC-A", + gateData = c(73615, 110174, 213000, 201000, 126000, + 47679, 260500, 260500, 113000, 35000) + ) + ) + ) + + pipL <- + addProcessingStep(pipL, + whichQueue = "pre-processing", + CytoProcessingStep( + name = "remove_dead_cells", + FUN = "removeDeadCellsManualGate", + ARGS = list( + FSCChannel = "FSC-A", + LDMarker = "L/D Aqua - Viability", + gateData = c(0, 0, 250000, 250000, + 0, 650, 650, 0) + ) + ) + ) + + pipL <- + addProcessingStep( + pipL, + whichQueue = "pre-processing", + CytoProcessingStep( + name = "perform_QC", + FUN = "qualityControlPeacoQC", + ARGS = list( + preTransform = TRUE, + min_cells = 150, # default + max_bins = 500, # default + step = 500, # default, + MAD = 6, # default + IT_limit = 0.55, # default + force_IT = 150, # default + peak_removal = 0.3333, # default + min_nr_bins_peakdetection = 10 # default + ) + ) + ) + + pipL <- + addProcessingStep(pipL, + whichQueue = "pre-processing", + CytoProcessingStep( + name = "transform", + FUN = "applyScaleTransforms", + ARGS = list() + ) + ) + + suppressWarnings(execute(pipL, + rmCache = TRUE, + path = outputDir + )) + + suppressWarnings(execute(pipL, + rmCache = FALSE, + path = outputDir, + saveLastStepFF = FALSE, + saveScaleTransforms = TRUE + )) + }, + NA + ) +}) + +test_that("CytoPipeline with json input raises no error", { + expect_error( + { + rawDataDir <- + system.file("extdata", package = "CytoPipeline") + experimentName <- "OMIP021_PeacoQC" + sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, + pattern = "Donor")) + jsonDir <- system.file("extdata", package = "CytoPipeline") + jsonPath <- file.path(jsonDir, "pipelineParams.json") + + pipL2 <- CytoPipeline(jsonPath, + experimentName = experimentName, + sampleFiles = sampleFiles) + + suppressWarnings(execute(pipL2, + rmCache = TRUE, + path = outputDir)) + }, + NA + ) +}) + +test_that("CytoPipeline with Biocparallel::Serial (by default) raises no error", + { + expect_error( + { + rawDataDir <- + system.file("extdata", package = "CytoPipeline") + sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, + pattern = "Donor")) + + jsonDir <- system.file("extdata", package = "CytoPipeline") + jsonPath <- file.path(jsonDir, "pipelineParams.json") + + pipL2 <- CytoPipeline(jsonPath, + sampleFiles = sampleFiles) + + # testing changing the experiment name on the fly + experimentName(pipL2) <- "BPSerial_Experiment" + + bp <- BiocParallel::SerialParam() + BiocParallel::register(bp, default = TRUE) + suppressWarnings(execute(pipL2, path = outputDir, + useBiocParallel = TRUE)) + }, + NA + ) +}) + +test_that("CytoPipeline with Biocparallel::SnowParam raises no error", { + expect_error( + { + rawDataDir <- + system.file("extdata", package = "CytoPipeline") + sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, + pattern = "Donor")) + + jsonDir <- system.file("extdata", package = "CytoPipeline") + jsonPath <- file.path(jsonDir, "pipelineParams.json") + + pipL2 <- CytoPipeline(jsonPath, + experimentName = "BPSNOW_Experiment", + sampleFiles = sampleFiles) + + logDir <- file.path(outputDir, "BiocParallel", "log") + + suppressWarnings(dir.create(logDir, recursive = TRUE)) + bp <- BiocParallel::SnowParam(workers = 2, log = TRUE, + logdir = logDir, + progressbar = TRUE) + suppressWarnings(execute(pipL2, path = outputDir, + useBiocParallel = TRUE, + BPPARAM = bp, rmCache = TRUE)) + + }, + NA + ) +}) + +test_that("CytoPipeline export as list works", { + jsonDir <- system.file("extdata", package = "CytoPipeline") + jsonPath <- file.path(jsonDir, "pipelineParams.json") + + pipL1 <- CytoPipeline(jsonPath) + pipList <- as.list(pipL1) + + pipL2 <- CytoPipeline(pipList) + expect_identical(pipL1, pipL2) + +}) + +test_that("CytoPipeline rebuilt from cache raises no error", { + expect_error( + { + experimentName <- "OMIP021_PeacoQC" + pipL3 <- buildCytoPipelineFromCache( + experimentName = experimentName, + path = outputDir + ) + suppressWarnings(execute(pipL3, + rmCache = FALSE, + path = outputDir + )) + }, + NA + ) +}) + + +test_that("CytoPipeline not in cache with warning", { + expect_warning( + pipL4 <- buildCytoPipelineFromCache( + experimentName = "non_existent", + path = outputDir + ), + regexp = "no cache directory found" + ) +}) + + +test_that("Check consistency with cache works", { + rawDataDir <- system.file("extdata", package = "CytoPipeline") + sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, + pattern = "Donor" + )) + + pipL5 <- CytoPipeline(experimentName = "DummyExperiment") + + sampleFiles(pipL5) <- sampleFiles + deleteCytoPipelineCache(pipL5, path = outputDir) + + pipL5 <- addProcessingStep( + pipL5, + "scale transform", + CytoProcessingStep( + name = "flowframe_read", + FUN = "readSampleFiles", + ARGS = list( + whichSamples = "all", + truncate_max_range = FALSE, + min.limit = NULL + ) + ) + ) + res <- checkCytoPipelineConsistencyWithCache(pipL5, path = outputDir) + + expect_error(suppressWarnings(execute(pipL5, + rmCache = TRUE, + path = outputDir + )), NA) + + res <- checkCytoPipelineConsistencyWithCache(pipL5, + path = outputDir + ) + expect_equal(res$isConsistent, TRUE) + expect_equal(unname(res$scaleTransformStepStatus[1]), "run") + + pipL5 <- + addProcessingStep(pipL5, + whichQueue = "pre-processing", + CytoProcessingStep( + name = "flowframe_read", + FUN = "readSampleFiles", + ARGS = list( + truncate_max_range = FALSE, + min.limit = NULL + ) + ) + ) + res <- checkCytoPipelineConsistencyWithCache(pipL5, + path = outputDir + ) + expect_equal(res$isConsistent, TRUE) + expect_equal(unname(res$scaleTransformStepStatus[1]), "run") + expect_equal(unname(res$preProcessingStepStatus[1, 1]), "not_run") + expect_equal(unname(res$preProcessingStepStatus[1, 2]), "not_run") + expect_equal(res$scaleTransformStepOutputObjNames, c("flowframe_read_obj")) + expect_equal(res$scaleTransformStepOutputClasses, c("flowSet")) + expect_equal(res$preProcessingStepOutputObjNames, c("unknown")) + expect_equal(res$preProcessingStepOutputClasses, c("unknown")) + + expect_error(execute(pipL5, + rmCache = FALSE, + path = outputDir + ), NA) + res <- checkCytoPipelineConsistencyWithCache(pipL5, + path = outputDir + ) + expect_equal(res$isConsistent, TRUE) + expect_equal(unname(res$scaleTransformStepStatus[1]), "run") + expect_equal(unname(res$preProcessingStepStatus[1, 1]), "run") + expect_equal(unname(res$preProcessingStepStatus[1, 2]), "run") + expect_equal(res$scaleTransformStepOutputObjNames, c("flowframe_read_obj")) + expect_equal(res$scaleTransformStepOutputClasses, c("flowSet")) + expect_equal(res$preProcessingStepOutputObjNames, c("flowframe_read_obj")) + expect_equal(res$preProcessingStepOutputClasses, c("flowFrame")) + + pipL5 <- + addProcessingStep(pipL5, + whichQueue = "pre-processing", + CytoProcessingStep( + name = "remove_margins", + FUN = "removeMarginsPeacoQC", + ARGS = list() + ) + ) + + res <- checkCytoPipelineConsistencyWithCache(pipL5, + path = outputDir + ) + expect_equal(res$isConsistent, TRUE) + expect_equal(unname(res$scaleTransformStepStatus[1]), "run") + expect_equal(unname(res$preProcessingStepStatus[1, 1]), "run") + expect_equal(unname(res$preProcessingStepStatus[1, 2]), "run") + expect_equal(unname(res$preProcessingStepStatus[2, 1]), "not_run") + expect_equal(unname(res$preProcessingStepStatus[2, 2]), "not_run") + expect_equal(res$scaleTransformStepOutputObjNames, c("flowframe_read_obj")) + expect_equal(res$scaleTransformStepOutputClasses, c("flowSet")) + expect_equal( + res$preProcessingStepOutputObjNames, + c("flowframe_read_obj", "unknown") + ) + expect_equal( + res$preProcessingStepOutputClasses, + c("flowFrame", "unknown") + ) + + + expect_error(suppressWarnings(execute(pipL5, + rmCache = FALSE, + path = outputDir + )), NA) + res <- checkCytoPipelineConsistencyWithCache(pipL5, + path = outputDir + ) + expect_equal(res$isConsistent, TRUE) + expect_equal(unname(res$scaleTransformStepStatus[1]), "run") + expect_equal(unname(res$preProcessingStepStatus[1, 1]), "run") + expect_equal(unname(res$preProcessingStepStatus[1, 2]), "run") + expect_equal(unname(res$preProcessingStepStatus[2, 1]), "run") + expect_equal(unname(res$preProcessingStepStatus[2, 2]), "run") + expect_equal(res$scaleTransformStepOutputObjNames, c("flowframe_read_obj")) + expect_equal(res$scaleTransformStepOutputClasses, c("flowSet")) + expect_equal( + res$preProcessingStepOutputObjNames, + c("flowframe_read_obj", "remove_margins_obj") + ) + expect_equal( + res$preProcessingStepOutputClasses, + c("flowFrame", "flowFrame") + ) + + pipL5_bad <- pipL5 + + pipL5_bad@flowFramesPreProcessingQueue[[2]]@name <- "aaaaaa" + res <- checkCytoPipelineConsistencyWithCache(pipL5_bad, + path = outputDir + ) + expect_equal(res$isConsistent, FALSE) + expect_equal( + res$inconsistencyMsg, + paste0( + "inconsistent pre-processing step #2 for sample file ", + "Donor1.fcs (different in cache)" + ) + ) + expect_equal(unname(res$scaleTransformStepStatus[1]), "run") + expect_equal(unname(res$preProcessingStepStatus[1, 1]), "run") + expect_equal(unname(res$preProcessingStepStatus[2, 1]), "inconsistent") + + expect_error(suppressWarnings(execute(pipL5_bad, + rmCache = FALSE, + path = outputDir + )), + regexp = "inconsistent pre-processing step" + ) + + pipL5_bad2 <- pipL5 + pipL5_bad2@flowFramesPreProcessingQueue[[1]]@ARGS$truncate_max_range <- + TRUE + res <- checkCytoPipelineConsistencyWithCache(pipL5_bad2, + path = outputDir + ) + expect_equal(res$isConsistent, FALSE) + expect_equal( + res$inconsistencyMsg, + paste0( + "inconsistent pre-processing step #1 for sample file ", + "Donor1.fcs (different in cache)" + ) + ) + expect_equal(unname(res$scaleTransformStepStatus[1]), "run") + expect_equal(unname(res$preProcessingStepStatus[1, 1]), "inconsistent") + expect_equal(unname(res$preProcessingStepStatus[2, 1]), "not_run") + + + pipL5 <- removeProcessingStep(pipL5, + whichQueue = "pre-processing", + index = 2 + ) + + res <- checkCytoPipelineConsistencyWithCache(pipL5, + path = outputDir + ) + + expect_equal(res$isConsistent, FALSE) + expect_equal( + res$inconsistencyMsg, + paste0( + "more pre-processing steps in cache than in CytoPipeline object" + ) + ) +}) + + +test_that("plotCytoPipelineProcessingQueue works", { + pipL6 <- CytoPipeline(experimentName = "DummyExperiment") + + deleteCytoPipelineCache(pipL6, path = outputDir) + + rawDataDir <- system.file("extdata", package = "CytoPipeline") + sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, + pattern = "Donor" + )) + + # put only second sample file for the time being + sampleFiles(pipL6) <- sampleFiles[2] + pipL6 <- addProcessingStep( + pipL6, + "scale transform", + CytoProcessingStep( + name = "flowframe_read", + FUN = "readSampleFiles", + ARGS = list( + whichSamples = "all", + truncate_max_range = FALSE, + min.limit = NULL + ) + ) + ) + + expect_error(plotCytoPipelineProcessingQueue( + pipL6, + whichQueue = "scale transform", + path = outputDir + ), NA) + + expect_error(suppressWarnings(execute(pipL6, + rmCache = TRUE, + path = outputDir + )), NA) + + expect_error(plotCytoPipelineProcessingQueue( + pipL6, + whichQueue = "scale transform", + path = outputDir + ), NA) + expect_message(plotCytoPipelineProcessingQueue( + pipL6, + whichQueue = "pre-processing", + path = outputDir + ), + regexp = "no sample file passed" + ) + + pipL6 <- + addProcessingStep(pipL6, + whichQueue = "pre-processing", + CytoProcessingStep( + name = "flowframe_read", + FUN = "readSampleFiles", + ARGS = list( + truncate_max_range = FALSE, + min.limit = NULL + ) + ) + ) + + expect_error(plotCytoPipelineProcessingQueue( + pipL6, + whichQueue = "scale transform", + path = outputDir + ), NA) + expect_message(plotCytoPipelineProcessingQueue( + pipL6, + whichQueue = "pre-processing", + path = outputDir + ), + regexp = "no sample file passed" + ) + + execute(pipL6, rmCache = FALSE, path = outputDir) + + plotCytoPipelineProcessingQueue(pipL6, + whichQueue = "scale transform", + path = outputDir + ) + expect_error(plotCytoPipelineProcessingQueue(pipL6, + sampleFile = 1, + whichQueue = "pre-processing", + path = outputDir + ), NA) + expect_error(plotCytoPipelineProcessingQueue(pipL6, + sampleFile = 2, + whichQueue = "pre-processing", + path = outputDir + ), + regexp = "out of bounds" + ) + + + pipL6 <- + addProcessingStep(pipL6, + whichQueue = "pre-processing", + CytoProcessingStep( + name = "remove_margins", + FUN = "removeMarginsPeacoQC", + ARGS = list() + ) + ) + plotCytoPipelineProcessingQueue(pipL6, + whichQueue = "scale transform", + path = outputDir + ) + plotCytoPipelineProcessingQueue(pipL6, + sampleFile = 1, + whichQueue = "pre-processing", + path = outputDir + ) + + suppressWarnings(execute(pipL6, + rmCache = FALSE, + path = outputDir + )) + + plotCytoPipelineProcessingQueue(pipL6, + whichQueue = "scale transform", + path = outputDir + ) + plotCytoPipelineProcessingQueue(pipL6, + sampleFile = 1, + whichQueue = "pre-processing", + path = outputDir + ) + + # add first sample file to see the impact + sampleFiles(pipL6) <- sampleFiles + + plotCytoPipelineProcessingQueue(pipL6, + whichQueue = "scale transform", + path = outputDir + ) + # following should show yellow boxes + plotCytoPipelineProcessingQueue(pipL6, + whichQueue = "pre-processing", + path = outputDir + ) + # following should show bow in green + plotCytoPipelineProcessingQueue(pipL6, + sampleFile = 2, + whichQueue = "pre-processing", + path = outputDir + ) + + suppressWarnings(execute(pipL6, + rmCache = FALSE, + path = outputDir + )) + + plotCytoPipelineProcessingQueue(pipL6, + whichQueue = "scale transform", + path = outputDir + ) + # following should now show the green box + plotCytoPipelineProcessingQueue(pipL6, + whichQueue = "pre-processing", + path = outputDir + ) + # following as well + plotCytoPipelineProcessingQueue(pipL6, + sampleFile = sampleFiles[2], + whichQueue = "pre-processing", + path = outputDir + ) + + + pipL6@flowFramesPreProcessingQueue[[2]]@name <- "aaaaaa" + + expect_error(plotCytoPipelineProcessingQueue( + pipL6, + whichQueue = "scale transform", + path = outputDir + ), + NA + ) + + expect_warning(plotCytoPipelineProcessingQueue( + pipL6, + sampleFile = 1, + whichQueue = "pre-processing", + path = outputDir + ), + regexp = "CytoPipeline object not consistent with cache" + ) + + + expect_error(execute(pipL6, + rmCache = FALSE, + path = outputDir + ), regexp = "inconsistent") +}) + +test_that("getCytoPipelineObject works", { + + expect_error( + { + experimentName <- "OMIP021_PeacoQC" + + rawDataDir <- + system.file("extdata", package = "CytoPipeline") + sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, + pattern = "Donor" + )) + + pipL7 <- buildCytoPipelineFromCache( + experimentName = experimentName, + path = outputDir + ) + + plotCytoPipelineProcessingQueue(pipL7, + sampleFile = 1, + whichQueue = "pre-processing", + path = outputDir + ) + + getCytoPipelineObjectInfos(pipL7, + whichQueue = "pre-processing", + sampleFile = sampleFiles[1], + path = outputDir + ) + getCytoPipelineObjectInfos(pipL7, + whichQueue = "pre-processing", + sampleFile = 1, + path = outputDir + ) + getCytoPipelineObjectInfos(pipL7, + whichQueue = "scale transform", + sampleFile = sampleFiles[1], + path = outputDir + ) + + ffFrom <- getCytoPipelineFlowFrame(pipL7, + whichQueue = "pre-processing", + sampleFile = sampleFiles[1], + objectName = "compensate_obj", + path = outputDir + ) + + ffTo <- getCytoPipelineFlowFrame(pipL7, + whichQueue = "pre-processing", + sampleFile = sampleFiles[1], + objectName = "remove_doublets_obj", + path = outputDir + ) + + ggplotFilterEvents(ffFrom, ffTo, + xChannel = "FSC-A", yChannel = "FSC-H" + ) + + plotCytoPipelineProcessingQueue(pipL7, + whichQueue = "scale transform", + path = outputDir + ) + }, + NA + ) + + expect_error(getCytoPipelineObjectInfos(pipL7, + whichQueue = "pre-processing", + sampleFile = 3, + path = outputDir), + "out of bounds") + + + expect_error( + getCytoPipelineScaleTransform( + pipL7, + whichQueue = "scale transform", + objectName = "flowframe_aggregate_obj", + path = outputDir + ), + regexp = "does not appear to be a transformList" + ) + + + expect_error( + transList <- + getCytoPipelineScaleTransform( + pipL7, + whichQueue = "scale transform", + objectName = "scale_transform_estimate_obj", + path = outputDir + ), + NA + ) + + expect_error( + ffAgg <- + getCytoPipelineFlowFrame(pipL7, + whichQueue = "scale transform", + objectName = "flowframe_aggregate_obj", + path = outputDir + ), + NA + ) + + +}) + +test_that("collectNbOfRetainedEvents works", { + + experimentName <- "OMIP021_PeacoQC" + + rawDataDir <- + system.file("extdata", package = "CytoPipeline") + sampleFiles <- file.path(rawDataDir, list.files(rawDataDir, + pattern = "Donor" + )) + + stepNames <- c("flowframe_read", "remove_margins", "compensate", + "remove_doublets", "remove_debris", "remove_dead_cells", + "perform_QC", "transform") + + # with missing whichSampleFiles argument + nbEventsDF1 <- collectNbOfRetainedEvents( + experimentName = experimentName, + path = outputDir + ) + + expect_equal(unname(colnames(nbEventsDF1)), stepNames) + expect_equal(unname(rownames(nbEventsDF1)), basename(sampleFiles)) + expect_equal(nbEventsDF1[1,1], 5000) + expect_equal(nbEventsDF1[1,2], 4494) + expect_equal(nbEventsDF1[1,3], 4494) + expect_equal(nbEventsDF1[1,4], 3541) + expect_equal(nbEventsDF1[1,5], 2983) + expect_equal(nbEventsDF1[1,6], 2888) + expect_equal(nbEventsDF1[1,7], 2000) + expect_equal(nbEventsDF1[1,8], 2000) + expect_equal(nbEventsDF1[2,1], 5000) + expect_equal(nbEventsDF1[2,2], 4700) + expect_equal(nbEventsDF1[2,3], 4700) + expect_equal(nbEventsDF1[2,4], 3809) + expect_equal(nbEventsDF1[2,5], 3347) + expect_equal(nbEventsDF1[2,6], 3306) + expect_equal(nbEventsDF1[2,7], 2500) + expect_equal(nbEventsDF1[2,8], 2500) + + # with explicit whichSampleFiles argument + nbEventsDF2 <- collectNbOfRetainedEvents( + experimentName = experimentName, + path = outputDir, + whichSampleFiles = sampleFiles[1] + ) + + expect_equal(unname(colnames(nbEventsDF2)), stepNames) + expect_equal(unname(rownames(nbEventsDF2)), basename(sampleFiles[1])) + expect_equal(nbEventsDF2[1,1], 5000) + expect_equal(nbEventsDF2[1,2], 4494) + expect_equal(nbEventsDF2[1,3], 4494) + expect_equal(nbEventsDF2[1,4], 3541) + expect_equal(nbEventsDF2[1,5], 2983) + expect_equal(nbEventsDF2[1,6], 2888) + expect_equal(nbEventsDF2[1,7], 2000) + expect_equal(nbEventsDF2[1,8], 2000) + + # with wrong whichSampleFiles argument + expect_error( + collectNbOfRetainedEvents( + experimentName = experimentName, + path = outputDir, + whichSampleFiles = 3 + ), regexp = "whichSampleFiles out of bounds") + +}) diff --git a/tests/testthat/test-CytoProcessingStep.R b/tests/testthat/test-CytoProcessingStep.R index acfc9d6..7273e7e 100644 --- a/tests/testthat/test-CytoProcessingStep.R +++ b/tests/testthat/test-CytoProcessingStep.R @@ -14,113 +14,113 @@ # GNU General Public License for more details (). -# # obtain OMIP021UTSamples, light-weight version used specifically for these -# # unit tests -# path <- system.file("scripts", -# package = "CytoPipeline" -# ) -# -# source(file.path(path,"MakeOMIP021UTSamples.R")) -# -# test_that("CytoProcessingStep basics works", { -# ps <- CytoProcessingStep("summing step", sum) -# -# psName <- getCPSName(ps) -# expect_equal(psName, "summing step") -# -# psFUN <- getCPSFUN(ps) -# expect_true(is.primitive(psFUN)) -# -# psARGS <- getCPSARGS(ps) -# expect_identical(psARGS, list()) -# -# expect_error(show(ps), NA) -# -# res <- executeProcessingStep(ps, 1:10) -# expect_equal(res, 55) -# }) -# -# test_that("CytoProcessingStep works with pData", { -# sumWithBounds <- function(low, high) { -# if (low > high) stop("low > high !") -# sum(seq(from = low, to = high)) -# } -# ps <- CytoProcessingStep("summing step", -# sumWithBounds, -# ARGS = list(low = 1, high = 10)) -# res <- executeProcessingStep(ps) -# expect_equal(res, 55) -# -# sPD <- data.frame(VAR1 = 5, VAR2 = 8) -# -# ps2 <- CytoProcessingStep("summing step", -# sumWithBounds, -# ARGS = list(low = 1, -# high = "$VAR2")) -# res2 <- executeProcessingStep(ps2, -# pData = sPD) -# expect_equal(res2, 36) -# -# ps3 <- CytoProcessingStep("summing step", -# sumWithBounds, -# ARGS = list(low = "$VAR1", -# high = "$VAR2")) -# -# res3 <- executeProcessingStep(ps3, -# pData = sPD) -# -# expect_equal(res3, 26) -# -# }) -# -# test_that("CytoProcessingStep wrong function works", { -# ps <- CytoProcessingStep("dummy step", "mistake_fun") -# -# psName <- getCPSName(ps) -# expect_equal(psName, "dummy step") -# -# expect_error(executeProcessingStep(ps, 1:10), -# regexp = "not found") -# }) -# -# test_that("CytoProcessingStep exports and imports work", { -# # case of a primitive -# ps <- CytoProcessingStep("summing step", sum) -# js_str <- as.json.CytoProcessingStep(ps) -# -# ps2 <- from.json.CytoProcessingStep(js_str) -# -# res <- executeProcessingStep(ps2, 1:10) -# expect_equal(res, 55) -# -# # case of a generic function -# ps <- CytoProcessingStep("median step", stats::median) -# -# js_str <- as.json.CytoProcessingStep(ps) -# -# ps2 <- from.json.CytoProcessingStep(js_str) -# -# res <- executeProcessingStep(ps2, 1:10) -# expect_equal(res, 5.5) -# -# # other case -# ps <- CytoProcessingStep("compensate step", "compensateFromMatrix") -# -# ff <- executeProcessingStep(ps, OMIP021UTSamples[[1]]) -# res <- sum(flowCore::exprs(ff)[,"FSC-A"]) -# expect_equal(res, 12553542.8) -# -# js_str <- as.json.CytoProcessingStep(ps) -# ps2 <- from.json.CytoProcessingStep(js_str) -# -# ff <- executeProcessingStep(ps2, OMIP021UTSamples[[1]]) -# res <- sum(flowCore::exprs(ff)[,"FSC-A"]) -# expect_equal(res, 12553542.8) -# -# # not yet implemented case (non generic, non primitive function as object) -# ps <- CytoProcessingStep("compensate step", compensateFromMatrix) -# expect_error(as.json.CytoProcessingStep(ps), -# regexp = "does not work") -# -# }) -# +# obtain OMIP021UTSamples, light-weight version used specifically for these +# unit tests +path <- system.file("scripts", + package = "CytoPipeline" +) + +source(file.path(path,"MakeOMIP021UTSamples.R")) + +test_that("CytoProcessingStep basics works", { + ps <- CytoProcessingStep("summing step", sum) + + psName <- getCPSName(ps) + expect_equal(psName, "summing step") + + psFUN <- getCPSFUN(ps) + expect_true(is.primitive(psFUN)) + + psARGS <- getCPSARGS(ps) + expect_identical(psARGS, list()) + + expect_error(show(ps), NA) + + res <- executeProcessingStep(ps, 1:10) + expect_equal(res, 55) +}) + +test_that("CytoProcessingStep works with pData", { + sumWithBounds <- function(low, high) { + if (low > high) stop("low > high !") + sum(seq(from = low, to = high)) + } + ps <- CytoProcessingStep("summing step", + sumWithBounds, + ARGS = list(low = 1, high = 10)) + res <- executeProcessingStep(ps) + expect_equal(res, 55) + + sPD <- data.frame(VAR1 = 5, VAR2 = 8) + + ps2 <- CytoProcessingStep("summing step", + sumWithBounds, + ARGS = list(low = 1, + high = "$VAR2")) + res2 <- executeProcessingStep(ps2, + pData = sPD) + expect_equal(res2, 36) + + ps3 <- CytoProcessingStep("summing step", + sumWithBounds, + ARGS = list(low = "$VAR1", + high = "$VAR2")) + + res3 <- executeProcessingStep(ps3, + pData = sPD) + + expect_equal(res3, 26) + +}) + +test_that("CytoProcessingStep wrong function works", { + ps <- CytoProcessingStep("dummy step", "mistake_fun") + + psName <- getCPSName(ps) + expect_equal(psName, "dummy step") + + expect_error(executeProcessingStep(ps, 1:10), + regexp = "not found") +}) + +test_that("CytoProcessingStep exports and imports work", { + # case of a primitive + ps <- CytoProcessingStep("summing step", sum) + js_str <- as.json.CytoProcessingStep(ps) + + ps2 <- from.json.CytoProcessingStep(js_str) + + res <- executeProcessingStep(ps2, 1:10) + expect_equal(res, 55) + + # case of a generic function + ps <- CytoProcessingStep("median step", stats::median) + + js_str <- as.json.CytoProcessingStep(ps) + + ps2 <- from.json.CytoProcessingStep(js_str) + + res <- executeProcessingStep(ps2, 1:10) + expect_equal(res, 5.5) + + # other case + ps <- CytoProcessingStep("compensate step", "compensateFromMatrix") + + ff <- executeProcessingStep(ps, OMIP021UTSamples[[1]]) + res <- sum(flowCore::exprs(ff)[,"FSC-A"]) + expect_equal(res, 12553542.8) + + js_str <- as.json.CytoProcessingStep(ps) + ps2 <- from.json.CytoProcessingStep(js_str) + + ff <- executeProcessingStep(ps2, OMIP021UTSamples[[1]]) + res <- sum(flowCore::exprs(ff)[,"FSC-A"]) + expect_equal(res, 12553542.8) + + # not yet implemented case (non generic, non primitive function as object) + ps <- CytoProcessingStep("compensate step", compensateFromMatrix) + expect_error(as.json.CytoProcessingStep(ps), + regexp = "does not work") + +}) + diff --git a/tests/testthat/test-gating.R b/tests/testthat/test-gating.R index dad6b04..57dd3ea 100644 --- a/tests/testthat/test-gating.R +++ b/tests/testthat/test-gating.R @@ -13,114 +13,114 @@ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details (). -# # obtain OMIP021UTSamples, light-weight version used specifically for these -# # unit tests -# path <- system.file("scripts", -# package = "CytoPipeline" -# ) -# -# source(file.path(path,"MakeOMIP021UTSamples.R")) -# -# test_that("singletsGate works", { -# mySingletsGate <- singletsGate(OMIP021UTSamples[[1]]) -# -# selectedSinglets <- flowCore::filter( -# OMIP021UTSamples[[1]], -# mySingletsGate -# ) -# ff_l <- flowCore::Subset(OMIP021UTSamples[[1]], selectedSinglets) -# -# linRange <- c(0, 250000) -# p <- ggplotFilterEvents( -# ffPre = OMIP021UTSamples[[1]], -# ffPost = ff_l, -# seed = 1, -# xChannel = "FSC-A", xLinearRange = linRange, -# yChannel = "FSC-H", yLinearRange = linRange -# ) -# -# vdiffr::expect_doppelganger("singletsGate default channels", fig = p) -# -# mySingletsGate <- singletsGate(OMIP021UTSamples[[1]], nmad = 3) -# -# selectedSinglets <- flowCore::filter( -# OMIP021UTSamples[[1]], -# mySingletsGate -# ) -# ff_l <- flowCore::Subset(OMIP021UTSamples[[1]], selectedSinglets) -# -# p <- ggplotFilterEvents( -# ffPre = OMIP021UTSamples[[1]], -# ffPost = ff_l, -# seed = 1, -# xChannel = "FSC-A", xLinearRange = linRange, -# yChannel = "FSC-H", yLinearRange = linRange -# ) -# -# vdiffr::expect_doppelganger( -# "singletsGate default channels with fixed nmad", -# fig = p -# ) -# -# mySingletsGate <- singletsGate(OMIP021UTSamples[[1]], -# channel1 = "SSC-A", -# channel2 = "SSC-H" -# ) -# -# selectedSinglets <- flowCore::filter( -# OMIP021UTSamples[[1]], -# mySingletsGate -# ) -# ff_l <- flowCore::Subset(OMIP021UTSamples[[1]], selectedSinglets) -# -# p <- ggplotFilterEvents( -# ffPre = OMIP021UTSamples[[1]], -# ffPost = ff_l, -# seed = 1, -# xChannel = "SSC-A", xLinearRange = linRange, -# yChannel = "SSC-H", yLinearRange = linRange -# ) -# -# vdiffr::expect_doppelganger("singletsGate selected channels", fig = p) -# -# # test application of two gates one after the other -# singletsGate1 <- singletsGate(OMIP021UTSamples[[1]], nmad = 3) -# singletsGate2 <- singletsGate(OMIP021UTSamples[[1]], -# channel1 = "SSC-A", -# channel2 = "SSC-H", -# filterId = "Singlets2" -# ) -# -# singletCombinedGate <- singletsGate1 & singletsGate2 -# -# selectedSinglets <- flowCore::filter( -# OMIP021UTSamples[[1]], -# singletCombinedGate -# ) -# -# ff_l <- flowCore::Subset(OMIP021UTSamples[[1]], selectedSinglets) -# -# p1 <- ggplotFilterEvents( -# ffPre = OMIP021UTSamples[[1]], -# ffPost = ff_l, -# seed = 1, -# xChannel = "FSC-A", xLinearRange = linRange, -# yChannel = "FSC-H", yLinearRange = linRange -# ) -# -# p2 <- ggplotFilterEvents( -# ffPre = OMIP021UTSamples[[1]], -# ffPost = ff_l, -# seed = 1, -# xChannel = "SSC-A", xLinearRange = linRange, -# yChannel = "SSC-H", yLinearRange = linRange -# ) -# -# -# vdiffr::expect_doppelganger("singletsGates one after the other - fig1", -# fig = p1 -# ) -# vdiffr::expect_doppelganger("singletsGates one after the other - fig2", -# fig = p2 -# ) -# }) +# obtain OMIP021UTSamples, light-weight version used specifically for these +# unit tests +path <- system.file("scripts", + package = "CytoPipeline" +) + +source(file.path(path,"MakeOMIP021UTSamples.R")) + +test_that("singletsGate works", { + mySingletsGate <- singletsGate(OMIP021UTSamples[[1]]) + + selectedSinglets <- flowCore::filter( + OMIP021UTSamples[[1]], + mySingletsGate + ) + ff_l <- flowCore::Subset(OMIP021UTSamples[[1]], selectedSinglets) + + linRange <- c(0, 250000) + p <- ggplotFilterEvents( + ffPre = OMIP021UTSamples[[1]], + ffPost = ff_l, + seed = 1, + xChannel = "FSC-A", xLinearRange = linRange, + yChannel = "FSC-H", yLinearRange = linRange + ) + + vdiffr::expect_doppelganger("singletsGate default channels", fig = p) + + mySingletsGate <- singletsGate(OMIP021UTSamples[[1]], nmad = 3) + + selectedSinglets <- flowCore::filter( + OMIP021UTSamples[[1]], + mySingletsGate + ) + ff_l <- flowCore::Subset(OMIP021UTSamples[[1]], selectedSinglets) + + p <- ggplotFilterEvents( + ffPre = OMIP021UTSamples[[1]], + ffPost = ff_l, + seed = 1, + xChannel = "FSC-A", xLinearRange = linRange, + yChannel = "FSC-H", yLinearRange = linRange + ) + + vdiffr::expect_doppelganger( + "singletsGate default channels with fixed nmad", + fig = p + ) + + mySingletsGate <- singletsGate(OMIP021UTSamples[[1]], + channel1 = "SSC-A", + channel2 = "SSC-H" + ) + + selectedSinglets <- flowCore::filter( + OMIP021UTSamples[[1]], + mySingletsGate + ) + ff_l <- flowCore::Subset(OMIP021UTSamples[[1]], selectedSinglets) + + p <- ggplotFilterEvents( + ffPre = OMIP021UTSamples[[1]], + ffPost = ff_l, + seed = 1, + xChannel = "SSC-A", xLinearRange = linRange, + yChannel = "SSC-H", yLinearRange = linRange + ) + + vdiffr::expect_doppelganger("singletsGate selected channels", fig = p) + + # test application of two gates one after the other + singletsGate1 <- singletsGate(OMIP021UTSamples[[1]], nmad = 3) + singletsGate2 <- singletsGate(OMIP021UTSamples[[1]], + channel1 = "SSC-A", + channel2 = "SSC-H", + filterId = "Singlets2" + ) + + singletCombinedGate <- singletsGate1 & singletsGate2 + + selectedSinglets <- flowCore::filter( + OMIP021UTSamples[[1]], + singletCombinedGate + ) + + ff_l <- flowCore::Subset(OMIP021UTSamples[[1]], selectedSinglets) + + p1 <- ggplotFilterEvents( + ffPre = OMIP021UTSamples[[1]], + ffPost = ff_l, + seed = 1, + xChannel = "FSC-A", xLinearRange = linRange, + yChannel = "FSC-H", yLinearRange = linRange + ) + + p2 <- ggplotFilterEvents( + ffPre = OMIP021UTSamples[[1]], + ffPost = ff_l, + seed = 1, + xChannel = "SSC-A", xLinearRange = linRange, + yChannel = "SSC-H", yLinearRange = linRange + ) + + + vdiffr::expect_doppelganger("singletsGates one after the other - fig1", + fig = p1 + ) + vdiffr::expect_doppelganger("singletsGates one after the other - fig2", + fig = p2 + ) +}) diff --git a/tests/testthat/test-ggplots.R b/tests/testthat/test-ggplots.R index f7812a2..c80a63d 100644 --- a/tests/testthat/test-ggplots.R +++ b/tests/testthat/test-ggplots.R @@ -15,410 +15,410 @@ -# # obtain OMIP021UTSamples, light-weight version used specifically for these -# # unit tests -# path <- system.file("scripts", -# package = "CytoPipeline" -# ) -# -# source(file.path(path,"MakeOMIP021UTSamples.R")) -# -# # TO DO: reactivate tests when ggplot version is fixed -# # error message (issue with vdiffr::expect_doppelganger()) -# # Backtrace: -# # 1. └─vdiffr::expect_doppelganger(...) at test-ggplots.R:58:4 -# # 2. └─vdiffr (local) writer(fig, testcase, title) -# # 3. └─vdiffr:::svglite(file) -# # 4. └─vdiffr:::svglite_(...) -# # ── Error ('test-ggplots.R:175'): ggplotEvents with 2D works ──────────────────── -# # Error in `svglite_(filename, bg, width, height, pointsize, standalone, -# # always_valid)`: Graphics API version mismatch -# -# # test_that("ggplotFlowRate works", { -# # p <- ggplotFlowRate(OMIP021UTSamples[[1]]) -# # vdiffr::expect_doppelganger("ggplotFlowRate single", fig = p) -# # -# # p <- ggplotFlowRate(OMIP021UTSamples) -# # vdiffr::expect_doppelganger("ggplotFlowRate double", fig = p) -# # -# # p <- ggplotFlowRate(OMIP021UTSamples[[1]], title = "Test Flow Rate plot") -# # vdiffr::expect_doppelganger("ggplotFlowRate single with title", fig = p) -# # -# # p <- ggplotFlowRate(OMIP021UTSamples[[1]], timeUnit = 50) -# # vdiffr::expect_doppelganger( -# # "ggplotFlowRate single with time unit", -# # fig = p -# # ) -# # }) -# # -# # -# # -# test_that("ggplotEvents with 1D works", { -# expect_error(ggplotEvents(OMIP021UTSamples[[1]], -# xChannel = "450/50Violet-A", -# xScale = "bi-exponential" -# ), -# regexp = "should be one of" -# ) -# -# p <- ggplotEvents(OMIP021UTSamples[[1]], -# xChannel = "FSC-A", -# xScale = "linear" -# ) -# #vdiffr::expect_doppelganger("ggplotEvents 1D linear single", fig = p) -# -# p <- ggplotEvents(OMIP021UTSamples[[1]], -# xChannel = "FSC-A", -# xScale = "linear", -# xLinearRange = c(0, 250000) -# ) -# # vdiffr::expect_doppelganger( -# # "ggplotEvents 1D linear explicit range - single", -# # fig = p -# # ) -# -# p <- ggplotEvents(OMIP021UTSamples, xChannel = "FSC-A", xScale = "linear") -# #vdiffr::expect_doppelganger("ggplotEvents 1D linear double", fig = p) -# -# p <- ggplotEvents(OMIP021UTSamples[[1]], -# xChannel = "450/50Violet-A", -# xScale = "logicle" -# ) -# #vdiffr::expect_doppelganger("ggplotEvents 1D logicle single", fig = p) -# -# p <- ggplotEvents(OMIP021UTSamples[[1]], -# xChannel = "450/50Violet-A", -# xScale = "logicle", xLogicleParams = list( -# a = 1, -# w = 2, -# m = 7, -# t = 270000 -# ) -# ) -# -# # vdiffr::expect_doppelganger( -# # "ggplotEvents 1D logicle with explicit params - single", -# # fig = p -# # ) -# -# p <- ggplotEvents(OMIP021UTSamples[[2]], -# xChannel = "450/50Violet-A", -# xScale = "logicle", nDisplayCells = 500, seed = 1 -# ) -# -# #vdiffr::expect_doppelganger("ggplotEvents 1D sub-sampling", fig = p) -# -# p <- ggplotEvents(OMIP021UTSamples[[2]], -# xChannel = "450/50Violet-A", -# xScale = "logicle", alpha = 0.5, fill = "red" -# ) -# -# #vdiffr::expect_doppelganger("ggplotEvents 1D fill and color", fig = p) -# -# compensationMatrix <- flowCore::spillover(OMIP021UTSamples[[1]])$SPILL -# -# ffC <- runCompensation(OMIP021UTSamples[[1]], -# spillover = compensationMatrix, -# updateChannelNames = FALSE -# ) -# -# transList <- flowCore::estimateLogicle( -# ffC, -# colnames(compensationMatrix) -# ) -# -# transList <- -# c( -# transList, -# flowCore::transformList( -# "FSC-A", -# flowCore::linearTransform(a = 0.00001) -# ) -# ) -# -# p <- ggplotEvents(OMIP021UTSamples[[1]], -# xChannel = "450/50Violet-A", -# xScale = "linear", transList = transList, -# runTransforms = FALSE -# ) -# # vdiffr::expect_doppelganger( -# # "ggplotEvents 1D transformList logicle not run", -# # fig = p -# # ) -# -# p <- ggplotEvents(OMIP021UTSamples[[1]], -# xChannel = "450/50Violet-A", -# xScale = "linear", transList = transList, -# runTransforms = TRUE -# ) -# # vdiffr::expect_doppelganger("ggplotEvents 1D transformList logicle run", -# # fig = p -# # ) -# -# p <- ggplotEvents(OMIP021UTSamples[[1]], -# xChannel = "FSC-A", -# xScale = "logicle", transList = transList, -# runTransforms = FALSE -# ) -# # vdiffr::expect_doppelganger( -# # "ggplotEvents 1D transformList linear not run", -# # fig = p -# # ) -# -# p <- ggplotEvents(OMIP021UTSamples[[1]], -# xChannel = "FSC-A", -# xScale = "logicle", transList = transList, -# runTransforms = TRUE -# ) -# # vdiffr::expect_doppelganger("ggplotEvents 1D transformList linear run", -# # fig = p -# # ) -# }) -# -# test_that("ggplotEvents with 2D works", { -# p <- ggplotEvents(OMIP021UTSamples, -# xChannel = "FSC-A", -# xScale = "linear", -# yChannel = "SSC-A", -# yScale = "linear" -# ) -# vdiffr::expect_doppelganger("ggplotEvents 2D linear double", fig = p) -# -# p <- ggplotEvents(OMIP021UTSamples[[1]], -# xChannel = "FSC-A", -# xScale = "linear", -# yChannel = "610/20Violet-A", -# yScale = "logicle" -# ) -# vdiffr::expect_doppelganger("ggplotEvents 2D x linear y logicle", fig = p) -# -# p <- ggplotEvents(OMIP021UTSamples[[1]], -# xChannel = "450/50Violet-A", -# xScale = "logicle", -# yChannel = "SSC-A", -# yScale = "linear" -# ) -# vdiffr::expect_doppelganger("ggplotEvents 2D x logicle y linear", fig = p) -# -# p <- ggplotEvents(OMIP021UTSamples[[1]], -# xChannel = "TETaGC", -# xScale = "logicle", -# yChannel = "CD27", -# yScale = "logicle" -# ) -# vdiffr::expect_doppelganger( -# "ggplotEvents 2D x logicle y logicle by markers", -# fig = p -# ) -# -# p <- ggplotEvents(OMIP021UTSamples[[1]], -# xChannel = "TETaGC", -# xScale = "logicle", -# yChannel = "CD27", -# yScale = "logicle", -# bins = 128 -# ) -# vdiffr::expect_doppelganger( -# "ggplotEvents 2D x logicle y logicle bins", -# fig = p -# ) -# -# -# compensationMatrix <- flowCore::spillover(OMIP021UTSamples[[1]])$SPILL -# -# ffC <- runCompensation(OMIP021UTSamples[[1]], -# spillover = compensationMatrix, -# updateChannelNames = FALSE -# ) -# -# transList <- flowCore::estimateLogicle( -# ffC, -# colnames(compensationMatrix) -# ) -# -# transList <- -# c( -# transList, -# flowCore::transformList( -# "FSC-A", -# flowCore::linearTransform(a = 0.00001) -# ) -# ) -# -# p <- ggplotEvents(OMIP021UTSamples[[1]], -# xChannel = "TETaGC", -# xScale = "logicle", -# yChannel = "CD27", -# yScale = "logicle", -# transList = transList, -# runTransforms = FALSE -# ) -# vdiffr::expect_doppelganger( -# "ggplotEvents 2D x logicle y logicle transList not run", -# fig = p -# ) -# p <- ggplotEvents(OMIP021UTSamples[[1]], -# xChannel = "TETaGC", -# xScale = "logicle", -# yChannel = "CD27", -# yScale = "logicle", -# transList = transList, -# runTransforms = TRUE -# ) -# vdiffr::expect_doppelganger( -# "ggplotEvents 2D x logicle y logicle transList run", -# fig = p -# ) -# }) -# -# -# test_that("ggplotFilterEvents works", { -# ffPre <- OMIP021UTSamples[[1]] -# -# LDMarker <- "L/D Aqua - Viability" -# -# LDChannel <- getChannelNamesFromMarkers(ffPre, markers = LDMarker) -# liveGateMatrix <- matrix( -# data = c( -# 50000, 50000, 100000, 200000, 200000, -# 100, 1000, 2000, 2000, 1 -# ), -# ncol = 2, -# dimnames = list( -# c(), -# c("FSC-A", LDChannel) -# ) -# ) -# -# liveGate <- flowCore::polygonGate( -# filterId = "Live", -# .gate = liveGateMatrix -# ) -# -# selectedLive <- flowCore::filter(ffPre, liveGate) -# ffL <- flowCore::Subset(ffPre, selectedLive) -# #ffL <- appendCellID(ffL, which(selectedLive@subSet)) -# -# p <- ggplotFilterEvents( -# ffPre = ffPre, -# ffPost = ffL, -# nDisplayCells = 1000, -# xChannel = "FSC-A", xScale = "linear", -# yChannel = LDMarker, yScale = "logicle" -# ) + -# ggtitle("Live gate filter") -# vdiffr::expect_doppelganger( -# "ggplotFilterEvents 2D - all points", -# fig = p -# ) -# -# p <- ggplotFilterEvents( -# ffPre = ffPre, -# ffPost = ffL, -# nDisplayCells = 1000, -# xChannel = "FSC-A", xScale = "linear", -# yChannel = LDMarker, yScale = "logicle", -# interactive = TRUE -# ) + -# ggtitle("Live gate filter") -# vdiffr::expect_doppelganger( -# "ggplotFilterEvents 2D - all points - interactive", -# fig = p -# ) -# -# p <- ggplotFilterEvents( -# ffPre = ffPre, -# ffPost = ffL, -# nDisplayCells = 500, -# seed = 1, -# xChannel = "FSC-A", xScale = "linear", -# yChannel = LDMarker, yScale = "logicle" -# ) + -# ggtitle("Live gate filter") -# vdiffr::expect_doppelganger( -# "ggplotFilterEvents 2D - 5000 points", -# fig = p -# ) -# -# p <- ggplotFilterEvents( -# ffPre = ffPre, -# ffPost = ffL, -# nDisplayCells = Inf, -# xChannel = "FSC-A", xScale = "linear", -# yChannel = LDMarker, yScale = "logicle" -# ) + -# ggtitle("Live gate filter") -# vdiffr::expect_doppelganger( -# "ggplotFilterEvents 2D - Infinite nb of points", -# fig = p -# ) -# -# p <- ggplotFilterEvents( -# size = 0.1, -# ffPre = ffPre, -# ffPost = ffL, -# seed = 1, -# xChannel = "FSC-A", xScale = "linear", -# yChannel = LDMarker, yScale = "logicle" -# ) + -# ggtitle("Live gate filter") -# vdiffr::expect_doppelganger( -# "ggplotFilterEvents 2D - small size", -# fig = p -# ) -# -# # calculate transformation list for the next steps -# compensationMatrix <- flowCore::spillover(OMIP021UTSamples[[1]])$SPILL -# -# ffC <- runCompensation(OMIP021UTSamples[[1]], -# spillover = compensationMatrix, -# updateChannelNames = FALSE -# ) -# -# transList <- flowCore::estimateLogicle( -# ffC, -# colnames(compensationMatrix) -# ) -# -# transList <- -# c( -# transList, -# flowCore::transformList( -# "FSC-A", -# flowCore::linearTransform(a = 0.00001) -# ) -# ) -# -# p <- ggplotFilterEvents( -# ffPre = ffPre, -# ffPost = ffL, -# seed = 1, -# xChannel = "FSC-A", -# yChannel = LDMarker, -# transList = transList, -# runTransforms = FALSE -# ) + -# ggtitle("Live gate filter") -# -# vdiffr::expect_doppelganger( -# "ggplotFilterEvents 2D - transList - not run", -# fig = p -# ) -# -# p <- ggplotFilterEvents( -# ffPre = ffPre, -# ffPost = ffL, -# seed = 1, -# xChannel = "FSC-A", -# yChannel = LDMarker, -# transList = transList, -# runTransforms = TRUE -# ) + -# ggtitle("Live gate filter") -# -# # temporary deactivation of one unit test to have the test passed on GHA +# obtain OMIP021UTSamples, light-weight version used specifically for these +# unit tests +path <- system.file("scripts", + package = "CytoPipeline" +) + +source(file.path(path,"MakeOMIP021UTSamples.R")) + +# TO DO: reactivate tests when ggplot version is fixed +# error message (issue with vdiffr::expect_doppelganger()) +# Backtrace: +# 1. └─vdiffr::expect_doppelganger(...) at test-ggplots.R:58:4 +# 2. └─vdiffr (local) writer(fig, testcase, title) +# 3. └─vdiffr:::svglite(file) +# 4. └─vdiffr:::svglite_(...) +# ── Error ('test-ggplots.R:175'): ggplotEvents with 2D works ──────────────────── +# Error in `svglite_(filename, bg, width, height, pointsize, standalone, +# always_valid)`: Graphics API version mismatch + +# test_that("ggplotFlowRate works", { +# p <- ggplotFlowRate(OMIP021UTSamples[[1]]) +# vdiffr::expect_doppelganger("ggplotFlowRate single", fig = p) +# +# p <- ggplotFlowRate(OMIP021UTSamples) +# vdiffr::expect_doppelganger("ggplotFlowRate double", fig = p) +# +# p <- ggplotFlowRate(OMIP021UTSamples[[1]], title = "Test Flow Rate plot") +# vdiffr::expect_doppelganger("ggplotFlowRate single with title", fig = p) +# +# p <- ggplotFlowRate(OMIP021UTSamples[[1]], timeUnit = 50) # vdiffr::expect_doppelganger( -# "ggplotFilterEvents 2D - transList - run", +# "ggplotFlowRate single with time unit", # fig = p # ) # }) +# +# +# +test_that("ggplotEvents with 1D works", { + expect_error(ggplotEvents(OMIP021UTSamples[[1]], + xChannel = "450/50Violet-A", + xScale = "bi-exponential" + ), + regexp = "should be one of" + ) + + p <- ggplotEvents(OMIP021UTSamples[[1]], + xChannel = "FSC-A", + xScale = "linear" + ) + #vdiffr::expect_doppelganger("ggplotEvents 1D linear single", fig = p) + + p <- ggplotEvents(OMIP021UTSamples[[1]], + xChannel = "FSC-A", + xScale = "linear", + xLinearRange = c(0, 250000) + ) + # vdiffr::expect_doppelganger( + # "ggplotEvents 1D linear explicit range - single", + # fig = p + # ) + + p <- ggplotEvents(OMIP021UTSamples, xChannel = "FSC-A", xScale = "linear") + #vdiffr::expect_doppelganger("ggplotEvents 1D linear double", fig = p) + + p <- ggplotEvents(OMIP021UTSamples[[1]], + xChannel = "450/50Violet-A", + xScale = "logicle" + ) + #vdiffr::expect_doppelganger("ggplotEvents 1D logicle single", fig = p) + + p <- ggplotEvents(OMIP021UTSamples[[1]], + xChannel = "450/50Violet-A", + xScale = "logicle", xLogicleParams = list( + a = 1, + w = 2, + m = 7, + t = 270000 + ) + ) + + # vdiffr::expect_doppelganger( + # "ggplotEvents 1D logicle with explicit params - single", + # fig = p + # ) + + p <- ggplotEvents(OMIP021UTSamples[[2]], + xChannel = "450/50Violet-A", + xScale = "logicle", nDisplayCells = 500, seed = 1 + ) + + #vdiffr::expect_doppelganger("ggplotEvents 1D sub-sampling", fig = p) + + p <- ggplotEvents(OMIP021UTSamples[[2]], + xChannel = "450/50Violet-A", + xScale = "logicle", alpha = 0.5, fill = "red" + ) + + #vdiffr::expect_doppelganger("ggplotEvents 1D fill and color", fig = p) + + compensationMatrix <- flowCore::spillover(OMIP021UTSamples[[1]])$SPILL + + ffC <- runCompensation(OMIP021UTSamples[[1]], + spillover = compensationMatrix, + updateChannelNames = FALSE + ) + + transList <- flowCore::estimateLogicle( + ffC, + colnames(compensationMatrix) + ) + + transList <- + c( + transList, + flowCore::transformList( + "FSC-A", + flowCore::linearTransform(a = 0.00001) + ) + ) + + p <- ggplotEvents(OMIP021UTSamples[[1]], + xChannel = "450/50Violet-A", + xScale = "linear", transList = transList, + runTransforms = FALSE + ) + # vdiffr::expect_doppelganger( + # "ggplotEvents 1D transformList logicle not run", + # fig = p + # ) + + p <- ggplotEvents(OMIP021UTSamples[[1]], + xChannel = "450/50Violet-A", + xScale = "linear", transList = transList, + runTransforms = TRUE + ) + # vdiffr::expect_doppelganger("ggplotEvents 1D transformList logicle run", + # fig = p + # ) + + p <- ggplotEvents(OMIP021UTSamples[[1]], + xChannel = "FSC-A", + xScale = "logicle", transList = transList, + runTransforms = FALSE + ) + # vdiffr::expect_doppelganger( + # "ggplotEvents 1D transformList linear not run", + # fig = p + # ) + + p <- ggplotEvents(OMIP021UTSamples[[1]], + xChannel = "FSC-A", + xScale = "logicle", transList = transList, + runTransforms = TRUE + ) + # vdiffr::expect_doppelganger("ggplotEvents 1D transformList linear run", + # fig = p + # ) +}) + +test_that("ggplotEvents with 2D works", { + p <- ggplotEvents(OMIP021UTSamples, + xChannel = "FSC-A", + xScale = "linear", + yChannel = "SSC-A", + yScale = "linear" + ) + vdiffr::expect_doppelganger("ggplotEvents 2D linear double", fig = p) + + p <- ggplotEvents(OMIP021UTSamples[[1]], + xChannel = "FSC-A", + xScale = "linear", + yChannel = "610/20Violet-A", + yScale = "logicle" + ) + vdiffr::expect_doppelganger("ggplotEvents 2D x linear y logicle", fig = p) + + p <- ggplotEvents(OMIP021UTSamples[[1]], + xChannel = "450/50Violet-A", + xScale = "logicle", + yChannel = "SSC-A", + yScale = "linear" + ) + vdiffr::expect_doppelganger("ggplotEvents 2D x logicle y linear", fig = p) + + p <- ggplotEvents(OMIP021UTSamples[[1]], + xChannel = "TETaGC", + xScale = "logicle", + yChannel = "CD27", + yScale = "logicle" + ) + vdiffr::expect_doppelganger( + "ggplotEvents 2D x logicle y logicle by markers", + fig = p + ) + + p <- ggplotEvents(OMIP021UTSamples[[1]], + xChannel = "TETaGC", + xScale = "logicle", + yChannel = "CD27", + yScale = "logicle", + bins = 128 + ) + vdiffr::expect_doppelganger( + "ggplotEvents 2D x logicle y logicle bins", + fig = p + ) + + + compensationMatrix <- flowCore::spillover(OMIP021UTSamples[[1]])$SPILL + + ffC <- runCompensation(OMIP021UTSamples[[1]], + spillover = compensationMatrix, + updateChannelNames = FALSE + ) + + transList <- flowCore::estimateLogicle( + ffC, + colnames(compensationMatrix) + ) + + transList <- + c( + transList, + flowCore::transformList( + "FSC-A", + flowCore::linearTransform(a = 0.00001) + ) + ) + + p <- ggplotEvents(OMIP021UTSamples[[1]], + xChannel = "TETaGC", + xScale = "logicle", + yChannel = "CD27", + yScale = "logicle", + transList = transList, + runTransforms = FALSE + ) + vdiffr::expect_doppelganger( + "ggplotEvents 2D x logicle y logicle transList not run", + fig = p + ) + p <- ggplotEvents(OMIP021UTSamples[[1]], + xChannel = "TETaGC", + xScale = "logicle", + yChannel = "CD27", + yScale = "logicle", + transList = transList, + runTransforms = TRUE + ) + vdiffr::expect_doppelganger( + "ggplotEvents 2D x logicle y logicle transList run", + fig = p + ) +}) + + +test_that("ggplotFilterEvents works", { + ffPre <- OMIP021UTSamples[[1]] + + LDMarker <- "L/D Aqua - Viability" + + LDChannel <- getChannelNamesFromMarkers(ffPre, markers = LDMarker) + liveGateMatrix <- matrix( + data = c( + 50000, 50000, 100000, 200000, 200000, + 100, 1000, 2000, 2000, 1 + ), + ncol = 2, + dimnames = list( + c(), + c("FSC-A", LDChannel) + ) + ) + + liveGate <- flowCore::polygonGate( + filterId = "Live", + .gate = liveGateMatrix + ) + + selectedLive <- flowCore::filter(ffPre, liveGate) + ffL <- flowCore::Subset(ffPre, selectedLive) + #ffL <- appendCellID(ffL, which(selectedLive@subSet)) + + p <- ggplotFilterEvents( + ffPre = ffPre, + ffPost = ffL, + nDisplayCells = 1000, + xChannel = "FSC-A", xScale = "linear", + yChannel = LDMarker, yScale = "logicle" + ) + + ggtitle("Live gate filter") + vdiffr::expect_doppelganger( + "ggplotFilterEvents 2D - all points", + fig = p + ) + + p <- ggplotFilterEvents( + ffPre = ffPre, + ffPost = ffL, + nDisplayCells = 1000, + xChannel = "FSC-A", xScale = "linear", + yChannel = LDMarker, yScale = "logicle", + interactive = TRUE + ) + + ggtitle("Live gate filter") + vdiffr::expect_doppelganger( + "ggplotFilterEvents 2D - all points - interactive", + fig = p + ) + + p <- ggplotFilterEvents( + ffPre = ffPre, + ffPost = ffL, + nDisplayCells = 500, + seed = 1, + xChannel = "FSC-A", xScale = "linear", + yChannel = LDMarker, yScale = "logicle" + ) + + ggtitle("Live gate filter") + vdiffr::expect_doppelganger( + "ggplotFilterEvents 2D - 5000 points", + fig = p + ) + + p <- ggplotFilterEvents( + ffPre = ffPre, + ffPost = ffL, + nDisplayCells = Inf, + xChannel = "FSC-A", xScale = "linear", + yChannel = LDMarker, yScale = "logicle" + ) + + ggtitle("Live gate filter") + vdiffr::expect_doppelganger( + "ggplotFilterEvents 2D - Infinite nb of points", + fig = p + ) + + p <- ggplotFilterEvents( + size = 0.1, + ffPre = ffPre, + ffPost = ffL, + seed = 1, + xChannel = "FSC-A", xScale = "linear", + yChannel = LDMarker, yScale = "logicle" + ) + + ggtitle("Live gate filter") + vdiffr::expect_doppelganger( + "ggplotFilterEvents 2D - small size", + fig = p + ) + + # calculate transformation list for the next steps + compensationMatrix <- flowCore::spillover(OMIP021UTSamples[[1]])$SPILL + + ffC <- runCompensation(OMIP021UTSamples[[1]], + spillover = compensationMatrix, + updateChannelNames = FALSE + ) + + transList <- flowCore::estimateLogicle( + ffC, + colnames(compensationMatrix) + ) + + transList <- + c( + transList, + flowCore::transformList( + "FSC-A", + flowCore::linearTransform(a = 0.00001) + ) + ) + + p <- ggplotFilterEvents( + ffPre = ffPre, + ffPost = ffL, + seed = 1, + xChannel = "FSC-A", + yChannel = LDMarker, + transList = transList, + runTransforms = FALSE + ) + + ggtitle("Live gate filter") + + vdiffr::expect_doppelganger( + "ggplotFilterEvents 2D - transList - not run", + fig = p + ) + + p <- ggplotFilterEvents( + ffPre = ffPre, + ffPost = ffL, + seed = 1, + xChannel = "FSC-A", + yChannel = LDMarker, + transList = transList, + runTransforms = TRUE + ) + + ggtitle("Live gate filter") + + # temporary deactivation of one unit test to have the test passed on GHA + vdiffr::expect_doppelganger( + "ggplotFilterEvents 2D - transList - run", + fig = p + ) +}) diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 6f4aca3..764ae93 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -13,510 +13,510 @@ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details (). -# data("OMIP021Samples") -# -# test_that("areSignalCols works", { -# ff <- OMIP021Samples[[1]] -# -# expectedRes <- c(rep(TRUE, times = 20), FALSE, FALSE) -# -# res <- areSignalCols(ff) -# expect_equal(res, expectedRes, ignore_attr = TRUE) -# -# # same with flowSet -# res <- areSignalCols(OMIP021Samples) -# expect_equal(res, expectedRes, ignore_attr = TRUE) -# }) -# -# test_that("areFluoCols works", { -# ff <- OMIP021Samples[[1]] -# -# expectedRes <- c( -# rep(FALSE, times = 4), -# rep(TRUE, times = 16), -# FALSE, FALSE -# ) -# -# res <- areFluoCols(ff) -# expect_equal(res, expectedRes, ignore_attr = TRUE) -# -# # same with flowSet -# res <- areFluoCols(OMIP021Samples) -# expect_equal(res, expectedRes, ignore_attr = TRUE) -# }) -# -# -# -# test_that("subsample works", { -# ff <- OMIP021Samples[[1]] -# -# nEvents <- 50 -# seed <- 0 -# ffSub <- subsample(ff, nEvents, seed = seed) -# expect_equal(flowCore::nrow(ffSub), nEvents) -# # withr::with_seed( -# # seed, -# # { -# # tgtLines <- sample(seq_len(flowCore::nrow(ff)), -# # size = nEvents, -# # replace = FALSE) -# # tgtIDs <- flowCore::exprs(ff)[tgtLines,"Original_ID"] -# # expect_true(all.equal(tgtIDs, -# # flowCore::exprs(ffSub)[,"Original_ID"])) -# # } -# # ) -# -# # same but reset the original IDs -# ffSub <- subsample(ff, nEvents, seed = seed, keepOriginalCellIDs = FALSE) -# expect_equal(flowCore::nrow(ffSub), nEvents) -# tgtIDs <- 1:nEvents -# expect_true(all.equal(tgtIDs, flowCore::exprs(ffSub)[,"Original_ID"])) -# -# -# # subsample with more samples than original nrow (5000) -# ffSub <- subsample(ff, 10000) -# -# nOriginal <- flowCore::nrow(ff) -# expect_equal(flowCore::nrow(ffSub), nOriginal) -# }) -# -# -# test_that(".addCompensation2FluoChannelNames works", { -# ff <- OMIP021Samples[[1]] -# -# ff2 <- .addCompensation2FluoChannelNames(ff) -# -# expect_equal( -# flowCore::colnames(ff2), -# c( -# "FSC-A", "FSC-H", "SSC-A", "SSC-H", -# "Comp-450/50Violet-A", "Comp-525/50Violet-A", "Comp-540/30Violet-A", -# "Comp-585/15Violet-A", "Comp-610/20Violet-A", "Comp-670/30Violet-A", -# "Comp-670/14Red-A", "Comp-730//45Red-A", "Comp-780/60Red-A", -# "Comp-530/30Blue-A", "Comp-710/50Blue-A", "Comp-582/15Yellow-A", -# "Comp-610/20Yellow-A", "Comp-670/30Yellow-A", "Comp-710/50Yellow-A", -# "Comp-780/60Yellow-A", "Time", "Original_ID" -# ) -# ) -# -# expect_error(.addCompensation2FluoChannelNames(OMIP021Samples), -# regexp = "type not recognized" -# ) -# }) -# -# test_that("runCompensation works", { -# ff <- OMIP021Samples[[1]] -# -# compMatrix <- flowCore::spillover(ff)$SPILL -# ff1 <- flowCore::compensate(ff, spillover = compMatrix) -# ff2 <- runCompensation(ff, -# spillover = compMatrix, -# updateChannelNames = FALSE -# ) -# -# expect_equal(ff2, ff1) -# -# ff2 <- runCompensation(ff, spillover = compMatrix) -# # the following avoids comparing name attributes -# expect_true(all(flowCore::exprs(ff2) == flowCore::exprs(ff1))) -# expect_equal( -# flowCore::colnames(ff2), -# c( -# "FSC-A", "FSC-H", "SSC-A", "SSC-H", -# "Comp-450/50Violet-A", "Comp-525/50Violet-A", "Comp-540/30Violet-A", -# "Comp-585/15Violet-A", "Comp-610/20Violet-A", "Comp-670/30Violet-A", -# "Comp-670/14Red-A", "Comp-730//45Red-A", "Comp-780/60Red-A", -# "Comp-530/30Blue-A", "Comp-710/50Blue-A", "Comp-582/15Yellow-A", -# "Comp-610/20Yellow-A", "Comp-670/30Yellow-A", "Comp-710/50Yellow-A", -# "Comp-780/60Yellow-A", "Time", "Original_ID" -# ) -# ) -# -# fs <- runCompensation(OMIP021Samples, -# spillover = compMatrix -# ) -# ff3 <- flowCore::compensate(OMIP021Samples[[1]], -# spillover = compMatrix -# ) -# # the following avoids comparing name attributes -# expect_true(all(flowCore::exprs(fs[[1]]) == flowCore::exprs(ff3))) -# expect_equal( -# flowCore::colnames(fs), -# c( -# "FSC-A", "FSC-H", "SSC-A", "SSC-H", -# "Comp-450/50Violet-A", "Comp-525/50Violet-A", "Comp-540/30Violet-A", -# "Comp-585/15Violet-A", "Comp-610/20Violet-A", "Comp-670/30Violet-A", -# "Comp-670/14Red-A", "Comp-730//45Red-A", "Comp-780/60Red-A", -# "Comp-530/30Blue-A", "Comp-710/50Blue-A", "Comp-582/15Yellow-A", -# "Comp-610/20Yellow-A", "Comp-670/30Yellow-A", "Comp-710/50Yellow-A", -# "Comp-780/60Yellow-A", "Time", "Original_ID" -# ) -# ) -# }) -# -# test_that("aggregateAndSample works", { -# nCells <- 100 -# agg <- aggregateAndSample( -# fs = OMIP021Samples, -# nTotalEvents = nCells, -# seed = 1 -# ) -# -# expect_equal(nrow(flowCore::exprs(agg)), nCells) -# -# ind1 <- which(flowCore::exprs(agg)[, "File"] == 1) -# expect_equal(nrow(flowCore::exprs(agg)[ind1, ]), nCells / 2) -# ind2 <- which(flowCore::exprs(agg)[, "File"] == 2) -# expect_equal(nrow(flowCore::exprs(agg)[ind2, ]), nCells / 2) -# }) -# -# test_that("aggregateAndSample still works with flow frame", { -# nCells <- 100 -# agg <- aggregateAndSample(fs = OMIP021Samples[[1]], -# nTotalEvents = nCells, -# seed = 1) -# expect_equal(nrow(flowCore::exprs(agg)), nCells) -# }) -# -# test_that("getTransfoParams works", { -# # hybrid transformation list : -# # - two channels are logicle-ly transformed with automatic param estimates -# # - one channel has explicit logicle transfo with default parameters -# # - one channel has linear transformation -# # - other channels have no transformation -# translist <- flowCore::estimateLogicle( -# OMIP021Samples[[1]], -# c("450/50Violet-A", "525/50Violet-A") -# ) -# translist <- c( -# translist, -# flowCore::transformList( -# "FSC-A", -# flowCore::linearTransform( -# a = 0.1, -# b = 0 -# ) -# ), -# flowCore::transformList( -# "540/30Violet-A", -# flowCore::logicleTransform() -# ) -# ) -# -# ret <- getTransfoParams(translist, channel = "SSC-A") -# expect_equal(is.null(ret), TRUE) -# -# ret <- getTransfoParams(translist, channel = "FSC-A") -# expect_equal(ret$type, "linear") -# expect_equal(ret$paramsList$a, 0.1) -# expect_equal(ret$paramsList$b, 0.) -# -# ret <- getTransfoParams(translist, channel = "525/50Violet-A") -# expect_equal(ret$type, "logicle") -# expect_equal(ret$paramsList$a, 0.) -# myW <- 0.26281575 -# expect_equal(ret$paramsList$w, myW) -# expect_equal(ret$paramsList$m, 4.5) -# expect_equal(ret$paramsList$t, 262143) -# -# ret <- getTransfoParams(translist, channel = "540/30Violet-A") -# expect_equal(ret$type, "logicle") -# expect_equal(ret$paramsList$a, 0.) -# expect_equal(ret$paramsList$w, 0.5) -# expect_equal(ret$paramsList$m, 4.5) -# expect_equal(ret$paramsList$t, 262144) -# -# tf <- flowCore::linearTransform(a = 1.1, b = 0.2) -# otherTransList <- flowCore::transformList( -# from = "FSC-A", -# tfun = tf -# ) -# ret <- getTransfoParams(otherTransList, -# channel = "FSC-A" -# ) -# expect_equal(ret$type, "linear") -# expect_equal(ret$paramsList$a, 1.1) -# expect_equal(ret$paramsList$b, 0.2) -# }) -# -# test_that("computeScatterChannelsLinearScale works", { -# ff <- OMIP021Samples[[1]] -# refMarker <- "APCCy7 - CD4" -# refChannel <- "780/60Red-A" -# -# targetFSCA <- list() -# targetFSCA$type <- "linear" -# targetFSCA$paramsList <- list() -# targetFSCA$paramsList$a <- 1.3504601e-05 -# targetFSCA$paramsList$b <- 0.41438898 -# -# targetSSCA <- list() -# targetSSCA$type <- "linear" -# targetSSCA$paramsList <- list() -# targetSSCA$paramsList$a <- 1.07290554e-05 -# targetSSCA$paramsList$b <- 0.38032844 -# -# transList <- flowCore::estimateLogicle(ff, -# channels = refChannel -# ) -# -# # base case, reference channel exists, transList pre-filled with logicle -# # transfo for ref channel -# retTransList <- -# computeScatterChannelsLinearScale(ff, -# transList = transList, -# referenceChannel = refMarker, -# silent = TRUE -# ) -# -# retFSCA <- getTransfoParams(retTransList, channel = "FSC-A") -# expect_equal(retFSCA, targetFSCA) -# -# retFSCH <- getTransfoParams(retTransList, channel = "FSC-H") -# expect_equal(retFSCH, targetFSCA) -# -# retSSCA <- getTransfoParams(retTransList, channel = "SSC-A") -# expect_equal(retSSCA, targetSSCA) -# -# retSSCH <- getTransfoParams(retTransList, channel = "SSC-H") -# expect_equal(retSSCH, targetSSCA) -# -# # test with a reference channel that does not exist -# expect_error(computeScatterChannelsLinearScale( -# ff, -# transList = transList, -# referenceChannel = "Yipee", -# silent = TRUE -# ), regexp = "can't find") -# -# # test with a reference channel that is not a fluo channel -# expect_error(computeScatterChannelsLinearScale( -# ff, -# transList = transList, -# referenceChannel = "SSC-A", -# silent = TRUE -# ), regexp = "should be a fluorochrome channel") -# -# # test with a NULL transList -# -# targetFSCA$paramsList$a <- 0.047610702 -# targetFSCA$paramsList$b <- -491.62068 -# targetSSCA$paramsList$a <- 0.037825469 -# targetSSCA$paramsList$b <- -611.70173 -# retTransList <- -# computeScatterChannelsLinearScale(ff, -# transList = NULL, -# referenceChannel = refMarker, -# silent = TRUE -# ) -# -# retFSCA <- getTransfoParams(retTransList, channel = "FSC-A") -# expect_equal(retFSCA, targetFSCA) -# -# retFSCH <- getTransfoParams(retTransList, channel = "FSC-H") -# expect_equal(retFSCH, targetFSCA) -# -# retSSCA <- getTransfoParams(retTransList, channel = "SSC-A") -# expect_equal(retSSCA, targetSSCA) -# -# retSSCH <- getTransfoParams(retTransList, channel = "SSC-H") -# expect_equal(retSSCH, targetSSCA) -# -# # test with a transList that does not contain the reference channel, -# # but well an already existing SSC-A transformation -# linTrans <- flowCore::linearTransform() -# stupidTransList <- flowCore::transformList( -# from = "SSC-A", -# tfun = linTrans -# ) -# retTransList <- -# computeScatterChannelsLinearScale(ff, -# transList = stupidTransList, -# referenceChannel = refMarker, -# silent = TRUE -# ) -# -# -# retFSCA <- getTransfoParams(retTransList, channel = "FSC-A") -# expect_equal(retFSCA, targetFSCA) -# -# retFSCH <- getTransfoParams(retTransList, channel = "FSC-H") -# expect_equal(retFSCH, targetFSCA) -# -# retSSCA <- getTransfoParams(retTransList, channel = "SSC-A") -# expect_equal(retSSCA, targetSSCA) -# -# retSSCH <- getTransfoParams(retTransList, channel = "SSC-H") -# expect_equal(retSSCH, targetSSCA) -# }) -# -# test_that("findTimeChannel works", { -# # with flow set -# ret <- findTimeChannel(OMIP021Samples) -# expect_equal(ret, "Time") -# -# # with flow frame -# ret2 <- findTimeChannel(OMIP021Samples[[1]]) -# expect_equal(ret2, "Time") -# -# # test exclude channels parameter -# ret3 <- findTimeChannel(OMIP021Samples[[1]], -# excludeChannels = "Time" -# ) -# expect_null(ret3) -# }) -# -# test_that("getChannelNamesFromMarkers works", { -# # with existing markers -# ret <- getChannelNamesFromMarkers( -# OMIP021Samples[[1]], -# c( -# "FSC-A", -# "L/D Aqua - Viability", -# "FITC - gdTCR", -# "PECy5 - CD28" -# ) -# ) -# -# expected <- c("FSC-A", "525/50Violet-A", "530/30Blue-A", "670/30Yellow-A") -# expect_equal(ret, expected) -# -# # with boolean vector -# indices <- c(1, 6, 14, 18) -# boolInput <- rep(FALSE, 21) -# boolInput[indices] <- TRUE -# ret <- getChannelNamesFromMarkers( -# OMIP021Samples[[1]], -# boolInput -# ) -# -# expect_equal(ret, expected) -# -# -# # with indices vector -# ret <- getChannelNamesFromMarkers( -# OMIP021Samples[[1]], -# indices -# ) -# expect_equal(ret, expected) -# -# # missing channel -# missingInput <- c("CD4") -# -# expect_error(getChannelNamesFromMarkers( -# OMIP021Samples[[1]], -# missingInput -# ), -# regexp = "could not be found" -# ) -# }) -# -# test_that("updateMarkerName works", { -# retFF <- updateMarkerName(OMIP021Samples[[1]], -# channel = "FSC-A", -# newMarkerName = "Fwd Scatter-A") -# -# checkMkName <- getChannelNamesFromMarkers(retFF, markers = "Fwd Scatter-A") -# expect_equal(checkMkName, "FSC-A") -# expect_equal(flowCore::keyword(retFF, "$P1S")[["$P1S"]], "Fwd Scatter-A") -# -# # same with channel as index -# retFF <- updateMarkerName(OMIP021Samples[[1]], -# channel = 1, -# newMarkerName = "Fwd Scatter-A") -# checkMkName <- getChannelNamesFromMarkers(retFF, markers = "Fwd Scatter-A") -# expect_equal(checkMkName, "FSC-A") -# expect_equal(flowCore::keyword(retFF, "$P1S")[["$P1S"]], "Fwd Scatter-A") -# -# # now channel provided as marker (desc), not channel name -# retFF <- updateMarkerName(OMIP021Samples[[1]], -# channel = "BV785 - CD3", -# newMarkerName = "fancy CD3") -# checkMkName <- getChannelNamesFromMarkers(retFF, markers = "fancy CD3") -# expect_equal(checkMkName, "670/30Violet-A") -# expect_equal(flowCore::keyword(retFF, "$P10S")[["$P10S"]], "fancy CD3") -# -# -# -# expect_error(updateMarkerName(OMIP021Samples[[1]], channel = "FFF-A", -# newMarkerName = "whatever"), -# regexp = "channel not found") -# }) -# -# test_that("removeChannels works", { -# retFF <- removeChannels(OMIP021Samples[[1]], -# channels = c("FSC-A", "SSC-A")) -# nRemainingChannels <- length(flowCore::colnames(retFF)) -# expect_equal(nRemainingChannels, 20) -# -# expect_warning(removeChannels(OMIP021Samples[[1]], -# channels = c("FSC-A", "SSC-S")), -# regexp = "not found in flowFrame") -# -# -# }) -# -# test_that("getFCSFileName works", { -# fName <- getFCSFileName(OMIP021Samples[[1]]) -# expect_equal(fName, "Donor1.fcs") -# }) -# -# test_that("writeFlowFrame works", { -# outputDir <- base::tempdir() -# -# ff_c <- readRDS(test_path("fixtures", "ff_c.rds")) -# -# expect_error(writeFlowFrame(ff, dir = file.path(outputDir, "notThere")), -# regexp = "Provided directory does not exist") -# -# prefix <- "File_" -# suffix <- "_export" -# -# writeFlowFrame(ff_c, dir = outputDir, -# useFCSFileName = TRUE, -# prefix = prefix, -# suffix = suffix, -# format = "fcs") -# -# outputFile <- file.path(outputDir, -# paste0(prefix, "Donor1", suffix, ".fcs")) -# -# thisFF <- flowCore::read.FCS(outputFile, transform = FALSE) -# expect_true(all(round(flowCore::exprs(thisFF), 0) -# == round(flowCore::exprs(ff_c), 0))) -# -# writeFlowFrame(ff_c, dir = outputDir, -# useFCSFileName = FALSE, -# prefix = prefix, -# suffix = suffix, -# format = "csv") -# -# outputCSV <- file.path(outputDir, -# paste0(prefix, suffix, ".csv")) -# -# thisExpr <- read.csv(file = outputCSV) -# expect_true(all(round(thisExpr,4) == round(flowCore::exprs(ff_c), 4))) -# }) -# -# -# test_that("updateCompMatrixLabels works", { -# myMat <- diag(nrow = 3) -# names <- c("450/50Violet-A", "BV605 - CD161", -# "670/30Yellow-A :: BV785 - CD3") -# colnames(myMat) <- rownames(myMat) <- names -# -# myNewMat <- .updateCompMatrixLabels(myMat, OMIP021Samples[[1]]) -# -# newColNames <- colnames(myNewMat) -# newRowNames <- rownames(myNewMat) -# -# expectedNewNames <- c("450/50Violet-A", -# "540/30Violet-A", -# "670/30Yellow-A") -# -# expect_equal(newColNames, expectedNewNames) -# expect_equal(newRowNames, expectedNewNames) -# }) -# -# -# +data("OMIP021Samples") + +test_that("areSignalCols works", { + ff <- OMIP021Samples[[1]] + + expectedRes <- c(rep(TRUE, times = 20), FALSE, FALSE) + + res <- areSignalCols(ff) + expect_equal(res, expectedRes, ignore_attr = TRUE) + + # same with flowSet + res <- areSignalCols(OMIP021Samples) + expect_equal(res, expectedRes, ignore_attr = TRUE) +}) + +test_that("areFluoCols works", { + ff <- OMIP021Samples[[1]] + + expectedRes <- c( + rep(FALSE, times = 4), + rep(TRUE, times = 16), + FALSE, FALSE + ) + + res <- areFluoCols(ff) + expect_equal(res, expectedRes, ignore_attr = TRUE) + + # same with flowSet + res <- areFluoCols(OMIP021Samples) + expect_equal(res, expectedRes, ignore_attr = TRUE) +}) + + + +test_that("subsample works", { + ff <- OMIP021Samples[[1]] + + nEvents <- 50 + seed <- 0 + ffSub <- subsample(ff, nEvents, seed = seed) + expect_equal(flowCore::nrow(ffSub), nEvents) + # withr::with_seed( + # seed, + # { + # tgtLines <- sample(seq_len(flowCore::nrow(ff)), + # size = nEvents, + # replace = FALSE) + # tgtIDs <- flowCore::exprs(ff)[tgtLines,"Original_ID"] + # expect_true(all.equal(tgtIDs, + # flowCore::exprs(ffSub)[,"Original_ID"])) + # } + # ) + + # same but reset the original IDs + ffSub <- subsample(ff, nEvents, seed = seed, keepOriginalCellIDs = FALSE) + expect_equal(flowCore::nrow(ffSub), nEvents) + tgtIDs <- 1:nEvents + expect_true(all.equal(tgtIDs, flowCore::exprs(ffSub)[,"Original_ID"])) + + + # subsample with more samples than original nrow (5000) + ffSub <- subsample(ff, 10000) + + nOriginal <- flowCore::nrow(ff) + expect_equal(flowCore::nrow(ffSub), nOriginal) +}) + + +test_that(".addCompensation2FluoChannelNames works", { + ff <- OMIP021Samples[[1]] + + ff2 <- .addCompensation2FluoChannelNames(ff) + + expect_equal( + flowCore::colnames(ff2), + c( + "FSC-A", "FSC-H", "SSC-A", "SSC-H", + "Comp-450/50Violet-A", "Comp-525/50Violet-A", "Comp-540/30Violet-A", + "Comp-585/15Violet-A", "Comp-610/20Violet-A", "Comp-670/30Violet-A", + "Comp-670/14Red-A", "Comp-730//45Red-A", "Comp-780/60Red-A", + "Comp-530/30Blue-A", "Comp-710/50Blue-A", "Comp-582/15Yellow-A", + "Comp-610/20Yellow-A", "Comp-670/30Yellow-A", "Comp-710/50Yellow-A", + "Comp-780/60Yellow-A", "Time", "Original_ID" + ) + ) + + expect_error(.addCompensation2FluoChannelNames(OMIP021Samples), + regexp = "type not recognized" + ) +}) + +test_that("runCompensation works", { + ff <- OMIP021Samples[[1]] + + compMatrix <- flowCore::spillover(ff)$SPILL + ff1 <- flowCore::compensate(ff, spillover = compMatrix) + ff2 <- runCompensation(ff, + spillover = compMatrix, + updateChannelNames = FALSE + ) + + expect_equal(ff2, ff1) + + ff2 <- runCompensation(ff, spillover = compMatrix) + # the following avoids comparing name attributes + expect_true(all(flowCore::exprs(ff2) == flowCore::exprs(ff1))) + expect_equal( + flowCore::colnames(ff2), + c( + "FSC-A", "FSC-H", "SSC-A", "SSC-H", + "Comp-450/50Violet-A", "Comp-525/50Violet-A", "Comp-540/30Violet-A", + "Comp-585/15Violet-A", "Comp-610/20Violet-A", "Comp-670/30Violet-A", + "Comp-670/14Red-A", "Comp-730//45Red-A", "Comp-780/60Red-A", + "Comp-530/30Blue-A", "Comp-710/50Blue-A", "Comp-582/15Yellow-A", + "Comp-610/20Yellow-A", "Comp-670/30Yellow-A", "Comp-710/50Yellow-A", + "Comp-780/60Yellow-A", "Time", "Original_ID" + ) + ) + + fs <- runCompensation(OMIP021Samples, + spillover = compMatrix + ) + ff3 <- flowCore::compensate(OMIP021Samples[[1]], + spillover = compMatrix + ) + # the following avoids comparing name attributes + expect_true(all(flowCore::exprs(fs[[1]]) == flowCore::exprs(ff3))) + expect_equal( + flowCore::colnames(fs), + c( + "FSC-A", "FSC-H", "SSC-A", "SSC-H", + "Comp-450/50Violet-A", "Comp-525/50Violet-A", "Comp-540/30Violet-A", + "Comp-585/15Violet-A", "Comp-610/20Violet-A", "Comp-670/30Violet-A", + "Comp-670/14Red-A", "Comp-730//45Red-A", "Comp-780/60Red-A", + "Comp-530/30Blue-A", "Comp-710/50Blue-A", "Comp-582/15Yellow-A", + "Comp-610/20Yellow-A", "Comp-670/30Yellow-A", "Comp-710/50Yellow-A", + "Comp-780/60Yellow-A", "Time", "Original_ID" + ) + ) +}) + +test_that("aggregateAndSample works", { + nCells <- 100 + agg <- aggregateAndSample( + fs = OMIP021Samples, + nTotalEvents = nCells, + seed = 1 + ) + + expect_equal(nrow(flowCore::exprs(agg)), nCells) + + ind1 <- which(flowCore::exprs(agg)[, "File"] == 1) + expect_equal(nrow(flowCore::exprs(agg)[ind1, ]), nCells / 2) + ind2 <- which(flowCore::exprs(agg)[, "File"] == 2) + expect_equal(nrow(flowCore::exprs(agg)[ind2, ]), nCells / 2) +}) + +test_that("aggregateAndSample still works with flow frame", { + nCells <- 100 + agg <- aggregateAndSample(fs = OMIP021Samples[[1]], + nTotalEvents = nCells, + seed = 1) + expect_equal(nrow(flowCore::exprs(agg)), nCells) +}) + +test_that("getTransfoParams works", { + # hybrid transformation list : + # - two channels are logicle-ly transformed with automatic param estimates + # - one channel has explicit logicle transfo with default parameters + # - one channel has linear transformation + # - other channels have no transformation + translist <- flowCore::estimateLogicle( + OMIP021Samples[[1]], + c("450/50Violet-A", "525/50Violet-A") + ) + translist <- c( + translist, + flowCore::transformList( + "FSC-A", + flowCore::linearTransform( + a = 0.1, + b = 0 + ) + ), + flowCore::transformList( + "540/30Violet-A", + flowCore::logicleTransform() + ) + ) + + ret <- getTransfoParams(translist, channel = "SSC-A") + expect_equal(is.null(ret), TRUE) + + ret <- getTransfoParams(translist, channel = "FSC-A") + expect_equal(ret$type, "linear") + expect_equal(ret$paramsList$a, 0.1) + expect_equal(ret$paramsList$b, 0.) + + ret <- getTransfoParams(translist, channel = "525/50Violet-A") + expect_equal(ret$type, "logicle") + expect_equal(ret$paramsList$a, 0.) + myW <- 0.26281575 + expect_equal(ret$paramsList$w, myW) + expect_equal(ret$paramsList$m, 4.5) + expect_equal(ret$paramsList$t, 262143) + + ret <- getTransfoParams(translist, channel = "540/30Violet-A") + expect_equal(ret$type, "logicle") + expect_equal(ret$paramsList$a, 0.) + expect_equal(ret$paramsList$w, 0.5) + expect_equal(ret$paramsList$m, 4.5) + expect_equal(ret$paramsList$t, 262144) + + tf <- flowCore::linearTransform(a = 1.1, b = 0.2) + otherTransList <- flowCore::transformList( + from = "FSC-A", + tfun = tf + ) + ret <- getTransfoParams(otherTransList, + channel = "FSC-A" + ) + expect_equal(ret$type, "linear") + expect_equal(ret$paramsList$a, 1.1) + expect_equal(ret$paramsList$b, 0.2) +}) + +test_that("computeScatterChannelsLinearScale works", { + ff <- OMIP021Samples[[1]] + refMarker <- "APCCy7 - CD4" + refChannel <- "780/60Red-A" + + targetFSCA <- list() + targetFSCA$type <- "linear" + targetFSCA$paramsList <- list() + targetFSCA$paramsList$a <- 1.3504601e-05 + targetFSCA$paramsList$b <- 0.41438898 + + targetSSCA <- list() + targetSSCA$type <- "linear" + targetSSCA$paramsList <- list() + targetSSCA$paramsList$a <- 1.07290554e-05 + targetSSCA$paramsList$b <- 0.38032844 + + transList <- flowCore::estimateLogicle(ff, + channels = refChannel + ) + + # base case, reference channel exists, transList pre-filled with logicle + # transfo for ref channel + retTransList <- + computeScatterChannelsLinearScale(ff, + transList = transList, + referenceChannel = refMarker, + silent = TRUE + ) + + retFSCA <- getTransfoParams(retTransList, channel = "FSC-A") + expect_equal(retFSCA, targetFSCA) + + retFSCH <- getTransfoParams(retTransList, channel = "FSC-H") + expect_equal(retFSCH, targetFSCA) + + retSSCA <- getTransfoParams(retTransList, channel = "SSC-A") + expect_equal(retSSCA, targetSSCA) + + retSSCH <- getTransfoParams(retTransList, channel = "SSC-H") + expect_equal(retSSCH, targetSSCA) + + # test with a reference channel that does not exist + expect_error(computeScatterChannelsLinearScale( + ff, + transList = transList, + referenceChannel = "Yipee", + silent = TRUE + ), regexp = "can't find") + + # test with a reference channel that is not a fluo channel + expect_error(computeScatterChannelsLinearScale( + ff, + transList = transList, + referenceChannel = "SSC-A", + silent = TRUE + ), regexp = "should be a fluorochrome channel") + + # test with a NULL transList + + targetFSCA$paramsList$a <- 0.047610702 + targetFSCA$paramsList$b <- -491.62068 + targetSSCA$paramsList$a <- 0.037825469 + targetSSCA$paramsList$b <- -611.70173 + retTransList <- + computeScatterChannelsLinearScale(ff, + transList = NULL, + referenceChannel = refMarker, + silent = TRUE + ) + + retFSCA <- getTransfoParams(retTransList, channel = "FSC-A") + expect_equal(retFSCA, targetFSCA) + + retFSCH <- getTransfoParams(retTransList, channel = "FSC-H") + expect_equal(retFSCH, targetFSCA) + + retSSCA <- getTransfoParams(retTransList, channel = "SSC-A") + expect_equal(retSSCA, targetSSCA) + + retSSCH <- getTransfoParams(retTransList, channel = "SSC-H") + expect_equal(retSSCH, targetSSCA) + + # test with a transList that does not contain the reference channel, + # but well an already existing SSC-A transformation + linTrans <- flowCore::linearTransform() + stupidTransList <- flowCore::transformList( + from = "SSC-A", + tfun = linTrans + ) + retTransList <- + computeScatterChannelsLinearScale(ff, + transList = stupidTransList, + referenceChannel = refMarker, + silent = TRUE + ) + + + retFSCA <- getTransfoParams(retTransList, channel = "FSC-A") + expect_equal(retFSCA, targetFSCA) + + retFSCH <- getTransfoParams(retTransList, channel = "FSC-H") + expect_equal(retFSCH, targetFSCA) + + retSSCA <- getTransfoParams(retTransList, channel = "SSC-A") + expect_equal(retSSCA, targetSSCA) + + retSSCH <- getTransfoParams(retTransList, channel = "SSC-H") + expect_equal(retSSCH, targetSSCA) +}) + +test_that("findTimeChannel works", { + # with flow set + ret <- findTimeChannel(OMIP021Samples) + expect_equal(ret, "Time") + + # with flow frame + ret2 <- findTimeChannel(OMIP021Samples[[1]]) + expect_equal(ret2, "Time") + + # test exclude channels parameter + ret3 <- findTimeChannel(OMIP021Samples[[1]], + excludeChannels = "Time" + ) + expect_null(ret3) +}) + +test_that("getChannelNamesFromMarkers works", { + # with existing markers + ret <- getChannelNamesFromMarkers( + OMIP021Samples[[1]], + c( + "FSC-A", + "L/D Aqua - Viability", + "FITC - gdTCR", + "PECy5 - CD28" + ) + ) + + expected <- c("FSC-A", "525/50Violet-A", "530/30Blue-A", "670/30Yellow-A") + expect_equal(ret, expected) + + # with boolean vector + indices <- c(1, 6, 14, 18) + boolInput <- rep(FALSE, 21) + boolInput[indices] <- TRUE + ret <- getChannelNamesFromMarkers( + OMIP021Samples[[1]], + boolInput + ) + + expect_equal(ret, expected) + + + # with indices vector + ret <- getChannelNamesFromMarkers( + OMIP021Samples[[1]], + indices + ) + expect_equal(ret, expected) + + # missing channel + missingInput <- c("CD4") + + expect_error(getChannelNamesFromMarkers( + OMIP021Samples[[1]], + missingInput + ), + regexp = "could not be found" + ) +}) + +test_that("updateMarkerName works", { + retFF <- updateMarkerName(OMIP021Samples[[1]], + channel = "FSC-A", + newMarkerName = "Fwd Scatter-A") + + checkMkName <- getChannelNamesFromMarkers(retFF, markers = "Fwd Scatter-A") + expect_equal(checkMkName, "FSC-A") + expect_equal(flowCore::keyword(retFF, "$P1S")[["$P1S"]], "Fwd Scatter-A") + + # same with channel as index + retFF <- updateMarkerName(OMIP021Samples[[1]], + channel = 1, + newMarkerName = "Fwd Scatter-A") + checkMkName <- getChannelNamesFromMarkers(retFF, markers = "Fwd Scatter-A") + expect_equal(checkMkName, "FSC-A") + expect_equal(flowCore::keyword(retFF, "$P1S")[["$P1S"]], "Fwd Scatter-A") + + # now channel provided as marker (desc), not channel name + retFF <- updateMarkerName(OMIP021Samples[[1]], + channel = "BV785 - CD3", + newMarkerName = "fancy CD3") + checkMkName <- getChannelNamesFromMarkers(retFF, markers = "fancy CD3") + expect_equal(checkMkName, "670/30Violet-A") + expect_equal(flowCore::keyword(retFF, "$P10S")[["$P10S"]], "fancy CD3") + + + + expect_error(updateMarkerName(OMIP021Samples[[1]], channel = "FFF-A", + newMarkerName = "whatever"), + regexp = "channel not found") +}) + +test_that("removeChannels works", { + retFF <- removeChannels(OMIP021Samples[[1]], + channels = c("FSC-A", "SSC-A")) + nRemainingChannels <- length(flowCore::colnames(retFF)) + expect_equal(nRemainingChannels, 20) + + expect_warning(removeChannels(OMIP021Samples[[1]], + channels = c("FSC-A", "SSC-S")), + regexp = "not found in flowFrame") + + +}) + +test_that("getFCSFileName works", { + fName <- getFCSFileName(OMIP021Samples[[1]]) + expect_equal(fName, "Donor1.fcs") +}) + +test_that("writeFlowFrame works", { + outputDir <- base::tempdir() + + ff_c <- readRDS(test_path("fixtures", "ff_c.rds")) + + expect_error(writeFlowFrame(ff, dir = file.path(outputDir, "notThere")), + regexp = "Provided directory does not exist") + + prefix <- "File_" + suffix <- "_export" + + writeFlowFrame(ff_c, dir = outputDir, + useFCSFileName = TRUE, + prefix = prefix, + suffix = suffix, + format = "fcs") + + outputFile <- file.path(outputDir, + paste0(prefix, "Donor1", suffix, ".fcs")) + + thisFF <- flowCore::read.FCS(outputFile, transform = FALSE) + expect_true(all(round(flowCore::exprs(thisFF), 0) + == round(flowCore::exprs(ff_c), 0))) + + writeFlowFrame(ff_c, dir = outputDir, + useFCSFileName = FALSE, + prefix = prefix, + suffix = suffix, + format = "csv") + + outputCSV <- file.path(outputDir, + paste0(prefix, suffix, ".csv")) + + thisExpr <- read.csv(file = outputCSV) + expect_true(all(round(thisExpr,4) == round(flowCore::exprs(ff_c), 4))) +}) + + +test_that("updateCompMatrixLabels works", { + myMat <- diag(nrow = 3) + names <- c("450/50Violet-A", "BV605 - CD161", + "670/30Yellow-A :: BV785 - CD3") + colnames(myMat) <- rownames(myMat) <- names + + myNewMat <- .updateCompMatrixLabels(myMat, OMIP021Samples[[1]]) + + newColNames <- colnames(myNewMat) + newRowNames <- rownames(myNewMat) + + expectedNewNames <- c("450/50Violet-A", + "540/30Violet-A", + "670/30Yellow-A") + + expect_equal(newColNames, expectedNewNames) + expect_equal(newRowNames, expectedNewNames) +}) + + +