# Analysis of single-cell genomic data with celda

# Introduction

CEllular Latent Dirichlet Allocation (celda) is a collection of Bayesian hierarchical models to perform feature and cell bi-clustering for count data generated by single-cell platforms. This algorithm is an extension of the Latent Dirichlet Allocation (LDA) topic modeling framework that has been popular in text mining applications and has shown good performance with sparse data. celda simultaneously clusters features (i.e. gene expression) into modules based on co-expression patterns across cells and cells into subpopulations based on the probabilities of the feature modules within each cell.

Starting from Bioconductor release 3.12 (`celda` version 1.6.0), `celda` makes use of SingleCellExperiment (SCE) objects for storing data and results. In this vignette we will demonstrate how to use celda to perform cell and feature clustering with a simple, small simulated dataset. This vignette does not include upstream importing of data, quality control, or filtering. To see a more complete analysis of larger real-world datasets, visit camplab.net/celda for additional vignettes.

# Installation

celda can be installed from Bioconductor:

    if (!requireNamespace("BiocManager", quietly = TRUE)) {
        install.packages("BiocManager")
    }
    BiocManager::install("celda")

To load the package, type the following:

    library(celda)

A complete list of help files are accessible using the help command with the `package` option.

    help(package = celda)

To see the latest updates and releases or to post a bug, see our GitHub page at https://github.com/campbio/celda. To ask questions about running celda, post a thread on Bioconductor support site at https://support.bioconductor.org/.

# Generation of a simulated single cell dataset

celda will take a matrix of counts where each row is a feature, each column is a cell, and each entry in the matrix is the number of counts of each feature in each cell. To illustrate the utility of celda, we will apply it to a simulated dataset.

In the function `simulateCells`, the K parameter designates the number of cell clusters, the L parameter determines the number of feature modules, the S parameter determines the number of samples in the simulated dataset, the G parameter determines the number of features to be simulated, and CRange specifies the lower and upper bounds of the number of cells to be generated in each sample.

To simulate a dataset of 5 samples with 5 cell populations, 10 feature modules, 200 features, and between 30 to 50 cells per sample using `celda_CG` model:

    simsce <- simulateCells("celda_CG",
        S = 5, K = 5, L = 10, G = 200, CRange = c(30, 50))

The `counts` assay slot in `simsce` contains the counts matrix. The dimensions of counts matrix:

    library(SingleCellExperiment)
    dim(counts(simsce))

    ## [1] 200 207

Columns `celda_sample_label` and `celda_cell_cluster` in `colData(simsce)` contain sample labels and celda cell population cluster labels. Here are the numbers of cells in each subpopulation and in each sample:

    table(colData(simsce)$celda_cell_cluster)

    ##
    ##  1  2  3  4  5
    ## 42 44 40 47 34

    table(colData(simsce)$celda_sample_label)

    ##
    ## Sample_1 Sample_2 Sample_3 Sample_4 Sample_5
    ##       43       48       45       40       31

Column `celda_feature_module` in `rowData(simsce)` contains feature module labels. Here is the number of features in each feature module:

    table(rowData(simsce)$celda_feature_module)

    ##
    ##  1  2  3  4  5  6  7  8  9 10
    ## 23 39 17 15 21 22 19 12  4 28

# Feature selection

A simple heuristic feature selection is performed to reduce the size of features used for clustering. To speed up the process, only features with at least 3 counts in at least 3 cells are included in downstream clustering for this data. A subset `SingleCellExperiment` object with filtered features is stored in `altExp(simsce, "featureSubset")` slot by default.

    simsce <- selectFeatures(simsce)

If the number of features is still too large, then a smaller subset of features can be obtained by selecting the top number of most variable genes. For an example code, see the PBMC3K tutorial in the online celda documentation.

# Performing bi-clustering with celda

There are currently three models within celda package: `celda_C` will cluster cells, `celda_G` will cluster features, and `celda_CG` will simultaneously cluster cells and features. Within the functions the `K` parameter will be the number of cell populations to be estimated, while the `L` parameter will be the number of feature modules to be estimated in the output model.

    sce <- celda_CG(x = simsce, K = 5, L = 10, verbose = FALSE, nchains = 1)

