# Using UCell with SingleCellExperiment

Massimo Andreatta1 and Santiago J. Carmona1

1Ludwig Institute for Cancer Research, Lausanne Branch, and Department of Oncology, CHUV and University of Lausanne, Epalinges 1066, Switzerland; and Swiss Institute of Bioinformatics, Lausanne, Switzerland

#### 6 November 2023

#### Package

UCell 2.6.2

# 1 Introduction

SingleCellExperiment is Bioconductor’s data structure of choice for storing single-cell experiment data. The function `ScoreSignatures_UCell()` allows performing signature scoring with UCell directly on `sce` objects. UCell scores are returned in a altExp object: `altExp(sce, 'UCell')`

# 2 Get some testing data

For this demo, we will download a single-cell dataset of lung cancer (Zilionis et al. (2019) Immunity) through the scRNA-seq package. This dataset contains >170,000 single cells; for the sake of simplicity, in this demo will we focus on immune cells, according to the annotations by the authors, and downsample to 5000 cells.

    library(scRNAseq)
    lung <- ZilionisLungData()
    immune <- lung$Used & lung$used_in_NSCLC_immune
    lung <- lung[,immune]
    lung <- lung[,1:5000]

    exp.mat <- Matrix::Matrix(counts(lung),sparse = TRUE)
    colnames(exp.mat) <- paste0(colnames(exp.mat), seq(1,ncol(exp.mat)))

# 3 Define gene signatures

Here we define some simple gene sets based on the “Human Cell Landscape” signatures Han et al. (2020) Nature. You may edit existing signatures, or add new one as elements in a list.

    signatures <- list(
        Tcell = c("CD3D","CD3E","CD3G","CD2","TRAC"),
        Myeloid = c("CD14","LYZ","CSF1R","FCER1G","SPI1","LCK-"),
        NK = c("KLRD1","NCR1","NKG7","CD3D-","CD3E-"),
        Plasma_cell = c("MZB1","DERL3","CD19-")
    )

# 4 Run UCell on sce object

    library(UCell)
    library(SingleCellExperiment)

    sce <- SingleCellExperiment(list(counts=exp.mat))
    sce <- ScoreSignatures_UCell(sce, features=signatures,
                                     assay = 'counts', name=NULL)
    altExp(sce, 'UCell')

    ## class: SummarizedExperiment
    ## dim: 4 5000
    ## metadata(0):
    ## assays(1): UCell
    ## rownames(4): Tcell Myeloid NK Plasma_cell
    ## rowData names(0):
    ## colnames(5000): bcHTNA1 bcHNVA2 ... bcGVZB4999 bcHGKL5000
    ## colData names(0):

Dimensionality reduction and visualization

    library(scater)
    library(patchwork)
    #PCA
    sce <- logNormCounts(sce)
    sce <- runPCA(sce, scale=TRUE, ncomponents=10)

    #UMAP
    set.seed(1234)
    sce <- runUMAP(sce, dimred="PCA")

Visualize UCell scores on low-dimensional representation (UMAP)

    pll <- lapply(names(signatures), function(x) {
        plotUMAP(sce, colour_by = x, by_exprs_values = "UCell",
                 point_size=0.2) + theme(aspect.ratio = 1)
    })
    wrap_plots(pll)

# 5 Signature smoothing

Single-cell data are sparse. It can be useful to ‘impute’ scores by neighboring cells and partially correct this sparsity. The function `SmoothKNN` performs smoothing of single-cell scores by weighted average of the k-nearest neighbors in a given dimensionality reduction. It can be applied directly on SingleCellExperiment objects to smooth UCell scores:

    sce <- SmoothKNN(sce, signature.names = names(signatures), reduction="PCA")

    a <- plotUMAP(sce, colour_by="Myeloid", by_exprs_values="UCell",
             point_size=0.2) + ggtitle("UCell") + theme(aspect.ratio = 1)

    b <- plotUMAP(sce, colour_by="Myeloid_kNN", by_exprs_values="UCell_kNN",
             point_size=0.2) + ggtitle("Smoothed UCell") + theme(aspect.ratio = 1)

    a | b

# 6 Resources

Please report any issues at the UCell GitHub repository.

More demos available on the Bioc landing page and at the UCell demo repository.

