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.

281 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
3 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 %>% pull(Groups))) < 2){
  120. dades$stats<<-null
  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. t_stats %>%
  167. flextable() %>%
  168. theme_vanilla() %>%
  169. fontsize(size=14, part="all") %>%
  170. padding(padding=10, part="all") %>%
  171. color(~ p.adj < 0.05, color = "red")%>%
  172. autofit() %>% htmltools_value()
  173. }
  174. })
  175. output$expPlotUI<- renderUI({
  176. if (!is.null(dades$final)){
  177. plotOutput("expPlot", width=paste0(input$width/10,"px"), height = paste0(input$height/10, "px"))
  178. }
  179. })
  180. output$expPlot <- renderPlot({
  181. observeEvent(dades$final, {})
  182. if (!is.null(dades$final)){
  183. print(dades$stats)
  184. t_substr<-dades$final
  185. t_stats<-dades$stats %>% filter(p.signif != "ns")
  186. t_maps<-dades$maps
  187. ids<-c("Mice", "Groups")
  188. set.seed(123)
  189. g<-ggplot(melt(t_substr, id=ids), aes(variable, value))+
  190. labs(x="", y="Spots/2.5*10^5 cells")+
  191. geom_boxplot(color="black", aes(fill=Groups), alpha=0.4, outlier.alpha = 0, position=position_dodge(width=0.8),width=input$`boxplot-width`)+
  192. geom_jitter(position=position_jitterdodge(jitter.width = 0.2), shape=21, aes(fill=Groups), size=input$`point-size`)+
  193. scale_x_discrete(limits=colnames(t_substr)[!colnames(t_substr) %in% c("Mice", "Groups")])
  194. if (input$theme == "BW"){
  195. g<-g+theme_bw(base_size = input$`font-size`)
  196. }
  197. if (input$theme == "Classic"){
  198. g<-g+theme_classic(base_size = input$`font-size`)
  199. }
  200. if (input$theme == "Default"){
  201. g<-g+theme_gray(base_size = input$`font-size`)
  202. }
  203. g<-g+theme(axis.text.x=element_text(angle=45, hjust=1))
  204. if (input$stats2 == T){
  205. g<-g+geom_segment(data=t_maps$brackets, aes(x=x1, xend=x2, y=y1, yend=y2), color="black")+
  206. geom_text(data=t_stats, aes(t_maps$label$x, t_maps$label$y, label=p.signif), color="black")
  207. }
  208. if (input$legend == F){
  209. g<-g+guides(color=FALSE, fill=FALSE)
  210. }
  211. if (input$colors != ""){
  212. v_col<-strsplit(input$colors, ",")[[1]]
  213. g<-g+scale_color_manual(values=v_col)+
  214. scale_fill_manual(values=v_col)
  215. }
  216. dades$plot<<-g
  217. g
  218. }
  219. }, res=72)
  220. output$downloadData <- downloadHandler(
  221. filename = function() {
  222. paste("elispot", ".zip", sep="")
  223. },
  224. content = function(file){
  225. print(file)
  226. # tempReport <- file.path(tempdir(), "elispots.Rmd")
  227. # file.copy("elispots.Rmd", tempReport, overwrite = TRUE)
  228. params=list(file=input$file1$datapath, positive=input$positive, showstats=input$showstats, test=input$test, umbral_pos=input$umbral_pos)
  229. rmarkdown::render("elispots.Rmd", output_file="Results_elispot.html", params=params, envir = new.env(parent = globalenv()))
  230. zip(file,
  231. c("Results_elispot.html","data4graphpad.xlsx")
  232. )
  233. },
  234. contentType="application/zip"
  235. )
  236. output$downloadPicture <- downloadHandler(
  237. filename = function() {
  238. paste("Figura", ".zip", sep="")
  239. },
  240. content = function(file){
  241. print(file)
  242. # tempReport <- file.path(tempdir(), "elispots.Rmd")
  243. # file.copy("elispots.Rmd", tempReport, overwrite = TRUE)
  244. png("plot.png", width = input$width, height=input$height, units = "px", res=720)
  245. plot(dades$plot)
  246. dev.off()
  247. save(dades$plot, list="plot", file="plot.RObject")
  248. zip(file,
  249. c("plot.png")
  250. )
  251. }
  252. )
  253. }
  254. # Run the application
  255. shinyApp(ui = ui, server = server)