Load in the data

This vignette highlights some example workflows for performing differential expression in Seurat. For demonstration purposes, we will be using the interferon-beta stimulated human PBMCs dataset (ifnb) that is available via the SeuratData package.

Perform default differential expression tests

The bulk of Seurat’s differential expression features can be accessed through the FindMarkers() function. By default, Seurat performs differential expression (DE) testing based on the non-parametric Wilcoxon rank sum test. To test for DE genes between two specific groups of cells, specify the ident.1 and ident.2 parameters.

# Normalize the data
ifnb <- NormalizeData(ifnb)

# Find DE features between CD16 Mono and CD1 Mono
Idents(ifnb) <- "seurat_annotations"
monocyte.de.markers <- FindMarkers(ifnb, ident.1 = "CD16 Mono", ident.2 = "CD14 Mono")
# view results
head(monocyte.de.markers)
##        p_val avg_log2FC pct.1 pct.2 p_val_adj
## VMO1       0   5.700274 0.778 0.084         0
## MS4A4A     0   3.349751 0.748 0.143         0
## FCGR3A     0   3.281942 0.982 0.418         0
## PLAC8      0   3.268470 0.636 0.124         0
## CXCL16     0   2.014758 0.938 0.475         0
## MS4A7      0   2.386436 0.978 0.558         0

The results data frame has the following columns :

  • p_val : p-value (unadjusted)
  • avg_log2FC : log fold-change of the average expression between the two groups. Positive values indicate that the feature is more highly expressed in the first group.
  • pct.1 : The percentage of cells where the feature is detected in the first group
  • pct.2 : The percentage of cells where the feature is detected in the second group
  • p_val_adj : Adjusted p-value, based on Bonferroni correction using all features in the dataset.

If the ident.2 parameter is omitted or set to NULL, FindMarkers() will test for differentially expressed features between the group specified by ident.1 and all other cells. Additionally, the parameter only.pos can be set to TRUE to only search for positive markers, i.e. features that are more highly expressed in the ident.1 group.

# Find differentially expressed features between CD14+ Monocytes and all other cells, only
# search for positive markers
monocyte.de.markers <- FindMarkers(ifnb, ident.1 = "CD16 Mono", ident.2 = NULL, only.pos = TRUE)
# view results
head(monocyte.de.markers)
##        p_val avg_log2FC pct.1 pct.2 p_val_adj
## FCGR3A     0   4.532656 0.982 0.168         0
## MS4A7      0   3.806350 0.978 0.216         0
## CXCL16     0   3.274267 0.938 0.196         0
## VMO1       0   6.254651 0.778 0.044         0
## MS4A4A     0   4.747731 0.748 0.055         0
## LST1       0   2.927351 0.912 0.228         0

Perform DE analysis within the same cell type across conditions

Since this dataset contains treatment information (control versus stimulated with interferon-beta), we can also ask what genes change in different conditions for cells of the same type. First, we create a column in the meta.data slot to hold both the cell type and treatment information and switch the current Idents to that column. Then we use FindMarkers() to find the genes that are different between control and stimulated CD14 monocytes.

ifnb$celltype.stim <- paste(ifnb$seurat_annotations, ifnb$stim, sep = "_")
Idents(ifnb) <- "celltype.stim"
mono.de <- FindMarkers(ifnb, ident.1 = "CD14 Mono_STIM", ident.2 = "CD14 Mono_CTRL", verbose = FALSE)
head(mono.de, n = 10)
##         p_val avg_log2FC pct.1 pct.2 p_val_adj
## IFIT1       0   7.319139 0.985 0.033         0
## CXCL10      0   8.036564 0.984 0.035         0
## RSAD2       0   6.741673 0.988 0.045         0
## TNFSF10     0   6.991279 0.989 0.047         0
## IFIT3       0   6.883785 0.992 0.056         0
## IFIT2       0   7.179929 0.961 0.039         0
## CXCL11      0   8.624208 0.932 0.012         0
## CCL8        0   9.134191 0.918 0.017         0
## IDO1        0   5.455898 0.965 0.089         0
## MX1         0   5.059052 0.960 0.093         0

However, the p-values obtained from this analysis should be interpreted with caution, because these tests treat each cell as an independent replicate and ignore inherent correlations between cells originating from the same sample. Such analyses have been shown to find a large number of false positive associations, as has been demonstrated by Squair et al., 2021, Zimmerman et al., 2021, Junttila et al., 2022, and others. Below, we show how pseudobulking can be used to account for such within-sample correlation.

