Overview of the sccomp package

Stefano Mangiola

2023-10-24

Lifecycle:maturing R build status

Sccomp is a generalised method for differential composition and variability analyses.

Characteristics

Installation

Bioconductor

if (!requireNamespace("BiocManager")) install.packages("BiocManager")
BiocManager::install("sccomp")

Github

devtools::install_github("stemangiola/sccomp")

Analysis

sccomp can model changes in composition and variability. By default, the formula for variability is either ~1, which assumes that the cell-group variability is independent of any covariate or ~ factor_of_interest, which assumes that the model is dependent on the factor of interest only. The variability model must be a subset of the model for composition.

Binary factor

From Seurat, SingleCellExperiment, metadata objects

single_cell_object |>
  sccomp_glm( 
    formula_composition = ~ type, 
    .sample =  sample, 
    .cell_group = cell_group, 
    bimodal_mean_variability_association = TRUE,
    cores = 1 
  )

From counts

counts_obj |>
  sccomp_glm( 
    formula_composition = ~ type, 
    .sample = sample,
    .cell_group = cell_group,
    .count = count, 
    bimodal_mean_variability_association = TRUE,
    cores = 1 
  )
## # A tibble: 72 × 18
##    cell_group parameter  factor c_lower c_effect c_upper   c_pH0   c_FDR c_n_eff
##    <chr>      <chr>      <chr>    <dbl>    <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
##  1 B1         (Intercep… <NA>    0.895     1.12   1.33   0       0         4908.
##  2 B1         typecancer type   -1.15     -0.765 -0.360  3.  e-3 1.00e-3   3897.
##  3 B2         (Intercep… <NA>    0.419     0.713  0.978  5.00e-4 2.63e-5   5210.
##  4 B2         typecancer type   -1.20     -0.725 -0.252  1.60e-2 4.55e-3   3129.
##  5 B3         (Intercep… <NA>   -0.650    -0.384 -0.105  9.28e-2 7.57e-3   4583.
##  6 B3         typecancer type   -0.737    -0.325  0.0823 2.63e-1 6.65e-2   3997.
##  7 BM         (Intercep… <NA>   -1.31     -1.02  -0.760  0       0         5325.
##  8 BM         typecancer type   -0.719    -0.319  0.0655 2.73e-1 7.68e-2   4994.
##  9 CD4 1      (Intercep… <NA>    0.0915    0.308  0.500  1.48e-1 2.20e-2   3994.
## 10 CD4 1      typecancer type   -0.102     0.178  0.459  5.60e-1 1.42e-1   3813.
## # ℹ 62 more rows
## # ℹ 9 more variables: c_R_k_hat <dbl>, v_lower <dbl>, v_effect <dbl>,
## #   v_upper <dbl>, v_pH0 <dbl>, v_FDR <dbl>, v_n_eff <dbl>, v_R_k_hat <dbl>,
## #   count_data <list>

Of the output table, the estimate columns start with the prefix c_ indicate composition, or with v_ indicate variability (when formula_variability is set).

Contrasts

seurat_obj |>
  sccomp_glm( 
    formula_composition = ~ 0 + type, 
    contrasts =  c("typecancer - typehealthy", "typehealthy - typecancer"),
    .sample = sample,
    .cell_group = cell_group, 
    bimodal_mean_variability_association = TRUE,
    cores = 1 
  )
## # A tibble: 60 × 18
##    cell_group  parameter factor c_lower c_effect c_upper   c_pH0   c_FDR c_n_eff
##    <chr>       <chr>     <chr>    <dbl>    <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
##  1 B immature  typecanc… type    -1.90    -1.42   -0.951 0       0            NA
##  2 B immature  typeheal… type     0.951    1.42    1.90  0       0            NA
##  3 B mem       typecanc… type    -2.35    -1.74   -1.11  0       0            NA
##  4 B mem       typeheal… type     1.11     1.74    2.35  0       0            NA
##  5 CD4 cm S10… typecanc… type    -1.23    -0.861  -0.517 0       0            NA
##  6 CD4 cm S10… typeheal… type     0.517    0.861   1.23  0       0            NA
##  7 CD4 cm hig… typecanc… type     0.915    1.81    2.86  5.00e-4 1.25e-4      NA
##  8 CD4 cm hig… typeheal… type    -2.86    -1.81   -0.915 5.00e-4 1.25e-4      NA
##  9 CD4 cm rib… typecanc… type     0.361    1.00    1.70  8.  e-3 2.05e-3      NA
## 10 CD4 cm rib… typeheal… type    -1.70    -1.00   -0.361 8.  e-3 2.05e-3      NA
## # ℹ 50 more rows
## # ℹ 9 more variables: c_R_k_hat <dbl>, v_lower <dbl>, v_effect <dbl>,
## #   v_upper <dbl>, v_pH0 <dbl>, v_FDR <dbl>, v_n_eff <dbl>, v_R_k_hat <dbl>,
## #   count_data <list>

