
rm(list=ls())
library(tidyverse)
library(ggpubr)
library(egg)
library(survival)
library(survminer)

coulor2 = c("#00AFBB", "#FC4E07")
coulor3 = c("#00AFBB", "#E7B800", "#FC4E07")
coulor4 = c("#00AFBB", "#E7B800", "#7bc043", "#FC4E07")

## chemotherapy
## Hypoxia.riskscore
grs = read.csv("i210GRS.lasson.csv")
tme = read.csv("IMvigor210.TME.lasson.csv")
cli = read.csv("01 immunotherapy_cohort.csv")

dat = reduce(list(grs, tme, cli), inner_join, by="ID")


compaired <- list(c("CR/PR","SD/PD"))
p5 = ggplot(dat, aes(x=Response, y=GRSscore)) +
  #geom_violin(aes(fill=Response)) +
  geom_boxplot(aes(fill=Response)) +
  theme_article() +
  theme(legend.position = "none",
        axis.text = element_text(colour = "black")) +
  labs(x="", y="Hypoxia score") +
  stat_compare_means(comparisons = compaired, label = "p.signif") +
  #scale_fill_manual(values = coulor2) +  
  scale_x_discrete(labels=c("CR/PR"="Response", "SD/PD"="Nonresponse"))

p5

## TME.riskscore
dat$TMEscore = -1*(dat$TMEscore)
p6 = ggplot(dat, aes(x=Response, y=TMEscore)) +
  #geom_violin(aes(fill=Response)) +
  geom_boxplot(aes(fill=Response)) +
  theme_article() +
  theme(legend.position = "none",
        axis.text = element_text(colour = "black")) +
  labs(x="", y="TME score") +
  stat_compare_means(comparisons = compaired, label = "p.signif") +
  #scale_fill_manual(values = coulor2) + 
  scale_x_discrete(labels=c("CR/PR"="Response", "SD/PD"="Nonresponse"))

p6


## classifer
#dat = read.csv("01 immunotherapy_cohort.csv")
#cut_value <- surv_cutpoint(dat, time="OS_time", event="OS", variables="Hypoxia.riskscore")
#cut_value <- as.numeric(cut_value[["cutpoint"]]["cutpoint"])
#dat$Hypoxia <- ifelse(dat$Hypoxia.riskscore >cut_value,'HypoxiaHigh','HypoxiaLow')

#cut_value <- surv_cutpoint(dat, time="OS_time", event="OS", variables="TME.riskscore")
#cut_value <- as.numeric(cut_value[["cutpoint"]]["cutpoint"])
#dat$TME <- ifelse(dat$TME.riskscore >cut_value,'TMELow', 'TMEHigh')

surv = dat
surv$GRS <- ifelse(surv$GRSscore > median(surv$GRSscore),'GRSHigh', 'GRSLow')
surv$TME <- ifelse(surv$TMEscore > median(surv$TMEscore), 'TMEHigh', 'TMELow') 

surv$group = paste(surv$GRS, surv$TME, sep = "/")
table(surv$group)

surv$GRSTME = forcats::fct_collapse(surv$group, Mixed = c("GRSHigh/TMEHigh", "GRSLow/TMELow"))
surv$GRSTME = factor(surv$GRSTME, 
                     levels = c("GRSLow/TMEHigh", "Mixed", "GRSHigh/TMELow"),
                     labels = c("GRSLow/TMEHigh", "Mixed", "GRSHigh/TMELow"))

dat = surv
dat$class = dat$GRSTME

dat1 = dat[,c("class", "Response")]
dat1 = count(dat1, class, Response)
dat1 = dat1 %>% group_by(class) %>% 
  summarise(Response = Response,n = n/sum(n))
dat1$Response = factor(dat1$Response,levels = c("SD/PD","CR/PR"), labels = c("Nonresponse", "Response"))

library(ggplot2)
p7 <- ggplot(data = dat1)+
  geom_bar(aes(x = class,y = n,
               fill = Response),
           stat = "identity")+
  #scale_fill_manual(values = coulor2)
  geom_text(aes(x = class,y = n,
                label = scales::percent(n)),
            color = "white",
            size = 6,
            position = position_fill(vjust = 0.5))+
  theme_article()+
  theme(legend.position = "top") +
  labs(x=" ", y="Percent weight") 

p7


## survival plot
surv = dat

cut_value <- surv_cutpoint(surv, time="OS_time", event="OS", variables="GRSscore")
cut_value <- as.numeric(cut_value[["cutpoint"]]["cutpoint"])
surv$GRS <- ifelse(surv$GRSscore > cut_value,'GRSHigh', 'GRSLow')

surv$GRS <- ifelse(surv$GRSscore > median(surv$GRSscore),'GRSHigh', 'GRSLow')

surv$TME <- ifelse(surv$TMEscore > median(surv$TMEscore),'TMEHigh', 'TMELow')

cut_value <- surv_cutpoint(surv, time="OS_time", event="OS", variables="TMEscore")
cut_value <- as.numeric(cut_value[["cutpoint"]]["cutpoint"])
surv$TME <- ifelse(surv$TMEscore > cut_value,'TMELow', 'TMEHigh') 

surv$group = paste(surv$GRS, surv$TME, sep = "/")
table(surv$group)

surv$GRSTME = forcats::fct_collapse(surv$group, Mixed = c("GRSHigh/TMEHigh", "GRSLow/TMELow"))
surv$GRSTME = factor(surv$GRSTME, 
                     levels = c("GRSLow/TMEHigh", "Mixed", "GRSHigh/TMELow"),
                     labels = c("GRSLow/TMEHigh", "Mixed", "GRSHigh/TMELow"))

dat = surv

fit <- survfit(Surv(OS_time, OS) ~ GRSTME, data = dat)
p8 <- ggsurvplot(fit,
                 title = "",
                 pval = TRUE, 
                 #conf.int = TRUE,
                 risk.table = F, 
                 risk.table.col = "strata",
                 xlab = "Time (months)",
                 ylab = "Overall Survival",
                 #ylab = "Disease Free Survival (%)",
                 linetype = "strata",
                 #surv.median.line = "hv", 
                 ggtheme = theme_article(),
                 legend.labs = c("GRSLow/TMEHigh", "Mixed","GRSHigh/TMELow"),
                 #palette = coulor3,
                 legend = c(0.85,0.90),
                 legend.title = "",
                 #xlim = c(0,72),
                 #break.x.by = 24,
                 surv.scale = "percent")
p8



##
library(patchwork)
pa = p1|p2|p3|p4
pb = plot_spacer()|p7|(p8$plot)
pc = p9|p10
pd = pa/pb/pc + plot_annotation(tag_levels = 'A')
pd = p7|p8$plot


  plot_spacer()|p3|p4$plot + plot_annotation(tag_levels = 'A') 
pb


