# Performing scClassify using pretrained model

Yingxin Lin1

1School of Mathematics and Statistics, The University of Sydney, Australia

#### 27 October 2023

# 1 Introduction

A common application of single-cell RNA sequencing (RNA-seq) data is to identify discrete cell types. To take advantage of the large collection of well-annotated scRNA-seq datasets, `scClassify` package implements a set of methods to perform accurate cell type classification based on ensemble learning and sample size calculation.

This vignette will provide an example showing how users can use a pretrained model of scClassify to predict cell types. A pretrained model is a `scClassifyTrainModel` object returned by `train_scClassify()`. A list of pretrained model can be found in https://sydneybiox.github.io/scClassify/index.html.

First, install `scClassify`, install `BiocManager` and use `BiocManager::install` to install `scClassify` package.

    # installation of scClassify
    if (!requireNamespace("BiocManager", quietly = TRUE)) {
      install.packages("BiocManager")
    }
    BiocManager::install("scClassify")

# 2 Setting up the data

We assume that you have log-transformed (size-factor normalized) matrices as query datasets, where each row refers to a gene and each column a cell. For demonstration purposes, we will take a subset of single-cell pancreas datasets from one independent study (Wang et al.).

    library(scClassify)
    data("scClassify_example")
    wang_cellTypes <- scClassify_example$wang_cellTypes
    exprsMat_wang_subset <- scClassify_example$exprsMat_wang_subset
    exprsMat_wang_subset <- as(exprsMat_wang_subset, "dgCMatrix")

Here, we load our pretrained model using a subset of the Xin et al. human pancreas dataset as our reference data.

First, let us check basic information relating to our pretrained model.

    data("trainClassExample_xin")
    trainClassExample_xin
    #> Class: scClassifyTrainModel
    #> Model name: training
    #> Feature selection methods: limma
    #> Number of cells in the training data: 674
    #> Number of cell types in the training data: 4

In this pretrained model, we have selected the genes based on Differential Expression using limma. To check the genes that are available in the pretrained model:

    features(trainClassExample_xin)
    #> [1] "limma"

We can also visualise the cell type tree of the reference data.

    plotCellTypeTree(cellTypeTree(trainClassExample_xin))

# 3 Running scClassify

Next, we perform `predict_scClassify` with our pretrained model `trainRes = trainClassExample` to predict the cell types of our query data matrix `exprsMat_wang_subset_sparse`. Here, we used `pearson` and `spearman` as similarity metrics.

    pred_res <- predict_scClassify(exprsMat_test = exprsMat_wang_subset,
                                   trainRes = trainClassExample_xin,
                                   cellTypes_test = wang_cellTypes,
                                   algorithm = "WKNN",
                                   features = c("limma"),
                                   similarity = c("pearson", "spearman"),
                                   prob_threshold = 0.7,
                                   verbose = TRUE)
    #> Performing unweighted ensemble learning...
    #> Using parameters:
    #> similarity  algorithm   features
    #>  "pearson"     "WKNN"    "limma"
    #> [1] "Using dynamic correlation cutoff..."
    #> [1] "Using dynamic correlation cutoff..."
    #> classify_res
    #>                correct   correctly unassigned           intermediate
    #>            0.704590818            0.239520958            0.000000000
    #> incorrectly unassigned         error assigned          misclassified
    #>            0.000000000            0.051896208            0.003992016
    #> Using parameters:
    #> similarity  algorithm   features
    #> "spearman"     "WKNN"    "limma"
    #> [1] "Using dynamic correlation cutoff..."
    #> [1] "Using dynamic correlation cutoff..."
    #> classify_res
    #>                correct   correctly unassigned           intermediate
    #>            0.702594810            0.013972056            0.000000000
    #> incorrectly unassigned         error assigned          misclassified
    #>            0.001996008            0.277445110            0.003992016
    #> weights for each base method:
    #> [1] NA NA

Noted that the `cellType_test` is not a required input. For datasets with unknown labels, users can simply leave it as `cellType_test = NULL`.

Prediction results for pearson as the similarity metric:

    table(pred_res$pearson_WKNN_limma$predRes, wang_cellTypes)
    #>                   wang_cellTypes
    #>                    acinar alpha beta delta ductal gamma stellate
    #>   alpha                 0   206    0     0      0     2        0
    #>   beta                  0     0  118     0      1     0        0
    #>   beta_delta_gamma      0     0    0     0     25     0        0
    #>   delta                 0     0    0    10      0     0        0
    #>   gamma                 0     0    0     0      0    19        0
    #>   unassigned            5     0    0     0     70     0       45

Prediction results for spearman as the similarity metric:

    table(pred_res$spearman_WKNN_limma$predRes, wang_cellTypes)
    #>                   wang_cellTypes
    #>                    acinar alpha beta delta ductal gamma stellate
    #>   alpha                 0   206    0     0      0     2        2
    #>   beta                  2     0  118     0     29     0        6
    #>   beta_delta_gamma      1     0    0     0     66     0       31
    #>   delta                 0     0    0    10      0     0        2
    #>   gamma                 0     0    0     0      0    18        0
    #>   unassigned            2     0    0     0      1     1        4

