# Normalizing single-cell RNA-seq data

Aaron Lun*

*infinite.monkeys.with.keyboards@gmail.com

#### Revised: February 6, 2021

#### Package

scuttle 1.2.1

# 1 Introduction

scuttle provides various low-level utilities for single-cell RNA-seq data analysis, typically used at the start of the analysis workflows or within high-level functions in other packages. This vignette will discuss the use of scaling normalization for removing cell-specific biases. To demonstrate, we will obtain the classic Zeisel dataset from the scRNAseq package and apply some quick quality control to remove damaged cells.

    library(scRNAseq)
    sce <- ZeiselBrainData()

    library(scuttle)
    sce <- quickPerCellQC(sce, subsets=list(Mito=grep("mt-", rownames(sce))),
        sub.fields=c("subsets_Mito_percent", "altexps_ERCC_percent"))

    sce

    ## class: SingleCellExperiment
    ## dim: 20006 2816
    ## metadata(0):
    ## assays(1): counts
    ## rownames(20006): Tspan12 Tshz1 ... mt-Rnr1 mt-Nd4l
    ## rowData names(1): featureType
    ## colnames(2816): 1772071015_C02 1772071017_G12 ... 1772063068_D01
    ##   1772066098_A12
    ## colData names(27): tissue group # ... high_altexps_ERCC_percent discard
    ## reducedDimNames(0):
    ## mainExpName: endogenous
    ## altExpNames(2): ERCC repeat

Note: A more comprehensive description of the use of scuttle functions (along with other packages) in a scRNA-seq analysis workflow is available at https://osca.bioconductor.org.

# 2 Computing size factors

Scaling normalization involves dividing the counts for each cell by a cell-specific “size factor” to adjust for uninteresting differences in sequencing depth and capture efficiency. The `librarySizeFactors()` function provides a simple definition of the size factor for each cell, computed as the library size of each cell after scaling them to have a mean of 1 across all cells. This is fast but inaccurate in the presence of differential expression between cells that introduce composition biases.

    summary(librarySizeFactors(sce))

    ##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
    ##  0.1757  0.5680  0.8680  1.0000  1.2783  4.0839

The `geometricSizeFactors()` function instead computes the geometric mean within each cell. This is more robust to composition biases but is only accurate when the counts are large and there are few zeroes.

    summary(geometricSizeFactors(sce))

    ##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
    ##  0.8261  0.9104  0.9758  1.0000  1.0640  1.5189

The `medianSizeFactors()` function uses a DESeq2-esque approach based on the median ratio from an average pseudo-cell. Briefly, we assume that most genes are non-DE, such that any systematic fold difference in coverage (as defined by the median ratio) represents technical biases that must be removed. This is highly robust to composition biases but relies on sufficient sequencing coverage to obtain well-defined ratios.

    summary(medianSizeFactors(sce))

    ##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's
    ##      NA      NA      NA     NaN      NA      NA    2816

All of these size factors can be stored in the `SingleCellExperiment` via the `sizeFactors<-()` setter function. Most downstream functions will pick these up automatically for any calculations that rely on size factors.

    sizeFactors(sce) <- librarySizeFactors(sce)

Alternatively, functions like `computeLibraryFactors()` can automatically compute and attach the size factors to our `SingleCellExperiment` object.

    sce <- computeLibraryFactors(sce)
    summary(sizeFactors(sce))

    ##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
    ##  0.1757  0.5680  0.8680  1.0000  1.2783  4.0839

# 3 Pooling normalization

The `computePooledFactors` method implements the pooling strategy for scaling normalization. This uses an approach similar to `medianSizeFactors()` to remove composition biases, but pools cells together to overcome problems with discreteness at low counts. Per-cell factors are then obtained from the pools using a deconvolution strategy.

    library(scran)
    clusters <- quickCluster(sce)

    sce <- computePooledFactors(sce, clusters=clusters)
    summary(sizeFactors(sce))

    ##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
    ##  0.1340  0.4910  0.8281  1.0000  1.3181  4.4579

