Chapter 7 Advanced options

7.1 Preconstructed indices

Advanced users can split the SingleR() workflow into two separate training and classification steps. This means that training (e.g., marker detection, assembling of nearest-neighbor indices) only needs to be performed once for any reference. The resulting data structure can then be re-used across multiple classifications with different test datasets, provided the gene annotation in the test dataset is identical to or a superset of the genes in the training set. To illustrate, we will consider the DICE reference dataset (Schmiedel et al. 2018) from the celldex package.

## class: SummarizedExperiment 
## dim: 29914 1561 
## metadata(0):
## assays(1): logcounts
## rownames(29914): ENSG00000121410 ENSG00000268895 ... ENSG00000159840
##   ENSG00000074755
## rowData names(0):
## colnames(1561): TPM_1 TPM_2 ... TPM_101 TPM_102
## colData names(3): label.main label.fine label.ont
## 
##                   B cells, naive                 Monocytes, CD14+ 
##                              106                              106 
##                 Monocytes, CD16+                         NK cells 
##                              105                              105 
##               T cells, CD4+, TFH               T cells, CD4+, Th1 
##                              104                              104 
##              T cells, CD4+, Th17            T cells, CD4+, Th1_17 
##                              104                              104 
##               T cells, CD4+, Th2       T cells, CD4+, memory TREG 
##                              104                              104 
##             T cells, CD4+, naive        T cells, CD4+, naive TREG 
##                              103                              104 
## T cells, CD4+, naive, stimulated             T cells, CD8+, naive 
##                              102                              104 
## T cells, CD8+, naive, stimulated 
##                              102

Let’s say we want to use the DICE reference to annotate the PBMC dataset from Chapter 1.

We use the trainSingleR() function to do all the necessary calculations that are independent of the test dataset. (Almost; see comments below about common.) This yields a list of various components that contains all identified marker genes and precomputed rank indices to be used in the score calculation. We can also turn on aggregation with aggr.ref=TRUE (Section @ref(pseudo-bulk aggregation)) to further reduce computational work.

We then use the trained object to annotate our dataset of interest through the classifySingleR() function. As we can see, this yields exactly the same result as applying SingleR() directly. The advantage here is that trained can be re-used for multiple classifySingleR() calls - possibly on different datasets - without having to repeat unnecessary steps when the reference is unchanged.

## 
##             B cells, naive           Monocytes, CD14+ 
##                        346                        514 
##           Monocytes, CD16+                   NK cells 
##                        187                        325 
##         T cells, CD4+, TFH         T cells, CD4+, Th1 
##                        456                        180 
##        T cells, CD4+, Th17      T cells, CD4+, Th1_17 
##                         60                         73 
##         T cells, CD4+, Th2 T cells, CD4+, memory TREG 
##                         46                        163 
##       T cells, CD4+, naive  T cells, CD4+, naive TREG 
##                         97                         41 
##       T cells, CD8+, naive 
##                        212
## [1] TRUE

The big caveat is that the universe of genes in the test dataset must be a superset of that the reference. This is the reason behind the intersection to common genes and the subsequent subsetting of dice. Practical use of preconstructed indices is best combined with some prior information about the gene-level annotation; for example, we might know that we always use a particular version of the Ensembl gene models, so we would filter out any genes in the reference dataset that are not in our test datasets.

7.2 Parallelization

Parallelization is an obvious approach to increasing annotation throughput. This is done using the framework in the BiocParallel package, which provides several options for parallelization depending on the available hardware. On POSIX-compliant systems (i.e., Linux and MacOS), the simplest method is to use forking by passing MulticoreParam() to the BPPARAM= argument:

Alternatively, one can use separate processes with SnowParam(), which is slower but can be used on all systems - including Windows, our old nemesis.

## [1] TRUE

When working on a cluster, passing BatchtoolsParam() to SingleR() allows us to seamlessly interface with various job schedulers like SLURM, LSF and so on. This permits heavy-duty parallelization across hundreds of CPUs for highly intensive jobs, though often some configuration is required - see the vignette for more details.

7.3 Approximate algorithms

It is possible to sacrifice accuracy to squeeze more speed out of SingleR. The most obvious approach is to simply turn off the fine-tuning with fine.tune=FALSE, which avoids the time-consuming fine-tuning iterations. When the reference labels are well-separated, this is probably an acceptable trade-off.

## 
##       B cells     Monocytes      NK cells T cells, CD4+ T cells, CD8+ 
##           348           705           357           950           340

