Decomposing scRNA-seq data using NMF - a demo

Non-negative matrix factorization is a method for the analysis of high dimensional data that allows extracting sparse and meaningful features from a set of non-negative data vectors. It is well suited for decomposing scRNA-seq data, effectively reducing large complex matrices (\(10^4\) of genes times \(10^5\) of cells) into a few interpretable gene programs. It has been especially used to extract recurrent gene programs in cancer cells (see e.g. Barkely et al. (2022) and Gavish et al. (2023)), which are otherwise difficult to integrate and analyse jointly.

Here, to illustrate the methods implemented in the GeneNMF package, we will apply NMF on a single-cell cell dataset of human PBMCs - a downsampled version of the dataset published by Hao et al. (2021).

Set up the environment

Here are some packages you’ll need for this demo:

install.packages("GeneNMF")
library(GeneNMF)
library(Seurat)
library(ggplot2)
library(UCell)
library(patchwork)
library(Matrix)
library(RcppML)
library(viridis)

Then download the test dataset for this demo.

options(timeout=1000)

ddir <- "input"
data.path <- sprintf("%s/pbmc_multimodal.downsampled20k.seurat.rds", ddir)

if (!file.exists(data.path)) {
    dir.create(ddir)
    dataUrl <- "https://www.dropbox.com/s/akzu3hp4uz2mpkv/pbmc_multimodal.downsampled20k.seurat.rds?dl=1"
    download.file(dataUrl, data.path)
}

seu <- readRDS(data.path)

NMF for dimensionality reduction

NMF can be applied to reduce the dimensionality of the data from tens of thousand of genes to a few dimensions (similarly to PCA). With the RunNMF() function, it can be directly applied on a Seurat object, and it will save the NMF results as a new dimensionality reduction.

ndim <- 15

seu <- FindVariableFeatures(seu, nfeatures = 1000)
seu <- runNMF(seu, k = ndim, assay="SCT")
seu@reductions$NMF
## A dimensional reduction object with key NMF_ 
##  Number of dimensions: 15 
##  Number of cells: 20000 
##  Projected dimensional reduction calculated:  FALSE 
##  Jackstraw run: FALSE 
##  Computed using assay: SCT

We can also further reduced the dimensionality to 2 dimensions using UMAP; in this space we can visualize all cells in a single plot.

seu <- RunUMAP(seu, reduction = "NMF", dims=1:ndim, reduction.name = "NMF_UMAP", reduction.key = "nmfUMAP_")
DimPlot(seu, reduction = "NMF_UMAP", group.by = "celltype.l1", label=T) + theme(aspect.ratio = 1,
                                                            axis.text = element_blank(),
    axis.title = element_blank(),
    axis.ticks = element_blank()) + ggtitle("NMF UMAP") + NoLegend()

Consistent NMF meta-programs across multiple samples

Identification of robust gene programs requires their detection across samples and variability of input parameters. Perhaps the most crucial parameter to NMF is the dimensionality k, which corresponds to the number of programs of the low-dimensional matrix. To determine robust programs, we can run NMF over multiple numbers of k and determine programs that are consistently found across these runs. The multiNMF() function automatically performs NMF over a list of samples and for multiple values of k:

seu.list <- SplitObject(seu, split.by = "donor")

geneNMF.programs <- multiNMF(seu.list, assay="SCT", slot="data", k=4:9, L1=c(0,0),
                    do_centering=TRUE, nfeatures = 2000)

We can now combine the gene programs identified over multiple samples and numbers of k into metaprograms (MPs), i.e. consensus programs that are robustly identified across NMF runs. Here we will define 10 MPs:

geneNMF.metaprograms <- getMetaPrograms(geneNMF.programs,
                                        nprograms=10,
                                        max.genes=50,
                                        hclust.method="ward.D2",
                                        min.confidence=0.3)

It can be useful to visualize pairwise similarity (Jaccard Index) between individual gene programs that compose meta-programs. We can see “blocks” corresponding to gene programs of high similarity across datasets and values of k. We can then cut the similarity tree at a given height to find blocks of similar programs and derive consensus gene signatures for each block. For example, here we cut the tree to the height corresponding to 10 clusters of programs (i.e. 10 MPs):

ph <- plotMetaPrograms(geneNMF.metaprograms, jaccard.cutoff = c(0,0.8))

