# Load the toy dataset
data(codex_toy_data)
# Examine the structure
str(codex_toy_data)
head(codex_toy_data)
# Summary of cell types
table(codex_toy_data$Annotation5)
# Summary by ROI
table(codex_toy_data$ROI_num)
table(codex_toy_data$ROI_num, codex_toy_data$Annotation5)
# Quick visualization of spatial distribution
if (requireNamespace("ggplot2", quietly = TRUE)) {
library(ggplot2)
ggplot(codex_toy_data, aes(x = X_cent, y = Y_cent, color = Annotation5)) +
geom_point(size = 0.8, alpha = 0.7) +
facet_wrap(~ROI_num, scales = "free") +
labs(title = "Toy CODEX Spatial Cell Distribution by ROI",
x = "X Coordinate", y = "Y Coordinate") +
theme_minimal() +
scale_y_reverse()
}
# Basic SGWT analysis example
# \donttest{
# Focus on BCL6- B Cell cells in ROI_1 for SGWT analysis
bcl6nb_data <- codex_toy_data[codex_toy_data$Annotation5 == "BCL6- B Cell" &
codex_toy_data$ROI_num == "ROI_1", ]
# Create binned representation
library(dplyr)
binned_data <- codex_toy_data %>%
filter(Annotation5 == "BCL6- B Cell", ROI_num == "ROI_1") %>%
mutate(
x_bin = cut(X_cent, breaks = 20, labels = FALSE),
y_bin = cut(Y_cent, breaks = 20, labels = FALSE)
) %>%
group_by(x_bin, y_bin) %>%
summarise(cell_count = n(), .groups = 'drop')
# Prepare for SGWT
complete_grid <- expand.grid(x_bin = 1:20, y_bin = 1:20)
sgwt_data <- complete_grid %>%
left_join(binned_data, by = c("x_bin", "y_bin")) %>%
mutate(
cell_count = ifelse(is.na(cell_count), 0, cell_count),
x = x_bin,
y = y_bin,
signal = cell_count / max(cell_count, na.rm = TRUE)
) %>%
select(x, y, signal)
# Apply SGWT using new workflow
SG <- initSGWT(sgwt_data, signals = "signal", J = 3, kernel_type = "heat")
SG <- runSpecGraph(SG, k = 8)
SG <- runSGWT(SG)
# View results
print(SG)
# }
Run the code above in your browser using DataLab