1 Introduction

Since read counts are summed across cells in a pseudobulk approach, modeling continuous cell-level covariates also requires a collapsing step. Here we summarize the values of a variable from a set of cells using the mean, and store the value for each cell type. Including these variables in a regression formula uses the summarized values from the corresponding cell type.

We demonstrate this feature on a lightly modified analysis of PBMCs from 8 individuals stimulated with interferon-β (Kang, et al, 2018, Nature Biotech).

2 Standard processing

Here is the code from the main vignette:

library(dreamlet)
library(muscat)
library(ExperimentHub)
library(scater)

# Download data, specifying EH2259 for the Kang, et al study
eh <- ExperimentHub()
sce <- eh[["EH2259"]]

# only keep singlet cells with sufficient reads
sce <- sce[rowSums(counts(sce) > 0) > 0, ]
sce <- sce[, colData(sce)$multiplets == "singlet"]

# compute QC metrics
qc <- perCellQCMetrics(sce)

# remove cells with few or many detected genes
ol <- isOutlier(metric = qc$detected, nmads = 2, log = TRUE)
sce <- sce[, !ol]

# set variable indicating stimulated (stim) or control (ctrl)
sce$StimStatus <- sce$stim

In many datasets, continuous cell-level variables could be mapped reads, gene count, mitochondrial rate, etc. There are no continuous cell-level variables in this dataset, so we can simulate two from a normal distribution:

sce$value1 <- rnorm(ncol(sce))
sce$value2 <- rnorm(ncol(sce))

3 Pseudobulk

Now compute the pseudobulk using standard code:

sce$id <- paste0(sce$StimStatus, sce$ind)

# Create pseudobulk
pb <- aggregateToPseudoBulk(sce,
  assay = "counts",
  cluster_id = "cell",
  sample_id = "id",
  verbose = FALSE
)

The means per variable, cell type, and sample are stored in the pseudobulk SingleCellExperiment object:

metadata(pb)$aggr_means
## # A tibble: 128 × 5
## # Groups:   cell [8]
##    cell    id       cluster   value1  value2
##    <fct>   <fct>      <dbl>    <dbl>   <dbl>
##  1 B cells ctrl101     3.96  0.102   -0.0409
##  2 B cells ctrl1015    4.00  0.00903  0.0262
##  3 B cells ctrl1016    4    -0.0819  -0.0363
##  4 B cells ctrl1039    4.04  0.118   -0.354 
##  5 B cells ctrl107     4     0.461   -0.0924
##  6 B cells ctrl1244    4    -0.0371  -0.0827
##  7 B cells ctrl1256    4.01  0.0280   0.0430
##  8 B cells ctrl1488    4.02 -0.0282   0.0773
##  9 B cells stim101     4.09 -0.196    0.0144
## 10 B cells stim1015    4.06 -0.0123  -0.0779
## # ℹ 118 more rows

4 Analysis

Including these variables in a regression formula uses the summarized values from the corresponding cell type. This happens behind the scenes, so the user doesn’t need to distinguish bewteen sample-level variables stored in colData(pb) and cell-level variables stored in metadata(pb)$aggr_means.

Variance partition and hypothesis testing proceeds as ususal:

form <- ~ StimStatus + value1 + value2

# Normalize and apply voom/voomWithDreamWeights
res.proc <- processAssays(pb, form, min.count = 5)

# run variance partitioning analysis
vp.lst <- fitVarPart(res.proc, form)

# Summarize variance fractions genome-wide for each cell type
plotVarPart(vp.lst, label.angle = 60)

# Differential expression analysis within each assay
res.dl <- dreamlet(res.proc, form)

# dreamlet results include coefficients for value1 and value2
res.dl
## class: dreamletResult 
## assays(8): B cells CD14+ Monocytes ... Megakaryocytes NK cells
## Genes:
##  min: 164 
##  max: 5262 
## details(7): assay n_retain ... n_errors error_initial
## coefNames(4): (Intercept) StimStatusstim value1 value2

