# Training basic model classifying a cell type from scRNA-seq data

#### Vy Nguyen

#### 2021-10-26

## Introduction

One of key functions of the scAnnotatR package is to provide users easy tools to train their own model classifying new cell types from labeled scRNA-seq data.

This vignette shows how to train a basic classification model for an independent cell type, which is not a child of any other cell type.

## Preparing train object and test object

The workflow starts with either a Seurat or SingleCellExperiment object where cells have already been assigned to different cell types.

To do this, users may have annotated scRNA-seq data (by a FACS-sorting process, for example), create a Seurat/ SingleCellExperiment (SCE) object based on the sequencing data and assign the predetermined cell types as cell meta data. If the scRNA-seq data has not been annotated yet, another possible approach is to follow the basic workflow (Seurat, for example) until assigning cell type identity to clusters.

In this vignette, we use the human lung dataset from Zilionis et al., 2019, which is available in the scRNAseq (2.4.0) library. The dataset is stored as a SCE object.

To start the training workflow, we first install and load the necessary libraries.

    # use BiocManager to install from Bioconductor
    if (!requireNamespace("BiocManager", quietly = TRUE))
        install.packages("BiocManager")

    # the scAnnotatR package
    if (!require(scAnnotatR))
      BiocManager::install("scAnnotatR")

    # we use the scRNAseq package to load example data
    if (!require(scRNAseq))
      BiocManager::install("scRNAseq")

    library(scRNAseq)
    library(scAnnotatR)

First, we load the dataset. To reduce the computational complexity of this vignette, we only use the first 5000 cells of the dataset.

    zilionis <- ZilionisLungData()
    #> snapshotDate(): 2021-10-18
    #> see ?scRNAseq and browseVignettes('scRNAseq') for documentation
    #> loading from cache
    #> see ?scRNAseq and browseVignettes('scRNAseq') for documentation
    #> loading from cache
    zilionis <- zilionis[, 1:5000]

We split this dataset into two parts, one for the training and the other for the testing.

    pivot = ncol(zilionis)%/%2
    train_set <- zilionis[, 1:pivot]
    test_set <- zilionis[, (1+pivot):ncol(zilionis)]

In this dataset, the cell type meta data is stored in the Most likely LM22 cell type slot of the SingleCellExperiment object (in both the train object and test object).

If the cell type is stored not stored as the default identification (set through `Idents` for Seurat object) the slot must be set as a parameter in the training and testing function (see below).

    unique(train_set$`Most likely LM22 cell type`)
    #>  [1] "Macrophages M0"               "Macrophages M2"
    #>  [3] "Monocytes"                    "Macrophages M1"
    #>  [5] "Mast cells activated"         NA
    #>  [7] "T cells CD4 memory resting"   "NK cells resting"
    #>  [9] "Neutrophils"                  "T cells CD8"
    #> [11] "Dendritic cells resting"      "T cells CD4 memory activated"
    #> [13] "Plasma cells"                 "T cells regulatory (Tregs)"
    #> [15] "T cells follicular helper"    "Eosinophils"
    #> [17] "Dendritic cells activated"    "B cells memory"
    #> [19] "Mast cells resting"           "NK cells activated"
    #> [21] "B cells naive"

    unique(test_set$`Most likely LM22 cell type`)
    #>  [1] "Monocytes"                    NA
    #>  [3] "Macrophages M0"               "T cells CD4 memory resting"
    #>  [5] "T cells regulatory (Tregs)"   "T cells follicular helper"
    #>  [7] "B cells memory"               "T cells CD8"
    #>  [9] "T cells CD4 memory activated" "Dendritic cells resting"
    #> [11] "Neutrophils"                  "Plasma cells"
    #> [13] "Mast cells activated"         "Macrophages M2"
    #> [15] "NK cells activated"           "B cells naive"
    #> [17] "Dendritic cells activated"    "NK cells resting"
    #> [19] "Macrophages M1"               "Eosinophils"
    #> [21] "Mast cells resting"

