1 Introduction

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.

2 Setting up the data

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, though, we’ll just simulate some counts here from a negative binomial distribution.

ngenes <- 10000
ncells <- 200
mu <- 2^runif(ngenes, -1, 5)
gene.counts <- matrix(rnbinom(ngenes*ncells, mu=mu, size=10), nrow=ngenes)

We add some arbitrary Ensembl gene IDs to give the impression that this is real (mouse) data.

library(org.Mm.eg.db)
all.ensembl <- unique(toTable(org.Mm.egENSEMBL)$ensembl_id)
rownames(gene.counts) <- sample(all.ensembl, ngenes)

We also have a set of counts for spike-in transcripts. These are appended to the counts for the endogenous genes. In practice, the reads should have been mapped to the spike-in transcipts by including the spike-in sequences in the genome index.

nspikes <- 100
ncells <- 200
mu <- 2^runif(nspikes, -1, 5)
spike.counts <- matrix(rnbinom(nspikes*ncells, mu=mu, size=10), nrow=nspikes)
rownames(spike.counts) <- paste0("ERCC-", seq_len(nspikes))
all.counts <- rbind(gene.counts, spike.counts)

Finally, we construct a SingleCellExperiment object to store all of the data. We also indicate which rows correspond to spike-in transcripts. This is done through the calculateQCMetrics method from scater, which takes a named list of sets of control genes. We indicate which sets of controls are spike-ins using the setSpike setter function. (In this case, there is only one control set, so the process may seem more complicated than necessary. The usefulness of this setup becomes more obvious when multiple control sets are present.) This information can be easily extracted later on using the isSpike, spikes and whichSpike methods.

library(scran)
sce <- SingleCellExperiment(list(counts=all.counts))
isSpike(sce, "MySpike") <- grep("^ERCC", rownames(sce))

This is simulated data, so we assume that quality control has already been applied to remove low-quality cells or low-abundance genes. Check out the scater and cellity packages for more details. Also see the simpleSingleCell workflow where all these steps are used in real data analyses.

3 Cell cycle phase assignment

We use a pre-defined classifier to assign cells into their cell cycle phases (Scialdone et al. 2015). This classifier was constructed from a training data set by identifying pairs of genes where the difference in expression within each pair changed sign across phases. Thus, by examining the sign of the difference in test data, the phase to which the cell belongs can be identified. Classifiers for human and mouse data are provided with the package – for other systems, classifiers can be constructed from a training set using the sandbag function.

mm.pairs <- readRDS(system.file("exdata", "mouse_cycle_markers.rds", package="scran"))

The classification itself is done using the cyclone function, given the count data and the trained classifier. This yields a number of scores representing the consistency of the signs with each phase.

assigned <- cyclone(sce, pairs=mm.pairs)
head(assigned$scores)
##      G1     S   G2M
## 1 0.755 0.789 0.080
## 2 0.689 0.441 0.339
## 3 0.608 0.721 0.064
## 4 0.870 0.278 0.365
## 5 0.900 0.342 0.134
## 6 0.915 0.529 0.178

Cells are considered to be in G1 phase, if the G1 score is above 0.5 and the G2/M score is below 0.5; to be in G2/M phase, if the G2/M score is above 0.5 and the G1 score is below 0.5; to be in S phase, if both scores are below 0.5; and to be unknown, if both scores are above 0.5. Despite the availability of a S score, it tends to be more accurate to assign cells based on the G1 and G2/M scores only.

table(assigned$phases)
## 
##  G1 G2M   S 
## 189   2   9

Note that it is generally best practice to perform cell cycle phase assignment before filtering out low-abundance genes. This is because the lack of expression of particular genes can provide some information about the cell cycle.

4 Normalizing cell-specific biases

4.1 Based on the gene counts

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.

sce <- computeSumFactors(sce)
summary(sizeFactors(sce))
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.9792  0.9950  0.9997  1.0000  1.0044  1.0229

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.

larger.sce <- SingleCellExperiment(list(counts=cbind(all.counts, all.counts, all.counts)))
clusters <- quickCluster(larger.sce, min.size=100)
larger.sce <- computeSumFactors(larger.sce, cluster=clusters)

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.

4.2 Based on the spike-in counts

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)
summary(sizeFactors(sce2))
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.7959  0.9591  1.0058  1.0000  1.0425  1.2002

Even if we decide to use the deconvolution size factors, it is strongly recommended to compute a separate set of size factors for the spike-ins. This is because the spike-ins are not affected by total mRNA content. Using the deconvolution size factors will over-normalize the spike-in counts, whereas the spike-in size factors are more appropriate. To obtain the latter without overwriting the former, we set general.use=FALSE in our call to computeSpikeFactors. This means that the spike-in-based size factors will be computed and stored in the SingleCellExperiment object, but will only be used by the spike-in transcripts. (Obviously, if the spike-in size factors were already being used for normalization, e.g., in sce2, then this extra step is unnecessary.)

sce <- computeSpikeFactors(sce, general.use=FALSE)

4.3 Computing normalized expression values

Normalized expression values are calculated using the normalize 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 <- normalize(sce)

5 Variance modelling

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 first decompose the total variance of each gene into its biological and technical components (A. T. Lun, McCarthy, and Marioni 2016). We fit a mean-variance trend to the normalized log-expression values with trendVar. By default, this done using only the spike-in transcripts, as these should only exhibit technical noise.

fit <- trendVar(sce, parametric=TRUE)

The fitted value of the trend is used as an estimate of the technical component. We subtract the fitted value from the total variance to obtain the biological component for each gene. We can then extract some certain number of top genes for use in downstream procedures; or more generally, take all potentially interesting genes with positive biological components.

decomp <- decomposeVar(sce, fit)
top.hvgs <- order(decomp$bio, decreasing=TRUE)
head(decomp[top.hvgs,])
## DataFrame with 6 rows and 6 columns
##                                mean             total               bio
##                           <numeric>         <numeric>         <numeric>
## ENSMUSG00000029238 2.62012318703119 0.758774047413953   0.2562824391791
## ENSMUSG00000006576 2.03840180003387 0.808128950229099 0.222696784697487
## ENSMUSG00000116254 2.52676580707837 0.734233522574202 0.218249859186442
## ENSMUSG00000110372 1.86651846214376 0.814491846969669  0.20639730931401
## ENSMUSG00000025439 2.58466784377239 0.702617285291092 0.195074282346045
## ENSMUSG00000069306 2.64679064570593 0.691163309561168 0.192400465309585
##                                 tech              p.value                FDR
##                            <numeric>            <numeric>          <numeric>
## ENSMUSG00000029238 0.502491608234854 4.37412873404548e-06 0.0218706436702274
## ENSMUSG00000006576 0.585432165531612 0.000301140365144531  0.198003716724644
## ENSMUSG00000116254  0.51598366338776 8.13964071532194e-05  0.162792814306439
## ENSMUSG00000110372 0.608094537655659  0.00097822672593972  0.290261338319513
## ENSMUSG00000025439 0.507543002945047 0.000267647181382783  0.198003716724644
## ENSMUSG00000069306 0.498762844251583 0.000256615931900306  0.198003716724644

We examine this in more detail by constructing a mean-variance plot. Here, the black points represent the endogenous genes; the red points represent spike-in transcripts; and the red line represents the mean-variance trend fitted to the spike-ins.

plot(decomp$mean, decomp$total, xlab="Mean log-expression", ylab="Variance")
o <- order(decomp$mean)
lines(decomp$mean[o], decomp$tech[o], col="red", lwd=2)
points(fit$mean, fit$var, col="red", pch=16)