| Title: | Spatial Transcriptomics Distance Calculation and Visualization |
|---|---|
| Description: | Analysis of spatial relationships between cell types in spatial transcriptomics data. Spatial proximity is a critical factor in cell-cell communication. The package calculates nearest neighbor distances between specified cell types and provides visualization tools to explore spatial patterns. Applications include studying cell-cell interactions, immune microenvironment characterization, and spatial organization of tissues. |
| Authors: | Zixiang Wang [aut, cre] (ORCID: <https://orcid.org/0000-0001-5252-9764>), Lei Yang [aut], Zhaojian Liu [aut] |
| Maintainer: | Zixiang Wang <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.6.6 |
| Built: | 2026-05-12 08:33:01 UTC |
| Source: | https://github.com/cran/STDistance |
Perform correlation analysis between spatial features and distance metrics with visualization
calculate_correlations( spatial_data, distance_results, spatial_feature, distance_metric, method = "pearson", plot = TRUE, plot_title = NULL )calculate_correlations( spatial_data, distance_results, spatial_feature, distance_metric, method = "pearson", plot = TRUE, plot_title = NULL )
spatial_data |
Spatial data containing feature columns and Newbarcode identifier |
distance_results |
Distance results containing distance metrics and Newbarcode identifier |
spatial_feature |
Column name from spatial_data to use for correlation (e.g., "gen2_SPLIz_numeric") |
distance_metric |
Column name from distance_results to use for correlation (e.g., "Epithelial_cells_A") |
method |
Correlation method ("pearson", "spearman", "kendall") |
plot |
Logical, whether to generate a scatter plot |
plot_title |
Title for the scatter plot (optional) |
A list containing correlation results and ggplot object (if plot=TRUE)
calculate_correlations(spatial_data = posi, distance_results = distance_results, spatial_feature = "gen2_SPLIz_numeric", distance_metric = "Epithelial_cells_A", method = "pearson", plot = TRUE, plot_title = "Correlation between Gene Expression and Distance")calculate_correlations(spatial_data = posi, distance_results = distance_results, spatial_feature = "gen2_SPLIz_numeric", distance_metric = "Epithelial_cells_A", method = "pearson", plot = TRUE, plot_title = "Correlation between Gene Expression and Distance")
Calculate nearest distances between cell types
calculate_nearest_distances( spatial_data, reference_type, target_types, x_col = "pxl_row_in_fullres", y_col = "pxl_col_in_fullres", id_col = "barcode", type_col = "Epi_strom" )calculate_nearest_distances( spatial_data, reference_type, target_types, x_col = "pxl_row_in_fullres", y_col = "pxl_col_in_fullres", id_col = "barcode", type_col = "Epi_strom" )
spatial_data |
A data frame containing spatial coordinates and cell type info |
reference_type |
The reference cell type to calculate distances from |
target_types |
Vector of target cell types to calculate distances to |
x_col |
Column name for x-coordinates |
y_col |
Column name for y-coordinates |
id_col |
Column name for cell identifiers |
type_col |
Column name for cell type information |
A data frame with nearest distances for each reference cell
calculate_nearest_distances(posi, reference_type="Macrophage", target_types=c("Epithelial_cells_A","Epithelial_cells_B", "Epithelial_cells_C","Epithelial_cells_D"), id_col = "Newbarcode", type_col = "celltype_ABCDepi")calculate_nearest_distances(posi, reference_type="Macrophage", target_types=c("Epithelial_cells_A","Epithelial_cells_B", "Epithelial_cells_C","Epithelial_cells_D"), id_col = "Newbarcode", type_col = "celltype_ABCDepi")
A data frame with nearest distances for each reference cell
data("distance_results")data("distance_results")
A data frame with 18 observations on the following 5 variables.
Newbarcodea character vector
Epithelial_cells_Aa numeric vector
Epithelial_cells_Ba numeric vector
Epithelial_cells_Ca numeric vector
Epithelial_cells_Da numeric vector
A data frame with nearest distances for each reference cell.
This study.
Wang, Z., Yang, L., Yang, S., Li, G., Xu, M., Kong, B., Shao, C., & Liu, Z. (2025). Isoform switch of CD47 provokes macrophage-mediated pyroptosis in ovarian cancer. bioRxiv, 2025.2004.2017.649282. https://doi.org/10.1101/2025.04.17.649282
data(distance_results) ## maybe str(distance_results) ; plot(distance_results) ...data(distance_results) ## maybe str(distance_results) ; plot(distance_results) ...
Metadata of a Seurat Object.
data("metadata")data("metadata")
A data frame with 293 observations on the following 9 variables.
orig.identa character vector
nCount_Spatiala numeric vector
nFeature_Spatiala numeric vector
nCount_SCTa numeric vector
nFeature_SCTa numeric vector
integrated_snn_res.0.8a numeric vector
seurat_clustersa numeric vector
celltype_ABCDepia character vector
gen2_SPLIz_numerica numeric vector
Metadata dataframe can be exported from Seurat Object as follows. The Seurat Object should be fully annotated and the metadata dataframe should included: orig.ident, celltype, the target gene expression, splicing index (SpliZ), etc.
This study.
Wang, Z., Yang, L., Yang, S., Li, G., Xu, M., Kong, B., Shao, C., & Liu, Z. (2025). Isoform switch of CD47 provokes macrophage-mediated pyroptosis in ovarian cancer. bioRxiv, 2025.2004.2017.649282. https://doi.org/10.1101/2025.04.17.649282
data(metadata) ## maybe str(metadata) ; plot(metadata) ...data(metadata) ## maybe str(metadata) ; plot(metadata) ...
Normalize spatial coordinates
normalize_spatial( data, sample_col = "Sample", x_col = "pxl_row_in_fullres", y_col = "pxl_col_in_fullres", min_value = 1, max_value = 10000 )normalize_spatial( data, sample_col = "Sample", x_col = "pxl_row_in_fullres", y_col = "pxl_col_in_fullres", min_value = 1, max_value = 10000 )
data |
A data frame containing spatial coordinates |
sample_col |
Column name specifying sample IDs |
x_col |
Column name for x-coordinates |
y_col |
Column name for y-coordinates |
min_value |
Minimum value for normalization range |
max_value |
Maximum value for normalization range |
A data frame with normalized coordinates
tissue_posi_normalized<-normalize_spatial(tissue_posi)tissue_posi_normalized<-normalize_spatial(tissue_posi)
Creates a boxplot visualization of distance metrics with overlaid mean points and SEM error bars. Suitable for comparing multiple groups with potentially skewed distributions.
plot_distance_boxplot( distance_result, id_col = "barcode", show_points = FALSE, y_scale = c("original", "log10"), palette = "Set2" )plot_distance_boxplot( distance_result, id_col = "barcode", show_points = FALSE, y_scale = c("original", "log10"), palette = "Set2" )
distance_result |
A data.frame generated by |
id_col |
Name of the column containing cell IDs (default: "barcode"). |
show_points |
Logical, whether to overlay individual data points (default: FALSE). |
y_scale |
Method for y-axis scaling: "original" or "log10" (default: "original"). |
palette |
Color palette name from |
A ggplot2 object. Additional customization can be done using ggplot2 functions.
plot_distance_boxplot(distance_results,id_col = "Newbarcode")plot_distance_boxplot(distance_results,id_col = "Newbarcode")
Creates a radial plot with automatic label placement to prevent overlaps between nodes and text labels.
plot_radial_distance( distance_result, reference_type, id_col = "barcode", scale_radius = 1, show_labels = TRUE, palette = "Set2", label_padding = 0.15, center_label_expansion = 1.5 )plot_radial_distance( distance_result, reference_type, id_col = "barcode", scale_radius = 1, show_labels = TRUE, palette = "Set2", label_padding = 0.15, center_label_expansion = 1.5 )
distance_result |
Data.frame from calculate_nearest_distances() |
reference_type |
Name of the reference cell type (center node) |
id_col |
Name of ID column (default: "barcode") |
scale_radius |
Scaling factor for layout (default: 1) |
show_labels |
Whether to show distance labels (default: TRUE) |
palette |
Color palette name (default: "Set2") |
label_padding |
Radial padding for labels (default: 0.15) |
center_label_expansion |
Center expansion for labels (default: 1.5) |
A ggplot2 object
plot_radial_distance(distance_results,id_col = "Newbarcode", reference_type = "Macrophages",label_padding = 0.3, show_labels = TRUE,palette = "Dark2")plot_radial_distance(distance_results,id_col = "Newbarcode", reference_type = "Macrophages",label_padding = 0.3, show_labels = TRUE,palette = "Dark2")
Merged spatial location and metadata information.
data("posi")data("posi")
A data frame with 293 observations on the following 18 variables.
Newbarcodea character vector
barcodea character vector
in_tissuea numeric vector
array_rowa numeric vector
array_cola numeric vector
pxl_row_in_fullresa numeric vector
pxl_col_in_fullresa numeric vector
Samplea character vector
Sampleida numeric vector
orig.identa character vector
nCount_Spatiala numeric vector
nFeature_Spatiala numeric vector
nCount_SCTa numeric vector
nFeature_SCTa numeric vector
integrated_snn_res.0.8a numeric vector
seurat_clustersa numeric vector
celltype_ABCDepia character vector
gen2_SPLIz_numerica numeric vector
Merged spatial location and metadata information.
This study.
Wang, Z., Yang, L., Yang, S., Li, G., Xu, M., Kong, B., Shao, C., & Liu, Z. (2025). Isoform switch of CD47 provokes macrophage-mediated pyroptosis in ovarian cancer. bioRxiv, 2025.2004.2017.649282. https://doi.org/10.1101/2025.04.17.649282
data(posi) ## maybe str(posi) ; plot(posi) ...data(posi) ## maybe str(posi) ; plot(posi) ...
Position of the spots from spatial transcriptome data.
data("tissue_posi")data("tissue_posi")
A data frame with 9984 observations on the following 9 variables.
barcodea character vector
in_tissuea numeric vector
array_rowa numeric vector
array_cola numeric vector
pxl_row_in_fullresa numeric vector
pxl_col_in_fullresa numeric vector
Samplea character vector
Sampleida numeric vector
Newbarcodea character vector
Please find the tissue_positions.csv from the spaceranger output files: ./out/spatial/tissue_positions.csv Multiple samples should be merged together, adding "samples" and "Newbarcode" columns.
This study.
Wang, Z., Yang, L., Yang, S., Li, G., Xu, M., Kong, B., Shao, C., & Liu, Z. (2025). Isoform switch of CD47 provokes macrophage-mediated pyroptosis in ovarian cancer. bioRxiv, 2025.2004.2017.649282. https://doi.org/10.1101/2025.04.17.649282
data(tissue_posi) ## maybe str(tissue_posi) ; plot(tissue_posi) ...data(tissue_posi) ## maybe str(tissue_posi) ; plot(tissue_posi) ...
Normalized tissue spots position.
data("tissue_posi_normalized")data("tissue_posi_normalized")
A data frame with 9984 observations on the following 9 variables.
barcodea character vector
in_tissuea numeric vector
array_rowa numeric vector
array_cola numeric vector
pxl_row_in_fullresa numeric vector
pxl_col_in_fullresa numeric vector
Samplea character vector
Sampleida numeric vector
Newbarcodea character vector
Normalized tissue spots position.
This study.
Wang, Z., Yang, L., Yang, S., Li, G., Xu, M., Kong, B., Shao, C., & Liu, Z. (2025). Isoform switch of CD47 provokes macrophage-mediated pyroptosis in ovarian cancer. bioRxiv, 2025.2004.2017.649282. https://doi.org/10.1101/2025.04.17.649282
data(tissue_posi_normalized) ## maybe str(tissue_posi_normalized) ; plot(tissue_posi_normalized) ...data(tissue_posi_normalized) ## maybe str(tissue_posi_normalized) ; plot(tissue_posi_normalized) ...
Visualize spatial network with expression gradient
visualize_spatial_gradient( spatial_data, sample, gradient_type, fixed_type, expression_col = "gen2_SPLIz_numeric", x_col = "pxl_row_in_fullres", y_col = "pxl_col_in_fullres", type_col = "Epi_strom", fixed_color = "#A9C6D9", line_color = "#666666", gradient_palette = "C", point_size = 1, point_alpha = 0.8, line_width = 0.3, line_alpha = 0.6, show_legend = TRUE, legend_title = "Expression", grid_major_color = "gray90", grid_minor_color = "gray95", border_color = "black", background_color = "white" )visualize_spatial_gradient( spatial_data, sample, gradient_type, fixed_type, expression_col = "gen2_SPLIz_numeric", x_col = "pxl_row_in_fullres", y_col = "pxl_col_in_fullres", type_col = "Epi_strom", fixed_color = "#A9C6D9", line_color = "#666666", gradient_palette = "C", point_size = 1, point_alpha = 0.8, line_width = 0.3, line_alpha = 0.6, show_legend = TRUE, legend_title = "Expression", grid_major_color = "gray90", grid_minor_color = "gray95", border_color = "black", background_color = "white" )
spatial_data |
Spatial coordinates data frame containing cell types and expression values |
sample |
Sample name in the spatial transcriptome data |
gradient_type |
Cell type to show with expression gradient coloring |
fixed_type |
Cell type to show in fixed color (default gray) |
expression_col |
Column name containing expression values (default "gen2_SPLIz_numeric") |
x_col |
Column name for x-coordinates (default "pxl_row_in_fullres") |
y_col |
Column name for y-coordinates (default "pxl_col_in_fullres") |
type_col |
Column name for cell type information (default "Epi_strom") |
fixed_color |
Color for the fixed cell type (default "#A9C6D9" - light gray-blue) |
line_color |
Color for connection lines (default "#666666" - dark gray) |
gradient_palette |
Color palette for expression gradient (default viridis option "C") |
point_size |
Size of points (default 1) |
point_alpha |
Transparency of points (default 0.8) |
line_width |
Width of connection lines (default 0.3) |
line_alpha |
Transparency of connection lines (default 0.6) |
show_legend |
Logical whether to show legend (default TRUE) |
legend_title |
Title for the legend (default "Expression") |
grid_major_color |
Color for major grid lines (default "gray90") |
grid_minor_color |
Color for minor grid lines (default "gray95") |
border_color |
Color for plot border (default "black") |
background_color |
Color for plot background (default "white") |
A ggplot object showing the spatial relationships with expression gradient
visualize_spatial_gradient(spatial_data = posi, sample="SP8", gradient_type = "Epithelial_cells_A", fixed_type = "Macrophage", expression_col = "gen2_SPLIz_numeric", type_col = "celltype_ABCDepi", legend_title = "Expression", background_color = "white")visualize_spatial_gradient(spatial_data = posi, sample="SP8", gradient_type = "Epithelial_cells_A", fixed_type = "Macrophage", expression_col = "gen2_SPLIz_numeric", type_col = "celltype_ABCDepi", legend_title = "Expression", background_color = "white")
Visualize spatial relationships between multiple cell types
visualize_spatial_multinetwork( spatial_data, sample, reference_type, target_types, x_col = "pxl_row_in_fullres", y_col = "pxl_col_in_fullres", type_col = "Epi_strom", color_palette = NULL, point_alpha = 0.7, line_alpha = 0.5, point_size = 1.5, line_width = 0.3, show_legend = TRUE )visualize_spatial_multinetwork( spatial_data, sample, reference_type, target_types, x_col = "pxl_row_in_fullres", y_col = "pxl_col_in_fullres", type_col = "Epi_strom", color_palette = NULL, point_alpha = 0.7, line_alpha = 0.5, point_size = 1.5, line_width = 0.3, show_legend = TRUE )
spatial_data |
Spatial coordinates data frame |
sample |
Sample name in the spatial transcriptome data |
reference_type |
Reference cell type (character vector of length 1) |
target_types |
Target cell type(s) (character vector of 1 or more) |
x_col |
Column name for x-coordinates |
y_col |
Column name for y-coordinates |
type_col |
Column name for cell type information |
color_palette |
Named vector of colors for cell types |
point_alpha |
Transparency level for points |
line_alpha |
Transparency level for connection lines |
point_size |
Size of points in plot |
line_width |
Width of connection lines |
show_legend |
Logical, whether to show legend |
A ggplot object showing the spatial relationships
visualize_spatial_multinetwork(posi, sample="SP8",reference_type="Macrophage", target_type=c("Epithelial_cells_A","Epithelial_cells_B"), type_col = "celltype_ABCDepi")visualize_spatial_multinetwork(posi, sample="SP8",reference_type="Macrophage", target_type=c("Epithelial_cells_A","Epithelial_cells_B"), type_col = "celltype_ABCDepi")
Visualize spatial relationships between cell types
visualize_spatial_network( spatial_data, sample, reference_type, target_type, x_col = "pxl_row_in_fullres", y_col = "pxl_col_in_fullres", type_col = "Epi_strom", color_palette = c(Macrophage = "#90ee90", Epithelial_cells_A = "#377EB8"), alpha = 0.7 )visualize_spatial_network( spatial_data, sample, reference_type, target_type, x_col = "pxl_row_in_fullres", y_col = "pxl_col_in_fullres", type_col = "Epi_strom", color_palette = c(Macrophage = "#90ee90", Epithelial_cells_A = "#377EB8"), alpha = 0.7 )
spatial_data |
Spatial coordinates data frame |
sample |
Sample name in the spatial transcriptome data |
reference_type |
Reference cell type |
target_type |
Target cell type |
x_col |
Column name for x-coordinates |
y_col |
Column name for y-coordinates |
type_col |
Column name for cell type information |
color_palette |
Named vector of colors for cell types |
alpha |
Transparency level for points and lines |
A ggplot object showing the spatial relationships
visualize_spatial_network(posi, sample="SP8", reference_type="Macrophage", target_type="Epithelial_cells_A", x_col = "pxl_row_in_fullres", y_col = "pxl_col_in_fullres", type_col = "celltype_ABCDepi", color_palette = c("Macrophage" = "#90ee90", "Epithelial_cells_A" = "#377EB8"))visualize_spatial_network(posi, sample="SP8", reference_type="Macrophage", target_type="Epithelial_cells_A", x_col = "pxl_row_in_fullres", y_col = "pxl_col_in_fullres", type_col = "celltype_ABCDepi", color_palette = c("Macrophage" = "#90ee90", "Epithelial_cells_A" = "#377EB8"))