# Characterising tissue structure with SPIAT

Yuzhou Feng

#### 2023-05-24

#### Package

SPIAT 1.2.2

    library(SPIAT)

# 1 Characterising the distribution of the cells of interest in identified tissue regions

In certain analysis the focus is to understand the spatial distribution of a certain type of cell populations relative to the tissue regions.

One example of this functionality is to characterise the immune population in tumour structures. The following analysis will focus on the tumour/immune example, including determining whether there is a clear tumour margin, automatically identifying the tumour margin, and finally quantifying the proportion of immune populations relative to the margin. However, these analyses can also be generalised to other tissue and cell types.

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.1 Determining whether there is a clear tumour margin

In some instances tumour cells are distributed in such a way that there are no clear tumour margins. While this can be derived intuitively in most cases, SPIAT offers a way of quantifying the ‘quality’ of the margin for downstream analyses. This is meant to be used to help flag images with relatively poor margins, and therefore we do not offer a cutoff value.

To determine if there is a clear tumour margin, SPIAT can calculate the ratio of tumour bordering cells to tumour total cells (R-BT). This ratio is high when there is a disproportional high number of tumour margin cells compared to internal tumour cells.

    R_BC(formatted_image, cell_type_of_interest = "Tumour", "Cell.Type")

    ## [1] 0.2014652

The result is 0.2014652. This low value means there are relatively low number of bordering cells compared to total tumour cells, meaning that this image has clear tumour margins.

## 1.2 Automatic identification of the tumour margin

We can identify margins with `identify_bordering_cells()`. This function leverages off the alpha hull method (Pateiro-Lopez, Rodriguez-Casal, and. 2019) from the alpha hull package. Here we use tumour cells (Tumour_marker) as the reference to identify the bordering cells but any cell type can be used.

    formatted_border <- identify_bordering_cells(formatted_image,
                                                 reference_cell = "Tumour",
                                                 feature_colname="Cell.Type")

    ## [1] "The alpha of Polygon is: 63.24375"

    # Get the number of tumour clusters
    attr(formatted_border, "n_of_clusters")

    ## [1] 3

There are 3 tumour clusters in the image.

## 1.3 Classification of cells based on their locations relative to the margin

We can then define four locations relative to the margin based on distances: “Internal margin”, “External margin”, “Outside” and “Inside”. Specifically, we define the area within a specified distance to the margin as either “Internal margin” (bordering the margin, inside the tumour area) and “External margin” (bordering the margin, surrounding the tumour area). The areas located further away than the specified distance from the margin are defined as “Inside” (i.e. the tumour area) and “Outside” (i.e. the tumour area).

First, we calculate the distance of cells to the tumour margin.

    formatted_distance <- calculate_distance_to_margin(formatted_border)

    ## [1] "Markers had been selected in minimum distance calculation: "
    ## [1] "Non-border" "Border"

Next, we classify cells based on their location. As a distance cutoff, we use a distance of 5 cells from the tumour margin. The function first calculates the average minimum distance between all pairs of nearest cells and then multiples this number by 5. Users can change the number of cell layers to increase/decrease the margin width.

    names_of_immune_cells <- c("Immune1", "Immune2","Immune3")

    formatted_structure <- define_structure(
      formatted_distance, cell_types_of_interest = names_of_immune_cells,
      feature_colname = "Cell.Type", n_margin_layers = 5)

    categories <- unique(formatted_structure$Structure)

We can plot and colour these structure categories.

    plot_cell_categories(formatted_structure, feature_colname = "Structure")