We can also inspect useful statistics, such as the “sample coverage” (in what fraction of samples the MP was detected); or the silhoette coefficient (how similar are individual programs in a MP relative to programs in other MPs - the higher the better).

Based on these metrics, one may decide to drop some of the programs, e.g. if they are specific for few samples only (low sample coverge), or have bad internal consistency (low silhouette and meanJaccard).

geneNMF.metaprograms$metaprograms.metrics
##      sampleCoverage  silhouette meanJaccard numberGenes numberPrograms
## MP1           1.000  0.82605598       0.834          50             27
## MP2           1.000  0.58203151       0.605          50             48
## MP3           1.000  0.56295385       0.579          50             48
## MP4           1.000  0.51359676       0.541          50             41
## MP5           1.000  0.42393323       0.550          50             22
## MP6           1.000  0.33653086       0.361          49             15
## MP7           1.000  0.26344233       0.321          50             41
## MP8           1.000  0.23339237       0.404          50             24
## MP9           1.000 -0.01466146       0.136          33             28
## MP10          0.875  0.28620891       0.340          50             18

What are the genes driving each program?

t(as.data.frame(lapply(geneNMF.metaprograms$metaprograms.genes, head)))
##      [,1]      [,2]       [,3]       [,4]      [,5]     [,6]       
## MP1  "ACRBP"   "ARHGAP18" "BEX3"     "CAVIN2"  "CD9"    "CLU"      
## MP2  "AKR1C3"  "CD160"    "CD247"    "HOPX"    "KLRF1"  "LAIR2"    
## MP3  "BANK1"   "CD22"     "CD79A"    "IGHD"    "IGHG3"  "IGHM"     
## MP4  "ADTRP"   "CCR7"     "CD27"     "FHIT"    "LEF1"   "LINC00861"
## MP5  "CD14"    "CXCL8"    "CYP1B1"   "EGR1"    "FCN1"   "FOS"      
## MP6  "C1orf54" "CLEC10A"  "CACNA2D3" "PID1"    "RTN1"   "ZNF503"   
## MP7  "CDKN1C"  "MS4A7"    "HES4"     "HMOX1"   "SMIM25" "C1QA"     
## MP8  "CYP1B1"  "RBP7"     "S100A12"  "S100A8"  "S100A9" "VCAN"     
## MP9  "IER3"    "SGK1"     "IL1RN"    "CCL3"    "CCL3L1" "CCL4L2"   
## MP10 "CFH"     "KLRG1"    "TRGC2"    "CAMK2N1" "LAG3"   "LINC01871"

Intepretation of meta-programs by GSEA

To aid the interpretation of MPs, we can compare them to known signatures from public databases. The runGSEA() function can be useful to scan msigDB and evaluate the overlap of detected MPs with signatures in the databases. Here we compare to the “C8” category (cell type signature gene sets); but other classes such as “H” (hallmark gene sets) may be more relevant in other contexts.

library(msigdbr)
library(fgsea)
top_p <- lapply(geneNMF.metaprograms$metaprograms.genes, function(program) {
  runGSEA(program, universe=rownames(seu), category = "C8")
})

For example, MP1 appears to correlate significantly with Platelet/Megakaryocyte signatures:

head(top_p$MP1)
##                                                  pathway         pval
##                                                   <char>        <num>
## 1:           TRAVAGLINI_LUNG_PLATELET_MEGAKARYOCYTE_CELL 3.068484e-66
## 2:                              HAY_BONE_MARROW_PLATELET 1.436801e-52
## 3: ZHENG_CORD_BLOOD_C1_PUTATIVE_MEGAKARYOCYTE_PROGENITOR 2.651212e-39
## 4:                  DESCARTES_FETAL_LIVER_MEGAKARYOCYTES 6.996490e-24
## 5:               DESCARTES_FETAL_PLACENTA_MEGAKARYOCYTES 1.181244e-23
## 6:                  DESCARTES_FETAL_HEART_MEGAKARYOCYTES 4.475700e-20
##            padj overlap  size overlapGenes
##           <num>   <int> <int>       <list>
## 1: 2.141802e-63      45   548 ACRBP, A....
## 2: 5.014434e-50      33   242 ACRBP, A....
## 3: 6.168486e-37      22    96 ARHGAP18....
## 4: 1.220888e-21      16   128 ACRBP, A....
## 5: 1.649016e-21      17   170 ACRBP, C....
## 6: 5.206731e-18      13    94 ACRBP, C....