Another approximation is based on the fact that the initial score calculation is done using a nearest-neighbors search. By default, this is an exact seach but we can switch to an approximate algorithm via the BNPARAM= argument. In the example below, we use the Annoy algorithm via the BiocNeighbors framework, which yields mostly similar results. (Note, though, that the Annoy method does involve a considerable amount of overhead, so for small jobs it will actually be slower than the exact search.)

##                
##                 B cells Monocytes NK cells T cells, CD4+ T cells, CD8+
##   B cells           348         0        0             0             0
##   Monocytes           0       705        0             0             0
##   NK cells            0         0      357             0             0
##   T cells, CD4+       0         0        0           950             0
##   T cells, CD8+       0         0        0             0           340

7.4 Cluster-level annotation

The default philosophy of SingleR is to perform annotation of each individual cell in the test dataset. An alternative strategy is to perform annotation of aggregated profiles for groups or clusters of cells. To demonstrate, we will perform a quick-and-dirty clustering of our PBMC dataset with a variety of Bioconductor packages.

By passing clusters= to SingleR(), we direct the function to compute an aggregated profile per cluster. Annotation is then performed on the cluster-level profiles rather than on the single-cell level. This has the major advantage of being much faster to compute as there are obviously fewer clusters than cells; it is also easier to interpret as it directly returns the likely cell type identity of each cluster.

## DataFrame with 11 rows and 5 columns
##                             scores  first.labels     tuning.scores
##                           <matrix>   <character>       <DataFrame>
## 1  0.1527906:0.265249:0.597833:...      NK cells 0.380985:0.365707
## 2  0.2084180:0.234905:0.360103:... T cells, CD8+ 0.554983:0.531042
## 3  0.0559597:0.264805:0.727242:...      NK cells 0.727242:0.399960
## 4  0.1681531:0.756536:0.207553:...     Monocytes 0.756536:0.207553
## 5  0.1420008:0.782841:0.209056:...     Monocytes 0.782841:0.209056
## 6  0.2153801:0.272004:0.407546:... T cells, CD4+ 0.680426:0.581005
## 7  0.2319068:0.597738:0.213721:...     Monocytes 0.597738:0.231907
## 8  0.6519196:0.331731:0.210751:...       B cells 0.651920:0.331731
## 9  0.1655003:0.756073:0.261631:...     Monocytes 0.756073:0.261631
## 10 0.2535745:0.258933:0.328738:... T cells, CD4+ 0.363252:0.306328
## 11 0.0713926:0.223101:0.117047:...     Monocytes 0.223101:0.117047
##           labels pruned.labels
##      <character>   <character>
## 1  T cells, CD4+ T cells, CD4+
## 2  T cells, CD4+ T cells, CD4+
## 3       NK cells      NK cells
## 4      Monocytes     Monocytes
## 5      Monocytes     Monocytes
## 6  T cells, CD4+ T cells, CD4+
## 7      Monocytes            NA
## 8        B cells       B cells
## 9      Monocytes     Monocytes
## 10 T cells, CD4+ T cells, CD4+
## 11     Monocytes            NA

This approach assumes that each cluster in the test dataset corresponds to exactly one reference label. If a cluster actually contains a mixture of multiple labels, this will not be reflected in its lone assigned label. (We note that it would be very difficult to determine the composition of the mixture from the SingleR() scores.) Indeed, there is no guarantee that the clustering is driven by the same factors that distinguish the reference labels, decreasing the reliability of the annotations when novel heterogeneity is present in the test dataset. The default per-cell strategy is safer and provides more information about the ambiguity of the annotations, which is important for closely related labels where a close correspondence between clusters and labels cannot be expected.

Session information

R version 4.0.3 (2020-10-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.1 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] scater_1.18.0               ggplot2_3.3.2              
 [3] bluster_1.0.0               scran_1.18.0               
 [5] scuttle_1.0.0               BiocNeighbors_1.8.0        
 [7] BiocParallel_1.24.0         SingleR_1.4.0              
 [9] TENxPBMCData_1.8.0          HDF5Array_1.18.0           
[11] rhdf5_2.34.0                DelayedArray_0.16.0        
[13] Matrix_1.2-18               SingleCellExperiment_1.12.0
[15] ensembldb_2.14.0            AnnotationFilter_1.14.0    
[17] GenomicFeatures_1.42.0      AnnotationDbi_1.52.0       
[19] celldex_1.0.0               SummarizedExperiment_1.20.0
[21] Biobase_2.50.0              GenomicRanges_1.42.0       
[23] GenomeInfoDb_1.26.0         IRanges_2.24.0             
[25] S4Vectors_0.28.0            BiocGenerics_0.36.0        
[27] MatrixGenerics_1.2.0        matrixStats_0.57.0         
[29] BiocStyle_2.18.0            rebook_1.0.0               

