scran 1.16.0
Single-cell RNA sequencing (scRNA-seq) is a widely used technique for profiling gene expression in individual cells. This allows molecular biology to be studied at a resolution that cannot be matched by bulk sequencing of cell populations. The scran package implements methods to perform low-level processing of scRNA-seq data, including cell cycle phase assignment, scaling normalization, variance modelling and testing for corrrelated genes. This vignette provides brief descriptions of these methods and some toy examples to demonstrate their use.
Note: A more comprehensive description of the use of scran (along with other packages) in a scRNA-seq analysis workflow is available at https://osca.bioconductor.org.
We start off with a count matrix where each row is a gene and each column is a cell. These can be obtained by mapping read sequences to a reference genome, and then counting the number of reads mapped to the exons of each gene. (See, for example, the Rsubread package to do both of these tasks.) Alternatively, pseudo-alignment methods can be used to quantify the abundance of each transcript in each cell. For simplicity, we will pull out an existing dataset from the scRNAseq package.
library(scRNAseq)
sce <- GrunPancreasData()
sce
## class: SingleCellExperiment
## dim: 20064 1728
## metadata(0):
## assays(1): counts
## rownames(20064): A1BG-AS1__chr19 A1BG__chr19 ... ZZEF1__chr17
## ZZZ3__chr1
## rowData names(2): symbol chr
## colnames(1728): D2ex_1 D2ex_2 ... D17TGFB_95 D17TGFB_96
## colData names(2): donor sample
## reducedDimNames(0):
## altExpNames(1): ERCC
This particular dataset is taken from a study of the human pancreas with the CEL-seq protocol (Grun et al. 2016).
It is provided as a SingleCellExperiment
object (from the SingleCellExperiment package), which contains the raw data and various annotations.
We perform some cursory quality control to remove cells with low total counts or high spike-in percentages:
library(scater)
qcstats <- perCellQCMetrics(sce)
qcfilter <- quickPerCellQC(qcstats, percent_subsets="altexps_ERCC_percent")
sce <- sce[,!qcfilter$discard]
summary(qcfilter$discard)
## Mode FALSE TRUE
## logical 1291 437
Cell-specific biases are normalized using the computeSumFactors
method, which implements the deconvolution strategy for scaling normalization (A. T. Lun, Bach, and Marioni 2016).
This computes size factors that are used to scale the counts in each cell.
The assumption is that most genes are not differentially expressed (DE) between cells, such that any differences in expression across the majority of genes represents some technical bias that should be removed.
library(scran)
clusters <- quickCluster(sce)
sce <- computeSumFactors(sce, clusters=clusters)
summary(sizeFactors(sce))
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.006722 0.442629 0.801312 1.000000 1.295809 9.227575
For larger data sets, clustering should be performed with the quickCluster
function before normalization.
Briefly, cells are grouped into clusters of similar expression; normalization is applied within each cluster to compute size factors for each cell; and the factors are rescaled by normalization between clusters.
This reduces the risk of violating the above assumption when many genes are DE between clusters in a heterogeneous population.
Note that computeSumFactors
will automatically remove low-abundance genes, which provides some protection against zero or negative size factor estimates.
We also assume that quality control on the cells has already been performed, as low-quality cells with few expressed genes can often have negative size factor estimates.
An alternative approach is to normalize based on the spike-in counts (Lun et al. 2017).
The idea is that the same quantity of spike-in RNA was added to each cell prior to library preparation.
Size factors are computed to scale the counts such that the total coverage of the spike-in transcripts is equal across cells.
The main practical difference is that spike-in normalization preserves differences in total RNA content between cells, whereas computeSumFactors
and other non-DE methods do not.
sce2 <- computeSpikeFactors(sce, "ERCC")
summary(sizeFactors(sce2))
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.01041 0.57760 0.88667 1.00000 1.27679 7.43466
Normalized expression values are calculated using the logNormCounts()
method from scater (McCarthy et al. 2017).
This will use the deconvolution size factors for the endogenous genes, and the spike-in-based size factors for the spike-in transcripts.
Each expression value can be interpreted as a log-transformed “normalized count”, and can be used in downstream applications like clustering or dimensionality reduction.
sce <- logNormCounts(sce)
We identify genes that drive biological heterogeneity in the data set by modelling the per-gene variance. The aim is use a subset of highly variable genes in downstream analyses like clustering, to improve resolution by removing genes driven by technical noise. We decompose the total variance of each gene into its biological and technical components by fitting a trend to the endogenous variances (A. T. Lun, McCarthy, and Marioni 2016). The fitted value of the trend is used as an estimate of the technical component, and we subtract the fitted value from the total variance to obtain the biological component for each gene.
dec <- modelGeneVar(sce)
plot(dec$mean, dec$total, xlab="Mean log-expression", ylab="Variance")
curve(metadata(dec)$trend(x), col="blue", add=TRUE)
If we have spike-ins, we can use them to fit the trend instead. This provides a more direct estimate of the technical variance and avoids assuming that most genes do not exhibit biological variaility.
dec2 <- modelGeneVarWithSpikes(sce, 'ERCC')
plot(dec2$mean, dec2$total, xlab="Mean log-expression", ylab="Variance")
points(metadata(dec2)$mean, metadata(dec2)$var, col="red")
curve(metadata(dec2)$trend(x), col="blue", add=TRUE)
If we have some uninteresting factors of variation, we can block on these using block=
.
This will perform the trend fitting and decomposition within each block before combining the statistics across blocks for output.
Statistics for each individual block can also be extracted for further inspection.
dec3 <- modelGeneVar(sce, block=sce$donor)
per.block <- dec3$per.block
par(mfrow=c(3, 2))
for (i in seq_along(per.block)) {
decX <- per.block[[i]]
plot(decX$mean, decX$total, xlab="Mean log-expression",
ylab="Variance", main=names(per.block)[i])
curve(metadata(decX)$trend(x), col="blue", add=TRUE)
}
We can then extract some top genes for use in downstream procedures.
This is usually done by passing the selected subset of genes to the subset.row
argument (or equivalent) in the desired downstream function, as shown below for the PCA step.
# Get the top 10% of genes.
top.hvgs <- getTopHVGs(dec, prop=0.1)
# Get the top 2000 genes.
top.hvgs2 <- getTopHVGs(dec, n=2000)
# Get all genes with positive biological components.
top.hvgs3 <- getTopHVGs(dec, var.threshold=0)
# Get all genes with FDR below 5%.
top.hvgs4 <- getTopHVGs(dec, fdr.threshold=0.05)
# Running the PCA with the 10% of HVGs.
sce <- runPCA(sce, subset_row=top.hvgs)
reducedDimNames(sce)
## [1] "PCA"
Principal components analysis is commonly performed to denoise and compact the data prior to downstream analysis.
A common question is how many PCs to retain; more PCs will capture more biological signal at the cost of retaining more noise and requiring more computational work.
One approach to choosing the number of PCs is to use the technical component estimates to determine the proportion of variance that should be retained.
This is implemented in denoisePCA()
, which takes the estimates returned by modelGeneVar()
or friends.
(For greater accuracy, we use the fit with the spikes; we also subset to only the top HVGs to remove noise.)
sced <- denoisePCA(sce, dec2, subset.row=getTopHVGs(dec2, prop=0.1))
ncol(reducedDim(sced, "PCA"))
## [1] 50
Another approach is based on the assumption that each subpopulation should be separated from each other on a different axis of variation. Thus, we choose the number of PCs that is not less than the number of subpopulations (which are unknown, of course, so we use the number of clusters as a proxy). It is then a simple to subset the dimensionality reduction result to the desired number of PCs.
output <- getClusteredPCs(reducedDim(sce))
npcs <- metadata(output)$chosen
reducedDim(sce, "PCAsub") <- reducedDim(sce, "PCA")[,1:npcs,drop=FALSE]
npcs
## [1] 14
Clustering of scRNA-seq data is commonly performed with graph-based methods due to their relative scalability and robustness.
scran provides several graph construction methods based on shared nearest neighbors (Xu and Su 2015) through the buildSNNGraph()
function.
This is most commonly generated from the selected PCs, after which methods from the igraph package can be used to identify clusters.
# In this case, using the PCs that we chose from getClusteredPCs().
g <- buildSNNGraph(sce, use.dimred="PCAsub")
cluster <- igraph::cluster_walktrap(g)$membership
sce$cluster <- factor(cluster)
table(sce$cluster)
##
## 1 2 3 4 5 6 7 8 9 10 11 12
## 79 285 64 170 166 164 124 32 57 63 63 24
By default, buildSNNGraph()
uses the mode of shared neighbor weighting described by Xu and Su (2015), but other weighting methods (e.g., the Jaccard index) are also available by setting type=
.
An unweighted \(k\)-nearest neighbor graph can also be constructed with buildKNNGraph()
.
We can then use methods from scater to visualize this on a \(t\)-SNE plot, as shown below.
sce <- runTSNE(sce, dimred="PCAsub")
plotTSNE(sce, colour_by="cluster", text_by="cluster")
For graph-based methods, another diagnostic is to examine the ratio of observed to expected edge weights for each pair of clusters (closely related to the modularity score used in many cluster_*
functions).
We would usually expect to see high observed weights between cells in the same cluster with minimal weights between clusters, indicating that the clusters are well-separated.
Off-diagonal entries indicate that some clusters are closely related, which is useful to know for checking that they are annotated consistently.
ratio <- clusterModularity(g, cluster, as.ratio=TRUE)
library(pheatmap)
pheatmap(log10(ratio+1), cluster_cols=FALSE, cluster_rows=FALSE,
col=rev(heat.colors(100)))
A more general diagnostic involves bootstrapping to determine the stability of the partitions between clusters.
Given a clustering function, the bootstrapCluster()
function uses bootstrapping to compute the co-assignment probability for each pair of original clusters, i.e., the probability that one randomly chosen cell from each cluster is assigned to the same cluster in the bootstrap replicate .
Larger probabilities indicate that the separation between those clusters is unstable to the extent that it is sensitive to sampling noise, and thus should not be used for downstream inferences.
ass.prob <- bootstrapCluster(sce, FUN=function(x) {
g <- buildSNNGraph(x, use.dimred="PCAsub")
igraph::cluster_walktrap(g)$membership
}, clusters=sce$cluster)
pheatmap(ass.prob, cluster_cols=FALSE, cluster_rows=FALSE,
col=colorRampPalette(c("white", "blue"))(100))
If necessary, further subclustering can be performed conveniently using the quickSubCluster()
wrapper function.
This splits the input SingleCellExperiment
into several smaller objects containing cells from each cluster and performs another round of clustering within that cluster, using a freshly identified set of HVGs to improve resolution for internal structure.
subout <- quickSubCluster(sce, sce$cluster)
table(subout[[1]]$subcluster) # subclusters for cluster 1
##
## 1.1 1.2
## 38 41
The findMarkers()
wrapper function will perform some simple differential expression tests between pairs of clusters to identify potential marker genes for each cluster.
For each cluster, we perform \(t\)-tests to identify genes that are DE in each cluster compared to at least one other cluster.
All pairwise tests are combined into a single ranking by simply taking the top genes from each pairwise comparison.
For example, if we take all genes with Top <= 5
, this is equivalent to the union of the top 5 genes from each pairwise comparison.
This aims to provide a set of genes that is guaranteed to be able to distinguish the chosen cluster from all others.
markers <- findMarkers(sce, sce$cluster)
markers[[1]][,1:3]
## DataFrame with 20064 rows and 3 columns
## Top p.value FDR
## <integer> <numeric> <numeric>
## CPE__chr4 1 1.72176e-62 1.43939e-59
## KCNQ1OT1__chr11 1 2.07621e-61 1.60220e-58
## LOC100131257__chr7 1 2.66948e-43 7.87654e-41
## PGM5P2__chr9 1 2.67376e-51 1.27730e-48
## SCG2__chr2 1 1.41986e-104 2.84880e-100
## ... ... ... ...
## TLX1NB__chr10 19918 1 1
## TNFRSF17__chr16 19946 1 1
## TSPAN32__chr11 19973 1 1
## WFDC11__chr20 20015 1 1
## XKR7__chr20 20025 1 1
We can modify the tests by passing a variety of arguments to findMarkers()
.
For example, the code below will perform Wilcoxon tests instead of \(t\)-tests;
only identify genes that are upregulated in the target cluster compared to each other cluster;
and require a minimum log2-fold change of 1 to be considered significant.
wmarkers <- findMarkers(sce, sce$cluster,
test.type="wilcox", direction="up", lfc=1)
wmarkers[[1]][,1:3]
## DataFrame with 20064 rows and 3 columns
## Top p.value FDR
## <integer> <numeric> <numeric>
## FBLIM1__chr1 1 7.55507e-30 2.16550e-26
## KCNQ1OT1__chr11 1 1.30379e-37 1.51912e-33
## PGM5P2__chr9 1 1.46543e-35 9.80080e-32
## UGDH-AS1__chr4 1 1.51428e-37 1.51912e-33
## LOC100131257__chr7 2 7.66839e-33 3.07717e-29
## ... ... ... ...
## TLX1NB__chr10 19918 1 1
## TNFRSF17__chr16 19946 1 1
## TSPAN32__chr11 19973 1 1
## WFDC11__chr20 20015 1 1
## XKR7__chr20 20025 1 1
We can also modify how the statistics are combined across pairwise comparisons.
Setting pval.type="all"
requires a gene to be DE between each cluster and every other cluster (rather than any other cluster, as is the default with pval.type="any"
).
This is a more stringent definition that can yield a more focused set of markers but may also fail to detect any markers in the presence of overclustering.
markers <- findMarkers(sce, sce$cluster, pval.type="all")
markers[[1]][,1:2]
## DataFrame with 20064 rows and 2 columns
## p.value FDR
## <numeric> <numeric>
## UGDH-AS1__chr4 8.36195e-20 1.67774e-15
## KCNQ1OT1__chr11 2.72694e-19 2.42652e-15
## LOC100131257__chr7 3.62817e-19 2.42652e-15
## TFDP2__chr3 5.98639e-19 3.00277e-15
## LOC643406__chr20 1.09251e-18 4.38402e-15
## ... ... ...
## ZP1__chr11 1 1
## ZP3__chr7 1 1
## ZPBP__chr7 1 1
## ZSCAN1__chr19 1 1
## ZSCAN20__chr1 1 1
The SingleCellExperiment
object can be easily converted into other formats using the convertTo
method.
This allows analyses to be performed using other pipelines and packages.
For example, if DE analyses were to be performed using edgeR, the count data in sce
could be used to construct a DGEList
.
y <- convertTo(sce, type="edgeR")
By default, rows corresponding to spike-in transcripts are dropped when get.spikes=FALSE
.
As such, the rows of y
may not correspond directly to the rows of sce
– users should match by row name to ensure correct cross-referencing between objects.
Normalization factors are also automatically computed from the size factors.
The same conversion strategy roughly applies to the other supported formats.
DE analyses can be performed using DESeq2 by converting the object to a DESeqDataSet
.
Cells can be ordered on pseudotime with monocle by converting the object to a CellDataSet
(in this case, normalized unlogged expression values are stored).
Further information can be obtained by examining the documentation for each function (e.g., ?convertTo
);
reading the Orchestrating Single Cell Analysis book;
or asking for help on the Bioconductor support site (please read the posting guide beforehand).
sessionInfo()
## R version 4.0.0 (2020-04-24)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 18.04.4 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.11-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.11-bioc/R/lib/libRlapack.so
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=C
## [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
##
## attached base packages:
## [1] parallel stats4 stats graphics grDevices utils datasets
## [8] methods base
##
## other attached packages:
## [1] pheatmap_1.0.12 scater_1.16.0
## [3] ggplot2_3.3.0 scRNAseq_2.1.7
## [5] BiocParallel_1.22.0 scran_1.16.0
## [7] SingleCellExperiment_1.10.0 SummarizedExperiment_1.18.0
## [9] DelayedArray_0.14.0 matrixStats_0.56.0
## [11] Biobase_2.48.0 GenomicRanges_1.40.0
## [13] GenomeInfoDb_1.24.0 IRanges_2.22.0
## [15] S4Vectors_0.26.0 BiocGenerics_0.34.0
## [17] knitr_1.28 BiocStyle_2.16.0
##
## loaded via a namespace (and not attached):
## [1] bitops_1.0-6 bit64_0.9-7
## [3] RColorBrewer_1.1-2 httr_1.4.1
## [5] tools_4.0.0 R6_2.4.1
## [7] irlba_2.3.3 vipor_0.4.5
## [9] DBI_1.1.0 colorspace_1.4-1
## [11] withr_2.2.0 tidyselect_1.0.0
## [13] gridExtra_2.3 bit_1.1-15.2
## [15] curl_4.3 compiler_4.0.0
## [17] BiocNeighbors_1.6.0 labeling_0.3
## [19] bookdown_0.18 scales_1.1.0
## [21] rappdirs_0.3.1 stringr_1.4.0
## [23] digest_0.6.25 rmarkdown_2.1
## [25] XVector_0.28.0 pkgconfig_2.0.3
## [27] htmltools_0.4.0 fastmap_1.0.1
## [29] dbplyr_1.4.3 limma_3.44.0
## [31] rlang_0.4.5 RSQLite_2.2.0
## [33] shiny_1.4.0.2 DelayedMatrixStats_1.10.0
## [35] farver_2.0.3 dplyr_0.8.5
## [37] RCurl_1.98-1.2 magrittr_1.5
## [39] BiocSingular_1.4.0 GenomeInfoDbData_1.2.3
## [41] Matrix_1.2-18 Rcpp_1.0.4.6
## [43] ggbeeswarm_0.6.0 munsell_0.5.0
## [45] viridis_0.5.1 lifecycle_0.2.0
## [47] stringi_1.4.6 yaml_2.2.1
## [49] edgeR_3.30.0 zlibbioc_1.34.0
## [51] Rtsne_0.15 BiocFileCache_1.12.0
## [53] AnnotationHub_2.20.0 grid_4.0.0
## [55] blob_1.2.1 promises_1.1.0
## [57] dqrng_0.2.1 ExperimentHub_1.14.0
## [59] crayon_1.3.4 lattice_0.20-41
## [61] cowplot_1.0.0 magick_2.3
## [63] locfit_1.5-9.4 pillar_1.4.3
## [65] igraph_1.2.5 glue_1.4.0
## [67] BiocVersion_3.11.1 evaluate_0.14
## [69] BiocManager_1.30.10 httpuv_1.5.2
## [71] vctrs_0.2.4 gtable_0.3.0
## [73] purrr_0.3.4 assertthat_0.2.1
## [75] xfun_0.13 mime_0.9
## [77] rsvd_1.0.3 xtable_1.8-4
## [79] later_1.0.0 viridisLite_0.3.0
## [81] tibble_3.0.1 AnnotationDbi_1.50.0
## [83] beeswarm_0.2.3 memoise_1.1.0
## [85] statmod_1.4.34 ellipsis_0.3.0
## [87] interactiveDisplayBase_1.26.0
Grun, D., M. J. Muraro, J. C. Boisset, K. Wiebrands, A. Lyubimova, G. Dharmadhikari, M. van den Born, et al. 2016. “De Novo Prediction of Stem Cell Identity using Single-Cell Transcriptome Data.” Cell Stem Cell 19 (2):266–77.
Lun, A. T. L., F. J. Calero-Nieto, L. Haim-Vilmovsky, B. Gottgens, and J. C. Marioni. 2017. “Assessing the reliability of spike-in normalization for analyses of single-cell RNA sequencing data.” Genome Res. 27 (11):1795–1806.
Lun, A. T., K. Bach, and J. C. Marioni. 2016. “Pooling across cells to normalize single-cell RNA sequencing data with many zero counts.” Genome Biol. 17 (April):75.
Lun, A. T., D. J. McCarthy, and J. C. Marioni. 2016. “A step-by-step workflow for low-level analysis of single-cell RNA-seq data with Bioconductor.” F1000Res 5:2122.
McCarthy, D. J., K. R. Campbell, A. T. Lun, and Q. F. Wills. 2017. “Scater: pre-processing, quality control, normalization and visualization of single-cell RNA-seq data in R.” Bioinformatics 33 (8):1179–86.
Xu, C., and Z. Su. 2015. “Identification of cell types from single-cell transcriptomes using a novel clustering method.” Bioinformatics 31 (12):1974–80.