Contents

1 Introduction

This vignette describes how each of the included clustering methods was applied to the collection of data sets in order to generate the clustering result summaries provided with the package. It also shows how to apply a new clustering method to the included data sets, to generate results that can be compared to those already included.

2 Applying a new clustering algorithm to a provided data set

The code below describes how we applied each of the included clustering methods to the data sets for our paper (Duò, Robinson, and Soneson 2018). The apply_*() functions, describing how the respective clustering methods were run, are available from the GitHub repository corresponding to the publication. In order to apply a new clustering algorithm to one of the data sets using the same framework, it is necessary to generate a function with the same format. The input arguments to this function should be:

The function should return a list with three elements:

If the method does not allow specification of the desired number of clusters, but has another parameter affecting the resolution, this can be accommodated as well (see the solution for Seurat in the code below).

First, load the package and define the data set and clustering method to use (note that in order to apply a method named <method>, there has to be a function named apply_<method>(), with the above specifications, available in the workspace).

suppressPackageStartupMessages({
  library(DuoClustering2018)
})

scename <- "sce_filteredExpr10_Koh"
sce <- sce_filteredExpr10_Koh()
## snapshotDate(): 2021-03-24
## see ?DuoClustering2018 and browseVignettes('DuoClustering2018') for documentation
## loading from cache
method <- "PCAHC"

Next, define the list of hyperparameter values. The package contains the hyperparameter values for the methods included in our paper.

## Load parameter files. General dataset and method parameters as well as
## dataset/method-specific parameters
params <- duo_clustering_all_parameter_settings_v2()[[paste0(scename, "_", 
                                                             method)]]
## snapshotDate(): 2021-03-24
## see ?DuoClustering2018 and browseVignettes('DuoClustering2018') for documentation
## loading from cache
params
## $nPC
## [1] 30
## 
## $range_clusters
##  [1]  2  3  4  5  6  7  8  9 10 11 12 13 14 15

Finally, define the number of times to apply the clustering method (for each value of the number of clusters), and run the clustering across a range of imposed numbers of clusters (defined in the parameter list).

## Set number of times to run clustering for each k
n_rep <- 5

## Run clustering
set.seed(1234)
L <- lapply(seq_len(n_rep), function(i) {  ## For each run
  cat(paste0("run = ", i, "\n"))
  if (method == "Seurat") {
    tmp <- lapply(params$range_resolutions, function(resolution) {  
      ## For each resolution
      cat(paste0("resolution = ", resolution, "\n"))
      ## Run clustering
      res <- get(paste0("apply_", method))(sce = sce, params = params, 
                                           resolution = resolution)
      
      ## Put output in data frame
      df <- data.frame(dataset = scename, 
                       method = method, 
                       cell = names(res$cluster),
                       run = i,
                       k = length(unique(res$cluster)),
                       resolution = resolution,
                       cluster = res$cluster,
                       stringsAsFactors = FALSE, row.names = NULL)
      tm <- data.frame(dataset = scename, 
                       method = method,
                       run = i, 
                       k = length(unique(res$cluster)),
                       resolution = resolution,
                       user.self = res$st[["user.self"]],
                       sys.self = res$st[["sys.self"]],
                       user.child = res$st[["user.child"]],
                       sys.child = res$st[["sys.child"]],
                       elapsed = res$st[["elapsed"]],
                       stringsAsFactors = FALSE, row.names = NULL)
      kest <- data.frame(dataset = scename, 
                         method = method,
                         run = i, 
                         k = length(unique(res$cluster)),
                         resolution = resolution,
                         est_k = res$est_k,
                         stringsAsFactors = FALSE, row.names = NULL)
      list(clusters = df, timing = tm, kest = kest)
    })  ## End for each resolution
  } else {
    tmp <- lapply(params$range_clusters, function(k) {  ## For each k
      cat(paste0("k = ", k, "\n"))
      ## Run clustering
      res <- get(paste0("apply_", method))(sce = sce, params = params, k = k)
      
      ## Put output in data frame
      df <- data.frame(dataset = scename, 
                       method = method, 
                       cell = names(res$cluster),
                       run = i,
                       k = k,
                       resolution = NA,
                       cluster = res$cluster,
                       stringsAsFactors = FALSE, row.names = NULL)
      tm <- data.frame(dataset = scename, 
                       method = method,
                       run = i, 
                       k = k,
                       resolution = NA,
                       user.self = res$st[["user.self"]],
                       sys.self = res$st[["sys.self"]],
                       user.child = res$st[["user.child"]],
                       sys.child = res$st[["sys.child"]],
                       elapsed = res$st[["elapsed"]],
                       stringsAsFactors = FALSE, row.names = NULL)
      kest <- data.frame(dataset = scename, 
                         method = method,
                         run = i, 
                         k = k,
                         resolution = NA,
                         est_k = res$est_k,
                         stringsAsFactors = FALSE, row.names = NULL)
      list(clusters = df, timing = tm, kest = kest)
    })  ## End for each k
  }
  
  ## Summarize across different values of k
  assignments <- do.call(rbind, lapply(tmp, function(w) w$clusters))
  timings <- do.call(rbind, lapply(tmp, function(w) w$timing))
  k_estimates <- do.call(rbind, lapply(tmp, function(w) w$kest))
  list(assignments = assignments, timings = timings, k_estimates = k_estimates)
})  ## End for each run

