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.

169 lines
7.7 KiB

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. # Application title
  13. titlePanel("ELISPOTs"),
  14. sidebarLayout(
  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. )
  29. # Define server logic required to draw a histogram
  30. server <- function(input, output) {
  31. dades<-reactiveValues()
  32. dades$taula<-NULL
  33. dades$stats<-NULL
  34. observe({
  35. if (!is.null(input$file1)){
  36. dades$taula<-read_xlsx(input$file1$datapath)
  37. }
  38. })
  39. output$distPlot <- renderPlot({
  40. observeEvent(dades$taula, {})
  41. if (!is.null(dades$taula)){
  42. ctrl<-"Ctrl+"
  43. mock<-"Mock"
  44. table<-dades$taula[,colnames(dades$taula) != "Groups"]
  45. t_mean<-dcast(melt(table, id="Mice"),Mice~variable, mean, na.rm=T)
  46. t_substr<-data.frame("Mice"=t_mean[,1],
  47. as.data.frame(t(apply(t_mean[,2:ncol(table)], 1, function(x) x-x[mock])))
  48. )
  49. t_mean_group<-merge(t_mean, unique(dades$taula[c("Mice","Groups")]), id="Mice")
  50. mock_mean<-dcast(t_mean_group, Groups~., value.var=mock, mean)
  51. mock_mean[,2]<-mock_mean[,2]*2
  52. mock_mean[mock_mean$. < input$umbral_pos,2]<-input$umbral_pos
  53. t_substr<-merge(t_substr, unique(dades$taula[c("Mice","Groups")]), id="Mice")
  54. t_substr<-t_substr[,c(1, ncol(t_substr), 2:(ncol(t_substr)-1))]
  55. t_substr<-t_substr[,c("Mice", "Groups", colnames(t_substr)[!colnames(t_substr) %in% c("Mice", "Groups")])]
  56. t_substr[,3:ncol(t_substr)]<-apply(t_substr[,3:ncol(t_substr)],2, function(x) replace(x, which(x < 0),0))
  57. colnames(t_substr)<-c("Mice", "Groups", colnames(t_mean)[2:ncol(t_mean)])
  58. t_substr_gp<-t_substr
  59. t_substr_gp[3:ncol(t_substr)]<-apply(t_substr[3:ncol(t_substr)], 2, function(x) gsub(".",",",x, fixed=T))
  60. t_substr_gp<-t_substr_gp[order(factor(t_substr_gp$Mice, levels = unique(table$Mice))),]
  61. doc<-t(t_substr_gp)
  62. write.xlsx(doc, "data4graphpad.xlsx",rowNames=T)
  63. t<-melt(t_substr[,!colnames(t_substr) %in% c(ctrl, mock)])
  64. if (input$test == "T-test (adj Holm)"){
  65. t_stats<-multi_stats(t, "value", "variable", "Groups", stat.test = "ttest")
  66. }
  67. if (input$test == "Wilcoxon (adj Holm)"){
  68. t_stats<-multi_stats(t, "value", "variable", "Groups", stat.test = "wilcox")
  69. }
  70. dades$stats<<-t_stats
  71. t_stats<-t_stats %>% filter(p.signif != "ns")
  72. t_maps<-generate_labstats(t_stats, t, "value", "variable", "Groups")
  73. if (input$showstats == F){
  74. t_stats<-as.data.frame(matrix(nrow=0, ncol=6))
  75. colnames(t_stats)<-c("variable", "group1", "group2", "p.adj", "p.signif", "Method")
  76. t_maps<-list()
  77. t_maps[["label"]]<-as.data.frame(matrix(nrow = 0, ncol=2))
  78. colnames(t_maps$label)<-c("x", "y")
  79. t_maps[["brackets"]]<-as.data.frame(matrix(nrow = 0, ncol=4))
  80. colnames(t_maps$brackets)<-c("y1", "y2", "x1", "x2")
  81. }
  82. set.seed(123)
  83. if (input$positive == T){
  84. ggplot(melt(t_substr, id=c("Mice", ctrl, "Groups")), aes(variable, value))+
  85. labs(x="", y="Spots/2.5*10^5 cells")+
  86. # geom_errorbar(stat="summary", position=position_dodge(width=0.9), width=0.5, aes(fill=Groups))+
  87. geom_hline(data=mock_mean, aes(color=Groups, yintercept = `.`))+
  88. # geom_bar(stat="summary", position="dodge", color="black", aes(fill=Groups))+
  89. geom_boxplot(color="black", aes(fill=Groups), alpha=0.4, outlier.alpha = 0)+
  90. geom_jitter(position=position_jitterdodge(jitter.width = 0.2), shape=21, aes(fill=Groups), size=3)+
  91. # geom_quasirandom(position = position_quasirandom(), shape=21)+
  92. scale_x_discrete(limits=colnames(t_substr)[!colnames(t_substr) %in% c("Mice", "Groups", ctrl, mock)])+
  93. geom_segment(data=t_maps$brackets, aes(x=x1, xend=x2, y=y1, yend=y2), color="black")+
  94. geom_text(data=t_stats, aes(t_maps$label$x, t_maps$label$y, label=p.signif), color="black")+
  95. theme_bw()+
  96. theme(axis.text.x=element_text(angle=45, hjust=1))
  97. }else{
  98. ggplot(melt(t_substr, id=c("Mice", ctrl, "Groups")), aes(variable, value))+
  99. labs(x="", y="Spots/2.5*10^5 cells")+
  100. # geom_errorbar(stat="summary", position=position_dodge(width=0.9), width=0.5, aes(fill=Groups))+
  101. # geom_bar(stat="summary", position="dodge", color="black", aes(fill=Groups))+
  102. geom_boxplot(color="black", aes(fill=Groups), alpha=0.4, outlier.alpha = 0)+
  103. geom_jitter(position=position_jitterdodge(jitter.width = 0.2), shape=21, aes(fill=Groups), size=3)+
  104. # geom_quasirandom(width=0.2, position=position_dodge(), shape=21)+
  105. scale_x_discrete(limits=colnames(t_substr)[!colnames(t_substr) %in% c("Mice", "Groups", ctrl, mock)])+
  106. geom_segment(data=t_maps$brackets, aes(x=x1, xend=x2, y=y1, yend=y2), color="black")+
  107. geom_text(data=t_stats, aes(t_maps$label$x, t_maps$label$y, label=p.signif), color="black")+
  108. theme_bw()+
  109. theme(axis.text.x=element_text(angle=45, hjust=1))
  110. }
  111. }
  112. })
  113. output$flexstats <- renderUI({
  114. observeEvent(dades$stats, {})
  115. t_stats<-dades$stats
  116. if (!is.null(dades$stats)){
  117. return(
  118. t_stats %>%
  119. flextable() %>%
  120. theme_vanilla() %>%
  121. fontsize(size=14, part="all") %>%
  122. padding(padding=10, part="all") %>%
  123. color(~ p.adj < 0.05, color = "red")%>%
  124. autofit()
  125. ) %>%
  126. htmltools_value()
  127. }
  128. })
  129. output$downloadData <- downloadHandler(
  130. filename = function() {
  131. paste("elispot", ".zip", sep="")
  132. },
  133. content = function(file){
  134. print(file)
  135. # tempReport <- file.path(tempdir(), "elispots.Rmd")
  136. # file.copy("elispots.Rmd", tempReport, overwrite = TRUE)
  137. params=list(file=input$file1$datapath, positive=input$positive, showstats=input$showstats, test=input$test, umbral_pos=input$umbral_pos)
  138. rmarkdown::render("elispots.Rmd", output_file="Results_elispot.html", params=params, envir = new.env(parent = globalenv()))
  139. zip(file,
  140. c("Results_elispot.html","data4graphpad.xlsx")
  141. )
  142. },
  143. contentType="application/zip"
  144. )
  145. }
  146. # Run the application
  147. shinyApp(ui = ui, server = server)