scDblFinder 1.8.0
The scDblFinder
method combines the strengths of various doublet detection approaches,
training an iterative classifier on the neighborhood of real cells and artificial doublets.
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("scDblFinder")
# or, to get that latest developments:
BiocManager::install("plger/scDblFinder")
The input of scDblFinder
is an object sce
of class SingleCellExperiment (empty drops having already been removed) containing at least the counts (assay ‘counts’). If normalized expression (assay ‘logcounts’) and PCA (reducedDim ‘PCA’) are also present, these will be used for the clustering step (unless clusters are given.)
Given an SCE object:
set.seed(123)
library(scDblFinder)
# we create a dummy dataset; since it's small we set a higher doublet rate
sce <- mockDoubletSCE(dbl.rate=0.1)
# we run scDblFinder (providing the unsually high doublet rate)
sce <- scDblFinder(sce, dbr=0.1)
## Creating ~5000 artificial doublets...
## Dimensional reduction
## Evaluating kNN...
## Training model...
## iter=0, 37 cells excluded from training.
## iter=1, 34 cells excluded from training.
## iter=2, 35 cells excluded from training.
## Threshold found:0.711
## 35 (6.6%) doublets called
For 10x data, it is usually safe to leave the dbr
empty, and it will be automatically estimated.
scDblFinder
will add a number of columns to the colData of sce
prefixed with ‘scDblFinder’, the most important of which are:
sce$scDblFinder.score
: the final doublet scoresce$scDblFinder.class
: the classification (doublet or singlet)table(truth=sce$type, call=sce$scDblFinder.class)
## call
## truth singlet doublet
## singlet 499 1
## doublet 0 34
If you have multiple samples (understood as different cell captures), then it is
preferable to look for doublets separately for each sample (for multiplexed samples with cell hashes,
this means for each batch). You can do this by simply providing a vector of the sample ids to the
samples
parameter of scDblFinder
or, if these are stored in a column of colData
, the name of the
column. In this case, you might also consider multithreading it using the BPPARAM
parameter (assuming you’ve got enough RAM!).
For example:
library(BiocParallel)
sce <- scDblFinder(sce, samples="sample_id", BPPARAM=MulticoreParam(3))
table(sce$scDblFinder.class)
Note that if you are running multiple samples using the cluster-based approach (see below), clustering will be performed sample-wise.
While this is typically not an issue for doublet identification, it means that the cluster labels (and putative origins of doublets) won’t match between samples. If you are interested in these, it is preferable to first cluster (for example using sce$cluster <- fastcluster(sce)
) and then provide the clusters to scDblFinder
, which will ensure concordant labels across samples.
Of note, if you have very large differences in number of cells between samples the scores will not be directly comparable. We are working on improving this, but in the meantime it would be preferable to stratify similar samples and threshold the sets separately.
Wrapped in the scDblFinder
function are the following steps:
Doublets can only arise within a given sample or capture, and for this reason are better sought independently for each sample, which also speeds up the analysis. If the samples
argument is given, scDblFinder
will use it to split the cells into samples/captures, and process each of them in parallel if the BPPARAM
argument is given. The classifier will be trained globally, but thresholds will be optimized on a per-sample basis. If your samples are multiplexed, i.e. the different samples are mixed in different batches, then the batches should be what you provide to this argument.
The analysis can be considerably sped up, at little if any cost in accuracy, by reducing the dataset to only the top expressed genes (controlled by the nfeatures
argument).
Then, depending on the clusters
argument, an eventual PCA and clustering (using the internal fastcluster
function) will be performed.
The rationale for the cluster-based approach is that homotypic doublets are nearly impossible to distinguish on the basis of their transcriptome, and therefore that creating that kind of doublets is a waste of computational resources that can moreover mislead the classifier into flagging singlets.
An alternative approach, however, is to generate doublets randomly (setting clusters
to FALSE or NULL), and use the iterative approach (see below) to exclude also unidentifiable artificial doublets from the training.
Depending on the clusters
and propRandom
arguments, artificial doublets will be generated by combining random cells and/or pairs of non-identical clusters (this can be performed manually using the getArtificialDoublets
function). A proportion of the doublets will simply use the sum of counts of the composing cells, while the rest will undergo a library size adjustment and poisson resampling.
A new PCA is performed on the combination of real and artificial cells, from which a kNN network is generated. Using this kNN, a number of parameters are gathered for each cell, such as the proportion of doublets (i.e. artificial doublets or known doublets provided through the knownDoublets
argument, if given) among the KNN, ratio of the distances to the nearest doublet and nearest non-doublet, etc. Several of this features are reported in the output with the ‘scDblFinder.’ prefix, e.g.:
distanceToNearest
: distance to the nearest cell (real or artificial)ratio
: the proportion of the KNN that are doublets. (If more than one value of k
is given, the various ratios will be used during classification and will be reported)weighted
: the proportion of the KNN that are doublets, weighted by their distance (useful for isolated cells)Unless the score
argument is set to ‘weighted’ or ‘ratio’ (in which case the aforementioned ratio is directly used as a doublet score), scDblFinder
then uses gradient boosted trees trained on the kNN-derived properties along with a few additional features (e.g. library size, number of non-zero features, and an estimate of the difficultly of detecting artificial doublets in the cell’s neighborhood, a variant of the cxds
score from the scds, etc.) to distinguish doublets (either artificial or given) from other cells, and assigns a score on this basis.
One problem of using a classifier for this task is that some of the real cells (the actual doublets) are mislabeled as singlet, so to speak.
scDblFinder
therefore iteratively retrains the classifier, each time excluding from the training the (real) cells called as doublets in the previous step (as well as unidentifiable artificial doublets). The number of steps being controlled by the iter
parameter (in our experience, 2 or 3 is optimal).
This score is available in the output, in the scDblFinder.score
colData column, and can be interpreted as a probability. If the data is multi-sample, a single model is trained for all samples.
Rather than thresholding on some arbitrary cutoff of the score, scDblFinder
uses the expected number of doublets in combination to the misclassification rate to establish a threshold. Unless it is manually given through the dbr
argument, the expected doublet rate is first estimated using the empirical rule of thumb applicable to 10X data, namely roughly 1% per 1000 cells captures (so with 5000 cells, (0.01*5)*5000 = 250 doublets, and the more cells you capture the higher the chance of creating a doublet). If samples were specified, and if the dbr
is automatically calculated, thresholding is performed separately across samples.
Thresholding then tries to simultaneously minimize: 1) the classification error (in terms of the proportion of known doublets below the threshold) and 2) the deviation from the expected number of doublets among real cells (as a ratio of the total number of expected doublets within the range determined by dbr.sd
, and adjusted for homotypic doublets). This means that, if you have no idea about the doublet rate, setting dbr.sd=1
will make the threshold depend entirely on the misclassification rate.
If artificial doublets are generated between clusters, it is sometimes possible to call the most likely origin (in terms of the combination of clusters) of a given putative real doublet.
We observed that at least one of the two composing cell is typically recognized, but that both are seldom correctly recognized, owing to the sometimes small relative contribution of one of the two original cells.
This information is provided through the scDblFinder.mostLikelyOrigin
column of the output (and the scDblFinder.originAmbiguous
column indicates whether this origin is ambiguous or rather clear).
This, in turn, allows us to identify enrichment over expectation for specific kinds of doublets.
Some statistics on each combination of clusters are saved in metadata(sce)$scDblFinder.stats
, and the plotDoubletMap
function can be used to visualize enrichments.
In addition, two frameworks are offered for testing the significance of enrichments:
clusterStickiness
function tests whether each cluster forms more doublet than would be expected given its abundance, by default using a single quasi-binomial model fitted across all doublet types.doubletPairwiseEnrichment
function separately tests whether each specific doublet type (i.e. combination of clusters) is more abundant than expected, by default using a poisson model.scDblFinder
has a fair number of parameters governing the preprocessing, generation of doublets, classification, etc. (see ?scDblFinder
). Here we describe just a few of the most important ones.
The expected proportion of doublets has no impact on the density of artificial doublets in the neighborhood, but impacts the classifier’s score and, especially, where the cutoff will be placed. It is specified through the dbr
parameter and the dbr.sd
parameter (the latter specifies a +/- range around dbr
within which the deviation from dbr
will be considered null). For 10x data, the more cells you capture the higher the chance of creating a doublet, and Chromium documentation indicates a doublet rate of roughly 1% per 1000 cells captures (so with 5000 cells, (0.01*5)*5000 = 250 doublets), and the default expected doublet rate will be set to this value (with a default standard deviation of 0.015). Note however that different protocols may create considerably more doublets, and that this should be updated accordingly. If you have unsure about the doublet rate, you might consider increasing dbr.sd
, so that it is estimated mostl/purely from the misclassification error.
Then you most likely have a wrong doublet rate. If you did not provide it (dbr
argument), the doublet rate will be calculated automatically using expected doublet rates from 10x, meaning that the more cells captured, the higher the doublet rates. If you have reasons to think that this is not applicable to your data, set the dbr
manually.
The most common cause for an unexpectedly large proportion of doublets is if you have a multi-sample dataset and did not split by samples. scDblFinder
will think that the data is a single capture with loads of cells, and hence with a very high doublet rate. Splitting by sample should solve the issue.
The thresholding tries to minimize both the deviance from the expected number of doublets and the misclassification (i.e. of artificial doublets), meaning that the effective (i.e. final) doublet rate will differ from the given one. scDblFinder
also considers false positives to be less problematic than false negatives. You can reduce to some degree the deviation from the input doublet rate by setting dbr.sd=0
.
While both approaches perform very similarly in benchmarks, the random generation approach is often slightly superior in complex datasets. If your data is very clearly segregated into clusters, or if you are interested in the origin of the doublets, the cluster-based approach is preferable. This will also enable a more accurate accounting of homotypic doublets, and therefore a slightly better thresholding.
Otherwise, and especially if your data does not segregate very clearly into clusters, the random approach (e.g. clusters=FALSE
) is preferable.
If you ran scDblFinder
on a multi-sample dataset and did not provide the cluster labels, then the labels are sample-specific (meaning that label ‘1’ in one sample might have nothing to do with label ‘1’ in another), and plotting them on a tSNE will look like they do not make sense. For this reason, when running multiple samples we recommend to first cluster all samples together (for example using sce$cluster <- fastcluster(sce)
) and then provide the clusters to scDblFinder
.
You will get this error if you have some cells that have zero (or very close to zero) reads. After filtering out these cells the error should go away. Note, however, that we recommend not having too stringent filtering before running scDblFinder.
Because it relies on the partly random generation of artificial doublets, running scDblFinder multiple times on the same data will yield slightly different results. You can ensure reproducibility using set.seed()
, however this will not be sufficient when multithreading. In such case, using the following procedure:
bp <- MulticoreParam(3, RNGseed=1234)
bpstart(bp)
sce <- scDblFinder(sce, clusters="cluster", samples="sample", BPPARAM=bp)
bpstop(bp)
If the input SCE already contains a logcounts
assay or a reducedDim
slot named ‘PCA’, scDblFinder will use them for the clustering step. In addition, a clustering can be manually given using the clusters
argument of scDblFinder()
. In this way, seurat clustering could for instance be used to create the artificial doublets (see ?Seurat::as.SingleCellExperiment.Seurat
for conversion to SCE).
After artificial doublets generation, the counts of real and artificial cells must then be reprocessed (i.e. normalization and PCA) together, which is performed internally using scater. If you wish this step to be performed differently, you may provide your own function for doing so (see the processing
argument in ?scDblFinder
). We note, however, that the impact of variations of this step on doublet detection is rather mild. In fact, not performing any normalization at all for instance decreases doublet identification accuracy, but by rather little.
Yes, specifically working on peak-level data. Because of the much greater sparsity of single-cell ATAC-seq data and the fact that scDblFinder
works with a selection of features, using it with standard parameters will yield a poor performance. We therefore recommend to use aggregateFeatures=TRUE
, which will aggregate similar features (rather than selecting features) before the normal scDblFinder
procedure, and yields excellent results. If few enough (which we recommend), these features can be directly used to calculated distances rather than going through the SVD step, in the following way:
sce <- scDblFinder(sce, aggregateFeatures=TRUE, nfeatures=25, processing="normFeatures")
The input to scDblFinder
should not include empty droplets, and it might be necessary to remove cells with a very low coverage (e.g. <200 reads) to avoid errors.
Further quality filtering should be performed downstream of doublet detection, for two reasons:
1. the default expected doublet rate is calculated on the basis of the cells given, and if you excluded a lot of cells as low quality, scDblFinder
might think that the doublet rate should be lower than it is.
2. kicking out all low quality cells first might hamper our ability to detect doublets that are formed by the combination of a good quality cell with a low-quality one.
This being said, these are mostly theoretical grounds, and unless your QC filtering is very stringent (and it shouldn’t be!), it’s unlikely to make a big difference.
sessionInfo()
## R version 4.1.1 (2021-08-10)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.3 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.14-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.14-bioc/R/lib/libRlapack.so
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_GB 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] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] bluster_1.4.0 scDblFinder_1.8.0
## [3] scater_1.22.0 ggplot2_3.3.5
## [5] scran_1.22.0 scuttle_1.4.0
## [7] ensembldb_2.18.0 AnnotationFilter_1.18.0
## [9] GenomicFeatures_1.46.0 AnnotationDbi_1.56.0
## [11] scRNAseq_2.7.2 SingleCellExperiment_1.16.0
## [13] SummarizedExperiment_1.24.0 Biobase_2.54.0
## [15] GenomicRanges_1.46.0 GenomeInfoDb_1.30.0
## [17] IRanges_2.28.0 S4Vectors_0.32.0
## [19] BiocGenerics_0.40.0 MatrixGenerics_1.6.0
## [21] matrixStats_0.61.0 BiocStyle_2.22.0
##
## loaded via a namespace (and not attached):
## [1] AnnotationHub_3.2.0 BiocFileCache_2.2.0
## [3] igraph_1.2.7 lazyeval_0.2.2
## [5] BiocParallel_1.28.0 digest_0.6.28
## [7] htmltools_0.5.2 magick_2.7.3
## [9] viridis_0.6.2 fansi_0.5.0
## [11] magrittr_2.0.1 memoise_2.0.0
## [13] ScaledMatrix_1.2.0 cluster_2.1.2
## [15] limma_3.50.0 Biostrings_2.62.0
## [17] prettyunits_1.1.1 colorspace_2.0-2
## [19] ggrepel_0.9.1 blob_1.2.2
## [21] rappdirs_0.3.3 xfun_0.27
## [23] dplyr_1.0.7 crayon_1.4.1
## [25] RCurl_1.98-1.5 jsonlite_1.7.2
## [27] glue_1.4.2 gtable_0.3.0
## [29] zlibbioc_1.40.0 XVector_0.34.0
## [31] DelayedArray_0.20.0 BiocSingular_1.10.0
## [33] scales_1.1.1 DBI_1.1.1
## [35] edgeR_3.36.0 Rcpp_1.0.7
## [37] viridisLite_0.4.0 xtable_1.8-4
## [39] progress_1.2.2 dqrng_0.3.0
## [41] bit_4.0.4 rsvd_1.0.5
## [43] metapod_1.2.0 httr_1.4.2
## [45] ellipsis_0.3.2 farver_2.1.0
## [47] pkgconfig_2.0.3 XML_3.99-0.8
## [49] sass_0.4.0 dbplyr_2.1.1
## [51] locfit_1.5-9.4 utf8_1.2.2
## [53] labeling_0.4.2 tidyselect_1.1.1
## [55] rlang_0.4.12 later_1.3.0
## [57] munsell_0.5.0 BiocVersion_3.14.0
## [59] tools_4.1.1 cachem_1.0.6
## [61] xgboost_1.4.1.1 generics_0.1.1
## [63] RSQLite_2.2.8 ExperimentHub_2.2.0
## [65] evaluate_0.14 stringr_1.4.0
## [67] fastmap_1.1.0 yaml_2.2.1
## [69] knitr_1.36 bit64_4.0.5
## [71] purrr_0.3.4 KEGGREST_1.34.0
## [73] sparseMatrixStats_1.6.0 mime_0.12
## [75] xml2_1.3.2 biomaRt_2.50.0
## [77] compiler_4.1.1 beeswarm_0.4.0
## [79] filelock_1.0.2 curl_4.3.2
## [81] png_0.1-7 interactiveDisplayBase_1.32.0
## [83] tibble_3.1.5 statmod_1.4.36
## [85] bslib_0.3.1 stringi_1.7.5
## [87] highr_0.9 lattice_0.20-45
## [89] ProtGenerics_1.26.0 Matrix_1.3-4
## [91] vctrs_0.3.8 pillar_1.6.4
## [93] lifecycle_1.0.1 BiocManager_1.30.16
## [95] jquerylib_0.1.4 BiocNeighbors_1.12.0
## [97] cowplot_1.1.1 data.table_1.14.2
## [99] bitops_1.0-7 irlba_2.3.3
## [101] httpuv_1.6.3 rtracklayer_1.54.0
## [103] R6_2.5.1 BiocIO_1.4.0
## [105] bookdown_0.24 promises_1.2.0.1
## [107] gridExtra_2.3 vipor_0.4.5
## [109] MASS_7.3-54 assertthat_0.2.1
## [111] rjson_0.2.20 withr_2.4.2
## [113] GenomicAlignments_1.30.0 Rsamtools_2.10.0
## [115] GenomeInfoDbData_1.2.7 parallel_4.1.1
## [117] hms_1.1.1 grid_4.1.1
## [119] beachmat_2.10.0 rmarkdown_2.11
## [121] DelayedMatrixStats_1.16.0 Rtsne_0.15
## [123] shiny_1.7.1 ggbeeswarm_0.6.0
## [125] restfulr_0.0.13