For larger data sets, a rough clustering should be performed prior to normalization. `computePooledFactors()` will then automatically apply normalization within each cluster first, before adjusting the scaling factors to be comparable across clusters. This reduces the risk of violating our assumptions (of a non-DE majority of genes) when many genes are DE between clusters in a heterogeneous population. In this case, we use the `quickCluster()` function from the scran package, but any clustering function can be used, for example:

    sce <- computePooledFactors(sce, clusters=sce$level1class)
    summary(sizeFactors(sce))

    ##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
    ##  0.1322  0.4797  0.8265  1.0000  1.3355  4.4953

We assume that quality control on the cells has already been performed prior to running this function. Low-quality cells with few expressed genes can often have negative size factor estimates.

# 4 Spike-in normalization

An alternative approach is to normalize based on the spike-in counts. 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.

    sce2 <- computeSpikeFactors(sce, "ERCC")
    summary(sizeFactors(sce2))

    ##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
    ##  0.1600  0.7746  0.9971  1.0000  1.1851  3.4923

The main practical difference from the other strategies is that spike-in normalization preserves differences in total RNA content between cells, whereas `computePooledFactors` and other non-DE methods do not. This can be important in certain applications where changes in total RNA content are associated with a biological phenomenon of interest.

# 5 Computing normalized expression matrices

Regardless of which size factor calculation we pick, the calculation of normalized expression values simply involves dividing each count by the size factor for the cell. This eliminates the cell-specific scaling effect for valid comparisons between cells in downstream analyses. The simplest approach to computing these values is to use the `logNormCounts()` function:

    sce <- logNormCounts(sce)
    assayNames(sce)

    ## [1] "counts"    "logcounts"

This computes log2-transformed normalized expression values by adding a constant pseudo-count and log-transforming. The resulting values can be roughly interpreted on the same scale as log-transformed counts and are stored in `"logcounts"`. This is the most common expression measure for downstream analyses as differences between values can be treated as log-fold changes. For example, Euclidean distances between cells are analogous to the average log-fold change across genes.

Of course, users can construct any arbitrary matrix of the same dimensions as the count matrix and store it as an assay. Here, we use the `normalizeCounts()` function to perform some custom normalization with random size factors.

    assay(sce, "normed") <- normalizeCounts(sce, log=FALSE,
        size.factors=runif(ncol(sce)), pseudo.count=1.5)

scuttle can also calculate counts-per-million using the aptly-named `calculateCPM()` function. The output is most appropriately stored as an assay named `"cpm"` in the assays of the `SingleCellExperiment` object. Related functions include `calculateTPM()` and `calculateFPKM()`, which do pretty much as advertised.

    assay(sce, "cpm") <- calculateCPM(sce)