Here is a comparison between the true cluster labels and the estimated cluster labels.

    table(celdaClusters(sce), celdaClusters(simsce))

    ##
    ##      1  2  3  4  5
    ##   1  0 44  0  0  0
    ##   2 42  0  0  0  0
    ##   3  0  0 40  0  0
    ##   4  0  0  0 47  0
    ##   5  0  0  0  0 34

    table(celdaModules(sce), celdaModules(simsce))

    ##
    ##       1  2  3  4  5  6  7  8  9 10
    ##   1   0 32  0  0  0  0  0  0  0  0
    ##   2  19  0  0  0  0  0  0  0  0  0
    ##   3   0  0 15  0  0  0  0  0  0  0
    ##   4   0  0  0 13  0  0  0  0  0  0
    ##   5   0  0  0  0 21  0  0  0  0  0
    ##   6   0  0  0  0  0 19  0  0  0  0
    ##   7   0  0  0  0  0  0  0 12  0  0
    ##   8   0  0  0  0  0  0 17  0  0  0
    ##   9   0  0  0  0  0  0  0  0  3  0
    ##   10  0  0  0  0  0  0  0  0  0 20

# Visualization

## Plotting cell populations on 2D-embeddings

celda contains its own wrapper function for tSNE and UMAP called `celdaTsne` and `celdaUmap`, respectively. Both of these functions can be used to embed cells into 2-dimensions. The output can be used in the downstream plotting functions `plotDimReduceCluster`, `plotDimReduceModule`, and `plotDimReduceFeature` to show cell population clusters, module probabilities, and expression of individual features, respectively.

    sce <- celdaUmap(sce)

    plotDimReduceCluster(x = sce, reducedDimName = "celda_UMAP")

    ## Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
    ## ℹ Please use tidy evaluation idioms with `aes()`.
    ## ℹ See also `vignette("ggplot2-in-packages")` for more information.
    ## ℹ The deprecated feature was likely used in the celda package.
    ##   Please report the issue at <https://github.com/campbio/celda/issues>.
    ## This warning is displayed once per session.
    ## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
    ## generated.

    plotDimReduceModule(x = sce, reducedDimName = "celda_UMAP", rescale = TRUE)

    plotDimReduceFeature(x = sce, reducedDimName = "celda_UMAP",
        normalize = TRUE, features = "Gene_1")

## Creating an expression heatmap

The clustering results can be viewed with a heatmap of the normalized counts using the function `celdaHeatmap`. The top `nfeatures` in each module will be selected according to the factorized module probability matrix.

    plot(celdaHeatmap(sce = sce, nfeatures = 10))

## Displaying relationships between modules and cell populations

The relationships between feature modules and cell populations can be visualized with `celdaProbabilityMap`. The absolute probabilities of each feature module in each cellular subpopulation is shown on the left. The normalized and z-scored expression of each module in each cell population is shown on the right.

    celdaProbabilityMap(sce)

## Examining co-expression with module heatmaps

`moduleHeatmap` creates a heatmap using only the features from a specific feature module. Cells are ordered from those with the lowest probability of the module to the highest. If more than one module is used, then cells will be ordered by the probabilities of the first module.

    moduleHeatmap(sce, featureModule = c(1,2), topCells = 100)

# Identifying reasonable numbers of feature modules and cell subpopulations

In the previous example, the best `K` (the number of cell clusters) and `L` (the number of feature modules) was already known. However, the optimal `K` and `L` for each new dataset will likely not be known beforehand and multiple choices of `K` and `L` may need to be tried and compared. celda offers two sets of functions to determine the optimum `K` and `L`, `recursiveSplitModule`/`recursiveSplitCell`, and `celdaGridSearch`.

## Using recursive splitting

Functions `recursiveSplitModule` and `recursiveSplitCell` offer a fast method to generate a celda model with optimum `K` and `L`. First, `recursiveSplitModule` is used to determine the optimal `L`. `recursiveSplitModule` first splits features into however many modules are specified in `initialL`. The module labels are then recursively split in a way that would generate the highest log-likelihood, all the way up to `maxL`.

    moduleSplit <- recursiveSplitModule(simsce, initialL = 2, maxL = 15)

Perplexity is a statistical measure of how well a probability model can predict new data. Lower perplexity indicates a better model. The perplexity of each model can be visualized with `plotGridSearchPerplexity`. In general, visual inspection of the plot can be used to select the optimal number of modules (`L`) or cell populations (`K`) by identifying the “elbow” - where the rate of decrease in the perplexity starts to drop off.

    plotGridSearchPerplexity(moduleSplit)

