1 Installation

To install the package, please use the following.

if (!requireNamespace("BiocManager"))
    install.packages("BiocManager")
BiocManager::install("miQC")

2 Introduction

This vignette provides a basic example of how to run miQC, which allows users to perform cell-wise filtering of single-cell RNA-seq data for quality control. Single-cell RNA-seq data is very sensitive to tissue quality and choice of experimental workflow; it’s critical to ensure compromised cells and failed cell libraries are removed. A high proportion of reads mapping to mitochondrial DNA is one sign of a damaged cell, so most analyses will remove cells with mtRNA over a certain threshold, but those thresholds can be arbitrary and/or detrimentally stringent, especially for archived tumor tissues. miQC jointly models both the proportion of reads mapping to mtDNA genes and the number of detected genes with mixture models in a probabilistic framework to identify the low-quality cells in a given dataset.

For more information about the statistical background of miQC and for biological use cases, consult the miQC paper (Hippen et al. 2021).

You’ll need the following packages installed to run this tutorial:

suppressPackageStartupMessages({
    library(SingleCellExperiment)
    library(scRNAseq)
    library(scater)
    library(flexmix)
    library(splines)
    library(miQC)
})

2.1 Example data

To demonstrate how to run miQC on a single-cell RNA-seq dataset, we’ll use data from mouse brain cells, originating from an experiment by Zeisel et al (Zeisel et al. 2015), and available through the Bioconductor package scRNAseq.

sce <- ZeiselBrainData()
sce
## class: SingleCellExperiment 
## dim: 20006 3005 
## metadata(0):
## assays(1): counts
## rownames(20006): Tspan12 Tshz1 ... mt-Rnr1 mt-Nd4l
## rowData names(1): featureType
## colnames(3005): 1772071015_C02 1772071017_G12 ... 1772066098_A12
##   1772058148_F03
## colData names(10): tissue group # ... level1class level2class
## reducedDimNames(0):
## mainExpName: endogenous
## altExpNames(2): ERCC repeat

2.2 Scater preprocessing

In order to calculate the percent of reads in a cell that map to mitochondrial genes, we first need to establish which genes are mitochondrial. For genes listed as HGNC symbols, this is as simple as searching for genes starting with mt-. For other IDs, we recommend using a biomaRt query to map to chromosomal location and identify all mitochondrial genes.

mt_genes <- grepl("^mt-",  rownames(sce))
feature_ctrls <- list(mito = rownames(sce)[mt_genes])

feature_ctrls
## $mito
##  [1] "mt-Tl1"  "mt-Tn"   "mt-Tr"   "mt-Tq"   "mt-Ty"   "mt-Tk"   "mt-Ta"  
##  [8] "mt-Tf"   "mt-Tp"   "mt-Tc"   "mt-Td"   "mt-Tl2"  "mt-Te"   "mt-Tv"  
## [15] "mt-Tg"   "mt-Tt"   "mt-Tw"   "mt-Tm"   "mt-Ti"   "mt-Nd3"  "mt-Nd6" 
## [22] "mt-Nd4"  "mt-Atp6" "mt-Nd2"  "mt-Nd5"  "mt-Nd1"  "mt-Co3"  "mt-Cytb"
## [29] "mt-Atp8" "mt-Co2"  "mt-Co1"  "mt-Rnr2" "mt-Rnr1" "mt-Nd4l"

miQC is designed to be run with the Bioconductor package scater, which has a built-in function addPerCellQC to calculate basic QC metrics like number of unique genes detected per cell and total number of reads. When we pass in our list of mitochondrial genes, it will also calculate percent mitochondrial reads.

