# Quantifying cell colocalisation with SPIAT

Yuzhou Feng

#### 2022-12-20

#### Package

SPIAT 1.0.4

    library(SPIAT)

With SPIAT we can quantify cell colocalisation, which refers to how much two cell types are colocalising and thus potentially interacting.

In this vignette we will use an inForm data file that’s already been formatted for SPIAT with `format_image_to_spe()`, which we can load with `data()`. We will use `define_celltypes()` to define the cells with certain combinations of markers.

    data("simulated_image")

    # define cell types
    formatted_image <- define_celltypes(
        simulated_image,
        categories = c("Tumour_marker","Immune_marker1,Immune_marker2",
                       "Immune_marker1,Immune_marker3",
                       "Immune_marker1,Immune_marker2,Immune_marker4", "OTHER"),
        category_colname = "Phenotype",
        names = c("Tumour", "Immune1", "Immune2", "Immune3", "Others"),
        new_colname = "Cell.Type")

# 1 Cells In Neighbourhood (CIN)

We can calculate the average percentage of cells of one cell type (target) within a radius of another cell type (reference) using `average_percentage_of_cells_within_radius()`.

    average_percentage_of_cells_within_radius(spe_object = formatted_image,
                                              reference_celltype = "Immune1",
                                              target_celltype = "Immune2",
                                              radius=100, feature_colname="Cell.Type")

    ## [1] 4.768123

Alternatively, this analysis can also be performed based on marker intensities rather than cell types. Here, we use `average_marker_intensity_within_radius()` to calculate the average intensity of the target_marker within a radius from the cells positive for the reference marker. Note that it pools all cells with the target marker that are within the specific radius of any reference cell. Results represent the average intensities within a radius.

    average_marker_intensity_within_radius(spe_object = formatted_image,
                                           reference_marker ="Immune_marker3",
                                           target_marker = "Immune_marker2",
                                           radius=30)

    ## [1] 0.5995357

To help identify suitable radii for `average_percentage_of_cells_within_radius()` and `average_marker_intensity_within_radius()` users can use `plot_average_intensity()`. This function calculates the average intensity of a target marker for a number of user-supplied radii values, and plots the intensity level at each specified radius as a line graph. The radius unit is pixels.

    plot_average_intensity(spe_object=formatted_image, reference_marker="Immune_marker3",
                           target_marker="Immune_marker2", radii=c(30, 35, 40, 45, 50, 75, 100))

This plot shows low levels of Immune_marker3 were observed in cells near Immune_marker2+ cells and these levels increased at larger radii. This suggests Immune_marker2+ and Immune_marker3+ cells may not be closely interacting and are actually repelled.

# 2 Mixing Score (MS) and Normalised Mixing Score (NMS)

This score was originally defined as the number of immune-tumour interactions divided by the number of immune-immune interactions (Keren et al. 2018). SPIAT generalises this method for any user-defined pair of cell types. `mixing_score_summary()` returns the mixing score between a reference cell type and a target cell type. This mixing score is defined as the number of target-reference interactions/number of reference-reference interactions within a specified radius. The higher the score the greater the mixing of the two cell types. The normalised score is normalised for the number of target and reference cells in the image.

    mixing_score_summary(spe_object = formatted_image, reference_celltype = "Immune1",
                         target_celltype = "Immune2", radius=100, feature_colname ="Cell.Type")

    ##   Reference  Target Number_of_reference_cells Number_of_target_cells
    ## 2   Immune1 Immune2                       338                    178
    ##   Reference_target_interaction Reference_reference_interaction Mixing_score
    ## 2                          583                            1184    0.4923986
    ##   Normalised_mixing_score
    ## 2                1.864476

# 3 Cross K function

Cross K function calculates the number of target cell types across a range of radii from a reference cell type, and compares the behaviour of the input image with an image of randomly distributed points using a Poisson point process. There are four patterns that can be distinguished from K-cross function, as illustrated in the plots below. (taken from here in April 2021).

Here, the black line represents the input image, the red line represents a randomly distributed point pattern.