Categorical factor (e.g. Bayesian ANOVA)

This is achieved through model comparison with loo. In the following example, the model with association with factors better fits the data compared to the baseline model with no factor association. For comparisons check_outliers must be set to FALSE as the leave-one-out must work with the same amount of data, while outlier elimination does not guarantee it.

If elpd_diff is away from zero of > 5 se_diff difference of 5, we are confident that a model is better than the other reference. In this case, -79.9 / 11.5 = -6.9, therefore we can conclude that model one, the one with factor association, is better than model two.

library(loo)

# Fit first model
model_with_factor_association = 
  seurat_obj |>
  sccomp_glm( 
    formula_composition = ~ type, 
    .sample =  sample, 
    .cell_group = cell_group, 
    check_outliers = FALSE, 
    bimodal_mean_variability_association = TRUE,
    cores = 1, 
    enable_loo = TRUE
  )

# Fit second model
model_without_association = 
  seurat_obj |>
  sccomp_glm( 
    formula_composition = ~ 1, 
    .sample =  sample, 
    .cell_group = cell_group, 
    check_outliers = FALSE, 
    bimodal_mean_variability_association = TRUE,
    cores = 1 , 
    enable_loo = TRUE
  )

# Compare models
loo_compare(
  model_with_factor_association |> attr("fit") |> loo(),
  model_without_association |> attr("fit") |> loo()
)

Differential variability, binary factor

We can model the cell-group variability also dependent on the type, and so test differences in variability

res = 
  seurat_obj |>
  sccomp_glm( 
    formula_composition = ~ type, 
    formula_variability = ~ type,
    .sample = sample,
    .cell_group = cell_group,
    bimodal_mean_variability_association = TRUE,
    cores = 1 
  )

res
## # A tibble: 60 × 18
##    cell_group  parameter factor c_lower c_effect c_upper   c_pH0   c_FDR c_n_eff
##    <chr>       <chr>     <chr>    <dbl>    <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
##  1 B immature  (Interce… <NA>     0.561    0.932  1.31   0       0         5101.
##  2 B immature  typeheal… type     0.825    1.36   1.90   0       0         4132.
##  3 B mem       (Interce… <NA>    -1.31    -0.753 -0.136  0.0373  0.00261   5083.
##  4 B mem       typeheal… type     1.05     1.81   2.55   0       0         4768.
##  5 CD4 cm S10… (Interce… <NA>     1.73     1.98   2.23   0       0         4767.
##  6 CD4 cm S10… typeheal… type     0.321    0.707  1.10   0.00600 0.00150   4051.
##  7 CD4 cm hig… (Interce… <NA>    -0.882   -0.406  0.0834 0.192   0.0277    5014.
##  8 CD4 cm hig… typeheal… type    -3.18    -1.45   1.06   0.15    0.0515    3209.
##  9 CD4 cm rib… (Interce… <NA>     0.145    0.477  0.835  0.0513  0.00710   3817.
## 10 CD4 cm rib… typeheal… type    -1.92    -1.08  -0.0551 0.0425  0.00986   4056.
## # ℹ 50 more rows
## # ℹ 9 more variables: c_R_k_hat <dbl>, v_lower <dbl>, v_effect <dbl>,
## #   v_upper <dbl>, v_pH0 <dbl>, v_FDR <dbl>, v_n_eff <dbl>, v_R_k_hat <dbl>,
## #   count_data <list>

Suggested settings

For single-cell RNA sequencing

We recommend setting bimodal_mean_variability_association = TRUE. The bimodality of the mean-variability association can be confirmed from the plots$credible_intervals_2D (see below).

For CyTOF and microbiome data

We recommend setting bimodal_mean_variability_association = FALSE (Default).

Visualisation

Summary plots

plots = plot_summary(res) 
## Joining with `by = join_by(cell_group, sample)`
## Joining with `by = join_by(cell_group, type)`
## Warning: Expected 2 pieces. Additional pieces discarded in 4 rows [6, 7, 13,
## 14].

A plot of group proportion, faceted by groups. The blue boxplots represent the posterior predictive check. If the model is likely to be descriptively adequate to the data, the blue box plot should roughly overlay with the black box plot, which represents the observed data. The outliers are coloured in red. A box plot will be returned for every (discrete) covariate present in formula_composition. The colour coding represents the significant associations for composition and/or variability.

plots$boxplot
## [[1]]

plot of chunk unnamed-chunk-10

A plot of estimates of differential composition (c_) on the x-axis and differential variability (v_) on the y-axis. The error bars represent 95% credible intervals. The dashed lines represent the minimal effect that the hypothesis test is based on. An effect is labelled as significant if bigger than the minimal effect according to the 95% credible interval. Facets represent the covariates in the model.