Perform DE analysis after pseudobulking

To pseudobulk, we will use AggregateExpression() to sum together gene counts of all the cells from the same sample for each cell type. This results in one gene expression profile per sample and cell type. We can then perform DE analysis using DESeq2 on the sample level. This treats the samples, rather than the individual cells, as independent observations.

First, we need to retrieve the sample information for each cell. This is not loaded in the metadata, so we will load it from the Github repo of the source data for the original paper.

Add sample information to the dataset

# load the inferred sample IDs of each cell
ctrl <- read.table(url("https://raw.githubusercontent.com/yelabucsf/demuxlet_paper_code/master/fig3/ye1.ctrl.8.10.sm.best"), head = T, stringsAsFactors = F)
stim <- read.table(url("https://raw.githubusercontent.com/yelabucsf/demuxlet_paper_code/master/fig3/ye2.stim.8.10.sm.best"), head = T, stringsAsFactors = F)
info <- rbind(ctrl, stim)

# rename the cell IDs by substituting the '-' into '.'
info$BARCODE <- gsub(pattern = "\\-", replacement = "\\.", info$BARCODE)

# only keep the cells with high-confidence sample ID
info <- info[grep(pattern = "SNG", x = info$BEST), ]

# remove cells with duplicated IDs in both ctrl and stim groups
info <- info[!duplicated(info$BARCODE) & !duplicated(info$BARCODE, fromLast = T), ]

# now add the sample IDs to ifnb 
rownames(info) <- info$BARCODE
info <- info[, c("BEST"), drop = F]
names(info) <- c("donor_id")
ifnb <- AddMetaData(ifnb, metadata = info)

# remove cells without donor IDs
ifnb$donor_id[is.na(ifnb$donor_id)] <- "unknown"
ifnb <- subset(ifnb, subset = donor_id != "unknown")


We can now perform pseudobulking (AggregateExpression()) based on the donor IDs.

# pseudobulk the counts based on donor-condition-celltype
pseudo_ifnb <- AggregateExpression(ifnb, assays = "RNA", return.seurat = T, group.by = c("stim", "donor_id", "seurat_annotations"))

# each 'cell' is a donor-condition-celltype pseudobulk profile
tail(Cells(pseudo_ifnb))
## [1] "STIM_SNG-1488_NK"          "STIM_SNG-1488_DC"         
## [3] "STIM_SNG-1488_B Activated" "STIM_SNG-1488_Mk"         
## [5] "STIM_SNG-1488_pDC"         "STIM_SNG-1488_Eryth"
pseudo_ifnb$celltype.stim <- paste(pseudo_ifnb$seurat_annotations, pseudo_ifnb$stim, sep = "_")

Next, we perform DE testing on the pseudobulk level for CD14 monocytes, and compare it against the previous single-cell-level DE results.

Idents(pseudo_ifnb) <- "celltype.stim"

bulk.mono.de <- FindMarkers(object = pseudo_ifnb, 
                         ident.1 = "CD14 Mono_STIM", 
                         ident.2 = "CD14 Mono_CTRL",
                         test.use = "DESeq2")
head(bulk.mono.de, n = 15)
##                  p_val avg_log2FC pct.1 pct.2     p_val_adj
## IL1RN    3.701542e-275   6.160156     1     1 5.201777e-271
## IFITM2   1.955626e-250   4.318976     1     1 2.748242e-246
## SSB      2.699554e-203   3.066647     1     1 3.793684e-199
## NT5C3A   2.239898e-198   5.412972     1     1 3.147729e-194
## RTCB     5.700554e-162   3.133362     1     1 8.010989e-158
## RABGAP1L 4.743010e-161   5.562364     1     1 6.665352e-157
## DYNLT1   9.735640e-159   2.402726     1     1 1.368150e-154
## PLSCR1   3.191691e-146   2.676047     1     1 4.485284e-142
## ISG20    9.664488e-145   5.443114     1     1 1.358150e-140
## NAPA     2.858013e-144   1.977719     1     1 4.016365e-140
## DDX58    5.957026e-142   4.640111     1     1 8.371409e-138
## HERC5    6.333722e-133   5.266515     1     1 8.900780e-129
## OASL     3.892853e-130   3.946745     1     1 5.470627e-126
## EIF2AK2  6.636434e-128   3.940167     1     1 9.326180e-124
## TMEM50A  6.731955e-117   1.355947     1     1 9.460417e-113
# compare the DE P-values between the single-cell level and the pseudobulk level results
names(bulk.mono.de) <- paste0(names(bulk.mono.de), ".bulk")
bulk.mono.de$gene <- rownames(bulk.mono.de)