Signature scores for meta-programs

A simple way to evaluate MPs learned from the data is to calculate gene signature scores with the UCell package.

mp.genes <- geneNMF.metaprograms$metaprograms.genes
seu <- AddModuleScore_UCell(seu, features = mp.genes, assay="SCT", ncores=4, name = "")

We can now see which MPs are enriched in specific cell subtypes (cell type annotation from the original study).

VlnPlot(seu, features=names(mp.genes), group.by = "celltype.l1",
        pt.size = 0, ncol=5)

MP signature scores can define integrated spaces

Individual cells can now be represented in terms of their MP scores. Importantly, here the MPs were learned as a consensus of gene programs found across multiple samples – as opposed to calculating NMF once on the whole dataset. This can be an effective strategy to mitigate batch effects and represent samples in a common coordinate space. Let’s store these coordinates in the Seurat object:

matrix <- seu@meta.data[,names(mp.genes)]
dimred <- as.matrix(matrix)

colnames(dimred) <- paste0("MP_",seq(1, ncol(dimred)))
#New dim reduction
seu@reductions[["MPsignatures"]] <- new("DimReduc",
                                         cell.embeddings = dimred,
                                         assay.used = "RNA",
                                         key = "MP_",
                                         global = FALSE)

We can also use these scores to generate a UMAP representation and visualize the data in 2D:

set.seed(123)
seu <- RunUMAP(seu, reduction="MPsignatures", dims=1:length(seu@reductions[["MPsignatures"]]),
               metric = "euclidean", reduction.name = "umap_MP")

How do the signature scores for the meta-programs look like in the combined space?

FeaturePlot(seu, features = names(mp.genes), reduction = "umap_MP", ncol=4) &
  scale_color_viridis(option="B") &
   theme(aspect.ratio = 1, axis.text=element_blank(), axis.ticks=element_blank())

Final remarks

