Contents

library(stJoincount)
library(pheatmap)
library(ggplot2)

v1.1.1

stJoincount: Quantification tool for spatial correlation between clusters in spatial transcriptomics preprocessed data using join count statistic approach.

Introduction

Spatial dependency is the relationship between location and attribute similarity. The measure reflects whether an attribute of a variable observed at one location is independent of values observed at neighboring locations. Positive spatial dependency exists when neighboring attributes are more similar than what could be explained by chance. Likewise, a negative spatial dependency is reflected by a dissimilarity of neighboring attributes. Join count analysis allows for quantification of the spatial dependencies of nominal data in an arrangement of spatially adjacent polygons.

This tool requires data produced with the 10X Genomics Visium Spatial Gene Expression platform with customized clusters. The purpose of this R package is to perform join count analysis for spatial correlation between clusters.

Installation

Users can install stJoincount with:

if (!requireNamespace("BiocManager", quietly = TRUE)) {
      install.packages("BiocManager")
  }
BiocManager::install("stJoincount")

Examples of how to run this tool are below:

Preprocessing

In this vignette, we are going to use an human breast cancer spatial transcriptomics sample.

fpath <- system.file("extdata", "dataframe.rda", package="stJoincount")
load(fpath)
head(humanBC)
#>                    imagecol imagerow Cluster
#> AATTGCAGCAATCGAC-1 431.2129 476.8069       4
#> ACCAGGAGTGTGATCT-1 273.0446 117.8218       9
#> ACCTCCGCCCTCGCTG-1 448.2178 423.9109       7
#> AGGTGTATCGCCATGA-1 144.2822 317.5000       1
#> ATAGTTCCACCCACTC-1 431.5099 323.9109       7
#> CCGTATTAGCGCAGTT-1 462.1535 200.4950       2

Within the ‘extdata’ user can find a dataframe “humanBC.rda”. This example data is a data.frame that comes from a Seurat object of a human breast cancer sample. It contains the following information that is essential to this algorithm - barcode (index), cluster (they could either be categorical or numerical labels), central pixel location (imagerow and imagecol). This dataframe is simplified after combining metadata with spatial coordinates. The index contains barcodes, and at least three other columns that have these information are required and the column names should be the same as following: imagerow: The row pixel coordinate of the center of the spot imagecol: The column pixel coordinate of the center of the spot Cluster: The label that corresponding to this barcode

The following codes demonstrate how to generate the described data.frame from Seurat/spatialExperiment Objects.

An example data preparation from Seurat:

fpath <- system.file("extdata", "SeuratBC.rda", package="stJoincount")
load(fpath)
df <- dataPrepFromSeurat(seuratBC, "label")

An example data preparation from SpatialExperiment object:

fpath <- system.file("extdata", "SpeBC.rda", package="stJoincount")
load(fpath)
df2 <- dataPrepFromSpE(SpeObjBC, "label")

Raster processing

This tool first converts a labeled spatial tissue map into a raster object, in which each spatial feature is represented by a pixel coded by label assignment. This process includes automatic calculation of optimal raster resolution and extent for the sample.

resolutionList <- resolutionCalc(humanBC)
resolutionList
#> [1] 152.89604  64.20792
mosaicIntegration <- rasterizeEachCluster(humanBC)
#> No optimal number found, using n = 110 instead.
#> In this case, there may be minor deviations in the subsequent calculation process.
#>         The results are for reference only

Visualization

After the labeled spatial sample being converted, the raster map can be visualized by:

mosaicIntPlot(humanBC, mosaicIntegration)

Join count analysis

A neighbors list is then created from the rasterized sample, in which adjacent and diagonal neighbors for each pixel are identified. After adding binary spatial weights to the neighbors list, a multi-categorical join count analysis is performed to tabulate “joins” between all possible combinations of label pairs. The function returns the observed join counts, the expected count under conditions of spatial randomness, and the variance calculated under non-free sampling.

joincount.result <- joincountAnalysis(mosaicIntegration)

The z-score is then calculated as the difference between observed and expected counts, divided by the square root of the variance. A heatmap of z-scores represents the result from the join count analysis for all possible label pairs.

matrix <- zscoreMatrix(humanBC, joincount.result)
zscorePlot(matrix)

