1 Introduction

In single-cell RNA-seq analysis, gene signature (or “module”) scoring constitutes a simple yet powerful approach to evaluate the strength of biological signals, typically associated to a specific cell type or biological process, in a transcriptome.

UCell is an R package for evaluating gene signatures in single-cell datasets. UCell signature scores, based on the Mann-Whitney U statistic, are robust to dataset size and heterogeneity, and their calculation demands less computing time and memory than other available methods, enabling the processing of large datasets in a few minutes even on machines with limited computing power. UCell can be applied to any single-cell data matrix, and includes functions to directly interact with Seurat objects.

2 Quick start

To test your installation, load a small sample dataset and run UCell:

library(UCell)

data(sample.matrix)
gene.sets <- list(Tcell_signature = c("CD2","CD3E","CD3D"),
                  Myeloid_signature = c("SPI1","FCER1G","CSF1R"))

scores <- ScoreSignatures_UCell(sample.matrix, features=gene.sets)
head(scores)
##                       Tcell_signature_UCell Myeloid_signature_UCell
## L5_ATTTCTGAGGTCGTGA               0.8991989                       0
## L4_TCACTATTCATCTCTA               0.6900312                       0
## L1_TCCTTCTTCTTTACAC               0.5932354                       0
## L5_AAAGTGAAGGCGCTCT               0.6090343                       0
## E2L3_CCTCAGTAGTGCAGGT             0.8506898                       0
## L5_CCCTCTCGTTCTAAGC               0.6557632                       0

3 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)))

4 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-")
)

5 Run UCell

Run ScoreSignatures_UCell and get directly signature scores for all cells

u.scores <- ScoreSignatures_UCell(exp.mat,features=signatures)
head(u.scores)
##   Tcell_UCell Myeloid_UCell NK_UCell Plasma_cell_UCell
## 1           0     0.5227121        0        0.00000000
## 2           0     0.5112892        0        0.00000000
## 3           0     0.3584502        0        0.07540874
## 4           0     0.1546426        0        0.00000000
## 5           0     0.4629927        0        0.00000000
## 6           0     0.5452238        0        0.00000000

Show the distribution of predicted scores

library(reshape2)
library(ggplot2)
melted <- reshape2::melt(u.scores)
colnames(melted) <- c("Cell","Signature","UCell_score")
p <- ggplot(melted, aes(x=Signature, y=UCell_score)) + 
    geom_violin(aes(fill=Signature), scale = "width") +
    geom_boxplot(width=0.1, outlier.size=0) +
    theme_bw() + theme(axis.text.x=element_blank())
p

6 Pre-calculating gene rankings

The time- and memory-demanding step in UCell is the calculation of gene rankings for each individual cell. If we plan to experiment with signatures, editing them or adding new cell subtypes, it is possible to pre-calculate the gene rankings once and for all and then apply new signatures over these pre-calculated ranks. Run the StoreRankings_UCell function to pre-calculate gene rankings over a dataset:

set.seed(123)
ranks <- StoreRankings_UCell(exp.mat)
ranks[1:5,1:5]
## 5 x 5 sparse Matrix of class "dgCMatrix"
##           1 2 3 4 5
## 5S_rRNA   . . . . .
## 5_8S_rRNA . . . . .
## 7SK       . . . . .
## A1BG      . . . . .
## A1BG-AS1  . . . . .

Then, we can apply our signature set, or any other new signature to the pre-calculated ranks. The calculations will be considerably faster.

set.seed(123)
u.scores.2 <- ScoreSignatures_UCell(features=signatures,
                                    precalc.ranks = ranks)

melted <- reshape2::melt(u.scores.2)
colnames(melted) <- c("Cell","Signature","UCell_score")
p <- ggplot(melted, aes(x=Signature, y=UCell_score)) + 
    geom_violin(aes(fill=Signature), scale = "width") +
    geom_boxplot(width=0.1, outlier.size = 0) + 
    theme_bw() + theme(axis.text.x=element_blank())
p

new.signatures <- list(Mast.cell = c("TPSAB1","TPSB2","CPA3","MS4A2"),
                       Lymphoid = c("LCK"))

u.scores.3 <- ScoreSignatures_UCell(features=new.signatures,
                                    precalc.ranks = ranks)
