# Using UCell with Seurat

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

#### 1 May 2024

#### Package

UCell 2.8.0

# 1 Introduction

The function `AddModuleScore_UCell()` allows operating directly on Seurat objects. UCell scores are calculated from raw counts or normalized data, and returned as metadata columns. The example below defines some simple signatures, and applies them on single-cell data stored in a Seurat object.

To see how this function differs from Seurat’s own `AddModuleScore()` (not based on per-cell ranks) see this vignette.

# 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 Seurat object

    library(UCell)
    library(Seurat)
    seurat.object <- CreateSeuratObject(counts = exp.mat,
                                        project = "Zilionis_immune")
    seurat.object <- AddModuleScore_UCell(seurat.object,
                                          features=signatures, name=NULL)
    head(seurat.object[[]])

    ##              orig.ident nCount_RNA nFeature_RNA Tcell   Myeloid NK Plasma_cell
    ## bcHTNA1 Zilionis_immune       7516         2613     0 0.5227121  0  0.00000000
    ## bcHNVA2 Zilionis_immune       5684         1981     0 0.5112892  0  0.00000000
    ## bcALZN3 Zilionis_immune       4558         1867     0 0.3584502  0  0.07540874
    ## bcFWBP4 Zilionis_immune       2915         1308     0 0.1546426  0  0.00000000
    ## bcBJYE5 Zilionis_immune       3576         1548     0 0.4629927  0  0.00000000
    ## bcGSBJ6 Zilionis_immune       2796         1270     0 0.5452238  0  0.00000000

Generate PCA and UMAP embeddings

    seurat.object <- NormalizeData(seurat.object)
    seurat.object <- FindVariableFeatures(seurat.object,
                         selection.method = "vst", nfeatures = 500)

    seurat.object <- ScaleData(seurat.object)
    seurat.object <- RunPCA(seurat.object, npcs = 20,
                            features=VariableFeatures(seurat.object))
    seurat.object <- RunUMAP(seurat.object, reduction = "pca",
                             dims = 1:20, seed.use=123)

Visualize UCell scores on low-dimensional representation (UMAP)

    library(ggplot2)
    library(patchwork)

    FeaturePlot(seurat.object, reduction = "umap", features = names(signatures))

# 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 Seurat objects to smooth UCell scores:

    seurat.object <- SmoothKNN(seurat.object,
                               signature.names = names(signatures),
                               reduction="pca")

    FeaturePlot(seurat.object, reduction = "umap", features = c("NK","NK_kNN"))

Smoothing (or imputation) has been designed for UCell scores, but it can be applied to any other data or metadata. For instance, we can perform knn-smoothing directly on gene expression measurements:

    genes <- c("CD2","CSF1R")
    seurat.object <- SmoothKNN(seurat.object, signature.names=genes,
                     assay="RNA", reduction="pca", k=20, suffix = "_smooth")

    DefaultAssay(seurat.object) <- "RNA"
    a <- FeaturePlot(seurat.object, reduction = "umap", features = genes)
    DefaultAssay(seurat.object) <- "RNA_smooth"
    b <- FeaturePlot(seurat.object, reduction = "umap", features = genes)
    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
- Hao, Yuhan, et al. (2021) Integrated analysis of multimodal single-cell data Cell

