<- c("fs", "futile.logger", "configr", "stringr", "ggpubr", "ggthemes", "SingleCellExperiment",
pkgs "RColorBrewer", "vroom", "jhtools", "glue", "jhuanglabHyperion", "openxlsx", "ggsci", "ggraph",
"patchwork", "cowplot", "tidyverse", "dplyr", "rstatix", "magrittr", "igraph","tidygraph")
suppressMessages(conflicted::conflict_scout())
for (pkg in pkgs){
suppressPackageStartupMessages(library(pkg, character.only = T))
}<- "./results/sup_figure7" %>% checkdir
res_dir <- "./data" %>% checkdir
dat_dir <- "./config" %>% checkdir
config_dir
#colors config
<- glue::glue("{config_dir}/configs.yaml")
config_fn <- jhtools::show_me_the_colors(config_fn, "stype3")
stype3_cols <- jhtools::show_me_the_colors(config_fn, "cell_type_new")
ctype10_cols <- jhtools::show_me_the_colors(config_fn, "meta_color")
meta_cols
#read in coldata
<- readr::read_csv(glue::glue("{dat_dir}/sce_coldata.csv"))
coldat <- readxl::read_excel(glue::glue("{dat_dir}/meta_clu.xlsx")) %>% dplyr::select(-9)
meta_clu <- readr::read_rds(glue::glue("{dat_dir}/ci_list.rds"))
ci <- readr::read_rds(glue::glue("{dat_dir}/sample_chemo_type_list.rds"))
sample_chemo_type_list <- readr::read_rds(glue::glue("{dat_dir}/metadata.rds"))
metadata
# function
<- function(x, conditionCol, group, colors, threshold = 0.1, selfDe = T){
interaction_plot <- "from_meta"
from <- "to_meta"
to <- "metacluster_"
fn <- colors
cols
if (selfDe == TRUE) {
<- "filteredself"
fil <- x %>% dplyr::filter(.data[[from]] != .data[[to]]) %>%
dt_cl group_by(.data[[from]], .data[[to]], .data[[conditionCol]]) %>%
summarise(N = n()) %>% left_join(x %>% dplyr::filter(.data[[from]] != .data[[to]]) %>%
group_by(.data[[conditionCol]]) %>%
summarise(NT = n()), by = conditionCol) %>%
::mutate(pct = N/NT * 100)
dplyrelse {
} <- ""
fil <- x %>% group_by(.data[[from]], .data[[to]], .data[[conditionCol]]) %>%
dt_cl summarise(N = n()) %>% left_join(x %>% group_by(.data[[conditionCol]]) %>%
summarise(NT = n()), by = conditionCol) %>%
::mutate(pct = N/NT * 100)
dplyr
}
<- dt_cl %>% group_by(.data[[conditionCol]])
dt_cl <- dt_cl %>% dplyr::filter(pct > 0.1) %>%
glist group_map(~as_tbl_graph(dplyr::select(.,c(.data[[from]], .data[[to]], pct))))
<- dt_cl %>% group_keys()
gkeys <- list()
plist for(i in 1:length(glist)){
<- glist[[i]]
g if(length(g) == 0) next
<- paste0(fn, group, "_", gkeys[i,][[conditionCol]], "_", fil)
names <- ggraph(g, "stress") +
plist[[names]] geom_edge_link(aes(edge_width = pct, alpha = pct)) +
geom_node_point(aes(color = name), size = 3) +
#geom_node_text(aes(label = name), size = 4, repel = T, colour = "red") +
scale_edge_width("interaction percentage", range = c(0.2, 1.5),
breaks = c(0.1,1,3,5,10,15,20,25),limits=c(0.1, 25), #oob = scales::squish,
guide = guide_legend(title.theme = element_text(size = 8),
ncol = 1, byrow = FALSE, keywidth = 0.8,
keyheight = 0.1)) +
scale_edge_alpha("interaction percentage", range = c(0.1, 1.5),
breaks = c(0.1,1,3,5,10,15,20,25),
limits=c(0.1, 25), oob = scales::squish) +
scale_color_manual(limits = as.factor(V(g)$name), values = colors) +
theme_graph(base_family = "Helvetica", base_size =8) +
guides(col = guide_legend(title = "interaction", ncol = 1,
byrow = FALSE, keywidth = 0.1, keyheight = 0.1,
override.aes = list(size=1),
title.theme = element_text(size = 8)))
}return(plist)
}
sup_figure7
sup_figure7a
<- c(list("nerve_invasion"), list("vascular_invasion"),
keycols_list list("lymph_metastasis_status"), list("size_alteration"),
list("size_alteration"), list("pfs_group"))
<-c("nerve_invasion", "vascular_invasion", "lymph_metastasis",
compare_groups "chemo_outcome_before", "chemo_outcome_after", "pfs_analysis")
names(keycols_list) <- compare_groups
<- rev(c("MC-macro-c2", "MC-macro-c1", "MC-immune-myeloid",
meta_cluster_names "MC-immune-enriched", "MC-stroma-CAF", "MC-stroma-macro",
"MC-stroma-mCAF", "MC-tumor-frontline", "MC-tumor-core"))
<- coldat %>%
total_group ::select(c(sample_id, sample_tiff_id, patient_id, cell_id, stype2,
dplyr%>%
cluster_names, meta_cluster)) na.omit() %>% dplyr::filter(meta_cluster %in% meta_cluster_names)
<- list("nerve_invasion" = c("1", "0"),
factor_list "vascular_invasion" = c("1", "0"),
"lymph_metastasis" = c("1", "0"),
"chemo_outcome_before" = c("increase", "decrease"),
"chemo_outcome_after" = c("increase", "decrease"),
"pfs_analysis" = c("short","long"))
<- list()
plist for(compare_group in compare_groups){
#cli::cli_h1(compare_group)
<- metadata[[compare_group]]
groups <- keycols_list[[compare_group]]
gp_key ::cli_alert_info(gp_key)
cli<- groups %>% drop_na(all_of(gp_key))
groups
<- total_group %>% dplyr::filter(patient_id %in% groups$patient_id)
dt_groups if (compare_group == "chemo_outcome_before") {
<- dt_groups %>% dplyr::filter(stype2 %in% c("before_chemo","puncture_pdac"))
dt_groups else if (compare_group %notin% c("paired_after_neoadj", "mpc_primary_metastasis", "tumor_para")) {
} <- dt_groups %>% dplyr::filter(stype2 %in% c("after_chemo","tumor"))
dt_groups
}
<- dt_groups %>%
dat_plot group_by(meta_cluster, sample_tiff_id) %>%
summarise(nc = n()) %>%
group_by(sample_tiff_id) %>%
::mutate(nt = sum(nc)) %>%
dplyr::mutate(pro = nc/nt) %>% ungroup()
dplyr$meta_cluster <- factor(dat_plot$meta_cluster, levels = meta_cluster_names)
dat_plot
<- coldat[, c("sample_tiff_id", "patient_id")] %>% distinct()
metainfo <- inner_join(dat_plot, metainfo, by = "sample_tiff_id") %>%
dat_plot inner_join(groups, by = "patient_id") %>% distinct()
<- factor(dat_plot[[gp_key]], levels = factor_list[[compare_group]])
dat_plot[[gp_key]] $meta_cluster <- fct_relevel(dat_plot$meta_cluster, meta_cluster_names)
dat_plot
<- ggboxplot(dat_plot, x = "meta_cluster", y = "pro", fill = gp_key, outlier.shape = NA,
p palette = pal_nejm("default")(3), xlab = NULL,size = 0.2) + theme_classic() +
theme(strip.placement = "outside",
panel.spacing = unit(3, "points"),
strip.background = element_blank(),
strip.text = element_text(face = "bold", size = 5),
axis.text.x = element_text(size = 10), axis.text.y = element_text(size = 10),
axis.line.x = element_line(size = 0.4),
axis.line.y = element_line(size = 0.4),
legend.position="right") +
labs(x= NULL, y = NULL)
<- expr(pro ~ !!ensym(gp_key))
exp1 <- dat_plot %>%
stat_test group_by(meta_cluster) %>% rstatix::wilcox_test(eval(exp1), p.adjust.method = "none")
<- stat_test %>% mutate(p.adj.signif = case_when(p >= 0.05 ~ "ns",
stat_test >= 0.01 & p < 0.05 ~ "*",
p >= 0.001 & p < 0.01 ~ "**",
p >= 0.0001 & p < 0.001 ~ "***",
p < 0.0001 ~ "****",
p TRUE ~ "ns"))
<- stat_test %>%
stat_test add_xy_position(x = "meta_cluster", dodge = 0.8)
<- p +
p1 stat_pvalue_manual(
tip.length = 0.01, hide.ns = T, label = "p.adj.signif",
stat_test, coord.flip = TRUE
+ coord_flip() +
) guides(fill = guide_legend(reverse = TRUE))
#ggsave(glue("metacluster_{compare_group}.pdf"), p1, width = 6, height = 6)
<- p1
plist[[compare_group]]
}<- (plist[[1]]|plist[[2]]|plist[[3]])/(plist[[4]]|plist[[5]]|plist[[6]]) +
pc plot_layout(guides = 'collect', widths = c(2, 2.5, 2.5, 2.5))
ggsave(glue::glue("{res_dir}/sup_figure7a_wilct.pdf"), pc, width = 10.5, height = 5)
pc
sup_figure7b
# clinical metadata
#chemo_outcome_before
<- metadata[["chemo_outcome_before"]][c("patient_id", "size_alteration")]
dt_cob <- coldat %>% dplyr::filter(patient_id %in% dt_cob$patient_id) %>%
dt_cob ::select(sample_id, stype2, patient_id, sample_tiff_id) %>% unique() %>%
dplyrleft_join(dt_cob, by = "patient_id") %>%
::filter(stype2 %in% c("before_chemo", "puncture_pdac"))
dplyr
#chemo_outcome_after
<- metadata[["chemo_outcome_after"]][c("patient_id", "size_alteration")]
dt_coa <- coldat %>% dplyr::filter(patient_id %in% dt_coa$patient_id) %>%
dt_coa ::select(sample_id, stype2, patient_id, sample_tiff_id) %>% unique() %>%
dplyrleft_join(dt_coa, by = "patient_id") %>%
::filter(stype2 %in% c("after_chemo", "tumor"))
dplyr
<- list(dt_cob, dt_coa)
dt_list names(dt_list) <- c("chemo_outcome_before", "chemo_outcome_after")
for (i in 1:length(dt_list)) {
<- left_join(dt_list[[i]], do.call("rbind", ci),
dt_list[[i]] by = "sample_tiff_id") %>% dplyr::select(-c(from, to))
<- dt_list[[i]] %>% left_join(meta_clu[, c("old_cluster_name", "meta_short_new")],
dt_list[[i]] by = c("from_cluster" = "old_cluster_name")) %>%
::rename("from_meta_old" = "from_meta", "from_meta" = "meta_short_new") %>%
dplyrleft_join(meta_clu[, c("old_cluster_name", "meta_short_new")],
by = c("to_cluster" = "old_cluster_name")) %>%
::rename("to_meta_old" = "to_meta", "to_meta" = "meta_short_new")
dplyr
"size_alteration"]] <- factor(dt_list[[i]][["size_alteration"]],
dt_list[[i]][[levels = factor_list[[names(dt_list)[i]]])
}
<- list(c("chemo_outcome_before", "size_alteration"),
group_list c("chemo_outcome_after", "size_alteration"))
<- purrr::map2(dt_list, group_list, ~interaction_plot(x = .x, conditionCol = .y[2],
p1 group = .y[1], colors = meta_cols))
names(p1) <- c("chemo_outcome_before", "chemo_outcome_after")
#sub keycols
<- keycols_list[c("nerve_invasion", "vascular_invasion", "lymph_metastasis", "pfs_analysis")]
keycols_sub
<- list()
dt_list for (i in 1:length(keycols_sub)) {
<- metadata[[names(keycols_sub[i])]][c("patient_id", keycols_sub[[i]])]
dt_list[[i]] <- coldat %>% dplyr::filter(patient_id %in% dt_list[[i]]$patient_id) %>%
dt_list[[i]] ::select(sample_id, stype2, patient_id, sample_tiff_id) %>% unique() %>%
dplyrleft_join(dt_list[[i]], by = "patient_id") %>% dplyr::filter(stype2 %in% c("after_chemo", "tumor"))
}
for (i in 1:length(dt_list)) {
<- left_join(dt_list[[i]], do.call("rbind", ci), by = "sample_tiff_id") %>%
dt_list[[i]] ::select(-c(from, to))
dplyr<- dt_list[[i]] %>% left_join(meta_clu[, c("old_cluster_name", "meta_short_new")],
dt_list[[i]] by = c("from_cluster" = "old_cluster_name")) %>%
::rename("from_meta_old" = "from_meta", "from_meta" = "meta_short_new") %>%
dplyrleft_join(meta_clu[, c("old_cluster_name", "meta_short_new")],
by = c("to_cluster" = "old_cluster_name")) %>%
::rename("to_meta_old" = "to_meta", "to_meta" = "meta_short_new")
dplyr
<- factor(dt_list[[i]][[keycols_sub[[i]]]],
dt_list[[i]][[keycols_sub[[i]]]] levels = factor_list[[names(keycols_sub)[i]]])
}
<- list()
p_list for (i in 1:length(dt_list)) {
for (j in 1:length(keycols_sub[[i]])) {
<- str_c(names(keycols_sub)[i],keycols_sub[[i]][j], sep= "_")
group1 <- interaction_plot(dt_list[[i]] %>%
p_list[[group1]] ::select(sample_id, keycols_sub[[i]][j], sample_tiff_id,
dplyr%>% na.omit(),
from_cluster, to_cluster, from_meta, to_meta) conditionCol = keycols_sub[[i]][j], group = group1, colors = meta_cols)
}
}
<- c(p_list, p1)
p_list
<- "
lo ABC
DEF
GHI
JKL
"
<- p_list[["nerve_invasion_nerve_invasion"]][[1]] + p_list[["vascular_invasion_vascular_invasion"]][[1]] +
p2 "lymph_metastasis_lymph_metastasis_status"]][[1]] + p_list[["nerve_invasion_nerve_invasion"]][[2]] +
p_list[["vascular_invasion_vascular_invasion"]][[2]] + p_list[["lymph_metastasis_lymph_metastasis_status"]][[2]] +
p_list[["chemo_outcome_before"]][[1]] + p_list[["chemo_outcome_after"]][[1]] +
p_list[["pfs_analysis_pfs_group"]][[1]] + p_list[["chemo_outcome_before"]][[2]] +
p_list[["chemo_outcome_after"]][[2]] + p_list[["pfs_analysis_pfs_group"]][[2]] + plot_layout(design = lo, guides='collect')
p_list[[
ggsave(glue::glue("{res_dir}/sup_figure7b.pdf"), p2, width = 12, height = 9.5)
p2