Skip to contents

Introduction

Local Geary’s C (Anselin 1995) is defined as:

\[ c_i = \sum_jw_{ij}(x_i - x_j)^2, \]

where \(w_{ij}\)s are spatial weights from location \(i\) to location \(j\) and \(x\) is a variable at spatial location. This is generalized to multiple variables in (Anselin 2019):

\[ c_{k,i} = \sum_{v=1}^k c_{v,i}, \]

where there are \(k\) variables. This is essentially a spatially weighted sum of squared distances between locations in feature space. This vignette demonstrates usage of multivariate local Geary’s C.

Here we load the packages used:

QC was performed in another vignette, so this vignette will not plot QC metrics.

(sfe <- McKellarMuscleData("full"))
#> see ?SFEData and browseVignettes('SFEData') for documentation
#> loading from cache
#> class: SpatialFeatureExperiment 
#> dim: 15123 4992 
#> metadata(0):
#> assays(1): counts
#> rownames(15123): ENSMUSG00000025902 ENSMUSG00000096126 ...
#>   ENSMUSG00000064368 ENSMUSG00000064370
#> rowData names(6): Ensembl symbol ... vars cv2
#> colnames(4992): AAACAACGAATAGTTC AAACAAGTATCTCCCA ... TTGTTTGTATTACACG
#>   TTGTTTGTGTAAATTC
#> colData names(12): barcode col ... prop_mito in_tissue
#> reducedDimNames(0):
#> mainExpName: NULL
#> altExpNames(0):
#> spatialCoords names(2) : imageX imageY
#> imgData names(1): sample_id
#> 
#> unit: full_res_image_pixels
#> Geometries:
#> colGeometries: spotPoly (POLYGON) 
#> annotGeometries: tissueBoundary (POLYGON), myofiber_full (POLYGON), myofiber_simplified (POLYGON), nuclei (POLYGON), nuclei_centroid (POINT) 
#> 
#> Graphs:
#> Vis5A:

The image can be added to the SFE object and plotted behind the geometries, and needs to be flipped to align to the spots because the origin is at the top left for the image but bottom left for geometries.

if (!file.exists("tissue_lowres_5a.jpeg")) {
    download.file("https://raw.githubusercontent.com/pachterlab/voyager/main/vignettes/tissue_lowres_5a.jpeg",
                  destfile = "tissue_lowres_5a.jpeg")
}
sfe <- addImg(sfe, file = "tissue_lowres_5a.jpeg", sample_id = "Vis5A", 
              image_id = "lowres", 
              scale_fct = 1024/22208)
sfe <- mirrorImg(sfe, sample_id = "Vis5A", image_id = "lowres")
sfe_tissue <- sfe[,colData(sfe)$in_tissue]
sfe_tissue <- sfe_tissue[rowSums(counts(sfe_tissue)) > 0,]
sfe_tissue <- logNormCounts(sfe_tissue)
colGraph(sfe_tissue, "visium") <- findVisiumGraph(sfe_tissue)

Gene expression

Here we compute multivariate local C for top highly variagle genes (HVGs) in this dataset:

hvgs <- getTopHVGs(sfe_tissue, fdr.threshold = 0.01)
sfe_tissue <- runMultivariate(sfe_tissue, "localC_perm_multi", subset_row = hvgs)

The results are stored in reducedDim although it’s not really a dimension reduction. It can also go into colData if dest = "colData". The test is two sided, but the alternative argument can be set to “greater” to only test for positive spatial autocorrelation and “less” for negative spatial autocorrelation.

names(reducedDim(sfe_tissue, "localC_perm_multi"))
#>  [1] "localC_perm_multi"  "E.Ci"               "Var.Ci"            
#>  [4] "Z.Ci"               "Pr(z != E(Ci))"     "Pr(z != E(Ci)) Sim"
#>  [7] "Pr(folded) Sim"     "Skewness"           "Kurtosis"          
#> [10] "-log10p Sim"        "-log10p_adj Sim"    "cluster"
spatialReducedDim(sfe_tissue, "localC_perm_multi", c(1, 12),
                  image_id = "lowres", maxcell = 5e4)

In Geary’s C, a value below 1 indicates positive spatial autocorrelation and a value above 1 indicates negative spatial autocorrelation. Local Geary’s C is not scaled, but from the square difference expression, a low value means a more homogeneous neighborhood and a high value means a more heterogeneous neighborhood. Here considering all 341 top HVGs, the muscle tendon junction and the unjury site are more heterogeneous, which is detected as negative cluster.

Permutation testing was performed, although Anselin noted that the pseudo-p-values should only be taken as indicative of interesting regions and should not be interpreted in a strict sense.