sce <- addPerCellQC(sce, subsets = feature_ctrls)
head(colData(sce))
## DataFrame with 6 rows and 22 columns
##                     tissue   group # total mRNA mol      well       sex
##                <character> <numeric>      <numeric> <numeric> <numeric>
## 1772071015_C02    sscortex         1           1221         3         3
## 1772071017_G12    sscortex         1           1231        95         1
## 1772071017_A05    sscortex         1           1652        27         1
## 1772071014_B06    sscortex         1           1696        37         3
## 1772067065_H06    sscortex         1           1219        43         3
## 1772071017_E02    sscortex         1           1378         5         1
##                      age  diameter        cell_id  level1class level2class
##                <numeric> <numeric>    <character>  <character> <character>
## 1772071015_C02         2         1 1772071015_C02 interneurons       Int10
## 1772071017_G12         1       353 1772071017_G12 interneurons       Int10
## 1772071017_A05         1        13 1772071017_A05 interneurons        Int6
## 1772071014_B06         2        19 1772071014_B06 interneurons       Int10
## 1772067065_H06         6        12 1772067065_H06 interneurons        Int9
## 1772071017_E02         1        21 1772071017_E02 interneurons        Int9
##                      sum  detected subsets_mito_sum subsets_mito_detected
##                <numeric> <numeric>        <numeric>             <numeric>
## 1772071015_C02     22354      4871              774                    23
## 1772071017_G12     22869      4712             1121                    27
## 1772071017_A05     32594      6055              952                    27
## 1772071014_B06     33525      5852              611                    28
## 1772067065_H06     21694      4724              164                    23
## 1772071017_E02     25919      5427             1122                    19
##                subsets_mito_percent altexps_ERCC_sum altexps_ERCC_detected
##                           <numeric>        <numeric>             <numeric>
## 1772071015_C02             3.462468             6706                    43
## 1772071017_G12             4.901832             6435                    46
## 1772071017_A05             2.920783             6335                    47
## 1772071014_B06             1.822521             7032                    43
## 1772067065_H06             0.755969             5931                    39
## 1772071017_E02             4.328871             6671                    43
##                altexps_ERCC_percent altexps_repeat_sum altexps_repeat_detected
##                           <numeric>          <numeric>               <numeric>
## 1772071015_C02              18.0070               8181                     419
## 1772071017_G12              15.6349              11854                     480
## 1772071017_A05              11.1238              18021                     582
## 1772071014_B06              12.8999              13955                     512
## 1772067065_H06              17.1908               6876                     363
## 1772071017_E02              13.3543              17364                     618
##                altexps_repeat_percent     total
##                             <numeric> <numeric>
## 1772071015_C02                21.9677     37241
## 1772071017_G12                28.8012     41158
## 1772071017_A05                31.6435     56950
## 1772071014_B06                25.5999     54512
## 1772067065_H06                19.9299     34501
## 1772071017_E02                34.7600     49954

3 miQC

3.1 Basic usage

Using the QC metrics generated via scater, we can use the plotMetrics function to visually inspect the quality of our dataset.

plotMetrics(sce)

We can see that most cells have a fairly low proportion of mitochondrial reads, given that the graph is much denser at the bottom. We likely have many cells that are intact and biologically meaningful. There are also a few cells that have almost half of their reads mapping to mitochondrial genes, which are likely broken or otherwise compromised and we will want to exclude from our downstream analysis. However, it’s not clear what boundaries to draw to separate the two groups of cells. With that in mind, we’ll generate a linear mixture model using the mixtureModel function.

model <- mixtureModel(sce)

This function is a wrapper for flexmix, which fits a mixture model on our data and returns the parameters of the two lines that best fit the data, as well as the posterior probability of each cell being derived from each distribution.

We can look at the parameters and posterior values directly with the functions

parameters(model)
##                        Comp.1        Comp.2
## coef.(Intercept) 26.892337488  9.5031116411
## coef.detected    -0.003755288 -0.0008343074
## sigma             6.489604000  3.3054080578
head(posterior(model))
##            [,1]      [,2]
## [1,] 0.10553245 0.8944675
## [2,] 0.09964694 0.9003531
## [3,] 0.12859082 0.8714092
## [4,] 0.14704844 0.8529516
## [5,] 0.14397716 0.8560228
## [6,] 0.11505558 0.8849444

Or we can visualize the model results using the plotModel function:

plotModel(sce, model)

