Spatially variable gene analysis with STBench

Introduction

We use the same SVG detection procedure for each normalised dataset to examine how normalisation affects spatial variability statistics and gene rankings. The supplied svg_example contains precomputed results, avoiding repeated, potentially expensive nnSVG calculations.

data(norm_example)
data(svg_example)
norm <- norm_example
svg_norm <- svg_example
names(svg_norm)
#> [1] "None"    "SpaNorm"

Detect spatially variable genes

To reproduce SVG detection from the normalised data, run the code below. The remaining sections use the supplied precomputed results.

svg_recomputed <- run_svg(
  norm_list = norm,
  method = "nnSVG",
  n_threads = 1,
  verbose = FALSE
)

Evaluate biologically relevant marker genes

We evaluate example genes available in the SVG results rather than assuming that a particular tissue-marker panel is present in this small dataset.

common_genes <- Reduce(intersect, lapply(svg_norm, rownames))
marker_genes <- head(common_genes, 5)
marker_genes
#> [1] "HSPB7"   "KCNC3"   "COL25A1" "MAL"     "CPXM2"

Compare SVG statistics

Higher likelihood-ratio (LR) statistics indicate stronger statistical evidence of spatial variation.

if (length(marker_genes) > 0L) {
  p_svg_markers <- plot_svg_stat(
    svg_norm = svg_norm,
    genes = marker_genes,
    stat = "LR_stat",
    transform = "log",
    point_size = 4,
    gene_id = "rownames",
    theme_name = "bw"
  )
  p_svg_markers$plot
}

Compare SVG rankings

Smaller ranks indicate higher relative priority in SVG detection.

if (length(marker_genes) > 0L) {
  p_svg_rank <- plot_svg_rank(
    svg_norm = svg_norm,
    genes = marker_genes,
    point_size = 3,
    gene_id = "rownames",
    theme_name = "bw"
  )
  p_svg_rank$plot
}

Investigate SVG ranks against LR statistics

if (length(marker_genes) > 0L) {
  p <- plot_svg_rank_vs_stat(
    svg_norm = svg_norm,
    genes = marker_genes,
    stat = "LR_stat",
    transform = "log",
    scales = "free",
    ncol = 3,
    point_size = 3,
    theme_name = "bw"
  )
  p$plot
}

Compare overall marker gene SVG statistics

if (length(marker_genes) > 0L) {
  p_svg_mk <- plot_svg_gene_set_distribution(
    svg_norm = svg_norm,
    genes = marker_genes,
    stat = "LR_stat",
    gene_id = "rownames",
    transform = "log",
    plot_type = "boxplot",
    add_jitter = TRUE,
    point_size = 1.5,
    theme_name = "bw"
  )
  p_svg_mk$plot
}

Evaluate SVG ranking consistency

Correlations are calculated using genes shared across the methods. Higher correlations indicate greater similarity in rankings.

p_cor <- plot_svg_rank_correlation(
  svg_norm = svg_norm,
  show_values = TRUE,
  digits = 2,
  theme_name = "classic"
)
p_cor$plot

Investigate housekeeping gene behaviour

Housekeeping genes are generally expected to exhibit relatively stable expression, although they are not necessarily spatially uniform. A tissue-specific, curated list is preferable in full analyses.

For a self-contained demonstration, we use a small subset of the available genes as an illustrative gene set. These genes are not claimed to be housekeeping genes.

example_gene_set <- head(common_genes, 10)
if (length(example_gene_set) > 0L) {
  p_svg_set <- plot_svg_gene_set_distribution(
    svg_norm = svg_norm,
    genes = example_gene_set,
    stat = "LR_stat",
    gene_id = "rownames",
    transform = "log",
    plot_type = "boxplot",
    add_jitter = TRUE,
    point_size = 1.5,
    theme_name = "classic"
  )
  p_svg_set$plot
}

Examine genes among top-ranked SVGs

