knitr::opts_chunk$set(echo = TRUE)
Vignette on Larsson 2019 data can be found here, which has allelic single-cell RNA-seq with 4 cell states.
The airpart package takes input data of counts from each of two alleles across genes (rows) and cells (columns) from a single-cell RNA-seq experiment.
For demonstration in the package vignette, we will simulate some data
using makeSimulatedData
function provided within the airpart
package. We will examine the allelic counts and then perform QC steps
before analyzing the data for allelic imbalance across groups of cells.
The simulated example dataset has 3 gene clusters with differential allelic imbalance (DAI):
Below we specify a number of simulation settings as arguments to the simulation function:
theta
in rbetabinom
is 20 (higher is
less dispersion)library(airpart) suppressPackageStartupMessages(library(SingleCellExperiment)) p.vec <- rep(c(0.2, 0.8, 0.5, 0.5, 0.7, 0.9), each = 2) set.seed(2021) sce <- makeSimulatedData( mu1 = 2, mu2 = 10, nct = 4, n = 20, ngenecl = 25, theta = 20, ncl = 3, p.vec = p.vec )
unique(rowData(sce)) # the true underlying allelic ratios table(sce$x) # counts of each cell type assays(sce)[["a1"]][1:5, 1:5] # allelic counts for the effect allele
In summary, airpart expects a SingleCellExperiment object with:
x
in the colData(sce)
a1
and a2
The allelic ratio is calculated as a1 / (a1 + a2)
.
Note: We assume that the cell types have been either provided by the
experiment, or identified based on total count. We assume the allelic
ratio was not used in determining the cell groupings in x
.
assayNames(sce) sce$x
In the preprocess
step, we add a pseudo-count for gene clustering
and visualization (not used for inference later on allelic imbalance
though, which uses original allelic counts). From the heatmap, we can
clearly identify the three gene clusters (across rows), and we also
see cell type differences (across columns). Within each cell type,
there are some cells with noisier estimates (lower total count) than
others. Again, the allelic ratio tells us how much more of the a1
allele is expressed, with 1 indicating all of the expression coming
from the a1
allele and 0 indicating all of the expression coming
from the a2
allele.
sce <- preprocess(sce) makeHeatmap(sce)
We recommend both QC on cells and on genes. We begin with cell allelic
ratio quality control. For details on these metrics, see ?cellQC
.
cellQCmetrics <- cellQC(sce, mad_detected = 4) cellQCmetrics
Now define cell filtering automatically or users can manually filter
out based on sum
,detected
and spikePercent
.
keep_cell <- ( cellQCmetrics$filter_sum | # sufficient features (genes) cellQCmetrics$filter_detected | # sufficient molecules counted # sufficient features expressed compared to spike genes, # high quality cells cellQCmetrics$filter_spike ) sce <- sce[, keep_cell]
We also recommend QC on genes for allelic ratio analysis. Note that we
require genes to be expressed in at least 25% of cells within each
cell type and the genes to have high allelic imbalance variation
across cell types. The following code chunk is recommended (not
evaluated here though). If users want to estimate homogeneous cell
type allelic imbalance, they can set sd = 0
and examine the below
summary step to find interesting gene clusters with weighted mean
deviating from 0.5.
featureQCmetric <- featureQC(sce) keep_feature <- (featureQCmetric$filter_celltype & featureQCmetric$filter_sd & featureQCmetric$filter_spike) sce <- sce[keep_feature, ]
airpart provides a function to cluster genes by their allelic
imbalance profile across cells (not using cell grouping information,
e.g. sce$x
). We then recommend providing genes
within a cluster to the partition function. Clustering genes increases
power for detecting cell type partitions, and improves speed as it
reduces the number of times the partition must be estimated.
We provide two methods for gene clustering.
Gaussian mixture modeling is the default method for gene
clustering. The scatter plot is shown based on top 2 PCs of the
smoothed allelic ratio data. The argument plot=FALSE
can be used to
avoid showing the plot.
sce <- geneCluster(sce, G = 1:4) metadata(sce)$geneCluster
sce.hc <- geneCluster(sce, method = "hierarchical") metadata(sce.hc)$geneCluster
In this simulated dataset case, the clustering is very similar, but on allelic scRNA-seq datasets, we have found improved clustering with the Gaussian mixture model approach (more similar genes within cluster, based on visual inspection of PCA plot and of allelic ratio heatmaps).
We first quickly look at the weighted mean of allelic ratio for each gene cluster. From this step we will identify the interesting gene clusters. The mean is calculated, weighting the information from each gene x cell element of the matrices by the total count.
summary <- summaryAllelicRatio(sce) summary
The following step is a complement of the QC on genes step. We recommend users only run airpart when the largest ordered allelic ratio difference > 0.05 for speed concerns. We find that the allelic ratio of most of the gene clusters in such cases (small absolute allelic ratio differences) won't provide enough evidence to detect differential allelic imbalance.
sapply(1:length(summary), function(i) { inst <- summary[[i]] inst_order <- inst[order(inst$weighted.mean), ] max(diff(inst_order$weighted.mean)) > 0.05 })
We recommend examining the experiment-wide beta-binomial over-dispersion, which helps to inform whether to use a binomial likelihood or a nonparametric approach to partitioning the cell types by allelic imbalance.
We focus on the first gene cluster (if a gene cluster is not provided,
estDisp
will choose the largest cluster).
The blue trend line gives the typical values of over-dispersion across all the genes in the cluster, and across all the cell types (accounting for differences across the cell types in the expected ratio).
estDisp(sce, genecluster = 1)
airpart offers a method for partitioning cell types using the generalized fused lasso with binomial likelihood, as implemented in the smurf package. Cell types are merged based on their similarity of allelic ratios, accounting for excess variance on the ratio from low counts. The penalization is determined using deviance on held-out data, with a 1 SE cross-validation rule for favoring smaller models (more fused cell types).
The fusion step can also taken into account both cell-level and
gene-level baseline effects, through the use of a formula
(see ?fusedLasso
for example).
sce_sub <- fusedLasso(sce, model = "binomial", genecluster = 1, ncores = 1, niter = 2 )
The partition groups and the penalty $\lambda$ from the fused lasso are stored in the metadata:
knitr::kable(metadata(sce_sub)$partition, row.names = FALSE)
metadata(sce_sub)$lambda
Above, ncores
is the number of CPU used for parallelization. As a
guide, one can specify niter=5
when the cts
weighted allelic ratio
difference is smaller than 0.1, in order to provide additional
estimator robustness.
If you run niter
> 1, you can use our consensus partition function
to derive the final partition. This function makes use of ensemble
consensus clustering via the clue package.
sce_sub <- consensusPart(sce_sub) knitr::kable(metadata(sce_sub)$partition, row.names = FALSE)
An alternative to the fused lasso with binomial likelihood is an
extension we have implemented wherein all pairs cell types are
compared with Mann-Whitney-Wilcoxon rank sum tests. In practice, we
find that when the allelic counts deviates strongly from a binomial
(e.g. large over-dispersion, small values of theta
), the wilcoxExt
function can offer improved performance, in terms of recovery of the
true partition of cell types by allelic imbalance. The partition is
decided based on a loss function motivated by the Bayesian Information
Criteria.
thrs <- 10^seq(from = -2, to = -0.4, by = 0.2) sce_sub_w <- wilcoxExt(sce, genecluster = 1, threshold = thrs) knitr::kable(metadata(sce_sub_w)$partition, row.names = FALSE) metadata(sce_sub_w)$threshold
After airpart determines a partition of cell types either by the fused
lasso with binomial likelihood or the nonparametric approach described
above, it uses those fused lasso estimates or weighted means as the
center of a Cauchy prior for posterior estimation of allelic ratios
per cell type and per gene. Posterior mean and credible intervals are
provided. The posterior inference makes use of a beta-binomial
likelihood, and a moderated estimate of the over-dispersion. The prior
from the partition and the moderated estimate of over-dispersion are
provided to the apeglm
function from the Bioconductor package of the
same name.
Note that the estimates and credible intervals are not equal for cell types in the same partition and for genes, because in this step we re-estimate the conditional cell type means per cell type (not per partition) and account for each gene's moderated estimate of over-dispersion.
sce_sub <- allelicRatio(sce_sub, DAItest = TRUE) makeForest(sce_sub, showtext = TRUE)
Allelic ratio estimates (ar
) as well as svalue
and credible
interval (lower
and upper
) are stored in rowData
. Can use extractResult
function to derive them.
genepoi <- paste0("gene", seq_len(5)) ar <- extractResult(sce_sub) knitr::kable(ar[genepoi,]) makeStep(sce_sub[genepoi,])
To derive statistical inference of allelic imbalance(AI), we suggest a low aggregate probability of false-sign-or-small (FSOS) events (s-value < .005) or examine credible intervals not overlapping an allelic ratio of 0.5. Here all selected 5 genes demonstrated AI on each cell type.
s <- extractResult(sce_sub, "svalue") apply(s[genepoi,],2, function(s){s<0.005})
To derive statistical inference of dynamic AI(DAI), raw p values from likelihood ratio test(LRT) and Benjamini-Hochberg (BH) corrected p value are stored in p.value
and adj.p.value
, respectively. Here all 25 genes demonstrated DAI across cells.
adj.p <- mcols(sce_sub)$adj.p.value adj.p < 0.05
To demonstrate showing partition results on a heatmap, let's make a more complex simulation, with 8 cell types, in 3 true groups by allelic ratio. In the code below, we construct the more complex simulation, run preprocessing, and examine the allelic ratio heatmap.
nct <- 8 p.vec <- (rep(c( -3, 0, -3, 3, rep(0, nct / 2), 2, 3, 4, 2 ), each = 2) + 5) / 10 sce <- makeSimulatedData( mu1 = 2, mu2 = 10, nct = nct, n = 30, ngenecl = 50, theta = 20, ncl = 3, p.vec = p.vec ) sce <- preprocess(sce) cellQCmetrics <- cellQC(sce, mad_detected = 4) keep_cell <- ( cellQCmetrics$filter_sum | # sufficient features (genes) cellQCmetrics$filter_detected | # sufficient molecules counted # sufficient features expressed compared to spike genes, # high quality cells cellQCmetrics$filter_spike ) sce <- sce[, keep_cell] featureQCmetric <- featureQC(sce) keep_feature <- (featureQCmetric$filter_celltype & featureQCmetric$filter_sd & featureQCmetric$filter_spike) sce <- sce[keep_feature, ] makeHeatmap(sce)
We can then perform gene clustering:
sce <- geneCluster(sce, G = 1:4) table(mcols(sce)$cluster)
We check for experiment-wide beta-binomial over-dispersion.
Note that larger theta
(y-axis) corresponds to less
over-dispersion.
We focus on the first gene cluster (if a gene cluster is not provided,
estDisp
will choose the largest cluster).
estDisp(sce, genecluster = 1)
We identify an interesting gene cluster and run the fused lasso.
sce_sub <- fusedLasso(sce, model = "binomial", genecluster = 1, ncores = 1 )
knitr::kable(metadata(sce_sub)$partition, row.names = FALSE)
Next we estimate allelic ratios per cell type and per gene, with credible intervals. For demonstration, we subset to the first 10 genes.
sce_sub2 <- sce_sub[1:10, ] sce_sub2 <- allelicRatio(sce_sub2)
We plot all cell types together, but one can set ctpoi=c(1,3,7)
to
limit the cell types to be plotted when there are too many cell
types. And one can set genepoi=c(1,3,7)
or genepoi=c("gene1","gene3","gene7")
to only plot selected genes.
makeForest(sce_sub2) ar <- extractResult(sce_sub2) knitr::kable(ar)
A violin plot with posterior mean allelic ratios (one estimate per gene) on the y-axis:
makeViolin(sce_sub2)
Finally, a heatmap as before, but now with the cell types grouped according to the partition:
makeHeatmap(sce_sub2)
The heatmap can also be shown ordered by cell type.
makeHeatmap(sce_sub2, order_by_group = FALSE)
sessionInfo()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.