5 Session Info

## R version 4.3.1 (2023-06-16)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 22.04.3 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.18-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] Matrix_1.6-1.1              muscData_1.15.0            
##  [3] scater_1.30.0               scuttle_1.12.0             
##  [5] SingleCellExperiment_1.24.0 SummarizedExperiment_1.32.0
##  [7] Biobase_2.62.0              GenomicRanges_1.54.0       
##  [9] GenomeInfoDb_1.38.0         IRanges_2.36.0             
## [11] S4Vectors_0.40.0            MatrixGenerics_1.14.0      
## [13] matrixStats_1.0.0           ExperimentHub_2.10.0       
## [15] AnnotationHub_3.10.0        BiocFileCache_2.10.0       
## [17] dbplyr_2.3.4                BiocGenerics_0.48.0        
## [19] muscat_1.16.0               dreamlet_1.0.0             
## [21] variancePartition_1.32.0    BiocParallel_1.36.0        
## [23] limma_3.58.0                ggplot2_3.4.4              
## [25] BiocStyle_2.30.0           
## 
## loaded via a namespace (and not attached):
##   [1] bitops_1.0-7                  httr_1.4.7                   
##   [3] RColorBrewer_1.1-3            doParallel_1.0.17            
##   [5] Rgraphviz_2.46.0              numDeriv_2016.8-1.1          
##   [7] tools_4.3.1                   sctransform_0.4.1            
##   [9] backports_1.4.1               utf8_1.2.4                   
##  [11] R6_2.5.1                      mgcv_1.9-0                   
##  [13] GetoptLong_1.0.5              withr_2.5.1                  
##  [15] prettyunits_1.2.0             gridExtra_2.3                
##  [17] cli_3.6.1                     sandwich_3.0-2               
##  [19] labeling_0.4.3                sass_0.4.7                   
##  [21] KEGGgraph_1.62.0              SQUAREM_2021.1               
##  [23] mvtnorm_1.2-3                 blme_1.0-5                   
##  [25] mixsqp_0.3-48                 zenith_1.4.0                 
##  [27] parallelly_1.36.0             invgamma_1.1                 
##  [29] RSQLite_2.3.1                 generics_0.1.3               
##  [31] shape_1.4.6                   gtools_3.9.4                 
##  [33] dplyr_1.1.3                   ggbeeswarm_0.7.2             
##  [35] fansi_1.0.5                   abind_1.4-5                  
##  [37] lifecycle_1.0.3               multcomp_1.4-25              
##  [39] yaml_2.3.7                    edgeR_4.0.0                  
##  [41] gplots_3.1.3                  SparseArray_1.2.0            
##  [43] grid_4.3.1                    blob_1.2.4                   
##  [45] promises_1.2.1                crayon_1.5.2                 
##  [47] lattice_0.22-5                beachmat_2.18.0              
##  [49] msigdbr_7.5.1                 annotate_1.80.0              
##  [51] KEGGREST_1.42.0               magick_2.8.1                 
##  [53] pillar_1.9.0                  knitr_1.44                   
##  [55] ComplexHeatmap_2.18.0         rjson_0.2.21                 
##  [57] boot_1.3-28.1                 estimability_1.4.1           
##  [59] corpcor_1.6.10                future.apply_1.11.0          
##  [61] codetools_0.2-19              glue_1.6.2                   
##  [63] data.table_1.14.8             vctrs_0.6.4                  
##  [65] png_0.1-8                     Rdpack_2.5                   
##  [67] gtable_0.3.4                  assertthat_0.2.1             
##  [69] cachem_1.0.8                  xfun_0.40                    
##  [71] mime_0.12                     rbibutils_2.2.15             
##  [73] S4Arrays_1.2.0                Rfast_2.0.8                  
##  [75] coda_0.19-4                   survival_3.5-7               
##  [77] iterators_1.0.14              statmod_1.5.0                
##  [79] ellipsis_0.3.2                interactiveDisplayBase_1.40.0
##  [81] TH.data_1.1-2                 nlme_3.1-163                 
##  [83] pbkrtest_0.5.2                bit64_4.0.5                  
##  [85] filelock_1.0.2                progress_1.2.2               
##  [87] EnvStats_2.8.1                bslib_0.5.1                  
##  [89] TMB_1.9.6                     irlba_2.3.5.1                
##  [91] vipor_0.4.5                   KernSmooth_2.23-22           
##  [93] colorspace_2.1-0              rmeta_3.0                    
##  [95] DBI_1.1.3                     DESeq2_1.42.0                
##  [97] tidyselect_1.2.0              emmeans_1.8.9                
##  [99] curl_5.1.0                    bit_4.0.5                    
## [101] compiler_4.3.1                graph_1.80.0                 
## [103] BiocNeighbors_1.20.0          DelayedArray_0.28.0          
## [105] bookdown_0.36                 scales_1.2.1                 
## [107] caTools_1.18.2                remaCor_0.0.16               
## [109] rappdirs_0.3.3                stringr_1.5.0                
## [111] digest_0.6.33                 minqa_1.2.6                  
## [113] rmarkdown_2.25                aod_1.3.2                    
## [115] XVector_0.42.0                RhpcBLASctl_0.23-42          
## [117] htmltools_0.5.6.1             pkgconfig_2.0.3              
## [119] lme4_1.1-34                   sparseMatrixStats_1.14.0     
## [121] mashr_0.2.79                  fastmap_1.1.1                
## [123] rlang_1.1.1                   GlobalOptions_0.1.2          
## [125] shiny_1.7.5.1                 DelayedMatrixStats_1.24.0    
## [127] farver_2.1.1                  jquerylib_0.1.4              
## [129] zoo_1.8-12                    jsonlite_1.8.7               
## [131] BiocSingular_1.18.0           RCurl_1.98-1.12              
## [133] magrittr_2.0.3                GenomeInfoDbData_1.2.11      
## [135] munsell_0.5.0                 Rcpp_1.0.11                  
## [137] babelgene_22.9                viridis_0.6.4                
## [139] EnrichmentBrowser_2.32.0      RcppZiggurat_0.1.6           
## [141] stringi_1.7.12                zlibbioc_1.48.0              
## [143] MASS_7.3-60                   plyr_1.8.9                   
## [145] listenv_0.9.0                 parallel_4.3.1               
## [147] ggrepel_0.9.4                 Biostrings_2.70.0            
## [149] splines_4.3.1                 hms_1.1.3                    
## [151] circlize_0.4.15               locfit_1.5-9.8               
## [153] reshape2_1.4.4                ScaledMatrix_1.10.0          
## [155] BiocVersion_3.18.0            XML_3.99-0.14                
## [157] evaluate_0.22                 BiocManager_1.30.22          
## [159] httpuv_1.6.12                 nloptr_2.0.3                 
## [161] foreach_1.5.2                 tidyr_1.3.0                  
## [163] purrr_1.0.2                   future_1.33.0                
## [165] clue_0.3-65                   scattermore_1.2              
## [167] ashr_2.2-63                   rsvd_1.0.5                   
## [169] broom_1.0.5                   xtable_1.8-4                 
## [171] fANCOVA_0.6-1                 later_1.3.1                  
## [173] viridisLite_0.4.2             truncnorm_1.0-9              
## [175] tibble_3.2.1                  lmerTest_3.1-3               
## [177] glmmTMB_1.1.8                 memoise_2.0.1                
## [179] beeswarm_0.4.0                AnnotationDbi_1.64.0         
## [181] cluster_2.1.4                 globals_0.16.2               
## [183] GSEABase_1.64.0