<- 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/figure5" %>% 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 <- readr::read_rds(glue::glue("{dat_dir}/compcounts_censored_assay.rds")) assay_cc
figure5
figure5a
<- 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))
<- coldat %>% dplyr::filter(sample_id %in% sample_chemo_type_list[["no_chemo_all"]]) %>%
tumor_nochemo ::filter(cell_type_new %in% "Epithelial tumor cell") %>%
dplyr::mutate(pos = case_when(cell_id %in% pos_cells10[["PD_L1_pos_Epithelial_tumor"]] ~ "PD-L1+ tumor",
dplyrTRUE ~ "none"))
<- tumor_nochemo %>%
pdl1_tumor ::filter(pos %in% "PD-L1+ tumor") %>%
dplyr::select(sample_id, sample_tiff_id, meta_merge, pos, cell_id) %>%
dplyrdistinct() %>% group_by(sample_id, sample_tiff_id, meta_merge) %>%
::summarise(nc = n()) %>% group_by(sample_id, sample_tiff_id) %>%
dplyr::mutate(nt = sum(nc), prop = nc/nt) %>%
dplyr::filter(meta_merge %notin% "MC-tumor-core")
dplyr
$meta_merge <- factor(pdl1_tumor$meta_merge, levels = c("MC-tumor-frontline", "MC-stroma-macro", "MC-others"))
pdl1_tumor
<- pdl1_tumor %>% dplyr::mutate(n = n()) %>% dplyr::filter(n > 1) %>% ungroup()
pdl1_tumor_fil
<- pdl1_tumor_fil %>%
stat_test ::wilcox_test(prop ~ meta_merge, comparisons = list(c("MC-tumor-frontline", "MC-stroma-macro"),
rstatixc("MC-tumor-frontline", "MC-others")),
p.adjust.method = "none")
<- stat_test %>%
stat_test ::add_xy_position(x = "meta_merge", dodge = 0.9, fun = "median_iqr")
rstatix$y.position <- c(.99, 1)
stat_test
<- ggplot(pdl1_tumor_fil, aes(x = meta_merge, y = prop)) +
p ::geom_flat_violin(aes(fill = meta_merge, color = meta_merge), position = position_nudge(x = .25)) +
PupillometryRgeom_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) +
stat_pvalue_manual(stat_test, tip.length = 0, bracket.size = 0.1, hide.ns = F, label = "p.adj.signif", size = 1.5) +
ylab(glue::glue("Ratio of regional PD-L1+ tumor")) + theme_bmbdc() +
theme(axis.title.y = element_text(size = 6),
axis.text.y = element_text(size = 6),
axis.title.x = element_blank(),
axis.line.x = element_line(linewidth = 0.4),
axis.line.y = element_line(linewidth = 0.4),
axis.text.x = element_text(size = 6),
legend.position = "none")
ggsave(glue::glue("{res_dir}/fig5b_pdl1_tumor_ratio_sample_meta3_compare.pdf"), p, width = 3, height = 3)
p
figure5b
<- coldat %>% dplyr::filter(sample_id %in% sample_chemo_type_list[["no_chemo_all"]]) %>%
tumor_nochemo ::filter(cell_type_new %in% "Epithelial tumor cell")
dplyr
<- tumor_nochemo %>%
pdl1_tumor ::filter(meta_merge %notin% "MC-tumor-core") %>%
dplyr::select(sample_id, sample_tiff_id, meta_merge, cell_id) %>%
dplyrdistinct()
<- assay_cc["PD_L1", ] %>%
pdl1_tumor_intens as_tibble(rownames = "cell_id") %>% left_join(pdl1_tumor, ., by = "cell_id")
<- pdl1_tumor_intens %>% group_by(sample_id, meta_merge) %>%
pdl1_tumor_intens_mean ::summarise(mean_pdl1 = mean(value)) %>% ungroup()
dplyr
<- list("TS" = c("MC-tumor-frontline", "MC-stroma-macro"),
compr_ls "TO" = c("MC-tumor-frontline", "MC-others"))
<- compr_ls %>% map(function(x){pdl1_tumor_intens_mean %>% dplyr::filter(meta_merge %in% x) %>%
stat_test group_by(sample_id) %>% dplyr::mutate(n = n()) %>% dplyr::filter(n > 1) %>% ungroup() %>%
::wilcox_test(mean_pdl1 ~ meta_merge, paired = T)}) %>% list_rbind()
rstatix
<- 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"))
$y.position <- c(1.04, 1.02)
stat_test
$meta_merge <- factor(pdl1_tumor_intens_mean$meta_merge, levels = c("MC-tumor-frontline", "MC-stroma-macro", "MC-others"))
pdl1_tumor_intens_mean
<- ggplot(pdl1_tumor_intens_mean, aes(x = meta_merge, y = mean_pdl1)) +
p ::geom_flat_violin(aes(fill = meta_merge, color = meta_merge), position = position_nudge(x = .25)) +
PupillometryRgeom_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.2, 0.8), oob = scales::rescale_none) +
stat_pvalue_manual(stat_test, tip.length = 0, bracket.size = 0.1, label = "p.adj.signif", size = 1.5) +
ylab(glue::glue("mean PD-L1 intentisy of tumor")) + theme_bmbdc() +
theme(axis.title.y = element_text(size = 8),
axis.text.y = element_text(size = 6),
axis.title.x = element_blank(),
axis.line.x = element_line(linewidth = 0.4),
axis.line.y = element_line(linewidth = 0.4),
axis.text.x = element_text(size = 8, angle = 10),
legend.position = "none")
ggsave(glue::glue("{res_dir}/fig5f_tumor_meta3_paired_compare_PDL1_mean_intensity_paired.pdf"), p, width = 3, height = 3)
p
figure5d
<- list(c("MC-stroma-Mp", "MC-tumor-frontline"), c("MC-stroma-Mp", "MC-others"), c("MC-tumor-frontline", "MC-others"))
my_comparisons
# Tumor to DN_Macrophage ----------------------------
<- readr::read_rds(glue::glue("{dat_dir}/distance_rds/SM_list_closecell_dist_k1_Epithelial_tumor_to_Macrophage_HLADRn_CD163n.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_Epithelial_tumor_to_Macrophage_HLADRn_CD163n.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_Epithelial_tumor_to_Macrophage_HLADRn_CD163n.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_closecell_distance inner_join(coldat[,c("cell_id", "sample_id")], by = c("from_cell" = "cell_id")) %>%
::filter(sample_id %in% sample_chemo_type_list$no_chemo_all)
dplyr
$meta_merge <- factor(df_closecell_distance$meta_merge, levels = c("MC-tumor-frontline", "MC-stroma-macro", "MC-others") %>% rev())
df_closecell_distance
<- ggdensity(df_closecell_distance, 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}/fig5d_tumor_to_Macrophage_Dn_minDis_meta3_compare_density.pdf"), p, width = 3.5, height = 2.9)
p
figure5e
<- 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))
<- c("DN-mp" = "#BC3C29FF", "Other-mp" = "#0072B5FF")
mac_colors
<- readr::read_rds(glue::glue("{dat_dir}/distance_rds/TB_list_closecell_dist_k1_Epithelial_tumor_to_Macrophage_HLADRn_CD163n.rds")) %>%
df_dnmac do.call("rbind", .) %>% as_tibble()
<- readr::read_rds(glue::glue("{dat_dir}/distance_rds/TB_list_closecell_dist_k1_Epithelial_tumor_to_Macrophage_other.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
<- df_mac_mean %>% group_by(cell_type11, chemo) %>% dplyr::mutate(nc = n())
df_mac_mean
<- ggplot(df_mac_mean,
p aes(x = chemo, y = log2(mean_dis))) +
::geom_split_violin(aes(x = chemo, y = log2(mean_dis), colour = cell_type11),
introdataviztrim = FALSE, linewidth = .3) +
geom_boxplot(aes(fill = cell_type11), width = .1, show.legend = FALSE,
outlier.size = .2, linewidth = .2, color = "black") +
# stat_summary(fun.data = "mean_se", geom = "pointrange", show.legend = F,
# position = position_dodge(.12), size = .025, color = "black") +
scale_fill_manual(values = mac_colors, name = "MC-tumor-frontline") +
scale_colour_manual(values = mac_colors, name = "MC-tumor-frontline") +
::geom_dl(aes(label = nc),
directlabelsmethod = list("top.points", cex = .4)) +
labs(y = "Log2(mean distance) of tumor to Mp") +
stat_pvalue_manual(stat_test, x = "chemo", tip.length = 0.01, hide.ns = F, label = "p.adj.signif", size = 1.5) +
theme_bmbdc() +
theme(title = element_text(size = 8),
axis.ticks = element_line(colour = "black"),
axis.title.y = element_text(size = 8),
axis.text.y = element_text(size = 6, colour = "black"),
axis.title.x = element_blank(),
axis.text.x = element_text(size = 8, 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'))
ggsave(glue::glue("{res_dir}/fig5e_tb_tumor_to_mac_dis_3metas_cacb_persam_paired.pdf"), p, width = 3, height = 3)
p
figure5f
<- pos_cells10[["PD_L1_pos_Epithelial_tumor"]]
ids <- coldat %>% dplyr::filter(sample_id %in% sample_chemo_type_list[["no_chemo_all"]])
cld_nochemo
<- list()
pl for (mr in c("TB_","SM_", "OthersnoBT_")) {
<- list()
dis_ls for (j in c("Macrophage_HLADRn_CD163n", "Macrophage_other")) {
<- read_rds(glue::glue("{dat_dir}/distance_rds/{mr}list_closecell_dist_k1_{j}_to_Epithelial_tumor.rds"))
list_closecell_distance <- do.call("rbind", list_closecell_distance) %>% as_tibble()
df_closecell_distance
<- inner_join(df_closecell_distance, cld_nochemo, by = c("to_cell" = "cell_id")) %>%
df_tumor_distance ::select(from_cell, to_cell, distance, sample_id, sample_tiff_id) %>%
dplyr::mutate(group = case_when(distance < median(distance) ~ "near",
dplyrTRUE ~ "far"),
pos = case_when(to_cell %in% ids ~ "PD-L1+ tumor",
TRUE ~ "tumor"))
<- df_tumor_distance %>% group_by(to_cell) %>%
dis_group ::summarise(group_all = paste(group, collapse = ";")) %>%
dplyr::mutate(group_new = case_when(str_detect(group_all, "near") ~ "near",
dplyrTRUE ~ "far")) %>%
::select(-group_all) %>% left_join(distinct(df_tumor_distance[, c(2,4,5,7)]), by = "to_cell")
dplyr
<- dis_group %>%
dis_group_prop group_by(sample_id, sample_tiff_id, group_new, pos) %>%
::summarise(nc = n()) %>%
dplyrgroup_by(sample_id, sample_tiff_id, group_new) %>%
::mutate(nt = sum(nc),
dplyrprop = nc/nt)
$pos <- as.factor(dis_group_prop$pos)
dis_group_prop<- dis_group_prop %>%
dis_group_prop ::complete(pos, fill = list(prop = 0)) %>% ungroup()
tidyr$group_new <- factor(dis_group_prop$group_new, levels = c("near", "far"))
dis_group_prop
#paired
<- dis_group_prop[dis_group_prop$pos == "PD-L1+ tumor",] %>%
dis_ls[[j]] group_by(sample_id, sample_tiff_id) %>% dplyr::mutate(n = n(), mac = j) %>%
::filter(n > 1) %>% ungroup()
dplyr
}
<- list_rbind(dis_ls) %>%
dis_group_prop_paired ::mutate(mac = case_match(mac, "Macrophage_HLADRn_CD163n" ~ "DN-mp",
dplyr"Macrophage_other" ~ "Other-mp"))
$group_new <- factor(dis_group_prop_paired$group_new, levels = c("near", "far"))
dis_group_prop_paired$mac <- factor(dis_group_prop_paired$mac, levels = c("DN-mp", "Other-mp"))
dis_group_prop_paired
<- dis_group_prop_paired %>%
stat_test group_by(mac) %>% rstatix::wilcox_test(prop ~ group_new, p.adjust.method = "none", 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 = "mac", dodge = 0.9, fun = "median_iqr")
rstatix$y.position <- 0.4
stat_test
<- dis_group_prop_paired %>% group_by(group_new, mac) %>% dplyr::mutate(nc = n()) %>% ungroup()
dis_group_prop_paired
<- c("near" = "#BC3C29FF", "far" = "#0072B5FF")
col_d
<- ggplot(dis_group_prop_paired,
pl[[mr]] aes(x = mac, y = prop)) +
geom_boxplot(aes(fill = group_new), width = .5, show.legend = FALSE,
outlier.shape = NA, linewidth = .2, color = "black") +
stat_summary(aes(fill = group_new, color = group_new), fun.data = "mean_se", geom = "pointrange", show.legend = F,
position = position_dodge(.5), fatten = .5, size = .5, stroke = .5, linewidth = .5, color = "black") +
scale_colour_manual(values = col_d) +
scale_fill_manual(values = col_d) +
labs(y = "Proportion of PD-L1+ tumors") +
scale_y_continuous(limits=c(0,0.4), oob = scales::rescale_none) +
stat_pvalue_manual(stat_test, x = "mac", tip.length = 0.01, hide.ns = F, label = "p.adj.signif", size = 1.5) +
::geom_dl(aes(label = nc),
directlabelsmethod = list("smart.grid", cex = .4)) +
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'))
ggsave(glue::glue("{res_dir}/fig5f_{mr}mac_nearest_PDL1_tumor_prop_compare_newgroup_paired.pdf"), pl[[mr]], width = 3, height = 3)
}
<- pl[["TB_"]] | pl[["SM_"]] | pl[["OthersnoBT_"]]
p ggsave(glue::glue("{res_dir}/fig5f_mac_nearest_PDL1_tumor_prop_compare_newgroup_paired.pdf"), p, width = 12, height = 4)
p