You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
5.9 KiB

  1. library(tidyverse)
  2. library(openxlsx)
  3. library(ggpubr)
  4. path="C:/Users/47926492N/OneDrive - IDIBELL - Institut d'Investigació Biomèdica de Bellvitge/ImmunoPreserve/TestIP/"
  5. data<-read.xlsx(paste0(path, "Panel1.xlsx"), sheet = "IC")
  6. data1<-data %>% gather(phen, value, -sample, -Population)
  7. data1["TimePoint"]<-sapply(data1$sample, function(x) strsplit(x, "_")[[1]][2])
  8. data1["sample"]<-sapply(data1$sample, function(x) strsplit(x, "_")[[1]][1])
  9. data1<-data1 %>% group_by(Population, TimePoint, phen) %>% summarise(value=mean(value))
  10. data1$phen<-gsub("p","+",data1$phen)
  11. data1$phen<-gsub("n","-",data1$phen)
  12. data1$phen<-gsub("_"," ",data1$phen)
  13. data1$phen<-gsub("n","-",data1$phen, fixed = T)
  14. data1$phen<-gsub("p","+",data1$phen, fixed = T)
  15. data1$phen<-gsub("_"," ",data1$phen)
  16. data1[data1$value < 1, "phen"]<-"Other"
  17. data1$phen<-gsub("[A-Z]*-*[0-9T]- *", "", data1$phen)
  18. data1$phen<-gsub("+ $", "", data1$phen)
  19. data1$phen[data1$phen == ""]<-"All Negative"
  20. data1["phen1"]<-"PD1"
  21. data1[!grepl("PD1+", data1$phen),"phen1"]<-NA
  22. data1["phen2"]<-"TIM3"
  23. data1[!grepl("TIM3+", data1$phen),"phen2"]<-NA
  24. data1["phen3"]<-"CTLA4"
  25. data1[!grepl("CTLA4+", data1$phen),"phen3"]<-NA
  26. data1["phen4"]<-"LAG3"
  27. data1[!grepl("LAG3+", data1$phen),"phen4"]<-NA
  28. data1<-data1 %>% arrange(desc(value))
  29. data2<-data1 %>% filter(!phen %in% c("All Negative","Other"))
  30. data1<-rbind(data2, data1 %>% filter(phen %in% c("All Negative","Other")) %>% arrange(desc(phen)))
  31. data1.list<-list()
  32. cont<-1
  33. for (i in data1$Population %>% unique){
  34. for (j in data1$TimePoint %>% unique){
  35. data_temp<-data1 %>% filter(Population == i & TimePoint == j)
  36. data_temp$ymax<-cumsum(data_temp$value)
  37. data_temp$ymin<-c(0, head(data_temp$ymax, n=-1))
  38. data1.list[[cont]]<-data_temp
  39. cont<-cont+1
  40. }
  41. }
  42. data1<-do.call(rbind, data1.list)
  43. data1<-data1 %>% filter(TimePoint %in% c("W1","W2","W8","W12"))
  44. data1$TimePoint<-factor(data1$TimePoint, levels=c("W1","W2","W8","W12"))
  45. data1$Population<-factor(data1$Population, levels=c("CD8","CD4"))
  46. color<-c(c("CTLA4+ LAG3+ PD1+ TIM3+"="black","All Negative"="grey90","Other"="grey50", "PD1+"="#C07AFF", "CTLA4+"="#3EB3DE","TIM3+"="#5EF551","LAG3+"="#DEBB3E"),
  47. c("CTLA4+ PD1+"="#6666FF","PD1+ TIM3+"="#849CA8", "LAG3+ PD1+"="#C47F9F", "CTLA4+ TIM3+"="#4ED498", "CTLA4+ LAG3+"="#8EB78E", "LAG3+ TIM3+"="#9ED848"),
  48. c("CTLA4+ PD1+ TIM3+"="#B81515", "LAG3+ PD1+ TIM3+"="#0f5860"))
  49. basic.color<-color[c("PD1+","TIM3+","CTLA4+","LAG3+")]
  50. names(basic.color)<-c("PD1","TIM3","CTLA4","LAG3")
  51. # Make the plot
  52. g_coex<-ggplot(data1)+
  53. facet_grid(Population~TimePoint)+
  54. geom_rect(aes(ymax=ymax, ymin=ymin, xmax=4.5, xmin=0), fill=color[data1$phen])+
  55. geom_rect(aes(ymax=ymax, ymin=ymin, xmax=5.4, xmin=5, fill=factor(phen1, levels=c("PD1","TIM3","CTLA4","LAG3"))))+
  56. geom_rect(aes(ymax=ymax, ymin=ymin, xmax=5.9, xmin=5.5, fill=factor(phen2, levels=c("PD1","TIM3","CTLA4","LAG3"))))+
  57. geom_rect(aes(ymax=ymax, ymin=ymin, xmax=6.4, xmin=6, fill=factor(phen3, levels=c("PD1","TIM3","CTLA4","LAG3"))))+
  58. geom_rect(aes(ymax=ymax, ymin=ymin, xmax=6.9, xmin=6.5, fill=factor(phen4, levels=c("PD1","TIM3","CTLA4","LAG3"))))+
  59. scale_fill_manual(values = basic.color, na.value="#FFFFFF00", drop=F, limits=c("PD1","TIM3","CTLA4","LAG3"), name="IC")+
  60. coord_polar(theta="y") + # Try to remove that to understand how the chart is built initially
  61. xlim(c(0, 7))+ # Try to remove that to see how to make a pie chart
  62. theme_classic()+
  63. guides(fill="none")+
  64. theme(strip.background = element_blank(),
  65. strip.text = element_text(size=12, face="bold"),
  66. axis.line = element_blank(),
  67. axis.ticks = element_blank(),
  68. # plot.margin = margin(-200,0,0,0),
  69. axis.text = element_blank())
  70. mtl_rec<-as.data.frame(matrix(nrow=0, ncol=3))
  71. colnames(mtl_rec)<-c(colnames(data1)[1:2],"phen")
  72. for (rec in c("PD1+", "TIM3+", "LAG3+","TIGIT+", "CTLA4+")){
  73. temp<-data1[grep(rec, data1$phen),]
  74. if (nrow(temp) > 0){
  75. mtl_rec<-rbind(mtl_rec, data.frame(temp, "Rec"=rec))
  76. }
  77. }
  78. mtl_rec_sum<-mtl_rec %>% group_by(TimePoint,Population,Rec, phen) %>% summarise(value=sum(value))
  79. ggplot(mtl_rec_sum, aes(Rec, value, fill=phen))+
  80. facet_grid(Population~TimePoint)+
  81. geom_bar(stat="identity", color="black")+
  82. scale_fill_manual(values = color, name="IC")
  83. data2<-data %>% gather(phen, value, -sample, -Population)
  84. data2["TimePoint"]<-sapply(data2$sample, function(x) strsplit(x, "_")[[1]][2])
  85. data2["sample"]<-sapply(data2$sample, function(x) strsplit(x, "_")[[1]][1])
  86. data2$phen<-gsub("n","-",data2$phen, fixed = T)
  87. data2$phen<-gsub("p","+",data2$phen, fixed = T)
  88. data2$phen<-gsub("_"," ",data2$phen)
  89. # data2[data2$value < 1, "phen"]<-"Other"
  90. data2$phen<-gsub("[A-Z]*-*[0-9T]- *", "", data2$phen)
  91. data2$phen<-gsub("+ $", "", data2$phen)
  92. data2$phen[data2$phen == ""]<-"All Negative"
  93. data2<-data2 %>% filter(TimePoint %in% c("W1","W2","W8","W12"))
  94. mtl_rec2<-as.data.frame(matrix(nrow=0, ncol=3))
  95. colnames(mtl_rec2)<-c(colnames(data2)[1:2],"phen")
  96. for (rec in c("PD1", "TIM3", "LAG3","CTLA4")){
  97. temp<-data2[grep(rec, data2$phen),]
  98. if (nrow(temp) > 0){
  99. mtl_rec2<-rbind(mtl_rec2, data.frame(temp, "Rec"=rec))
  100. }
  101. }
  102. mtl_rec2<-mtl_rec2 %>% group_by(sample,Population,TimePoint,Rec) %>% summarise(value=sum(value, na.rm=T))
  103. mtl_rec2$Rec<-factor(mtl_rec2$Rec, levels=c("PD1", "TIM3", "CTLA4","LAG3"))
  104. mtl_rec2$TimePoint<-factor(mtl_rec2$TimePoint, levels=c("W1","W2","W8","W12"))
  105. mtl_rec2$Population<-factor(mtl_rec2$Population, levels=c("CD8","CD4"))
  106. g_Rec<-ggplot(mtl_rec2, aes(TimePoint, value))+
  107. facet_grid(Population~Rec)+
  108. geom_point(color="grey80")+
  109. geom_line(aes(group=sample), color="grey80")+
  110. geom_point(stat="summary", aes(color=Rec), size=2)+
  111. geom_line(aes(group=Rec, color=Rec), stat="summary", size=1)+
  112. scale_color_manual(values = basic.color, name="IC")+
  113. labs(y="% CD8/CD4 T cells")+
  114. guides(color="none")+
  115. theme_bw()
  116. ggarrange(g_Rec, g_coex, ncol = 1)
  117. ggsave(paste0(path,"Analysis/IC_coex.png"), width = 7.5, height = 7.5)