names(mono.de) <- paste0(names(mono.de), ".sc")
mono.de$gene <- rownames(mono.de)

merge_dat <- merge(mono.de, bulk.mono.de, by = "gene")
merge_dat <- merge_dat[order(merge_dat$p_val.bulk), ]

# Number of genes that are marginally significant in both; marginally significant only in bulk; and marginally significant only in single-cell
common <- merge_dat$gene[which(merge_dat$p_val.bulk < 0.05 & 
                                merge_dat$p_val.sc < 0.05)]
only_sc <- merge_dat$gene[which(merge_dat$p_val.bulk > 0.05 & 
                                  merge_dat$p_val.sc < 0.05)]
only_bulk <- merge_dat$gene[which(merge_dat$p_val.bulk < 0.05 & 
                                    merge_dat$p_val.sc > 0.05)]
print(paste0('# Common: ',length(common)))
## [1] "# Common: 3519"
print(paste0('# Only in single-cell: ',length(only_sc)))
## [1] "# Only in single-cell: 1649"
print(paste0('# Only in bulk: ',length(only_bulk)))
## [1] "# Only in bulk: 204"

We can see that while the p-values are correlated between the single-cell and pseudobulk data, the single-cell p-values are often smaller and suggest higher levels of significance. In particular, there are 3,519 genes with evidence of differential expression (prior to multiple hypothesis testing) in both analyses, 1,649 genes that only appear to be differentially expressed in the single-cell analysis, and just 204 genes that only appear to be differentially expressed in the bulk analysis. We can investigate these discrepancies using VlnPlot.

First, we can examine the top genes that are differentially expressed in both analyses.

# create a new column to annotate sample-condition-celltype in the single-cell dataset
ifnb$donor_id.stim <- paste0(ifnb$stim, "-", ifnb$donor_id)

# generate violin plot 
Idents(ifnb) <- "celltype.stim"
print(merge_dat[merge_dat$gene%in%common[1:2],c('gene','p_val.sc','p_val.bulk')])
##        gene p_val.sc    p_val.bulk
## 2785  IL1RN        0 3.701542e-275
## 2739 IFITM2        0 1.955626e-250
VlnPlot(ifnb, features = common[1:2], idents = c("CD14 Mono_CTRL", "CD14 Mono_STIM"), group.by = "stim") 

VlnPlot(ifnb, features = common[1:2], idents = c("CD14 Mono_CTRL", "CD14 Mono_STIM"), group.by = "donor_id.stim", ncol = 1) 

In both the pseudobulk and single-cell analyses, the p-values for these two genes are astronomically small. For both of these genes, when just comparing all stimulated CD4 monocytes to all control CD4 monocytes across samples, we see much higher expression in the stimulated cells. When breaking down these cells by sample, we continue to see consistently higher expression levels in the stimulated samples compared to the control samples; in other words, this finding is not driven by just one or two samples. Because of this consistency, we find this signal in both analyses.

By contrast, we can examine examples of genes that are only DE under the single-cell analysis.

print(merge_dat[merge_dat$gene%in%c('SRGN','HLA-DRA'),c('gene','p_val.sc','p_val.bulk')])
##         gene     p_val.sc p_val.bulk
## 5710    SRGN 4.025076e-21  0.1823188
## 2603 HLA-DRA 4.989863e-09  0.1851302
VlnPlot(ifnb, features <- c('SRGN','HLA-DRA'), idents = c("CD14 Mono_CTRL", "CD14 Mono_STIM"), group.by = "stim") 

VlnPlot(ifnb, features <- c('SRGN','HLA-DRA'), idents = c("CD14 Mono_CTRL", "CD14 Mono_STIM"), group.by = "donor_id.stim", ncol = 1) 

Here, SRGN and HLA-DRA both have very small p-values in the single-cell analysis (on the orders of \(10^{-21}\) and \(10^{-9}\)), but much larger p-values around 0.18 in the pseudobulk analysis. While there appears to be a difference between control and simulated cells when ignoring sample information, the signal is much weaker on the sample level, and we can see notable variability from sample to sample.

