From 6169cc39b844fceea5e872375fc7cc5a50d270b7 Mon Sep 17 00:00:00 2001 From: Robrecht Cannoodt Date: Mon, 18 Sep 2023 16:20:53 +0200 Subject: [PATCH] add more types to dummy data --- tests/testthat/helper-dummy_data.R | 68 +++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/tests/testthat/helper-dummy_data.R b/tests/testthat/helper-dummy_data.R index b48585fb..5d740e19 100644 --- a/tests/testthat/helper-dummy_data.R +++ b/tests/testthat/helper-dummy_data.R @@ -11,11 +11,12 @@ #' #' @examples #' dummy <- dummy_data() -dummy_data <- function(n_obs = 10L, n_vars = 20L, - output = c( - "list", "SingleCellExperiment", - "Seurat" - )) { +dummy_data <- function( + n_obs = 10L, + n_vars = 20L, + output = c( + "list", "SingleCellExperiment", "Seurat" + )) { output <- match.arg(output) switch(output, @@ -39,21 +40,55 @@ dummy_list <- function(n_obs = 10L, n_vars = 20L) { # generate X X <- Matrix::rsparsematrix(nrow = n_obs, ncol = n_vars, density = .1) - # generate layers + # generate layers in dense, sparse, row compressed and column compressed layers <- list( - X2 = X * 2, - X3 = X * 3 + dense = as.matrix(X), + CsparseMatrix = as(X, "CsparseMatrix"), + RsparseMatrix = as(X, "RsparseMatrix") ) - # generate obs + # generate obs with different types (character, integer, factors, etc.) obs <- data.frame( - cell_type = sample(c("tcell", "bcell"), n_obs, replace = TRUE), - cluster = sample.int(3, n_obs, replace = TRUE) + character = paste0("cell", seq_len(n_obs)), + integer = seq_len(n_obs), + factor = factor(paste0("cell", seq_len(n_obs))), + factor_ordered = factor(paste0("cell", seq_len(n_obs)), ordered = TRUE), + logical = sample(c(TRUE, FALSE), n_obs, replace = TRUE), + numeric = runif(n_obs), + character_with_nas = c(paste0("cell", seq_len(n_obs - 1)), NA), + integer_with_nas = c(seq_len(n_obs - 1), NA), + factor_with_nas = c( + factor(paste0("cell", seq_len(n_obs - 1))), + NA_character_ + ), + factor_ordered_with_nas = c( + factor(paste0("cell", seq_len(n_obs - 1)), ordered = TRUE), + NA_character_ + ), + logical_with_nas = c(sample(c(TRUE, FALSE), n_obs - 1, replace = TRUE), NA), + numeric_with_nas = c(runif(n_obs - 1), NA) ) - # generate var + # generate var with different types (character, integer, factors, etc.) var <- data.frame( - geneinfo = sample(c("a", "b", "c"), n_vars, replace = TRUE) + character = paste0("gene", seq_len(n_vars)), + integer = seq_len(n_vars), + factor = factor(paste0("gene", seq_len(n_vars))), + factor_ordered = factor(paste0("gene", seq_len(n_vars)), ordered = TRUE), + logical = sample(c(TRUE, FALSE), n_vars, replace = TRUE), + numeric = runif(n_vars), + character_with_nas = c(paste0("gene", seq_len(n_vars - 1)), NA), + integer_with_nas = c(seq_len(n_vars - 1), NA), + factor_with_nas = c( + factor(paste0("gene", seq_len(n_vars - 1))), + NA_character_ + ), + factor_ordered_with_nas = c( + factor(paste0("gene", seq_len(n_vars - 1)), ordered = TRUE), + NA_character_ + ), + logical_with_nas = c(sample(c(TRUE, FALSE), n_vars - 1, replace = TRUE), NA), + numeric_with_nas = c(runif(n_vars - 1), NA) ) # generate obs_names @@ -122,18 +157,19 @@ dummy_Seurat <- function(...) { # nolint dummy <- dummy_data(...) - X <- t(dummy$X) + X <- t(dummy$layers[["dense"]]) colnames(X) <- dummy$obs_names rownames(X) <- dummy$var_names seurat <- SeuratObject::CreateSeuratObject(X) - X2 <- t(dummy$layers$X2) + X2 <- as(t(dummy$layers[["CsparseMatrix"]]), "CsparseMatrix") colnames(X2) <- dummy$obs_names rownames(X2) <- dummy$var_names seurat <- SeuratObject::SetAssayData(seurat, "data", X2) - X3 <- as.matrix(t(dummy$layers$X3)) + # seurat doesn't support RsparseMatrices + X3 <- as.matrix(t(dummy$layers[["RsparseMatrix"]])) colnames(X3) <- dummy$obs_names rownames(X3) <- dummy$var_names seurat <- SeuratObject::SetAssayData(seurat, "scale.data", X3)