We can also calculate the proportions of immune cells in each of the locations.

    immune_proportions <- calculate_proportions_of_cells_in_structure(
      spe_object = formatted_structure,
      cell_types_of_interest = names_of_immune_cells, feature_colname ="Cell.Type")

    immune_proportions

    ##                Cell.Type                            Relative_to
    ## 1                Immune1             All_cells_in_the_structure
    ## 2                Immune2             All_cells_in_the_structure
    ## 3                Immune3             All_cells_in_the_structure
    ## 4                Immune1 All_cells_of_interest_in_the_structure
    ## 5                Immune2 All_cells_of_interest_in_the_structure
    ## 6                Immune3 All_cells_of_interest_in_the_structure
    ## 7                Immune1  The_same_cell_type_in_the_whole_image
    ## 8                Immune2  The_same_cell_type_in_the_whole_image
    ## 9                Immune3  The_same_cell_type_in_the_whole_image
    ## 10 All_cells_of_interest             All_cells_in_the_structure
    ##    P.Infiltrated.CoI P.Internal.Margin.CoI P.External.Margin.CoI P.Stromal.CoI
    ## 1         0.00000000            0.00000000           0.001733102    0.09658928
    ## 2         0.00000000            0.00000000           0.001733102    0.05073087
    ## 3         0.12576687            0.08071749           0.681109185    0.04585841
    ## 4         0.00000000            0.00000000           0.002531646    0.50000000
    ## 5         0.00000000            0.00000000           0.002531646    0.26261128
    ## 6         1.00000000            1.00000000           0.994936709    0.23738872
    ## 7         0.00000000            0.00000000           0.002958580    0.99704142
    ## 8         0.00000000            0.00000000           0.005617978    0.99438202
    ## 9         0.06507937            0.05714286           0.623809524    0.25396825
    ## 10        0.12576687            0.08071749           0.684575390    0.19317856

Finally, we can calculate summaries of the distances for immune cells in the tumour structure.

    immune_distances <- calculate_summary_distances_of_cells_to_borders(
      spe_object = formatted_structure,
      cell_types_of_interest = names_of_immune_cells, feature_colname = "Cell.Type")

    immune_distances

    ##                    Cell.Type               Area    Min_d    Max_d    Mean_d
    ## 1 All_cell_types_of_interest Within_border_area 10.93225 192.4094  86.20042
    ## 2 All_cell_types_of_interest             Stroma 10.02387 984.0509 218.11106
    ## 3                    Immune1 Within_border_area       NA       NA        NA
    ## 4                    Immune1             Stroma 84.20018 970.7749 346.14096
    ## 5                    Immune2 Within_border_area       NA       NA        NA
    ## 6                    Immune2             Stroma 79.42753 984.0509 333.26374
    ## 7                    Immune3 Within_border_area 10.93225 192.4094  86.20042
    ## 8                    Immune3             Stroma 10.02387 971.5638 102.79227
    ##    Median_d  St.dev_d
    ## 1  88.23299  45.27414
    ## 2 133.18220 199.87586
    ## 3        NA        NA
    ## 4 301.01535 187.04247
    ## 5        NA        NA
    ## 6 284.09062 185.67518
    ## 7  88.23299  45.27414
    ## 8  68.19218 131.32714

Note that for cell types that are not present in a tumour structure, there will be NAs in the results.

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

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