# Session information

    sessionInfo()

    ## R version 4.1.0 (2021-05-18)
    ## Platform: x86_64-w64-mingw32/x64 (64-bit)
    ## Running under: Windows Server 2012 R2 x64 (build 9600)
    ##
    ## Matrix products: default
    ##
    ## locale:
    ## [1] LC_COLLATE=C
    ## [2] LC_CTYPE=English_United States.1252
    ## [3] LC_MONETARY=English_United States.1252
    ## [4] LC_NUMERIC=C
    ## [5] LC_TIME=English_United States.1252
    ##
    ## attached base packages:
    ## [1] parallel  stats4    stats     graphics  grDevices utils     datasets
    ## [8] methods   base
    ##
    ## other attached packages:
    ##  [1] scran_1.20.1                ensembldb_2.16.4
    ##  [3] AnnotationFilter_1.16.0     GenomicFeatures_1.44.0
    ##  [5] AnnotationDbi_1.54.1        scuttle_1.2.1
    ##  [7] scRNAseq_2.6.1              SingleCellExperiment_1.14.1
    ##  [9] SummarizedExperiment_1.22.0 Biobase_2.52.0
    ## [11] GenomicRanges_1.44.0        GenomeInfoDb_1.28.1
    ## [13] IRanges_2.26.0              S4Vectors_0.30.0
    ## [15] BiocGenerics_0.38.0         MatrixGenerics_1.4.1
    ## [17] matrixStats_0.60.0          BiocStyle_2.20.2
    ##
    ## loaded via a namespace (and not attached):
    ##   [1] rjson_0.2.20                  ellipsis_0.3.2
    ##   [3] bluster_1.2.1                 XVector_0.32.0
    ##   [5] BiocNeighbors_1.10.0          bit64_4.0.5
    ##   [7] interactiveDisplayBase_1.30.0 fansi_0.5.0
    ##   [9] xml2_1.3.2                    sparseMatrixStats_1.4.0
    ##  [11] cachem_1.0.5                  knitr_1.33
    ##  [13] jsonlite_1.7.2                Rsamtools_2.8.0
    ##  [15] cluster_2.1.2                 dbplyr_2.1.1
    ##  [17] png_0.1-7                     shiny_1.6.0
    ##  [19] BiocManager_1.30.16           compiler_4.1.0
    ##  [21] httr_1.4.2                    dqrng_0.3.0
    ##  [23] assertthat_0.2.1              Matrix_1.3-4
    ##  [25] fastmap_1.1.0                 lazyeval_0.2.2
    ##  [27] limma_3.48.1                  later_1.2.0
    ##  [29] BiocSingular_1.8.1            htmltools_0.5.1.1
    ##  [31] prettyunits_1.1.1             tools_4.1.0
    ##  [33] igraph_1.2.6                  rsvd_1.0.5
    ##  [35] glue_1.4.2                    GenomeInfoDbData_1.2.6
    ##  [37] dplyr_1.0.7                   rappdirs_0.3.3
    ##  [39] Rcpp_1.0.7                    jquerylib_0.1.4
    ##  [41] vctrs_0.3.8                   Biostrings_2.60.2
    ##  [43] ExperimentHub_2.0.0           rtracklayer_1.52.0
    ##  [45] DelayedMatrixStats_1.14.1     xfun_0.24
    ##  [47] stringr_1.4.0                 beachmat_2.8.0
    ##  [49] mime_0.11                     lifecycle_1.0.0
    ##  [51] irlba_2.3.3                   restfulr_0.0.13
    ##  [53] statmod_1.4.36                XML_3.99-0.6
    ##  [55] edgeR_3.34.0                  AnnotationHub_3.0.1
    ##  [57] zlibbioc_1.38.0               hms_1.1.0
    ##  [59] promises_1.2.0.1              ProtGenerics_1.24.0
    ##  [61] yaml_2.2.1                    curl_4.3.2
    ##  [63] memoise_2.0.0                 sass_0.4.0
    ##  [65] biomaRt_2.48.2                stringi_1.7.3
    ##  [67] RSQLite_2.2.7                 BiocVersion_3.13.1
    ##  [69] BiocIO_1.2.0                  ScaledMatrix_1.0.0
    ##  [71] filelock_1.0.2                BiocParallel_1.26.1
    ##  [73] rlang_0.4.11                  pkgconfig_2.0.3
    ##  [75] bitops_1.0-7                  evaluate_0.14
    ##  [77] lattice_0.20-44               purrr_0.3.4
    ##  [79] GenomicAlignments_1.28.0      bit_4.0.4
    ##  [81] tidyselect_1.1.1              magrittr_2.0.1
    ##  [83] bookdown_0.22                 R6_2.5.0
    ##  [85] generics_0.1.0                metapod_1.0.0
    ##  [87] DelayedArray_0.18.0           DBI_1.1.1
    ##  [89] pillar_1.6.2                  withr_2.4.2
    ##  [91] KEGGREST_1.32.0               RCurl_1.98-1.3
    ##  [93] tibble_3.1.3                  crayon_1.4.1
    ##  [95] utf8_1.2.2                    BiocFileCache_2.0.0
    ##  [97] rmarkdown_2.9                 progress_1.2.2
    ##  [99] locfit_1.5-9.4                grid_4.1.0
    ## [101] blob_1.2.2                    digest_0.6.27
    ## [103] xtable_1.8-4                  httpuv_1.6.1
    ## [105] bslib_0.2.5.1