# 4 Session Info

    sessionInfo()
    #> R version 4.3.1 (2023-06-16)
    #> Platform: aarch64-apple-darwin20 (64-bit)
    #> Running under: macOS Ventura 13.6.1
    #>
    #> Matrix products: default
    #> BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib
    #> LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0
    #>
    #> locale:
    #> [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
    #>
    #> time zone: America/New_York
    #> tzcode source: internal
    #>
    #> attached base packages:
    #> [1] stats     graphics  grDevices utils     datasets  methods   base
    #>
    #> other attached packages:
    #> [1] scClassify_1.14.0 BiocStyle_2.30.0
    #>
    #> loaded via a namespace (and not attached):
    #>   [1] bitops_1.0-7                gridExtra_2.3
    #>   [3] rlang_1.1.1                 magrittr_2.0.3
    #>   [5] matrixStats_1.0.0           compiler_4.3.1
    #>   [7] mgcv_1.9-0                  DelayedMatrixStats_1.24.0
    #>   [9] vctrs_0.6.3                 reshape2_1.4.4
    #>  [11] stringr_1.5.0               pkgconfig_2.0.3
    #>  [13] crayon_1.5.2                fastmap_1.1.1
    #>  [15] magick_2.7.4                XVector_0.42.0
    #>  [17] labeling_0.4.2              ggraph_2.1.0
    #>  [19] utf8_1.2.3                  rmarkdown_2.23
    #>  [21] purrr_1.0.1                 xfun_0.39
    #>  [23] zlibbioc_1.48.0             cachem_1.0.8
    #>  [25] GenomeInfoDb_1.38.0         jsonlite_1.8.7
    #>  [27] highr_0.10                  rhdf5filters_1.14.0
    #>  [29] DelayedArray_0.28.0         Rhdf5lib_1.24.0
    #>  [31] BiocParallel_1.36.0         tweenr_2.0.2
    #>  [33] parallel_4.3.1              cluster_2.1.4
    #>  [35] R6_2.5.1                    bslib_0.5.0
    #>  [37] stringi_1.7.12              limma_3.58.0
    #>  [39] diptest_0.76-0              GenomicRanges_1.54.0
    #>  [41] jquerylib_0.1.4             Rcpp_1.0.11
    #>  [43] bookdown_0.34               SummarizedExperiment_1.32.0
    #>  [45] knitr_1.43                  mixtools_2.0.0
    #>  [47] IRanges_2.36.0              Matrix_1.6-0
    #>  [49] splines_4.3.1               igraph_1.5.0
    #>  [51] tidyselect_1.2.0            abind_1.4-5
    #>  [53] yaml_2.3.7                  hopach_2.62.0
    #>  [55] viridis_0.6.3               codetools_0.2-19
    #>  [57] minpack.lm_1.2-3            Cepo_1.8.0
    #>  [59] lattice_0.21-8              tibble_3.2.1
    #>  [61] plyr_1.8.8                  Biobase_2.62.0
    #>  [63] withr_2.5.0                 evaluate_0.21
    #>  [65] survival_3.5-5              RcppParallel_5.1.7
    #>  [67] proxy_0.4-27                polyclip_1.10-4
    #>  [69] kernlab_0.9-32              pillar_1.9.0
    #>  [71] BiocManager_1.30.22         MatrixGenerics_1.14.0
    #>  [73] stats4_4.3.1                plotly_4.10.2
    #>  [75] generics_0.1.3              RCurl_1.98-1.12
    #>  [77] S4Vectors_0.40.1            ggplot2_3.4.2
    #>  [79] sparseMatrixStats_1.14.0    munsell_0.5.0
    #>  [81] scales_1.2.1                glue_1.6.2
    #>  [83] lazyeval_0.2.2              proxyC_0.3.3
    #>  [85] tools_4.3.1                 data.table_1.14.8
    #>  [87] graphlayouts_1.0.0          tidygraph_1.2.3
    #>  [89] rhdf5_2.46.0                grid_4.3.1
    #>  [91] tidyr_1.3.0                 colorspace_2.1-0
    #>  [93] SingleCellExperiment_1.24.0 nlme_3.1-162
    #>  [95] GenomeInfoDbData_1.2.10     patchwork_1.1.2
    #>  [97] ggforce_0.4.1               HDF5Array_1.30.0
    #>  [99] cli_3.6.1                   fansi_1.0.4
    #> [101] segmented_1.6-4             S4Arrays_1.2.0
    #> [103] viridisLite_0.4.2           dplyr_1.1.2
    #> [105] gtable_0.3.3                sass_0.4.6
    #> [107] digest_0.6.33               BiocGenerics_0.48.0
    #> [109] SparseArray_1.2.0           ggrepel_0.9.3
    #> [111] htmlwidgets_1.6.2           farver_2.1.1
    #> [113] htmltools_0.5.5             lifecycle_1.0.3
    #> [115] httr_1.4.6                  statmod_1.5.0
    #> [117] MASS_7.3-60