CytoSPACER provides seamless integration with the Seurat ecosystem, supporting both Seurat v4 and v5. This vignette demonstrates how to use CytoSPACER with Seurat objects for spatial transcriptomics analysis.
The Seurat integration workflow consists of:
The run_cytospace_seurat() function handles data
extraction automatically:
# Run CytoSPACER directly from Seurat objects
results <- run_cytospace_seurat(
sc_seurat = sc_seurat,
st_seurat = st_seurat,
cell_type_col = "celltype", # Column in metadata with cell type labels
sc_assay = "RNA", # Assay to use from scRNA-seq
st_assay = "Spatial", # Assay to use from spatial data
mean_cells_per_spot = 5,
distance_metric = "pearson",
seed = 42
)# Add CytoSPACER results to spatial Seurat object
st_seurat <- add_cytospace_to_seurat(st_seurat, results)
# Check new metadata columns
head(st_seurat@meta.data)The function adds:
n_cells_cytospace: Number of cells assigned to each
spotdominant_celltype_cytospace: Most abundant cell type
per spotfrac_*: Fractional abundance for each cell type# Save scRNA-seq data to files
extract_seurat_data(
seurat_obj = sc_seurat,
assay = "RNA",
cell_type_col = "celltype",
output_dir = "cytospace_input/",
prefix = "scRNA_",
sparse = FALSE
)
# Save spatial data to files
extract_spatial_data(
seurat_obj = st_seurat,
assay = "Spatial",
output_dir = "cytospace_input/",
prefix = "ST_"
)library(CytoSPACER)
library(Seurat)
# 1. Load data
sc_seurat <- readRDS("scRNA_processed.rds")
st_seurat <- Load10X_Spatial("visium_output/")
# 2. Preprocess spatial data if needed
st_seurat <- SCTransform(st_seurat, assay = "Spatial", verbose = FALSE)
# 3. Run CytoSPACER
results <- run_cytospace_seurat(
sc_seurat = sc_seurat,
st_seurat = st_seurat,
cell_type_col = "celltype",
mean_cells_per_spot = 5,
distance_metric = "pearson",
n_workers = 4,
seed = 42
)
# 4. Add results to Seurat object
st_seurat <- add_cytospace_to_seurat(st_seurat, results)
# 5. Visualize
p1 <- SpatialDimPlot(st_seurat, group.by = "dominant_celltype_cytospace")
p2 <- SpatialFeaturePlot(st_seurat, features = "n_cells_cytospace")
# 6. Save results
write_cytospace_results(results, output_dir = "cytospace_output/")
saveRDS(st_seurat, "st_seurat_with_cytospace.rds")CytoSPACER automatically detects Seurat version and handles the differences:
Ensure your scRNA-seq reference has high-quality cell type annotations:
Check gene overlap between scRNA-seq and spatial data:
1. Missing cell type column:
# Check available metadata columns
colnames(sc_seurat@meta.data)
# Ensure cell_type_col exists
if (!"celltype" %in% colnames(sc_seurat@meta.data)) {
stop("Cell type column not found!")
}2. No spatial image:
# Check available images
names(st_seurat@images)
# Specify image name if multiple exist
results <- run_cytospace_seurat(
sc_seurat = sc_seurat,
st_seurat = st_seurat,
cell_type_col = "celltype",
image_name = "slice1"
)3. Assay not found:
sessionInfo()
#> R version 4.6.1 (2026-06-24)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 26.04 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] future_1.70.0 Matrix_1.7-5 CytoSPACER_1.0.0 rmarkdown_2.31
#>
#> loaded via a namespace (and not attached):
#> [1] gtable_0.3.6 future.apply_1.20.2 jsonlite_2.0.0
#> [4] dplyr_1.2.1 compiler_4.6.1 Rcpp_1.1.1-1.1
#> [7] tidyselect_1.2.1 parallel_4.6.1 jquerylib_0.1.4
#> [10] globals_0.19.1 scales_1.4.0 yaml_2.3.12
#> [13] fastmap_1.2.0 lattice_0.22-9 ggplot2_4.0.3
#> [16] R6_2.6.1 labeling_0.4.3 generics_0.1.4
#> [19] knitr_1.51 tibble_3.3.1 maketools_1.3.2
#> [22] pillar_1.11.1 bslib_0.11.0 RColorBrewer_1.1-3
#> [25] rlang_1.2.0 cachem_1.1.0 xfun_0.59
#> [28] sass_0.4.10 sys_3.4.3 S7_0.2.2
#> [31] otel_0.2.0 cli_3.6.6 withr_3.0.3
#> [34] progressr_1.0.0 magrittr_2.0.5 digest_0.6.39
#> [37] grid_4.6.1 lifecycle_1.0.5 vctrs_0.7.3
#> [40] evaluate_1.0.5 glue_1.8.1 data.table_1.18.4
#> [43] farver_2.1.2 listenv_1.0.0 codetools_0.2-20
#> [46] buildtools_1.0.0 parallelly_1.48.0 pkgconfig_2.0.3
#> [49] tools_4.6.1 htmltools_0.5.9