Skip to contents

In this notebook, we’ll have a look at how you can use spatial methods in semla. we will continue with the same Seurat object that we created in the ‘radial distances’ tutorial. If you haven’t downloaded and processed the data yet, you should go back to that tutorial before continuing with the analysis presented here.

Region neighbors

In the previous tutorial we calculated radial distances from the center of a tumor region. These distances can easily be used to define a ‘tumor microenvironment’ as well as the leading edge of the tumor.

If you are only interested in finding spots that are located at the border of a region of interest, you can use the RegionNeighbors() function instead. RegionNeighbors automatically detects spots that are located inside or outside the border or both.

Let’s demonstrate how it works with an example. If we want to find the spots that are neighbors to cluster 3, we set column_name = "seurat_clusters" and column_labels = "3":

se <- RegionNeighbors(se, 
                      column_name = "seurat_clusters", 
                      column_labels = "3")
##  Finding neighboring spots for '3'
## →   Excluding neighbors from the same group
## →   169 neighbors left
## →   Returning neighbors

In our meta.data slot, we now have a new column called “nb_to_3”, short for “neighbors to cluster 3”. In the plot below, you can see that this column provides information about which spots are outside the border of cluster 3.

MapLabels(se, crop_area = c(0.45, 0.6, 0.8, 0.93),
          column_name = "nb_to_3", drop_na = TRUE,
          image_use = "raw", pt_size = 3)

We can tweak the function to identify the inner border instead, i.e. the spots that are located at the “tumor edge”. When we run RegionNeighbors with mode="inner", we get a column called inner_border_3 which tells us what spots are on the “tumor edge”.

se <- RegionNeighbors(se, 
                      column_name = "seurat_clusters", 
                      column_labels = "3", 
                      mode = "inner")
##  Finding neighboring spots for '3'
## →   Returning neighbors
MapLabels(se, 
          crop_area = c(0.45, 0.6, 0.8, 0.93),
          column_name = "inner_border_3",
          image_use = "raw", 
          pt_size = 3, 
          drop_na = TRUE)

If we want to get both the inner and outer borders, we set mode="inner_outer.

NB: If we run a mode that returns “outer” border spots, the prefix of the returned column will be “nb_to_” by default. This behavior can be overridden by setting the column_key=...

se <- RegionNeighbors(se, column_name = "seurat_clusters", 
                      column_labels = "3", mode = "inner_outer")
##  Finding neighboring spots for '3'
## →   Returning neighbors
MapLabels(se, crop_area = c(0.45, 0.6, 0.8, 0.93),
          column_name = "nb_to_3",
          image_use = "raw", pt_size = 3, drop_na = TRUE)

The last mode (mode="all_inner_outer") allows us to select the outer border spots while keeping all spots belonging to our cluster of interest.

se <- RegionNeighbors(se, column_name = "seurat_clusters", column_key = "(all)nb_to_",
                      column_labels = "3", mode = "all_inner_outer")
##  Finding neighboring spots for '3'
## →   Returning neighbors
MapLabels(se, crop_area = c(0.45, 0.6, 0.8, 0.93),
          column_name = "(all)nb_to_3",
          image_use = "raw", pt_size = 3, drop_na = TRUE)

One example of how this information can be used is to run a DE test between the outer and inner borders to find genes that are differentially expressed in spots just outside the “tumor edge”:

border_markers <- FindMarkers(se, ident.1 = "nb_to_3", 
                              ident.2 = "3", group.by = "nb_to_3")

# Filter results
border_markers_up <- border_markers |> 
  filter(p_val_adj < 0.01, avg_log2FC > 0) |> 
  arrange(-avg_log2FC) |> 
  slice_head(n = 10)

# Subset Seurat object to include border spots
se_border_spots <- SubsetSTData(se, expression = nb_to_3 %in% c("3", "nb_to_3"))

# Violin plot
VlnPlot(se_border_spots, features = rownames(border_markers_up), group.by = "nb_to_3") &
  theme(axis.title = element_blank())

If you want to select a wider band of spots surrounding a ROI, you can calculate radial distances and set thresholds instead.



Package version
  • semla: 1.1.6
