R/mock.R
f1a36577
 #' Generate count matrix for spatially variable genes.
 #'
b519d606
 #' @param size An `integer` scalar. Cells will be spatially arranged on a `size
 #'   x size` grid. Default: 10, corresponding to 100 cells.
 #' @param tot_genes An `integer` scalar. Total number of genes. Default: 1000.
 #' @param de_genes An `integer` scaler. The number of spatially variable genes.
 #'   Default: 100.
 #' @param return_SPE A `logical`, whether to return result as a
720eb4fe
 #'   \linkS4class{SpatialExperiment}. Default: `FALSE`.
f1a36577
 #'
720eb4fe
 #' @return
 #' If `return_SPE = TRUE`, returns a \linkS4class{SpatialExperiment} object.
 #'
 #' If not, a `list` containing:
f1a36577
 #' * `coordinates`: `data.frame` with `x` and `y` columns;
 #' * `counts`: `matrix` with generated gene counts.
720eb4fe
 #'
 #' @examples
6b99d290
 #' spe <- mockSVG(size = 20, tot_genes = 3, de_genes = 1, return_SPE = TRUE)
720eb4fe
 #' spe
 #'
f1a36577
 #' @export
f4072dd3
 #' @importFrom stats rnbinom runif
6b99d290
 mockSVG <- function(size, tot_genes, de_genes, return_SPE = FALSE) {
720eb4fe
     n_cells <- size * size
9eab3712
     coordinates <- data.frame(
         x = rep(seq.int(size), size),
         y = rep(seq.int(size), each = size)
     )
 
     mu <- 2^runif(tot_genes, -1, 5)
     counts <- matrix(
720eb4fe
         rnbinom(tot_genes * n_cells, mu = mu, size = 10),
9eab3712
         nrow = tot_genes
     )
     m <- (size / 2) + 1
     mask <- coordinates$x < m & coordinates$y < m
     counts[seq.int(de_genes), mask] <- counts[seq.int(de_genes), mask] + 20
 
720eb4fe
     ## Set dimnames
     gene_names <- sprintf("Gene_%s", formatC(seq_len(tot_genes), width = 4, flag = 0))
     cell_names <- sprintf("Cell_%s", formatC(seq_len(n_cells), width = 3, flag = 0))
     rownames(coordinates) <- cell_names
     dimnames(counts) <- list(gene_names, cell_names)
 
     out <- list(coordinates = coordinates, counts = counts)
 
     if (return_SPE) {
         out <- SpatialExperiment::SpatialExperiment(
             assays = list(counts = counts),
72c6e9af
             spatialCoords = as.matrix(coordinates)
720eb4fe
         )
     }
     out
f1a36577
 }