From e95caafe28142b25d874af4d9cb1a17fdc738480 Mon Sep 17 00:00:00 2001 From: marcelcosta Date: Tue, 5 Apr 2022 16:57:26 +0200 Subject: [PATCH] Cambio a pie chart para Visor IC. --- BDAccess/app.R | 156 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 125 insertions(+), 31 deletions(-) diff --git a/BDAccess/app.R b/BDAccess/app.R index b15a8fd..effed10 100644 --- a/BDAccess/app.R +++ b/BDAccess/app.R @@ -829,45 +829,139 @@ server <- function(input, output) { output$visorplot<-renderPlot({ if (input$nhc == 3){ - pops<-sqlFetch(dta, "POPULATIONS") - g_pop<-pops %>% dplyr::filter(samples == input$id) %>% gather(pop,value,-samples) %>% - mutate(pop=factor(pop, levels=c("CD45pos_Alive","T_cells","CD8","CD4","DN","NK", "B_cells", - "CD45neg_LDneg","EpCAMneg_HLAIneg","EpCAMneg_HLAIpos","EpCAMpos_HLAIpos"))) %>% - ggplot(aes(pop, value))+ - geom_bar(stat="identity", color="black", fill="grey70")+ - labs(title = input$id, y="% parent", x="")+ - theme_bw()+ - theme(axis.text.x = element_text(angle=90, hjust=1, vjust=0.5)) - tl<-sqlFetch(dta, "IC") %>% filter(samples == input$id) + data<-sqlFetch(dta, "IC") %>% filter(samples == input$id) + data1<-data %>% gather(phen, value, -samples, -Population) + + data1$phen<-gsub("p","+",data1$phen) + data1$phen<-gsub("n","-",data1$phen) + data1$phen<-gsub("_"," ",data1$phen) + + data1$phen<-gsub("n","-",data1$phen, fixed = T) + data1$phen<-gsub("p","+",data1$phen, fixed = T) + data1$phen<-gsub("_"," ",data1$phen) + data1[data1$value < 0.5, "phen"]<-"Other" + data1$phen<-gsub("[A-Z]*-*[0-9T]- *", "", data1$phen) + data1$phen<-gsub("+ $", "", data1$phen) + data1$phen[data1$phen == ""]<-"All Negative" + + # data1<-data1 %>% filter(value > 0.5) + + data1["phen1"]<-"PD1" + data1[!grepl("PD1+", data1$phen),"phen1"]<-NA + + data1["phen2"]<-"TIM3" + data1[!grepl("TIM3+", data1$phen),"phen2"]<-NA + + data1["phen3"]<-"CTLA4" + data1[!grepl("CTLA4+", data1$phen),"phen3"]<-NA - mtl<-melt(tl, variable.name = "Receptors") - mtl$Receptors<-as.character(mtl$Receptors) #Para poder depurar bien el texto, lo pasamos a tipo character - mtl$Receptors<-gsub("n","-",mtl$Receptors, fixed = T) - mtl$Receptors<-gsub("p","+",mtl$Receptors, fixed = T) - mtl$Receptors<-gsub("_"," ",mtl$Receptors) - mtl[mtl$value < 1, "Receptors"]<-"Other" - mtl$Receptors<-gsub("[A-Z]*-*[0-9T]- *", "", mtl$Receptors) - mtl$Receptors<-gsub("+ $", "", mtl$Receptors) - mtl$Receptors[mtl$Receptors == ""]<-"All Negative" + data1["phen4"]<-"TIGIT" + data1[!grepl("TIGIT+", data1$phen),"phen4"]<-NA - mtl$Receptors<-factor(mtl$Receptors) - mtl$Population<-factor(mtl$Population, levels = c("CD8", "CD4")) + data1["phen5"]<-"LAG3" + data1[!grepl("LAG3+", data1$phen),"phen5"]<-NA - # colorCount<-length(unique(mtl$Receptors)) - # getPalette = colorRampPalette(RColorBrewer::brewer.pal(12, "Set3")) + data1<-data1 %>% arrange(desc(value)) + data2<-data1 %>% filter(!phen %in% c("All Negative","Other")) + data1<-rbind(data2, data1 %>% filter(phen %in% c("All Negative","Other")) %>% arrange(desc(phen))) - color<-c(c("CTLA4+ LAG3+ PD1+ TIGIT+ TIM3+"="black","All Negative"="white","Other"="grey50", "PD1+"="#C07AFF", "CTLA4+"="#3EB3DE","TIM3+"="#5EF551","LAG3+"="#DEBB3E","TIGIT+"="#FA7055"), + data_cd8<-data1 %>% filter(Population == "CD8") + data_cd4<-data1 %>% filter(Population == "CD4") + + data_cd8$ymax<-cumsum(data_cd8$value) + data_cd8$ymin<-c(0, head(data_cd8$ymax, n=-1)) + + data_cd4$ymax<-cumsum(data_cd4$value) + data_cd4$ymin<-c(0, head(data_cd4$ymax, n=-1)) + + data1<-rbind(data_cd8, data_cd4) + + + color<-c(c("CTLA4+ LAG3+ PD1+ TIGIT+ TIM3+"="black","All Negative"="grey90","Other"="grey50", "PD1+"="#C07AFF", "CTLA4+"="#3EB3DE","TIM3+"="#5EF551","LAG3+"="#DEBB3E","TIGIT+"="#FA7055"), c("CTLA4+ PD1+"="#6666FF","PD1+ TIM3+"="#849CA8", "LAG3+ PD1+"="#C47F9F","PD1+ TIGIT+"="#D259AA", "CTLA4+ TIM3+"="#4ED498", "CTLA4+ LAG3+"="#8EB78E", "CTLA4+ TIGIT+"="#9C929A", "LAG3+ TIM3+"="#9ED848", "TIGIT+ TIM3+"="#ACB353", "LAG3+ TIGIT+"="#EC964A"), c("CTLA4+ PD1+ TIGIT+"="#B86B6A","CTLA4+ PD1+ TIGIT+ TIM3+"="#B81515","LAG3+ PD1+ TIGIT+"="#007D8A", "PD1+ TIGIT+ TIM3+"="#D64545", "LAG3+ PD1+ TIGIT+ TIM3+"="#0f5860", "LAG3+ TIGIT+ TIM3+"="#50cad3")) + basic.color<-color[c("PD1+","TIGIT+","TIM3+","CTLA4+","LAG3+")] + names(basic.color)<-c("PD1","TIGIT","TIM3","CTLA4","LAG3") + # Make the plot + g1<-ggplot(data1)+ + facet_wrap(.~Population)+ + geom_rect(aes(ymax=ymax, ymin=ymin, xmax=4.5, xmin=0), fill=color[data1$phen])+ + geom_rect(aes(ymax=ymax, ymin=ymin, xmax=5.4, xmin=5, fill=factor(phen1, levels=c("PD1","TIGIT","TIM3","CTLA4","LAG3"))))+ + geom_rect(aes(ymax=ymax, ymin=ymin, xmax=5.9, xmin=5.5, fill=factor(phen4, levels=c("PD1","TIGIT","TIM3","CTLA4","LAG3"))))+ + geom_rect(aes(ymax=ymax, ymin=ymin, xmax=6.4, xmin=6, fill=factor(phen2, levels=c("PD1","TIGIT","TIM3","CTLA4","LAG3"))))+ + geom_rect(aes(ymax=ymax, ymin=ymin, xmax=6.9, xmin=6.5, fill=factor(phen3, levels=c("PD1","TIGIT","TIM3","CTLA4","LAG3"))))+ + geom_rect(aes(ymax=ymax, ymin=ymin, xmax=7.4, xmin=7, fill=factor(phen5, levels=c("PD1","TIGIT","TIM3","CTLA4","LAG3"))))+ + scale_fill_manual(values = basic.color, na.value="#FFFFFF00", drop=F, limits=c("PD1","TIGIT","TIM3","CTLA4","LAG3"), name="IC")+ + coord_polar(theta="y") + # Try to remove that to understand how the chart is built initially + xlim(c(0, 8)) +# Try to remove that to see how to make a pie chart + theme_classic()+ + theme(strip.background = element_blank(), + strip.text = element_text(size=12, face="bold"), + axis.line = element_blank(), + axis.ticks = element_blank(), + plot.margin = margin(-200,0,0,0), + axis.text = element_blank()) + + # df.color<-data.frame("phen"=names(color), "color"=color) + # df.color<-rbind( + # df.color %>% filter(!phen %in% c("All Negative","Other")) %>% arrange(phen), + # df.color %>% filter(phen %in% c("All Negative","Other")) %>% arrange(desc(phen)) + # ) + # + # g2<-ggplot(df.color, aes(phen, 1))+ + # geom_tile(fill=df.color$color)+ + # scale_x_discrete(limits=df.color$phen)+ + # ggtitle("Phenotype Combination")+ + # theme(axis.text.x = element_text(angle=90, hjust=1, vjust=0.5), + # axis.text.y = element_blank(), + # axis.ticks = element_blank(), + # axis.title = element_blank(), + # panel.background = element_blank())+ + # coord_equal() + + # g_IC<-ggpubr::ggarrange(g1,g2,ncol=1) + g_IC<-g1 - g_IC<-ggplot(mtl, aes(samples, value, fill=Receptors))+ - geom_bar(stat="summary", fun="sum",color="black")+ - labs(x="Patient", y="% CD8+", fill="")+ - facet_grid(.~Population)+ - scale_fill_manual(values = color[levels(mtl$Receptors)[levels(mtl$Receptors) %in% unique(mtl$Receptors)]])+ - theme_bw()+ - theme(axis.text.x=element_text(angle=45, hjust=1)) + pops<-sqlFetch(dta, "POPULATIONS") + + g_pop<-pops %>% dplyr::filter(samples == input$id) %>% gather(pop,value,-samples) %>% + mutate(pop=factor(pop, levels=c("CD45pos_Alive","T_cells","CD8","CD4","DN","NK", "B_cells", + "CD45neg_LDneg","EpCAMneg_HLAIneg","EpCAMneg_HLAIpos","EpCAMpos_HLAIpos"))) %>% + ggplot(aes(pop, value))+ + geom_bar(stat="identity", color="black", fill="grey70")+ + labs(title = input$id, y="% parent", x="")+ + theme_bw()+ + theme(axis.text.x = element_text(angle=90, hjust=1, vjust=0.5)) + # tl<-sqlFetch(dta, "IC") %>% filter(samples == input$id) + # + # mtl<-melt(tl, variable.name = "Receptors") + # mtl$Receptors<-as.character(mtl$Receptors) #Para poder depurar bien el texto, lo pasamos a tipo character + # mtl$Receptors<-gsub("n","-",mtl$Receptors, fixed = T) + # mtl$Receptors<-gsub("p","+",mtl$Receptors, fixed = T) + # mtl$Receptors<-gsub("_"," ",mtl$Receptors) + # mtl[mtl$value < 1, "Receptors"]<-"Other" + # mtl$Receptors<-gsub("[A-Z]*-*[0-9T]- *", "", mtl$Receptors) + # mtl$Receptors<-gsub("+ $", "", mtl$Receptors) + # mtl$Receptors[mtl$Receptors == ""]<-"All Negative" + # + # mtl$Receptors<-factor(mtl$Receptors) + # mtl$Population<-factor(mtl$Population, levels = c("CD8", "CD4")) + # + # # colorCount<-length(unique(mtl$Receptors)) + # # getPalette = colorRampPalette(RColorBrewer::brewer.pal(12, "Set3")) + # + # color<-c(c("CTLA4+ LAG3+ PD1+ TIGIT+ TIM3+"="black","All Negative"="white","Other"="grey50", "PD1+"="#C07AFF", "CTLA4+"="#3EB3DE","TIM3+"="#5EF551","LAG3+"="#DEBB3E","TIGIT+"="#FA7055"), + # c("CTLA4+ PD1+"="#6666FF","PD1+ TIM3+"="#849CA8", "LAG3+ PD1+"="#C47F9F","PD1+ TIGIT+"="#D259AA", "CTLA4+ TIM3+"="#4ED498", "CTLA4+ LAG3+"="#8EB78E", "CTLA4+ TIGIT+"="#9C929A", "LAG3+ TIM3+"="#9ED848", "TIGIT+ TIM3+"="#ACB353", "LAG3+ TIGIT+"="#EC964A"), + # c("CTLA4+ PD1+ TIGIT+"="#B86B6A","CTLA4+ PD1+ TIGIT+ TIM3+"="#B81515","LAG3+ PD1+ TIGIT+"="#007D8A", "PD1+ TIGIT+ TIM3+"="#D64545", "LAG3+ PD1+ TIGIT+ TIM3+"="#0f5860", "LAG3+ TIGIT+ TIM3+"="#50cad3")) + # + # g_IC<-ggplot(mtl, aes(samples, value, fill=Receptors))+ + # geom_bar(stat="summary", fun="sum",color="black")+ + # labs(x="Patient", y="% CD8+", fill="")+ + # facet_grid(.~Population)+ + # scale_fill_manual(values = color[levels(mtl$Receptors)[levels(mtl$Receptors) %in% unique(mtl$Receptors)]])+ + # theme_bw()+ + # theme(axis.text.x=element_text(angle=45, hjust=1)) ggpubr::ggarrange(g_pop, g_IC, heights = c(0.4, 0.6), ncol = 1) } })