Modeling continuous cell-level covariates

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

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

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.119   -0.126  
##  2 B cells ctrl1015    4.00 -0.0690  -0.0141 
##  3 B cells ctrl1016    4    -0.154   -0.00902
##  4 B cells ctrl1039    4.04  0.00514 -0.104  
##  5 B cells ctrl107     4     0.0544   0.135  
##  6 B cells ctrl1244    4     0.0638  -0.0771 
##  7 B cells ctrl1256    4.01  0.108   -0.0260 
##  8 B cells ctrl1488    4.02  0.0333   0.112  
##  9 B cells stim101     4.09  0.124    0.00736
## 10 B cells stim1015    4.06  0.0168   0.0751 
## # ℹ 118 more rows

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

Session Info

## R version 4.4.2 (2024-10-31)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.1 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so;  LAPACK version 3.12.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        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: Etc/UTC
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
##  [1] muscData_1.20.0             scater_1.35.0              
##  [3] scuttle_1.17.0              ExperimentHub_2.15.0       
##  [5] AnnotationHub_3.15.0        BiocFileCache_2.15.0       
##  [7] dbplyr_2.5.0                muscat_1.21.0              
##  [9] dreamlet_1.5.0              SingleCellExperiment_1.29.1
## [11] SummarizedExperiment_1.37.0 Biobase_2.67.0             
## [13] GenomicRanges_1.59.0        GenomeInfoDb_1.43.1        
## [15] IRanges_2.41.1              S4Vectors_0.45.2           
## [17] BiocGenerics_0.53.3         generics_0.1.3             
## [19] MatrixGenerics_1.19.0       matrixStats_1.4.1          
## [21] variancePartition_1.37.1    BiocParallel_1.41.0        
## [23] limma_3.63.2                ggplot2_3.5.1              
## [25] BiocStyle_2.35.0           
## 
## loaded via a namespace (and not attached):
##   [1] bitops_1.0-9              httr_1.4.7               
##   [3] RColorBrewer_1.1-3        doParallel_1.0.17        
##   [5] Rgraphviz_2.51.0          numDeriv_2016.8-1.1      
##   [7] tools_4.4.2               sctransform_0.4.1        
##   [9] backports_1.5.0           utf8_1.2.4               
##  [11] R6_2.5.1                  metafor_4.6-0            
##  [13] mgcv_1.9-1                GetoptLong_1.0.5         
##  [15] withr_3.0.2               prettyunits_1.2.0        
##  [17] gridExtra_2.3             cli_3.6.3                
##  [19] labeling_0.4.3            sass_0.4.9               
##  [21] KEGGgraph_1.67.0          SQUAREM_2021.1           
##  [23] mvtnorm_1.3-2             blme_1.0-6               
##  [25] mixsqp_0.3-54             zenith_1.9.0             
##  [27] parallelly_1.39.0         invgamma_1.1             
##  [29] RSQLite_2.3.8             shape_1.4.6.1            
##  [31] gtools_3.9.5              dplyr_1.1.4              
##  [33] Matrix_1.7-1              metadat_1.2-0            
##  [35] ggbeeswarm_0.7.2          fansi_1.0.6              
##  [37] abind_1.4-8               lifecycle_1.0.4          
##  [39] yaml_2.3.10               edgeR_4.5.0              
##  [41] mathjaxr_1.6-0            gplots_3.2.0             
##  [43] SparseArray_1.7.2         grid_4.4.2               
##  [45] blob_1.2.4                crayon_1.5.3             
##  [47] lattice_0.22-6            beachmat_2.23.1          
##  [49] msigdbr_7.5.1             annotate_1.85.0          
##  [51] KEGGREST_1.47.0           sys_3.4.3                
##  [53] maketools_1.3.1           pillar_1.9.0             
##  [55] knitr_1.49                ComplexHeatmap_2.23.0    
##  [57] rjson_0.2.23              boot_1.3-31              
##  [59] corpcor_1.6.10            future.apply_1.11.3      
##  [61] codetools_0.2-20          glue_1.8.0               
##  [63] data.table_1.16.2         vctrs_0.6.5              
##  [65] png_0.1-8                 Rdpack_2.6.2             
##  [67] gtable_0.3.6              assertthat_0.2.1         
##  [69] cachem_1.1.0              xfun_0.49                
##  [71] mime_0.12                 rbibutils_2.3            
##  [73] S4Arrays_1.7.1            Rfast_2.1.0              
##  [75] reformulas_0.4.0          iterators_1.0.14         
##  [77] statmod_1.5.0             nlme_3.1-166             
##  [79] pbkrtest_0.5.3            bit64_4.5.2              
##  [81] filelock_1.0.3            progress_1.2.3           
##  [83] EnvStats_3.0.0            bslib_0.8.0              
##  [85] TMB_1.9.15                irlba_2.3.5.1            
##  [87] vipor_0.4.7               KernSmooth_2.23-24       
##  [89] colorspace_2.1-1          rmeta_3.0                
##  [91] DBI_1.2.3                 DESeq2_1.47.1            
##  [93] tidyselect_1.2.1          curl_6.0.1               
##  [95] bit_4.5.0                 compiler_4.4.2           
##  [97] graph_1.85.0              BiocNeighbors_2.1.0      
##  [99] DelayedArray_0.33.2       scales_1.3.0             
## [101] caTools_1.18.3            remaCor_0.0.18           
## [103] rappdirs_0.3.3            stringr_1.5.1            
## [105] digest_0.6.37             minqa_1.2.8              
## [107] rmarkdown_2.29            aod_1.3.3                
## [109] XVector_0.47.0            RhpcBLASctl_0.23-42      
## [111] htmltools_0.5.8.1         pkgconfig_2.0.3          
## [113] lme4_1.1-35.5             sparseMatrixStats_1.19.0 
## [115] mashr_0.2.79              fastmap_1.2.0            
## [117] rlang_1.1.4               GlobalOptions_0.1.2      
## [119] UCSC.utils_1.3.0          DelayedMatrixStats_1.29.0
## [121] farver_2.1.2              jquerylib_0.1.4          
## [123] jsonlite_1.8.9            BiocSingular_1.23.0      
## [125] RCurl_1.98-1.16           magrittr_2.0.3           
## [127] GenomeInfoDbData_1.2.13   munsell_0.5.1            
## [129] Rcpp_1.0.13-1             babelgene_22.9           
## [131] viridis_0.6.5             EnrichmentBrowser_2.37.0 
## [133] RcppZiggurat_0.1.6        stringi_1.8.4            
## [135] zlibbioc_1.52.0           MASS_7.3-61              
## [137] plyr_1.8.9                parallel_4.4.2           
## [139] listenv_0.9.1             ggrepel_0.9.6            
## [141] Biostrings_2.75.1         splines_4.4.2            
## [143] hms_1.1.3                 circlize_0.4.16          
## [145] locfit_1.5-9.10           buildtools_1.0.0         
## [147] reshape2_1.4.4            ScaledMatrix_1.15.0      
## [149] BiocVersion_3.21.1        XML_3.99-0.17            
## [151] evaluate_1.0.1            RcppParallel_5.1.9       
## [153] BiocManager_1.30.25       nloptr_2.1.1             
## [155] foreach_1.5.2             tidyr_1.3.1              
## [157] purrr_1.0.2               future_1.34.0            
## [159] clue_0.3-66               scattermore_1.2          
## [161] ashr_2.2-63               rsvd_1.0.5               
## [163] broom_1.0.7               xtable_1.8-4             
## [165] fANCOVA_0.6-1             viridisLite_0.4.2        
## [167] truncnorm_1.0-9           tibble_3.2.1             
## [169] lmerTest_3.1-3            glmmTMB_1.1.10           
## [171] memoise_2.0.1             beeswarm_0.4.0           
## [173] AnnotationDbi_1.69.0      cluster_2.1.6            
## [175] globals_0.16.3            GSEABase_1.69.0