plots$credible_intervals_1D

plot of chunk unnamed-chunk-11

Visualisation of the MCMC chains from the posterior distribution

It is possible to directly evaluate the posterior distribution. In this example, we plot the Monte Carlo chain for the slope parameter of the first cell type. We can see that it has converged and is negative with probability 1.

res %>% attr("fit") %>% rstan::traceplot("beta[2,1]")

plot of chunk unnamed-chunk-12

Plot 1D significance plot

plots = plot_summary(res)
## Joining with `by = join_by(cell_group, sample)`
## Joining with `by = join_by(cell_group, type)`
## Warning: Expected 2 pieces. Additional pieces discarded in 4 rows [6, 7, 13,
## 14].
plots$credible_intervals_1D

plot of chunk unnamed-chunk-13

Plot 2D significance plot. Data points are cell groups. Error bars are the 95% credible interval. The dashed lines represent the default threshold fold change for which the probabilities (c_pH0, v_pH0) are calculated. pH0 of 0 represent the rejection of the null hypothesis that no effect is observed.

This plot is provided only if differential variability has been tested. The differential variability estimates are reliable only if the linear association between mean and variability for (intercept) (left-hand side facet) is satisfied. A scatterplot (besides the Intercept) is provided for each category of interest. The for each category of interest, the composition and variability effects should be generally uncorrelated.

plots$credible_intervals_2D

plot of chunk unnamed-chunk-14

sessionInfo()
## 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] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] rstan_2.32.3        StanHeaders_2.26.28 tidyr_1.3.0        
## [4] forcats_1.0.0       ggplot2_3.4.4       sccomp_1.6.0       
## [7] dplyr_1.1.3        
## 
## loaded via a namespace (and not attached):
##  [1] tidyselect_1.2.0            farver_2.1.1               
##  [3] loo_2.6.0                   bitops_1.0-7               
##  [5] SingleCellExperiment_1.24.0 RCurl_1.98-1.12            
##  [7] digest_0.6.33               lifecycle_1.0.3            
##  [9] SeuratObject_4.1.4          processx_3.8.2             
## [11] magrittr_2.0.3              compiler_4.3.1             
## [13] rlang_1.1.1                 tools_4.3.1                
## [15] utf8_1.2.4                  knitr_1.44                 
## [17] labeling_0.4.3              prettyunits_1.2.0          
## [19] S4Arrays_1.2.0              curl_5.1.0                 
## [21] sp_2.1-1                    pkgbuild_1.4.2             
## [23] DelayedArray_0.28.0         RColorBrewer_1.1-3         
## [25] abind_1.4-5                 withr_2.5.1                
## [27] purrr_1.0.2                 BiocGenerics_0.48.0        
## [29] grid_4.3.1                  stats4_4.3.1               
## [31] fansi_1.0.5                 colorspace_2.1-0           
## [33] future_1.33.0               inline_0.3.19              
## [35] progressr_0.14.0            globals_0.16.2             
## [37] scales_1.2.1                SummarizedExperiment_1.32.0
## [39] cli_3.6.1                   crayon_1.5.2               
## [41] generics_0.1.3              RcppParallel_5.1.7         
## [43] future.apply_1.11.0         tzdb_0.4.0                 
## [45] stringr_1.5.0               zlibbioc_1.48.0            
## [47] parallel_4.3.1              XVector_0.42.0             
## [49] matrixStats_1.0.0           vctrs_0.6.4                
## [51] V8_4.4.0                    boot_1.3-28.1              
## [53] Matrix_1.6-1.1              jsonlite_1.8.7             
## [55] callr_3.7.3                 IRanges_2.36.0             
## [57] hms_1.1.3                   patchwork_1.1.3            
## [59] S4Vectors_0.40.0            ggrepel_0.9.4              
## [61] listenv_0.9.0               glue_1.6.2                 
## [63] parallelly_1.36.0           codetools_0.2-19           
## [65] ps_1.7.5                    stringi_1.7.12             
## [67] gtable_0.3.4                GenomeInfoDb_1.38.0        
## [69] QuickJSR_1.0.7              GenomicRanges_1.54.0       
## [71] munsell_0.5.0               tibble_3.2.1               
## [73] pillar_1.9.0                GenomeInfoDbData_1.2.11    
## [75] R6_2.5.1                    evaluate_0.22              
## [77] lattice_0.22-5              Biobase_2.62.0             
## [79] readr_2.1.4                 rstantools_2.3.1.1         
## [81] Rcpp_1.0.11                 gridExtra_2.3              
## [83] SparseArray_1.2.0           xfun_0.40                  
## [85] MatrixGenerics_1.14.0       pkgconfig_2.0.3