|
|
- library(shiny)
- library(openCyto)
- library(flowCore)
- library(flowWorkspace)
- library(CytoML)
- library(ggcyto)
- # library(reshape2)
- # library(CitFuns)
- library(openxlsx)
- library(tidyverse)
-
-
- # Define UI for application that draws a histogram
- ui <- fluidPage(
-
- # Application title
- titlePanel("Análisis Citometría ImmunoPreserve"),
-
- # Sidebar with a slider input for number of bins
- sidebarLayout(
- sidebarPanel(
- selectInput("phenotype", "Panel", selected="Panel1", choices=c("Panel1", "Panel2","Panel3","panel4")),
- ),
- mainPanel(
- textInput("cytopath", label="Directorio fenotipo", value=""),
- actionButton("goButtonDir","Selecciona directorio fenotipo"),
- textOutput("session"),
- hr(),
- actionButton("fcsconvert", "Convertir a fcs"),
- hr(),
- actionButton("pngexport", "Exportar informes"),
- hr(),
- textInput("dbpath", label="Ruta Base de Dades", value=""),
- actionButton("goButtondbpath","Selecciona Ruta Base de Dades"),
- textOutput("sessiondbpath"),
- actionButton("popexport", "Actualizar BBDD")
- )
- )
- )
-
- # Define server logic required to draw a histogram
- server <- function(input, output) {
-
- observe({
- if(input$goButtonDir > 0){
- if (input$cytopath == ""){
- cito_dir<<-choose.dir() %>% gsub("\\","/",. ,fixed=T) %>% paste0("/") %>% stringi::stri_enc_tonative()
- }else{
- cito_dir<<-input$cytopath %>% gsub("\\","/",. ,fixed=T) %>% gsub("/$", "", .) %>% paste0("/") %>% stringi::stri_enc_tonative()
- }
-
- output$session <- renderText(
- cito_dir
- )
- }
- })
-
- observe({
- if(input$goButtondbpath > 0){
- if (input$dbpath == ""){
- db_path<<-choose.dir() %>% gsub("\\","/",. ,fixed=T) %>% paste0("/") %>% stringi::stri_enc_tonative()
- }else{
- db_path<<-input$dbpath %>% gsub("\\","/",. ,fixed=T) %>% gsub("/$", "", .) %>% paste0("/") %>% stringi::stri_enc_tonative()
- }
-
- output$sessiondbpath <- renderText(
- db_path
- )
- }
- })
-
- observeEvent(input$fcsconvert,{
- route<-cito_dir
-
- files<-list.files(route, ".LMD")
- for (lmd in files){
- fcs<-read.FCS(paste0(route,lmd), dataset = 2)
- keyword(fcs)['$FIL']<-paste0(gsub(".LMD","",lmd), ".fcs")
- write.FCS(fcs, paste0(route, gsub(".LMD","",lmd), ".fcs"))
- }
- print("Conversión completada")
- })
-
- 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
-
- ws<-open_flowjo_xml(paste0(route,"Panel1.wsp"))
- gs<-flowjo_to_gatingset(ws, name="All Samples")
-
- sampleNames(gs)<-sapply(sampleNames(gs), function(x) strsplit(x, "Panel1 ")[[1]][2]) %>%
- gsub("[[:space:]][0-9]*.fcs_.[0-9]*","", . , perl = T)
-
- gs<-gs[sampleNames(gs)[!grepl("Iso|ISO|iso",sampleNames(gs))]]
-
- bool.comb<-apply(
- expand.grid(c("","!"), c("","!"), c("","!"), c("","!")),
- 1,
- function(x) paste0(x[1],"CTLA4 & ",x[2],"LAG3 & ",x[3],"PD1 & ",x[4], "TIM3")
- )
-
- bool.name<-apply(
- expand.grid(c("+","-"), c("+","-"), c("+","-"), c("+","-")),
- 1,
- function(x) paste0("CTLA4",x[1]," LAG3",x[2]," PD1",x[3]," TIM3",x[4])
- )
-
- 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])
- }
-
- 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("CTLA4", nodes)]
- nodes<-nodes[!grepl("_CD4$|_CD8$|CTLA4$|TIM3$|PD1$|LAG3$", 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$pop<-sapply(strsplit(pop_sp$pop, "/"), tail, 1)
- pop_sp<-pop_sp %>% spread(pop, percent)
-
- ## Esto si hay Isotipo
-
- # 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)
- # if (input$dbtype == "OV"){
- # pop_sp <- rename(pop_sp, "samples"="sample")
- # }
- # if (input$dbtype %in% c("UM", "CC")){
- # pop_sp <- rename(pop_sp, "CODIGO"="sample")
- #
-
- pop_sql<-read.xlsx(paste0(db_path,"Panel1.xlsx"), sheet = "IC")
- pop_sp<-pop_sp %>% merge(pop_sql %>% slice(0), all=T) %>% select(colnames(pop_sql))
-
- for (id in names){
- print(id)
- # iso<-sampleNames(gs)[grepl(id, sampleNames(gs)) & grepl("iso|Iso|ISO",sampleNames(gs))]
- # ab<-sampleNames(gs)[grepl(id, sampleNames(gs)) & grepl("ab|Ab|AB",sampleNames(gs))]
-
- 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-Z]*-*[0-9T]- *", "", data1$phen)
- data1$phen<-gsub("+ $", "", data1$phen)
- data1$phen[data1$phen == ""]<-"All Negative"
-
- 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
-
- data1["phen4"]<-"LAG3"
- data1[!grepl("LAG3+", data1$phen),"phen4"]<-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_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+ 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
- g_coex<-ggplot(data1)+
- facet_grid(.~factor(Population, levels=c("CD8","CD4")))+
- 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","TIM3","CTLA4","LAG3"))))+
- geom_rect(aes(ymax=ymax, ymin=ymin, xmax=5.9, xmin=5.5, fill=factor(phen2, levels=c("PD1","TIM3","CTLA4","LAG3"))))+
- geom_rect(aes(ymax=ymax, ymin=ymin, xmax=6.4, xmin=6, fill=factor(phen3, levels=c("PD1","TIM3","CTLA4","LAG3"))))+
- geom_rect(aes(ymax=ymax, ymin=ymin, xmax=6.9, xmin=6.5, fill=factor(phen4, levels=c("PD1","TIM3","CTLA4","LAG3"))))+
- scale_fill_manual(values = basic.color, na.value="#FFFFFF00", drop=F, limits=c("PD1","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())
-
- nodes<-gs_get_pop_paths(gs)
- nodes_parent<-nodes[!grepl("CTLA4|LAG3|PD1|TIM3|root$", nodes)]
- nodes_cd4<-nodes[grepl("CTLA4$|LAG3$|PD1$|TIM3$", nodes) & grepl("/_CD4/",nodes)]
- nodes_cd8<-nodes[grepl("CTLA4$|LAG3$|PD1$|TIM3$", nodes) & grepl("/_CD8/",nodes)]
-
-
- g1<-ggcyto_arrange(autoplot(gs[[id]], nodes_parent), nrow=2)
- g2<-ggcyto_arrange(autoplot(gs[[id]], nodes_cd8), nrow=1)
- g3<-ggcyto_arrange(autoplot(gs[[id]], nodes_cd4), nrow=1)
-
- g_dots<-gridExtra::gtable_rbind(g1,g2,g3)
-
- g_all<-ggpubr::ggarrange(g_dots, g_coex, ncol=1, heights=c(0.75,0.25))
- ggsave(paste0(route,id,".IC.png"), g_all, width = 10, height = 14)
- ggsave(paste0(db_path, "Informes/",id,".IC.png"), g_all, width = 10, height = 14)
- }
- print("Informes finalizados!")
- }
- })
-
- 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)
-
- pop_sql<-sqlFetch(dta, "POPULATIONS") %>% slice(0)
- pop_sp<-pop_sp %>% merge(pop_sql, all=T) %>% select(colnames(pop_sql))
-
- 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 == "Panel1"){
- route<-cito_dir
-
- ws<-open_flowjo_xml(paste0(route,"Panel1.wsp"))
- gs<-flowjo_to_gatingset(ws, name="All Samples")
-
- sampleNames(gs)<-sapply(sampleNames(gs), function(x) strsplit(x, "Panel1 ")[[1]][2]) %>%
- gsub("[[:space:]][0-9]*.fcs_.[0-9]*","", . , perl = T)
-
- gs<-gs[sampleNames(gs)[!grepl("Iso|ISO|iso",sampleNames(gs))]]
-
- bool.comb<-apply(
- expand.grid(c("","!"), c("","!"), c("","!"), c("","!")),
- 1,
- function(x) paste0(x[1],"CTLA4 & ",x[2],"LAG3 & ",x[3],"PD1 & ",x[4], "TIM3")
- )
-
- bool.name<-apply(
- expand.grid(c("+","-"), c("+","-"), c("+","-"), c("+","-")),
- 1,
- function(x) paste0("CTLA4",x[1]," LAG3",x[2]," PD1",x[3]," TIM3",x[4])
- )
-
- 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])
- }
-
- 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("CTLA4", nodes)]
- nodes<-nodes[!grepl("_CD4$|_CD8$|CTLA4$|TIM3$|PD1$|LAG3$", 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$pop<-sapply(strsplit(pop_sp$pop, "/"), tail, 1)
- pop_sp<-pop_sp %>% spread(pop, percent)
-
- ## Esto si hay Isotipo
-
- # 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)
- # if (input$dbtype == "OV"){
- # pop_sp <- rename(pop_sp, "samples"="sample")
- # }
- # if (input$dbtype %in% c("UM", "CC")){
- # pop_sp <- rename(pop_sp, "CODIGO"="sample")
- #
-
- pop_sql<-read.xlsx(paste0(db_path,"Panel1.xlsx"), sheet = "IC")
- pop_sp<-pop_sp %>% merge(pop_sql %>% slice(0), all=T) %>% select(colnames(pop_sql))
- ic_sp<-rbind(pop_sql, pop_sp)
-
- nodes<-sapply(strsplit(gs_get_pop_paths(gs), "/"), tail, 1)
- nodes<-gs_get_pop_paths(gs)[grepl("_",nodes)]
- pop<-gs_pop_get_stats(gs, nodes=nodes,type="percent") %>% as.data.frame %>% mutate(percent=percent*100)
- pop<-pop %>% mutate(Population=str_extract(pop, "/_CD[4,8]{1}/"),
- Population=case_when(is.na(Population)~"",
- Population == "/_CD4/"~"_CD4",
- Population == "/_CD8/"~"_CD8",
- TRUE~Population),
- pop=gsub("_","",pop),
- pop=paste0(pop,Population)) %>% select(-Population)
-
- pop$pop<-sapply(strsplit(pop$pop, "/"), tail, 1)
- 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)
-
- pop_sql<-read.xlsx(paste0(db_path,"Panel1.xlsx"), sheet = "POPULATIONS")
- pop_sp<-pop_sp %>% merge(pop_sql %>% slice(0), all=T) %>% select(colnames(pop_sql))
- pop_sp<-rbind(pop_sql,pop_sp)
-
- write.xlsx(list("IC"=ic_sp, "POPULATIONS"=pop_sp), paste0(db_path, "Panel1.xlsx"))
- print("Tabla Panel1 sincronizada.")
- }
- })
-
- }
-
- # Run the application
- shinyApp(ui = ui, server = server)
|