NMF can be a powerful tool to extract gene programs for scRNA-seq data in an unbiased manner. Because it is calculated for each sample separately, it bypasses the need to perform batch effect correction to analyse samples jointly. This aspect makes it particularly interesting for the analysis of gene programs in cancer cells (see e.g. Barkely et al. (2022) and Gavish et al. (2023).

References

  • GeneNMF package - LINK
  • We use the ultra-fast RcppML method for NMF - LINK

Session Info

sessionInfo()
## R version 4.3.2 (2023-10-31)
## Platform: x86_64-apple-darwin20 (64-bit)
## Running under: macOS Monterey 12.6
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRblas.0.dylib 
## LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## time zone: Europe/Zurich
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices datasets  utils     methods   base     
## 
## other attached packages:
##  [1] fgsea_1.28.0        msigdbr_7.5.1       viridis_0.6.5      
##  [4] viridisLite_0.4.2   RcppML_0.3.7        Matrix_1.6-5       
##  [7] patchwork_1.2.0     UCell_2.6.2         ggplot2_3.5.0      
## [10] Seurat_5.0.2        SeuratObject_5.0.1  sp_2.1-3           
## [13] GeneNMF_0.4.0       Biobase_2.62.0      BiocGenerics_0.48.1
## [16] renv_1.0.5         
## 
## loaded via a namespace (and not attached):
##   [1] RcppAnnoy_0.0.22            splines_4.3.2              
##   [3] later_1.3.2                 bitops_1.0-7               
##   [5] tibble_3.2.1                polyclip_1.10-6            
##   [7] fastDummies_1.7.3           lifecycle_1.0.4            
##   [9] doParallel_1.0.17           globals_0.16.2             
##  [11] lattice_0.21-9              MASS_7.3-60                
##  [13] magrittr_2.0.3              plotly_4.10.4              
##  [15] sass_0.4.8                  rmarkdown_2.25             
##  [17] jquerylib_0.1.4             yaml_2.3.8                 
##  [19] httpuv_1.6.14               NMF_0.26                   
##  [21] sctransform_0.4.1           spam_2.10-0                
##  [23] spatstat.sparse_3.0-3       reticulate_1.35.0          
##  [25] cowplot_1.1.3               pbapply_1.7-2              
##  [27] RColorBrewer_1.1-3          abind_1.4-5                
##  [29] zlibbioc_1.48.0             Rtsne_0.17                 
##  [31] GenomicRanges_1.54.1        purrr_1.0.2                
##  [33] RCurl_1.98-1.14             GenomeInfoDbData_1.2.11    
##  [35] IRanges_2.36.0              S4Vectors_0.40.2           
##  [37] ggrepel_0.9.5               irlba_2.3.5.1              
##  [39] listenv_0.9.1               spatstat.utils_3.0-4       
##  [41] pheatmap_1.0.12             goftest_1.2-3              
##  [43] RSpectra_0.16-1             spatstat.random_3.2-3      
##  [45] fitdistrplus_1.1-11         parallelly_1.37.1          
##  [47] leiden_0.4.3.1              codetools_0.2-19           
##  [49] DelayedArray_0.28.0         tidyselect_1.2.0           
##  [51] farver_2.1.1                matrixStats_1.2.0          
##  [53] stats4_4.3.2                rmdformats_1.0.4           
##  [55] spatstat.explore_3.2-6      jsonlite_1.8.8             
##  [57] BiocNeighbors_1.20.2        ellipsis_0.3.2             
##  [59] progressr_0.14.0            ggridges_0.5.6             
##  [61] survival_3.5-7              iterators_1.0.14           
##  [63] foreach_1.5.2               tools_4.3.2                
##  [65] ica_1.0-3                   Rcpp_1.0.12                
##  [67] glue_1.7.0                  SparseArray_1.2.4          
##  [69] gridExtra_2.3               xfun_0.42                  
##  [71] MatrixGenerics_1.14.0       GenomeInfoDb_1.38.6        
##  [73] dplyr_1.1.4                 withr_3.0.0                
##  [75] BiocManager_1.30.22         fastmap_1.1.1              
##  [77] fansi_1.0.6                 digest_0.6.34              
##  [79] R6_2.5.1                    mime_0.12                  
##  [81] colorspace_2.1-0            scattermore_1.2            
##  [83] tensor_1.5                  spatstat.data_3.0-4        
##  [85] utf8_1.2.4                  tidyr_1.3.1                
##  [87] generics_0.1.3              data.table_1.15.2          
##  [89] httr_1.4.7                  htmlwidgets_1.6.4          
##  [91] S4Arrays_1.2.0              uwot_0.1.16                
##  [93] pkgconfig_2.0.3             gtable_0.3.4               
##  [95] registry_0.5-1              lmtest_0.9-40              
##  [97] SingleCellExperiment_1.24.0 XVector_0.42.0             
##  [99] htmltools_0.5.7             dotCall64_1.1-1            
## [101] bookdown_0.37               scales_1.3.0               
## [103] png_0.1-8                   knitr_1.45                 
## [105] rstudioapi_0.15.0           reshape2_1.4.4             
## [107] nlme_3.1-163                cachem_1.0.8               
## [109] zoo_1.8-12                  stringr_1.5.1              
## [111] KernSmooth_2.23-22          parallel_4.3.2             
## [113] miniUI_0.1.1.1              pillar_1.9.0               
## [115] grid_4.3.2                  vctrs_0.6.5                
## [117] RANN_2.6.1                  promises_1.2.1             
## [119] xtable_1.8-4                cluster_2.1.4              
## [121] evaluate_0.23               cli_3.6.2                  
## [123] compiler_4.3.2              rlang_1.1.3                
## [125] crayon_1.5.2                rngtools_1.5.2             
## [127] future.apply_1.11.1         labeling_0.4.3             
## [129] plyr_1.8.9                  stringi_1.8.3              
## [131] deldir_2.0-4                gridBase_0.4-7             
## [133] BiocParallel_1.36.0         babelgene_22.9             
## [135] munsell_0.5.0               lazyeval_0.2.2             
## [137] spatstat.geom_3.2-9         RcppHNSW_0.6.0             
## [139] future_1.33.1               shiny_1.8.0                
## [141] highr_0.10                  SummarizedExperiment_1.32.0
## [143] ROCR_1.0-11                 igraph_2.0.2               
## [145] bslib_0.6.1                 fastmatch_1.1-4