Gene Detection Analysis for scRNA-seq

Ruoxin Li, Gerald Quon

2024-05-01

Introduction

This tutorial provides an example analysis for modelling gene detection pattern as outlined in R.Li et al, 2018. The goal of this tutorial is to provide an overview of the cell type classification and visualization tasks by learning a low dimensional embedding through a class of gene detection models: that is BFA and Binary PCA.

Summary of workflow

The following workflow summarizes a typical dimensionality reduction procedure performed by BFA or Binary PCA.

  1. Data processing
  2. Dimensionality reduction
  3. Visualization

Installation

Let’s start with the installation

if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install("scBFA")

next we can load dependent packages

library(zinbwave)
library(SingleCellExperiment)
library(ggplot2)
library(scBFA)

Information of example dataset

The example dataset is generated from our scRNA-seq pre-DC/cDC dataset sourced from https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE89232 After performing all quality control procedure of genes and cells (as outlined in the paper), we then select 500 most variable genes for illustration purpose. The example dataset consists of 950 cells and 500 genes


# raw counts matrix with rows are genes and columns are cells
data("exprdata")

# a vector specify the ground truth of cell types provided by conquer database
data("celltype")

Working with SingleCellExperiment class

The design of BFA and Binary PCA allows three kinds of input object(scData):

For illustration, here we construct a singleCellExperiment class to be the input of BFA and Binary PCA.

sce <- SingleCellExperiment(assay = list(counts = exprdata))

Gene Detection Model analysis

Binary Factor Analysis

Let \(N\) stands for number of cells, \(G\) stands for the number of genes, and \(K\) stands for the number of latent dimensions.

A bfa model object computes the following parameters after fitting the gene detection matrix.

  1. \(Z\) is \(N\) by \(K\) embedding matrix and is named as “ZZ” in the model object
  2. \(A\) is \(G\) by \(K\) loading matrix and is named as “AA” in the model object
  3. \(\beta\) if there is cell-level covariates (e.g batch effect), \(\beta\) is corresponding coefficient matrix and is named as “beta” in the model object
  4. \(\gamma\) if there is gene-level covariates (e.g QC measures), \(\gamma\) is corresponding coefficient matrix and is named as “gamma” in the model object

We choose 3 as number of latent dimensions and project the gene detection matrix on the embedding space.

bfa_model = scBFA(scData = sce, numFactors = 2) 

We then visualize the low dimensional embedding of BFA in tSNE space. Points are colored by their corresponding cell types.

set.seed(5)
df = as.data.frame(bfa_model$ZZ)
df$celltype = celltype

p1 <- ggplot(df,aes(x = V1,y = V2,colour = celltype))
p1 <- p1 + geom_jitter(size=2.5,alpha = 0.8) 
colorvalue <- c("#43d5f9","#24b71f","#E41A1C", "#ffc935","#3d014c","#39ddb2",
                "slateblue2","maroon","#f7df27","palevioletred1","olivedrab3",
                "#377EB8","#5043c1","blue","aquamarine2","chartreuse4",
                "burlywood2","indianred1","mediumorchid1")
p1 <- p1 + xlab("tsne axis 1") + ylab("tsne axis 2") 
p1 <- p1 + scale_color_manual(values = colorvalue)
p1 <- p1 + theme(panel.background = element_blank(),
                  legend.position = "right",
                  axis.text=element_blank(),
                  axis.line.x = element_line(color="black"),
                  axis.line.y = element_line(color="black"),
                  plot.title = element_blank()
                   )
p1

Binary PCA

bpca = BinaryPCA(scData = sce) 

We then visualize the low dimensional embedding of Binary PCA in tSNE space. Points are colored by their corresponding cell types.


df = as.data.frame(bpca$x[,c(1:2)])
colnames(df) = c("V1","V2")
df$celltype = celltype

p1 <- ggplot(df,aes(x = V1,y = V2,colour = celltype))
p1 <- p1 + geom_jitter(size=2.5,alpha = 0.8) 
colorvalue <- c("#43d5f9","#24b71f","#E41A1C", "#ffc935","#3d014c","#39ddb2",
                "slateblue2","maroon","#f7df27","palevioletred1","olivedrab3",
                "#377EB8","#5043c1","blue","aquamarine2","chartreuse4",
                "burlywood2","indianred1","mediumorchid1")
p1 <- p1 + xlab("tsne axis 1") + ylab("tsne axis 2") 
p1 <- p1 + scale_color_manual(values = colorvalue)
p1 <- p1 + theme(panel.background = element_blank(),
                legend.position = "right",
                axis.text=element_blank(),
                axis.line.x = element_line(color="black"),
                axis.line.y = element_line(color="black"),
                plot.title = element_blank()
                )
p1

Session Info

