# Install any missing packages automatically -----------------------------
<- c(
pkgs "fs", "futile.logger", "configr", "stringr", "ggpubr", "ggthemes",
"jhtools", "glue", "ggsci", "patchwork", "tidyverse", "dplyr", "Seurat",
"paletteer", "cowplot", "ComplexHeatmap", "circlize", "plot1cell"
)<- pkgs[!pkgs %in% installed.packages()[, "Package"]]
to_install 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))
<- "collabrators"
project <- "wangwenjie"
dataset <- "mouse"
species
<- glue("~/projects/{project}/analysis/{dataset}/{species}/figures/fig1")
workdir ::dir_create(workdir)
fssetwd(workdir)
<- "~/projects/collabrators/code/wangwenjie/mouse/figures/configs.yaml"
yaml_fn
# Retrieve colour palettes from YAML -------------------------------------
<- jhtools::show_me_the_colors(config_fn = yaml_fn, "tissue")
cols_tissue <- jhtools::show_me_the_colors(config_fn = yaml_fn, "stage")
stg_cols
# Shared ggplot theme -----------------------------------------------------
<- theme_classic(base_size = 8) +
my_theme1 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)
<- "~/projects/collabrators/analysis/wangwenjie/mouse/figures/rds/mmu_visium_obj_lst.rds"
rds_fn1 <- readr::read_rds(rds_fn1)
mmu_visium <- c("E95", "E115", "E135")
samples_mmu # Generate each spatial panel and save individual PDFs -------------------
<- lapply(samples_mmu, \(samp) {
plst1 <- mmu_visium[[samp]]
obj1
# Dynamic sizing per sample
if (samp == "E95") {
<- 1.6; plot_width <- 2; plot_height <- 2
scale_fct else if (samp == "E115") {
} <- 1.1; plot_width <- 4; plot_height <- 4
scale_fct else {
} <- 2.0; plot_width <- 6; plot_height <- 6
scale_fct
}
<- SpatialDimPlot(obj1,
p pt.size.factor = scale_fct,
image.alpha = 0,
group.by = "tissuetype") &
scale_fill_manual(values = cols_tissue) &
& NoAxes() & NoLegend()
my_theme1
ggsave(glue("fig1b_mouse_tissuetype_spatial_{samp}.pdf"), p,
width = plot_width, height = plot_height)
p|> setNames(samples_mmu)
})
# Build universal legend -------------------------------------------------
<- bind_rows(lapply(samples_mmu, \(samp) {
tbl1 tibble(tissuetype = unique(mmu_visium[[samp]]$tissuetype))
}))
<- ggplot(tbl1, aes(1, tissuetype, color = tissuetype)) +
pt1 geom_point(size = 0.4) +
scale_color_manual(values = cols_tissue) +
+
my_theme1 coord_fixed() +
labs(color = "")
<- cowplot::get_legend(pt1)
legend "legend"]] <- cowplot::plot_grid(NULL, legend, NULL,
plst1[[ncol = 3, rel_widths = c(.1, 1, .1))
ggsave("fig1b_legend_mouse_tissuetype_spatial.pdf", plst1[["legend"]],
width = 2, height = 2)
# Assemble final panel ----------------------------------------------------
<- cowplot::plot_grid(plotlist = plst1, nrow = 1,
fig1b_mouse 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)
<- c("yao1", "yao2", "yao5")
samples_hsa <- "~/projects/collabrators/analysis/wangwenjie/mouse/figures/rds/hsa_visium_obj_lst.rds"
rds_fn2 <- readr::read_rds(rds_fn2)
hsa_visium
<- lapply(samples_hsa, \(samp) {
plst1 <- hsa_visium[[samp]]
obj1
if (samp == "yao1") {
<- 1.8; plot_width <- 2; plot_height <- 2
scale_fct else if (samp == "yao2") {
} <- 1.3; plot_width <- 4; plot_height <- 4
scale_fct else {
} <- 1.5; plot_width <- 6; plot_height <- 6
scale_fct
}
<- SpatialDimPlot(obj1,
p pt.size.factor = scale_fct,
image.alpha = 0,
group.by = "tissue") &
scale_fill_manual(values = cols_tissue) &
& NoAxes() & NoLegend()
my_theme1
ggsave(glue("fig1c_human_tissue_spatial_{samp}.pdf"), p,
width = plot_width, height = plot_height)
p|> setNames(samples_hsa)
})
<- lapply(samples_hsa, \(samp) {
tbl1 tibble(tissue = unique(hsa_visium[[samp]]$tissue))
|> bind_rows() |> dplyr::distinct()
})
<- ggplot(tbl1, aes(1, tissue, color = tissue)) +
pt1 geom_point(size = 0.4) +
scale_color_manual(values = cols_tissue) +
+
my_theme1 coord_fixed() +
labs(color = "")
<- cowplot::get_legend(pt1)
legend "legend"]] <- cowplot::plot_grid(NULL, legend, NULL,
plst1[[ncol = 3, rel_widths = c(.1, 1, .1))
ggsave("fig1c_legend_human_tissue_spatial.pdf", plst1[["legend"]],
width = 2, height = 2)
<- cowplot::plot_grid(plotlist = plst1, nrow = 1,
fig1c_human 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
<- "~/projects/collabrators/analysis/wangwenjie/mouse/figures/rds/mmu_visium_merged_obj.rds"
rds_fn4 <- readr::read_rds(rds_fn4)
visium_mmu_mrg
<- prepare_circlize_data(visium_mmu_mrg, scale = 0.7)
circ_dat <- cols_tissue[sort(unique(circ_dat$tissuetype))]
clust_cols <- Legend(at = names(stg_cols), type = "grid",
lgd_squre 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
<- "~/projects/collabrators/analysis/wangwenjie/mouse/figures/rds/mmu_visium_mtb_gene_merged_obj.rds"
rds_fn5 <- readr::read_rds(rds_fn5)
seu_new3_mrg
<- prepare_circlize_data(seu_new3_mrg, scale = 0.7)
circ_dat <- cols_tissue[sort(unique(circ_dat$tissuetype))]
clust_cols
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
<- "~/projects/collabrators/analysis/wangwenjie/mouse/figures/rds/mouse_mz_obj_merged.rds"
rds_fn2 <- readr::read_rds(rds_fn2)
mz_obj_mrg
<- prepare_circlize_data(mz_obj_mrg, scale = 0.7)
circ_dat <- cols_tissue[sort(unique(circ_dat$tissuetype))]
clust_cols
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
<- "~/projects/collabrators/analysis/wangwenjie/mouse/figures/rds/human_visium_obj_merged.rds"
rds_fn6 <- readr::read_rds(rds_fn6)
seu_new2_mrg
<- prepare_circlize_data(seu_new2_mrg, scale = 0.7)
circ_dat <- cols_tissue[sort(unique(circ_dat$tissue))]
clust_cols
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
<- "~/projects/collabrators/analysis/wangwenjie/mouse/figures/rds/hsa_visium_mtb_gene_merged_obj.rds"
rds_fn8 <- readr::read_rds(rds_fn8)
seu_new3_mrg
<- prepare_circlize_data(seu_new3_mrg, scale = 0.7)
circ_dat <- cols_tissue[sort(unique(circ_dat$tissue))]
clust_cols
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
<- "~/projects/collabrators/analysis/wangwenjie/mouse/figures/rds/human_mz_obj_merged.rds"
rds_fn4 <- readr::read_rds(rds_fn4)
seu_mrg2
<- prepare_circlize_data(seu_mrg2, scale = 0.7)
circ_dat <- cols_tissue[sort(unique(circ_dat$tissue))]
clust_cols
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()