As expected, the cells at the very top of the graph are almost certainly compromised, most likely to have been derived from the distribution with fewer unique genes and higher baseline mitochondrial expression.

We can use these posterior probabilities to choose which cells to keep, and visualize the consequences of this filtering with the plotFiltering function.

plotFiltering(sce, model)

To actually perform the filtering and remove the indicated cells from our SingleCellExperiment object, we can run the filterCells parameter.

sce <- filterCells(sce, model)
## Removing 359 out of 3005 cells.
sce
## class: SingleCellExperiment 
## dim: 20006 2646 
## metadata(0):
## assays(1): counts
## rownames(20006): Tspan12 Tshz1 ... mt-Rnr1 mt-Nd4l
## rowData names(1): featureType
## colnames(2646): 1772071015_C02 1772071017_G12 ... 1772066097_D04
##   1772066098_A12
## colData names(23): tissue group # ... total prob_compromised
## reducedDimNames(0):
## mainExpName: endogenous
## altExpNames(2): ERCC repeat

3.2 Other model types

In most cases, a linear mixture model will be satisfactory as well as simpler, but miQC also supports some non-linear mixture models: currently polynomials and b-splines. A user should only need to change the model_type parameter when making the model, and all visualization and filtering functions will work the same as with a linear model.

sce <- ZeiselBrainData()
sce <- addPerCellQC(sce, subsets = feature_ctrls)

model2 <- mixtureModel(sce, model_type = "spline")
plotModel(sce, model2)

plotFiltering(sce, model2)

model3 <- mixtureModel(sce, model_type = "polynomial")
plotModel(sce, model3)

plotFiltering(sce, model3)

miQC is designed to combine information about mitochondrial percentage and library complexity (number of genes discovered) to make filtering decisions, but if an even simpler model is preferred, miQC can make a model based only on mitochondrial information. This can be done by changing the model_type parameter to “one_dimensional”, which runs a 1D gaussian mixture model.

model4 <- mixtureModel(sce, model_type = "one_dimensional")
plotModel(sce, model4)

plotFiltering(sce, model4)

3.3 Extras

3.3.1 Changing posterior cutoff

miQC defaults to removing any cell with 75% or greater posterior probability of being compromised, but if we want to be more or less stringent, we can alter the posterior_cutoff parameter, like so:

plotFiltering(sce, model4, posterior_cutoff = 0.9)

Note that when performing miQC multiple times on different samples for the same experiment, it’s recommended to select the same posterior_cutoff for all, to give consistency in addition to the flexibility of sample-specific models.

3.3.2 Preventing exclusion of low-mito cells

miQC includes two parameters to accomodate unusual and undesired behavior in the linear distributions. These issues are especially visible in some cancer datasets with a stringent posterior cutoff. We’ve included a bare-bones version of QC data for a high-grade serous ovarian tumor (full version of the data is available at GEO, accession GSM4816047).

data("hgsoc_metrics")
sce <- SingleCellExperiment(colData = metrics)
model <- mixtureModel(sce)
plotFiltering(sce, model, posterior_cutoff = 0.6, enforce_left_cutoff = FALSE,
                keep_all_below_boundary = FALSE)

The first issue is the group of cells at the bottom of the distribution getting marked for removal. These cells happen to be near the x-intercept of the compromised cell line, which increases their posterior probability of being compromised. But since they have decent library complexity and a low mitochondrial percentage, so it doesn’t make biological sense to exclude them. When the keep_all_below_boundary parameter is set to True, as is the default, any cells below the intact cell line are kept:

plotFiltering(sce, model, posterior_cutoff = 0.6, enforce_left_cutoff = FALSE,
                keep_all_below_boundary = TRUE)

3.3.3 Preventing U-shaped boundary

The second issue is the U-shape in the boundary between kept and discarded cells. When this occurs, there will be cells at the bottom of the trough that are discarded, but some cells with less library complexity (farther left) and higher percentage of mitochondrial reads (higher) – meaning they are worse in both of our QC metrics – will be kept. To avoid this happening, the parameter enforce_left_cutoff will identify the cell marked for removal with the lowest mitochondrial percentage, determine its library complexity, and discard all cells with both lower complexity and higher mitochondrial percentage:

