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. Celda uses Dirichlet-multinomial distributions to model cells and genes so no additional normalization is required for single-cell counts.
In this vignette we will demonstrate how to use Celda to perform cell and feature clustering with simulated data.
The package can be loaded using the library
command.
library(celda)
Complete list of help files are accessible using the help command with a 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/compbiomed/celda. To ask questions about running Celda, visit our Google group at https://groups.google.com/forum/#!forum/celda-list.
library(celda)
Celda will take a matrix of counts where each row is a feature, each cell is a column, 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, and the G parameter determines the number of features to be simulated.
sim_counts <- simulateCells("celda_CG", K = 5, L = 10, S = 5, G = 100)
The counts
variable contains the counts matrix.
The dimensions of counts matrix:
dim(sim_counts$counts)
The z
variable contains the cluster labels for each cell.
Here is the number of cells in each subpopulation:
table(sim_counts$z)
The y
variable contains the feature module assignment for each feature.
Here is the number of features in each feature module:
table(sim_counts$y)
The sample.label
is used to denote the sample from which each cell was derived. In this simulated dataset, we have 5 samples:
table(sim_counts$sample.label)
There are currently three models within this package: celda_C
will cluster cells, celda_G
will cluster features, and celda_CG
will simultaneously cluster cells and features. Within the function 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.
celda.model <- celda_CG(counts = sim_counts$counts, K = 5, L = 10, verbose = FALSE)
Here is a comparison between the true cluster labels and the estimated cluster labels, which can be found within the z
and y
objects.
table(celda.model$z, sim_counts$z) table(celda.model$y, sim_counts$y)
Celda can also be thought of as performing matrix factorization of the original counts matrix into smaller matrices that describe the contribution of each feature in each module, each module in each cell population, or each cell population in each sample. Each of these following matrices can be viewed as raw counts, proportions, or posterior probabilities.
factorized <- factorizeMatrix(counts = sim_counts$counts, celda.mod = celda.model) names(factorized)
The cell
object contains each feature module's contribution to each cell subpopulation. Here, there are 10 feature modules to r ncol(factorized$proportions$cell)
cells.
dim(factorized$proportions$cell) head(factorized$proportions$cell[,1:3], 5)
The cell.population
contains each feature module's contribution to each of the cell states. Since K and L were set to be 5 and 10, there are 5 cell subpopulations to 10 feature modules.
cell.pop <- factorized$proportions$cell.population dim(cell.pop) head(cell.pop, 5)
The module
object contains each feature's contribution to the module it belongs.
dim(factorized$proportions$module) head(factorized$proportions$module, 5)
The top features in a feature module can be selected using the topRank
function on the module
matrix:
top.genes <- topRank(matrix = factorized$proportions$module, n = 10, threshold = NULL)
top.genes$names$L1
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.
celdaHeatmap(counts = sim_counts$counts, celda.mod = celda.model, nfeatures = 10)
Celda contains its own wrapper function for tSNE, celdaTsne
, which 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 a individual features, respectively:
tsne <- celdaTsne(counts = sim_counts$counts, celda.mod = celda.model)
plotDimReduceCluster(dim1 = tsne[,1], dim2 = tsne[,2], cluster = celda.model$z) plotDimReduceModule(dim1 = tsne[,1], dim2 = tsne[,2], celda.mod = celda.model, counts = sim_counts$counts, rescale = TRUE) plotDimReduceFeature(dim1 = tsne[,1], dim2 = tsne[,2], counts = sim_counts$counts, features = "Gene_1")
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(counts = sim_counts$counts, celda.mod = celda.model)
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(counts = sim_counts$counts, celda.mod = celda.model, feature.module = 1, top.cells = 100)
While celdaHeatmap
will plot a heatmap directly with a Celda object, the plotHeatmap
function is a more general heatmap function which takes a normalized expression matrix as the input. Simple normalization of the counts matrix can be performed with the normalizeCounts
function. For instance, if users want to display specific modules and cell populations, the feature.ix
and cells.ix
parameters can be used to select rows and columns out of the matrix.
genes = c(top.genes$names$L1, top.genes$names$L10) gene.ix = which(rownames(sim_counts$counts) %in% genes) norm.counts <- normalizeCounts(counts = sim_counts$counts, scale.fun = scale)
plotHeatmap(counts = norm.counts, z = celda.model$z, y = celda.model$y, feature.ix = gene.ix, show.names.feature = TRUE)
Celda employs the MAST package (McDavid A, 2018) for differential expression analysis of single-cell data. The differentialExpression
function can determine features that are differentially expressed in one cell subpopulation versus all other subpopulations, between two individual cell subpopulations, or between different groups of cell populations.
If you wish to compare one cell subpopulation compared to all others, leave c2 as NULL:
diffexp.clust1 <- differentialExpression(counts = sim_counts$counts, celda.mod = celda.model, c1 = 1, c2 = NULL) head(diffexp.clust1,5)
If you wish to compare two cell subpopulations, use both c1 and c2 parameters.
diffexp.clust1vs2 <- differentialExpression(counts = sim_counts$counts, celda.mod = celda.model, c1 = 1, c2 = 2) diffexp.clust1vs2 <- diffexp.clust1vs2[diffexp.clust1vs2$FDR < 0.05 & abs(diffexp.clust1vs2$Log2_FC) > 2,] head(diffexp.clust1vs2, 5)
The differentially expressed genes can be visualized further with a heatmap:
diffexp.gene.ix = which(rownames(sim_counts$counts) %in% diffexp.clust1vs2$Gene) norm.counts <- normalizeCounts(counts = sim_counts$counts, scale.fun = scale)
plotHeatmap(counts = norm.counts[,celda.model$z %in% c(1,2)], z = celda.model$z[celda.model$z %in% c(1,2)], y = celda.model$y, feature.ix = diffexp.gene.ix, show.names.feature = TRUE)
In the previous example, the best K and L 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. Additionally, performing multiple random starts for each combination of K and L will increase the chances of finding the most optimal solution. Celda is able to run multiple combinations of K and L with multiple chains in parallel via the celdaGridSearch
function.
Perplexity is a statistical measure of how well a probability model can predict new data. Lower perplexity indicates a better model. The resamplePerplexity
function "perturbs" the original counts matrix by resampling the counts of each cell according to its normalized probability distribution. Perplexity is calculated on the resampled matrix and the procedure is repeated resample
times. 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.
cgs <- celdaGridSearch(sim_counts$counts, params.test = list(K = 3:6, L = 8:11), cores = 1, model = "celda_CG", nchains = 2, max.iter = 100, best.only = TRUE)
"best.only=TRUE" indicates that only the chain with the best log likelihood will be returned for each K/L combination.
resamplePerplexity
calculates the perplexity of each model's cluster assignments, as well as resamplings of that count matrix. The result of this function can be visualized with plotGridSearchPerplexity
for determination of the optimal K/L values.
cgs <- resamplePerplexity(counts = sim_counts$counts, celda.list = cgs, resample = 5)
plotGridSearchPerplexity(celda.list = 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. 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 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 object to a single model.
celda.model = subsetCeldaList(celda.list = cgs, params = list(K = 5, L = 10))
If the "best.only" 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 use select a specific chain by specifying the index within the subsetCeldaList
function.
cgs <- celdaGridSearch(sim_counts$counts, params.test = list(K = 3:6, L = 8:11), cores = 1, model = "celda_CG", nchains = 2, max.iter = 100, best.only = FALSE) cgs <- resamplePerplexity(counts = sim_counts$counts, celda.list = cgs, resample = 2) cgs.K5.L10 = subsetCeldaList(celda.list = cgs, params = list(K = 5, L = 10)) celda.model1 <- selectBestModel(celda.list = cgs.K5.L10)
Celda also contains several utility functions for the users' convenience.
featureModuleLookup
can be used to look up the module a specific feature was clustered to.
featureModuleLookup(counts = sim_counts$counts, celda.mod = celda.model, feature = c("Gene_99"))
recodeClusterZ
and recodeClusterY
allows the user to recode the cell and feature cluster labels, respectively.
celda.model.z.recoded <- recodeClusterZ(celda.mod = celda.model, 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(celda.model$z, celda.model.z.recoded$z)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.