We want to train a classifier for B cells and their phenotypes. Considering memory B cells, naive B cells and plasma cells as B cell phenotypes, we convert all those cells to a uniform cell label, ie. B cells. All non B cells are converted into ‘others’.

    # change cell label
    train_set$B_cell <- unlist(lapply(train_set$`Most likely LM22 cell type`,
                                      function(x) if (is.na(x)) {'ambiguous'} else if (x %in% c('Plasma cells', 'B cells memory', 'B cells naive')) {'B cells'} else {'others'}))

    test_set$B_cell <- unlist(lapply(test_set$`Most likely LM22 cell type`,
                                     function(x) if (is.na(x)) {'ambiguous'} else if (x %in% c('Plasma cells', 'B cells memory', 'B cells naive')) {'B cells'} else {'others'}))

We observe that there are cells marked NAs. Those can be understood as 1/different from all indicated cell types or 2/any unknown cell types. Here we consider the second case, ie. we don’t know whether they are positive or negative to B cells. To avoid any effect of these cells, we can assign them as ‘ambiguous’. All cells tagged ‘ambiguous’ will be ignored by scAnnotatR from training and testing.

We may want to check the number of cells in each category:

    table(train_set$B_cell)
    #>
    #>   B cells ambiguous    others
    #>        70      1406      1024

## Defining marker genes

Next, we define a set of marker genes, which will be used in training the classification model. Supposing we are training a model for classifying B cells, we define the set of marker genes as follows:

    selected_marker_genes_B <- c("CD19", "MS4A1", "CD79A", "CD79B", 'CD27', 'IGHG1', 'IGHG2', 'IGHM',
                             "CR2", "MEF2C", 'VPREB3', 'CD86', 'LY86', "BLK", "DERL3")

## Train model

When the model is being trained, three pieces of information must be provided:

- the Seurat/SCE object used for training
- the set of applied marker genes
- the cell type defining the trained model

In case the dataset does not contain any cell classified as the target cell type, the function will fail.

If the cell type annotation is not set in the default identification slot (`Idents` for `Seurat` objects) the name of the metadata field must be provided to the `sce_tag_slot parameter`.

When training on an imbalanced dataset (f.e. a datasets containing 90% B cells and only very few other cell types), the trained model may bias toward the majority group and ignore the presence of the minority group. To avoid this, the number of positive cells and negative cells will be automatically balanced before training. Therefore, a smaller number cells will be randomly picked
from the majority group. To use the same set of cells while training multiple times for one model, users can use `set.seed`.

    set.seed(123)
    classifier_B <- train_classifier(train_obj = train_set, cell_type = "B cells",
                                     marker_genes = selected_marker_genes_B,
                                     assay = 'counts', tag_slot = 'B_cell')
    #> Warning: Cell types containing "/", ",", "-", "+", ".", "and", "or", "(", ")", and "ambiguous" are considered as ambiguous. They are removed from training and testing.
    #> Loading required package: ggplot2
    #> Loading required package: lattice
    #> Warning in .local(x, ...): Variable(s) `' constant. Cannot scale data.

    classifier_B
    #> An object of class scAnnotatR for B cells
    #> * 15 marker genes applied: BLK, CD19, CD27, CD79A, CD79B, CD86, CR2, DERL3, IGHG1, IGHG2, IGHM, LY86, MEF2C, MS4A1, VPREB3
    #> * Predicting probability threshold: 0.5
    #> * No parent model

The classification model is a `scAnnotatR` object. Details about the classification model are accessible via getter methods.

For example:

    caret_model(classifier_B)
    #> Support Vector Machines with Linear Kernel
    #>
    #> No pre-processing
    #> Resampling: Cross-Validated (10 fold)
    #> Summary of sample sizes: 984, 984, 985, 985, 985, 985, ...
    #> Addtional sampling using down-sampling
    #>
    #> Resampling results:
    #>
    #>   Accuracy   Kappa
    #>   0.9543119  0.6263564
    #>
    #> Tuning parameter 'C' was held constant at a value of 1