plotFiltering(sce, model, posterior_cutoff = 0.6, enforce_left_cutoff = TRUE,
                keep_all_below_boundary = TRUE)

This will make a de facto mitochondrial percentage cutoff for all cells with low library complexity, but will be more permissive for cells with high library complexity and high mitochondrial percentage, which are more likely to be intact cells with a biological reason for high mitochondrial expression than their low library complexity counterparts.

3.4 When not to use miQC

The miQC model is based on the assumption that there are a non-trivial number of compromised cells in the dataset, which is not true in all datasets. We recommend running plotMetrics on a dataset before running miQC to see if the two-distribution model is appropriate. Look for the distinctive triangular shape where cells have a wide variety of mitochondrial percentages at lower gene counts and taper off to lower mitochondrial percentage at higher gene counts, as can be seen in the Zeisel data.

For example of a dataset where there’s not a significant number of compromised cells, so the two-distribution assumption is not met, look at another dataset from the scRNAseq package, mouse data from Buettner et al (Buettner et al. 2015).

sce <- BuettnerESCData()
mt_genes <- grepl("^mt-", rowData(sce)$AssociatedGeneName)
feature_ctrls <- list(mito = rownames(sce)[mt_genes])
sce <- addPerCellQC(sce, subsets = feature_ctrls)

plotMetrics(sce)

The mixtureModel function will throw a warning if only one distribution is found, in which case no cells would be filtered. In these cases, we recommend using other filtering methods, such as a cutoff on mitochondrial percentage or identifying outliers using median absolute deviation (MAD).

4 Session Information

