scDblFinder 1.4.0
To demonstrate, we’ll use one of the mammary gland datasets from the scRNAseq package. We will subset it down to a random set of 1000 cells for speed.
library(scRNAseq)
sce <- BachMammaryData(samples="G_1")
set.seed(1001)
sce <- sce[,sample(ncol(sce), 1000)]
For the purposes of this demonstration, we’ll perform an extremely expedited analysis. One would usually take more care here and do some quality control, create some diagnostic plots, etc., but we don’t have the space for that.
library(scuttle)
sce <- logNormCounts(sce)
library(scran)
dec <- modelGeneVar(sce)
hvgs <- getTopHVGs(dec, n=1000)
library(scater)
set.seed(1002)
sce <- runPCA(sce, ncomponents=10, subset_row=hvgs)
sce <- runTSNE(sce, dimred="PCA")
We run computeDoubletDensity()
to obtain a doublet score for each cell based on the density of simulated doublets around it.
We log this to get some better dynamic range.
set.seed(1003)
library(scDblFinder)
scores <- computeDoubletDensity(sce, subset.row=hvgs)
plotTSNE(sce, colour_by=I(log1p(scores)))
We use a fairly simple approach in doubletCells
that involves creating simulated doublets from the original data set:
We allow specification of two sets of size factors for different purposes. The first set is the normalization set: division of counts by these size factors yields expression values to be compared across cells. This is necessary to compute log-normalized expression values for the PCA.
These size factors are usually computed from some method that assumes most genes are not DE. We default to library size normalization though any arbitrary set of size factors can be used. The size factor for each doublet is computed as the sum of size factors for the individual cells, based on the additivity of scaling biases.
The second set is the RNA content set: division of counts by these size factors yields expression values that are proportional to absolute abundance across cells. This affects the creation of simulated doublets by controlling the scaling of the count profiles for the individual cells. These size factors would normally be estimated with spike-ins, but in their absence we default to using unity for all cells.
The use of unity values implies that the library size for each cell is a good proxy for total RNA content. This is unlikely to be true: technical biases mean that the library size is an imprecise relative estimate of the content. Saturation effects and composition biases also mean that the expected library size for each population is not an accurate estimate of content. The imprecision will spread out the simulated doublets while the inaccuracy will result in a systematic shift from the location of true doublets.
Arguably, such problems exist for any doublet estimation method without spike-in information. We can only hope that the inaccuracies have only minor effects on the creation of simulated cells. Indeed, the first effect does mitigate the second to some extent by ensuring that some simulated doublets will occupy the neighbourhood of the true doublets.
These two sets of size factors play different roles so it is possible to specify both of them. We use the following algorithm to accommodate non-unity values for the RNA content size factors:
To understand the correctness of the rescaled normalization size factors, consider a non-DE gene with abundance \(\lambda_g\). The expected count in each cell is \(\lambda_g s_i\) for scaling bias \(s_i\) (i.e., normalization size factor). The rescaled count is \(\lambda_g s_i c_i^{-1}\) for some RNA content size factor \(c_i\). The rescaled normalization size factor is \(s_i c_i^{-1}\), such that normalization yields \(\lambda_g\) as desired. This also holds for doublets where the scaling biases and size factors are additive.
We assume that the simulation accurately mimics doublet creation - amongst other things, we assume that doublets are equally likely to form between any cell populations and any differences in total RNA between subpopulations are captured or negligible. If these assumptions hold, then at any given region in the expression space, the number of doublets among the real cells is proportional to the number of simulated doublets lying in the same region. Thus, the probability that a cell is a doublet is proportional to the ratio of the number of neighboring simulated doublets to the number of neighboring real cells.
A mild additional challenge here is that the number of simulated cells \(N_s\) can vary. Ideally, we would like the expected output of the function to be the same regardless of the user’s choice of \(N_s\), i.e., the chosen value should only affect the precision/speed trade-off. Many other doublet-based methods take a \(k\)-nearest neighbours approach to compute densities; but if \(N_s\) is too large relative to the number of real cells, all of the \(k\) nearest neighbours will be simulated, while if \(N_s\) is too small, all of the nearest neighbors will be original cells.
Thus, we use a modified version of the \(k\)NN approach whereby we identify the distance from each cell to its \(k\)-th nearest neighbor. This defines a hypersphere around that cell in which we count the number of simulated cells. We then compute the odds ratio of the number of simulated cells in the hypersphere to \(N_s\), divided by the ratio of \(k\) to the total number of cells in the dataset. This score captures the relative frequency of simulated cells to real cells while being robust to changes to \(N_s\).
sessionInfo()
## R version 4.0.3 (2020-10-10)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 18.04.5 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.12-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.12-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] bluster_1.0.0 scater_1.18.0
## [3] ggplot2_3.3.2 scran_1.18.0
## [5] scuttle_1.0.0 ensembldb_2.14.0
## [7] AnnotationFilter_1.14.0 GenomicFeatures_1.42.0
## [9] AnnotationDbi_1.52.0 scRNAseq_2.3.17
## [11] SingleCellExperiment_1.12.0 SummarizedExperiment_1.20.0
## [13] Biobase_2.50.0 GenomicRanges_1.42.0
## [15] GenomeInfoDb_1.26.0 IRanges_2.24.0
## [17] S4Vectors_0.28.0 BiocGenerics_0.36.0
## [19] MatrixGenerics_1.2.0 matrixStats_0.57.0
## [21] scDblFinder_1.4.0 BiocStyle_2.18.0
##
## loaded via a namespace (and not attached):
## [1] Rtsne_0.15 ggbeeswarm_0.6.0
## [3] colorspace_1.4-1 ellipsis_0.3.1
## [5] XVector_0.30.0 BiocNeighbors_1.8.0
## [7] farver_2.0.3 bit64_4.0.5
## [9] interactiveDisplayBase_1.28.0 xml2_1.3.2
## [11] sparseMatrixStats_1.2.0 knitr_1.30
## [13] Rsamtools_2.6.0 dbplyr_1.4.4
## [15] shiny_1.5.0 BiocManager_1.30.10
## [17] compiler_4.0.3 httr_1.4.2
## [19] dqrng_0.2.1 lazyeval_0.2.2
## [21] assertthat_0.2.1 Matrix_1.2-18
## [23] fastmap_1.0.1 limma_3.46.0
## [25] later_1.1.0.1 BiocSingular_1.6.0
## [27] htmltools_0.5.0 prettyunits_1.1.1
## [29] tools_4.0.3 rsvd_1.0.3
## [31] igraph_1.2.6 gtable_0.3.0
## [33] glue_1.4.2 GenomeInfoDbData_1.2.4
## [35] dplyr_1.0.2 rappdirs_0.3.1
## [37] Rcpp_1.0.5 vctrs_0.3.4
## [39] Biostrings_2.58.0 rtracklayer_1.50.0
## [41] ExperimentHub_1.16.0 DelayedMatrixStats_1.12.0
## [43] xfun_0.18 stringr_1.4.0
## [45] beachmat_2.6.0 mime_0.9
## [47] lifecycle_0.2.0 irlba_2.3.3
## [49] statmod_1.4.35 XML_3.99-0.5
## [51] AnnotationHub_2.22.0 edgeR_3.32.0
## [53] zlibbioc_1.36.0 scales_1.1.1
## [55] ProtGenerics_1.22.0 hms_0.5.3
## [57] promises_1.1.1 yaml_2.2.1
## [59] curl_4.3 memoise_1.1.0
## [61] gridExtra_2.3 biomaRt_2.46.0
## [63] stringi_1.5.3 RSQLite_2.2.1
## [65] BiocVersion_3.12.0 BiocParallel_1.24.0
## [67] rlang_0.4.8 pkgconfig_2.0.3
## [69] bitops_1.0-6 evaluate_0.14
## [71] lattice_0.20-41 purrr_0.3.4
## [73] labeling_0.4.2 GenomicAlignments_1.26.0
## [75] cowplot_1.1.0 bit_4.0.4
## [77] tidyselect_1.1.0 magrittr_1.5
## [79] bookdown_0.21 R6_2.4.1
## [81] magick_2.5.0 generics_0.0.2
## [83] DelayedArray_0.16.0 DBI_1.1.0
## [85] withr_2.3.0 pillar_1.4.6
## [87] RCurl_1.98-1.2 tibble_3.0.4
## [89] crayon_1.3.4 xgboost_1.2.0.1
## [91] BiocFileCache_1.14.0 rmarkdown_2.5
## [93] viridis_0.5.1 progress_1.2.2
## [95] locfit_1.5-9.4 grid_4.0.3
## [97] data.table_1.13.2 blob_1.2.1
## [99] digest_0.6.27 xtable_1.8-4
## [101] httpuv_1.5.4 openssl_1.4.3
## [103] munsell_0.5.0 beeswarm_0.2.3
## [105] viridisLite_0.3.0 vipor_0.4.5
## [107] askpass_1.1