loaded via a namespace (and not attached):
  [1] snow_0.4-3                    AnnotationHub_2.22.0         
  [3] BiocFileCache_1.14.0          igraph_1.2.6                 
  [5] lazyeval_0.2.2                digest_0.6.27                
  [7] htmltools_0.5.0               viridis_0.5.1                
  [9] magrittr_1.5                  memoise_1.1.0                
 [11] limma_3.46.0                  Biostrings_2.58.0            
 [13] askpass_1.1                   prettyunits_1.1.1            
 [15] colorspace_1.4-1              blob_1.2.1                   
 [17] rappdirs_0.3.1                xfun_0.19                    
 [19] dplyr_1.0.2                   callr_3.5.1                  
 [21] crayon_1.3.4                  RCurl_1.98-1.2               
 [23] graph_1.68.0                  glue_1.4.2                   
 [25] gtable_0.3.0                  zlibbioc_1.36.0              
 [27] XVector_0.30.0                BiocSingular_1.6.0           
 [29] Rhdf5lib_1.12.0               scales_1.1.1                 
 [31] DBI_1.1.0                     edgeR_3.32.0                 
 [33] Rcpp_1.0.5                    viridisLite_0.3.0            
 [35] xtable_1.8-4                  progress_1.2.2               
 [37] dqrng_0.2.1                   bit_4.0.4                    
 [39] rsvd_1.0.3                    httr_1.4.2                   
 [41] ellipsis_0.3.1                farver_2.0.3                 
 [43] pkgconfig_2.0.3               XML_3.99-0.5                 
 [45] CodeDepends_0.6.5             dbplyr_1.4.4                 
 [47] locfit_1.5-9.4                labeling_0.4.2               
 [49] tidyselect_1.1.0              rlang_0.4.8                  
 [51] later_1.1.0.1                 munsell_0.5.0                
 [53] BiocVersion_3.12.0            tools_4.0.3                  
 [55] generics_0.1.0                RSQLite_2.2.1                
 [57] ExperimentHub_1.16.0          evaluate_0.14                
 [59] stringr_1.4.0                 fastmap_1.0.1                
 [61] yaml_2.2.1                    processx_3.4.4               
 [63] knitr_1.30                    bit64_4.0.5                  
 [65] purrr_0.3.4                   sparseMatrixStats_1.2.0      
 [67] mime_0.9                      xml2_1.3.2                   
 [69] biomaRt_2.46.0                compiler_4.0.3               
 [71] beeswarm_0.2.3                curl_4.3                     
 [73] interactiveDisplayBase_1.28.0 tibble_3.0.4                 
 [75] statmod_1.4.35                stringi_1.5.3                
 [77] ps_1.4.0                      lattice_0.20-41              
 [79] ProtGenerics_1.22.0           vctrs_0.3.4                  
 [81] pillar_1.4.6                  lifecycle_0.2.0              
 [83] rhdf5filters_1.2.0            BiocManager_1.30.10          
 [85] cowplot_1.1.0                 bitops_1.0-6                 
 [87] irlba_2.3.3                   httpuv_1.5.4                 
 [89] rtracklayer_1.50.0            R6_2.5.0                     
 [91] bookdown_0.21                 promises_1.1.1               
 [93] gridExtra_2.3                 vipor_0.4.5                  
 [95] codetools_0.2-16              assertthat_0.2.1             
 [97] openssl_1.4.3                 withr_2.3.0                  
 [99] GenomicAlignments_1.26.0      Rsamtools_2.6.0              
[101] GenomeInfoDbData_1.2.4        hms_0.5.3                    
[103] grid_4.0.3                    beachmat_2.6.0               
[105] rmarkdown_2.5                 DelayedMatrixStats_1.12.0    
[107] Rtsne_0.15                    shiny_1.5.0                  
[109] ggbeeswarm_0.6.0             

Bibliography

Schmiedel, Benjamin J., Divya Singh, Ariel Madrigal, Alan G. Valdovino-Gonzalez, Brandie M. White, Jose Zapardiel-Gonzalo, Brendan Ha, et al. 2018. “Impact of Genetic Polymorphisms on Human Immune Cell Gene Expression.” Cell 175 (6): 1701–1715.e16. https://doi.org/10.1016/j.cell.2018.10.022.