This shiny app generate results from elipots lectures.
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.

286 lines
13 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. library(shiny)
  2. library(openxlsx)
  3. library(readxl)
  4. library(ggplot2)
  5. library(reshape2)
  6. library(dplyr)
  7. library(ggbeeswarm)
  8. library(magrittr)
  9. library(flextable)
  10. source("funcions.R")
  11. ui <- fluidPage(
  12. #Navbar
  13. navbarPage("ELISPOTS",
  14. tabPanel("Diseño",
  15. sidebarPanel(
  16. fileInput(inputId = "file1", label = "Dades", multiple = F),
  17. selectInput(inputId = "test", "Test Estadístic", selected = "Ttest", choices = c("T-test (adj Holm)","Wilcoxon (adj Holm)")),
  18. sliderInput(inputId = "umbral_pos", "Mínimo para positivo:", min = 0, max=100, step = 5, value = 10),
  19. checkboxInput(inputId = "positive", label = "Mostrar positivitat", value = F),
  20. checkboxInput(inputId = "showstats", label = "Mostrar estadística", value = F),
  21. downloadButton("downloadData", "Descarregar Informe")
  22. ),
  23. mainPanel(
  24. plotOutput("distPlot"),
  25. uiOutput("flexstats")
  26. )
  27. ),
  28. tabPanel("Exportar",
  29. sidebarPanel(width=2,
  30. sliderInput("width", "Ancho", min=1000, max=20000, step=1000, value=10000),
  31. sliderInput("height", "Altura", min=1000, max=20000, step=1000, value=6000),
  32. textInput("colors", label="Colors", value=""),
  33. sliderInput("boxplot-width", "% Ancho Boxplots", min=0.1, max=1, step=0.1, value=0.7),
  34. sliderInput("point-size", "Tamaño puntos", min=1, max=10, step=1, value=3),
  35. sliderInput("font-size", "Tamaño textos", min=5, max=30, step=1, value=11),
  36. checkboxInput(inputId = "stats2", label = "Mostrar estadística", value = F),
  37. checkboxInput(inputId = "legend", label = "Mostrar llegenda", value = T),
  38. selectInput("theme", "Seleccionar Tema", selected="BW", choices=c("BW", "Default", "Classic")),
  39. downloadButton("downloadPicture", "Exportar")
  40. ),
  41. mainPanel(
  42. uiOutput("expPlotUI")
  43. )
  44. )
  45. )
  46. )
  47. # Define server logic required to draw a histogram
  48. server <- function(input, output) {
  49. dades<-reactiveValues()
  50. dades$taula<-NULL
  51. dades$stats<-NULL
  52. dades$final<-NULL
  53. dades$maps<-NULL
  54. dades$plot<-NULL
  55. observe({
  56. if (!is.null(input$file1)){
  57. dades$taula<-read_xlsx(input$file1$datapath)
  58. }
  59. })
  60. output$distPlot <- renderPlot({
  61. observeEvent(dades$taula, {})
  62. if (!is.null(dades$taula)){
  63. ctrl<-"Ctrl+"
  64. mock<-"Mock"
  65. table<-dades$taula[,colnames(dades$taula) != "Groups"]
  66. t_mean<-dcast(melt(table, id="Mice"),Mice~variable, mean, na.rm=T)
  67. if (length(grep("Mock", colnames(t_mean))) == 1){
  68. t_substr<-data.frame("Mice"=t_mean[,1],
  69. as.data.frame(t(apply(t_mean[,2:ncol(table)], 1, function(x) x-x[mock])))
  70. )
  71. t_mean_group<-merge(t_mean, unique(dades$taula[c("Mice","Groups")]), id="Mice")
  72. t_mean_group$Groups<-as.factor(t_mean_group$Groups)
  73. mock_mean<-dcast(t_mean_group, Groups~., value.var=mock, mean)
  74. mock_mean[,2]<-mock_mean[,2]*2
  75. mock_mean[mock_mean$. < input$umbral_pos,2]<-input$umbral_pos
  76. t_substr<-merge(t_substr, unique(dades$taula[c("Mice","Groups")]), id="Mice")
  77. t_substr<-t_substr[,c(1, ncol(t_substr), 2:(ncol(t_substr)-1))]
  78. t_substr<-t_substr[,c("Mice", "Groups", colnames(t_substr)[!colnames(t_substr) %in% c("Mice", "Groups")])]
  79. t_substr[,3:ncol(t_substr)]<-apply(t_substr[,3:ncol(t_substr)],2, function(x) replace(x, which(x < 0),0))
  80. colnames(t_substr)<-c("Mice", "Groups", colnames(t_mean)[2:ncol(t_mean)])
  81. }else{
  82. t_especifica<-t_mean[,grep("Mock_", colnames(t_mean), invert=T)]
  83. t_mock<-t_mean[,c(which(colnames(t_mean) == "Mice"),grep("Mock_", colnames(t_mean)))]
  84. t_temp<-melt(t_especifica, variable.name = "condition", value.name = "spots")
  85. t_temp["spots_mock"]<-melt(t_mock, variable.name = "condition")[,"value"]
  86. t_substr<-data.frame(t_temp[c("Mice","condition")], "spots"=t_temp["spots"]-t_temp["spots_mock"])
  87. t_substr<-dcast(t_substr, Mice~condition)
  88. t_substr<-merge(t_substr, unique(dades$taula[c("Mice","Groups")]), id="Mice")
  89. t_substr$Groups<-as.factor(t_substr$Groups)
  90. }
  91. t_substr_gp<-t_substr
  92. t_substr_gp[3:ncol(t_substr)]<-apply(t_substr[3:ncol(t_substr)], 2, function(x) gsub(".",",",x, fixed=T))
  93. t_substr_gp<-t_substr_gp[order(factor(t_substr_gp$Mice, levels = unique(table$Mice))),]
  94. doc<-t(t_substr_gp)
  95. write.xlsx(doc, "data4graphpad.xlsx",rowNames=T)
  96. t<-melt(t_substr[,!colnames(t_substr) %in% c(ctrl, mock)])
  97. if (input$showstats == F){
  98. t_stats<-as.data.frame(matrix(nrow=0, ncol=6))
  99. colnames(t_stats)<-c("variable", "group1", "group2", "p.adj", "p.signif", "Method")
  100. t_maps<-list()
  101. t_maps[["label"]]<-as.data.frame(matrix(nrow = 0, ncol=2))
  102. colnames(t_maps$label)<-c("x", "y")
  103. t_maps[["brackets"]]<-as.data.frame(matrix(nrow = 0, ncol=4))
  104. colnames(t_maps$brackets)<-c("y1", "y2", "x1", "x2")
  105. dades$stats<<-t_stats
  106. dades$maps<<-t_maps
  107. }else{
  108. if (input$test == "T-test (adj Holm)"){
  109. t_stats<-multi_stats(t, "value", "variable", "Groups", stat.test = "ttest")
  110. }
  111. if (input$test == "Wilcoxon (adj Holm)"){
  112. t_stats<-multi_stats(t, "value", "variable", "Groups", stat.test = "wilcox")
  113. }
  114. dades$stats<<-t_stats
  115. t_stats<-t_stats %>% filter(p.signif != "ns")
  116. t_maps<-generate_labstats(t_stats, t, "value", "variable", "Groups")
  117. dades$maps<<-t_maps
  118. }
  119. if (length(unique(dades$taula[,"Groups"])) < 2){
  120. dades$stats<<-1
  121. }
  122. c(ctrl, mock)[c(ctrl, mock) %in% colnames(t_substr)]
  123. dades$final<<-t_substr %>% select(-c(ctrl, mock)[c(ctrl, mock) %in% colnames(t_substr)])
  124. set.seed(123)
  125. if (input$positive == T){
  126. ids=c("Mice", "Groups", c(ctrl, mock)[c(ctrl, mock) %in% colnames(t_substr)])
  127. validate(
  128. need(exists("mock_mean"), "No se puede elegir positividad con múltiples mocks")
  129. )
  130. if (exists("mock_mean")){
  131. ggplot(melt(t_substr, id=ids), aes(variable, value))+
  132. labs(x="", y="Spots/2.5*10^5 cells")+
  133. # geom_errorbar(stat="summary", position=position_dodge(width=0.9), width=0.5, aes(fill=Groups))+
  134. geom_hline(data=mock_mean, aes(color=Groups, yintercept = `.`))+
  135. # geom_bar(stat="summary", position="dodge", color="black", aes(fill=Groups))+
  136. geom_boxplot(color="black", aes(fill=Groups), alpha=0.4, outlier.alpha = 0)+
  137. geom_jitter(position=position_jitterdodge(jitter.width = 0.2), shape=21, aes(fill=Groups), size=3)+
  138. # geom_quasirandom(position = position_quasirandom(), shape=21)+
  139. scale_x_discrete(limits=colnames(t_substr)[!colnames(t_substr) %in% c("Mice", "Groups", ctrl, mock)])+
  140. geom_segment(data=t_maps$brackets, aes(x=x1, xend=x2, y=y1, yend=y2), color="black")+
  141. geom_text(data=t_stats, aes(t_maps$label$x, t_maps$label$y, label=p.signif), color="black")+
  142. theme_bw()+
  143. theme(axis.text.x=element_text(angle=45, hjust=1))
  144. }
  145. }else{
  146. ids=c("Mice", "Groups", c(ctrl, mock)[c(ctrl, mock) %in% colnames(t_substr)])
  147. ggplot(melt(t_substr, id=ids), aes(variable, value))+
  148. labs(x="", y="Spots/2.5*10^5 cells")+
  149. # geom_errorbar(stat="summary", position=position_dodge(width=0.9), width=0.5, aes(fill=Groups))+
  150. # geom_bar(stat="summary", position="dodge", color="black", aes(fill=Groups))+
  151. geom_boxplot(color="black", aes(fill=Groups), alpha=0.4, outlier.alpha = 0)+
  152. geom_jitter(position=position_jitterdodge(jitter.width = 0.2), shape=21, aes(fill=Groups), size=3)+
  153. # geom_quasirandom(width=0.2, position=position_dodge(), shape=21)+
  154. scale_x_discrete(limits=colnames(t_substr)[!colnames(t_substr) %in% c("Mice", "Groups", ctrl, mock)])+
  155. geom_segment(data=t_maps$brackets, aes(x=x1, xend=x2, y=y1, yend=y2), color="black")+
  156. geom_text(data=t_stats, aes(t_maps$label$x, t_maps$label$y, label=p.signif), color="black")+
  157. theme_bw()+
  158. theme(axis.text.x=element_text(angle=45, hjust=1))
  159. }
  160. }
  161. })
  162. output$flexstats <- renderUI({
  163. observeEvent(dades$stats, {})
  164. t_stats<-dades$stats
  165. if (!is.null(dades$stats)){
  166. if (t_stats == 1){
  167. validate(
  168. need(t_stats == 1, "Con un sólo grupo no se puede hacer estadística")
  169. )
  170. }else{
  171. return(
  172. t_stats %>%
  173. flextable() %>%
  174. theme_vanilla() %>%
  175. fontsize(size=14, part="all") %>%
  176. padding(padding=10, part="all") %>%
  177. color(~ p.adj < 0.05, color = "red")%>%
  178. autofit()
  179. ) %>%
  180. htmltools_value()
  181. }
  182. }
  183. })
  184. output$expPlotUI<- renderUI({
  185. if (!is.null(dades$final)){
  186. plotOutput("expPlot", width=paste0(input$width/10,"px"), height = paste0(input$height/10, "px"))
  187. }
  188. })
  189. output$expPlot <- renderPlot({
  190. observeEvent(dades$final, {})
  191. if (!is.null(dades$final)){
  192. t_substr<-dades$final
  193. t_stats<-dades$stats %>% filter(p.signif != "ns")
  194. t_maps<-dades$maps
  195. ids<-c("Mice", "Groups")
  196. set.seed(123)
  197. g<-ggplot(melt(t_substr, id=ids), aes(variable, value))+
  198. labs(x="", y="Spots/2.5*10^5 cells")+
  199. geom_boxplot(color="black", aes(fill=Groups), alpha=0.4, outlier.alpha = 0, position=position_dodge(width=0.8),width=input$`boxplot-width`)+
  200. geom_jitter(position=position_jitterdodge(jitter.width = 0.2), shape=21, aes(fill=Groups), size=input$`point-size`)+
  201. scale_x_discrete(limits=colnames(t_substr)[!colnames(t_substr) %in% c("Mice", "Groups")])
  202. if (input$theme == "BW"){
  203. g<-g+theme_bw(base_size = input$`font-size`)
  204. }
  205. if (input$theme == "Classic"){
  206. g<-g+theme_classic(base_size = input$`font-size`)
  207. }
  208. if (input$theme == "Default"){
  209. g<-g+theme_gray(base_size = input$`font-size`)
  210. }
  211. g<-g+theme(axis.text.x=element_text(angle=45, hjust=1))
  212. if (input$stats2 == T){
  213. g<-g+geom_segment(data=t_maps$brackets, aes(x=x1, xend=x2, y=y1, yend=y2), color="black")+
  214. geom_text(data=t_stats, aes(t_maps$label$x, t_maps$label$y, label=p.signif), color="black")
  215. }
  216. if (input$legend == F){
  217. g<-g+guides(color=FALSE, fill=FALSE)
  218. }
  219. if (input$colors != ""){
  220. v_col<-strsplit(input$colors, ",")[[1]]
  221. g<-g+scale_color_manual(values=v_col)+
  222. scale_fill_manual(values=v_col)
  223. }
  224. dades$plot<<-g
  225. g
  226. }
  227. }, res=72)
  228. output$downloadData <- downloadHandler(
  229. filename = function() {
  230. paste("elispot", ".zip", sep="")
  231. },
  232. content = function(file){
  233. print(file)
  234. # tempReport <- file.path(tempdir(), "elispots.Rmd")
  235. # file.copy("elispots.Rmd", tempReport, overwrite = TRUE)
  236. params=list(file=input$file1$datapath, positive=input$positive, showstats=input$showstats, test=input$test, umbral_pos=input$umbral_pos)
  237. rmarkdown::render("elispots.Rmd", output_file="Results_elispot.html", params=params, envir = new.env(parent = globalenv()))
  238. zip(file,
  239. c("Results_elispot.html","data4graphpad.xlsx")
  240. )
  241. },
  242. contentType="application/zip"
  243. )
  244. output$downloadPicture <- downloadHandler(
  245. filename = function() {
  246. paste("Figura", ".png", sep="")
  247. },
  248. content = function(file){
  249. print(file)
  250. # tempReport <- file.path(tempdir(), "elispots.Rmd")
  251. # file.copy("elispots.Rmd", tempReport, overwrite = TRUE)
  252. png(file, width = input$width, height=input$height, units = "px", res=720)
  253. plot(dades$plot)
  254. dev.off()
  255. }
  256. )
  257. }
  258. # Run the application
  259. shinyApp(ui = ui, server = server)