## Test model

The `test_classifier` model automatically tests a classifier’s performance against another dataset. Here, we used the `test_set` created before:

    classifier_B_test <- test_classifier(classifier = classifier_B, test_obj = test_set,
                                         assay = 'counts', tag_slot = 'B_cell')
    #> Warning: Cell types containing "/", ",", "-", "+", ".", "and", "or", "(", ")", and "ambiguous" are considered as ambiguous. They are removed from training and testing.
    #> Current probability threshold: 0.5
    #>          Positive    Negative    Total
    #> Actual       90      1024        1114
    #> Predicted    115     999     1114
    #> Accuracy: 0.957809694793537
    #> Sensivity (True Positive Rate) for B cells: 0.877777777777778
    #> Specificity (1 - False Positive Rate) for B cells: 0.96484375
    #> Area under the curve: 0.96728515625

### Interpreting test model result

Apart from the output exported to console, test classifier function also returns an object, which is a list of:

- test_tag: actual cell label, this can be different from the label provided by users because of ambiguous characters or the incoherence in cell type and sub cell type label assignment.

- pred: cell type prediction using current classifier

- acc: prediction accuracy at the fixed probability threshold, the probability threshold value can also be queried using p_thres(classifier)

- auc: AUC score provided by current classifier

- overall_roc: True Positive Rate and False Positive Rate with a certain number of prediction probability thresholds

Every classifier internally consists of a trained SVM and a probability threshold. Only cells that are classified with a probability exceeding this threshold are classified as the respective cell type. The overall_roc slot summarizes the True Positive Rate (sensitivity) and False Positive Rate (1 - specificity) obtained by the trained model according to different thresholds.

    classifier_B_test$overall_roc
    #>       p_thres        fpr       tpr
    #>  [1,]     0.1 0.96582031 1.0000000
    #>  [2,]     0.2 0.88378906 1.0000000
    #>  [3,]     0.3 0.07519531 0.8888889
    #>  [4,]     0.4 0.06445312 0.8888889
    #>  [5,]     0.5 0.03515625 0.8777778
    #>  [6,]     0.6 0.03222656 0.8333333
    #>  [7,]     0.7 0.02929688 0.8222222
    #>  [8,]     0.8 0.02343750 0.7555556
    #>  [9,]     0.9 0.01269531 0.6777778

In this example of B cell classifier, the current threshold is at 0.5. The higher sensitivity can be reached if we set the p_thres at 0.4. However, we will then have lower specificity, which means that we will incorrectly classify some cells as B cells. At the sime time, we may not retrieve all actual B cells with higher p_thres (0.6, for example).

There is of course a certain trade-off between the sensitivity and the specificity of the model. Depending on the need of the project or the user-own preference, a probability threshold giving higher sensitivity or higher specificity can be chosen. In our perspective, p_thres at 0.5 is a good choice for the current B cell model.

### Plotting ROC curve

Apart from numbers, we also provide a method to plot the ROC curve.

    roc_curve <- plot_roc_curve(test_result = classifier_B_test)

    plot(roc_curve)

### Which model to choose?

Changes in the training data, in the set of marker genes and in the prediction probability threshold will all lead to a change in model performance.

There are several ways to evaluate the trained model, including the overall accuracy, the AUC score and the sensitivity/specificity of the model when testing on an independent dataset. In this example, we choose the model which has the best AUC score.

Tip: Using more general markers of the whole population leads to higher sensitivity. This sometimes produces lower specificity because of close cell types (T cells and NK cells, for example). While training some models, we observed that we can use the markers producing high sensitivity but at the same time can improve the specificity by increasing the probability threshold. Of course, this can only applied in some cases, because some markers can even have a larger affect on the specificity than the prediction probability threshold.

