Skip to contents

This function generates pseudobulk samples by aggregating single-cell transcriptomics.

Usage

CreatePseudobulks(data = NULL, groups, counts = NULL, n_mixtures = 100)

Arguments

data

A matrix of normalized gene expression data (genes x cells). If NULL, counts must be provided.

groups

A named vector indicating the group (e.g., spatial ecotype) for each cell. The names should correspond to the column names of the data matrix.

counts

A matrix of raw counts data (genes x cells). Used to generate normalized data if data is not provided.

n_mixtures

An integer specifying the number of pseudobulk samples to create. Default is 100.

Value

A list containing two elements:

Fracs

A matrix of the fractions of each group in the pseudobulk samples (rows represent pseudobulk samples, columns represent groups).

Mixtures

A matrix of pseudobulk gene expression data (genes x pseudobulk samples).

Details

  • If `data` is not provided, the function will normalize the `counts` matrix by dividing each column by its sum and multiplying by 10,000.

  • If the maximum value in `data` is less than 80, it assumes the data is in log2 scale and converts it back to non-log scale.

  • The `groups` vector is used to ensure that cells are correctly assigned to their respective groups. If `groups` does not have names, it is assumed that the names correspond to the column names of the `data` matrix.

  • Pseudobulk samples are created by sampling cells from each group based on predefined fractions, and then calculating the average expression for each gene in the pseudobulk samples.

  • The pseudobulk data is then normalized using Seurat's `NormalizeData` function.

Examples

library(SpatialEcoTyper)
library(data.table)
counts <- fread("https://spatialecotyper.stanford.edu/inc/inc.public.vignettes.php?file=scRNAseq_demo_counts.tsv",
                sep = "\t", header = TRUE, data.table = FALSE)
#> Warning: URL 'https://spatialecotyper.stanford.edu/inc/inc.public.vignettes.php?file=scRNAseq_demo_counts.tsv': Timeout of 60 seconds was reached
#> Error in download.file(file, tmpFile, method = method, mode = "wb", quiet = !showProgress): download from 'https://spatialecotyper.stanford.edu/inc/inc.public.vignettes.php?file=scRNAseq_demo_counts.tsv' failed
rownames(counts) = counts[, 1]
#> Error in counts[, 1]: object of type 'closure' is not subsettable
counts = as.matrix(counts[, -1])
#> Error in (function (cond) .Internal(C_tryCatchHelper(addr, 1L, cond)))(structure(list(message = "object of type 'closure' is not subsettable",     call = counts[, -1], object = new("standardGeneric", .Data = function (object,         ...)     standardGeneric("counts"), generic = structure("counts", package = "BiocGenerics"),         package = "BiocGenerics", group = list(), valueClass = character(0),         signature = "object", default = NULL, skeleton = (function (object,             ...)         stop(gettextf("invalid call in method dispatch to '%s' (no default method)",             "counts"), domain = NA))(object, ...))), class = c("notSubsettableError", "error", "condition"))): error in evaluating the argument 'x' in selecting a method for function 'as.matrix': object of type 'closure' is not subsettable
groups <- sample(paste0("SE", 1:10), ncol(counts), replace = TRUE)
#> Error in sample.int(length(x), size, replace, prob): invalid 'size' argument
names(groups) <- colnames(counts)
#> Error: object 'groups' not found
result = CreatePseudobulks(counts = counts, groups = groups, n_mixtures = 20)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 't': argument is not a matrix
head(result$Mixtures[, 1:5]) ## Gene expression matrix of pseudobulks
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'head': object 'result' not found
head(result$Fracs) ## SE fractions in pseudobulks
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'head': object 'result' not found