Session info
## R version 4.2.3 (2023-03-15)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Big Sur ... 10.16
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices datasets  utils     methods   base     
## 
## other attached packages:
## [1] patchwork_1.1.3    tibble_3.2.1       semla_1.1.6        ggplot2_3.4.3     
## [5] dplyr_1.1.3        SeuratObject_4.1.3 Seurat_4.3.0.1    
## 
## loaded via a namespace (and not attached):
##   [1] Rtsne_0.16             colorspace_2.1-0       deldir_1.0-9          
##   [4] ellipsis_0.3.2         ggridges_0.5.4         rprojroot_2.0.3       
##   [7] fs_1.6.3               spatstat.data_3.0-1    farver_2.1.1          
##  [10] leiden_0.4.3           listenv_0.9.0          ggrepel_0.9.3         
##  [13] fansi_1.0.4            codetools_0.2-19       splines_4.2.3         
##  [16] cachem_1.0.8           knitr_1.43             zeallot_0.1.0         
##  [19] polyclip_1.10-4        jsonlite_1.8.7         ica_1.0-3             
##  [22] cluster_2.1.4          png_0.1-8              uwot_0.1.16           
##  [25] spatstat.sparse_3.0-2  shiny_1.7.5            sctransform_0.3.5     
##  [28] BiocManager_1.30.22    compiler_4.2.3         httr_1.4.7            
##  [31] Matrix_1.5-1           fastmap_1.1.1          lazyeval_0.2.2        
##  [34] limma_3.54.2           cli_3.6.1              later_1.3.1           
##  [37] htmltools_0.5.6        tools_4.2.3            igraph_1.5.1          
##  [40] gtable_0.3.4           glue_1.6.2             RANN_2.6.1            
##  [43] reshape2_1.4.4         Rcpp_1.0.11            scattermore_1.2       
##  [46] jquerylib_0.1.4        pkgdown_2.0.7          vctrs_0.6.3           
##  [49] nlme_3.1-163           spatstat.explore_3.2-1 progressr_0.14.0      
##  [52] lmtest_0.9-40          spatstat.random_3.1-5  xfun_0.40             
##  [55] stringr_1.5.0          globals_0.16.2         mime_0.12             
##  [58] miniUI_0.1.1.1         lifecycle_1.0.3        irlba_2.3.5.1         
##  [61] renv_1.0.2             goftest_1.2-3          future_1.33.0         
##  [64] MASS_7.3-60            zoo_1.8-12             scales_1.2.1          
##  [67] spatstat.utils_3.0-3   ragg_1.2.5             promises_1.2.1        
##  [70] parallel_4.2.3         RColorBrewer_1.1-3     yaml_2.3.7            
##  [73] memoise_2.0.1          reticulate_1.31        pbapply_1.7-2         
##  [76] gridExtra_2.3          sass_0.4.7             stringi_1.7.12        
##  [79] highr_0.10             desc_1.4.2             rlang_1.1.1           
##  [82] pkgconfig_2.0.3        systemfonts_1.0.4      matrixStats_1.0.0     
##  [85] evaluate_0.21          lattice_0.21-8         tensor_1.5            
##  [88] ROCR_1.0-11            purrr_1.0.2            labeling_0.4.3        
##  [91] htmlwidgets_1.6.2      cowplot_1.1.1          tidyselect_1.2.0      
##  [94] parallelly_1.36.0      RcppAnnoy_0.0.21       plyr_1.8.8            
##  [97] magrittr_2.0.3         R6_2.5.1               magick_2.7.4          
## [100] generics_0.1.3         withr_2.5.0            pillar_1.9.0          
## [103] fitdistrplus_1.1-11    abind_1.4-5            survival_3.5-7        
## [106] sp_2.0-0               future.apply_1.11.0    KernSmooth_2.23-22    
## [109] utf8_1.2.3             spatstat.geom_3.2-4    plotly_4.10.2         
## [112] rmarkdown_2.24         grid_4.2.3             data.table_1.14.8     
## [115] forcats_1.0.0          digest_0.6.33          xtable_1.8-4          
## [118] dbscan_1.1-11          tidyr_1.3.0            httpuv_1.6.11         
## [121] textshaping_0.3.6      munsell_0.5.0          viridisLite_0.4.2     
## [124] bslib_0.5.1            shinyjs_2.1.0