if (length(example_gene_set) > 0L) {
  p_topn <- plot_svg_topn_gene_set(
    svg_norm = svg_norm,
    genes = example_gene_set,
    top_n = c(10, 20, 50),
    gene_id = "rownames",
    theme_name = "bw",
    gene_set_name = "Example gene set"
  )
  p_topn$plot
}

Compare SVG statistics between gene sets

gene_sets <- list(
  "Example gene set" = example_gene_set,
  "Other genes" = setdiff(common_genes, example_gene_set)
)
if (all(lengths(gene_sets) > 0L)) {
  p_svg_sets <- plot_svg_gene_sets(
    svg_norm = svg_norm,
    gene_sets = gene_sets,
    stat = "LR_stat",
    gene_id = "rownames",
    transform = "log",
    plot_type = "boxplot",
    theme_name = "bw"
  )
  p_svg_sets$plot
}

Spatial expression of marker genes

Expression can be scaled between 0 and 1 by gene for comparing spatial patterns; unscaled values can also be plotted, but numerical scales need not be directly comparable across transformations.

if (length(marker_genes) > 0L) {
  p_marker_maps <- plot_spatial_expression(
    norm_list = norm,
    genes = head(marker_genes, 2),
    methods = names(norm),
    assay_name = "logcounts",
    scale_expr = TRUE,
    scale_by = "gene",
    point_size = 0.5,
    colour_option = "magma",
    ncol_genes = 1,
    theme_name = "classic"
  )
  p_marker_maps$plot
}

Session information