Perform DE analysis using alternative tests

Finally, we also support many other DE tests using other methods. For completeness, the following tests are currently supported:

  • “wilcox” : Wilcoxon rank sum test (default, using ‘presto’ package)
  • “wilcox_limma” : Wilcoxon rank sum test (using ‘limma’ package)
  • “bimod” : Likelihood-ratio test for single cell feature expression, (McDavid et al., Bioinformatics, 2013)
  • “roc” : Standard AUC classifier
  • “t” : Student’s t-test
  • “poisson” : Likelihood ratio test assuming an underlying negative binomial distribution. Use only for UMI-based datasets
  • “negbinom” : Likelihood ratio test assuming an underlying negative binomial distribution. Use only for UMI-based datasets
  • “LR” : Uses a logistic regression framework to determine differentially expressed genes. Constructs a logistic regression model predicting group membership based on each feature individually and compares this to a null model with a likelihood ratio test.
  • “MAST” : GLM-framework that treates cellular detection rate as a covariate (Finak et al, Genome Biology, 2015) (Installation instructions)
  • “DESeq2” : DE based on a model using the negative binomial distribution (Love et al, Genome Biology, 2014) (Installation instructions) For MAST and DESeq2, please ensure that these packages are installed separately in order to use them as part of Seurat. Once installed, use the test.use parameter can be used to specify which DE test to use.
# Test for DE features using the MAST package
Idents(ifnb) <- "seurat_annotations"
head(FindMarkers(ifnb, ident.1 = "CD14 Mono", ident.2 = "CD16 Mono", test.use = "MAST"))

Session Info

