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.

646 lines
31 KiB

  1. library(shiny)
  2. library(openCyto)
  3. library(flowCore)
  4. library(flowWorkspace)
  5. library(CytoML)
  6. library(ggcyto)
  7. library(RColorBrewer)
  8. # library(reshape2)
  9. # library(CitFuns)
  10. library(openxlsx)
  11. library(tidyverse)
  12. # Define UI for application that draws a histogram
  13. ui <- fluidPage(
  14. # Application title
  15. titlePanel("Análisis Citometría ImmunoPreserve"),
  16. # Sidebar with a slider input for number of bins
  17. sidebarLayout(
  18. sidebarPanel(
  19. selectInput("phenotype", "Panel", selected="Panel1", choices=c("Panel1", "Panel2","Panel3","panel4")),
  20. ),
  21. mainPanel(
  22. textInput("cytopath", label="Directorio fenotipo", value=""),
  23. actionButton("goButtonDir","Selecciona directorio fenotipo"),
  24. textOutput("session"),
  25. hr(),
  26. actionButton("fcsconvert", "Convertir a fcs"),
  27. hr(),
  28. actionButton("pngexport", "Exportar informes"),
  29. hr(),
  30. textInput("dbpath", label="Ruta Base de Dades", value=""),
  31. actionButton("goButtondbpath","Selecciona Ruta Base de Dades"),
  32. textOutput("sessiondbpath"),
  33. actionButton("popexport", "Actualizar BBDD")
  34. )
  35. )
  36. )
  37. # Define server logic required to draw a histogram
  38. server <- function(input, output) {
  39. observe({
  40. if(input$goButtonDir > 0){
  41. if (input$cytopath == ""){
  42. cito_dir<<-choose.dir() %>% gsub("\\","/",. ,fixed=T) %>% paste0("/") %>% stringi::stri_enc_tonative()
  43. }else{
  44. cito_dir<<-input$cytopath %>% gsub("\\","/",. ,fixed=T) %>% gsub("/$", "", .) %>% paste0("/") %>% stringi::stri_enc_tonative()
  45. }
  46. output$session <- renderText(
  47. cito_dir
  48. )
  49. }
  50. })
  51. observe({
  52. if(input$goButtondbpath > 0){
  53. if (input$dbpath == ""){
  54. db_path<<-choose.dir() %>% gsub("\\","/",. ,fixed=T) %>% paste0("/") %>% stringi::stri_enc_tonative()
  55. }else{
  56. db_path<<-input$dbpath %>% gsub("\\","/",. ,fixed=T) %>% gsub("/$", "", .) %>% paste0("/") %>% stringi::stri_enc_tonative()
  57. }
  58. output$sessiondbpath <- renderText(
  59. db_path
  60. )
  61. }
  62. })
  63. observeEvent(input$fcsconvert,{
  64. route<-cito_dir
  65. files<-list.files(route, ".LMD")
  66. for (lmd in files){
  67. fcs<-read.FCS(paste0(route,lmd), dataset = 2)
  68. keyword(fcs)['$FIL']<-paste0(gsub(".LMD","",lmd), ".fcs")
  69. write.FCS(fcs, paste0(route, gsub(".LMD","",lmd), ".fcs"))
  70. }
  71. print("Conversión completada")
  72. })
  73. observeEvent(input$pngexport,{
  74. if (input$phenotype == "Panel1"){
  75. route<-cito_dir
  76. ws<-open_flowjo_xml(paste0(route,"Panel1.wsp"))
  77. gs<-flowjo_to_gatingset(ws, name="All Samples")
  78. sampleNames(gs)<-sapply(sampleNames(gs), function(x) strsplit(x, "Panel1 ")[[1]][2]) %>%
  79. gsub("[[:space:]][0-9]*.fcs_.[0-9]*","", . , perl = T)
  80. gs<-gs[sampleNames(gs)[!grepl("Iso|ISO|iso",sampleNames(gs))]]
  81. bool.comb<-apply(
  82. expand.grid(c("","!"), c("","!"), c("","!"), c("","!")),
  83. 1,
  84. function(x) paste0(x[1],"CTLA4 & ",x[2],"LAG3 & ",x[3],"PD1 & ",x[4], "TIM3")
  85. )
  86. bool.name<-apply(
  87. expand.grid(c("+","-"), c("+","-"), c("+","-"), c("+","-")),
  88. 1,
  89. function(x) paste0("CTLA4",x[1]," LAG3",x[2]," PD1",x[3]," TIM3",x[4])
  90. )
  91. print("Booleanos CD8")
  92. for (i in 1:length(bool.comb)){
  93. call<-substitute(booleanFilter(v), list(v=as.symbol(bool.comb[i])))
  94. boolgate<-eval(call)
  95. gs_pop_add(gs, boolgate, parent="_CD8", name = bool.name[i])
  96. }
  97. print("Booleanos CD4")
  98. for (i in 1:length(bool.comb)){
  99. call<-substitute(booleanFilter(v), list(v=as.symbol(bool.comb[i])))
  100. boolgate<-eval(call)
  101. gs_pop_add(gs, boolgate, parent="_CD4", name = bool.name[i])
  102. }
  103. recompute(gs)
  104. names<-sampleNames(gs) # %>% gsub("ab|Ab|AB|iso|Iso|ISO| ","",.) %>% unique()
  105. nodes<-gs_get_pop_paths(gs)
  106. # nodes<-gsub("â\u0081»", "-", nodes)
  107. # nodes<-gsub("â\u0081º", "+", nodes)
  108. nodes<-nodes[grepl("CTLA4", nodes)]
  109. nodes<-nodes[!grepl("_CD4$|_CD8$|CTLA4$|TIM3$|PD1$|LAG3$", nodes)]
  110. pop<-gs_pop_get_stats(gs, nodes=nodes,type="percent") %>% as.data.frame %>% mutate(percent=percent*100)
  111. pop$percent<-round(pop$percent, digits=2)
  112. # pop$pop<-gsub("â\u0081»", "n", pop$pop)
  113. # pop$pop<-gsub("â\u0081º", "p", pop$pop)
  114. pop$pop<-gsub("-", "n", pop$pop, fixed=T)
  115. pop$pop<-gsub("+", "p", pop$pop, fixed=T)
  116. pop$pop<-gsub(" ", "_", pop$pop)
  117. ## Esto si no hay isotipo
  118. pop_sp<-pop
  119. pop_sp["Population"]<-str_extract(pop_sp$pop, "/_CD[4,8]{1}/") %>% gsub("/|_","",.)
  120. pop_sp$pop<-sapply(strsplit(pop_sp$pop, "/"), tail, 1)
  121. pop_sp<-pop_sp %>% spread(pop, percent)
  122. ## Esto si hay Isotipo
  123. # pop["Type"]<-"ab"
  124. # pop[grepl("iso|ISO|Iso",pop$sample),"Type"]<-"iso"
  125. # pop$sample<-gsub("iso|ISO|Iso|ab|AB|Ab| ","",pop$sample)
  126. # pop_sp<-pop %>% spread(Type, percent)
  127. # pop_sp["Net"]<-pop_sp$ab
  128. # 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"]
  129. # pop_sp$Net[pop_sp$Net < 0]<-0
  130. # pop_sp["Population"]<-str_extract(pop_sp$pop, "/CD[4,8]{1}/") %>% gsub("/","",.)
  131. # pop_sp$pop<-sapply(strsplit(pop_sp$pop, "/"), tail, 1)
  132. #
  133. # pop_sp<-pop_sp %>% select(-ab,-iso) %>% spread(pop,Net)
  134. # pop_sp$CTLA4n_LAG3n_PD1n_TIGITn_TIM3n<- pop_sp %>% select(-CTLA4n_LAG3n_PD1n_TIGITn_TIM3n) %>% group_by(sample,Population) %>%
  135. # gather(pop, value, -sample,-Population) %>% summarise(n=100-sum(value)) %>% pull(n)
  136. # if (input$dbtype == "OV"){
  137. # pop_sp <- rename(pop_sp, "samples"="sample")
  138. # }
  139. # if (input$dbtype %in% c("UM", "CC")){
  140. # pop_sp <- rename(pop_sp, "CODIGO"="sample")
  141. #
  142. pop_sql<-read.xlsx(paste0(db_path,"Panel1.xlsx"), sheet = "IC")
  143. pop_sp<-pop_sp %>% merge(pop_sql %>% slice(0), all=T) %>% select(colnames(pop_sql))
  144. for (id in names){
  145. print(id)
  146. # iso<-sampleNames(gs)[grepl(id, sampleNames(gs)) & grepl("iso|Iso|ISO",sampleNames(gs))]
  147. # ab<-sampleNames(gs)[grepl(id, sampleNames(gs)) & grepl("ab|Ab|AB",sampleNames(gs))]
  148. data<-pop_sp %>% filter(sample == id)
  149. data1<-data %>% gather(phen, value, -sample, -Population)
  150. data1$phen<-gsub("p","+",data1$phen)
  151. data1$phen<-gsub("n","-",data1$phen)
  152. data1$phen<-gsub("_"," ",data1$phen)
  153. data1$phen<-gsub("n","-",data1$phen, fixed = T)
  154. data1$phen<-gsub("p","+",data1$phen, fixed = T)
  155. data1$phen<-gsub("_"," ",data1$phen)
  156. data1[data1$value < 1, "phen"]<-"Other"
  157. data1$phen<-gsub("[A-Z]*-*[0-9T]- *", "", data1$phen)
  158. data1$phen<-gsub("+ $", "", data1$phen)
  159. data1$phen[data1$phen == ""]<-"All Negative"
  160. data1["phen1"]<-"PD1"
  161. data1[!grepl("PD1+", data1$phen),"phen1"]<-NA
  162. data1["phen2"]<-"TIM3"
  163. data1[!grepl("TIM3+", data1$phen),"phen2"]<-NA
  164. data1["phen3"]<-"CTLA4"
  165. data1[!grepl("CTLA4+", data1$phen),"phen3"]<-NA
  166. data1["phen4"]<-"LAG3"
  167. data1[!grepl("LAG3+", data1$phen),"phen4"]<-NA
  168. data1<-data1 %>% arrange(desc(value))
  169. data2<-data1 %>% filter(!phen %in% c("All Negative","Other"))
  170. data1<-rbind(data2, data1 %>% filter(phen %in% c("All Negative","Other")) %>% arrange(desc(phen)))
  171. data_cd8<-data1 %>% filter(Population == "CD8")
  172. data_cd4<-data1 %>% filter(Population == "CD4")
  173. data_cd8$ymax<-cumsum(data_cd8$value)
  174. data_cd8$ymin<-c(0, head(data_cd8$ymax, n=-1))
  175. data_cd4$ymax<-cumsum(data_cd4$value)
  176. data_cd4$ymin<-c(0, head(data_cd4$ymax, n=-1))
  177. data1<-rbind(data_cd8, data_cd4)
  178. color<-c(c("CTLA4+ LAG3+ PD1+ TIM3+"="black","All Negative"="grey90","Other"="grey50", "PD1+"="#C07AFF", "CTLA4+"="#3EB3DE","TIM3+"="#5EF551","LAG3+"="#DEBB3E"),
  179. c("CTLA4+ PD1+"="#6666FF","PD1+ TIM3+"="#849CA8", "LAG3+ PD1+"="#C47F9F", "CTLA4+ TIM3+"="#4ED498", "CTLA4+ LAG3+"="#8EB78E", "LAG3+ TIM3+"="#9ED848"),
  180. c("CTLA4+ PD1+ TIM3+"="#B81515", "LAG3+ PD1+ TIM3+"="#0f5860"))
  181. basic.color<-color[c("PD1+","TIM3+","CTLA4+","LAG3+")]
  182. names(basic.color)<-c("PD1","TIM3","CTLA4","LAG3")
  183. # Make the plot
  184. g_coex<-ggplot(data1)+
  185. facet_grid(.~factor(Population, levels=c("CD8","CD4")))+
  186. geom_rect(aes(ymax=ymax, ymin=ymin, xmax=4.5, xmin=0), fill=color[data1$phen])+
  187. geom_rect(aes(ymax=ymax, ymin=ymin, xmax=5.4, xmin=5, fill=factor(phen1, levels=c("PD1","TIM3","CTLA4","LAG3"))))+
  188. geom_rect(aes(ymax=ymax, ymin=ymin, xmax=5.9, xmin=5.5, fill=factor(phen2, levels=c("PD1","TIM3","CTLA4","LAG3"))))+
  189. geom_rect(aes(ymax=ymax, ymin=ymin, xmax=6.4, xmin=6, fill=factor(phen3, levels=c("PD1","TIM3","CTLA4","LAG3"))))+
  190. geom_rect(aes(ymax=ymax, ymin=ymin, xmax=6.9, xmin=6.5, fill=factor(phen4, levels=c("PD1","TIM3","CTLA4","LAG3"))))+
  191. scale_fill_manual(values = basic.color, na.value="#FFFFFF00", drop=F, limits=c("PD1","TIM3","CTLA4","LAG3"), name="IC")+
  192. coord_polar(theta="y") + # Try to remove that to understand how the chart is built initially
  193. xlim(c(0, 8)) +# Try to remove that to see how to make a pie chart
  194. theme_classic()+
  195. theme(strip.background = element_blank(),
  196. strip.text = element_text(size=12, face="bold"),
  197. axis.line = element_blank(),
  198. axis.ticks = element_blank(),
  199. # plot.margin = margin(-200,0,0,0),
  200. axis.text = element_blank())
  201. nodes<-gs_get_pop_paths(gs)
  202. nodes_parent<-nodes[!grepl("CTLA4|LAG3|PD1|TIM3|root$", nodes)]
  203. nodes_cd4<-nodes[grepl("CTLA4$|LAG3$|PD1$|TIM3$", nodes) & grepl("/_CD4/",nodes)]
  204. nodes_cd8<-nodes[grepl("CTLA4$|LAG3$|PD1$|TIM3$", nodes) & grepl("/_CD8/",nodes)]
  205. g1<-ggcyto_arrange(autoplot(gs[[id]], nodes_parent), nrow=2)
  206. g2<-ggcyto_arrange(autoplot(gs[[id]], nodes_cd8), nrow=1)
  207. g3<-ggcyto_arrange(autoplot(gs[[id]], nodes_cd4), nrow=1)
  208. g_dots<-gridExtra::gtable_rbind(g1,g2,g3)
  209. g_all<-ggpubr::ggarrange(g_dots, g_coex, ncol=1, heights=c(0.75,0.25))
  210. ggsave(paste0(route,id,".IC.png"), g_all, width = 10, height = 14)
  211. ggsave(paste0(db_path, "Informes/",id,".IC.png"), g_all, width = 10, height = 14)
  212. }
  213. print("Informes finalizados!")
  214. }
  215. if (input$phenotype == "Panel3"){
  216. route<-cito_dir
  217. ws<-open_flowjo_xml(paste0(route,"Panel3.wsp"))
  218. gs<-flowjo_to_gatingset(ws, name="All Samples")
  219. sampleNames(gs)<-sapply(sampleNames(gs), function(x) strsplit(x, "Panel3 ")[[1]][2]) %>%
  220. gsub("[[:space:]][0-9]*.fcs_.[0-9]*","", . , perl = T)
  221. gs<-gs[sampleNames(gs)[!grepl("Iso|ISO|iso|NoBV421",sampleNames(gs))]]
  222. bool.comb<-apply(
  223. expand.grid(c("","!"), c("","!"), c("","!")),
  224. 1,
  225. function(x) paste0(x[1],"CD38 & ",x[2],"HLADR & ",x[3],"GZMB")
  226. )
  227. bool.name<-apply(
  228. expand.grid(c("+","-"), c("+","-"), c("+","-")),
  229. 1,
  230. function(x) paste0("CD38",x[1]," HLADR",x[2]," GZMB",x[3])
  231. )
  232. print("Booleanos CD8")
  233. for (i in 1:length(bool.comb)){
  234. call<-substitute(booleanFilter(v), list(v=as.symbol(bool.comb[i])))
  235. boolgate<-eval(call)
  236. gs_pop_add(gs, boolgate, parent="CD8", name = bool.name[i])
  237. }
  238. print("Booleanos CD4")
  239. for (i in 1:length(bool.comb)){
  240. call<-substitute(booleanFilter(v), list(v=as.symbol(bool.comb[i])))
  241. boolgate<-eval(call)
  242. gs_pop_add(gs, boolgate, parent="CD4", name = bool.name[i])
  243. }
  244. print("Booleanos NK")
  245. for (i in 1:length(bool.comb)){
  246. call<-substitute(booleanFilter(v), list(v=as.symbol(bool.comb[i])))
  247. boolgate<-eval(call)
  248. gs_pop_add(gs, boolgate, parent="NK", name = bool.name[i])
  249. }
  250. recompute(gs)
  251. names<-sampleNames(gs) # %>% gsub("ab|Ab|AB|iso|Iso|ISO| ","",.) %>% unique()
  252. nodes<-gs_get_pop_paths(gs)
  253. # nodes<-gsub("â\u0081»", "-", nodes)
  254. # nodes<-gsub("â\u0081º", "+", nodes)
  255. nodes<-nodes[grepl("CD38", nodes)]
  256. nodes<-nodes[!grepl("CD38$|HLADR$|GZMB$", nodes)]
  257. pop<-gs_pop_get_stats(gs, nodes=nodes,type="percent") %>% as.data.frame %>% mutate(percent=percent*100)
  258. pop$percent<-round(pop$percent, digits=2)
  259. # pop$pop<-gsub("â\u0081»", "n", pop$pop)
  260. # pop$pop<-gsub("â\u0081º", "p", pop$pop)
  261. # pop$pop<-gsub("-", "n", pop$pop, fixed=T)
  262. # pop$pop<-gsub("+", "p", pop$pop, fixed=T)
  263. # pop$pop<-gsub(" ", "_", pop$pop)
  264. ## Esto si no hay isotipo
  265. pop_sp<-pop
  266. pop_sp["Population"]<-str_extract(pop_sp$pop, "/CD[4,8]{1}/") %>% gsub("/|_","",.)
  267. pop_sp$Population[is.na(pop_sp$Population)]<-"NK"
  268. pop_sp$pop<-sapply(strsplit(pop_sp$pop, "/"), tail, 1)
  269. pop_sp<-pop_sp %>% spread(pop, percent)
  270. pop_sql<-read.xlsx(paste0(db_path,"Panel3.xlsx"), check.names = F, sep.names = " ")
  271. pop_sp<-pop_sp %>% merge(pop_sql %>% slice(0), all=T) %>% select(colnames(pop_sql))
  272. for (id in names){
  273. print(id)
  274. data<-pop_sp %>% filter(sample == id)
  275. data1<-data %>% gather(phen, value, -sample, -Population)
  276. # data1$phen<-gsub("p","+",data1$phen)
  277. # data1$phen<-gsub("n","-",data1$phen)
  278. # data1$phen<-gsub("_"," ",data1$phen)
  279. # data1$phen<-gsub("n","-",data1$phen, fixed = T)
  280. # data1$phen<-gsub("p","+",data1$phen, fixed = T)
  281. # data1$phen<-gsub("_"," ",data1$phen)
  282. # data1[data1$value < 1, "phen"]<-"Other"
  283. data1$phen<-gsub("[A-Z0-9]*-", "", data1$phen)
  284. data1$phen<-gsub(" ", " ", data1$phen)
  285. data1$phen<-gsub("[ ]*$", "", data1$phen)
  286. data1$phen<-gsub("^[ ]*", "", data1$phen)
  287. data1$phen[data1$phen == ""]<-"All Negative"
  288. data1["phen1"]<-"CD38"
  289. data1[!grepl("CD38+", data1$phen),"phen1"]<-NA
  290. data1["phen2"]<-"HLADR"
  291. data1[!grepl("HLADR+", data1$phen),"phen2"]<-NA
  292. data1["phen3"]<-"GZMB"
  293. data1[!grepl("GZMB+", data1$phen),"phen3"]<-NA
  294. data1<-data1 %>% arrange(desc(value))
  295. data2<-data1 %>% filter(!phen %in% c("All Negative","Other"))
  296. data1<-rbind(data2, data1 %>% filter(phen %in% c("All Negative","Other")) %>% arrange(desc(phen)))
  297. data_cd8<-data1 %>% filter(Population == "CD8")
  298. data_cd4<-data1 %>% filter(Population == "CD4")
  299. data_NK<-data1 %>% filter(Population == "NK")
  300. data_cd8$ymax<-cumsum(data_cd8$value)
  301. data_cd8$ymin<-c(0, head(data_cd8$ymax, n=-1))
  302. data_cd4$ymax<-cumsum(data_cd4$value)
  303. data_cd4$ymin<-c(0, head(data_cd4$ymax, n=-1))
  304. data_NK$ymax<-cumsum(data_NK$value)
  305. data_NK$ymin<-c(0, head(data_NK$ymax, n=-1))
  306. data1<-rbind(data_cd8, data_cd4, data_NK)
  307. # color<-c(c("CTLA4+ LAG3+ PD1+ TIM3+"="black","All Negative"="grey90","Other"="grey50", "PD1+"="#C07AFF", "CTLA4+"="#3EB3DE","TIM3+"="#5EF551","LAG3+"="#DEBB3E"),
  308. # c("CTLA4+ PD1+"="#6666FF","PD1+ TIM3+"="#849CA8", "LAG3+ PD1+"="#C47F9F", "CTLA4+ TIM3+"="#4ED498", "CTLA4+ LAG3+"="#8EB78E", "LAG3+ TIM3+"="#9ED848"),
  309. # c("CTLA4+ PD1+ TIM3+"="#B81515", "LAG3+ PD1+ TIM3+"="#0f5860"))
  310. # basic.color<-color[c("PD1+","TIM3+","CTLA4+","LAG3+")]
  311. # names(basic.color)<-c("PD1","TIM3","CTLA4","LAG3")
  312. # Make the plot
  313. color<-colorRampPalette(brewer.pal(8, "Set3"))(length(bool.name))
  314. names(color)<-bool.name %>% gsub("[A-Z0-9]*-", "", .) %>% gsub(" "," ",.) %>% gsub("[ ]*$", "", .) %>% gsub("^[ ]*", "", .) %>% gsub("^$","All Negative", .)
  315. color["All Negative"]<-"grey90"
  316. basic.color<-color[c("CD38+","HLADR+","GZMB+")]
  317. names(basic.color)<-c("CD38","HLADR","GZMB")
  318. g_coex<-ggplot(data1)+
  319. facet_grid(.~factor(Population, levels=c("CD8","CD4","NK")))+
  320. geom_rect(aes(ymax=ymax, ymin=ymin, xmax=4.5, xmin=0), fill=color[data1$phen])+
  321. geom_rect(aes(ymax=ymax, ymin=ymin, xmax=5.4, xmin=5, fill=factor(phen1, levels=c("CD38","HLADR","GZMB"))))+
  322. geom_rect(aes(ymax=ymax, ymin=ymin, xmax=5.9, xmin=5.5, fill=factor(phen2, levels=c("CD38","HLADR","GZMB"))))+
  323. geom_rect(aes(ymax=ymax, ymin=ymin, xmax=6.4, xmin=6, fill=factor(phen3, levels=c("CD38","HLADR","GZMB"))))+
  324. scale_fill_manual(values = basic.color, na.value="#FFFFFF00", drop=F, limits=c("CD38","HLADR","GZMB"), name="Activation")+
  325. coord_polar(theta="y") + # Try to remove that to understand how the chart is built initially
  326. xlim(c(0, 6.5)) +# Try to remove that to see how to make a pie chart
  327. theme_classic()+
  328. theme(strip.background = element_blank(),
  329. strip.text = element_text(size=12, face="bold"),
  330. axis.line = element_blank(),
  331. axis.ticks = element_blank(),
  332. # plot.margin = margin(-200,0,0,0),
  333. axis.text = element_blank())
  334. nodes<-gs_get_pop_paths(gs)
  335. nodes_parent<-nodes[!grepl("CD38|HLADR|GZMB|root$", nodes)]
  336. nodes_cd4<-nodes[grepl("CD38$|HLADR$|GZMB$", nodes) & grepl("/CD4/",nodes)]
  337. nodes_cd8<-nodes[grepl("CD38$|HLADR$|GZMB$", nodes) & grepl("/CD8/",nodes)]
  338. nodes_NK<-nodes[grepl("CD38$|HLADR$|GZMB$", nodes) & grepl("/NK/",nodes)]
  339. g1<-ggcyto_arrange(autoplot(gs[[id]], nodes_parent), nrow=3)
  340. g2<-ggcyto_arrange(autoplot(gs[[id]], nodes_cd8), nrow=1)
  341. g3<-ggcyto_arrange(autoplot(gs[[id]], nodes_cd4), nrow=1)
  342. g4<-ggcyto_arrange(autoplot(gs[[id]], nodes_NK), nrow=1)
  343. g_dots<-ggpubr::ggarrange(gridExtra::gtable_rbind(g1), NULL,
  344. gridExtra::gtable_rbind(g2,g3,g4), nrow=1, widths=c(0.35,0.1,0.55))
  345. # g_dots<-gridExtra::gtable_cbind(g1, gridExtra::gtable_rbind(g2,g3,g4))
  346. g_all<-ggpubr::ggarrange(g_dots, g_coex, ncol=1, heights=c(0.75,0.25))
  347. ggsave(paste0(route,id,".Panel3.png"), g_all, width = 14, height = 12)
  348. ggsave(paste0(db_path, "Informes/",id,".Panel3.png"), g_all, width = 14, height = 12)
  349. }
  350. print("Informes finalizados!")
  351. }
  352. })
  353. observeEvent(input$popexport,{
  354. if (input$phenotype == "Panel1"){
  355. route<-cito_dir
  356. ws<-open_flowjo_xml(paste0(route,"Panel1.wsp"))
  357. gs<-flowjo_to_gatingset(ws, name="All Samples")
  358. sampleNames(gs)<-sapply(sampleNames(gs), function(x) strsplit(x, "Panel1 ")[[1]][2]) %>%
  359. gsub("[[:space:]][0-9]*.fcs_.[0-9]*","", . , perl = T)
  360. gs<-gs[sampleNames(gs)[!grepl("Iso|ISO|iso",sampleNames(gs))]]
  361. bool.comb<-apply(
  362. expand.grid(c("","!"), c("","!"), c("","!"), c("","!")),
  363. 1,
  364. function(x) paste0(x[1],"CTLA4 & ",x[2],"LAG3 & ",x[3],"PD1 & ",x[4], "TIM3")
  365. )
  366. bool.name<-apply(
  367. expand.grid(c("+","-"), c("+","-"), c("+","-"), c("+","-")),
  368. 1,
  369. function(x) paste0("CTLA4",x[1]," LAG3",x[2]," PD1",x[3]," TIM3",x[4])
  370. )
  371. print("Booleanos CD8")
  372. for (i in 1:length(bool.comb)){
  373. call<-substitute(booleanFilter(v), list(v=as.symbol(bool.comb[i])))
  374. boolgate<-eval(call)
  375. gs_pop_add(gs, boolgate, parent="_CD8", name = bool.name[i])
  376. }
  377. print("Booleanos CD4")
  378. for (i in 1:length(bool.comb)){
  379. call<-substitute(booleanFilter(v), list(v=as.symbol(bool.comb[i])))
  380. boolgate<-eval(call)
  381. gs_pop_add(gs, boolgate, parent="_CD4", name = bool.name[i])
  382. }
  383. recompute(gs)
  384. names<-sampleNames(gs) # %>% gsub("ab|Ab|AB|iso|Iso|ISO| ","",.) %>% unique()
  385. nodes<-gs_get_pop_paths(gs)
  386. # nodes<-gsub("â\u0081»", "-", nodes)
  387. # nodes<-gsub("â\u0081º", "+", nodes)
  388. nodes<-nodes[grepl("CTLA4", nodes)]
  389. nodes<-nodes[!grepl("_CD4$|_CD8$|CTLA4$|TIM3$|PD1$|LAG3$", nodes)]
  390. pop<-gs_pop_get_stats(gs, nodes=nodes,type="percent") %>% as.data.frame %>% mutate(percent=percent*100)
  391. pop$percent<-round(pop$percent, digits=2)
  392. # pop$pop<-gsub("â\u0081»", "n", pop$pop)
  393. # pop$pop<-gsub("â\u0081º", "p", pop$pop)
  394. pop$pop<-gsub("-", "n", pop$pop, fixed=T)
  395. pop$pop<-gsub("+", "p", pop$pop, fixed=T)
  396. pop$pop<-gsub(" ", "_", pop$pop)
  397. ## Esto si no hay isotipo
  398. pop_sp<-pop
  399. pop_sp["Population"]<-str_extract(pop_sp$pop, "/_CD[4,8]{1}/") %>% gsub("/|_","",.)
  400. pop_sp$pop<-sapply(strsplit(pop_sp$pop, "/"), tail, 1)
  401. pop_sp<-pop_sp %>% spread(pop, percent)
  402. ## Esto si hay Isotipo
  403. # pop["Type"]<-"ab"
  404. # pop[grepl("iso|ISO|Iso",pop$sample),"Type"]<-"iso"
  405. # pop$sample<-gsub("iso|ISO|Iso|ab|AB|Ab| ","",pop$sample)
  406. # pop_sp<-pop %>% spread(Type, percent)
  407. # pop_sp["Net"]<-pop_sp$ab
  408. # 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"]
  409. # pop_sp$Net[pop_sp$Net < 0]<-0
  410. # pop_sp["Population"]<-str_extract(pop_sp$pop, "/CD[4,8]{1}/") %>% gsub("/","",.)
  411. # pop_sp$pop<-sapply(strsplit(pop_sp$pop, "/"), tail, 1)
  412. #
  413. # pop_sp<-pop_sp %>% select(-ab,-iso) %>% spread(pop,Net)
  414. # pop_sp$CTLA4n_LAG3n_PD1n_TIGITn_TIM3n<- pop_sp %>% select(-CTLA4n_LAG3n_PD1n_TIGITn_TIM3n) %>% group_by(sample,Population) %>%
  415. # gather(pop, value, -sample,-Population) %>% summarise(n=100-sum(value)) %>% pull(n)
  416. # if (input$dbtype == "OV"){
  417. # pop_sp <- rename(pop_sp, "samples"="sample")
  418. # }
  419. # if (input$dbtype %in% c("UM", "CC")){
  420. # pop_sp <- rename(pop_sp, "CODIGO"="sample")
  421. #
  422. pop_sql<-read.xlsx(paste0(db_path,"Panel1.xlsx"), sheet = "IC")
  423. pop_sp<-pop_sp %>% merge(pop_sql %>% slice(0), all=T) %>% select(colnames(pop_sql))
  424. ic_sp<-rbind(pop_sql, pop_sp)
  425. nodes<-sapply(strsplit(gs_get_pop_paths(gs), "/"), tail, 1)
  426. nodes<-gs_get_pop_paths(gs)[grepl("_",nodes)]
  427. pop<-gs_pop_get_stats(gs, nodes=nodes,type="percent") %>% as.data.frame %>% mutate(percent=percent*100)
  428. pop<-pop %>% mutate(Population=str_extract(pop, "/_CD[4,8]{1}/"),
  429. Population=case_when(is.na(Population)~"",
  430. Population == "/_CD4/"~"_CD4",
  431. Population == "/_CD8/"~"_CD8",
  432. TRUE~Population),
  433. pop=gsub("_","",pop),
  434. pop=paste0(pop,Population)) %>% select(-Population)
  435. pop$pop<-sapply(strsplit(pop$pop, "/"), tail, 1)
  436. pop$pop<-gsub(" ","_",pop$pop)
  437. # pop$pop<-gsub("+","pos",pop$pop, fixed=T)
  438. # pop$pop<-gsub("-","neg",pop$pop, fixed=T)
  439. # pop<-rename(pop, "samples"="sample")
  440. pop$percent<-round(pop$percent, digits=2)
  441. pop_sp<-pop %>% spread(pop, percent)
  442. pop_sql<-read.xlsx(paste0(db_path,"Panel1.xlsx"), sheet = "POPULATIONS")
  443. pop_sp<-pop_sp %>% merge(pop_sql %>% slice(0), all=T) %>% select(colnames(pop_sql))
  444. pop_sp<-rbind(pop_sql,pop_sp)
  445. write.xlsx(list("IC"=ic_sp, "POPULATIONS"=pop_sp), paste0(db_path, "Panel1.xlsx"))
  446. print("Tabla Panel1 sincronizada.")
  447. }
  448. if (input$phenotype == "Panel3"){
  449. route<-cito_dir
  450. ws<-open_flowjo_xml(paste0(route,"Panel3.wsp"))
  451. gs<-flowjo_to_gatingset(ws, name="All Samples")
  452. sampleNames(gs)<-sapply(sampleNames(gs), function(x) strsplit(x, "Panel3 ")[[1]][2]) %>%
  453. gsub("[[:space:]][0-9]*.fcs_.[0-9]*","", . , perl = T)
  454. gs<-gs[sampleNames(gs)[!grepl("Iso|ISO|iso|NoBV421",sampleNames(gs))]]
  455. bool.comb<-apply(
  456. expand.grid(c("","!"), c("","!"), c("","!")),
  457. 1,
  458. function(x) paste0(x[1],"CD38 & ",x[2],"HLADR & ",x[3],"GZMB")
  459. )
  460. bool.name<-apply(
  461. expand.grid(c("+","-"), c("+","-"), c("+","-")),
  462. 1,
  463. function(x) paste0("CD38",x[1]," HLADR",x[2]," GZMB",x[3])
  464. )
  465. print("Booleanos CD8")
  466. for (i in 1:length(bool.comb)){
  467. call<-substitute(booleanFilter(v), list(v=as.symbol(bool.comb[i])))
  468. boolgate<-eval(call)
  469. gs_pop_add(gs, boolgate, parent="CD8", name = bool.name[i])
  470. }
  471. print("Booleanos CD4")
  472. for (i in 1:length(bool.comb)){
  473. call<-substitute(booleanFilter(v), list(v=as.symbol(bool.comb[i])))
  474. boolgate<-eval(call)
  475. gs_pop_add(gs, boolgate, parent="CD4", name = bool.name[i])
  476. }
  477. print("Booleanos NK")
  478. for (i in 1:length(bool.comb)){
  479. call<-substitute(booleanFilter(v), list(v=as.symbol(bool.comb[i])))
  480. boolgate<-eval(call)
  481. gs_pop_add(gs, boolgate, parent="NK", name = bool.name[i])
  482. }
  483. recompute(gs)
  484. names<-sampleNames(gs) # %>% gsub("ab|Ab|AB|iso|Iso|ISO| ","",.) %>% unique()
  485. nodes<-gs_get_pop_paths(gs)
  486. # nodes<-gsub("â\u0081»", "-", nodes)
  487. # nodes<-gsub("â\u0081º", "+", nodes)
  488. nodes<-nodes[grepl("CD38", nodes)]
  489. nodes<-nodes[!grepl("CD38$|HLADR$|GZMB$", nodes)]
  490. pop<-gs_pop_get_stats(gs, nodes=nodes,type="percent") %>% as.data.frame %>% mutate(percent=percent*100)
  491. pop$percent<-round(pop$percent, digits=2)
  492. # pop$pop<-gsub("â\u0081»", "n", pop$pop)
  493. # pop$pop<-gsub("â\u0081º", "p", pop$pop)
  494. # pop$pop<-gsub("-", "n", pop$pop, fixed=T)
  495. # pop$pop<-gsub("+", "p", pop$pop, fixed=T)
  496. # pop$pop<-gsub(" ", "_", pop$pop)
  497. ## Esto si no hay isotipo
  498. pop_sp<-pop
  499. pop_sp["Population"]<-str_extract(pop_sp$pop, "/CD[4,8]{1}/") %>% gsub("/|_","",.)
  500. pop_sp$Population[is.na(pop_sp$Population)]<-"NK"
  501. pop_sp$pop<-sapply(strsplit(pop_sp$pop, "/"), tail, 1)
  502. pop_sp<-pop_sp %>% spread(pop, percent)
  503. pop_sql<-read.xlsx(paste0(db_path,"Panel3.xlsx"), check.names = F, sep.names = " ")
  504. pop_sp<-pop_sp %>% merge(pop_sql %>% slice(0), all=T) %>% select(colnames(pop_sql))
  505. pop_sp<-rbind(pop_sql, pop_sp)
  506. write.xlsx(pop_sp, paste0(db_path, "Panel3.xlsx"))
  507. print("Tabla Panel3 sincronizada.")
  508. }
  509. })
  510. }
  511. # Run the application
  512. shinyApp(ui = ui, server = server)