Contents

1 Installation

if (!require("BiocManager")) {
  install.packages("BiocManager")
}
BiocManager::install("spicyR")
# load required packages
library(spicyR)
library(ggplot2)
library(SingleCellExperiment)
library(SpatialExperiment)
library(imcRtools)

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

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

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

spicyTestPair <- spicy(
  diabetesData_SCE,
  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 delta cells cells are significantly less likely to be found near beta cells in the onset diabetes group compared to the non-diabetic control.

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

spicyTest <- spicy(
  diabetesData_SCE,
  condition = "stage",
  subject = "case"
)

spicyTest
#>         conditionOnset conditionLong-duration 
#>                      0                     15
topPairs(spicyTest)
#>                          intercept coefficient      p.value adj.pvalue
#> beta__delta           1.815458e+02  -48.735693 0.0005033247 0.07169649
#> delta__beta           1.817943e+02  -48.166076 0.0005601288 0.07169649
#> B__unknown            4.327556e-15   11.770938 0.0052338392 0.42051606
#> delta__delta          2.089550e+02  -52.061196 0.0125129422 0.42051606
#> unknown__macrophage   1.007337e+01  -15.826919 0.0207410908 0.42051606
#> unknown__B            0.000000e+00   12.142848 0.0225855404 0.42051606
#> macrophage__unknown   1.004424e+01  -14.471666 0.0244668075 0.42051606
#> B__Th                 3.142958e-15   26.847934 0.0245039854 0.42051606
#> otherimmune__naiveTc -9.292508e+00   33.584755 0.0255812944 0.42051606
#> ductal__ductal        1.481580e+01   -8.632569 0.0266935703 0.42051606
#>                             from         to
#> beta__delta                 beta      delta
#> delta__beta                delta       beta
#> B__unknown                     B    unknown
#> delta__delta               delta      delta
#> unknown__macrophage      unknown macrophage
#> unknown__B               unknown          B
#> macrophage__unknown   macrophage    unknown
#> B__Th                          B         Th
#> otherimmune__naiveTc otherimmune    naiveTc
#> ductal__ductal            ductal     ductal

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

We can represent this as a bubble plot using the signifPlot() function by providing it the spicy object obtained. Here, we can observe that the most significant relationships occur between pancreatic beta and delta cells, suggesting that the 2 cell types are far more localised during diabetes onset compared to non-diabetics.

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

If we’re interested and wish to examine a specific cell type-cell type relationship in more detail, we can use spicyBoxPlot, specifying the relationship we want to examine.

In the bubble plot above, we can see that the most significant relationship is between beta and delta islet cells in the pancreas. To examine this further, we can specify either from = beta and to = delta or rank = 1 parameters in spicyBoxPlot.

spicyBoxPlot(results = spicyTest,
             # from = "beta",
             # to = "delta",
             rank = 1)

4.3 Mixed effects modelling for custom metrics

spicyR can also be applied to custom distance or abundance metrics. Here, we provide an example where we apply the spicy function to a custom abundance metric.

We first obtain the custom abundance metric by converting the a SpatialExperiment object from the existing SingleCellExperiment object. A KNN interactions graph is then generated with the function buildSpatialGraph from the imcRtools package. This generates a colPairs object inside of the SpatialExperiment object. spicyR provides the function convPairs for converting a colPairs object stored within a SingleCellExperiment object into an abundance matrix by effectively calculating the average number of nearby cells types for every cell type. For example, if there exists on average 5 neutrophils for every macrophage in image 1, the column neutrophil__macrophage would have a value of 5 for image 1.

spicy can take any input of pairwise cell type combinations across multiple images and run a mixed effects model to determine collective differences across conditions.

diabetesData_SPE <- SpatialExperiment(diabetesData_SCE,
                                      colData = colData(diabetesData_SCE)) 

spatialCoords(diabetesData_SPE) <- data.frame(colData(diabetesData_SPE)$x, colData(diabetesData_SPE)$y) |> as.matrix()
spatialCoordsNames(diabetesData_SPE) <- c("x", "y")

diabetesData_SPE <- imcRtools::buildSpatialGraph(diabetesData_SPE, img_id = "imageID", type = "knn", k = 20, coords = c("x", "y"))
#> 'sample_id's are duplicated across 'SpatialExperiment' objects to cbind; appending sample indices.
#> The returned object is ordered by the 'imageID' entry.

pairAbundances <- convPairs(diabetesData_SPE,
                  colPair = "knn_interaction_graph")

head(pairAbundances)
#>     acinar__acinar acinar__alpha acinar__delta acinar__ductal
#> P15       13.88848     0.3676471    0.18137255       3.674020
#> Q06       14.18182     0.6923077    0.09634810       3.048174
#> Q20       14.29848     0.3844857    0.48229342       3.296796
#> P36       13.54382     0.4418103    0.12787356       3.705460
#> P34       15.34466     0.3737864    0.13713592       2.672330
#> Q01       12.77208     0.7364672    0.05698006       3.601140
#>     acinar__endothelial acinar__macrophage acinar__neutrophil
#> P15           0.2879902          0.4901961          0.1764706
#> Q06           0.2494172          0.3411033          0.6130536
#> Q20           0.3237774          0.4333895          0.1669477
#> P36           0.2988506          0.4971264          0.6264368
#> P34           0.1917476          0.3446602          0.2050971
#> Q01           0.2549858          0.8789174          0.8603989
#>     acinar__otherimmune acinar__stromal acinar__Tc acinar__Th acinar__unknown
#> P15          0.04901961      0.19485294 0.06985294 0.02450980       0.5955882
#> Q06          0.01398601      0.17094017 0.05749806 0.04273504       0.4180264
#> Q20          0.06070826      0.09949410 0.07082631 0.00000000       0.3827993
#> P36          0.03017241      0.10416667 0.06681034 0.02442529       0.5330460
#> P34          0.08737864      0.06674757 0.03640777 0.01456311       0.5254854
#> Q01          0.12250712      0.04273504 0.02279202 0.03846154       0.5954416
#>     alpha__acinar alpha__alpha alpha__delta alpha__ductal alpha__endothelial
#> P15      5.833333     7.270833    3.0208333      1.583333          0.9375000
#> Q06      5.756944     8.729167    1.5625000      2.756944          0.4097222
#> Q20      6.228571     4.714286    5.8285714      2.400000          0.6857143
#> P36      6.241379     6.770115    2.4482759      3.379310          0.1494253
#> P34      5.075472     8.981132    3.1698113      1.830189          0.3962264
#> Q01      2.708571    12.794286    0.8914286      2.154286          0.6400000
#>     alpha__macrophage alpha__neutrophil alpha__stromal  alpha__Tc
#> P15       0.270833333        0.02083333     0.04166667 0.06250000
#> Q06       0.006944444        0.02083333     0.05555556 0.00000000
#> Q20       0.000000000        0.00000000     0.00000000 0.00000000
#> P36       0.126436782        0.28735632     0.08045977 0.00000000
#> P34       0.132075472        0.07547170     0.00000000 0.00000000
#> Q01       0.085714286        0.16571429     0.18857143 0.05142857
#>     alpha__unknown delta__acinar delta__alpha delta__delta delta__ductal
#> P15      0.9583333      6.136364     6.772727     2.863636     1.3181818
#> Q06      0.1666667      4.913043     9.478261     2.217391     2.3478261
#> Q20      0.1428571      4.759259     3.925926     8.129630     1.8333333
#> P36      0.5057471      6.000000     7.576923     3.076923     2.1153846
#> P34      0.3396226      5.500000     9.277778     3.166667     0.8333333
#> Q01      0.3200000      3.692308    11.692308     1.230769     1.7692308
#>     delta__endothelial delta__macrophage  delta__Th delta__unknown
#> P15          1.2272727        0.54545455 0.09090909      1.0454545
#> Q06          0.5217391        0.00000000 0.00000000      0.1739130
#> Q20          1.0925926        0.00000000 0.00000000      0.1481481
#> P36          0.1153846        0.23076923 0.00000000      0.4615385
#> P34          0.5000000        0.22222222 0.00000000      0.4444444
#> Q01          0.9230769        0.07692308 0.00000000      0.1538462
#>     ductal__acinar ductal__alpha ductal__delta ductal__ductal
#> P15       12.84513     0.3893805     0.1415929       4.654867
#> Q06       12.83819     1.3009709     0.2006472       3.977346
#> Q20       13.45638     0.5838926     0.7583893       4.060403
#> P36       12.04348     0.7560386     0.1425121       5.260870
#> P34       13.38272     0.6049383     0.1111111       4.055556
#> Q01       10.50633     1.6413502     0.1054852       5.147679
#>     ductal__endothelial ductal__macrophage ductal__neutrophil
#> P15           0.2522124          0.5663717         0.19026549
#> Q06           0.2588997          0.2944984         0.41423948
#> Q20           0.2885906          0.3087248         0.09395973
#> P36           0.2125604          0.4009662         0.66666667
#> P34           0.2530864          0.5679012         0.22222222
#> Q01           0.1940928          0.8734177         0.90295359
#>     ductal__otherimmune ductal__stromal ductal__Tc  ductal__Th ductal__unknown
#> P15          0.04424779      0.24336283 0.04424779 0.026548673       0.6017699
#> Q06          0.00000000      0.16181230 0.09708738 0.032362460       0.3462783
#> Q20          0.06040268      0.06711409 0.08724832 0.000000000       0.2348993
#> P36          0.02173913      0.08212560 0.03864734 0.004830918       0.3695652
#> P34          0.06172840      0.08024691 0.04320988 0.049382716       0.5679012
#> Q01          0.10548523      0.04641350 0.05063291 0.004219409       0.4135021
#>     endothelial__acinar endothelial__alpha endothelial__delta
#> P15           11.000000          2.0000000         1.14285714
#> Q06           11.666667          2.1111111         0.48148148
#> Q20            8.320000          0.9600000         2.04000000
#> P36            9.849057          0.3207547         0.09433962
#> P34            9.666667          1.3333333         0.60000000
#> Q01            9.250000          5.6000000         0.65000000
#>     endothelial__ductal endothelial__endothelial endothelial__macrophage
#> P15            3.142857                0.5714286               0.6190476
#> Q06            2.407407                0.9629630               0.7407407
#> Q20            1.720000                3.4000000               0.8000000
#> P36            2.037736                2.4150943               1.3018868
#> P34            2.733333                1.8666667               0.6000000
#> Q01            2.350000                0.1500000               0.6000000
#>     endothelial__neutrophil endothelial__otherimmune endothelial__stromal
#> P15               0.0952381                0.0952381            0.2857143
#> Q06               0.7037037                0.0000000            0.2222222
#> Q20               0.4000000                0.8400000            0.0000000
#> P36               1.3396226                0.0000000            0.2264151
#> P34               0.3333333                0.2000000            0.2666667
#> Q01               0.5000000                0.0500000            0.0500000
#>     endothelial__Th endothelial__unknown macrophage__acinar macrophage__alpha
#> P15      0.09523810            0.9523810           12.50000        0.44117647
#> Q06      0.03703704            0.4444444           13.70968        0.03225806
#> Q20      0.00000000            1.5200000           14.08696        0.00000000
#> P36      0.03773585            2.3207547           11.66667        0.23809524
#> P34      0.00000000            1.6000000           12.33333        0.66666667
#> Q01      0.05000000            0.7500000           12.42000        0.26000000
#>     macrophage__delta macrophage__ductal macrophage__endothelial
#> P15        0.38235294           3.647059               0.4117647
#> Q06        0.00000000           2.774194               0.6451613
#> Q20        0.04347826           2.521739               0.4782609
#> P36        0.12698413           2.873016               0.9682540
#> P34        0.23809524           4.000000               0.4761905
#> Q01        0.02000000           4.000000               0.2400000
#>     macrophage__macrophage macrophage__neutrophil macrophage__otherimmune
#> P15              0.9117647              0.1176471              0.08823529
#> Q06              0.5161290              1.1935484              0.00000000
#> Q20              1.6521739              0.2173913              0.00000000
#> P36              1.2380952              1.2063492              0.04761905
#> P34              0.4761905              0.3333333              0.04761905
#> Q01              1.0000000              0.9800000              0.16000000
#>     macrophage__stromal macrophage__Tc macrophage__Th macrophage__unknown
#> P15           0.2058824     0.17647059     0.05882353           1.0588235
#> Q06           0.2903226     0.03225806     0.03225806           0.7741935
#> Q20           0.2608696     0.08695652     0.00000000           0.6521739
#> P36           0.1269841     0.09523810     0.01587302           1.3968254
#> P34           0.1428571     0.19047619     0.09523810           1.0000000
#> Q01           0.0800000     0.06000000     0.00000000           0.7600000
#>     neutrophil__acinar neutrophil__alpha neutrophil__ductal
#> P15           13.72727        0.09090909           3.909091
#> Q06           14.01818        0.05454545           2.181818
#> Q20           13.55556        0.11111111           1.555556
#> P36           11.57692        0.32051282           3.705128
#> P34           13.90909        0.36363636           3.090909
#> Q01           11.86792        0.56603774           3.943396
#>     neutrophil__endothelial neutrophil__macrophage neutrophil__stromal
#> P15              0.09090909              0.4545455          0.36363636
#> Q06              0.30909091              0.6909091          0.23636364
#> Q20              1.22222222              0.4444444          0.00000000
#> P36              0.87179487              1.1025641          0.14102564
#> P34              0.54545455              0.7272727          0.27272727
#> Q01              0.20754717              1.0566038          0.03773585
#>     neutrophil__unknown otherimmune__acinar otherimmune__ductal
#> P15           1.3636364            11.80000            3.600000
#> Q06           0.9818182            16.00000            0.000000
#> Q20           1.1111111             8.00000            1.250000
#> P36           0.7820513            13.66667            3.333333
#> P34           0.6363636            16.20000            2.000000
#> Q01           0.6981132            12.71429            2.857143
#>     otherimmune__endothelial otherimmune__macrophage otherimmune__neutrophil
#> P15                0.8000000               0.6000000               0.2000000
#> Q06                0.0000000               0.0000000               3.0000000
#> Q20                3.5000000               0.5000000               1.5000000
#> P36                0.0000000               0.6666667               0.6666667
#> P34                0.8000000               0.2000000               0.2000000
#> Q01                0.1428571               1.2857143               1.5714286
#>     otherimmune__otherimmune otherimmune__stromal otherimmune__Tc
#> P15                     1.20                  0.8       0.2000000
#> Q06                     0.00                  0.0       0.0000000
#> Q20                     1.25                  0.0       0.0000000
#> P36                     0.00                  0.0       0.0000000
#> P34                     0.00                  0.2       0.2000000
#> Q01                     0.00                  0.0       0.1428571
#>     otherimmune__Th otherimmune__unknown stromal__acinar stromal__alpha
#> P15             0.2             0.600000        12.76923      0.1538462
#> Q06             1.0             0.000000        12.88235      0.4705882
#> Q20             0.0             3.250000        16.50000      0.0000000
#> P36             0.0             1.333333        12.50000      0.5714286
#> P34             0.0             0.200000        12.25000      0.0000000
#> Q01             0.0             1.285714         8.40000      6.0000000
#>     stromal__ductal stromal__endothelial stromal__macrophage
#> P15        3.769231            0.4615385           0.6153846
#> Q06        3.352941            0.2941176           0.4705882
#> Q20        2.000000            0.0000000           1.0000000
#> P36        2.500000            0.6428571           0.5714286
#> P34        3.250000            1.2500000           0.7500000
#> Q01        3.000000            0.2000000           0.8000000
#>     stromal__neutrophil stromal__otherimmune stromal__stromal stromal__unknown
#> P15           0.3076923            0.3076923        0.1538462        1.4615385
#> Q06           0.8823529            0.0000000        0.4705882        0.7058824
#> Q20           0.0000000            0.0000000        0.0000000        0.2500000
#> P36           0.5714286            0.0000000        0.1428571        1.9285714
#> P34           0.7500000            0.2500000        0.0000000        0.7500000
#> Q01           0.4000000            0.0000000        0.0000000        1.0000000
#>     Tc__acinar Tc__alpha Tc__ductal Tc__macrophage    Tc__Tc    Tc__Th
#> P15  13.750000 1.0000000   1.750000      1.5000000 0.5000000 0.5000000
#> Q06  12.833333 0.0000000   5.166667      0.3333333 0.5000000 0.3333333
#> Q20  14.333333 0.0000000   4.333333      0.6666667 0.0000000 0.0000000
#> P36  13.857143 0.1428571   2.428571      0.8571429 0.0000000 0.2857143
#> P34   9.333333 0.0000000   2.000000      1.0000000 0.6666667 0.0000000
#> Q01   6.500000 4.5000000   5.000000      1.5000000 0.0000000 0.0000000
#>     Tc__unknown Th__acinar Th__delta Th__ductal Th__endothelial Th__macrophage
#> P15   1.0000000   10.00000         1        4.0       1.0000000            2.0
#> Q06   0.3333333   12.80000         0        2.8       0.2000000            0.2
#> Q20   0.3333333    0.00000         0        0.0       0.0000000            0.0
#> P36   0.7142857   12.66667         0        1.0       0.6666667            1.0
#> P34   1.6666667   10.00000         0        6.0       0.0000000            2.0
#> Q01   1.0000000   17.00000         0        1.0       0.5000000            0.0
#>        Th__Tc Th__unknown unknown__acinar unknown__alpha unknown__delta
#> P15 1.0000000         1.0        10.51020      1.1020408     0.48979592
#> Q06 0.4000000         0.6        13.47619      0.5952381     0.14285714
#> Q20 0.0000000         0.0        13.10526      0.3157895     0.47368421
#> P36 0.6666667         2.0        11.23810      0.5357143     0.19047619
#> P34 0.0000000         2.0        13.47059      0.5882353     0.23529412
#> Q01 0.0000000         1.0        11.62857      1.6571429     0.02857143
#>     unknown__ductal unknown__endothelial unknown__macrophage
#> P15        2.816327            0.3877551           0.7755102
#> Q06        2.547619            0.3333333           0.6190476
#> Q20        2.000000            1.6315789           0.7368421
#> P36        2.154762            1.4404762           1.0476190
#> P34        2.882353            0.7058824           0.6176471
#> Q01        2.800000            0.4285714           1.0857143
#>     unknown__neutrophil unknown__otherimmune unknown__stromal unknown__Tc
#> P15           0.3265306           0.04081633       0.46938776  0.08163265
#> Q06           1.4047619           0.00000000       0.26190476  0.04761905
#> Q20           0.4210526           0.42105263       0.05263158  0.05263158
#> P36           0.8214286           0.08333333       0.36904762  0.05952381
#> P34           0.2352941           0.02941176       0.08823529  0.14705882
#> Q01           1.0571429           0.20000000       0.17142857  0.05714286
#>     unknown__Th unknown__unknown acinar__gamma alpha__gamma delta__gamma
#> P15  0.04081633        2.9591837    0.00000000    0.0000000    0.0000000
#> Q06  0.07142857        0.4761905    0.07459207    0.5347222    0.2173913
#> Q20  0.00000000        0.7894737    0.00000000    0.0000000    0.0000000
#> P36  0.07142857        1.9880952    0.00000000    0.0000000    0.0000000
#> P34  0.05882353        0.9411765    0.00000000    0.0000000    0.0000000
#> Q01  0.00000000        0.8857143    0.00000000    0.0000000    0.0000000
#>     delta__stromal ductal__gamma endothelial__gamma endothelial__Tc
#> P15      0.0000000     0.0000000          0.0000000      0.00000000
#> Q06      0.1304348     0.0776699          0.1851852      0.03703704
#> Q20      0.0000000     0.0000000          0.0000000      0.00000000
#> P36      0.1153846     0.0000000          0.0000000      0.05660377
#> P34      0.0000000     0.0000000          0.0000000      0.80000000
#> Q01      0.1538462     0.0000000          0.0000000      0.00000000
#>     gamma__acinar gamma__alpha gamma__delta gamma__ductal gamma__endothelial
#> P15             0     0.000000    0.0000000             0          0.0000000
#> Q06             7     5.923077    0.3846154             2          0.3846154
#> Q20             0     0.000000    0.0000000             0          0.0000000
#> P36             0     0.000000    0.0000000             0          0.0000000
#> P34             0     0.000000    0.0000000             0          0.0000000
#> Q01             0     0.000000    0.0000000             0          0.0000000
#>     gamma__gamma neutrophil__neutrophil neutrophil__otherimmune neutrophil__Tc
#> P15     0.000000              0.0000000              0.00000000     0.00000000
#> Q06     4.307692              1.2727273              0.05454545     0.01818182
#> Q20     0.000000              0.6666667              0.66666667     0.00000000
#> P36     0.000000              1.2564103              0.02564103     0.06410256
#> P34     0.000000              0.0000000              0.09090909     0.00000000
#> Q01     0.000000              1.3773585              0.18867925     0.01886792
#>     neutrophil__Th stromal__delta stromal__Tc stromal__Th Tc__neutrophil
#> P15     0.00000000      0.0000000   0.0000000   0.0000000      0.0000000
#> Q06     0.18181818      0.1764706   0.1176471   0.1764706      0.1666667
#> Q20     0.00000000      0.0000000   0.2500000   0.0000000      0.0000000
#> P36     0.03846154      0.2857143   0.1428571   0.1428571      1.0000000
#> P34     0.09090909      0.0000000   0.7500000   0.0000000      0.0000000
#> Q01     0.00000000      0.2000000   0.0000000   0.0000000      0.5000000
#>     Tc__stromal Th__neutrophil Th__otherimmune Th__stromal unknown__gamma
#> P15   0.0000000       0.000000             0.0   0.0000000     0.00000000
#> Q06   0.3333333       2.000000             0.2   0.8000000     0.02380952
#> Q20   0.3333333       0.000000             0.0   0.0000000     0.00000000
#> P36   0.2857143       1.333333             0.0   0.6666667     0.00000000
#> P34   1.0000000       0.000000             0.0   0.0000000     0.00000000
#> Q01   0.0000000       0.500000             0.0   0.0000000     0.00000000
#>     delta__neutrophil delta__otherimmune neutrophil__delta otherimmune__alpha
#> P15        0.00000000         0.00000000        0.00000000          0.0000000
#> Q06        0.00000000         0.00000000        0.00000000          0.0000000
#> Q20        0.09259259         0.01851852        0.66666667          0.2500000
#> P36        0.30769231         0.00000000        0.11538462          0.3333333
#> P34        0.05555556         0.00000000        0.27272727          0.0000000
#> Q01        0.07692308         0.00000000        0.01886792          0.0000000
#>     otherimmune__delta alpha__otherimmune Tc__endothelial Tc__otherimmune
#> P15                0.0         0.00000000       0.0000000       0.0000000
#> Q06                0.0         0.00000000       0.0000000       0.0000000
#> Q20                0.5         0.00000000       0.0000000       0.0000000
#> P36                0.0         0.01149425       0.4285714       0.0000000
#> P34                0.0         0.00000000       4.0000000       0.3333333
#> Q01                0.0         0.00000000       0.0000000       0.5000000
#>      acinar__B B__acinar B__delta B__ductal B__macrophage B__neutrophil
#> P15 0.00000000         0        0         0             0             0
#> Q06 0.00000000         0        0         0             0             0
#> Q20 0.00000000         0        0         0             0             0
#> P36 0.00000000         0        0         0             0             0
#> P34 0.00000000         0        0         0             0             0
#> Q01 0.01709402        14        1         1             1             3
#>       delta__B delta__Tc   ductal__B macrophage__B neutrophil__B Tc__delta
#> P15 0.00000000 0.0000000 0.000000000          0.00    0.00000000       0.0
#> Q06 0.00000000 0.0000000 0.000000000          0.00    0.00000000       0.0
#> Q20 0.00000000 0.0000000 0.000000000          0.00    0.00000000       0.0
#> P36 0.00000000 0.0000000 0.000000000          0.00    0.00000000       0.0
#> P34 0.00000000 0.0000000 0.000000000          0.00    0.00000000       0.0
#> Q01 0.07692308 0.1538462 0.008438819          0.02    0.01886792       0.5
#>     alpha__Th gamma__neutrophil gamma__Th gamma__unknown neutrophil__gamma
#> P15         0                 0         0              0                 0
#> Q06         0                 0         0              0                 0
#> Q20         0                 0         0              0                 0
#> P36         0                 0         0              0                 0
#> P34         0                 0         0              0                 0
#> Q01         0                 0         0              0                 0
#>     Th__alpha Th__gamma Th__Th acinar__naiveTc ductal__naiveTc naiveTc__acinar
#> P15         0         0      0               0               0               0
#> Q06         0         0      0               0               0               0
#> Q20         0         0      0               0               0               0
#> P36         0         0      0               0               0               0
#> P34         0         0      0               0               0               0
#> Q01         0         0      0               0               0               0
#>     naiveTc__ductal naiveTc__neutrophil neutrophil__naiveTc acinar__beta
#> P15               0                   0                   0            0
#> Q06               0                   0                   0            0
#> Q20               0                   0                   0            0
#> P36               0                   0                   0            0
#> P34               0                   0                   0            0
#> Q01               0                   0                   0            0
#>     alpha__beta beta__acinar beta__alpha beta__beta beta__delta beta__ductal
#> P15           0            0           0          0           0            0
#> Q06           0            0           0          0           0            0
#> Q20           0            0           0          0           0            0
#> P36           0            0           0          0           0            0
#> P34           0            0           0          0           0            0
#> Q01           0            0           0          0           0            0
#>     beta__endothelial beta__macrophage beta__neutrophil beta__stromal beta__Tc
#> P15                 0                0                0             0        0
#> Q06                 0                0                0             0        0
#> Q20                 0                0                0             0        0
#> P36                 0                0                0             0        0
#> P34                 0                0                0             0        0
#> Q01                 0                0                0             0        0
#>     beta__unknown delta__beta ductal__beta endothelial__beta macrophage__beta
#> P15             0           0            0                 0                0
#> Q06             0           0            0                 0                0
#> Q20             0           0            0                 0                0
#> P36             0           0            0                 0                0
#> P34             0           0            0                 0                0
#> Q01             0           0            0                 0                0
#>     neutrophil__beta stromal__beta Tc__beta unknown__beta beta__gamma
#> P15                0             0        0             0           0
#> Q06                0             0        0             0           0
#> Q20                0             0        0             0           0
#> P36                0             0        0             0           0
#> P34                0             0        0             0           0
#> Q01                0             0        0             0           0
#>     gamma__beta beta__Th Th__beta beta__otherimmune macrophage__naiveTc
#> P15           0        0        0                 0                   0
#> Q06           0        0        0                 0                   0
#> Q20           0        0        0                 0                   0
#> P36           0        0        0                 0                   0
#> P34           0        0        0                 0                   0
#> Q01           0        0        0                 0                   0
#>     naiveTc__macrophage naiveTc__stromal naiveTc__unknown otherimmune__beta
#> P15                   0                0                0                 0
#> Q06                   0                0                0                 0
#> Q20                   0                0                0                 0
#> P36                   0                0                0                 0
#> P34                   0                0                0                 0
#> Q01                   0                0                0                 0
#>     stromal__naiveTc unknown__naiveTc endothelial__naiveTc naiveTc__endothelial
#> P15                0                0                    0                    0
#> Q06                0                0                    0                    0
#> Q20                0                0                    0                    0
#> P36                0                0                    0                    0
#> P34                0                0                    0                    0
#> Q01                0                0                    0                    0
#>     naiveTc__otherimmune naiveTc__Tc naiveTc__Th otherimmune__naiveTc
#> P15                    0           0           0                    0
#> Q06                    0           0           0                    0
#> Q20                    0           0           0                    0
#> P36                    0           0           0                    0
#> P34                    0           0           0                    0
#> Q01                    0           0           0                    0
#>     Tc__naiveTc Th__naiveTc alpha__naiveTc gamma__otherimmune naiveTc__alpha
#> P15           0           0              0                  0              0
#> Q06           0           0              0                  0              0
#> Q20           0           0              0                  0              0
#> P36           0           0              0                  0              0
#> P34           0           0              0                  0              0
#> Q01           0           0              0                  0              0
#>     otherimmune__gamma naiveTc__naiveTc delta__naiveTc naiveTc__delta B__B
#> P15                  0                0              0              0    0
#> Q06                  0                0              0              0    0
#> Q20                  0                0              0              0    0
#> P36                  0                0              0              0    0
#> P34                  0                0              0              0    0
#> Q01                  0                0              0              0    0
#>     B__endothelial B__otherimmune B__stromal B__Tc B__Th B__unknown
#> P15              0              0          0     0     0          0
#> Q06              0              0          0     0     0          0
#> Q20              0              0          0     0     0          0
#> P36              0              0          0     0     0          0
#> P34              0              0          0     0     0          0
#> Q01              0              0          0     0     0          0
#>     beta__naiveTc endothelial__B naiveTc__beta otherimmune__B stromal__B Tc__B
#> P15             0              0             0              0          0     0
#> Q06             0              0             0              0          0     0
#> Q20             0              0             0              0          0     0
#> P36             0              0             0              0          0     0
#> P34             0              0             0              0          0     0
#> Q01             0              0             0              0          0     0
#>     Th__B unknown__B alpha__B B__alpha B__beta B__naiveTc beta__B naiveTc__B
#> P15     0          0        0        0       0          0       0          0
#> Q06     0          0        0        0       0          0       0          0
#> Q20     0          0        0        0       0          0       0          0
#> P36     0          0        0        0       0          0       0          0
#> P34     0          0        0        0       0          0       0          0
#> Q01     0          0        0        0       0          0       0          0
#>     gamma__naiveTc gamma__stromal gamma__Tc stromal__gamma Tc__gamma
#> P15              0              0         0              0         0
#> Q06              0              0         0              0         0
#> Q20              0              0         0              0         0
#> P36              0              0         0              0         0
#> P34              0              0         0              0         0
#> Q01              0              0         0              0         0
#>     gamma__macrophage macrophage__gamma naiveTc__gamma gamma__B B__gamma
#> P15                 0                 0              0        0        0
#> Q06                 0                 0              0        0        0
#> Q20                 0                 0              0        0        0
#> P36                 0                 0              0        0        0
#> P34                 0                 0              0        0        0
#> Q01                 0                 0              0        0        0

spicy can take any input of pairwise cell type combinations across multiple images and run a mixed effects model to determine collective differences across conditions. To check out other custom distance metrics which can be used, feel free to check out the Statial package.

spicyTestColPairs <- spicy(
  diabetesData_SPE,
  condition = "stage",
  subject = "case",
  alternateResult = pairAbundances,
  weights = FALSE
)
#> Cell count weighting set to FALSE for alternate results
#> Testing for spatial differences across conditions accounting for multiple images per subject

topPairs(spicyTestColPairs)
#>                            intercept coefficient     p.value adj.pvalue
#> naiveTc__otherimmune    8.0222690269 -3.94642071 0.002026678  0.1480027
#> gamma__Th               0.0015244421  0.01195244 0.003035951  0.1480027
#> macrophage__beta        0.0000000000  0.19829545 0.004003918  0.1480027
#> Th__delta               0.3206950166  0.63117121 0.004200428  0.1480027
#> neutrophil__otherimmune 6.5324859454 -2.57350623 0.004513131  0.1480027
#> B__Th                   1.2000000000  5.11013889 0.004800429  0.1480027
#> endothelial__stromal    0.1982832447  0.60998756 0.005951482  0.1480027
#> B__delta                0.3560010823  0.65020515 0.006006260  0.1480027
#> alpha__otherimmune      9.2333978810 -3.41741456 0.006382759  0.1480027
#> otherimmune__Th         0.0008673736  0.01574398 0.006775857  0.1480027
#>                                from          to
#> naiveTc__otherimmune        naiveTc otherimmune
#> gamma__Th                     gamma          Th
#> macrophage__beta         macrophage        beta
#> Th__delta                        Th       delta
#> neutrophil__otherimmune  neutrophil otherimmune
#> B__Th                             B          Th
#> endothelial__stromal    endothelial     stromal
#> B__delta                          B       delta
#> alpha__otherimmune            alpha otherimmune
#> otherimmune__Th         otherimmune          Th

Again, we can present this spicy object as a bubble plot using the signifPlot() function by providing it with the spicy object.

signifPlot(
  spicyTestColPairs,
  marksToPlot = c(
    "alpha", "acinar", "ductal", "naiveTc", "neutrophil", "Tc",
    "Th", "otherimmune"
  )
)

5 sessionInfo()

sessionInfo()
#> R version 4.3.2 (2023-10-31)
#> 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] imcRtools_1.8.0             SpatialExperiment_1.12.0   
#>  [3] SingleCellExperiment_1.24.0 SummarizedExperiment_1.32.0
#>  [5] Biobase_2.62.0              GenomicRanges_1.54.1       
#>  [7] GenomeInfoDb_1.38.1         IRanges_2.36.0             
#>  [9] S4Vectors_0.40.1            BiocGenerics_0.48.1        
#> [11] MatrixGenerics_1.14.0       matrixStats_1.1.0          
#> [13] ggplot2_3.4.4               spicyR_1.14.2              
#> [15] BiocStyle_2.30.0           
#> 
#> loaded via a namespace (and not attached):
#>   [1] splines_4.3.2               later_1.3.1                
#>   [3] bitops_1.0-7                svgPanZoom_0.3.4           
#>   [5] tibble_3.2.1                polyclip_1.10-6            
#>   [7] lifecycle_1.0.4             sf_1.0-14                  
#>   [9] rstatix_0.7.2               vroom_1.6.4                
#>  [11] lattice_0.22-5              MASS_7.3-60                
#>  [13] MultiAssayExperiment_1.28.0 backports_1.4.1            
#>  [15] magrittr_2.0.3              sass_0.4.7                 
#>  [17] rmarkdown_2.25              jquerylib_0.1.4            
#>  [19] yaml_2.3.7                  httpuv_1.6.12              
#>  [21] ClassifyR_3.6.2             sp_2.1-1                   
#>  [23] spatstat.sparse_3.0-3       DBI_1.1.3                  
#>  [25] minqa_1.2.6                 RColorBrewer_1.1-3         
#>  [27] abind_1.4-5                 zlibbioc_1.48.0            
#>  [29] purrr_1.0.2                 ggraph_2.1.0               
#>  [31] RCurl_1.98-1.13             tweenr_2.0.2               
#>  [33] GenomeInfoDbData_1.2.11     ggrepel_0.9.4              
#>  [35] RTriangle_1.6-0.12          spatstat.utils_3.0-4       
#>  [37] terra_1.7-55                pheatmap_1.0.12            
#>  [39] units_0.8-4                 goftest_1.2-3              
#>  [41] spatstat.random_3.2-1       DelayedMatrixStats_1.24.0  
#>  [43] svglite_2.1.2               codetools_0.2-19           
#>  [45] DelayedArray_0.28.0         scuttle_1.12.0             
#>  [47] DT_0.30                     ggforce_0.4.1              
#>  [49] tidyselect_1.2.0            raster_3.6-26              
#>  [51] farver_2.1.1                lme4_1.1-35.1              
#>  [53] viridis_0.6.4               spatstat.explore_3.2-5     
#>  [55] jsonlite_1.8.7              BiocNeighbors_1.20.0       
#>  [57] e1071_1.7-13                ellipsis_0.3.2             
#>  [59] tidygraph_1.2.3             survival_3.5-7             
#>  [61] systemfonts_1.0.5           tools_4.3.2                
#>  [63] Rcpp_1.0.11                 glue_1.6.2                 
#>  [65] gridExtra_2.3               SparseArray_1.2.2          
#>  [67] xfun_0.41                   mgcv_1.9-0                 
#>  [69] EBImage_4.44.0              dplyr_1.1.3                
#>  [71] HDF5Array_1.30.0            scam_1.2-14                
#>  [73] shinydashboard_0.7.2        withr_2.5.2                
#>  [75] numDeriv_2016.8-1.1         BiocManager_1.30.22        
#>  [77] fastmap_1.1.1               boot_1.3-28.1              
#>  [79] rhdf5filters_1.14.1         fansi_1.0.5                
#>  [81] digest_0.6.33               R6_2.5.1                   
#>  [83] mime_0.12                   colorspace_2.1-0           
#>  [85] tensor_1.5                  jpeg_0.1-10                
#>  [87] spatstat.data_3.0-3         utf8_1.2.4                 
#>  [89] tidyr_1.3.0                 generics_0.1.3             
#>  [91] data.table_1.14.8           class_7.3-22               
#>  [93] graphlayouts_1.0.2          htmlwidgets_1.6.2          
#>  [95] S4Arrays_1.2.0              pkgconfig_2.0.3            
#>  [97] gtable_0.3.4                XVector_0.42.0             
#>  [99] htmltools_0.5.7             carData_3.0-5              
#> [101] bookdown_0.36               fftwtools_0.9-11           
#> [103] scales_1.2.1                png_0.1-8                  
#> [105] knitr_1.45                  tzdb_0.4.0                 
#> [107] reshape2_1.4.4              rjson_0.2.21               
#> [109] nlme_3.1-163                nloptr_2.0.3               
#> [111] proxy_0.4-27                cachem_1.0.8               
#> [113] rhdf5_2.46.0                stringr_1.5.0              
#> [115] KernSmooth_2.23-22          parallel_4.3.2             
#> [117] vipor_0.4.5                 concaveman_1.1.0           
#> [119] pillar_1.9.0                grid_4.3.2                 
#> [121] vctrs_0.6.4                 promises_1.2.1             
#> [123] ggpubr_0.6.0                car_3.1-2                  
#> [125] beachmat_2.18.0             distances_0.1.9            
#> [127] xtable_1.8-4                beeswarm_0.4.0             
#> [129] evaluate_0.23               readr_2.1.4                
#> [131] magick_2.8.1                cli_3.6.1                  
#> [133] locfit_1.5-9.8              compiler_4.3.2             
#> [135] rlang_1.1.2                 crayon_1.5.2               
#> [137] ggsignif_0.6.4              labeling_0.4.3             
#> [139] classInt_0.4-10             plyr_1.8.9                 
#> [141] ggbeeswarm_0.7.2            stringi_1.7.12             
#> [143] viridisLite_0.4.2           deldir_1.0-9               
#> [145] BiocParallel_1.36.0         nnls_1.5                   
#> [147] cytomapper_1.14.0           lmerTest_3.1-3             
#> [149] munsell_0.5.0               tiff_0.1-11                
#> [151] spatstat.geom_3.2-7         Matrix_1.6-2               
#> [153] hms_1.1.3                   bit64_4.0.5                
#> [155] sparseMatrixStats_1.14.0    Rhdf5lib_1.24.0            
#> [157] shiny_1.7.5.1               highr_0.10                 
#> [159] igraph_1.5.1                broom_1.0.5                
#> [161] bslib_0.5.1                 bit_4.0.5