## Save classification model for further use

New classification models can be stored using the `save_new_model` function:

    # no copy of pretrained models is performed
    save_new_model(new_model = classifier_B, path_to_models = tempdir(),
                   include.default = FALSE)
    #> Saving new models to /tmp/RtmpEkO8bg/new_models.rda...
    #> Finished saving new model

Parameters:

- new_model: The new model that should be added to the database in the specified directory.
- path_to_models: The directory where the new models should be stored.
- include.default: If set, the default models shipped with the package are added to the database.

Users can also choose whether copy all pretrained models of the packages to the new model database. If not, in the future, user can only choose to use either default pretrained models or new models by specifying only one path to models.

Models can be deleted from the model database using the `delete_model` function:

    # delete the "B cells" model from the new database
    delete_model("B cells", path_to_models = tempdir())

## Session Info

    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] caret_6.0-90                lattice_0.20-45
    #>  [3] ggplot2_3.3.5               scRNAseq_2.7.2
    #>  [5] scAnnotatR_1.0.0            SingleCellExperiment_1.16.0
    #>  [7] SummarizedExperiment_1.24.0 Biobase_2.54.0
    #>  [9] GenomicRanges_1.46.0        GenomeInfoDb_1.30.0
    #> [11] IRanges_2.28.0              S4Vectors_0.32.0
    #> [13] BiocGenerics_0.40.0         MatrixGenerics_1.6.0
    #> [15] matrixStats_0.61.0          SeuratObject_4.0.2
    #> [17] Seurat_4.0.5
    #>
    #> loaded via a namespace (and not attached):
    #>   [1] utf8_1.2.2                    reticulate_1.22
    #>   [3] tidyselect_1.1.1              RSQLite_2.2.8
    #>   [5] AnnotationDbi_1.56.0          htmlwidgets_1.5.4
    #>   [7] BiocParallel_1.28.0           grid_4.1.1
    #>   [9] Rtsne_0.15                    pROC_1.18.0
    #>  [11] munsell_0.5.0                 codetools_0.2-18
    #>  [13] ica_1.0-2                     future_1.22.1
    #>  [15] miniUI_0.1.1.1                withr_2.4.2
    #>  [17] colorspace_2.0-2              filelock_1.0.2
    #>  [19] highr_0.9                     knitr_1.36
    #>  [21] ROCR_1.0-11                   tensor_1.5
    #>  [23] listenv_0.8.0                 labeling_0.4.2
    #>  [25] GenomeInfoDbData_1.2.7        polyclip_1.10-0
    #>  [27] bit64_4.0.5                   farver_2.1.0
    #>  [29] parallelly_1.28.1             vctrs_0.3.8
    #>  [31] generics_0.1.1                ipred_0.9-12
    #>  [33] xfun_0.27                     BiocFileCache_2.2.0
    #>  [35] R6_2.5.1                      AnnotationFilter_1.18.0
    #>  [37] bitops_1.0-7                  spatstat.utils_2.2-0
    #>  [39] cachem_1.0.6                  DelayedArray_0.20.0
    #>  [41] assertthat_0.2.1              BiocIO_1.4.0
    #>  [43] promises_1.2.0.1              scales_1.1.1
    #>  [45] nnet_7.3-16                   gtable_0.3.0
    #>  [47] globals_0.14.0                goftest_1.2-3
    #>  [49] ensembldb_2.18.0              timeDate_3043.102
    #>  [51] rlang_0.4.12                  splines_4.1.1
    #>  [53] rtracklayer_1.54.0            lazyeval_0.2.2
    #>  [55] ModelMetrics_1.2.2.2          spatstat.geom_2.3-0
    #>  [57] BiocManager_1.30.16           yaml_2.2.1
    #>  [59] reshape2_1.4.4                abind_1.4-5
    #>  [61] GenomicFeatures_1.46.0        httpuv_1.6.3
    #>  [63] tools_4.1.1                   lava_1.6.10
    #>  [65] ellipsis_0.3.2                spatstat.core_2.3-0
    #>  [67] jquerylib_0.1.4               RColorBrewer_1.1-2
    #>  [69] proxy_0.4-26                  ggridges_0.5.3
    #>  [71] Rcpp_1.0.7                    plyr_1.8.6
    #>  [73] progress_1.2.2                zlibbioc_1.40.0
    #>  [75] purrr_0.3.4                   RCurl_1.98-1.5
    #>  [77] prettyunits_1.1.1             rpart_4.1-15
    #>  [79] deldir_1.0-6                  pbapply_1.5-0
    #>  [81] cowplot_1.1.1                 zoo_1.8-9
    #>  [83] ggrepel_0.9.1                 cluster_2.1.2
    #>  [85] magrittr_2.0.1                data.table_1.14.2
    #>  [87] scattermore_0.7               lmtest_0.9-38
    #>  [89] RANN_2.6.1                    ProtGenerics_1.26.0
    #>  [91] fitdistrplus_1.1-6            hms_1.1.1
    #>  [93] patchwork_1.1.1               mime_0.12
    #>  [95] evaluate_0.14                 xtable_1.8-4
    #>  [97] XML_3.99-0.8                  gridExtra_2.3
    #>  [99] biomaRt_2.50.0                compiler_4.1.1
    #> [101] tibble_3.1.5                  KernSmooth_2.23-20
    #> [103] crayon_1.4.1                  htmltools_0.5.2
    #> [105] mgcv_1.8-38                   later_1.3.0
    #> [107] tidyr_1.1.4                   lubridate_1.8.0
    #> [109] DBI_1.1.1                     ExperimentHub_2.2.0
    #> [111] dbplyr_2.1.1                  MASS_7.3-54
    #> [113] rappdirs_0.3.3                data.tree_1.0.0
    #> [115] Matrix_1.3-4                  parallel_4.1.1
    #> [117] gower_0.2.2                   igraph_1.2.7
    #> [119] pkgconfig_2.0.3               GenomicAlignments_1.30.0
    #> [121] plotly_4.10.0                 spatstat.sparse_2.0-0
    #> [123] recipes_0.1.17                xml2_1.3.2
    #> [125] foreach_1.5.1                 bslib_0.3.1
    #> [127] XVector_0.34.0                prodlim_2019.11.13
    #> [129] stringr_1.4.0                 digest_0.6.28
    #> [131] sctransform_0.3.2             RcppAnnoy_0.0.19
    #> [133] spatstat.data_2.1-0           Biostrings_2.62.0
    #> [135] rmarkdown_2.11                leiden_0.3.9
    #> [137] uwot_0.1.10                   restfulr_0.0.13
    #> [139] curl_4.3.2                    kernlab_0.9-29
    #> [141] Rsamtools_2.10.0              shiny_1.7.1
    #> [143] rjson_0.2.20                  lifecycle_1.0.1
    #> [145] nlme_3.1-153                  jsonlite_1.7.2
    #> [147] viridisLite_0.4.0             fansi_0.5.0
    #> [149] pillar_1.6.4                  KEGGREST_1.34.0
    #> [151] fastmap_1.1.0                 httr_1.4.2
    #> [153] survival_3.2-13               interactiveDisplayBase_1.32.0
    #> [155] glue_1.4.2                    png_0.1-7
    #> [157] iterators_1.0.13              BiocVersion_3.14.0
    #> [159] bit_4.0.4                     class_7.3-19
    #> [161] stringi_1.7.5                 sass_0.4.0
    #> [163] blob_1.2.2                    AnnotationHub_3.2.0
    #> [165] memoise_2.0.0                 dplyr_1.0.7
    #> [167] irlba_2.3.3                   e1071_1.7-9
    #> [169] future.apply_1.8.1            ape_5.5