downsampleSeuObjByIdentAndMaxcells: Sample max number of Cells From each identity in a Seurat...

View source: R/Seurat.Utils.R

downsampleSeuObjByIdentAndMaxcellsR Documentation

Sample max number of Cells From each identity in a Seurat Object

Description

This function samples a specified maximum number of cells from each identity class in a Seurat object, in the meta.data. It ensures that the sampling does not exceed the total number of cells available per identity.

Usage

downsampleSeuObjByIdentAndMaxcells(
  obj,
  ident = GetNamedClusteringRuns()[1],
  max.cells = min(table(obj[[ident]])),
  verbose = TRUE,
  replacement.thr = 0.05,
  dsample.to.repl.thr = (max.cells/ncol(obj)) < replacement.thr,
  plot_stats = TRUE,
  seed = 1989
)

Arguments

obj

A Seurat object from which cells are to be sampled.

ident

A character vector specifying the identity class from which cells are to be sampled.

max.cells

A positive integer indicating the maximum number of cells to sample from each identity class.

verbose

Logical indicating if messages about the sampling process should be printed to the console. Defaults to TRUE.

replacement.thr

A numeric value between 0 and 1 indicating the percentage of cells to sample from each identity class. Defaults to 0.05.

dsample.to.repl.thr

Logical indicating if sampling should be done with replacement. Defaults to FALSE.

plot_stats

Logical indicating to plot a barplot.

seed

An integer to set the seed for reproducibility.

Details

This function checks for the presence of the specified identity class within the object's metadata. If the number of cells within any identity class is less than or equal to the max.cells parameter, all cells from that class are retained. Otherwise, a random sample of max.cells is taken from the class. The function updates the identity of the cells in the returned Seurat object to reflect the sampled cells. If verbose is TRUE, it prints the total number of cells sampled and provides a visual summary of the fraction of cells retained per identity class.

Value

Returns a Seurat object containing only the sampled cells.

Examples

# Assuming `seuratObj` is a Seurat object with identities stored in its metadata
sampledSeuratObj <- downsampleSeuObjByIdentAndMaxcells(obj = seuratObj, ident = "cellType", max.cells = 100)


vertesy/Seurat.utils documentation built on Dec. 4, 2024, 5:20 p.m.