Skip to content

Commit

Permalink
add more types to dummy data
Browse files Browse the repository at this point in the history
  • Loading branch information
rcannood committed Sep 18, 2023
1 parent fc08613 commit 6169cc3
Showing 1 changed file with 52 additions and 16 deletions.
68 changes: 52 additions & 16 deletions tests/testthat/helper-dummy_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6169cc3

Please sign in to comment.