From ac2630acd3a5e41dbb321755ede4f9c7da81a900 Mon Sep 17 00:00:00 2001 From: marcelcosta Date: Fri, 13 Nov 2020 16:21:12 +0100 Subject: [PATCH] =?UTF-8?q?A=C3=B1adido=20curvas=20de=20superviviencia?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- invivos/app.R | 200 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 152 insertions(+), 48 deletions(-) diff --git a/invivos/app.R b/invivos/app.R index 3f65416..820db34 100755 --- a/invivos/app.R +++ b/invivos/app.R @@ -8,6 +8,8 @@ library(ggbeeswarm) library(gtools) library(gridExtra) source("../../funcions.R") +library(survminer) +library(survival) # Define UI for application ui <- fluidPage( @@ -34,6 +36,7 @@ ui <- fluidPage( 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")), + sliderInput("cutoff", "Cutoff para Survival", min=100, max=1500, step=50, value=750), checkboxInput("filter_stats","Filtrar Estadística") ), mainPanel( @@ -42,6 +45,7 @@ ui <- fluidPage( h3('Cinéticas'), plotOutput('cin_group'), plotOutput('cin_indiv'), + plotOutput('survival', height="800px"), h3('Estadística'), verbatimTextOutput('stats'), tableOutput('tab_stats') @@ -248,7 +252,6 @@ server <- function(input, output) { if ("ID.tumor" %in% colnames(table)){table<-rename(table, "ID tumor"=`ID.tumor`)} # table[table$ID.tumor == "R","0"]<-NA col_nodays<-c("ID", "Cage","Group", "ID.animal","ID animal", "ID.tumor", "ID tumor", "TS","DPV", "Absorbance") - print(grep(0, colnames(table)[!colnames(table) %in% col_nodays])) if (length(grep(0, colnames(table)[!colnames(table) %in% col_nodays])) == 0){ table["0"]<-0 } @@ -327,6 +330,54 @@ server <- function(input, output) { } } }) + + output$survival<-renderPlot({ + if (!is.null(input$file_analy) & !is.null(analysis$taula_def)){ + observeEvent(analysis$taula_def, {}) + table<-analysis$taula_def + if (input$vacc == "Sí"){ + g<-list() + for (side in c("L","R")){ + tableR<-filter(table, `ID tumor` == side) %>% filter(!is.na(Volume)) + endtime<-dcast(tableR %>% filter(Volume < cutoff), Cage+`ID animal`+`ID tumor`+Group~., value.var = "Timepoint", fun.aggregate = function(x){max(as.numeric(as.character(x)))}) %>% rename("end"=".") + endtime["Dead"]<-dcast(tableR, Cage+`ID animal`+`ID tumor`+Group~., value.var = "Volume", fun.aggregate = function(x){max(as.numeric(as.character(x)))}) %>% pull(".") > input$cutoff + table_tumor<<-endtime + + g[side]<-ggsurvplot(survfit(Surv(table_tumor$end, table_tumor$Dead) ~ table_tumor$Group, data=table_tumor), + pval = T, pval.method = T, + title = side, + # legend.labs = paste(c("< median", ">= median"), "MICA"), + ggtheme=theme_classic(base_size=15) + # conf.int = TRUE, + # Add risk table + # risk.table = TRUE, + # tables.height = 0.2, + # tables.theme = theme_cleantable(), + + # Color palettes. Use custom color: c("#E7B800", "#2E9FDF"), + # or brewer color (e.g.: "Dark2"), or ggsci color (e.g.: "jco") + # palette = c("#E7B800", "#2E9FDF") + ) + } + do.call(grid.arrange, g) + + }else{ + tableR<-table %>% filter(!is.na(Volume)) + endtime<-dcast(tableR %>% filter(Volume < cutoff), `ID animal`+Group~., value.var = "Timepoint", fun.aggregate = function(x){max(as.numeric(as.character(x)))}) %>% rename("end"=".") + endtime["Dead"]<-dcast(tableR, `ID animal`+Group~., value.var = "Volume", fun.aggregate = function(x){max(as.numeric(as.character(x)))}) %>% pull(".") > input$cutoff + table_tumor<<-endtime + + g<-ggsurvplot(survfit(Surv(table_tumor$end, table_tumor$Dead) ~ table_tumor$Group, data=table_tumor), + pval = T, pval.method = T, + # legend.labs = paste(c("< median", ">= median"), "MICA"), + ggtheme=theme_classic(base_size=15) + # palette = c("#E7B800", "#2E9FDF") + ) + g + } + } + }) + output$stats<-renderPrint({ stattest<-"dunn" oneside<-"" @@ -409,58 +460,106 @@ server <- function(input, output) { if (!is.null(input$file_analy) & !is.null(analysis$taula_def)){ table<-analysis$taula_def - print(head(table)) - - if (input$fig_id == "Cinética Grupo"){ - if (input$vacc == "Sí"){ - g<-ggplot(table, aes(as.numeric(as.character(Timepoint)), Volume, color=Group, group=Group))+ - 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"))~., scale="free_y")+ - theme_bw() - }else{ - g<-ggplot(table, aes(Timepoint, Volume, color=Group, group=Group))+ - theme_bw() + if (input$fig_id %in% c("Cinética Grupo", "Cinética Individual")){ + if (input$fig_id == "Cinética Grupo"){ + if (input$vacc == "Sí"){ + g<-ggplot(table, aes(as.numeric(as.character(Timepoint)), Volume, color=Group, group=Group))+ + 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"))~., scale="free_y")+ + theme_bw() + }else{ + g<-ggplot(table, aes(Timepoint, Volume, color=Group, group=Group))+ + theme_bw() + } + } + if (input$fig_id == "Cinética Individual"){ + if (input$vacc == "Sí"){ + g<-ggplot(table, aes(as.numeric(as.character(Timepoint)), Volume, color=Group, group=Group))+ + 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")+ + theme_bw() + }else{ + g<-ggplot(table, aes(Timepoint, Volume, color=Group, group=`ID animal`))+ + facet_wrap(.~Group)+ + theme_bw() + } + } + + g<-g+geom_errorbar(stat="summary", width=input$`errorbar-width`)+ + geom_line(stat="summary")+ + geom_point(stat="summary", size=input$`point-size`)+ + labs(x="Days after tumor challenge")+ + scale_y_continuous(expand = expansion(mult = c(0,0.05))) + + if (input$theme == "BW"){ + g<-g+theme_bw(base_size = input$`font-size`) + } + if (input$theme == "Classic"){ + g<-g+theme_classic(base_size = input$`font-size`) + } + if (input$theme == "Default"){ + g<-g+theme_gray(base_size = input$`font-size`) + } + # g<-g+theme(axis.text.x=element_text(angle=45, hjust=1)) + if (input$legend == F){ + g<-g+guides(color=FALSE, fill=FALSE) + } + if (input$colors != ""){ + v_col<-strsplit(input$colors, ",")[[1]] + g<-g+scale_color_manual(values=v_col)+ + scale_fill_manual(values=v_col) + } + }else{ + gg_color_hue <- function(n, l=65) { + hues <- seq(15, 375, length=n+1) + hcl(h=hues, l=l, c=100)[1:n] } - } - if (input$fig_id == "Cinética Individual"){ if (input$vacc == "Sí"){ - g<-ggplot(table, aes(as.numeric(as.character(Timepoint)), Volume, color=Group, group=Group))+ - 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")+ - theme_bw() + g<-list() + count<-1 + for (side in c("L","R")){ + tableR<-filter(table, `ID tumor` == side) %>% filter(!is.na(Volume)) + endtime<-dcast(tableR %>% filter(Volume < cutoff), Cage+`ID animal`+`ID tumor`+Group~., value.var = "Timepoint", fun.aggregate = function(x){max(as.numeric(as.character(x)))}) %>% rename("end"=".") + endtime["Dead"]<-dcast(tableR, Cage+`ID animal`+`ID tumor`+Group~., value.var = "Volume", fun.aggregate = function(x){max(as.numeric(as.character(x)))}) %>% pull(".") > input$cutoff + table_tumor<<-endtime + if (input$colors != ""){ + col<-input$colors + }else{ + col<-gg_color_hue(length(unique(endtime$Group))) + } + g[[count]]<-ggsurvplot(survfit(Surv(table_tumor$end, table_tumor$Dead) ~ table_tumor$Group, data=table_tumor), + pval = T, pval.method = T, + title = side, + # legend.labs = paste(c("< median", ">= median"), "MICA"), + ggtheme=theme_classic(base_size=input$`font-size`), + palette = col + ) + count<-count+1 + } + g_surv_vacc<-g + }else{ - g<-ggplot(table, aes(Timepoint, Volume, color=Group, group=`ID animal`))+ - facet_wrap(.~Group)+ - theme_bw() + if (input$colors != ""){ + col<-input$colors + }else{ + col<-gg_color_hue(length(unique(table_tumor$Group))) + } + g<-ggsurvplot(survfit(Surv(table_tumor$end, table_tumor$Dead) ~ table_tumor$Group, data=table_tumor), + pval = T, pval.method = T, + # legend.labs = paste(c("< median", ">= median"), "MICA"), + ggtheme=theme_classic(base_size=input$`font-size`), + palette = col + ) } } - g<-g+geom_errorbar(stat="summary", width=input$`errorbar-width`)+ - geom_line(stat="summary")+ - geom_point(stat="summary", size=input$`point-size`)+ - labs(x="Days after tumor challenge")+ - scale_y_continuous(expand = expansion(mult = c(0,0.05))) - - if (input$theme == "BW"){ - g<-g+theme_bw(base_size = input$`font-size`) - } - if (input$theme == "Classic"){ - g<-g+theme_classic(base_size = input$`font-size`) - } - if (input$theme == "Default"){ - g<-g+theme_gray(base_size = input$`font-size`) - } - # g<-g+theme(axis.text.x=element_text(angle=45, hjust=1)) - if (input$legend == F){ - g<-g+guides(color=FALSE, fill=FALSE) - } - if (input$colors != ""){ - v_col<-strsplit(input$colors, ",")[[1]] - g<-g+scale_color_manual(values=v_col)+ - scale_fill_manual(values=v_col) + if (input$vacc == "Sí" & input$fig_id == "Survival"){ + dades$plot<<-g_surv_vacc + arrange_ggsurvplots(g, nrow=2, ncol=1) + }else{ + dades$plot<<-g + g } - dades$plot<<-g - g } }, res=72) @@ -469,11 +568,16 @@ server <- function(input, output) { paste("Figura", ".png", sep="") }, content = function(file){ - print(file) # tempReport <- file.path(tempdir(), "elispots.Rmd") # file.copy("elispots.Rmd", tempReport, overwrite = TRUE) png(file, width = input$width, height=input$height, units = "px", res=720) - plot(dades$plot) + if (input$fig_id == "Survival"){ + if (input$vacc == "Sí"){ + arrange_ggsurvplots(dades$plot, nrow=2, ncol=1) + }else{ + arrange_ggsurvplots(list(dades$plot), ncol=1) + } + }else{plot(dades$plot)} dev.off() })