Contents

# load required packages
library(spicyR)
library(ggplot2)
library(SingleCellExperiment)

1 Installation

if (!require("BiocManager"))
    install.packages("BiocManager")
BiocManager::install("spicyR")

2 Overview

This guide will provide a step-by-step guide on how mixed effects models can be applied to multiple segmented and labelled images to identify how the localisation of different cell types can change across different conditions. Here, the subject is modelled as a random effect, and the different conditions are modelled as a fixed effect.

3 Example data

Here, we use a subset of the Damond et al 2019 imaging mass cytometry dataset. We will compare the spatial distributions of cells in the pancreatic islets of individuals with early onset diabetes and healthy controls.

diabetesData_SCE is a SingleCellExperiment object containing single-cell data of 160 images from 8 subjects, with 20 images per subjects.

cellSummary() returns a DataFrame object providing the location (x and y) and cell type (cellType) of each cell and the image it belongs to (imageID).

imagePheno() returns a tibble object providing the corresponding subject (subject) and condition (condition) for each image.

data("diabetesData_SCE")
diabetesData_SCE
#> class: SingleCellExperiment 
#> dim: 0 253777 
#> metadata(0):
#> assays(0):
#> rownames: NULL
#> rowData names(0):
#> colnames: NULL
#> colData names(11): imageID cellID ... group stage
#> reducedDimNames(0):
#> mainExpName: NULL
#> altExpNames(0):

In this data set, cell types include immune cell types (B cells, naive T cells, T Helper cells, T cytotoxic cells, neutrophils, macrophages) and pancreatic islet cells (alpha, beta, gamma, delta).

4 Mixed Effects Modelling

To investigate changes in colocalisation between two different cell types, we measure the level of colocalisation between two cell types by modelling with the Lcross() function in the spatstat.explore package. Specifically, the mean difference between the obtained function and the theoretical function is used as a measure for the level of colocalisation. Differences of this statistic between two conditions is modelled using a weighted mixed effects model, with condition as the fixed effect and subject as the random effect. spicyTestBootstrap ## Testing for change in colocalisation for a specific pair of cells

Firstly, we can see whether one cell type tends to be around another cell type in one condition compared to the other. This can be done using the spicy() function, where we include condition, and subject. In this example, we want to see whether or not Delta cells (to) tend to be found around Beta cells (from) in onset diabetes images compared to non-diabetic images.

spicyTestPair <- spicy(diabetesData,
                       condition = "stage",
                       subject = "case",
                       from = "beta",
                       to = "delta")

topPairs(spicyTestPair)
#>             intercept coefficient     p.value  adj.pvalue from    to
#> beta__delta   179.729   -58.24478 0.000109702 0.000109702 beta delta

We obtain a spicy object which details the results of the mixed effects modelling performed. As the coefficient in spicyTest is positive, we find that Th cells cells are more likely to be found around beta cells in the onset diabetes group compared to the non-diabetic control.

4.1 Test for change in colocalisation for all pairwise cell combinations

Here, we can perform what we did above for all pairwise combinations of cell types by excluding the from and to parameters from spicy().

spicyTest <- spicy(diabetesData_SCE,
                   condition = "stage",
                   subject = "case")
data("spicyTest")
spicyTest
#> 
#> Number of cell type pairs: 256
#> Number of differentially localised cell type pairs:
#>         conditionOnset conditionLong-duration 
#>                      7                      7
topPairs(spicyTest)  
#>                   intercept coefficient      p.value adj.pvalue       from
#> Th_Th             -8.550349   55.505421 0.0002069040 0.03207907         Th
#> beta_ductal      -19.461966   10.416870 0.0005559049 0.03207907       beta
#> ductal_beta      -19.407845   10.382899 0.0005695262 0.03207907     ductal
#> delta_beta       101.111037  -30.418789 0.0006118914 0.03207907      delta
#> beta_delta       101.052488  -30.370186 0.0006265444 0.03207907       beta
#> unknown_beta      -6.704261    8.546938 0.0010115597 0.03891502    unknown
#> beta_unknown      -6.686305    8.505666 0.0010640825 0.03891502       beta
#> gamma_neutrophil  -3.234976  -19.102452 0.0229478395 0.39078920      gamma
#> neutrophil_gamma  -3.228265  -19.065803 0.0230795918 0.39078920 neutrophil
#> ductal_Th         -1.454500  -10.508455 0.0268678016 0.39078920     ductal
#>                          to
#> Th_Th                    Th
#> beta_ductal          ductal
#> ductal_beta            beta
#> delta_beta             beta
#> beta_delta            delta
#> unknown_beta           beta
#> beta_unknown        unknown
#> gamma_neutrophil neutrophil
#> neutrophil_gamma      gamma
#> ductal_Th                Th

Again, we obtain a spicy object which outlines the result of the mixed effects models performed for each pairwise combination if cell types.

We can represent this as a heatmap using the spatialMEMMultiPlot() function by providing it the spicy object obtained.

signifPlot(spicyTest, 
           breaks=c(-3, 3, 1),
           marksToPlot = c("alpha", "beta", "gamma", "delta", 
                           "B", "naiveTc", "Th", "Tc", "neutrophil", "macrophage"))