sessionInfo()
#> R version 4.6.2 (2026-10-29)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 26.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.32.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=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: Etc/UTC
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] STBench_0.99.2 rmarkdown_2.32
#> 
#> loaded via a namespace (and not attached):
#>   [1] RcppHungarian_0.3           splines_4.6.2              
#>   [3] ggplotify_0.1.4             tibble_3.3.1               
#>   [5] polyclip_1.10-7             enrichit_0.2.7             
#>   [7] lifecycle_1.0.5             httr2_1.3.0                
#>   [9] aricode_1.1.0               edgeR_4.99.16              
#>  [11] processx_3.10.0             lattice_0.23-1             
#>  [13] MASS_7.3-66                 magrittr_2.0.5             
#>  [15] limma_3.99.5                sass_0.5.0                 
#>  [17] jquerylib_0.1.4             yaml_2.3.12                
#>  [19] metapod_1.21.1              otel_0.3.0                 
#>  [21] ggtangle_0.1.5              pbapply_1.7-5              
#>  [23] DBI_1.3.0                   buildtools_1.0.0           
#>  [25] RColorBrewer_1.1-3          abind_1.4-8                
#>  [27] GenomicRanges_1.65.4        purrr_1.2.2                
#>  [29] BiocGenerics_0.59.13        yulab.utils_0.2.6          
#>  [31] tweenr_2.0.3                rappdirs_0.3.4             
#>  [33] aisdk_1.4.12                gdtools_0.6.0              
#>  [35] clevr_0.1.2                 IRanges_2.47.5             
#>  [37] S4Vectors_0.51.10           enrichplot_1.99.8          
#>  [39] ggrepel_0.9.8               irlba_2.4.1                
#>  [41] tidytree_0.4.9              maketools_1.3.2            
#>  [43] RSpectra_0.16-2             dqrng_0.4.1                
#>  [45] codetools_0.2-20            DelayedArray_0.39.8        
#>  [47] ggforce_0.5.0               DOSE_4.7.4                 
#>  [49] scuttle_1.23.2              Banksy_1.9.6               
#>  [51] tidyselect_1.2.1            aplot_0.3.2                
#>  [53] farver_2.1.2                ScaledMatrix_1.21.0        
#>  [55] viridis_0.6.5               matrixStats_1.5.0          
#>  [57] stats4_4.6.2                Seqinfo_1.3.2              
#>  [59] jsonlite_2.0.0              BiocNeighbors_2.7.3        
#>  [61] scater_1.41.3               systemfonts_1.3.4          
#>  [63] dbscan_1.3.0                tools_4.6.2                
#>  [65] ggnewscale_0.5.2            treeio_1.37.1              
#>  [67] Rcpp_1.1.2                  glue_1.8.1                 
#>  [69] gridExtra_2.3.1             SparseArray_1.13.5         
#>  [71] BiocBaseUtils_1.15.1        xfun_0.62                  
#>  [73] qvalue_2.45.0               MatrixGenerics_1.25.0      
#>  [75] dplyr_1.2.1                 withr_3.0.3                
#>  [77] fastmap_1.2.0               bluster_1.23.1             
#>  [79] callr_3.8.0                 digest_0.6.39              
#>  [81] rsvd_2.0.0                  R6_2.6.1                   
#>  [83] gridGraphics_0.5-1          colorspace_2.1-4           
#>  [85] GO.db_3.24.0                sccore_1.0.7               
#>  [87] RSQLite_3.53.3              tidyr_1.3.2                
#>  [89] generics_0.1.4              fontLiberation_0.1.0       
#>  [91] data.table_1.18.6.1         treelayout_0.1.0           
#>  [93] FNN_1.1.4.1                 htmlwidgets_1.6.4          
#>  [95] httr_1.4.9                  S4Arrays_1.13.3            
#>  [97] org.Mm.eg.db_3.24.6         scatterpie_0.2.6           
#>  [99] scatterplot3d_0.3-45        uwot_0.2.5                 
#> [101] pkgconfig_2.0.3             gtable_0.3.6               
#> [103] rdist_0.0.6                 blob_1.3.0                 
#> [105] BRISC_1.0.6                 S7_1.0.0                   
#> [107] SingleCellExperiment_1.35.2 XVector_0.53.0             
#> [109] sys_3.4.3                   clusterProfiler_4.21.7     
#> [111] htmltools_0.5.9             fontBitstreamVera_0.1.1    
#> [113] nnSVG_1.17.1                scales_1.4.0               
#> [115] Biobase_2.73.2              png_0.1-9                  
#> [117] SpatialExperiment_1.23.0    scran_1.41.1               
#> [119] ggfun_0.2.2                 knitr_1.52                 
#> [121] reshape2_1.4.5              rjson_0.2.23               
#> [123] nlme_3.1-172                cachem_1.1.0               
#> [125] Polychrome_1.6.2            stringr_1.6.0              
#> [127] parallel_4.6.2              vipor_0.4.7                
#> [129] AnnotationDbi_1.75.2        pillar_1.11.1              
#> [131] grid_4.6.2                  vctrs_0.7.3                
#> [133] RANN_2.6.3                  tidydr_0.0.7               
#> [135] BiocSingular_1.29.3         beachmat_2.29.3            
#> [137] cluster_2.1.8.3             beeswarm_0.4.0             
#> [139] evaluate_1.0.5              magick_2.9.1               
#> [141] cli_3.6.6                   compiler_4.6.2             
#> [143] rlang_1.3.0                 crayon_1.5.3               
#> [145] labeling_0.4.3              mclust_6.1.4               
#> [147] ps_1.9.3                    plyr_1.8.9                 
#> [149] fs_2.1.0                    ggbeeswarm_0.7.3           
#> [151] ggiraph_0.9.7               stringi_1.8.9              
#> [153] viridisLite_0.4.3           BiocParallel_1.47.0        
#> [155] Biostrings_2.81.9           lazyeval_0.2.3             
#> [157] fontquiver_0.2.1            GOSemSim_2.39.5            
#> [159] Matrix_1.7-6                patchwork_1.3.2            
#> [161] sparseMatrixStats_1.25.0    bit64_4.8.6                
#> [163] ggplot2_4.0.4               SpaNorm_1.7.17             
#> [165] KEGGREST_1.53.6             statmod_1.5.2              
#> [167] SummarizedExperiment_1.43.0 leidenAlg_1.1.8            
#> [169] igraph_2.3.4                memoise_2.0.1              
#> [171] bslib_0.12.0                ggtree_4.3.2               
#> [173] bit_4.6.0                   gson_0.2.2                 
#> [175] ape_5.8-1