## R version 4.2.2 Patched (2022-11-10 r83330)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.6 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.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       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] ggplot2_3.4.4                  thp1.eccite.SeuratData_3.1.5  
##  [3] stxBrain.SeuratData_0.1.1      ssHippo.SeuratData_3.1.4      
##  [5] pbmcsca.SeuratData_3.0.0       pbmcref.SeuratData_1.0.0      
##  [7] pbmcMultiome.SeuratData_0.1.4  pbmc3k.SeuratData_3.1.4       
##  [9] panc8.SeuratData_3.0.2         ifnb.SeuratData_3.0.0         
## [11] hcabm40k.SeuratData_3.0.0      cbmc.SeuratData_3.1.4         
## [13] bonemarrowref.SeuratData_1.0.0 bmcite.SeuratData_0.3.0       
## [15] SeuratData_0.2.2.9001          Seurat_5.0.1            
## [17] testthat_3.2.0                 SeuratObject_5.0.1            
## [19] sp_2.1-1                      
## 
## loaded via a namespace (and not attached):
##   [1] utf8_1.2.4                  spatstat.explore_3.2-5     
##   [3] reticulate_1.34.0           tidyselect_1.2.0           
##   [5] RSQLite_2.3.1               AnnotationDbi_1.60.2       
##   [7] htmlwidgets_1.6.2           grid_4.2.2                 
##   [9] BiocParallel_1.32.6         Rtsne_0.16                 
##  [11] devtools_2.4.5              munsell_0.5.0              
##  [13] codetools_0.2-19            ragg_1.2.5                 
##  [15] ica_1.0-3                   future_1.33.0              
##  [17] miniUI_0.1.1.1              withr_2.5.2                
##  [19] spatstat.random_3.2-1       colorspace_2.1-0           
##  [21] progressr_0.14.0            Biobase_2.58.0             
##  [23] highr_0.10                  knitr_1.45                 
##  [25] rstudioapi_0.14             stats4_4.2.2               
##  [27] ROCR_1.0-11                 tensor_1.5                 
##  [29] listenv_0.9.0               labeling_0.4.3             
##  [31] MatrixGenerics_1.10.0       GenomeInfoDbData_1.2.9     
##  [33] polyclip_1.10-6             farver_2.1.1               
##  [35] bit64_4.0.5                 rprojroot_2.0.4            
##  [37] parallelly_1.36.0           vctrs_0.6.4                
##  [39] generics_0.1.3              xfun_0.40                  
##  [41] GenomeInfoDb_1.34.9         R6_2.5.1                   
##  [43] ggbeeswarm_0.7.1            locfit_1.5-9.8             
##  [45] DelayedArray_0.24.0         bitops_1.0-7               
##  [47] spatstat.utils_3.0-4        cachem_1.0.8               
##  [49] promises_1.2.1              scales_1.2.1               
##  [51] beeswarm_0.4.0              gtable_0.3.4               
##  [53] globals_0.16.2              processx_3.8.2             
##  [55] goftest_1.2-3               spam_2.10-0                
##  [57] rlang_1.1.1                 systemfonts_1.0.4          
##  [59] splines_4.2.2               lazyeval_0.2.2             
##  [61] spatstat.geom_3.2-7         yaml_2.3.7                 
##  [63] reshape2_1.4.4              abind_1.4-5                
##  [65] httpuv_1.6.12               tools_4.2.2                
##  [67] usethis_2.1.6               ellipsis_0.3.2             
##  [69] jquerylib_0.1.4             RColorBrewer_1.1-3         
##  [71] BiocGenerics_0.44.0         sessioninfo_1.2.2          
##  [73] ggridges_0.5.4              Rcpp_1.0.11                
##  [75] plyr_1.8.9                  zlibbioc_1.44.0            
##  [77] RCurl_1.98-1.12             purrr_1.0.2                
##  [79] ps_1.7.5                    prettyunits_1.2.0          
##  [81] deldir_1.0-9                pbapply_1.7-2              
##  [83] cowplot_1.1.1               urlchecker_1.0.1           
##  [85] S4Vectors_0.36.2            zoo_1.8-12                 
##  [87] SummarizedExperiment_1.28.0 ggrepel_0.9.4              
##  [89] cluster_2.1.4               fs_1.6.3                   
##  [91] magrittr_2.0.3              data.table_1.14.8          
##  [93] RSpectra_0.16-1             scattermore_1.2            
##  [95] lmtest_0.9-40               RANN_2.6.1                 
##  [97] fitdistrplus_1.1-11         matrixStats_1.0.0          
##  [99] pkgload_1.3.3               patchwork_1.1.3            
## [101] mime_0.12                   evaluate_0.22              
## [103] xtable_1.8-4                XML_3.99-0.14              
## [105] IRanges_2.32.0              fastDummies_1.7.3          
## [107] gridExtra_2.3               compiler_4.2.2             
## [109] tibble_3.2.1                KernSmooth_2.23-22         
## [111] crayon_1.5.2                htmltools_0.5.6.1          
## [113] later_1.3.1                 geneplotter_1.76.0         
## [115] tidyr_1.3.0                 DBI_1.1.3                  
## [117] MASS_7.3-58.2               rappdirs_0.3.3             
## [119] Matrix_1.6-1.1              brio_1.1.3                 
## [121] cli_3.6.1                   parallel_4.2.2             
## [123] dotCall64_1.1-0             igraph_1.5.1               
## [125] GenomicRanges_1.50.2        pkgconfig_2.0.3            
## [127] pkgdown_2.0.7               plotly_4.10.3              
## [129] spatstat.sparse_3.0-3       annotate_1.76.0            
## [131] vipor_0.4.5                 bslib_0.5.1                
## [133] XVector_0.38.0              stringr_1.5.0              
## [135] callr_3.7.3                 digest_0.6.33              
## [137] sctransform_0.4.1           RcppAnnoy_0.0.21           
## [139] Biostrings_2.66.0           spatstat.data_3.0-3        
## [141] rmarkdown_2.25              leiden_0.4.3               
## [143] uwot_0.1.16                 shiny_1.7.5.1              
## [145] lifecycle_1.0.4             nlme_3.1-162               
## [147] jsonlite_1.8.7              desc_1.4.2                 
## [149] viridisLite_0.4.2           limma_3.54.1               
## [151] fansi_1.0.5                 pillar_1.9.0               
## [153] lattice_0.21-9              ggrastr_1.0.1              
## [155] KEGGREST_1.38.0             fastmap_1.1.1              
## [157] httr_1.4.7                  pkgbuild_1.4.2             
## [159] survival_3.5-7              glue_1.6.2                 
## [161] remotes_2.4.2.1             png_0.1-8                  
## [163] bit_4.0.5                   presto_1.0.0               
## [165] stringi_1.7.12              sass_0.4.7                 
## [167] profvis_0.3.7               blob_1.2.4                 
## [169] textshaping_0.3.6           RcppHNSW_0.5.0             
## [171] DESeq2_1.38.3               memoise_2.0.1              
## [173] dplyr_1.1.3                 irlba_2.3.5.1              
## [175] future.apply_1.11.0