Closed JPingLin closed 1 year ago
Here it is:
library(EBImage)
library(ggforce)
png.device <- function(...) {
png(..., type="cairo", res=300, units='in')
}
image_8k = readImage("data-raw/image_8k.tif")
img_8kv2 = rgbImage(green = image_8k[,,4], blue = image_8k[,,2])
scalefactors <- rjson::fromJSON(file="/pathToData/spatial/scalefactors_json.json")
sf <- scalefactors$tissue_hires_scalef
hires_radius <- (scalefactors$spot_diameter_fullres / 2) * sf
angles <- c(11 * pi / 6, 3 * pi / 2, pi / 2, 5 * pi / 6, pi / 6, 7 * pi / 6)
angles <- angles - (pi / 3)
# angles <- angles[c(2, 1, 4, 3, 6, 5)] ## Flip internal layout
## ggforce::geom_arc() starts with 0 along the y-axis (90 in polar coordinates)
## and sweeps clockwise instead of counter clockwise
angle_transform <- function(x) {
- (x - pi / 2)
}
pixels_per_micron <- scalefactors$spot_diameter_fullres / 55
hires_pixels_per_mm <- ceiling((1000 * pixels_per_micron) * scalefactors$tissue_hires_scalef)
hires_interspot_dist <- hires_pixels_per_mm * 0.1
cancer_stroma2 = ifelse(sce.enhanced$spatial.cluster %in% c(3,4,6), "High", "Low")
sce.enhanced$class = factor(cancer_stroma2)
spot_coords <- colData(sce) %>%
as.data.frame() %>%
mutate(spot.idx=row_number()) %>%
select(spot.idx, imagerow, imagecol)
edata <- colData(sce.enhanced) %>%
as.data.frame() %>%
dplyr::rename(imagerow.subspot=imagerow,
imagecol.subspot=imagecol) %>%
left_join(spot_coords, by=c("spot.idx")) %>%
mutate_at(c("imagerow", "imagecol"), ~ .x * scalefactors$tissue_hires_scalef) %>%
mutate(start_angle=angles[subspot.idx],
end_angle=start_angle + (pi / 3),
x_start = imagecol + hires_radius * cos(start_angle),
y_start = (-imagerow) + hires_radius * sin(start_angle),) %>%
mutate_at(c("start_angle", "end_angle"), angle_transform) %>%
mutate_at("spatial.cluster", function(x) factor(x, levels=seq_len(10)))
ROI1 = sce$imagecol*scalefactors$tissue_hires_scalef>250 & sce$imagecol*scalefactors$tissue_hires_scalef<(250+hires_pixels_per_mm) & sce$imagerow*scalefactors$tissue_hires_scalef>970 & sce$imagerow*scalefactors$tissue_hires_scalef<(970+hires_pixels_per_mm)
subset.sce.enhanced1 = sce.enhanced[,ROI1]
overlay.ROI1.enhanced = ggplot(edata %>% filter(spot.idx %in% subset.sce.enhanced1$spot.idx)) +
annotation_raster(img_8kv2, 1, 2000, -2000, -1) +
geom_arc(aes(x0=imagecol, y0=-imagerow, r=hires_radius,
start=start_angle, end=end_angle, color=class),
size=0.8) +
geom_segment(aes(x=imagecol, y=-imagerow, xend=x_start, yend=y_start, color=class),
size=0.8) +
scale_color_viridis_d(direction = -1, option = "C") +
coord_fixed() +
theme_void() +
guides(col = F) +
labs(color = "CD45 cluster")
Wonderful, thank you so much @edward130603!
Hi, thank you for the great tool!
I wonder, do you mind sharing your code that was used to create Fig.4d and 4i (subspot level) in your Nat Biotechnol paper?
I've tried to use ggplot to recreate scatterplots and overlay to the visium tissue image, but my scaling of triangle shapes to different directions with 6 in a group was not successful. I think your code will be a great help for me to approach the desired visualization outcomes.
Thank you so much!