Using SpatialExperiment and plotSpatial in RStudio to visualize my spatial neighbours in the IMC data. Unfortunately, it only gives a black square (without any errors or warnings).
What would be the reason? See script:
counts <- as.matrix(all_cells_combined[, c(1:5)])
# Create the DataFrame for spatial coordinates:
#with modified coords
meta_data <- data.frame(
x = all_cells_combined$`centroid-1`,
y = all_cells_combined$`centroid-0`,
Cell_id = all_cells_combined$Cell_id,
roi_id = all_cells_combined$ROI,
sample_id = all_cells_combined$patient
)
# Make sure the dimensions of assay and colData align correctly
# by transposing the 'y' matrix if needed:
if (ncol(counts) != ncol(meta_data)) {
counts <- t(counts)
}
# Create the SpatialExperiment object:
spe <- SpatialExperiment(
assay = counts,
colData = meta_data,
spatialCoordsNames = c("x", "y")
)
pheno <- data.frame(all_cells_combined$phenotype)
colnames(pheno) <- c("pheno")
colData(spe) <- cbind(colData(spe), pheno)
#spatial analysis
spe <- buildSpatialGraph(spe, img_id = "roi_id", type = "knn", k=10, coords = c("x", "y"))
spe <- buildSpatialGraph(spe, img_id = "roi_id", type = "expansion", threshold = 20, coords = c("x", "y"))
duplicated_ids <- duplicated(spe$sample_id)
print(spe$sample_id[duplicated_ids])
unique(spe$sample_id)
png("file.png",
width = 20, height = 20, res = 300, units = "in")
plotSpatial(spe[,spe$roi_id == "x1"],
coords = c("x", "y"),
node_color_by = "pheno",
node_size_fix = 1.5,
img_id = "roi_id",
draw_edges = TRUE,
colPairName = "knn_interaction_graph",
nodes_first = FALSE,
edge_color_fix = "white") + #scale_color_manual(values = color_mapping) +
theme(
panel.background = element_rect(fill = "black"),
legend.position = "none",
panel.grid = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank()
)
dev.off()
I have checked the coordinates etc. and those are correct.
counts <- countInteractions(spe,
group_by = "roi_id",
label = "pheno",
colPairName = "knn_interaction_graph",
method = c("classic"),
patch_size = NULL
) %>% as.data.frame()
I have done this after ‘plotting’ and it works/looks adequate. So that’s why I am confused as to why it wouldn’t plot properly.
Many thanks in advance!