diff --git a/BDAccess/app.R b/BDAccess/app.R index ba61117..e9a575d 100644 --- a/BDAccess/app.R +++ b/BDAccess/app.R @@ -9,6 +9,9 @@ library(openCyto) library(flowCore) library(flowWorkspace) library(CytoML) +library(ggcyto) + +filter<-dplyr::filter print(getwd()) source("../sqlFunctions.R", encoding = "UTF-8") @@ -73,7 +76,8 @@ ui <- fluidPage( mainPanel( htmlOutput("report"), h3("Nitrogen"), - tableOutput("nitrogen") + tableOutput("nitrogen"), + plotOutput("visorplot") ) ), @@ -83,13 +87,20 @@ ui <- fluidPage( selectInput("phenotype", "Tipo de análisis", selected="Pop", choices=c("Pop", "IC")), ), mainPanel( - actionButton("goButtonDir","Selecciona directorio fenotipo"), - textOutput("session"), - hr(), - actionButton("fcsconvert", "Convertir a fcs"), - hr(), - actionButton("pngexport", "Exportar informes"), - actionButton("popexport", "Actualizar BBDD") + tabsetPanel( + tabPanel("Entrada", + actionButton("goButtonDir","Selecciona directorio fenotipo"), + textOutput("session"), + hr(), + actionButton("fcsconvert", "Convertir a fcs"), + hr(), + actionButton("pngexport", "Exportar informes"), + actionButton("popexport", "Actualizar BBDD") + ), + tabPanel("Visor", + + ) + ) ) ), @@ -843,50 +854,128 @@ server <- function(input, output) { }) observeEvent(input$pngexport,{ - 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,".png"),p,width = 10, height = 10) + 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 == "IC"){ + route<-cito_dir + + ws<-open_flowjo_xml(paste0(route,"IC.wsp")) + gs<-flowjo_to_gatingset(ws, name="All Samples") + + sampleNames(gs)<-sapply(sampleNames(gs), function(x) strsplit(x, "ICs ")[[1]][2]) %>% + gsub("[[:space:]][0-9]*.fcs_.[0-9]*","", . , perl = T) + + names<-sampleNames(gs) %>% gsub("ab|Ab|AB|iso|Iso|ISO| ","",.) %>% unique() + + nodes<-gs_get_pop_paths(gs) + nodes_parent<-nodes[!grepl("CTLA4|LAG3|PD1|TIGIT|TIM3|root$", nodes)] + nodes_cd4<-nodes[grepl("CTLA4$|LAG3$|PD1$|TIGIT$|TIM3$", nodes) & grepl("/CD4/",nodes)] + nodes_cd8<-nodes[grepl("CTLA4$|LAG3$|PD1$|TIGIT$|TIM3$", nodes) & grepl("/CD8/",nodes)] + + for (id in names){ + print(id) + iso<-sampleNames(gs)[grepl(id, sampleNames(gs)) & grepl("iso",sampleNames(gs))] + ab<-sampleNames(gs)[grepl(id, sampleNames(gs)) & grepl("ab",sampleNames(gs))] + + g1<-ggcyto_arrange(autoplot(gs[[ab]], nodes_parent, bins=128), nrow=1) + g2<-ggcyto_arrange(autoplot(gs[[iso]], nodes_cd8, bins=64), nrow=1) + g3<-ggcyto_arrange(autoplot(gs[[ab]], nodes_cd8, bins=64), nrow=1) + g4<-ggcyto_arrange(autoplot(gs[[iso]], nodes_cd4, bins=64), nrow=1) + g5<-ggcyto_arrange(autoplot(gs[[ab]], nodes_cd4, bins=64), nrow=1) + g_all<-gridExtra::gtable_rbind(g1,g2,g3,g4,g5) + ggsave(paste0(route,id,".IC.png"), g_all, width = 10, height = 10) + } + } }) - - observeEvent(input$popexport,{ - 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) - 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) - - 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") - pop$percent<-round(pop$percent, digits=2) - pop_sp<-pop %>% spread(pop, percent) - - vartypes<-rep("Number", pop_sp %>% select(-samples) %>% colnames %>% length) - names(vartypes)<-pop_sp %>% select(-samples) %>% colnames - - sqlSave(dta, pop_sp, tablename="POPULATIONS", append = T, varTypes = vartypes, rownames = F) - print("Tabla POPULATIONS sincronizada.") - - }) + observeEvent(input$popexport,{ + 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) + + 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) + + 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") + pop$percent<-round(pop$percent, digits=2) + pop_sp<-pop %>% spread(pop, percent) + + vartypes<-rep("Number", pop_sp %>% select(-samples) %>% colnames %>% length) + names(vartypes)<-pop_sp %>% select(-samples) %>% colnames + + sqlSave(dta, pop_sp, tablename="POPULATIONS", append = T, varTypes = vartypes, rownames = F) + print("Tabla POPULATIONS sincronizada.") + } + if (input$phenotype == "IC"){ + route<-cito_dir + + ws<-open_flowjo_xml(paste0(route,"IC.wsp")) + gs<-flowjo_to_gatingset(ws, name="All Samples") + + sampleNames(gs)<-sapply(sampleNames(gs), function(x) strsplit(x, "ICs ")[[1]][2]) %>% + gsub("[[:space:]][0-9]*.fcs_.[0-9]*","", . , perl = T) + + nodes<-gs_get_pop_paths(gs) + # nodes<-gsub("â\u0081»", "-", nodes) + # nodes<-gsub("â\u0081º", "+", nodes) + nodes<-nodes[grepl("CTLA4", nodes)] + nodes<-nodes[!grepl("CD4$|CD8$|CTLA4$|TIM3$|PD1$|LAG3$|TIGIT$|/CTLA4â\u0081»$|/TIM3â\u0081»$|/PD1â\u0081»$|/LAG3â\u0081»$|/TIGITâ\u0081»$", 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(" ", "_", pop$pop) + + pop["Type"]<-"ab" + pop[grepl("iso|ISO|Iso",pop$sample),"Type"]<-"iso" + pop$sample<-gsub("iso|ISO|Iso|ab|AB|Ab| ","",pop$sample) + + pop_sp<-pop %>% spread(Type, percent) + pop_sp["Net"]<-pop_sp$ab + pop_sp[!grepl("CTLA4n_LAG3n_PD1n_TIGITn_TIM3n",pop_sp$pop),"Net"]<-pop_sp[!grepl("CTLA4n_LAG3n_PD1n_TIGITn_TIM3n",pop_sp$pop),"ab"]-pop_sp[!grepl("CTLA4n_LAG3n_PD1n_TIGITn_TIM3n",pop_sp$pop),"iso"] + pop_sp$Net[pop_sp$Net < 0]<-0 + pop_sp["Population"]<-str_extract(pop_sp$pop, "/CD[4,8]{1}/") %>% gsub("/","",.) + pop_sp$pop<-sapply(strsplit(pop_sp$pop, "/"), tail, 1) + + pop_sp<-pop_sp %>% select(-ab,-iso) %>% spread(pop,Net) + pop_sp$`CTLA4n_LAG3n_PD1n_TIGITn_TIM3n`<- pop_sp %>% select(-`CTLA4n_LAG3n_PD1n_TIGITn_TIM3n`) %>% group_by(sample,Population) %>% + gather(pop, value, -sample,-Population) %>% summarise(n=100-sum(value)) %>% pull(n) + pop_sp <- rename(pop_sp, "samples"="sample") + + vartypes<-rep("Number", pop_sp %>% select(-samples, -Population) %>% colnames %>% length) + names(vartypes)<-pop_sp %>% select(-samples, -Population) %>% colnames + + sqlSave(dta, pop_sp, tablename="IC", append = T, varTypes = vartypes, rownames = F) + print("Tabla IC sincronizada.") + } + }) + ## scRNAseq ----