## R version 4.2.1 (2022-06-23)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.5 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.16-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.16-bioc/R/lib/libRlapack.so
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_GB              LC_COLLATE=C              
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] splines   stats4    stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] ensembldb_2.22.0            AnnotationFilter_1.22.0     GenomicFeatures_1.50.0      AnnotationDbi_1.60.0       
##  [5] miQC_1.6.0                  flexmix_2.3-18              lattice_0.20-45             scater_1.26.0              
##  [9] ggplot2_3.3.6               scuttle_1.8.0               scRNAseq_2.11.0             SingleCellExperiment_1.20.0
## [13] SummarizedExperiment_1.28.0 Biobase_2.58.0              GenomicRanges_1.50.0        GenomeInfoDb_1.34.0        
## [17] IRanges_2.32.0              S4Vectors_0.36.0            BiocGenerics_0.44.0         MatrixGenerics_1.10.0      
## [21] matrixStats_0.62.0          BiocStyle_2.26.0           
## 
## loaded via a namespace (and not attached):
##   [1] AnnotationHub_3.6.0           BiocFileCache_2.6.0           lazyeval_0.2.2               
##   [4] BiocParallel_1.32.0           digest_0.6.30                 htmltools_0.5.3              
##   [7] magick_2.7.3                  viridis_0.6.2                 fansi_1.0.3                  
##  [10] magrittr_2.0.3                memoise_2.0.1                 ScaledMatrix_1.6.0           
##  [13] Biostrings_2.66.0             prettyunits_1.1.1             colorspace_2.0-3             
##  [16] blob_1.2.3                    rappdirs_0.3.3                ggrepel_0.9.1                
##  [19] xfun_0.34                     dplyr_1.0.10                  crayon_1.5.2                 
##  [22] RCurl_1.98-1.9                jsonlite_1.8.3                glue_1.6.2                   
##  [25] gtable_0.3.1                  zlibbioc_1.44.0               XVector_0.38.0               
##  [28] DelayedArray_0.24.0           BiocSingular_1.14.0           scales_1.2.1                 
##  [31] DBI_1.1.3                     Rcpp_1.0.9                    viridisLite_0.4.1            
##  [34] xtable_1.8-4                  progress_1.2.2                bit_4.0.4                    
##  [37] rsvd_1.0.5                    httr_1.4.4                    ellipsis_0.3.2               
##  [40] modeltools_0.2-23             pkgconfig_2.0.3               XML_3.99-0.12                
##  [43] farver_2.1.1                  nnet_7.3-18                   sass_0.4.2                   
##  [46] dbplyr_2.2.1                  utf8_1.2.2                    tidyselect_1.2.0             
##  [49] labeling_0.4.2                rlang_1.0.6                   later_1.3.0                  
##  [52] munsell_0.5.0                 BiocVersion_3.16.0            tools_4.2.1                  
##  [55] cachem_1.0.6                  cli_3.4.1                     generics_0.1.3               
##  [58] RSQLite_2.2.18                ExperimentHub_2.6.0           evaluate_0.17                
##  [61] stringr_1.4.1                 fastmap_1.1.0                 yaml_2.3.6                   
##  [64] knitr_1.40                    bit64_4.0.5                   purrr_0.3.5                  
##  [67] KEGGREST_1.38.0               sparseMatrixStats_1.10.0      mime_0.12                    
##  [70] xml2_1.3.3                    biomaRt_2.54.0                compiler_4.2.1               
##  [73] beeswarm_0.4.0                filelock_1.0.2                curl_4.3.3                   
##  [76] png_0.1-7                     interactiveDisplayBase_1.36.0 tibble_3.1.8                 
##  [79] bslib_0.4.0                   stringi_1.7.8                 highr_0.9                    
##  [82] ProtGenerics_1.30.0           Matrix_1.5-1                  vctrs_0.5.0                  
##  [85] pillar_1.8.1                  lifecycle_1.0.3               BiocManager_1.30.19          
##  [88] jquerylib_0.1.4               BiocNeighbors_1.16.0          bitops_1.0-7                 
##  [91] irlba_2.3.5.1                 httpuv_1.6.6                  rtracklayer_1.58.0           
##  [94] R6_2.5.1                      BiocIO_1.8.0                  bookdown_0.29                
##  [97] promises_1.2.0.1              gridExtra_2.3                 vipor_0.4.5                  
## [100] codetools_0.2-18              assertthat_0.2.1              rjson_0.2.21                 
## [103] withr_2.5.0                   GenomicAlignments_1.34.0      Rsamtools_2.14.0             
## [106] GenomeInfoDbData_1.2.9        parallel_4.2.1                hms_1.1.2                    
## [109] grid_4.2.1                    beachmat_2.14.0               rmarkdown_2.17               
## [112] DelayedMatrixStats_1.20.0     shiny_1.7.3                   ggbeeswarm_0.6.0             
## [115] restfulr_0.0.15

References

Buettner, Florian, Kedar N. Natarajan, F. Paolo Casale, Valentina Proserpio, Antonio Scialdone, Fabian J. Theis, Sarah A. Teichmann, John C. Marioni, and Oliver Stegle. 2015. “Computational Analysis of Cell-to-Cell Heterogeneity in Single-Cell RNA-Sequencing Data Reveals Hidden Subpopulations of Cells.” Nature Biotechnology 33 (2): 155–60. https://doi.org/10.1038/nbt.3102.

Hippen, Ariel A., Matias M. Falco, Lukas M. Weber, Erdogan Pekcan Erkan, Kaiyang Zhang, Jennifer Anne Doherty, Anna Vähärautio, Casey S. Greene, and Stephanie C. Hicks. 2021. “miQC: An Adaptive Probabilistic Framework for Quality Control of Single-Cell RNA-Sequencing Data.” PLOS Computational Biology 17 (8): e1009290. https://doi.org/10.1371/journal.pcbi.1009290.

Zeisel, Amit, Ana B. Muñoz-Manchado, Simone Codeluppi, Peter Lönnerberg, Gioele La Manno, Anna Juréus, Sueli Marques, et al. 2015. “Brain Structure. Cell Types in the Mouse Cortex and Hippocampus Revealed by Single-Cell RNA-Seq.” Science (New York, N.Y.) 347 (6226). https://doi.org/10.1126/science.aaa1934.