Computes various statistics (mean, median, zscore, tscore, etc.) on a matrix, grouped by clusters.

CalcStats(object, ...)

# S3 method for Seurat
CalcStats(
  seu,
  features,
  group.by = NULL,
  cells = NULL,
  slot = "data",
  assay = NULL,
  method = "zscore",
  exp.transform = F,
  order = NULL,
  n = Inf,
  p.threshold = 0.05,
  priority = c("expr", "none")
)

# S3 method for default
CalcStats(
  matr,
  f,
  method = "zscore",
  exp.transform = F,
  t = F,
  order = NULL,
  n = Inf,
  p.threshold = 0.05
)

Arguments

object

An object. Can either be a Seurat object or a matrix.

seu

A Seurat object. Only applicable when using the Seurat method.

features

Features for computation, including gene expression, metrics, PC scores, or any other data retrievable via `FetchData()`. Defaults to NULL, implying all features in the matrix. Only applicable for the Seurat method.

group.by

A variable from `meta.data` for grouping or a character vector of equal length as the number of cells. Only applicable for the Seurat method.

cells

Cell identifiers to be used. Defaults to all cells. Only applicable for the Seurat method.

slot

Slot from which to fetch feature data. Only applicable for the Seurat method.

assay

Name of the assay to use. Defaults to the active assay. Only applicable for the Seurat method.

method

Computation method, one of: "mean", "median", "zscore", "tscore", "p", or "logFC". Default: "zscore".

exp.transform

Indicates if data should be transformed with `expm1`. Default: FALSE.

order

Sort rows by either "value" or "p" (t.test).

n

Top n rows for each cluster. Ignored if `order` is NULL. Default: Inf.

p.threshold

Rows with p-value greater than this threshold are omitted when `order = "p"`.

priority

If set to "expr", fetches data from the expression matrix over `meta.data`. Only applicable for the Seurat method.

matr

A matrix or data frame with rows as features and columns as cells.

f

A factor or vector indicating cell identity. It should match the column length of `matr`.

t

Transpose matrix if features are columns and cells are rows. Default: FALSE.

Value

A matrix.

Details

Computes statistics for each feature across cell types. "p" value is determined by t-test. For log-normalized data's LogFC computation, it's advised to set `exp.transform` to TRUE.

Examples

library(Seurat)
library(SeuratExtend)

# Using a Seurat object as input. First, select some genes for calculation:
genes <- VariableFeatures(pbmc)[1:20]

# Calculate zscore, grouping by the default 'ident' (cluster):
genes.zscore <- CalcStats(pbmc, features = genes, method = "zscore")
head(genes.zscore)

# Visualize with a heatmap:
Heatmap(genes.zscore, lab_fill = "zscore")

# Select more genes and retain the top 4 genes of each cluster, sorted by p-value.
# This can be a convenient method to display the top marker genes of each cluster:
genes <- VariableFeatures(pbmc)
genes.zscore <- CalcStats(
  pbmc, features = genes, method = "zscore", group.by = "cluster",
  order = "p", n = 4)
Heatmap(genes.zscore, lab_fill = "zscore")

# It's also possible to use a matrix as input. For instance, we initially perform
# Geneset Enrichment Analysis (GSEA) using the Hallmark 50 geneset and obtain the AUCell matrix
# (rows represent pathways, columns represent cells):
pbmc <- GeneSetAnalysis(pbmc, genesets = hall50$human)
matr <- pbmc@misc$AUCell$genesets

# Next, we calculate the zscore, grouped by 'cluster':
gsea.zscore <- CalcStats(matr, f = pbmc$cluster, method = "zscore")
Heatmap(gsea.zscore, lab_fill = "zscore")