sessionInfo()
#> R version 4.4.0 beta (2024-04-15 r86425)
#> 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_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [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] scBFA_1.18.0                ggplot2_3.5.1              
#>  [3] zinbwave_1.26.0             SingleCellExperiment_1.26.0
#>  [5] SummarizedExperiment_1.34.0 Biobase_2.64.0             
#>  [7] GenomicRanges_1.56.0        GenomeInfoDb_1.40.0        
#>  [9] IRanges_2.38.0              S4Vectors_0.42.0           
#> [11] BiocGenerics_0.50.0         MatrixGenerics_1.16.0      
#> [13] matrixStats_1.3.0          
#> 
#> loaded via a namespace (and not attached):
#>   [1] RcppAnnoy_0.0.22        splines_4.4.0           later_1.3.2            
#>   [4] tibble_3.2.1            polyclip_1.10-6         XML_3.99-0.16.1        
#>   [7] fastDummies_1.7.3       lifecycle_1.0.4         edgeR_4.2.0            
#>  [10] globals_0.16.3          lattice_0.22-6          MASS_7.3-60.2          
#>  [13] magrittr_2.0.3          limma_3.60.0            plotly_4.10.4          
#>  [16] sass_0.4.9              rmarkdown_2.26          jquerylib_0.1.4        
#>  [19] yaml_2.3.8              httpuv_1.6.15           Seurat_5.0.3           
#>  [22] sctransform_0.4.1       spam_2.10-0             sp_2.1-4               
#>  [25] spatstat.sparse_3.0-3   reticulate_1.36.1       cowplot_1.1.3          
#>  [28] pbapply_1.7-2           DBI_1.2.2               RColorBrewer_1.1-3     
#>  [31] ADGofTest_0.3           abind_1.4-5             zlibbioc_1.50.0        
#>  [34] Rtsne_0.17              pspline_1.0-19          purrr_1.0.2            
#>  [37] GenomeInfoDbData_1.2.12 ggrepel_0.9.5           irlba_2.3.5.1          
#>  [40] listenv_0.9.1           spatstat.utils_3.0-4    genefilter_1.86.0      
#>  [43] goftest_1.2-3           RSpectra_0.16-1         spatstat.random_3.2-3  
#>  [46] annotate_1.82.0         fitdistrplus_1.1-11     parallelly_1.37.1      
#>  [49] leiden_0.4.3.1          codetools_0.2-20        DelayedArray_0.30.0    
#>  [52] tidyselect_1.2.1        farver_2.1.1            UCSC.utils_1.0.0       
#>  [55] spatstat.explore_3.2-7  jsonlite_1.8.8          progressr_0.14.0       
#>  [58] ggridges_0.5.6          survival_3.6-4          tools_4.4.0            
#>  [61] ica_1.0-3               Rcpp_1.0.12             glue_1.7.0             
#>  [64] gridExtra_2.3           SparseArray_1.4.0       xfun_0.43              
#>  [67] DESeq2_1.44.0           dplyr_1.1.4             numDeriv_2016.8-1.1    
#>  [70] withr_3.0.0             fastmap_1.1.1           fansi_1.0.6            
#>  [73] digest_0.6.35           R6_2.5.1                mime_0.12              
#>  [76] colorspace_2.1-0        scattermore_1.2         tensor_1.5             
#>  [79] spatstat.data_3.0-4     RSQLite_2.3.6           copula_1.1-3           
#>  [82] utf8_1.2.4              tidyr_1.3.1             generics_0.1.3         
#>  [85] data.table_1.15.4       httr_1.4.7              htmlwidgets_1.6.4      
#>  [88] S4Arrays_1.4.0          uwot_0.2.2              pkgconfig_2.0.3        
#>  [91] gtable_0.3.5            blob_1.2.4              lmtest_0.9-40          
#>  [94] XVector_0.44.0          pcaPP_2.0-4             htmltools_0.5.8.1      
#>  [97] dotCall64_1.1-1         SeuratObject_5.0.1      scales_1.3.0           
#> [100] png_0.1-8               knitr_1.46              reshape2_1.4.4         
#> [103] nlme_3.1-164            cachem_1.0.8            zoo_1.8-12             
#> [106] stringr_1.5.1           KernSmooth_2.23-22      parallel_4.4.0         
#> [109] miniUI_0.1.1.1          softImpute_1.4-1        AnnotationDbi_1.66.0   
#> [112] pillar_1.9.0            grid_4.4.0              vctrs_0.6.5            
#> [115] RANN_2.6.1              promises_1.3.0          xtable_1.8-4           
#> [118] cluster_2.1.6           evaluate_0.23           mvtnorm_1.2-4          
#> [121] cli_3.6.2               locfit_1.5-9.9          compiler_4.4.0         
#> [124] rlang_1.1.3             crayon_1.5.2            future.apply_1.11.2    
#> [127] labeling_0.4.3          plyr_1.8.9              stringi_1.8.3          
#> [130] viridisLite_0.4.2       deldir_2.0-4            BiocParallel_1.38.0    
#> [133] munsell_0.5.1           Biostrings_2.72.0       gsl_2.1-8              
#> [136] lazyeval_0.2.2          spatstat.geom_3.2-9     Matrix_1.7-0           
#> [139] RcppHNSW_0.6.0          stabledist_0.7-1        patchwork_1.2.0        
#> [142] bit64_4.0.5             future_1.33.2           KEGGREST_1.44.0        
#> [145] statmod_1.5.0           shiny_1.8.1.1           highr_0.10             
#> [148] ROCR_1.0-11             igraph_2.0.3            memoise_2.0.1          
#> [151] bslib_0.7.0             bit_4.0.5