spatialReducedDim(sfe_tissue, "localC_perm_multi", c(11, 12),
                  image_id = "lowres", maxcell = 5e4, 
                  divergent = TRUE, diverge_center = -log10(0.05))

Warm colors indicate adjusted p < 0.05. This should be interpreted along with the clusters. In this dataset, there are interestingly homogeneous regions in the myofibers, and an interestingly heterogeneous region in the injury site. Most of the significant regions are positive cluster, but the center of the injury site is significant and is negative cluster.

Top principal components

Because multivariate local Geary’s C is a spatially weighted sum of squared distances between locations in feature space, it’s affected by the curse of dimensionality when used on a large number of features, when uniformly distributed data points in higher dimensions become more equidistant to each other with increasing number of dimensions. However, real data is not uniformly distributed and can have a much smaller effective dimension than the number of features, as many genes are co-regulated. Anselin suggested using the main principal components, but the issue of curse of dimensionality remains to be further investigated. Furthermore, as the cosine and Manhattan distances have been suggested to mitigate curse of dimensionality, I wonder what if I use these instead of the Euclidean distance in feature space for multivariate local Geary’s C.

So here we perform multivariate local Geary’s C on the top PCs:

sfe_tissue <- runPCA(sfe_tissue, ncomponents = 20, scale = TRUE)
ElbowPlot(sfe_tissue)

What percentage of variance is explained by the top 20 PCs?

sum(attr(reducedDim(sfe_tissue, "PCA"), "percentVar"))
#> [1] 38.8627
out <- localC_perm(reducedDim(sfe_tissue, "PCA"), 
                   listw = colGraph(sfe_tissue, "visium"))
out <- Voyager:::.localCpermmulti2df(out, 
                                     nb = colGraph(sfe_tissue, "visium")$neighbours,
                                     p.adjust.method = "BH")
reducedDim(sfe_tissue, "localC_PCs", withDimnames = FALSE) <- out
spatialReducedDim(sfe_tissue, "localC_PCs", c(1, 12),
                  image_id = "lowres", maxcell = 5e4)

spatialReducedDim(sfe_tissue, "localC_PCs", c(11, 12),
                  image_id = "lowres", maxcell = 5e4, 
                  divergent = TRUE, diverge_center = -log10(0.05))

The area that seem significant from the permutation test is larger than that from the HVGs, and the area considered negative clusters is smaller. The significant regions are pretty much all positive cluster. Do the differences in results have anything to do with curse of dimensionality? Twenty dimensions can still exhibit curse of dimensionality, but over 300 HVGs here would be worse. Or is it that we lose a lot of information, including negative spatial autocorrelation, by only using 20 PCs?

Session info