# A Session Info

    sessionInfo()

    ## R version 4.4.0 beta (2024-04-15 r86425 ucrt)
    ## Platform: x86_64-w64-mingw32/x64
    ## Running under: Windows Server 2022 x64 (build 20348)
    ##
    ## Matrix products: default
    ##
    ##
    ## locale:
    ## [1] LC_COLLATE=C
    ## [2] LC_CTYPE=English_United States.utf8
    ## [3] LC_MONETARY=English_United States.utf8
    ## [4] LC_NUMERIC=C
    ## [5] LC_TIME=English_United States.utf8
    ##
    ## time zone: America/New_York
    ## tzcode source: internal
    ##
    ## attached base packages:
    ## [1] stats4    stats     graphics  grDevices utils     datasets  methods
    ## [8] base
    ##
    ## other attached packages:
    ##  [1] patchwork_1.2.0             ggplot2_3.5.1
    ##  [3] Seurat_5.0.3                SeuratObject_5.0.1
    ##  [5] sp_2.1-4                    UCell_2.8.0
    ##  [7] scRNAseq_2.17.9             SingleCellExperiment_1.26.0
    ##  [9] SummarizedExperiment_1.34.0 Biobase_2.64.0
    ## [11] GenomicRanges_1.56.0        GenomeInfoDb_1.40.0
    ## [13] IRanges_2.38.0              S4Vectors_0.42.0
    ## [15] BiocGenerics_0.50.0         MatrixGenerics_1.16.0
    ## [17] matrixStats_1.3.0           BiocStyle_2.32.0
    ##
    ## loaded via a namespace (and not attached):
    ##   [1] RcppAnnoy_0.0.22         later_1.3.2              splines_4.4.0
    ##   [4] BiocIO_1.14.0            bitops_1.0-7             filelock_1.0.3
    ##   [7] tibble_3.2.1             polyclip_1.10-6          XML_3.99-0.16.1
    ##  [10] fastDummies_1.7.3        lifecycle_1.0.4          httr2_1.0.1
    ##  [13] globals_0.16.3           lattice_0.22-6           ensembldb_2.28.0
    ##  [16] MASS_7.3-60.2            alabaster.base_1.4.0     magrittr_2.0.3
    ##  [19] plotly_4.10.4            sass_0.4.9               rmarkdown_2.26
    ##  [22] jquerylib_0.1.4          yaml_2.3.8               httpuv_1.6.15
    ##  [25] sctransform_0.4.1        spam_2.10-0              spatstat.sparse_3.0-3
    ##  [28] reticulate_1.36.1        pbapply_1.7-2            cowplot_1.1.3
    ##  [31] DBI_1.2.2                RColorBrewer_1.1-3       abind_1.4-5
    ##  [34] zlibbioc_1.50.0          Rtsne_0.17               purrr_1.0.2
    ##  [37] AnnotationFilter_1.28.0  RCurl_1.98-1.14          rappdirs_0.3.3
    ##  [40] GenomeInfoDbData_1.2.12  ggrepel_0.9.5            irlba_2.3.5.1
    ##  [43] spatstat.utils_3.0-4     listenv_0.9.1            alabaster.sce_1.4.0
    ##  [46] goftest_1.2-3            RSpectra_0.16-1          spatstat.random_3.2-3
    ##  [49] fitdistrplus_1.1-11      parallelly_1.37.1        leiden_0.4.3.1
    ##  [52] codetools_0.2-20         DelayedArray_0.30.0      tidyselect_1.2.1
    ##  [55] farver_2.1.1             UCSC.utils_1.0.0         spatstat.explore_3.2-7
    ##  [58] BiocFileCache_2.12.0     GenomicAlignments_1.40.0 jsonlite_1.8.8
    ##  [61] BiocNeighbors_1.22.0     progressr_0.14.0         ggridges_0.5.6
    ##  [64] survival_3.6-4           tools_4.4.0              ica_1.0-3
    ##  [67] Rcpp_1.0.12              glue_1.7.0               gridExtra_2.3
    ##  [70] SparseArray_1.4.0        xfun_0.43                dplyr_1.1.4
    ##  [73] HDF5Array_1.32.0         gypsum_1.0.0             withr_3.0.0
    ##  [76] BiocManager_1.30.22      fastmap_1.1.1            rhdf5filters_1.16.0
    ##  [79] fansi_1.0.6              digest_0.6.35            mime_0.12
    ##  [82] R6_2.5.1                 colorspace_2.1-0         scattermore_1.2
    ##  [85] tensor_1.5               spatstat.data_3.0-4      RSQLite_2.3.6
    ##  [88] paws.storage_0.5.0       tidyr_1.3.1              utf8_1.2.4
    ##  [91] generics_0.1.3           data.table_1.15.4        rtracklayer_1.64.0
    ##  [94] htmlwidgets_1.6.4        httr_1.4.7               S4Arrays_1.4.0
    ##  [97] uwot_0.2.2               pkgconfig_2.0.3          gtable_0.3.5
    ## [100] blob_1.2.4               lmtest_0.9-40            XVector_0.44.0
    ## [103] htmltools_0.5.8.1        dotCall64_1.1-1          bookdown_0.39
    ## [106] ProtGenerics_1.36.0      scales_1.3.0             alabaster.matrix_1.4.0
    ## [109] png_0.1-8                knitr_1.46               reshape2_1.4.4
    ## [112] rjson_0.2.21             nlme_3.1-164             curl_5.2.1
    ## [115] cachem_1.0.8             zoo_1.8-12               rhdf5_2.48.0
    ## [118] stringr_1.5.1            BiocVersion_3.19.1       KernSmooth_2.23-22
    ## [121] parallel_4.4.0           miniUI_0.1.1.1           AnnotationDbi_1.66.0
    ## [124] restfulr_0.0.15          pillar_1.9.0             grid_4.4.0
    ## [127] alabaster.schemas_1.4.0  vctrs_0.6.5              RANN_2.6.1
    ## [130] promises_1.3.0           dbplyr_2.5.0             xtable_1.8-4
    ## [133] cluster_2.1.6            evaluate_0.23            magick_2.8.3
    ## [136] tinytex_0.50             GenomicFeatures_1.56.0   cli_3.6.2
    ## [139] compiler_4.4.0           Rsamtools_2.20.0         rlang_1.1.3
    ## [142] crayon_1.5.2             paws.common_0.7.2        future.apply_1.11.2
    ## [145] labeling_0.4.3           plyr_1.8.9               stringi_1.8.3
    ## [148] deldir_2.0-4             viridisLite_0.4.2        alabaster.se_1.4.0
    ## [151] BiocParallel_1.38.0      munsell_0.5.1            Biostrings_2.72.0
    ## [154] lazyeval_0.2.2           spatstat.geom_3.2-9      Matrix_1.7-0
    ## [157] ExperimentHub_2.12.0     RcppHNSW_0.6.0           bit64_4.0.5
    ## [160] future_1.33.2            Rhdf5lib_1.26.0          shiny_1.8.1.1
    ## [163] KEGGREST_1.44.0          highr_0.10               alabaster.ranges_1.4.0
    ## [166] AnnotationHub_3.12.0     ROCR_1.0-11              igraph_2.0.3
    ## [169] memoise_2.0.1            bslib_0.7.0              bit_4.0.5