# Install any missing packages automatically -----------------------------
pkgs <- c(
"fs", "futile.logger", "configr", "stringr", "ggpubr", "ggthemes",
"jhtools", "glue", "ggsci", "patchwork", "tidyverse", "dplyr", "Seurat",
"paletteer", "cowplot", "ComplexHeatmap", "circlize", "plot1cell"
)
to_install <- pkgs[!pkgs %in% installed.packages()[, "Package"]]
if (length(to_install)) install.packages(to_install, repos = "https://cloud.r-project.org")
# Load all packages quietly ---------------------------------------------
suppressPackageStartupMessages(lapply(pkgs, library, character.only = TRUE))
project <- "collabrators"
dataset <- "wangwenjie"
species <- "mouse"
workdir <- glue("~/projects/{project}/analysis/{dataset}/{species}/figures/fig1")
fs::dir_create(workdir)
setwd(workdir)
yaml_fn <- "~/projects/collabrators/code/wangwenjie/mouse/figures/configs.yaml"
# Retrieve colour palettes from YAML -------------------------------------
cols_tissue <- jhtools::show_me_the_colors(config_fn = yaml_fn, "tissue")
stg_cols <- jhtools::show_me_the_colors(config_fn = yaml_fn, "stage")
# Shared ggplot theme -----------------------------------------------------
my_theme1 <- theme_classic(base_size = 8) +
theme(
legend.key.size = unit(3, "mm"),
axis.text = element_text(color = "black"),
axis.ticks = element_line(color = "black"),
plot.title = element_text(hjust = 0.5)
)13 Figure 1
13.1 Figure 1 | The spatiotemporal transcriptomic and metabolomic atlases during organogenesis
The document is self-contained: it installs missing packages (if running on a local machine), loads data, generates all panels, and writes the final PDFs to the working directory that is automatically created. Below is the code generating Figure 1:
13.2 Figure 1B – Mouse tissue atlas (VISIUM)
rds_fn1 <- "~/projects/collabrators/analysis/wangwenjie/mouse/figures/rds/mmu_visium_obj_lst.rds"
mmu_visium <- readr::read_rds(rds_fn1)
samples_mmu <- c("E95", "E115", "E135")
# Generate each spatial panel and save individual PDFs -------------------
plst1 <- lapply(samples_mmu, \(samp) {
obj1 <- mmu_visium[[samp]]
# Dynamic sizing per sample
if (samp == "E95") {
scale_fct <- 1.6; plot_width <- 2; plot_height <- 2
} else if (samp == "E115") {
scale_fct <- 1.1; plot_width <- 4; plot_height <- 4
} else {
scale_fct <- 2.0; plot_width <- 6; plot_height <- 6
}
p <- SpatialDimPlot(obj1,
pt.size.factor = scale_fct,
image.alpha = 0,
group.by = "tissuetype") &
scale_fill_manual(values = cols_tissue) &
my_theme1 & NoAxes() & NoLegend()
ggsave(glue("fig1b_mouse_tissuetype_spatial_{samp}.pdf"), p,
width = plot_width, height = plot_height)
p
}) |> setNames(samples_mmu)
# Build universal legend -------------------------------------------------
tbl1 <- bind_rows(lapply(samples_mmu, \(samp) {
tibble(tissuetype = unique(mmu_visium[[samp]]$tissuetype))
}))
pt1 <- ggplot(tbl1, aes(1, tissuetype, color = tissuetype)) +
geom_point(size = 0.4) +
scale_color_manual(values = cols_tissue) +
my_theme1 +
coord_fixed() +
labs(color = "")
legend <- cowplot::get_legend(pt1)
plst1[["legend"]] <- cowplot::plot_grid(NULL, legend, NULL,
ncol = 3, rel_widths = c(.1, 1, .1))
ggsave("fig1b_legend_mouse_tissuetype_spatial.pdf", plst1[["legend"]],
width = 2, height = 2)
# Assemble final panel ----------------------------------------------------
fig1b_mouse <- cowplot::plot_grid(plotlist = plst1, nrow = 1,
rel_widths = c(.8, 1, 1.2, 1))
ggsave("fig1b_mouse_tissuetype.pdf", fig1b_mouse,
width = unit(8, "cm"), height = unit(3, "cm"))13.3 Figure 1C – Human tissue atlas (VISIUM)
samples_hsa <- c("yao1", "yao2", "yao5")
rds_fn2 <- "~/projects/collabrators/analysis/wangwenjie/mouse/figures/rds/hsa_visium_obj_lst.rds"
hsa_visium <- readr::read_rds(rds_fn2)
plst1 <- lapply(samples_hsa, \(samp) {
obj1 <- hsa_visium[[samp]]
if (samp == "yao1") {
scale_fct <- 1.8; plot_width <- 2; plot_height <- 2
} else if (samp == "yao2") {
scale_fct <- 1.3; plot_width <- 4; plot_height <- 4
} else {
scale_fct <- 1.5; plot_width <- 6; plot_height <- 6
}
p <- SpatialDimPlot(obj1,
pt.size.factor = scale_fct,
image.alpha = 0,
group.by = "tissue") &
scale_fill_manual(values = cols_tissue) &
my_theme1 & NoAxes() & NoLegend()
ggsave(glue("fig1c_human_tissue_spatial_{samp}.pdf"), p,
width = plot_width, height = plot_height)
p
}) |> setNames(samples_hsa)
tbl1 <- lapply(samples_hsa, \(samp) {
tibble(tissue = unique(hsa_visium[[samp]]$tissue))
}) |> bind_rows() |> dplyr::distinct()
pt1 <- ggplot(tbl1, aes(1, tissue, color = tissue)) +
geom_point(size = 0.4) +
scale_color_manual(values = cols_tissue) +
my_theme1 +
coord_fixed() +
labs(color = "")
legend <- cowplot::get_legend(pt1)
plst1[["legend"]] <- cowplot::plot_grid(NULL, legend, NULL,
ncol = 3, rel_widths = c(.1, 1, .1))
ggsave("fig1c_legend_human_tissue_spatial.pdf", plst1[["legend"]],
width = 2, height = 2)
fig1c_human <- cowplot::plot_grid(plotlist = plst1, nrow = 1,
rel_widths = c(.6, 1, 1.2, 1))
ggsave("fig1c_human_tissue.pdf", fig1c_human,
width = unit(8, "cm"), height = unit(3, "cm"))13.4 Figure 1D – plot1cell circle plots
Mouse – all genes
rds_fn4 <- "~/projects/collabrators/analysis/wangwenjie/mouse/figures/rds/mmu_visium_merged_obj.rds"
visium_mmu_mrg <- readr::read_rds(rds_fn4)
circ_dat <- prepare_circlize_data(visium_mmu_mrg, scale = 0.7)
clust_cols <- cols_tissue[sort(unique(circ_dat$tissuetype))]
lgd_squre <- Legend(at = names(stg_cols), type = "grid",
legend_gp = gpar(fill = stg_cols),
title_position = "topleft", title = "stage")
pdf("fig1d_mouse_plot1cell_all_genes.pdf", width = 8, height = 8)
plot_circlize(circ_dat, do.label = TRUE, pt.size = 0.5,
col.use = clust_cols, bg.color = "white",
kde2d.n = 200, repel = TRUE, label.cex = 0.9)
add_track(circ_dat, group = "stage",
colors = stg_cols[order(names(stg_cols))], track_num = 2)
draw(lgd_squre, x = unit(40, "mm"), y = unit(12, "mm"),
just = c("right", "bottom"))
dev.off()13.5 Mouse – metabolic genes
rds_fn5 <- "~/projects/collabrators/analysis/wangwenjie/mouse/figures/rds/mmu_visium_mtb_gene_merged_obj.rds"
seu_new3_mrg <- readr::read_rds(rds_fn5)
circ_dat <- prepare_circlize_data(seu_new3_mrg, scale = 0.7)
clust_cols <- cols_tissue[sort(unique(circ_dat$tissuetype))]
pdf("fig1d_mouse_plot1cell_mtb_genes.pdf", width = 8, height = 8)
plot_circlize(circ_dat, do.label = TRUE, pt.size = 0.5,
col.use = clust_cols, bg.color = "white",
kde2d.n = 200, repel = TRUE, label.cex = 0.9)
add_track(circ_dat, group = "stage", colors = stg_cols, track_num = 2)
draw(lgd_squre, x = unit(40, "mm"), y = unit(12, "mm"),
just = c("right", "bottom"))
dev.off()13.6 Mouse – M/Z data
rds_fn2 <- "~/projects/collabrators/analysis/wangwenjie/mouse/figures/rds/mouse_mz_obj_merged.rds"
mz_obj_mrg <- readr::read_rds(rds_fn2)
circ_dat <- prepare_circlize_data(mz_obj_mrg, scale = 0.7)
clust_cols <- cols_tissue[sort(unique(circ_dat$tissuetype))]
pdf("fig1c_plot1cell_all_mz_mrg.pdf", width = 5.5, height = 5.5)
plot_circlize(circ_dat, do.label = TRUE, pt.size = 0.4,
col.use = clust_cols, bg.color = "white",
contour.nlevels = 100, kde2d.n = 2000,
repel = TRUE, label.cex = 0.7)
add_track(circ_dat, group = "stage",
colors = stg_cols[sort(unique(circ_dat$stage))], track_num = 2)
draw(lgd_squre, x = unit(25, "mm"), y = unit(8, "mm"),
just = c("right", "bottom"))
dev.off()13.7 Human – all genes
rds_fn6 <- "~/projects/collabrators/analysis/wangwenjie/mouse/figures/rds/human_visium_obj_merged.rds"
seu_new2_mrg <- readr::read_rds(rds_fn6)
circ_dat <- prepare_circlize_data(seu_new2_mrg, scale = 0.7)
clust_cols <- cols_tissue[sort(unique(circ_dat$tissue))]
pdf("fig1d_human_plot1cell_all_genes.pdf", width = 8, height = 8)
plot_circlize(circ_dat, do.label = TRUE, pt.size = 0.5,
col.use = clust_cols, bg.color = "white",
kde2d.n = 200, repel = TRUE, label.cex = 0.9)
add_track(circ_dat, group = "stage",
colors = stg_cols[order(names(stg_cols))], track_num = 2)
draw(lgd_squre, x = unit(40, "mm"), y = unit(12, "mm"),
just = c("right", "bottom"))
dev.off()13.8 Human – metabolic genes
rds_fn8 <- "~/projects/collabrators/analysis/wangwenjie/mouse/figures/rds/hsa_visium_mtb_gene_merged_obj.rds"
seu_new3_mrg <- readr::read_rds(rds_fn8)
circ_dat <- prepare_circlize_data(seu_new3_mrg, scale = 0.7)
clust_cols <- cols_tissue[sort(unique(circ_dat$tissue))]
pdf("fig1d_human_plot1cell_mtb_genes_merged.pdf", width = 8, height = 8)
plot_circlize(circ_dat, do.label = TRUE, pt.size = 0.5,
col.use = clust_cols, bg.color = "white",
kde2d.n = 200, repel = TRUE, label.cex = 0.9)
add_track(circ_dat, group = "stage",
colors = stg_cols[order(names(stg_cols))], track_num = 2)
draw(lgd_squre, x = unit(40, "mm"), y = unit(12, "mm"),
just = c("right", "bottom"))
dev.off()13.9 Human – M/Z data
rds_fn4 <- "~/projects/collabrators/analysis/wangwenjie/mouse/figures/rds/human_mz_obj_merged.rds"
seu_mrg2 <- readr::read_rds(rds_fn4)
circ_dat <- prepare_circlize_data(seu_mrg2, scale = 0.7)
clust_cols <- cols_tissue[sort(unique(circ_dat$tissue))]
pdf("fig1d_human_plot1cell_all_mz_mrg.pdf", width = 5.5, height = 5.5)
plot_circlize(circ_dat, do.label = TRUE, pt.size = 0.4,
col.use = clust_cols, bg.color = "white",
contour.nlevels = 100, kde2d.n = 2000,
repel = TRUE, label.cex = 0.7)
add_track(circ_dat, group = "stage",
colors = stg_cols[sort(unique(circ_dat$stage))], track_num = 2)
draw(lgd_squre, x = unit(25, "mm"), y = unit(8, "mm"),
just = c("right", "bottom"))
dev.off()