If you find UCell useful, you may also check out the scGate package, which relies on UCell scores to automatically purify populations of interest based on gene signatures.

See also SignatuR for easy storing and retrieval of gene signatures.

# 7 References

# Appendix

- Andreatta, M., Carmona, S. J. (2021) UCell: Robust and scalable single-cell gene signature scoring Computational and Structural Biotechnology Journal
- Zilionis, R., Engblom, C., …, Klein, A. M. (2019) Single-Cell Transcriptomics of Human and Mouse Lung Cancers Reveals Conserved Myeloid Populations across Individuals and Species Immunity
- Amezquita, Robert A., et al. (2020) “Orchestrating single-cell analysis with Bioconductor.” Nature methods

# A Session Info

    sessionInfo()

    ## R version 4.3.2 (2023-10-31)
    ## Platform: x86_64-pc-linux-gnu (64-bit)
    ## Running under: Ubuntu 22.04.3 LTS
    ##
    ## Matrix products: default
    ## BLAS:   /home/biocbuild/bbs-3.18-bioc/R/lib/libRblas.so
    ## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
    ##
    ## 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
    ##
    ## time zone: America/New_York
    ## tzcode source: system (glibc)
    ##
    ## attached base packages:
    ## [1] stats4    stats     graphics  grDevices utils     datasets  methods
    ## [8] base
    ##
    ## other attached packages:
    ##  [1] scater_1.30.0               scuttle_1.12.0
    ##  [3] patchwork_1.1.3             ggplot2_3.4.4
    ##  [5] Seurat_5.0.0                SeuratObject_5.0.0
    ##  [7] sp_2.1-1                    UCell_2.6.2
    ##  [9] scRNAseq_2.16.0             SingleCellExperiment_1.24.0
    ## [11] SummarizedExperiment_1.32.0 Biobase_2.62.0
    ## [13] GenomicRanges_1.54.1        GenomeInfoDb_1.38.0
    ## [15] IRanges_2.36.0              S4Vectors_0.40.1
    ## [17] BiocGenerics_0.48.1         MatrixGenerics_1.14.0
    ## [19] matrixStats_1.0.0           BiocStyle_2.30.0
    ##
    ## loaded via a namespace (and not attached):
    ##   [1] ProtGenerics_1.34.0           spatstat.sparse_3.0-3
    ##   [3] bitops_1.0-7                  httr_1.4.7
    ##   [5] RColorBrewer_1.1-3            tools_4.3.2
    ##   [7] sctransform_0.4.1             utf8_1.2.4
    ##   [9] R6_2.5.1                      lazyeval_0.2.2
    ##  [11] uwot_0.1.16                   withr_2.5.2
    ##  [13] prettyunits_1.2.0             gridExtra_2.3
    ##  [15] progressr_0.14.0              cli_3.6.1
    ##  [17] spatstat.explore_3.2-5        fastDummies_1.7.3
    ##  [19] labeling_0.4.3                sass_0.4.7
    ##  [21] spatstat.data_3.0-3           ggridges_0.5.4
    ##  [23] pbapply_1.7-2                 Rsamtools_2.18.0
    ##  [25] parallelly_1.36.0             RSQLite_2.3.3
    ##  [27] generics_0.1.3                BiocIO_1.12.0
    ##  [29] ica_1.0-3                     spatstat.random_3.2-1
    ##  [31] dplyr_1.1.3                   Matrix_1.6-1.1
    ##  [33] ggbeeswarm_0.7.2              fansi_1.0.5
    ##  [35] abind_1.4-5                   lifecycle_1.0.3
    ##  [37] yaml_2.3.7                    SparseArray_1.2.1
    ##  [39] BiocFileCache_2.10.1          Rtsne_0.16
    ##  [41] grid_4.3.2                    blob_1.2.4
    ##  [43] promises_1.2.1                ExperimentHub_2.10.0
    ##  [45] crayon_1.5.2                  miniUI_0.1.1.1
    ##  [47] lattice_0.22-5                beachmat_2.18.0
    ##  [49] cowplot_1.1.1                 GenomicFeatures_1.54.1
    ##  [51] KEGGREST_1.42.0               magick_2.8.1
    ##  [53] pillar_1.9.0                  knitr_1.45
    ##  [55] rjson_0.2.21                  future.apply_1.11.0
    ##  [57] codetools_0.2-19              leiden_0.4.3
    ##  [59] glue_1.6.2                    data.table_1.14.8
    ##  [61] vctrs_0.6.4                   png_0.1-8
    ##  [63] spam_2.10-0                   gtable_0.3.4
    ##  [65] cachem_1.0.8                  xfun_0.41
    ##  [67] S4Arrays_1.2.0                mime_0.12
    ##  [69] survival_3.5-7                interactiveDisplayBase_1.40.0
    ##  [71] ellipsis_0.3.2                fitdistrplus_1.1-11
    ##  [73] ROCR_1.0-11                   nlme_3.1-163
    ##  [75] bit64_4.0.5                   progress_1.2.2
    ##  [77] filelock_1.0.2                RcppAnnoy_0.0.21
    ##  [79] bslib_0.5.1                   irlba_2.3.5.1
    ##  [81] vipor_0.4.5                   KernSmooth_2.23-22
    ##  [83] colorspace_2.1-0              DBI_1.1.3
    ##  [85] tidyselect_1.2.0              bit_4.0.5
    ##  [87] compiler_4.3.2                curl_5.1.0
    ##  [89] BiocNeighbors_1.20.0          xml2_1.3.5
    ##  [91] DelayedArray_0.28.0           plotly_4.10.3
    ##  [93] bookdown_0.36                 rtracklayer_1.62.0
    ##  [95] scales_1.2.1                  lmtest_0.9-40
    ##  [97] rappdirs_0.3.3                stringr_1.5.0
    ##  [99] digest_0.6.33                 goftest_1.2-3
    ## [101] spatstat.utils_3.0-4          rmarkdown_2.25
    ## [103] XVector_0.42.0                htmltools_0.5.7
    ## [105] pkgconfig_2.0.3               sparseMatrixStats_1.14.0
    ## [107] highr_0.10                    dbplyr_2.4.0
    ## [109] fastmap_1.1.1                 ensembldb_2.26.0
    ## [111] rlang_1.1.2                   htmlwidgets_1.6.2
    ## [113] shiny_1.7.5.1                 DelayedMatrixStats_1.24.0
    ## [115] farver_2.1.1                  jquerylib_0.1.4
    ## [117] zoo_1.8-12                    jsonlite_1.8.7
    ## [119] BiocParallel_1.36.0           BiocSingular_1.18.0
    ## [121] RCurl_1.98-1.13               magrittr_2.0.3
    ## [123] GenomeInfoDbData_1.2.11       dotCall64_1.1-0
    ## [125] munsell_0.5.0                 Rcpp_1.0.11
    ## [127] viridis_0.6.4                 reticulate_1.34.0
    ## [129] stringi_1.7.12                zlibbioc_1.48.0
    ## [131] MASS_7.3-60                   AnnotationHub_3.10.0
    ## [133] plyr_1.8.9                    parallel_4.3.2
    ## [135] listenv_0.9.0                 ggrepel_0.9.4
    ## [137] deldir_1.0-9                  Biostrings_2.70.1
    ## [139] splines_4.3.2                 tensor_1.5
    ## [141] hms_1.1.3                     igraph_1.5.1
    ## [143] spatstat.geom_3.2-7           RcppHNSW_0.5.0
    ## [145] reshape2_1.4.4                biomaRt_2.58.0
    ## [147] ScaledMatrix_1.10.0           BiocVersion_3.18.0
    ## [149] XML_3.99-0.15                 evaluate_0.23
    ## [151] BiocManager_1.30.22           httpuv_1.6.12
    ## [153] RANN_2.6.1                    tidyr_1.3.0
    ## [155] purrr_1.0.2                   polyclip_1.10-6
    ## [157] future_1.33.0                 scattermore_1.2
    ## [159] rsvd_1.0.5                    xtable_1.8-4
    ## [161] restfulr_0.0.15               AnnotationFilter_1.26.0
    ## [163] RSpectra_0.16-1               later_1.3.1
    ## [165] viridisLite_0.4.2             tibble_3.2.1
    ## [167] beeswarm_0.4.0                memoise_2.0.1
    ## [169] AnnotationDbi_1.64.1          GenomicAlignments_1.38.0
    ## [171] cluster_2.1.4                 globals_0.16.2