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")
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.
## [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.
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"
## [1] 3
There are 3 tumour clusters in the image.
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.
## [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.
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.
## R version 4.4.2 (2024-10-31)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.1 LTS
##
## Matrix products: default
## BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so; LAPACK version 3.12.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 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: Etc/UTC
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] SPIAT_1.9.0 SpatialExperiment_1.17.0
## [3] SingleCellExperiment_1.29.1 SummarizedExperiment_1.37.0
## [5] Biobase_2.67.0 GenomicRanges_1.59.1
## [7] GenomeInfoDb_1.43.2 IRanges_2.41.2
## [9] S4Vectors_0.45.2 BiocGenerics_0.53.3
## [11] generics_0.1.3 MatrixGenerics_1.19.0
## [13] matrixStats_1.4.1 BiocStyle_2.35.0
##
## loaded via a namespace (and not attached):
## [1] RColorBrewer_1.1-3 sys_3.4.3 jsonlite_1.8.9
## [4] shape_1.4.6.1 magrittr_2.0.3 spatstat.utils_3.1-1
## [7] magick_2.8.5 farver_2.1.2 rmarkdown_2.29
## [10] GlobalOptions_0.1.2 zlibbioc_1.52.0 vctrs_0.6.5
## [13] spatstat.explore_3.3-3 elsa_1.1-28 terra_1.8-5
## [16] htmltools_0.5.8.1 S4Arrays_1.7.1 raster_3.6-30
## [19] SparseArray_1.7.2 sass_0.4.9 pracma_2.4.4
## [22] bslib_0.8.0 htmlwidgets_1.6.4 mmand_1.6.3
## [25] plyr_1.8.9 plotly_4.10.4 cachem_1.1.0
## [28] buildtools_1.0.0 lifecycle_1.0.4 iterators_1.0.14
## [31] pkgconfig_2.0.3 Matrix_1.7-1 R6_2.5.1
## [34] fastmap_1.2.0 GenomeInfoDbData_1.2.13 clue_0.3-66
## [37] digest_0.6.37 colorspace_2.1-1 tensor_1.5
## [40] crosstalk_1.2.1 labeling_0.4.3 spatstat.sparse_3.1-0
## [43] httr_1.4.7 polyclip_1.10-7 abind_1.4-8
## [46] compiler_4.4.2 splancs_2.01-45 bit64_4.5.2
## [49] withr_3.0.2 doParallel_1.0.17 apcluster_1.4.13
## [52] R.utils_2.12.3 DelayedArray_0.33.3 rjson_0.2.23
## [55] gtools_3.9.5 tools_4.4.2 goftest_1.2-3
## [58] R.oo_1.27.0 glue_1.8.0 dbscan_1.2-0
## [61] nlme_3.1-166 grid_4.4.2 Rtsne_0.17
## [64] cluster_2.1.8 reshape2_1.4.4 gtable_0.3.6
## [67] spatstat.data_3.1-4 tzdb_0.4.0 R.methodsS3_1.8.2
## [70] tidyr_1.3.1 data.table_1.16.4 sp_2.1-4
## [73] XVector_0.47.0 spatstat.geom_3.3-4 ggrepel_0.9.6
## [76] RANN_2.6.2 foreach_1.5.2 pillar_1.10.0
## [79] stringr_1.5.1 vroom_1.6.5 circlize_0.4.16
## [82] dplyr_1.1.4 lattice_0.22-6 bit_4.5.0.1
## [85] deldir_2.0-4 tidyselect_1.2.1 ComplexHeatmap_2.23.0
## [88] maketools_1.3.1 knitr_1.49 gridExtra_2.3
## [91] xfun_0.49 dittoSeq_1.19.0 pheatmap_1.0.12
## [94] stringi_1.8.4 UCSC.utils_1.3.0 lazyeval_0.2.2
## [97] yaml_2.3.10 evaluate_1.0.1 codetools_0.2-20
## [100] interp_1.1-6 sgeostat_1.0-27 tibble_3.2.1
## [103] BiocManager_1.30.25 cli_3.6.3 alphahull_2.5
## [106] munsell_0.5.1 jquerylib_0.1.4 Rcpp_1.0.13-1
## [109] spatstat.random_3.3-2 png_0.1-8 spatstat.univar_3.1-1
## [112] parallel_4.4.2 ggplot2_3.5.1 viridisLite_0.4.2
## [115] scales_1.3.0 ggridges_0.5.6 purrr_1.0.2
## [118] crayon_1.5.3 GetoptLong_1.0.5 rlang_1.1.4
## [121] cowplot_1.1.3