<- c("fs", "futile.logger", "configr", "stringr", "ggpubr", "ggthemes",
pkgs "vroom", "jhtools", "glue", "openxlsx", "ggsci", "patchwork", "cowplot",
"tidyverse", "dplyr", "SingleCellExperiment", "survminer", "survival")
suppressMessages(conflicted::conflict_scout())
for (pkg in pkgs){
suppressPackageStartupMessages(library(pkg, character.only = T))
}<- "./results/sup_figure10" %>% 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, "cell_type_new")
ctype10_cols <- jhtools::show_me_the_colors(config_fn, "meta_color")
meta_cols <- jhtools::show_me_the_colors(config_fn, "meta_merge")
meta_merge_cols
#read in coldata
<- readr::read_csv(glue::glue("{dat_dir}/sce_coldata.csv"))
coldat <- readr::read_csv(glue::glue("{dat_dir}/metadata_sinfo.csv"))
sinfo <- 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 <- readr::read_rds(glue::glue("{dat_dir}/pos_cell_list_10percent.rds")) pos_cells10
sup_figure10
sup_figure10a
<- coldat %>%
coldat ::mutate(meta_merge = case_when(meta_cluster %notin%
dplyrc("MC-tumor-frontline", "MC-stroma-macro", "MC-tumor-core") ~ "MC-others",
TRUE ~ meta_cluster))
<- list(c("MC-stroma-macro", "MC-tumor-frontline"), c("MC-stroma-macro", "MC-others"), c("MC-tumor-frontline", "MC-others"))
my_comparisons
#a
<- coldat %>% dplyr::filter(cell_type_new == "HLA-DR-CD163- mp") %>%
Macrophage_Dn_total ::filter(sample_id %in% sample_chemo_type_list$no_chemo_all) %>%
dplyrgroup_by(sample_id, meta_merge) %>% summarise(nt = n()) %>% ungroup()
<- list()
pl for (i in c("Arginase_1_pos_Macrophage", "EGFR_pos_Macrophage")) {
<- coldat %>% dplyr::filter(cell_type_new == "HLA-DR-CD163- mp") %>%
Macrophage_Dn_pos ::filter(sample_id %in% sample_chemo_type_list$no_chemo_all) %>%
dplyr::filter(cell_id %in% pos_cells10[[i]]) %>%
dplyrgroup_by(sample_id, meta_merge) %>% summarise(np = n()) %>% ungroup()
<- left_join(Macrophage_Dn_total, Macrophage_Dn_pos, by = c("sample_id", "meta_merge")) %>%
Macrophage_Dn_pos replace_na(list(np = 0)) %>% dplyr::mutate(prop = np/nt) %>%
ungroup() %>% dplyr::select(-c(nt, np)) %>%
::filter(meta_merge != "MC-tumor-core") %>%
dplyrgroup_by(meta_merge) %>% dplyr::mutate(gn = n()) %>% ungroup
$meta_merge <- factor(Macrophage_Dn_pos$meta_merge, levels = c("MC-tumor-frontline", "MC-stroma-macro", "MC-others"))
Macrophage_Dn_pos<- str_replace_all(i, c("_pos_" = "+ ", "Macrophage" = "Mp", "_" = "-"))
yname
<- ggplot(Macrophage_Dn_pos, aes(x = meta_merge, y = prop, fill = meta_merge, color = meta_merge)) +
pl[[i]] ::geom_flat_violin(aes(color = meta_merge), position = position_nudge(x = .25), bounds = c(0, 0.5)) +
introdatavizgeom_jitter(aes(color = meta_merge), width = 0.1, size = 0.25) +
geom_boxplot(width = .1, position = position_nudge(x = .25), fill = "white", linewidth = 0.25, outlier.shape = NA) +
scale_fill_manual(values = meta_merge_cols) + scale_color_manual(values = meta_merge_cols) +
scale_y_continuous(limits=c(0, 0.6), oob = scales::rescale_none) +
stat_compare_means(aes(label = "p.signif"), hide.ns = F, tip.length = 0.02, bracket.size = 0.1,
comparisons = my_comparisons, method = "wilcox.test",
size = 2, label.y = c(0.51,0.52,0.53)) +
::geom_dl(aes(label = gn),
directlabelsmethod = list("last.points", cex = .5)) +
ylab(glue::glue("Proportion of {yname}")) + theme_bmbdc() +
theme(axis.title.y = element_text(size = 6),
axis.text.y = element_text(size = 6),
axis.title.x = element_blank(),
axis.text.x = element_text(size = 6),
legend.position = "none")
ggsave(glue::glue("{res_dir}/sfig10a_{i}_sample_meta3_compare_vlnplot1.pdf"), pl[[i]], width = 3, height = 3)
}
"Arginase_1_pos_Macrophage"]] | pl[["EGFR_pos_Macrophage"]] pl[[
sup_figure10b
<- c("Arginase_1_pos_Macrophage", "PD_L1_pos_Macrophage", "CCR6_pos_Macrophage",
pos_mac "EGFR_pos_Macrophage", "E_Cadherin_pos_Macrophage", "Vimentin_pos_Macrophage")
<- coldat %>%
coldat ::mutate(cell_type11 = case_when(cell_type_new %in%
dplyrc("HLA-DR+CD163- mp", "HLA-DR+CD163+ mp", "HLA-DR-CD163+ mp") ~ "Other-mp",
%in% c("HLA-DR-CD163- mp") ~ "DN-mp",
cell_type_new TRUE ~ cell_type_new))
<- coldat %>% dplyr::filter(cell_type11 %in% c("DN-mp", "Other-mp")) %>%
cld_mac ::filter(sample_id %in% sample_chemo_type_list[["no_chemo_no_punc"]] &
dplyr%notin% "MC-tumor-core")
meta_merge
<- c("DN-mp" = "#BC3C29FF", "Other-mp" = "#0072B5FF")
mac_colors <- list()
pl for (i in pos_mac) {
<- cld_mac %>%
cld_mac_pos ::mutate(pos_pheno = case_when(cell_id %in% pos_cells10[[i]] ~ i,
dplyrTRUE ~ cell_type11))
<- cld_mac_pos %>% group_by(sample_id, meta_merge, cell_type11, pos_pheno) %>%
pos_prop_meta ::summarise(nc = n()) %>% group_by(sample_id, meta_merge, cell_type11) %>%
dplyr::mutate(nt = sum(nc), prop = nc/nt) %>% ungroup()
dplyr
$pos_pheno <- factor(pos_prop_meta$pos_pheno)
pos_prop_meta<- pos_prop_meta %>% group_by(sample_id, meta_merge, cell_type11) %>%
pos_prop_meta ::complete(pos_pheno, fill = list(prop = 0)) %>%
tidyrungroup() %>% dplyr::filter(pos_pheno %in% i) %>%
group_by(sample_id,meta_merge) %>%
::mutate(n = n(),
dplyrpos_pheno = str_replace_all(pos_pheno, c("_pos_" = "+ ", "Macrophage" = "Mp", "_" = "-"))) %>%
::filter(n == 2) %>% ungroup()
dplyr
$meta_merge <- factor(pos_prop_meta$meta_merge, levels = c("MC-tumor-frontline","MC-stroma-macro","MC-others"))
pos_prop_meta$cell_type11 <- factor(pos_prop_meta$cell_type11, levels = c("DN-mp", "Other-mp"))
pos_prop_meta<- pos_prop_meta %>%
stat_test group_by(meta_merge) %>% rstatix::wilcox_test(prop ~ cell_type11, paired = T)
<- 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_merge", dodge = 0.9, fun = "median_iqr")
rstatix
<- pos_prop_meta %>%
pos_prop_meta ::mutate(prop = case_when(prop > quantile(prop, prob = .98) ~ quantile(prop, prob = .98),
dplyrTRUE ~ prop))
$y.position <- 0.4
stat_test
<- ggplot(pos_prop_meta,
pl[[i]] aes(x = meta_merge, y = prop)) +
# introdataviz::geom_split_violin(aes(x = meta_merge, y = prop, fill = cell_type11),
# alpha = .6, trim = FALSE, linewidth = .2, color = "black") +
geom_boxplot(aes(fill = cell_type11), width = .5, show.legend = FALSE,
outlier.shape = NA, linewidth = .2, color = "black") +
stat_summary(aes(fill = cell_type11, color = cell_type11), fun.data = "mean_se", geom = "pointrange", show.legend = F,
position = position_dodge(.5), fatten = .2, size = .2, stroke = .5, linewidth = .2, color = "black") +
scale_fill_manual(values = mac_colors) +
scale_colour_manual(values = mac_colors) +
labs(y = glue::glue("Proportion of {unique(pos_prop_meta$pos_pheno)}")) +
scale_y_continuous(limits=c(0, 0.4), oob = scales::rescale_none) +
stat_pvalue_manual(stat_test, x = "meta_merge", tip.length = 0.01, hide.ns = F, label = "p.adj.signif", size = 2) +
theme_bmbdc() +
theme(title = element_text(size = 6),
axis.ticks = element_line(colour = "black"),
axis.title.y = element_text(size = 6),
axis.text.y = element_text(size = 6, colour = "black"),
axis.title.x = element_blank(),
axis.text.x = element_text(size = 6, colour = "black", angle = 20),
axis.line.x = element_line(linewidth = 0.4),
axis.line.y = element_line(linewidth = 0.4),
legend.position="top",
legend.text = element_text(size = 4),
legend.title = element_text(size = 4),
legend.key.size = unit(0.1, 'cm'))
ggsave(glue::glue("{res_dir}/sfig10b_mac_persample_paired_{i}_mac_prop_boxplot.pdf"), pl[[i]], width = 3, height = 3)
}
"Arginase_1_pos_Macrophage"]] | pl[["PD_L1_pos_Macrophage"]])/
(pl[["CCR6_pos_Macrophage"]] | pl[["EGFR_pos_Macrophage"]])/
(pl[["E_Cadherin_pos_Macrophage"]] | pl[["Vimentin_pos_Macrophage"]]) (pl[[
sup_figure10c
# CD8T to Macrophage_other ----------------------------
<- readr::read_rds(glue::glue("{dat_dir}/distance_rds/SM_list_closecell_dist_k1_CD8T_to_Macrophage_oth.rds")) %>%
df_closecell_distance_SM do.call("rbind", .) %>% as_tibble() %>% dplyr::mutate(meta_merge = "MC-stroma-macro")
<- readr::read_rds(glue::glue("{dat_dir}/distance_rds/TB_list_closecell_dist_k1_CD8T_to_Macrophage_oth.rds")) %>%
df_closecell_distance_TB do.call("rbind", .) %>% as_tibble() %>% dplyr::mutate(meta_merge = "MC-tumor-frontline")
<- readr::read_rds(glue::glue("{dat_dir}/distance_rds/OthersnoBT_list_closecell_dist_k1_CD8T_to_Macrophage_oth.rds")) %>% do.call("rbind", .) %>% as_tibble() %>% dplyr::mutate(meta_merge = "MC-others")
df_closecell_distance_othersnoBT
<- rbind(df_closecell_distance_SM, df_closecell_distance_TB, df_closecell_distance_othersnoBT) %>% as_tibble()
df_closecell_distance <- df_closecell_distance %>%
df_dist_meta3 ::filter(from_cell %in% (coldat %>% dplyr::filter(sample_id %in% sample_chemo_type_list$no_chemo_all) %>% .$cell_id))
dplyr
<- ggdensity(df_dist_meta3, x = "distance",
p add = "mean",
color = "meta_merge",
palette = meta_merge_cols) +
scale_x_continuous(limits=c(0,200), oob = scales::rescale_none) +
theme(axis.title.y = element_blank(),
axis.text.y = element_text(size = 6),
axis.title.x = element_text(size = 6),
axis.text.x = element_text(size = 6),
legend.position = "none")
ggsave(glue::glue("{res_dir}/sup_figure10c_CD8T_dist_to_OthMac_3metas_density.pdf"), p, width = 3.5, height = 2.9)
p
sup_figure10d
<- c("DN-mp" = "#BC3C29FF", "Other-mp" = "#0072B5FF")
mac_colors <- c("TB_" = "MC-tumor-frontline", "SM_" = "MC-stroma-macro", "OthersnoBT_" = "MC-others")
meta_names
<- list()
pl for (r in c("SM_", "OthersnoBT_")) {
<- readr::read_rds(glue::glue("{dat_dir}/distance_rds/{r}list_closecell_dist_k1_CD8T_to_Macrophage_DN.rds")) %>%
df_dnmac do.call("rbind", .) %>% as_tibble()
<- readr::read_rds(glue::glue("{dat_dir}/distance_rds/{r}list_closecell_dist_k1_CD8T_to_Macrophage_oth.rds")) %>%
df_othmac do.call("rbind", .) %>% as_tibble()
<- rbind(df_dnmac, df_othmac) %>% as_tibble()
df_mac <- df_mac %>%
df_mac inner_join(coldat, by = c("to_cell" = "cell_id")) %>%
::filter(sample_id %in% c(sample_chemo_type_list[["chemo"]],
dplyr"no_chemo_no_punc"]])) %>%
sample_chemo_type_list[[::mutate(chemo = case_when(sample_id %in% sample_chemo_type_list[["chemo"]] ~ "Surgery after chemo",
dplyrTRUE ~ "Upfront surgery"))
#sample
<- df_mac %>% group_by(sample_id, cell_type11, chemo) %>%
df_mac_mean ::summarise(mean_dis = mean(distance)) %>%
dplyrgroup_by(sample_id, chemo) %>% dplyr::mutate(nc = n()) %>%
ungroup() %>% dplyr::filter(nc > 1)
$cell_type11 <- factor(df_mac_mean$cell_type11, levels = c("DN-mp", "Other-mp"))
df_mac_mean$chemo <- factor(df_mac_mean$chemo, levels = c("Upfront surgery", "Surgery after chemo"))
df_mac_mean
<- df_mac_mean %>%
stat_test group_by(chemo) %>% rstatix::wilcox_test(mean_dis ~ cell_type11, paired = T)
<- 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 = "chemo", dodge = 0.9, fun = "median_iqr")
rstatix$y.position <- 12
stat_test
<- ggplot(df_mac_mean,
pl[[r]] aes(x = chemo, y = log2(mean_dis))) +
::geom_split_violin(aes(x = chemo, y = log2(mean_dis), color = cell_type11),
introdataviztrim = FALSE, linewidth = .3) +
geom_boxplot(aes(fill = cell_type11), width = .15, show.legend = FALSE,
outlier.size = .2, linewidth = .2, color = "black") +
scale_fill_manual(values = mac_colors, name = glue::glue("{meta_names[r]}")) +
scale_colour_manual(values = mac_colors, name = glue::glue("{meta_names[r]}")) +
labs(y = "Log2(mean distance) of CD8T to Mp") +
theme_bmbdc() +
theme(title = element_text(size = 6),
axis.ticks = element_line(colour = "black"),
axis.title.y = element_text(size = 6),
axis.text.y = element_text(size = 6, colour = "black"),
axis.title.x = element_blank(),
axis.text.x = element_text(size = 6, colour = "black"),
axis.line.x = element_line(linewidth = 0.4),
axis.line.y = element_line(linewidth = 0.4),
legend.position="top",
legend.text = element_text(size = 4),
legend.title = element_text(size = 4),
legend.key.size = unit(0.1, 'cm')) +
stat_pvalue_manual(stat_test, x = "chemo", tip.length = 0.01, hide.ns = F, label = "p.adj.signif", size = 1.5)
ggsave(glue::glue("{res_dir}/sfig10d_CD8T_to_mac_dis_{r}cacb_persam_paired.pdf"), pl[[r]], width = 3, height = 3)
}"SM_"]] | pl[["OthersnoBT_"]] pl[[
sup_figure10e
<- coldat %>% dplyr::filter(sample_id %in% c(sample_chemo_type_list[["chemo"]],
cld_cacb "no_chemo_no_punc"]])) %>%
sample_chemo_type_list[[::mutate(chemo = case_when(sample_id %in% sample_chemo_type_list[["chemo"]] ~ "Surgery after chemo",
dplyrTRUE ~ "Upfront surgery")) %>%
::select(sample_id, cell_id, cell_type11, chemo, meta_merge)
dplyr
#MC-tumor-frontline
<- read_rds(glue::glue("{dat_dir}/distance_rds/TB_list_closecell_dist_k1_CD8T_to_Macrophage_DN.rds"))
list_closecell_distance_macDN <- read_rds(glue::glue("{dat_dir}/distance_rds/TB_list_closecell_dist_k1_CD8T_to_Macrophage_oth.rds"))
list_closecell_distance_macOth <- do.call("rbind", c(list_closecell_distance_macDN, list_closecell_distance_macOth)) %>% as_tibble()
df_closecell_distance <- df_closecell_distance %>%
TB_dist inner_join(cld_cacb, by = c("to_cell" = "cell_id"))
#MC-stroma-macro
<- read_rds(glue::glue("{dat_dir}/distance_rds/SM_list_closecell_dist_k1_CD8T_to_Macrophage_DN.rds"))
list_closecell_distance_macDN <- read_rds(glue::glue("{dat_dir}/distance_rds/SM_list_closecell_dist_k1_CD8T_to_Macrophage_oth.rds"))
list_closecell_distance_macOth <- do.call("rbind", c(list_closecell_distance_macDN, list_closecell_distance_macOth)) %>% as_tibble()
df_closecell_distance <- df_closecell_distance %>%
SM_dist inner_join(cld_cacb, by = c("to_cell" = "cell_id"))
#MC-others
<- read_rds(glue::glue("{dat_dir}/distance_rds/OthersnoBT_list_closecell_dist_k1_CD8T_to_Macrophage_DN.rds"))
list_closecell_distance_macDN <- read_rds(glue::glue("{dat_dir}/distance_rds/OthersnoBT_list_closecell_dist_k1_CD8T_to_Macrophage_oth.rds"))
list_closecell_distance_macOth <- do.call("rbind", c(list_closecell_distance_macDN, list_closecell_distance_macOth)) %>% as_tibble()
df_closecell_distance <- df_closecell_distance %>%
OthenoBT_dist inner_join(cld_cacb, by = c("to_cell" = "cell_id"))
<- rbind(TB_dist, SM_dist, OthenoBT_dist)
df_dist
#per sample
<- df_dist %>% group_by(sample_id, cell_type11, chemo, meta_merge) %>%
df_dist_mean ::summarise(dis_mean = mean(distance)) %>% ungroup()
dplyr
$meta_merge <- factor(df_dist_mean$meta_merge, levels = c("MC-tumor-frontline", "MC-stroma-macro", "MC-others"))
df_dist_mean$chemo <- factor(df_dist_mean$chemo, levels = c("Upfront surgery", "Surgery after chemo"))
df_dist_mean
<- c("Upfront surgery" = "#BC3C29FF", "Surgery after chemo" = "#0072B5FF")
cols
<- list()
pl for (i in c("DN-mp", "Other-mp")) {
<- df_dist_mean %>% dplyr::filter(cell_type11 %in% i)
dat <- dat %>%
stat_test group_by(meta_merge) %>% rstatix::wilcox_test(dis_mean ~ chemo, 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_merge", dodge = 0.9, fun = "median_iqr")
rstatix$y.position <- 12
stat_test
<- ggplot(dat, aes(x = meta_merge, y = log2(dis_mean))) +
pl[[i]] ::geom_split_violin(aes(x = meta_merge, y = log2(dis_mean), color = chemo),
introdataviztrim = FALSE, linewidth = .3) +
geom_boxplot(aes(fill = chemo), width = .2, show.legend = FALSE, linetype = 2,
outlier.size = .2, linewidth = .2, color = "black") +
scale_fill_manual(values = cols) +
scale_colour_manual(values = cols) +
labs(y = glue::glue("Log2(mean distance) of CD8T to {i}")) +
theme_bmbdc() +
theme(title = element_text(size = 6),
axis.ticks = element_line(colour = "black"),
axis.title.y = element_text(size = 6),
axis.text.y = element_text(size = 6, colour = "black"),
axis.title.x = element_blank(),
axis.text.x = element_text(size = 6, colour = "black", angle = 10),
axis.line.x = element_line(linewidth = 0.4),
axis.line.y = element_line(linewidth = 0.4),
legend.position="top",
legend.text = element_text(size = 4),
legend.title = element_blank(),
legend.key.size = unit(0.1, 'cm')) +
stat_pvalue_manual(stat_test, x = "meta_merge", tip.length = 0.01, hide.ns = F, label = "p.adj.signif", size = 1.5)
ggsave(glue::glue("{res_dir}/sup_figure10e_CD8T_to_{i}_dis_3metas_chemo_persam.pdf"), pl[[i]], width = 3, height = 3)
}
"DN-mp"]] | pl[["Other-mp"]] pl[[