# 3 Reproducibility

    sessionInfo()

    ## R version 4.3.0 RC (2023-04-13 r84269 ucrt)
    ## Platform: x86_64-w64-mingw32/x64 (64-bit)
    ## Running under: Windows Server 2022 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
    ##
    ## time zone: America/New_York
    ## tzcode source: internal
    ##
    ## attached base packages:
    ## [1] stats4    stats     graphics  grDevices utils     datasets  methods
    ## [8] base
    ##
    ## other attached packages:
    ##  [1] SPIAT_1.2.2                 SpatialExperiment_1.10.0
    ##  [3] SingleCellExperiment_1.22.0 SummarizedExperiment_1.30.1
    ##  [5] Biobase_2.60.0              GenomicRanges_1.52.0
    ##  [7] GenomeInfoDb_1.36.0         IRanges_2.34.0
    ##  [9] S4Vectors_0.38.1            BiocGenerics_0.46.0
    ## [11] MatrixGenerics_1.12.0       matrixStats_0.63.0
    ## [13] BiocStyle_2.28.0
    ##
    ## loaded via a namespace (and not attached):
    ##   [1] RColorBrewer_1.1-3        shape_1.4.6
    ##   [3] jsonlite_1.8.4            magrittr_2.0.3
    ##   [5] spatstat.utils_3.0-3      magick_2.7.4
    ##   [7] farver_2.1.1              rmarkdown_2.21
    ##   [9] GlobalOptions_0.1.2       zlibbioc_1.46.0
    ##  [11] vctrs_0.6.2               Cairo_1.6-0
    ##  [13] spatstat.explore_3.2-1    DelayedMatrixStats_1.22.0
    ##  [15] RCurl_1.98-1.12           elsa_1.1-28
    ##  [17] terra_1.7-29              htmltools_0.5.5
    ##  [19] S4Arrays_1.0.4            raster_3.6-20
    ##  [21] Rhdf5lib_1.22.0           rhdf5_2.44.0
    ##  [23] sass_0.4.6                pracma_2.4.2
    ##  [25] bslib_0.4.2               htmlwidgets_1.6.2
    ##  [27] mmand_1.6.3               plyr_1.8.8
    ##  [29] plotly_4.10.1             cachem_1.0.8
    ##  [31] lifecycle_1.0.3           iterators_1.0.14
    ##  [33] pkgconfig_2.0.3           Matrix_1.5-4.1
    ##  [35] R6_2.5.1                  fastmap_1.1.1
    ##  [37] clue_0.3-64               GenomeInfoDbData_1.2.10
    ##  [39] digest_0.6.31             colorspace_2.1-0
    ##  [41] tensor_1.5                dqrng_0.3.0
    ##  [43] crosstalk_1.2.0           beachmat_2.16.0
    ##  [45] labeling_0.4.2            fansi_1.0.4
    ##  [47] spatstat.sparse_3.0-1     httr_1.4.6
    ##  [49] polyclip_1.10-4           abind_1.4-5
    ##  [51] compiler_4.3.0            splancs_2.01-43
    ##  [53] bit64_4.0.5               withr_2.5.0
    ##  [55] doParallel_1.0.17         BiocParallel_1.34.2
    ##  [57] apcluster_1.4.10          highr_0.10
    ##  [59] HDF5Array_1.28.1          R.utils_2.12.2
    ##  [61] DelayedArray_0.26.3       rjson_0.2.21
    ##  [63] gtools_3.9.4              tools_4.3.0
    ##  [65] goftest_1.2-3             R.oo_1.25.0
    ##  [67] glue_1.6.2                dbscan_1.1-11
    ##  [69] nlme_3.1-162              rhdf5filters_1.12.1
    ##  [71] grid_4.3.0                Rtsne_0.16
    ##  [73] cluster_2.1.4             reshape2_1.4.4
    ##  [75] generics_0.1.3            gtable_0.3.3
    ##  [77] spatstat.data_3.0-1       tzdb_0.4.0
    ##  [79] R.methodsS3_1.8.2         tidyr_1.3.0
    ##  [81] data.table_1.14.8         sp_1.6-0
    ##  [83] utf8_1.2.3                XVector_0.40.0
    ##  [85] spatstat.geom_3.2-1       ggrepel_0.9.3
    ##  [87] RANN_2.6.1                foreach_1.5.2
    ##  [89] pillar_1.9.0              stringr_1.5.0
    ##  [91] vroom_1.6.3               limma_3.56.1
    ##  [93] circlize_0.4.15           dplyr_1.1.2
    ##  [95] lattice_0.21-8            bit_4.0.5
    ##  [97] deldir_1.0-9              tidyselect_1.2.0
    ##  [99] ComplexHeatmap_2.16.0     locfit_1.5-9.7
    ## [101] scuttle_1.10.1            knitr_1.42
    ## [103] gridExtra_2.3             bookdown_0.34
    ## [105] edgeR_3.42.2              xfun_0.39
    ## [107] dittoSeq_1.12.0           DropletUtils_1.20.0
    ## [109] pheatmap_1.0.12           stringi_1.7.12
    ## [111] lazyeval_0.2.2            yaml_2.3.7
    ## [113] evaluate_0.21             codetools_0.2-19
    ## [115] interp_1.1-4              sgeostat_1.0-27
    ## [117] archive_1.1.5             tibble_3.2.1
    ## [119] BiocManager_1.30.20       cli_3.6.1
    ## [121] alphahull_2.5             munsell_0.5.0
    ## [123] jquerylib_0.1.4           Rcpp_1.0.10
    ## [125] spatstat.random_3.1-5     png_0.1-8
    ## [127] parallel_4.3.0            ggplot2_3.4.2
    ## [129] sparseMatrixStats_1.12.0  bitops_1.0-7
    ## [131] viridisLite_0.4.2         scales_1.2.1
    ## [133] ggridges_0.5.4            purrr_1.0.1
    ## [135] crayon_1.5.2              GetoptLong_1.0.5
    ## [137] rlang_1.1.1               cowplot_1.1.1

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

Pateiro-Lopez, Beatriz, Alberto Rodriguez-Casal, and. 2019. Alphahull: Generalization of the Convex Hull of a Sample of Points in the Plane. https://CRAN.R-project.org/package=alphahull.