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
#=============
source("boxplot_stat_function.R")
source("boxplot_function.R") # for sam
#source("boxplot_function.R") # for sam
source("plot_data.R")
#=============
@ -23,10 +23,10 @@ pdf(output_boxplot_stats, width=22, height=16)
#-------------
# NPA
#-------------
my_sample_npa = "NPA"
title_npa = "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")
plots_npa = doMyPlotsStats(lf_fp_npa)
@ -37,7 +37,7 @@ npa_plot = ggpubr::ggarrange(plotlist = plots_npa
, common.legend = T)
#npa_plot
npa_plot_annot = annotate_figure(npa_plot
, top = text_grob(my_sample_npa
, top = text_grob(title_npa
, color = "blue"
, face = "bold"
, size = 18)
@ -58,13 +58,14 @@ npa_plot_annot
# FIXME: error handling!
# 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
cat("\nPlotting boxplots with stats for:", my_sample_sam
cat("\nPlotting boxplots with stats for:", title_sam
, "\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
, align = "hv"
, ncol = 7
@ -72,7 +73,7 @@ sam_plot = ggpubr::ggarrange(plotlist = plots_sam
, common.legend = T)
#sam_plot
sam_plot_annot = annotate_figure(sam_plot
, top = text_grob(my_sample_sam
, top = text_grob(title_sam
, color = "blue"
, face = "bold"
, size = 18)
@ -91,10 +92,10 @@ sam_plot_annot
#-------------
# SERUM
#-------------
my_sample_serum = "SERUM"
title_serum = "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")
plots_serum = doMyPlotsStats(lf_fp_serum)
@ -105,7 +106,7 @@ serum_plot = ggpubr::ggarrange(plotlist = plots_serum
, common.legend = T)
#serum_plot
serum_plot_annot = annotate_figure(serum_plot
, top = text_grob(my_sample_serum
, top = text_grob(title_serum
, color = "blue"
, face = "bold"
, size = 18)

View file

@ -5,27 +5,30 @@ getwd()
############################################################
# TASK: boxplots with stats
############################################################
is.error <- function(x) inherits(x, "try-error")
doMyPlotsStats <- function(df) {
mediators = levels(as.factor(df$mediator))
mediators = levels(as.factor(df$mediator))
plots <- list()
for (i in mediators) {
cat("Creating plot for:", i, "\n")
single=df[df$mediator==i,]
plots <- list()
max_y = max(single$value, na.rm = T)
#cat("Plotting:", i, "max_y:", max_y, "\n")
for (i in mediators) {
cat("Plotting:", i, "\n")
single=df[df$mediator==i,]
max_y = max(single$value, na.rm = T)
cat("Plotting:", i, "max_y:", max_y, "\n")
#----------
# boxplot
#----------
p = ggplot(single)+ geom_boxplot(aes(x = timepoint
, y = value
, color = obesity
#, palette = c("#00BFC4", "#F8766D")
))+
scale_colour_manual(values=c("#00BFC4", "#F8766D")) +
#----------
# boxplot
#----------
p = ggplot(single)+ geom_boxplot(aes(x = timepoint
, y = value
, color = obesity
#, palette = c("#00BFC4", "#F8766D")
))+
scale_colour_manual(values=c("#00BFC4", "#F8766D")) +
theme(axis.text.x = element_text(size = 15)
, axis.text.y = element_text(size = 15
, angle = 0
@ -36,11 +39,14 @@ doMyPlotsStats <- function(df) {
, legend.position = "none"
, plot.subtitle = element_text(size = 20, hjust = 0.5)
, plot.title = element_text(size = 20, hjust = 0.5)) +
labs(title = i)
labs(title = i)
#--------
# stats
#---------
stats_state <- try({
cat('Calculating stats for:', i, "\n")
#--------
# stats
#---------
stat_df <- single %>%
group_by(timepoint, mediator) %>%
wilcox_test(value ~ obesity, paired = F) %>%
@ -49,17 +55,24 @@ doMyPlotsStats <- function(df) {
stat_df <- stat_df %>%
add_xy_position(x = "timepoint", dodge = 0.8)
p2 = p + stat_pvalue_manual(stat_df
#, y.position = max_y
, label = "{p_format} {p.signif}"
, hide.ns = T
, tip.length = 0)+
scale_y_continuous(expand = expansion(mult = c(0.05, 0.25)))
plots[[i]] <- p2
}, 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
, label = "{p_format} {p.signif}"
, hide.ns = T
, tip.length = 0)+
scale_y_continuous(expand = expansion(mult = c(0.05, 0.25)))
}
return(plots)
plots[[i]] <- p
}
return(plots)
}
###############################################################

27
boxplot_stat_log.R Normal file → Executable file
View file

@ -7,12 +7,13 @@ getwd()
# Input
#=============
source("boxplot_stat_function.R")
source("boxplot_function.R")
source("plot_data.R")
#=============
# 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
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]
#------------------------------------------
my_sample_npa = "NPA"
title_npa = "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")
plots_npa = doMyPlotsStats(lf_fp_npa)
@ -63,10 +64,10 @@ npa_plot = ggpubr::ggarrange(plotlist = plots_npa
, common.legend = T)
#npa_plot
npa_plot_annot = annotate_figure(npa_plot
, top = text_grob(my_sample_npa
, top = text_grob(title_npa
, color = "blue"
, face = "bold"
, size = 14)
, size = 18)
, bottom = text_grob(paste0("Mosaic data\nFlu positive adults (n=", fp_npa, ")")
, color = "blue"
, hjust = 1
@ -90,10 +91,10 @@ head(lf_fp_sam$value)
lf_fp_sam$value = log10(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
cat("\nPlotting boxplots with stats for:", my_sample_sam
cat("\nPlotting boxplots with stats for:", title_sam
, "\n========================================================\n")
plots_sam = doMyPlots(lf_fp_sam)
@ -104,10 +105,10 @@ sam_plot = ggpubr::ggarrange(plotlist = plots_sam
, common.legend = T)
#sam_plot
sam_plot_annot = annotate_figure(sam_plot
, top = text_grob(my_sample_sam
, top = text_grob(title_sam
, color = "blue"
, face = "bold"
, size = 14)
, size = 18)
, bottom = text_grob(paste0("Mosaic data\nFlu positive adults (n=", fp_sam, ")")
, color = "blue"
, hjust = 1
@ -131,10 +132,10 @@ head(lf_fp_serum$value)
lf_fp_serum$value = log10(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
cat("\nPlotting boxplots with stats for:", my_sample_serum
cat("\nPlotting boxplots with stats for:", title_serum
, "\n========================================================\n")
plots_serum = doMyPlotsStats(lf_fp_serum)
@ -145,10 +146,10 @@ serum_plot = ggpubr::ggarrange(plotlist = plots_serum
, common.legend = T)
#serum_plot
serum_plot_annot = annotate_figure(serum_plot
, top = text_grob(my_sample_serum
, top = text_grob(title_serum
, color = "blue"
, face = "bold"
, size = 14)
, size = 18)
, bottom = text_grob(paste0("Mosaic data\nFlu positive adults (n=", fp_serum, ")")
, color = "blue"
, hjust = 1