In this example, the perplexity for `L` stops decreasing at L = 10, thus L = 10 would be a good choice. Sometimes the perplexity alone does not show a clear elbow or “leveling off”. However, the rate of perplexity change (RPC) can be more informative to determine when adding new modules does not add much additional information Zhao et al., 2015). An RPC closer to zero indicates that the addition of new modules or cell clusters is not substantially decreasing the perplexity. The RPC of models can be visualized using function `plotRPC`:

    plotRPC(moduleSplit)

Once you have identified the optimal `L` (in this case, L is selected to be 10), the module labels are used for initialization in `recursiveSplitCell`. Similarly to `recursiveSplitModule`, cells are initially split into a small number of subpopulations, and the subpopulations are recursively split up.

    moduleSplitSelect <- subsetCeldaList(moduleSplit, params = list(L = 10))

    cellSplit <- recursiveSplitCell(moduleSplitSelect,
        initialK = 3,
        maxK = 12,
        yInit = celdaModules(moduleSplitSelect))

    plotGridSearchPerplexity(cellSplit)

    plotRPC(cellSplit)

In this plot, the perplexity for K stops decreasing at K = 5, with a final K/L combination of K = 5, L = 10. Generally, this method can be used to pick a reasonable `L` and a potential range of `K`. However, manual review of specific selections of `K` is often required to ensure results are biologically coherent.

Once users have chosen the K/L parameters for further analysis, the `subsetCeldaList` function can be used to subset the celda list SCE object to a single model SCE object.

    sce <- subsetCeldaList(cellSplit, params = list(K = 5, L = 10))

## Using a grid search

Alternativley to recursive splitting, celda is able to run multiple combinations of K and L with multiple chains in parallel via the `celdaGridSearch` function.

    cgs <- celdaGridSearch(simsce,
        paramsTest = list(K = seq(4, 6), L = seq(9, 11)),
        cores = 1,
        model = "celda_CG",
        nchains = 2,
        maxIter = 100,
        verbose = FALSE,
        bestOnly = TRUE)

Setting `verbose` to `TRUE` will print the output of each model to a text file. These results can be visualized with `plotGridSearchPerplexity`. The major goal is to pick the lowest `K` and `L` combination with relatively good perplexity. In general, visual inspection of the plot can be used to select the number of modules (`L`) or cell populations (`K`) where the rate of decrease in the perplexity starts to drop off. `bestOnly = TRUE` indicates that only the chain with the best log likelihood will be returned for each K/L combination.

    plotGridSearchPerplexity(cgs)

In this example, the perplexity for `L` stops decreasing at L = 10 for the majority of `K` values. For the line corresponding to L = 10, the perplexity stops decreasing at K = 5. Thus L = 10 and K = 5 would be a good choice. Again, manual review of specific selections of K is often be required to ensure results are biologically coherent.

Once users have chosen the K/L parameters for further analysis, the `subsetCeldaList` function can be used to subset the celda list SCE object to a single model SCE object.

    sce <- subsetCeldaList(cgs, params = list(K = 5, L = 10))

If the “bestOnly” parameter is set to FALSE in the `celdaGridSearch`, then the `selectBestModel` function can be used to select the chains with the lowest log likelihoods within each combination of parameters. Alternatively, users can select a specific chain by specifying the index within the `subsetCeldaList` function.

    cgs <- celdaGridSearch(simsce,
        paramsTest = list(K = seq(4, 6), L = seq(9, 11)),
        cores = 1,
        model = "celda_CG",
        nchains = 2,
        maxIter = 100,
        verbose = FALSE,
        bestOnly = FALSE)

    cgs <- resamplePerplexity(cgs, celdaList = cgs, resample = 2)

    cgsK5L10 <- subsetCeldaList(cgs, params = list(K = 5, L = 10))

    sce <- selectBestModel(cgsK5L10)

# Miscellaneous utility functions

celda also contains several utility functions for the users’ convenience.

## Finding the modules for feature with featureModuleLookup

`featureModuleLookup` can be used to look up the module a specific feature was clustered to.

    featureModuleLookup(sce, feature = c("Gene_99"))

    ## Gene_99
    ##       4

## Reordering cluster labels with recodeClusterZ, recodeClusterY

`recodeClusterZ` and `recodeClusterY` allows the user to recode the cell and feature cluster labels, respectively.

    sceZRecoded <- recodeClusterZ(sce,
        from = c(1, 2, 3, 4, 5), to = c(2, 1, 3, 4, 5))