- 1st plot: The red line and black line are close to each other, meaning the two types of points are randomly independently distributed.
- 2nd plot: The red line is under the black line, with a large difference in the middle of the plot, meaning the points are mixed and split into clusters.
- 3rd plot: With the increase of radius, the black line diverges further from the red line, meaning that there is one mixed cluster of two types of points.
- 4th plot: The red line is above the black line, meaning that the two types of points form separated clusters.

We can calculate the cross K-function using SPIAT. Here, we need to define which are the cell types of interest. In this example, we are using Tumour cells as the reference population, and Immune3 cells as the target population.

    df_cross <- calculate_cross_functions(formatted_image, method = "Kcross",
                                          cell_types_of_interest = c("Tumour","Immune2"),
                                          feature_colname ="Cell.Type",
                                          dist = 100)

The results shows similar pattern as the 4th plot in the cross K diagram. This means “Tumour” cells and “Immune2” cells are not colocalised (or form separate clusters).

We can calculate the area under the curve (AUC) of the cross K-function. In general, this tells us the two types of cells are:

- negative values: separate clusters
- positive values: mixing of cell types

    AUC_of_cross_function(df_cross)

    ## [1] -0.2836735

The AUC score is close to zero so this tells us that the two types of cells either do not have a relationship or they form a ring surrounding a cluster.

# 4 Cross-K Intersection (CKI)

There is another pattern in cross K curve which has not been previously appreciated, which is when there is a “ring” of one cell type, generally immune cells, surrounding the area of another cell type, generally tumour cells. For this pattern, the observed and expected curves in cross K function cross or intersect, such as the cross K plot above.

We note that crossing is not exclusively present in cases where there is an immune ring. When separate clusters of two cell types are close, there can be a crossing at a small radius. In images with infiltration, crossing may also happen at an extremely low distances due to randomness. To use the CKI to detect a ring pattern, users need to determine a threshold for when there is a true immune ring. Based on our tests, these generally fall within at a quarter to half of the image size, but users are encouraged to experiment with their datasets.

Here we use the colocalisation of “Tumour” and “Immune3” cells as an example. Let’s revisit the example image.

Compute the cross K function between “Tumour” and “Immune3”:

    df_cross <- calculate_cross_functions(formatted_image, method = "Kcross",
                                          cell_types_of_interest = c("Tumour","Immune3"),
                                          feature_colname ="Cell.Type",
                                          dist = 100)

Then find the intersection of the observed and expected cross K curves.

    crossing_of_crossK(df_cross)

    ## [1] "Crossing of cross K function is detected for this image, indicating a potential immune ring."
    ## [1] "The crossing happens at the 50% of the specified distance."

    ## [1] 0.5

The result shows that the crossing happens at 50% of the specified distance (100) of the cross K function, which is very close to the edge of the tumour cluster. This means that the crossing is not due to the randomness in cell distribution, nor due to two close located immune and tumour clusters. This result aligns with the observation that there is an immune ring surrounding the tumour cluster.

# 5 You can access the vignettes for other modules of SPIAT here:

- Overview of SPIAT
- Data reading and formatting
- Quality control and visualisation
- Basic analysis
- Spatial heterogeneity
- Tissue structure
- Cellular neighborhood

