--- title: "Spatial Mixed-Effects Modelling with spicy" date: "`r BiocStyle::doc_date()`" params: test: FALSE author: - name: Nicolas Canete affiliation: - &WIMR Westmead Institute for Medical Research, University of Sydney, Australia email: nicolas.canete@sydney.edu.au - name: Ellis Patrick affiliation: - &WIMR Westmead Institute for Medical Research, University of Sydney, Australia - School of Mathematics and Statistics, University of Sydney, Australia package: "`r BiocStyle::pkg_ver('spicyR')`" vignette: > %\VignetteIndexEntry{"Introduction to spicy"} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} output: BiocStyle::html_document --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>", warning = FALSE ) library(BiocStyle) ``` ```{r warning=FALSE, message=FALSE} # load required packages library(spicyR) library(ggplot2) library(SingleCellExperiment) ``` # Installation ```{r, eval = FALSE} if (!require("BiocManager")) { install.packages("BiocManager") } BiocManager::install("spicyR") ``` # 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. # 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. ```{r} data("diabetesData_SCE") diabetesData_SCE ``` 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). # Mixed Effects Modelling To investigate changes in localisation between two different cell types, we measure the level of localisation between two cell types by modelling with the L-function. Specifically, the mean difference between the obtained function and the theoretical function is used as a measure for the level of localisation. 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. ## Testing for change in localisation for a specific pair of cells Firstly, we can test whether one cell type tends to be more localised with 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. ```{r message=FALSE} spicyTestPair <- spicy( diabetesData_SCE, condition = "stage", subject = "case", from = "beta", to = "delta" ) topPairs(spicyTestPair) ``` 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 near beta cells in the onset diabetes group compared to the non-diabetic control. ## Test for change in localisation 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()`. ```{r message=FALSE} spicyTest <- spicy( diabetesData_SCE, condition = "stage", subject = "case" ) spicyTest topPairs(spicyTest) ``` 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 `signifPlot()` function by providing it the `spicy` object obtained. ```{r} signifPlot( spicyTest, breaks = c(-3, 3, 1), marksToPlot = c( "alpha", "beta", "gamma", "delta", "B", "naiveTc", "Th", "Tc", "neutrophil", "macrophage" ) ) ``` # sessionInfo() ```{r} sessionInfo() ```