## Summarize across different runs
assignments <- do.call(rbind, lapply(L, function(w) w$assignments))
timings <- do.call(rbind, lapply(L, function(w) w$timings))
k_estimates <- do.call(rbind, lapply(L, function(w) w$k_estimates))

## Add true group for each cell
truth <- data.frame(cell = as.character(rownames(colData(sce))),
                    trueclass = as.character(colData(sce)$phenoid),
                    stringsAsFactors = FALSE)
assignments$trueclass <- truth$trueclass[match(assignments$cell, truth$cell)]

## Combine results
res <- list(assignments = assignments, timings = timings,
            k_estimates = k_estimates)

df <- dplyr::full_join(res$assignments %>%
                         dplyr::select(dataset, method, cell, run, k, 
                                       resolution, cluster, trueclass),
                       res$k_estimates %>%
                         dplyr::select(dataset, method, run, k, 
                                       resolution, est_k)
) %>% dplyr::full_join(res$timings %>% dplyr::select(dataset, method, run, k,
                                                     resolution, elapsed))

The resulting df data frames can then be combined across data sets, filterings and methods and used as input to the provided plotting functions.

3 Session info

sessionInfo()
## R Under development (unstable) (2021-02-10 r79979)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.2 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.13-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.13-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] plyr_1.8.6                   ExperimentHub_1.17.1        
##  [3] AnnotationHub_2.23.2         BiocFileCache_1.15.1        
##  [5] dbplyr_2.1.0                 tidyr_1.1.3                 
##  [7] dplyr_1.0.5                  DuoClustering2018_1.9.0     
##  [9] SingleCellExperiment_1.13.12 SummarizedExperiment_1.21.1 
## [11] Biobase_2.51.0               GenomicRanges_1.43.3        
## [13] GenomeInfoDb_1.27.8          IRanges_2.25.6              
## [15] S4Vectors_0.29.9             BiocGenerics_0.37.1         
## [17] MatrixGenerics_1.3.1         matrixStats_0.58.0          
## [19] BiocStyle_2.19.1            
## 
## loaded via a namespace (and not attached):
##  [1] bitops_1.0-6                  bit64_4.0.5                  
##  [3] filelock_1.0.2                httr_1.4.2                   
##  [5] tools_4.1.0                   bslib_0.2.4                  
##  [7] utf8_1.2.1                    R6_2.5.0                     
##  [9] DBI_1.1.1                     colorspace_2.0-0             
## [11] withr_2.4.1                   tidyselect_1.1.0             
## [13] gridExtra_2.3                 bit_4.0.4                    
## [15] curl_4.3                      compiler_4.1.0               
## [17] DelayedArray_0.17.10          labeling_0.4.2               
## [19] bookdown_0.21                 sass_0.3.1                   
## [21] scales_1.1.1                  rappdirs_0.3.3               
## [23] stringr_1.4.0                 digest_0.6.27                
## [25] rmarkdown_2.7                 XVector_0.31.1               
## [27] pkgconfig_2.0.3               htmltools_0.5.1.1            
## [29] highr_0.8                     fastmap_1.1.0                
## [31] rlang_0.4.10                  ggthemes_4.2.4               
## [33] RSQLite_2.2.4                 shiny_1.6.0                  
## [35] farver_2.1.0                  jquerylib_0.1.3              
## [37] generics_0.1.0                jsonlite_1.7.2               
## [39] mclust_5.4.7                  RCurl_1.98-1.3               
## [41] magrittr_2.0.1                GenomeInfoDbData_1.2.4       
## [43] Matrix_1.3-2                  Rcpp_1.0.6                   
## [45] munsell_0.5.0                 fansi_0.4.2                  
## [47] viridis_0.5.1                 lifecycle_1.0.0              
## [49] stringi_1.5.3                 yaml_2.2.1                   
## [51] debugme_1.1.0                 zlibbioc_1.37.0              
## [53] grid_4.1.0                    blob_1.2.1                   
## [55] promises_1.2.0.1              crayon_1.4.1                 
## [57] lattice_0.20-41               Biostrings_2.59.2            
## [59] KEGGREST_1.31.1               magick_2.7.1                 
## [61] knitr_1.31                    pillar_1.5.1                 
## [63] reshape2_1.4.4                glue_1.4.2                   
## [65] BiocVersion_3.13.1            evaluate_0.14                
## [67] BiocManager_1.30.10           png_0.1-7                    
## [69] vctrs_0.3.6                   httpuv_1.5.5                 
## [71] gtable_0.3.0                  purrr_0.3.4                  
## [73] assertthat_0.2.1              cachem_1.0.4                 
## [75] ggplot2_3.3.3                 xfun_0.22                    
## [77] mime_0.10                     xtable_1.8-4                 
## [79] later_1.1.0.1                 viridisLite_0.3.0            
## [81] tibble_3.1.0                  AnnotationDbi_1.53.1         
## [83] memoise_2.0.0                 ellipsis_0.3.1               
## [85] interactiveDisplayBase_1.29.0

References

Duò, A, MD Robinson, and D Soneson. 2018. “A Systematic Performance Evaluation of Clustering Methods for Single-Cell RNA-seq Data.” F1000Research 7: 1141.