The model prior to reordering cell labels compared to after reordering cell labels:

    table(celdaClusters(sce), celdaClusters(sceZRecoded))

    ##
    ##      1  2  3  4  5
    ##   1  0 44  0  0  0
    ##   2 42  0  0  0  0
    ##   3  0  0 40  0  0
    ##   4  0  0  0 47  0
    ##   5  0  0  0  0 34

# Session Information

    sessionInfo()

    ## R version 4.6.0 (2026-04-24)
    ## Platform: x86_64-pc-linux-gnu
    ## Running under: Ubuntu 24.04.4 LTS
    ##
    ## Matrix products: default
    ## BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
    ## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so;  LAPACK version 3.12.0
    ##
    ## locale:
    ##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
    ##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
    ##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
    ##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
    ##  [9] LC_ADDRESS=C               LC_TELEPHONE=C
    ## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
    ##
    ## time zone: Etc/UTC
    ## tzcode source: system (glibc)
    ##
    ## attached base packages:
    ## [1] stats4    stats     graphics  grDevices utils     datasets  methods
    ## [8] base
    ##
    ## other attached packages:
    ##  [1] celda_1.29.0                Matrix_1.7-5
    ##  [3] SingleCellExperiment_1.35.0 SummarizedExperiment_1.43.0
    ##  [5] Biobase_2.73.1              GenomicRanges_1.65.0
    ##  [7] Seqinfo_1.3.0               IRanges_2.47.0
    ##  [9] S4Vectors_0.51.1            BiocGenerics_0.59.0
    ## [11] generics_0.1.4              MatrixGenerics_1.25.0
    ## [13] matrixStats_1.5.0           BiocStyle_2.41.0
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] pROC_1.19.0.1         gridExtra_2.3         rlang_1.2.0
    ##  [4] magrittr_2.0.5        clue_0.3-68           GetoptLong_1.1.1
    ##  [7] otel_0.2.0            compiler_4.6.0        png_0.1-9
    ## [10] vctrs_0.7.3           reshape2_1.4.5        combinat_0.0-8
    ## [13] stringr_1.6.0         shape_1.4.6.1         pkgconfig_2.0.3
    ## [16] crayon_1.5.3          fastmap_1.2.0         magick_2.9.1
    ## [19] XVector_0.53.0        labeling_0.4.3        rmarkdown_2.31
    ## [22] xfun_0.57             WriteXLS_6.8.0        cachem_1.1.0
    ## [25] jsonlite_2.0.0        DelayedArray_0.39.1   cluster_2.1.8.2
    ## [28] irlba_2.3.7           parallel_4.6.0        R6_2.6.1
    ## [31] bslib_0.10.0          stringi_1.8.7         RColorBrewer_1.1-3
    ## [34] MCMCprecision_0.4.2   jquerylib_0.1.4       Rcpp_1.1.1-1.1
    ## [37] iterators_1.0.14      knitr_1.51            FNN_1.1.4.1
    ## [40] tidyselect_1.2.1      abind_1.4-8           yaml_2.3.12
    ## [43] viridis_0.6.5         enrichR_3.4           doParallel_1.0.17
    ## [46] codetools_0.2-20      curl_7.1.0            lattice_0.22-9
    ## [49] tibble_3.3.1          plyr_1.8.9            withr_3.0.2
    ## [52] S7_0.2.2              evaluate_1.0.5        Rtsne_0.17
    ## [55] RcppEigen_0.3.4.0.2   circlize_0.4.18       pillar_1.11.1
    ## [58] BiocManager_1.30.27   foreach_1.5.2         dbscan_1.2.4
    ## [61] ggplot2_4.0.3         scales_1.4.0          glue_1.8.1
    ## [64] maketools_1.3.2       tools_4.6.0           dendextend_1.19.1
    ## [67] sys_3.4.3             data.table_1.18.4     buildtools_1.0.0
    ## [70] Cairo_1.7-0           grid_4.6.0            colorspace_2.1-2
    ## [73] cli_3.6.6             S4Arrays_1.13.0       viridisLite_0.4.3
    ## [76] ComplexHeatmap_2.29.0 ggdendro_0.2.0        dplyr_1.2.1
    ## [79] uwot_0.2.4            gtable_0.3.6          sass_0.4.10
    ## [82] digest_0.6.39         SparseArray_1.13.2    ggrepel_0.9.8
    ## [85] rjson_0.2.23          farver_2.1.2          htmltools_0.5.9
    ## [88] lifecycle_1.0.5       httr_1.4.8            GlobalOptions_0.1.4
    ## [91] MASS_7.3-65