From 0ad5926807d825d38daf49858c3ad43495d9ebee Mon Sep 17 00:00:00 2001 From: marcelcosta Date: Wed, 4 Nov 2020 07:21:23 +0100 Subject: [PATCH] =?UTF-8?q?completar=20an=C3=A1lisis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- invivos/app.R | 190 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 183 insertions(+), 7 deletions(-) diff --git a/invivos/app.R b/invivos/app.R index bee5a9d..8744ca3 100755 --- a/invivos/app.R +++ b/invivos/app.R @@ -6,6 +6,7 @@ library(dplyr) library(car) library(ggbeeswarm) library(gtools) +library(gridExtra) source("../../funcions.R") # Define UI for application @@ -17,21 +18,42 @@ ui <- fluidPage( sidebarPanel( fileInput(inputId = "file_sizes", label = "Hoja de tamaños", multiple = F), selectInput(inputId = "measure_sys", "Sistema de medida", selected = "L-W-D", choices = c("L-W-D","Min-Max","Absorbance")), - sliderInput("ncages", "Cajas", min=1, max=10, value=1), - sliderInput("iterations", "Iteraciones", min=100, max=2000, step=100, value=100), + uiOutput('ncages'), + uiOutput('lowcut'), + uiOutput('upcut'), + uiOutput('goButton'), + uiOutput('iterations'), downloadButton("downloadData", "Descargar Excel") ), mainPanel( + plotOutput("firstPlot"), plotOutput("distPlot") ) ), - tabPanel("Análisis") + tabPanel("Análisis", + sidebarPanel( + fileInput(inputId = "file_analy", label = "Hoja de análisis", multiple = F), + selectInput(inputId = "vacc", "Experimento de Vacunación", selected = "No", choices = c("Sí","No")), + checkboxInput("filter_stats","Filtrar Estadística") + ), + mainPanel( + h3('Ratones'), + tableOutput('ntable'), + h3('Cinéticas'), + plotOutput('cin_group'), + plotOutput('cin_indiv'), + h3('Estadística'), + verbatimTextOutput('stats'), + tableOutput('tab_stats') + )) ) ) # Define server logic required to draw a histogram server <- function(input, output) { + + # Diseño dades<-reactiveValues() dades$taula<-NULL dades$groups<-NULL @@ -42,12 +64,42 @@ server <- function(input, output) { dades$groups<-read.xlsx(input$file_sizes$datapath, sheet = 2)[,1] } }) - output$distPlot <- renderPlot({ + output$firstPlot <- renderPlot({ observeEvent(dades$taula, {}) if (!is.null(dades$taula)){ + ggplot(dades$taula, aes(x="1", y=Volumen))+geom_quasirandom(width=0.2) + } + }) + + output$iterations<-renderUI({ + if (!is.null(dades$taula)){ + sliderInput("iterations", "Iteraciones", min=100, max=2000, step=100, value=100) + } + }) + output$lowcut<-renderUI({ + if (!is.null(dades$taula)){ + sliderInput("lowcut", "Corte inferior", min=0, max=1000, step=5, value=0) + } + }) + output$upcut<-renderUI({ + if (!is.null(dades$taula)){ + sliderInput("upcut", "Corte superior", min=0, max=1000, step=5, value=400) + } + }) + output$goButton<-renderUI({ + if (!is.null(dades$taula)){ + actionButton("goButton", "Selecciona") + } + }) + output$ncages<-renderUI({ + if (is.null(dades$taula)){ + sliderInput("ncages", "Cajas", min=1, max=10, value=1) + } + }) + grafic<-eventReactive(input$goButton,{ df<-dades$taula - up_cuttof<-400 - low_cuttof<-50 + up_cuttof<-input$upcut + low_cuttof<-input$lowcut df<-df[df$Volumen < up_cuttof & df$Volumen > low_cuttof,] df["Mouse"]<-gsub("[a-zA-Z]", "", df$MouseID) @@ -88,6 +140,11 @@ server <- function(input, output) { geom_jitter(width=0.25)+ geom_point(stat="summary", color="blue", size=3)+ lims(y=c(0,max(df_def$Volumen)+10)) + }) + output$distPlot <- renderPlot({ + observeEvent(dades$taula, {}) + if (!is.null(dades$taula)){ + grafic() } }) @@ -144,7 +201,126 @@ server <- function(input, output) { write.xlsx(dtemplate,file) } ) - + + # Análisis + analysis<-reactiveValues() + analysis$taula<-NULL + analysis$taula_def<-NULL + observe({ + if (!is.null(input$file_analy)){ + analysis$taula<-read.xlsx(input$file_analy$datapath, sheet = 1) + } + }) + output$ntable<-renderTable({ + if (!is.null(input$file_analy)){ + observeEvent(analysis$taula, {}) + stattest<-"dunn" + oneside<-"" + cutoff<-750 + + table<-analysis$taula + table<- table %>% add_column("0"=0, .before="12") + table[table$ID.tumor == "R","0"]<-NA + table<-melt(table, id=colnames(table)[1:6], variable.name = "Timepoint") + if ("DPV" %in% colnames(table)){ + table<-dcast(table, Cage+ID.animal+ID.tumor+Group+Timepoint~DPV, value.var = "value") + table$Major<-table$Major/1000 + table$Minor<-table$Minor/1000 + table["Volume"]<-((table$Major*table$Minor*table$Minor)*(pi/6))*1000 + } + if ("TS" %in% colnames(table)){ + table<-dcast(table, Cage+ID.animal+ID.tumor+Group+Timepoint~TS, value.var = "value") + table["Volume"]<-table$`TS-Deep`*table$`TS-Length`*table$`TS-Width`*pi/6 + } + table<-table %>% filter(!is.na(Group)) + analysis$taula_def<-table + + table_plot<-dcast(dcast(table %>% filter(!is.na(Volume)), Cage+`ID.animal`+Group+Timepoint~., value.var = "Volume", fun.aggregate = mean), Group~Timepoint) + table_plot + } + }) + output$cin_group<-renderPlot({ + if (!is.null(input$file_analy) & !is.null(analysis$taula_def)){ + observeEvent(analysis$taula_def, {}) + table<-analysis$taula_def + ggplot(table, aes(as.numeric(as.character(Timepoint)), Volume, color=Group, group=Group))+ + geom_errorbar(stat="summary", width=0.05)+ + geom_line(stat="summary")+ + geom_point(stat="summary")+ + facet_grid(factor(ID.tumor, labels = c("Vaccination", "Rechallenge"))~., scale="free_y")+ + labs(x="Days after tumor challenge")+ + scale_y_continuous(expand = expansion(mult = c(0,0.05)))+ + scale_x_continuous(expand = expansion(mult = c(0,0.05)), limits = c(0, (round(max(as.numeric(as.character(table$Timepoint))) / 5)+1)*5))+ + theme_bw() + } + }) + output$cin_indiv<-renderPlot({ + if (!is.null(input$file_analy) & !is.null(analysis$taula_def)){ + observeEvent(analysis$taula_def, {}) + table<-analysis$taula_def + ggplot(table, aes(as.numeric(as.character(Timepoint)), Volume, color=Group, group=paste0(Cage,ID.animal)))+ + # geom_errorbar(stat="summary", width=0.05)+ + geom_line()+ + geom_point()+ + scale_y_continuous(expand = expansion(mult = c(0,0.05)))+ + scale_x_continuous(expand = expansion(mult = c(0,0.05)), limits = c(0, (round(max(as.numeric(as.character(table$Timepoint))) / 5)+1)*5))+ + facet_grid(factor(ID.tumor, labels = c("Vaccination", "Rechallenge"))~Group, scale="free_y")+ + labs(x="Days after tumor challenge")+ + theme_bw() + } + }) + output$stats<-renderPrint({ + stattest<-"dunn" + oneside<-"" + cutoff<-750 + if (!is.null(input$file_analy) & !is.null(analysis$taula_def)){ + observeEvent(analysis$taula_def, {}) + table<-analysis$taula_def + if (oneside != ""){ + tableR<-filter(table, ID.tumor == rechallenge) %>% filter(!is.na(Volume)) + summary(aov(Volume~Group+Timepoint+Error(ID.animal), data=table[table$ID.tumor == oneside,])) + }else{ + for (side in c("L","R")){ + tableR<-filter(table, ID.tumor == side) %>% filter(!is.na(Volume)) + if (length(unique(tableR$Volume)) > 1 & length(unique(tableR$Timepoint)) > 1){ + print(paste0("Side: ",side)) + # print(summary(aov(Volume~Group+Timepoint+Error(paste0(ID.animal,Cage)), data=tableR))) + print(summary(aov(Volume~Group+Timepoint+Error(ID.animal), data=tableR))) + } + } + } + } + }) + output$tab_stats<-renderTable({ + stattest<-"dunn" + oneside<-"" + cutoff<-750 + if (!is.null(input$file_analy) & !is.null(analysis$taula_def)){ + table<-analysis$taula_def + table_stats<-list() + for (side in c("L","R")){ + tableR<-filter(table, ID.tumor == side) %>% filter(!is.na(Volume)) + if (length(unique(tableR$Volume)) > 1){ + table_stats[[side]]<-multi_stats(tableR, "Volume", "Timepoint", "Group", stat.test=stattest) + } + table_kw<-as.data.frame(matrix(nrow=0, ncol=2)) + for (point in unique(tableR$Timepoint)){ + len_group<-length(unique(tableR %>% filter(Timepoint == point) %>% pull(Group))) + if (len_group > 1){ + table_kw<-rbind(table_kw, data.frame(point,kruskal.test(tableR %>% filter(Timepoint == point) %>% pull(Volume), tableR %>% filter(Timepoint == point) %>% pull(Group))[3][[1]])) + } + } + colnames(table_kw)<-c("Timepoint", "KW-p.val") + table_stats[[side]]<-merge(table_stats[[side]], table_kw) + } + table_stats_def<-bind_rows(table_stats, .id = "ID tumor") + if (input$filter_stats == T){ + table_stats_def %>% filter(p.adj < 0.05) + }else{ + table_stats_def + } + } + }) } # Run the application