A unified R package for detecting spatially variable genes (SVGs) in spatial transcriptomics data, integrating multiple state-of-the-art methods with optimized performance.
📚 Documentation: https://zaoqu-liu.github.io/SVG/
SVG detection is a fundamental step in spatial transcriptomics analysis, identifying genes whose expression patterns exhibit significant spatial structure. This package provides:
# Install from R-Universe
install.packages("SVG", repos = "https://zaoqu-liu.r-universe.dev")# Install from GitHub
devtools::install_github("Zaoqu-Liu/SVG")
# Or install with dependencies
devtools::install_github("Zaoqu-Liu/SVG", dependencies = TRUE)# Install from Bioconductor (after acceptance)
BiocManager::install("SVG")Core dependencies (automatically installed): - Matrix,
Rcpp, RcppArmadillo
Optional dependencies for specific methods:
# For Delaunay triangulation
install.packages("geometry")
# For KNN network
install.packages("RANN")
# For nnSVG method (required)
install.packages("BRISC")
# For accurate p-values
install.packages("CompQuadForm")library(SVG)
# Load your data
# expr_matrix: genes x spots (rows x columns)
# spatial_coords: spots x coordinates (x, y)
# Unified interface
results <- CalSVG(expr_matrix, spatial_coords, method = "meringue")
# Or use method-specific functions
results_meringue <- CalSVG_MERINGUE(expr_matrix, spatial_coords)
results_binspect <- CalSVG_binSpect(expr_matrix, spatial_coords)
results_sparkx <- CalSVG_SPARKX(expr_matrix, spatial_coords)
results_nnsvg <- CalSVG_nnSVG(expr_matrix, spatial_coords)
# Get significant SVGs
sig_genes <- results$gene[results$p.adj < 0.05]Spatial autocorrelation using Moran’s I with binary adjacency network.
results <- CalSVG_MERINGUE(
expr_matrix,
spatial_coords,
network_method = "delaunay", # or "knn"
k = 10, # neighbors for KNN
alternative = "greater", # test for clustering
adjust_method = "BH"
)Best for: General SVG detection with explicit spatial network
Moran’s I using inverse distance squared weights (Seurat default).
results <- CalSVG_Seurat(
expr_matrix,
spatial_coords,
weight_scheme = "inverse_squared", # 1/d^2 (default)
adjust_method = "BH"
)Best for: Compatible with Seurat workflow, continuous distance weighting
Binary spatial enrichment test using Fisher’s exact test.
results <- CalSVG_binSpect(
expr_matrix,
spatial_coords,
bin_method = "kmeans", # or "rank"
rank_percent = 30, # for rank method
do_fisher_test = TRUE
)Best for: Fast computation, robust to outliers
Non-parametric kernel-based test using multiple spatial kernels.
results <- CalSVG_SPARKX(
expr_matrix,
spatial_coords,
kernel_option = "mixture", # or "single"
n_threads = 4
)Best for: Large datasets, diverse pattern types
Nearest-neighbor Gaussian processes for spatial modeling.
results <- CalSVG_nnSVG(
expr_matrix,
spatial_coords,
n_neighbors = 10,
cov_model = "exponential",
n_threads = 4
)Best for: Statistical rigor, effect size estimation
Spatial pattern detection using mark variogram from spatstat.
results <- CalSVG_MarkVario(
expr_matrix,
spatial_coords,
r_metric = 5, # distance to evaluate variogram
normalize = TRUE
)Best for: Different perspective on spatial patterns
| Method | Principle | Speed | Scalability | Effect Size |
|---|---|---|---|---|
| MERINGUE | Moran’s I (network) | Fast | Medium | Moderate |
| Seurat | Moran’s I (1/d²) | Fast | Medium | Moderate |
| binSpect | Fisher test | Very Fast | High | Odds ratio |
| SPARK-X | Kernel test | Fast | Very High | No |
| nnSVG | Gaussian process | Slow | Medium | Yes (prop_sv) |
| MarkVario | Variogram | Moderate | Medium | Variogram value |
All methods return a data.frame with unified column names:
| Column | Description |
|---|---|
gene |
Gene identifier |
p.value |
Raw p-value |
p.adj |
Adjusted p-value (BH/FDR) |
Method-specific columns:
| Method | Additional Columns |
|---|---|
| MERINGUE | observed, expected, sd,
z_score |
| Seurat | observed, expected, sd,
rank |
| binSpect | estimate (odds ratio), score |
| SPARK-X | stat_*, pval_* (per kernel) |
| nnSVG | sigma.sq, tau.sq, phi,
prop_sv, LR_stat |
| MarkVario | r.metric.value, rank (no p-values) |
| Tutorial | Description |
|---|---|
| Introduction | Complete guide with all methods, benchmarks, and visualizations |
library(SVG)
# Simulate data
set.seed(42)
n_spots <- 500
n_genes <- 100
# Expression matrix (genes x spots)
expr <- matrix(rpois(n_genes * n_spots, lambda = 10),
nrow = n_genes)
rownames(expr) <- paste0("gene_", 1:n_genes)
colnames(expr) <- paste0("spot_", 1:n_spots)
# Add spatial pattern to first 10 genes
coords <- cbind(x = runif(n_spots, 0, 100),
y = runif(n_spots, 0, 100))
rownames(coords) <- colnames(expr)
# Create spatial gradient for first genes
for (i in 1:10) {
expr[i, ] <- expr[i, ] + round(coords[, 1] / 10)
}
# Run SVG detection
results <- CalSVG(expr, coords, method = "meringue")
# Top SVGs
head(results)
# Significant genes
sig_genes <- results$gene[results$p.adj < 0.05]
print(sig_genes)library(SpatialExperiment)
library(SVG)
# From SpatialExperiment object
spe <- readRDS("your_spe.rds")
expr_matrix <- as.matrix(logcounts(spe))
spatial_coords <- spatialCoords(spe)
results <- CalSVG(expr_matrix, spatial_coords, method = "meringue")# Use multiple cores for faster computation
results <- CalSVG(
expr_matrix, spatial_coords,
method = "sparkx",
n_threads = parallel::detectCores() - 1
)If you use this package, please cite:
Liu Z. (2024). SVG: Spatially Variable Genes Detection Methods.
R package version 1.0.0. https://github.com/Zaoqu-Liu/SVG
And cite the original method papers: - MERINGUE: Miller et al. (2021)
Genome Research - binSpect: Dries et al. (2021) Genome Biology
- SPARK-X: Zhu et al. (2021) Genome Biology - nnSVG: Weber et al. (2023)
Nature Communications
MIT License
Zaoqu Liu
Chinese Academy of Medical Sciences and Peking Union Medical
College
Email: liuzaoqu@163.com
ORCID: 0000-0002-0452-742X
Contributions are welcome! Please feel free to submit a Pull Request.