sessionInfo()
#> R version 4.3.2 (2023-10-31)
#> Platform: x86_64-apple-darwin20 (64-bit)
#> Running under: macOS Ventura 13.6
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRblas.0.dylib 
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> time zone: UTC
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats4    stats     graphics  grDevices utils     datasets  methods  
#> [8] base     
#> 
#> other attached packages:
#>  [1] spdep_1.3-1                    sf_1.0-14                     
#>  [3] spData_2.3.0                   scran_1.30.0                  
#>  [5] scater_1.30.0                  ggplot2_3.4.4                 
#>  [7] scuttle_1.12.0                 SingleCellExperiment_1.24.0   
#>  [9] SummarizedExperiment_1.32.0    Biobase_2.62.0                
#> [11] GenomicRanges_1.54.1           GenomeInfoDb_1.38.1           
#> [13] IRanges_2.36.0                 S4Vectors_0.40.2              
#> [15] BiocGenerics_0.48.1            MatrixGenerics_1.14.0         
#> [17] matrixStats_1.1.0              SpatialFeatureExperiment_1.3.0
#> [19] SFEData_1.4.0                  Voyager_1.4.0                 
#> 
#> loaded via a namespace (and not attached):
#>   [1] later_1.3.1                   bitops_1.0-7                 
#>   [3] filelock_1.0.2                tibble_3.2.1                 
#>   [5] lifecycle_1.0.4               edgeR_4.0.2                  
#>   [7] rprojroot_2.0.4               lattice_0.22-5               
#>   [9] magrittr_2.0.3                limma_3.58.1                 
#>  [11] sass_0.4.7                    rmarkdown_2.25               
#>  [13] jquerylib_0.1.4               yaml_2.3.7                   
#>  [15] metapod_1.10.0                httpuv_1.6.12                
#>  [17] sp_2.1-2                      RColorBrewer_1.1-3           
#>  [19] DBI_1.1.3                     abind_1.4-5                  
#>  [21] zlibbioc_1.48.0               purrr_1.0.2                  
#>  [23] RCurl_1.98-1.13               rappdirs_0.3.3               
#>  [25] GenomeInfoDbData_1.2.11       ggrepel_0.9.4                
#>  [27] irlba_2.3.5.1                 terra_1.7-55                 
#>  [29] units_0.8-4                   RSpectra_0.16-1              
#>  [31] dqrng_0.3.1                   pkgdown_2.0.7                
#>  [33] DelayedMatrixStats_1.24.0     codetools_0.2-19             
#>  [35] DelayedArray_0.28.0           tidyselect_1.2.0             
#>  [37] farver_2.1.1                  ScaledMatrix_1.10.0          
#>  [39] viridis_0.6.4                 BiocFileCache_2.10.1         
#>  [41] jsonlite_1.8.7                BiocNeighbors_1.20.0         
#>  [43] e1071_1.7-13                  ellipsis_0.3.2               
#>  [45] systemfonts_1.0.5             dbscan_1.1-11                
#>  [47] tools_4.3.2                   ggnewscale_0.4.9             
#>  [49] ragg_1.2.6                    Rcpp_1.0.11                  
#>  [51] glue_1.6.2                    gridExtra_2.3                
#>  [53] SparseArray_1.2.2             xfun_0.41                    
#>  [55] dplyr_1.1.4                   HDF5Array_1.30.0             
#>  [57] withr_2.5.2                   BiocManager_1.30.22          
#>  [59] fastmap_1.1.1                 boot_1.3-28.1                
#>  [61] rhdf5filters_1.14.1           bluster_1.12.0               
#>  [63] fansi_1.0.5                   digest_0.6.33                
#>  [65] rsvd_1.0.5                    R6_2.5.1                     
#>  [67] mime_0.12                     textshaping_0.3.7            
#>  [69] colorspace_2.1-0              wk_0.9.0                     
#>  [71] RSQLite_2.3.3                 utf8_1.2.4                   
#>  [73] generics_0.1.3                class_7.3-22                 
#>  [75] httr_1.4.7                    S4Arrays_1.2.0               
#>  [77] pkgconfig_2.0.3               scico_1.5.0                  
#>  [79] gtable_0.3.4                  blob_1.2.4                   
#>  [81] XVector_0.42.0                htmltools_0.5.7              
#>  [83] scales_1.2.1                  png_0.1-8                    
#>  [85] SpatialExperiment_1.12.0      knitr_1.45                   
#>  [87] rjson_0.2.21                  curl_5.1.0                   
#>  [89] proxy_0.4-27                  cachem_1.0.8                 
#>  [91] rhdf5_2.46.0                  stringr_1.5.1                
#>  [93] BiocVersion_3.18.1            KernSmooth_2.23-22           
#>  [95] parallel_4.3.2                vipor_0.4.5                  
#>  [97] AnnotationDbi_1.64.1          desc_1.4.2                   
#>  [99] s2_1.1.4                      pillar_1.9.0                 
#> [101] grid_4.3.2                    vctrs_0.6.4                  
#> [103] promises_1.2.1                BiocSingular_1.18.0          
#> [105] dbplyr_2.4.0                  beachmat_2.18.0              
#> [107] xtable_1.8-4                  cluster_2.1.4                
#> [109] beeswarm_0.4.0                evaluate_0.23                
#> [111] magick_2.8.1                  cli_3.6.1                    
#> [113] locfit_1.5-9.8                compiler_4.3.2               
#> [115] rlang_1.1.2                   crayon_1.5.2                 
#> [117] labeling_0.4.3                classInt_0.4-10              
#> [119] fs_1.6.3                      ggbeeswarm_0.7.2             
#> [121] stringi_1.8.2                 viridisLite_0.4.2            
#> [123] deldir_2.0-2                  BiocParallel_1.36.0          
#> [125] munsell_0.5.0                 Biostrings_2.70.1            
#> [127] Matrix_1.6-3                  ExperimentHub_2.10.0         
#> [129] patchwork_1.1.3               sparseMatrixStats_1.14.0     
#> [131] bit64_4.0.5                   Rhdf5lib_1.24.0              
#> [133] KEGGREST_1.42.0               statmod_1.5.0                
#> [135] shiny_1.8.0                   highr_0.10                   
#> [137] interactiveDisplayBase_1.40.0 AnnotationHub_3.10.0         
#> [139] igraph_1.5.1                  memoise_2.0.1                
#> [141] bslib_0.6.0                   bit_4.0.5

References

Anselin, Luc. 1995. “Local Indicators of Spatial Association—LISA.” Geogr. Anal. 27 (2): 93–115.
———. 2019. “A Local Indicator of Multivariate Spatial Association: Extending Geary’s c.” Geogr. Anal. 51 (2): 133–50.