# 6 Reproducibility

    sessionInfo()

    ## R version 4.2.2 (2022-10-31 ucrt)
    ## Platform: x86_64-w64-mingw32/x64 (64-bit)
    ## Running under: Windows Server x64 (build 20348)
    ##
    ## Matrix products: default
    ##
    ## locale:
    ## [1] LC_COLLATE=C
    ## [2] LC_CTYPE=English_United States.utf8
    ## [3] LC_MONETARY=English_United States.utf8
    ## [4] LC_NUMERIC=C
    ## [5] LC_TIME=English_United States.utf8
    ##
    ## attached base packages:
    ## [1] stats4    stats     graphics  grDevices utils     datasets  methods
    ## [8] base
    ##
    ## other attached packages:
    ##  [1] SPIAT_1.0.4                 SpatialExperiment_1.8.0
    ##  [3] SingleCellExperiment_1.20.0 SummarizedExperiment_1.28.0
    ##  [5] Biobase_2.58.0              GenomicRanges_1.50.2
    ##  [7] GenomeInfoDb_1.34.4         IRanges_2.32.0
    ##  [9] S4Vectors_0.36.1            BiocGenerics_0.44.0
    ## [11] MatrixGenerics_1.10.0       matrixStats_0.63.0
    ## [13] BiocStyle_2.26.0
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] nlme_3.1-161              bitops_1.0-7
    ##  [3] spatstat.sparse_3.0-0     tools_4.2.2
    ##  [5] bslib_0.4.2               utf8_1.2.2
    ##  [7] R6_2.5.1                  HDF5Array_1.26.0
    ##  [9] DBI_1.1.3                 colorspace_2.0-3
    ## [11] rhdf5filters_1.10.0       withr_2.5.0
    ## [13] tidyselect_1.2.0          gridExtra_2.3
    ## [15] compiler_4.2.2            cli_3.5.0
    ## [17] spatstat.explore_3.0-5    DelayedArray_0.24.0
    ## [19] labeling_0.4.2            bookdown_0.31
    ## [21] sass_0.4.4                scales_1.2.1
    ## [23] spatstat.data_3.0-0       apcluster_1.4.10
    ## [25] goftest_1.2-3             stringr_1.5.0
    ## [27] digest_0.6.31             dbscan_1.1-11
    ## [29] spatstat.utils_3.0-1      rmarkdown_2.19
    ## [31] R.utils_2.12.2            XVector_0.38.0
    ## [33] pkgconfig_2.0.3           htmltools_0.5.4
    ## [35] sparseMatrixStats_1.10.0  fastmap_1.1.0
    ## [37] limma_3.54.0              highr_0.9
    ## [39] rlang_1.0.6               DelayedMatrixStats_1.20.0
    ## [41] farver_2.1.1              jquerylib_0.1.4
    ## [43] generics_0.1.3            jsonlite_1.8.4
    ## [45] gtools_3.9.4              spatstat.random_3.0-1
    ## [47] BiocParallel_1.32.4       dplyr_1.0.10
    ## [49] R.oo_1.25.0               RCurl_1.98-1.9
    ## [51] magrittr_2.0.3            GenomeInfoDbData_1.2.9
    ## [53] scuttle_1.8.3             Matrix_1.5-3
    ## [55] Rcpp_1.0.9                munsell_0.5.0
    ## [57] Rhdf5lib_1.20.0           fansi_1.0.3
    ## [59] abind_1.4-5               lifecycle_1.0.3
    ## [61] R.methodsS3_1.8.2         stringi_1.7.8
    ## [63] yaml_2.3.6                edgeR_3.40.1
    ## [65] zlibbioc_1.44.0           plyr_1.8.8
    ## [67] rhdf5_2.42.0              grid_4.2.2
    ## [69] parallel_4.2.2            dqrng_0.3.0
    ## [71] deldir_1.0-6              lattice_0.20-45
    ## [73] beachmat_2.14.0           tensor_1.5
    ## [75] locfit_1.5-9.6            magick_2.7.3
    ## [77] knitr_1.41                pillar_1.8.1
    ## [79] rjson_0.2.21              spatstat.geom_3.0-3
    ## [81] reshape2_1.4.4            codetools_0.2-18
    ## [83] glue_1.6.2                evaluate_0.19
    ## [85] BiocManager_1.30.19       vctrs_0.5.1
    ## [87] RANN_2.6.1                gtable_0.3.1
    ## [89] polyclip_1.10-4           assertthat_0.2.1
    ## [91] cachem_1.0.6              ggplot2_3.4.0
    ## [93] xfun_0.35                 DropletUtils_1.18.1
    ## [95] pracma_2.4.2              viridisLite_0.4.1
    ## [97] tibble_3.1.8

# 7 Author Contributions

AT, YF, TY, ML, JZ, VO, MD are authors of the package code. MD and YF wrote the vignette. AT, YF and TY designed the package.

Keren, Leeat, Marc Bosse, Diana Marquez, Roshan Angoshtari, Samir Jain, Sushama Varma, Soo-Ryum Yang, et al. 2018. “A Structured Tumor-Immune Microenvironment in Triple Negative Breast Cancer Revealed by Multiplexed Ion Beam Imaging.” Cell 174 (6): 1373–87.