5 sessionInfo()

sessionInfo()
#> R version 4.2.2 (2022-10-31)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 20.04.5 LTS
#> 
#> Matrix products: default
#> BLAS:   /home/biocbuild/bbs-3.16-bioc/R/lib/libRblas.so
#> LAPACK: /home/biocbuild/bbs-3.16-bioc/R/lib/libRlapack.so
#> 
#> 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       
#> 
#> attached base packages:
#> [1] stats4    stats     graphics  grDevices utils     datasets  methods  
#> [8] base     
#> 
#> other attached packages:
#>  [1] SingleCellExperiment_1.20.0 SummarizedExperiment_1.28.0
#>  [3] Biobase_2.58.0              GenomicRanges_1.50.1       
#>  [5] GenomeInfoDb_1.34.3         IRanges_2.32.0             
#>  [7] S4Vectors_0.36.0            BiocGenerics_0.44.0        
#>  [9] MatrixGenerics_1.10.0       matrixStats_0.63.0         
#> [11] ggplot2_3.4.0               spicyR_1.10.5              
#> [13] BiocStyle_2.26.0           
#> 
#> loaded via a namespace (and not attached):
#>   [1] SpatialExperiment_1.8.0   minqa_1.2.5              
#>   [3] colorspace_2.0-3          rjson_0.2.21             
#>   [5] deldir_1.0-6              ellipsis_0.3.2           
#>   [7] scuttle_1.8.0             XVector_0.38.0           
#>   [9] spatstat.data_3.0-0       farver_2.1.1             
#>  [11] fansi_1.0.3               codetools_0.2-18         
#>  [13] splines_4.2.2             R.methodsS3_1.8.2        
#>  [15] sparseMatrixStats_1.10.0  cachem_1.0.6             
#>  [17] knitr_1.41                polyclip_1.10-4          
#>  [19] jsonlite_1.8.3            nloptr_2.0.3             
#>  [21] R.oo_1.25.0               pheatmap_1.0.12          
#>  [23] ggforce_0.4.1             HDF5Array_1.26.0         
#>  [25] spatstat.sparse_3.0-0     BiocManager_1.30.19      
#>  [27] compiler_4.2.2            dqrng_0.3.0              
#>  [29] assertthat_0.2.1          Matrix_1.5-3             
#>  [31] fastmap_1.1.0             limma_3.54.0             
#>  [33] cli_3.4.1                 tweenr_2.0.2             
#>  [35] htmltools_0.5.3           tools_4.2.2              
#>  [37] lmerTest_3.1-3            gtable_0.3.1             
#>  [39] glue_1.6.2                GenomeInfoDbData_1.2.9   
#>  [41] dplyr_1.0.10              Rcpp_1.0.9               
#>  [43] jquerylib_0.1.4           vctrs_0.5.1              
#>  [45] rhdf5filters_1.10.0       spatstat.explore_3.0-5   
#>  [47] nlme_3.1-160              DelayedMatrixStats_1.20.0
#>  [49] spatstat.random_3.0-1     xfun_0.35                
#>  [51] stringr_1.4.1             beachmat_2.14.0          
#>  [53] lme4_1.1-31               lifecycle_1.0.3          
#>  [55] goftest_1.2-3             scam_1.2-13              
#>  [57] edgeR_3.40.0              zlibbioc_1.44.0          
#>  [59] MASS_7.3-58.1             scales_1.2.1             
#>  [61] spatstat.utils_3.0-1      parallel_4.2.2           
#>  [63] rhdf5_2.42.0              RColorBrewer_1.1-3       
#>  [65] yaml_2.3.6                sass_0.4.2               
#>  [67] stringi_1.7.8             highr_0.9                
#>  [69] boot_1.3-28               BiocParallel_1.32.1      
#>  [71] rlang_1.0.6               pkgconfig_2.0.3          
#>  [73] bitops_1.0-7              evaluate_0.18            
#>  [75] lattice_0.20-45           purrr_0.3.5              
#>  [77] tensor_1.5                Rhdf5lib_1.20.0          
#>  [79] labeling_0.4.2            tidyselect_1.2.0         
#>  [81] magrittr_2.0.3            bookdown_0.30            
#>  [83] R6_2.5.1                  magick_2.7.3             
#>  [85] generics_0.1.3            DelayedArray_0.24.0      
#>  [87] DBI_1.1.3                 pillar_1.8.1             
#>  [89] withr_2.5.0               mgcv_1.8-41              
#>  [91] abind_1.4-5               RCurl_1.98-1.9           
#>  [93] tibble_3.1.8              DropletUtils_1.18.0      
#>  [95] utf8_1.2.2                spatstat.geom_3.0-3      
#>  [97] rmarkdown_2.18            locfit_1.5-9.6           
#>  [99] grid_4.2.2                data.table_1.14.6        
#> [101] digest_0.6.30             tidyr_1.2.1              
#> [103] numDeriv_2016.8-1.1       R.utils_2.12.2           
#> [105] munsell_0.5.0             concaveman_1.1.0         
#> [107] bslib_0.4.1