sessionInfo()
#> R Under development (unstable) (2023-10-22 r85388)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 22.04.3 LTS
#> 
#> Matrix products: default
#> BLAS:   /home/biocbuild/bbs-3.19-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_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [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] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] S4Vectors_0.41.0  ggplot2_3.4.4     pheatmap_1.0.12   stJoincount_1.5.0
#> [5] BiocStyle_2.31.0 
#> 
#> loaded via a namespace (and not attached):
#>   [1] RcppAnnoy_0.0.21            splines_4.4.0              
#>   [3] later_1.3.1                 bitops_1.0-7               
#>   [5] tibble_3.2.1                polyclip_1.10-6            
#>   [7] lifecycle_1.0.3             sf_1.0-14                  
#>   [9] globals_0.16.2              lattice_0.22-5             
#>  [11] MASS_7.3-60.1               magrittr_2.0.3             
#>  [13] plotly_4.10.3               sass_0.4.7                 
#>  [15] rmarkdown_2.25              jquerylib_0.1.4            
#>  [17] yaml_2.3.7                  httpuv_1.6.12              
#>  [19] Seurat_4.4.0                sctransform_0.4.1          
#>  [21] sp_2.1-1                    spatstat.sparse_3.0-3      
#>  [23] reticulate_1.34.0           cowplot_1.1.1              
#>  [25] pbapply_1.7-2               DBI_1.1.3                  
#>  [27] RColorBrewer_1.1-3          abind_1.4-5                
#>  [29] zlibbioc_1.49.0             Rtsne_0.16                 
#>  [31] GenomicRanges_1.55.0        purrr_1.0.2                
#>  [33] BiocGenerics_0.49.0         RCurl_1.98-1.12            
#>  [35] GenomeInfoDbData_1.2.11     IRanges_2.37.0             
#>  [37] ggrepel_0.9.4               irlba_2.3.5.1              
#>  [39] listenv_0.9.0               spatstat.utils_3.0-4       
#>  [41] terra_1.7-55                units_0.8-4                
#>  [43] goftest_1.2-3               spatstat.random_3.2-1      
#>  [45] fitdistrplus_1.1-11         parallelly_1.36.0          
#>  [47] leiden_0.4.3                codetools_0.2-19           
#>  [49] DelayedArray_0.29.0         tidyselect_1.2.0           
#>  [51] raster_3.6-26               farver_2.1.1               
#>  [53] matrixStats_1.0.0           stats4_4.4.0               
#>  [55] spatstat.explore_3.2-5      jsonlite_1.8.7             
#>  [57] e1071_1.7-13                ellipsis_0.3.2             
#>  [59] progressr_0.14.0            ggridges_0.5.4             
#>  [61] survival_3.5-7              tools_4.4.0                
#>  [63] ica_1.0-3                   Rcpp_1.0.11                
#>  [65] glue_1.6.2                  gridExtra_2.3              
#>  [67] SparseArray_1.3.0           xfun_0.40                  
#>  [69] MatrixGenerics_1.15.0       GenomeInfoDb_1.39.0        
#>  [71] dplyr_1.1.3                 withr_2.5.1                
#>  [73] BiocManager_1.30.22         fastmap_1.1.1              
#>  [75] boot_1.3-28.1               fansi_1.0.5                
#>  [77] spData_2.3.0                digest_0.6.33              
#>  [79] R6_2.5.1                    mime_0.12                  
#>  [81] colorspace_2.1-0            wk_0.9.0                   
#>  [83] scattermore_1.2             tensor_1.5                 
#>  [85] spatstat.data_3.0-3         utf8_1.2.4                 
#>  [87] tidyr_1.3.0                 generics_0.1.3             
#>  [89] data.table_1.14.8           class_7.3-22               
#>  [91] httr_1.4.7                  htmlwidgets_1.6.2          
#>  [93] S4Arrays_1.3.0              spdep_1.2-8                
#>  [95] uwot_0.1.16                 pkgconfig_2.0.3            
#>  [97] gtable_0.3.4                lmtest_0.9-40              
#>  [99] SingleCellExperiment_1.25.0 XVector_0.43.0             
#> [101] htmltools_0.5.6.1           bookdown_0.36              
#> [103] SeuratObject_4.1.4          scales_1.2.1               
#> [105] Biobase_2.63.0              png_0.1-8                  
#> [107] SpatialExperiment_1.13.0    knitr_1.44                 
#> [109] reshape2_1.4.4              rjson_0.2.21               
#> [111] nlme_3.1-163                proxy_0.4-27               
#> [113] cachem_1.0.8                zoo_1.8-12                 
#> [115] stringr_1.5.0               KernSmooth_2.23-22         
#> [117] parallel_4.4.0              miniUI_0.1.1.1             
#> [119] s2_1.1.4                    pillar_1.9.0               
#> [121] grid_4.4.0                  vctrs_0.6.4                
#> [123] RANN_2.6.1                  promises_1.2.1             
#> [125] xtable_1.8-4                cluster_2.1.4              
#> [127] evaluate_0.22               magick_2.8.1               
#> [129] cli_3.6.1                   compiler_4.4.0             
#> [131] rlang_1.1.1                 crayon_1.5.2               
#> [133] future.apply_1.11.0         labeling_0.4.3             
#> [135] classInt_0.4-10             plyr_1.8.9                 
#> [137] stringi_1.7.12              viridisLite_0.4.2          
#> [139] deldir_1.0-9                munsell_0.5.0              
#> [141] lazyeval_0.2.2              spatstat.geom_3.2-7        
#> [143] Matrix_1.6-1.1              patchwork_1.1.3            
#> [145] future_1.33.0               shiny_1.7.5.1              
#> [147] SummarizedExperiment_1.33.0 ROCR_1.0-11                
#> [149] igraph_1.5.1                bslib_0.5.1