melted <- reshape2::melt(u.scores.3)
colnames(melted) <- c("Cell","Signature","UCell_score")
p <- ggplot(melted, aes(x=Signature, y=UCell_score)) + 
    geom_violin(aes(fill=Signature), scale = "width") +
    geom_boxplot(width=0.1, outlier.size=0) + 
    theme_bw() + theme(axis.text.x=element_blank())
p

7 Multi-core processing

If your machine has multi-core capabilities and enough RAM, running UCell in parallel can speed up considerably your analysis. The example below runs on a single core - you may modify this behavior by setting e.g. workers=4 to parallelize to 4 cores:

BPPARAM <- BiocParallel::MulticoreParam(workers=1)
u.scores <- ScoreSignatures_UCell(exp.mat,features=signatures,
                                  BPPARAM=BPPARAM)

8 Interacting with SingleCellExperiment or Seurat

SingleCellExperiment and Seurat are popular environments for single-cell analysis. The UCell package implements functions to interact directly with these pipelines, as described in dedicated demos available on the Bioc landing page.

9 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.

10 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

A Session Info

sessionInfo()
## R Under development (unstable) (2024-01-16 r85808)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 22.04.4 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.19-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] reshape2_1.4.4              scater_1.31.2              
##  [3] scuttle_1.13.1              patchwork_1.2.0            
##  [5] ggplot2_3.5.0               Seurat_5.0.2               
##  [7] SeuratObject_5.0.1          sp_2.1-3                   
##  [9] UCell_2.7.7                 scRNAseq_2.17.3            
## [11] SingleCellExperiment_1.25.0 SummarizedExperiment_1.33.3
## [13] Biobase_2.63.0              GenomicRanges_1.55.3       
## [15] GenomeInfoDb_1.39.8         IRanges_2.37.1             
## [17] S4Vectors_0.41.4            BiocGenerics_0.49.1        
## [19] MatrixGenerics_1.15.0       matrixStats_1.2.0          
## [21] BiocStyle_2.31.0           
## 
## loaded via a namespace (and not attached):
##   [1] ProtGenerics_1.35.3       spatstat.sparse_3.0-3    
##   [3] bitops_1.0-7              httr_1.4.7               
##   [5] RColorBrewer_1.1-3        tools_4.4.0              
##   [7] sctransform_0.4.1         alabaster.base_1.3.21    
##   [9] utf8_1.2.4                R6_2.5.1                 
##  [11] HDF5Array_1.31.6          lazyeval_0.2.2           
##  [13] uwot_0.1.16               rhdf5filters_1.15.2      
##  [15] withr_3.0.0               prettyunits_1.2.0        
##  [17] gridExtra_2.3             progressr_0.14.0         
##  [19] cli_3.6.2                 spatstat.explore_3.2-6   
##  [21] fastDummies_1.7.3         labeling_0.4.3           
##  [23] alabaster.se_1.3.4        sass_0.4.8               
##  [25] spatstat.data_3.0-4       ggridges_0.5.6           
##  [27] pbapply_1.7-2             Rsamtools_2.19.3         
##  [29] parallelly_1.37.1         RSQLite_2.3.5            
##  [31] generics_0.1.3            BiocIO_1.13.0            
##  [33] ica_1.0-3                 spatstat.random_3.2-3    
##  [35] dplyr_1.1.4               Matrix_1.6-5             
##  [37] ggbeeswarm_0.7.2          fansi_1.0.6              
##  [39] abind_1.4-5               lifecycle_1.0.4          
##  [41] yaml_2.3.8                rhdf5_2.47.5             
##  [43] SparseArray_1.3.4         BiocFileCache_2.11.1     
##  [45] Rtsne_0.17                grid_4.4.0               
##  [47] blob_1.2.4                promises_1.2.1           
##  [49] ExperimentHub_2.11.1      crayon_1.5.2             
##  [51] miniUI_0.1.1.1            lattice_0.22-5           
##  [53] beachmat_2.19.1           cowplot_1.1.3            
##  [55] GenomicFeatures_1.55.3    KEGGREST_1.43.0          
##  [57] magick_2.8.3              pillar_1.9.0             
##  [59] knitr_1.45                rjson_0.2.21             
##  [61] future.apply_1.11.1       codetools_0.2-19         
##  [63] leiden_0.4.3.1            glue_1.7.0               
##  [65] data.table_1.15.2         vctrs_0.6.5              
##  [67] png_0.1-8                 gypsum_0.99.12           
##  [69] spam_2.10-0               gtable_0.3.4             
##  [71] aws.s3_0.3.21             cachem_1.0.8             
##  [73] xfun_0.42                 S4Arrays_1.3.6           
##  [75] mime_0.12                 survival_3.5-8           
##  [77] ellipsis_0.3.2            fitdistrplus_1.1-11      
##  [79] ROCR_1.0-11               nlme_3.1-164             
##  [81] bit64_4.0.5               alabaster.ranges_1.3.3   
##  [83] progress_1.2.3            filelock_1.0.3           
##  [85] RcppAnnoy_0.0.22          bslib_0.6.1              
##  [87] irlba_2.3.5.1             vipor_0.4.7              
##  [89] KernSmooth_2.23-22        colorspace_2.1-0         
##  [91] DBI_1.2.2                 tidyselect_1.2.0         
##  [93] bit_4.0.5                 compiler_4.4.0           
##  [95] curl_5.2.1                httr2_1.0.0              
##  [97] BiocNeighbors_1.21.2      xml2_1.3.6               
##  [99] DelayedArray_0.29.9       plotly_4.10.4            
## [101] bookdown_0.38             rtracklayer_1.63.0       
## [103] scales_1.3.0              lmtest_0.9-40            
## [105] rappdirs_0.3.3            stringr_1.5.1            
## [107] digest_0.6.34             goftest_1.2-3            
## [109] spatstat.utils_3.0-4      alabaster.matrix_1.3.13  
## [111] rmarkdown_2.26            XVector_0.43.1           
## [113] htmltools_0.5.7           pkgconfig_2.0.3          
## [115] base64enc_0.1-3           sparseMatrixStats_1.15.0 
## [117] highr_0.10                dbplyr_2.4.0             
## [119] fastmap_1.1.1             ensembldb_2.27.1         
## [121] rlang_1.1.3               htmlwidgets_1.6.4        
## [123] DelayedMatrixStats_1.25.1 shiny_1.8.0              
## [125] farver_2.1.1              jquerylib_0.1.4          
## [127] zoo_1.8-12                jsonlite_1.8.8           
## [129] BiocParallel_1.37.1       BiocSingular_1.19.0      
## [131] RCurl_1.98-1.14           magrittr_2.0.3           
## [133] GenomeInfoDbData_1.2.11   dotCall64_1.1-1          
## [135] Rhdf5lib_1.25.1           munsell_0.5.0            
## [137] Rcpp_1.0.12               viridis_0.6.5            
## [139] reticulate_1.35.0         stringi_1.8.3            
## [141] alabaster.schemas_1.3.1   zlibbioc_1.49.0          
## [143] MASS_7.3-60.2             AnnotationHub_3.11.1     
## [145] plyr_1.8.9                parallel_4.4.0           
## [147] listenv_0.9.1             ggrepel_0.9.5            
## [149] deldir_2.0-4              Biostrings_2.71.2        
## [151] splines_4.4.0             tensor_1.5               
## [153] hms_1.1.3                 igraph_2.0.2             
## [155] spatstat.geom_3.2-9       RcppHNSW_0.6.0           
## [157] ScaledMatrix_1.11.1       biomaRt_2.59.1           
## [159] BiocVersion_3.19.1        XML_3.99-0.16.1          
## [161] evaluate_0.23             BiocManager_1.30.22      
## [163] httpuv_1.6.14             RANN_2.6.1               
## [165] tidyr_1.3.1               purrr_1.0.2              
## [167] polyclip_1.10-6           future_1.33.1            
## [169] scattermore_1.2           alabaster.sce_1.3.3      
## [171] rsvd_1.0.5                xtable_1.8-4             
## [173] restfulr_0.0.15           AnnotationFilter_1.27.0  
## [175] RSpectra_0.16-1           later_1.3.2              
## [177] viridisLite_0.4.2         tibble_3.2.1             
## [179] beeswarm_0.4.0            memoise_2.0.1            
## [181] aws.signature_0.6.0       AnnotationDbi_1.65.2     
## [183] GenomicAlignments_1.39.4  cluster_2.1.6            
## [185] globals_0.16.2