From 3dfae26144630debd86a52acf7f53e41a582c5c5 Mon Sep 17 00:00:00 2001 From: marcelcosta Date: Fri, 22 Apr 2022 16:27:04 +0200 Subject: [PATCH] =?UTF-8?q?A=C3=B1adido=20el=20c=C3=B3digo=20para=20el=20p?= =?UTF-8?q?anel3.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CitoProcess/app.R | 282 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 246 insertions(+), 36 deletions(-) diff --git a/CitoProcess/app.R b/CitoProcess/app.R index fc04e54..09ef4a9 100644 --- a/CitoProcess/app.R +++ b/CitoProcess/app.R @@ -4,6 +4,7 @@ library(flowCore) library(flowWorkspace) library(CytoML) library(ggcyto) +library(RColorBrewer) # library(reshape2) # library(CitFuns) library(openxlsx) @@ -82,21 +83,6 @@ server <- function(input, output) { }) observeEvent(input$pngexport,{ - # if (input$phenotype == "Pop"){ - # route<-cito_dir - # - # ws<-open_flowjo_xml(paste0(route,"Populations.wsp")) - # gs<-flowjo_to_gatingset(ws, name="All Samples") - # - # sampleNames(gs)<-sapply(sampleNames(gs), function(x) strsplit(x, "Pop ")[[1]][2]) %>% - # gsub("[[:space:]][0-9]*.fcs_.[0-9]*","", . , perl = T) - # - # for (samp in sampleNames(gs)){ - # print(samp) - # p<-autoplot(gs[[samp]], bins=64) - # ggsave(paste0(route, samp,".pop.png"),p,width = 10, height = 10) - # } - # } if (input$phenotype == "Panel1"){ route<-cito_dir @@ -278,39 +264,186 @@ server <- function(input, output) { } print("Informes finalizados!") } - }) - - observeEvent(input$popexport,{ - if (input$phenotype == "Pop"){ + + if (input$phenotype == "Panel3"){ route<-cito_dir - ws<-open_flowjo_xml(paste0(route,"Populations.wsp")) + ws<-open_flowjo_xml(paste0(route,"Panel3.wsp")) gs<-flowjo_to_gatingset(ws, name="All Samples") - sampleNames(gs)<-sapply(sampleNames(gs), function(x) strsplit(x, "Pop ")[[1]][2]) %>% + sampleNames(gs)<-sapply(sampleNames(gs), function(x) strsplit(x, "Panel3 ")[[1]][2]) %>% gsub("[[:space:]][0-9]*.fcs_.[0-9]*","", . , perl = T) - nodes<-sapply(strsplit(gs_get_pop_paths(gs), "/"), tail, 1) - nodes<-nodes[grepl("_",nodes)] - pop<-gs_pop_get_stats(gs, nodes=nodes,type="percent") %>% as.data.frame %>% mutate(percent=percent*100) + gs<-gs[sampleNames(gs)[!grepl("Iso|ISO|iso|NoBV421",sampleNames(gs))]] - pop[,"pop"]<-gsub("_","",pop$pop) - pop$pop<-gsub(" ","_",pop$pop) - pop$pop<-gsub("+","pos",pop$pop, fixed=T) - pop$pop<-gsub("-","neg",pop$pop, fixed=T) - pop<-rename(pop, "samples"="sample") + bool.comb<-apply( + expand.grid(c("","!"), c("","!"), c("","!")), + 1, + function(x) paste0(x[1],"CD38 & ",x[2],"HLADR & ",x[3],"GZMB") + ) + + bool.name<-apply( + expand.grid(c("+","-"), c("+","-"), c("+","-")), + 1, + function(x) paste0("CD38",x[1]," HLADR",x[2]," GZMB",x[3]) + ) + + print("Booleanos CD8") + for (i in 1:length(bool.comb)){ + call<-substitute(booleanFilter(v), list(v=as.symbol(bool.comb[i]))) + boolgate<-eval(call) + gs_pop_add(gs, boolgate, parent="CD8", name = bool.name[i]) + } + + print("Booleanos CD4") + for (i in 1:length(bool.comb)){ + call<-substitute(booleanFilter(v), list(v=as.symbol(bool.comb[i]))) + boolgate<-eval(call) + gs_pop_add(gs, boolgate, parent="CD4", name = bool.name[i]) + } + + print("Booleanos NK") + for (i in 1:length(bool.comb)){ + call<-substitute(booleanFilter(v), list(v=as.symbol(bool.comb[i]))) + boolgate<-eval(call) + gs_pop_add(gs, boolgate, parent="NK", name = bool.name[i]) + } + + recompute(gs) + + names<-sampleNames(gs) # %>% gsub("ab|Ab|AB|iso|Iso|ISO| ","",.) %>% unique() + + nodes<-gs_get_pop_paths(gs) + # nodes<-gsub("â\u0081»", "-", nodes) + # nodes<-gsub("â\u0081º", "+", nodes) + nodes<-nodes[grepl("CD38", nodes)] + nodes<-nodes[!grepl("CD38$|HLADR$|GZMB$", nodes)] + + pop<-gs_pop_get_stats(gs, nodes=nodes,type="percent") %>% as.data.frame %>% mutate(percent=percent*100) pop$percent<-round(pop$percent, digits=2) - pop_sp<-pop %>% spread(pop, percent) - pop_sql<-sqlFetch(dta, "POPULATIONS") %>% slice(0) - pop_sp<-pop_sp %>% merge(pop_sql, all=T) %>% select(colnames(pop_sql)) + # pop$pop<-gsub("â\u0081»", "n", pop$pop) + # pop$pop<-gsub("â\u0081º", "p", pop$pop) + # pop$pop<-gsub("-", "n", pop$pop, fixed=T) + # pop$pop<-gsub("+", "p", pop$pop, fixed=T) + # pop$pop<-gsub(" ", "_", pop$pop) + + ## Esto si no hay isotipo + + pop_sp<-pop + pop_sp["Population"]<-str_extract(pop_sp$pop, "/CD[4,8]{1}/") %>% gsub("/|_","",.) + pop_sp$Population[is.na(pop_sp$Population)]<-"NK" + pop_sp$pop<-sapply(strsplit(pop_sp$pop, "/"), tail, 1) + pop_sp<-pop_sp %>% spread(pop, percent) - vartypes<-rep("Number", pop_sp %>% select(-samples) %>% colnames %>% length) - names(vartypes)<-pop_sp %>% select(-samples) %>% colnames + pop_sql<-read.xlsx(paste0(db_path,"Panel3.xlsx"), check.names = F, sep.names = " ") + pop_sp<-pop_sp %>% merge(pop_sql %>% slice(0), all=T) %>% select(colnames(pop_sql)) - sqlSave(dta, pop_sp, tablename="POPULATIONS", append = T, varTypes = vartypes, rownames = F) - print("Tabla POPULATIONS sincronizada.") + for (id in names){ + print(id) + + data<-pop_sp %>% filter(sample == id) + data1<-data %>% gather(phen, value, -sample, -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 < 1, "phen"]<-"Other" + data1$phen<-gsub("[A-Z0-9]*-", "", data1$phen) + data1$phen<-gsub(" ", " ", data1$phen) + data1$phen<-gsub("[ ]*$", "", data1$phen) + data1$phen<-gsub("^[ ]*", "", data1$phen) + data1$phen[data1$phen == ""]<-"All Negative" + + data1["phen1"]<-"CD38" + data1[!grepl("CD38+", data1$phen),"phen1"]<-NA + + data1["phen2"]<-"HLADR" + data1[!grepl("HLADR+", data1$phen),"phen2"]<-NA + + data1["phen3"]<-"GZMB" + data1[!grepl("GZMB+", data1$phen),"phen3"]<-NA + + 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))) + + data_cd8<-data1 %>% filter(Population == "CD8") + data_cd4<-data1 %>% filter(Population == "CD4") + data_NK<-data1 %>% filter(Population == "NK") + + 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)) + + data_NK$ymax<-cumsum(data_NK$value) + data_NK$ymin<-c(0, head(data_NK$ymax, n=-1)) + + data1<-rbind(data_cd8, data_cd4, data_NK) + + + # color<-c(c("CTLA4+ LAG3+ PD1+ TIM3+"="black","All Negative"="grey90","Other"="grey50", "PD1+"="#C07AFF", "CTLA4+"="#3EB3DE","TIM3+"="#5EF551","LAG3+"="#DEBB3E"), + # c("CTLA4+ PD1+"="#6666FF","PD1+ TIM3+"="#849CA8", "LAG3+ PD1+"="#C47F9F", "CTLA4+ TIM3+"="#4ED498", "CTLA4+ LAG3+"="#8EB78E", "LAG3+ TIM3+"="#9ED848"), + # c("CTLA4+ PD1+ TIM3+"="#B81515", "LAG3+ PD1+ TIM3+"="#0f5860")) + + # basic.color<-color[c("PD1+","TIM3+","CTLA4+","LAG3+")] + # names(basic.color)<-c("PD1","TIM3","CTLA4","LAG3") + # Make the plot + + color<-colorRampPalette(brewer.pal(8, "Set3"))(length(bool.name)) + names(color)<-bool.name %>% gsub("[A-Z0-9]*-", "", .) %>% gsub(" "," ",.) %>% gsub("[ ]*$", "", .) %>% gsub("^[ ]*", "", .) %>% gsub("^$","All Negative", .) + color["All Negative"]<-"grey90" + basic.color<-color[c("CD38+","HLADR+","GZMB+")] + names(basic.color)<-c("CD38","HLADR","GZMB") + g_coex<-ggplot(data1)+ + facet_grid(.~factor(Population, levels=c("CD8","CD4","NK")))+ + 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("CD38","HLADR","GZMB"))))+ + geom_rect(aes(ymax=ymax, ymin=ymin, xmax=5.9, xmin=5.5, fill=factor(phen2, levels=c("CD38","HLADR","GZMB"))))+ + geom_rect(aes(ymax=ymax, ymin=ymin, xmax=6.4, xmin=6, fill=factor(phen3, levels=c("CD38","HLADR","GZMB"))))+ + scale_fill_manual(values = basic.color, na.value="#FFFFFF00", drop=F, limits=c("CD38","HLADR","GZMB"), name="Activation")+ + coord_polar(theta="y") + # Try to remove that to understand how the chart is built initially + xlim(c(0, 6.5)) +# 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()) + + nodes<-gs_get_pop_paths(gs) + nodes_parent<-nodes[!grepl("CD38|HLADR|GZMB|root$", nodes)] + nodes_cd4<-nodes[grepl("CD38$|HLADR$|GZMB$", nodes) & grepl("/CD4/",nodes)] + nodes_cd8<-nodes[grepl("CD38$|HLADR$|GZMB$", nodes) & grepl("/CD8/",nodes)] + nodes_NK<-nodes[grepl("CD38$|HLADR$|GZMB$", nodes) & grepl("/NK/",nodes)] + + + g1<-ggcyto_arrange(autoplot(gs[[id]], nodes_parent), nrow=3) + g2<-ggcyto_arrange(autoplot(gs[[id]], nodes_cd8), nrow=1) + g3<-ggcyto_arrange(autoplot(gs[[id]], nodes_cd4), nrow=1) + g4<-ggcyto_arrange(autoplot(gs[[id]], nodes_NK), nrow=1) + + g_dots<-ggpubr::ggarrange(gridExtra::gtable_rbind(g1), NULL, + gridExtra::gtable_rbind(g2,g3,g4), nrow=1, widths=c(0.35,0.1,0.55)) + + # g_dots<-gridExtra::gtable_cbind(g1, gridExtra::gtable_rbind(g2,g3,g4)) + + g_all<-ggpubr::ggarrange(g_dots, g_coex, ncol=1, heights=c(0.75,0.25)) + ggsave(paste0(route,id,".Panel3.png"), g_all, width = 14, height = 12) + ggsave(paste0(db_path, "Informes/",id,".Panel3.png"), g_all, width = 14, height = 12) + } + print("Informes finalizados!") } + }) + + observeEvent(input$popexport,{ if (input$phenotype == "Panel1"){ route<-cito_dir @@ -427,6 +560,83 @@ server <- function(input, output) { write.xlsx(list("IC"=ic_sp, "POPULATIONS"=pop_sp), paste0(db_path, "Panel1.xlsx")) print("Tabla Panel1 sincronizada.") } + if (input$phenotype == "Panel3"){ + route<-cito_dir + + ws<-open_flowjo_xml(paste0(route,"Panel3.wsp")) + gs<-flowjo_to_gatingset(ws, name="All Samples") + + sampleNames(gs)<-sapply(sampleNames(gs), function(x) strsplit(x, "Panel3 ")[[1]][2]) %>% + gsub("[[:space:]][0-9]*.fcs_.[0-9]*","", . , perl = T) + + gs<-gs[sampleNames(gs)[!grepl("Iso|ISO|iso|NoBV421",sampleNames(gs))]] + + bool.comb<-apply( + expand.grid(c("","!"), c("","!"), c("","!")), + 1, + function(x) paste0(x[1],"CD38 & ",x[2],"HLADR & ",x[3],"GZMB") + ) + + bool.name<-apply( + expand.grid(c("+","-"), c("+","-"), c("+","-")), + 1, + function(x) paste0("CD38",x[1]," HLADR",x[2]," GZMB",x[3]) + ) + + print("Booleanos CD8") + for (i in 1:length(bool.comb)){ + call<-substitute(booleanFilter(v), list(v=as.symbol(bool.comb[i]))) + boolgate<-eval(call) + gs_pop_add(gs, boolgate, parent="CD8", name = bool.name[i]) + } + + print("Booleanos CD4") + for (i in 1:length(bool.comb)){ + call<-substitute(booleanFilter(v), list(v=as.symbol(bool.comb[i]))) + boolgate<-eval(call) + gs_pop_add(gs, boolgate, parent="CD4", name = bool.name[i]) + } + + print("Booleanos NK") + for (i in 1:length(bool.comb)){ + call<-substitute(booleanFilter(v), list(v=as.symbol(bool.comb[i]))) + boolgate<-eval(call) + gs_pop_add(gs, boolgate, parent="NK", name = bool.name[i]) + } + + recompute(gs) + + names<-sampleNames(gs) # %>% gsub("ab|Ab|AB|iso|Iso|ISO| ","",.) %>% unique() + + nodes<-gs_get_pop_paths(gs) + # nodes<-gsub("â\u0081»", "-", nodes) + # nodes<-gsub("â\u0081º", "+", nodes) + nodes<-nodes[grepl("CD38", nodes)] + nodes<-nodes[!grepl("CD38$|HLADR$|GZMB$", nodes)] + + pop<-gs_pop_get_stats(gs, nodes=nodes,type="percent") %>% as.data.frame %>% mutate(percent=percent*100) + pop$percent<-round(pop$percent, digits=2) + + # pop$pop<-gsub("â\u0081»", "n", pop$pop) + # pop$pop<-gsub("â\u0081º", "p", pop$pop) + # pop$pop<-gsub("-", "n", pop$pop, fixed=T) + # pop$pop<-gsub("+", "p", pop$pop, fixed=T) + # pop$pop<-gsub(" ", "_", pop$pop) + + ## Esto si no hay isotipo + + pop_sp<-pop + pop_sp["Population"]<-str_extract(pop_sp$pop, "/CD[4,8]{1}/") %>% gsub("/|_","",.) + pop_sp$Population[is.na(pop_sp$Population)]<-"NK" + pop_sp$pop<-sapply(strsplit(pop_sp$pop, "/"), tail, 1) + pop_sp<-pop_sp %>% spread(pop, percent) + + pop_sql<-read.xlsx(paste0(db_path,"Panel3.xlsx"), check.names = F, sep.names = " ") + pop_sp<-pop_sp %>% merge(pop_sql %>% slice(0), all=T) %>% select(colnames(pop_sql)) + + write.xlsx(pop_sp, paste0(db_path, "Panel3.xlsx")) + print("Tabla Panel3 sincronizada.") + } }) }