added try catch for error handling

This commit is contained in:
Tanushree Tunstall 2020-11-02 16:26:22 +00:00
parent 9c141326aa
commit 8033c1785f
3 changed files with 69 additions and 54 deletions

View file

@ -7,7 +7,7 @@ getwd()
# Input # Input
#============= #=============
source("boxplot_stat_function.R") source("boxplot_stat_function.R")
source("boxplot_function.R") # for sam #source("boxplot_function.R") # for sam
source("plot_data.R") source("plot_data.R")
#============= #=============
@ -23,10 +23,10 @@ pdf(output_boxplot_stats, width=22, height=16)
#------------- #-------------
# NPA # NPA
#------------- #-------------
my_sample_npa = "NPA" title_npa = "NPA"
fp_npa = length(unique(lf_fp_npa$mosaic)); fp_npa fp_npa = length(unique(lf_fp_npa$mosaic)); fp_npa
cat("\nPlotting boxplots with stats for:", my_sample_npa cat("\nPlotting boxplots with stats for:", title_npa
, "\n========================================================\n") , "\n========================================================\n")
plots_npa = doMyPlotsStats(lf_fp_npa) plots_npa = doMyPlotsStats(lf_fp_npa)
@ -37,7 +37,7 @@ npa_plot = ggpubr::ggarrange(plotlist = plots_npa
, common.legend = T) , common.legend = T)
#npa_plot #npa_plot
npa_plot_annot = annotate_figure(npa_plot npa_plot_annot = annotate_figure(npa_plot
, top = text_grob(my_sample_npa , top = text_grob(title_npa
, color = "blue" , color = "blue"
, face = "bold" , face = "bold"
, size = 18) , size = 18)
@ -58,13 +58,14 @@ npa_plot_annot
# FIXME: error handling! # FIXME: error handling!
# For now, just calling plotting function without stats # For now, just calling plotting function without stats
#------------- #-------------
my_sample_sam = "SAM" title_sam = "SAM"
fp_sam = length(unique(lf_fp_sam$mosaic)); fp_sam fp_sam = length(unique(lf_fp_sam$mosaic)); fp_sam
cat("\nPlotting boxplots with stats for:", my_sample_sam cat("\nPlotting boxplots with stats for:", title_sam
, "\n========================================================\n") , "\n========================================================\n")
plots_sam = doMyPlots(lf_fp_sam) #plots_sam = doMyPlots(lf_fp_sam)
plots_sam = doMyPlotsStats(lf_fp_sam)
sam_plot = ggpubr::ggarrange(plotlist = plots_sam sam_plot = ggpubr::ggarrange(plotlist = plots_sam
, align = "hv" , align = "hv"
, ncol = 7 , ncol = 7
@ -72,7 +73,7 @@ sam_plot = ggpubr::ggarrange(plotlist = plots_sam
, common.legend = T) , common.legend = T)
#sam_plot #sam_plot
sam_plot_annot = annotate_figure(sam_plot sam_plot_annot = annotate_figure(sam_plot
, top = text_grob(my_sample_sam , top = text_grob(title_sam
, color = "blue" , color = "blue"
, face = "bold" , face = "bold"
, size = 18) , size = 18)
@ -91,10 +92,10 @@ sam_plot_annot
#------------- #-------------
# SERUM # SERUM
#------------- #-------------
my_sample_serum = "SERUM" title_serum = "SERUM"
fp_serum = length(unique(lf_fp_serum$mosaic)); fp_serum fp_serum = length(unique(lf_fp_serum$mosaic)); fp_serum
cat("\nPlotting boxplots with stats for:", my_sample_serum cat("\nPlotting boxplots with stats for:", title_serum
, "\n========================================================\n") , "\n========================================================\n")
plots_serum = doMyPlotsStats(lf_fp_serum) plots_serum = doMyPlotsStats(lf_fp_serum)
@ -105,7 +106,7 @@ serum_plot = ggpubr::ggarrange(plotlist = plots_serum
, common.legend = T) , common.legend = T)
#serum_plot #serum_plot
serum_plot_annot = annotate_figure(serum_plot serum_plot_annot = annotate_figure(serum_plot
, top = text_grob(my_sample_serum , top = text_grob(title_serum
, color = "blue" , color = "blue"
, face = "bold" , face = "bold"
, size = 18) , size = 18)

View file

@ -5,17 +5,20 @@ getwd()
############################################################ ############################################################
# TASK: boxplots with stats # TASK: boxplots with stats
############################################################ ############################################################
is.error <- function(x) inherits(x, "try-error")
doMyPlotsStats <- function(df) { doMyPlotsStats <- function(df) {
mediators = levels(as.factor(df$mediator)) mediators = levels(as.factor(df$mediator))
plots <- list() plots <- list()
for (i in mediators) { for (i in mediators) {
cat("Plotting:", i, "\n") cat("Creating plot for:", i, "\n")
single=df[df$mediator==i,] single=df[df$mediator==i,]
max_y = max(single$value, na.rm = T) max_y = max(single$value, na.rm = T)
cat("Plotting:", i, "max_y:", max_y, "\n") #cat("Plotting:", i, "max_y:", max_y, "\n")
#---------- #----------
# boxplot # boxplot
@ -41,6 +44,9 @@ doMyPlotsStats <- function(df) {
#-------- #--------
# stats # stats
#--------- #---------
stats_state <- try({
cat('Calculating stats for:', i, "\n")
stat_df <- single %>% stat_df <- single %>%
group_by(timepoint, mediator) %>% group_by(timepoint, mediator) %>%
wilcox_test(value ~ obesity, paired = F) %>% wilcox_test(value ~ obesity, paired = F) %>%
@ -49,13 +55,20 @@ doMyPlotsStats <- function(df) {
stat_df <- stat_df %>% stat_df <- stat_df %>%
add_xy_position(x = "timepoint", dodge = 0.8) add_xy_position(x = "timepoint", dodge = 0.8)
p2 = p + stat_pvalue_manual(stat_df }, silent=TRUE)
#print(stats_state)
if ( is.error(stats_state)) {cat("⚠️ Stats ERROR:", i, "\n")}
if ( ! is.error(stats_state)) {
cat('Adding stats to plot for:', i, "\n")
p = p + stat_pvalue_manual(stat_df
#, y.position = max_y #, y.position = max_y
, label = "{p_format} {p.signif}" , label = "{p_format} {p.signif}"
, hide.ns = T , hide.ns = T
, tip.length = 0)+ , tip.length = 0)+
scale_y_continuous(expand = expansion(mult = c(0.05, 0.25))) scale_y_continuous(expand = expansion(mult = c(0.05, 0.25)))
plots[[i]] <- p2 }
plots[[i]] <- p
} }
return(plots) return(plots)

27
boxplot_stat_log.R Normal file → Executable file
View file

@ -7,12 +7,13 @@ getwd()
# Input # Input
#============= #=============
source("boxplot_stat_function.R") source("boxplot_stat_function.R")
source("boxplot_function.R")
source("plot_data.R") source("plot_data.R")
#============= #=============
# Output # Output
#============= #=============
outfile_bp = paste0("boxplots_stats_npa_serum_LOG", ".pdf") outfile_bp = paste0("boxplots_stats_all_LOG", ".pdf")
output_boxplot_stats = paste0(outdir_plots, outfile_bp); output_boxplot_stats output_boxplot_stats = paste0(outdir_plots, outfile_bp); output_boxplot_stats
pdf(output_boxplot_stats, width=22, height=16) pdf(output_boxplot_stats, width=22, height=16)
############################################################### ###############################################################
@ -49,10 +50,10 @@ vl_temp_fix2 = lf_fp_npa[lf_fp_npa$mediator=="vl_pfu_ul",]
vl_temp_fix2$value[1:20] vl_temp_fix2$value[1:20]
#------------------------------------------ #------------------------------------------
my_sample_npa = "NPA" title_npa = "NPA"
fp_npa = length(unique(lf_fp_npa$mosaic)); fp_npa fp_npa = length(unique(lf_fp_npa$mosaic)); fp_npa
cat("\nPlotting boxplots with stats for:", my_sample_npa cat("\nPlotting boxplots with stats for:", title_npa
, "\n========================================================\n") , "\n========================================================\n")
plots_npa = doMyPlotsStats(lf_fp_npa) plots_npa = doMyPlotsStats(lf_fp_npa)
@ -63,10 +64,10 @@ npa_plot = ggpubr::ggarrange(plotlist = plots_npa
, common.legend = T) , common.legend = T)
#npa_plot #npa_plot
npa_plot_annot = annotate_figure(npa_plot npa_plot_annot = annotate_figure(npa_plot
, top = text_grob(my_sample_npa , top = text_grob(title_npa
, color = "blue" , color = "blue"
, face = "bold" , face = "bold"
, size = 14) , size = 18)
, bottom = text_grob(paste0("Mosaic data\nFlu positive adults (n=", fp_npa, ")") , bottom = text_grob(paste0("Mosaic data\nFlu positive adults (n=", fp_npa, ")")
, color = "blue" , color = "blue"
, hjust = 1 , hjust = 1
@ -90,10 +91,10 @@ head(lf_fp_sam$value)
lf_fp_sam$value = log10(lf_fp_sam$value) lf_fp_sam$value = log10(lf_fp_sam$value)
head(lf_fp_sam$value) head(lf_fp_sam$value)
my_sample_sam = "SAM" title_sam = "SAM"
fp_sam = length(unique(lf_fp_sam$mosaic)); fp_sam fp_sam = length(unique(lf_fp_sam$mosaic)); fp_sam
cat("\nPlotting boxplots with stats for:", my_sample_sam cat("\nPlotting boxplots with stats for:", title_sam
, "\n========================================================\n") , "\n========================================================\n")
plots_sam = doMyPlots(lf_fp_sam) plots_sam = doMyPlots(lf_fp_sam)
@ -104,10 +105,10 @@ sam_plot = ggpubr::ggarrange(plotlist = plots_sam
, common.legend = T) , common.legend = T)
#sam_plot #sam_plot
sam_plot_annot = annotate_figure(sam_plot sam_plot_annot = annotate_figure(sam_plot
, top = text_grob(my_sample_sam , top = text_grob(title_sam
, color = "blue" , color = "blue"
, face = "bold" , face = "bold"
, size = 14) , size = 18)
, bottom = text_grob(paste0("Mosaic data\nFlu positive adults (n=", fp_sam, ")") , bottom = text_grob(paste0("Mosaic data\nFlu positive adults (n=", fp_sam, ")")
, color = "blue" , color = "blue"
, hjust = 1 , hjust = 1
@ -131,10 +132,10 @@ head(lf_fp_serum$value)
lf_fp_serum$value = log10(lf_fp_serum$value) lf_fp_serum$value = log10(lf_fp_serum$value)
head(lf_fp_serum$value) head(lf_fp_serum$value)
my_sample_serum = "serum" title_serum = "SERUM"
fp_serum = length(unique(lf_fp_serum$mosaic)); fp_serum fp_serum = length(unique(lf_fp_serum$mosaic)); fp_serum
cat("\nPlotting boxplots with stats for:", my_sample_serum cat("\nPlotting boxplots with stats for:", title_serum
, "\n========================================================\n") , "\n========================================================\n")
plots_serum = doMyPlotsStats(lf_fp_serum) plots_serum = doMyPlotsStats(lf_fp_serum)
@ -145,10 +146,10 @@ serum_plot = ggpubr::ggarrange(plotlist = plots_serum
, common.legend = T) , common.legend = T)
#serum_plot #serum_plot
serum_plot_annot = annotate_figure(serum_plot serum_plot_annot = annotate_figure(serum_plot
, top = text_grob(my_sample_serum , top = text_grob(title_serum
, color = "blue" , color = "blue"
, face = "bold" , face = "bold"
, size = 14) , size = 18)
, bottom = text_grob(paste0("Mosaic data\nFlu positive adults (n=", fp_serum, ")") , bottom = text_grob(paste0("Mosaic data\nFlu positive adults (n=", fp_serum, ")")
, color = "blue" , color = "blue"
, hjust = 1 , hjust = 1