Create cell communities using Leiden community detection
Source:R/create_communities.R
create_communities.RdAssigns cells in a SingleCellExperiment object to communities
using the Leiden algorithm at a single chosen resolution. Community
assignments are added to colData(sce) and a per-community
summary table is returned alongside the updated object. Use
test_resolution first to identify a suitable resolution.
Usage
create_communities(
sce,
graph.name = "RNA_snn",
resolution = 0.5,
min_cells = 5,
feature.col = "nFeature_RNA",
count.col = "nCount_RNA"
)Arguments
- sce
A
SingleCellExperimentobject. The object must contain a pre-computed nearest-neighbour graph stored inmetadata(sce)$graphsunder the name given bygraph.name. See the Graph preparation section below for how to build and store the graph- graph.name
A character string giving the name of the graph to retrieve from
metadata(sce)$graphs. Defaults to"RNA_snn".- resolution
A single numeric value controlling the granularity of Leiden clustering. Higher values produce more, smaller communities. Use
test_resolutionto explore candidate values before committing to one. Defaults to0.5.- min_cells
A positive integer. Communities with fewer than this many cells are considered too small; their cells are reassigned to the nearest valid community. If all communities fall below this threshold the function stops with an informative error. Defaults to
5.- feature.col
A character string giving the name of the
colData(sce)column holding the number of detected features (genes) per cell. Defaults to"nFeature_RNA", the Seurat convention. Set this if your object uses a different column name (e.g."detected", as produced byscuttle::perCellQCMetrics). If the column is not found,mean_detectedis returned asNAwith a warning.- count.col
A character string giving the name of the
colData(sce)column holding total counts per cell. Defaults to"nCount_RNA", the Seurat convention. Set this if your object uses a different column name (e.g."sum", as produced byscuttle::perCellQCMetrics). If the column is not found,mean_total_countsis returned asNAwith a warning.
Value
A named list with two elements:
sceThe input
SingleCellExperimentobject with a newcommunity_idcolumn added tocolData.communities_summaryA
data.framewith one row per community containing:community_idCommunity identifier.
n_cellsNumber of cells in the community.
mean_detectedMean number of detected features per cell, taken from the
feature.colcolumn ofcolData.mean_total_countsMean total counts per cell, taken from the
count.colcolumn ofcolData.
Details
Graph preparation:
This function does not build the nearest-neighbour graph itself.
The graph must be computed and stored in metadata(sce)$graphs before
calling this function. Two workflows are supported:
1. From scratch with scran:
library(scran)
# Build a shared nearest-neighbour graph on the PCA embedding
g <- scran::buildSNNGraph(sce, use.dimred = "PCA", k = 20)
# Store it under the name this function will look for
metadata(sce)$graphs[["RNA_snn"]] <- g2. From an existing Seurat object:
library(Seurat)
# Convert to SCE first
sce <- as.SingleCellExperiment(seurat_obj)
# Extract the adjacency matrix from the Seurat object, convert to igraph,
# and store in the SCE metadata
g <- igraph::graph_from_adjacency_matrix(
seurat_obj@graphs[["RNA_snn"]],
mode = "max",
weighted = TRUE
)
metadata(sce)$graphs[["RNA_snn"]] <- gThe value passed to graph.name must match the key used when storing
the graph in metadata(sce)$graphs.
See also
test_resolution to evaluate resolution choices before
running this function
Examples
if (FALSE) { # \dontrun{
library(scran)
# Build and store the graph
g <- scran::buildSNNGraph(sce, use.dimred = "PCA", k = 20)
metadata(sce)$graphs[["RNA_snn"]] <- g
# Optionally explore resolutions first
test_resolution(sce, resolutions = c(0.5, 1, 2.5, 5))
# Create communities at the chosen resolution
out <- create_communities(sce, resolution = 1)
sce <- out$sce
summary_df <- out$communities_summary
# If sce was built with scuttle::perCellQCMetrics() rather than Seurat,
# specify the matching column names
out <- create_communities(sce, resolution = 1,
feature.col = "detected",
count.col = "sum")
} # }