From 3d11ffbdbcae113ec83bfc09e134c7671b3abd0a Mon Sep 17 00:00:00 2001 From: Bryan Roessler Date: Mon, 30 Sep 2024 02:21:54 -0400 Subject: [PATCH] Add pre-calculated error bars to scatter plots --- .../apps/r/calculate_interaction_zscores.R | 117 +++++++++--------- 1 file changed, 61 insertions(+), 56 deletions(-) diff --git a/qhtcp-workflow/apps/r/calculate_interaction_zscores.R b/qhtcp-workflow/apps/r/calculate_interaction_zscores.R index fac84935..dfae2934 100644 --- a/qhtcp-workflow/apps/r/calculate_interaction_zscores.R +++ b/qhtcp-workflow/apps/r/calculate_interaction_zscores.R @@ -560,18 +560,30 @@ generate_scatter_plot <- function(plot, config) { # Add error bars if specified if (!is.null(config$error_bar) && config$error_bar && !is.null(config$y_var)) { - y_mean_col <- paste0("mean_", config$y_var) - y_sd_col <- paste0("sd_", config$y_var) - - plot <- plot + - geom_errorbar( - aes( - ymin = !!sym(y_mean_col) - !!sym(y_sd_col), - ymax = !!sym(y_mean_col) + !!sym(y_sd_col) - ), - alpha = 0.3, - linewidth = 0.5 - ) + if (!is.null(config$error_bar_params)) { + plot <- plot + + geom_errorbar( + aes( + ymin = config$error_bar_params$ymin, + ymax = config$error_bar_params$ymax + ), + alpha = 0.3, + linewidth = 0.5 + ) + } else { + y_mean_col <- paste0("mean_", config$y_var) + y_sd_col <- paste0("sd_", config$y_var) + + plot <- plot + + geom_errorbar( + aes( + ymin = !!sym(y_mean_col) - !!sym(y_sd_col), + ymax = !!sym(y_mean_col) + !!sym(y_sd_col) + ), + alpha = 0.3, + linewidth = 0.5 + ) + } } # Customize X-axis if specified @@ -675,9 +687,10 @@ generate_interaction_plot_configs <- function(df, limits_map = NULL, plot_type = df_filtered <- df %>% mutate(OrfRepCombined = if (plot_type == "reference") paste(OrfRep, Gene, num, sep = "_") else paste(OrfRep, Gene, sep = "_")) - plots <- list() + # Separate the plots into two groups: overall variables and delta comparisons + overall_plots <- list() + delta_plots <- list() - # Generate plots for overall variables (L, K, r, AUC) for (var in c("L", "K", "r", "AUC")) { y_limits <- limits_map[[var]] @@ -692,14 +705,12 @@ generate_interaction_plot_configs <- function(df, limits_map = NULL, plot_type = error_bar = TRUE, x_breaks = unique(df_filtered$conc_num_factor_factor), x_labels = as.character(unique(df_filtered$conc_num)), - grid_layout = list(ncol = 2, nrow = 2), position = "jitter", smooth = TRUE ) - plots <- append(plots, list(config)) + overall_plots <- append(overall_plots, list(config)) } - # Generate Delta comparison plots unique_groups <- df_filtered %>% select(all_of(group_vars)) %>% distinct() for (i in seq_len(nrow(unique_groups))) { @@ -713,13 +724,13 @@ generate_interaction_plot_configs <- function(df, limits_map = NULL, plot_type = for (var in c("Delta_L", "Delta_K", "Delta_r", "Delta_AUC")) { y_limits <- limits_map[[var]] y_span <- y_limits[2] - y_limits[1] - + # Error bars WT_sd_var <- paste0("WT_sd_", sub("Delta_", "", var)) WT_sd_value <- group_data[[WT_sd_var]][1] error_bar_ymin <- 0 - (2 * WT_sd_value) error_bar_ymax <- 0 + (2 * WT_sd_value) - + # Annotations Z_Shift_value <- round(group_data[[paste0("Z_Shift_", sub("Delta_", "", var))]][1], 2) Z_lm_value <- round(group_data[[paste0("Z_lm_", sub("Delta_", "", var))]][1], 2) @@ -752,14 +763,16 @@ generate_interaction_plot_configs <- function(df, limits_map = NULL, plot_type = smooth = TRUE, x_breaks = unique(group_data$conc_num_factor_factor), x_labels = as.character(unique(group_data$conc_num)), - ylim_vals = y_limits, - grid_layout = list(ncol = 4, nrow = 3) + ylim_vals = y_limits ) - plots <- append(plots, list(config)) + delta_plots <- append(delta_plots, list(config)) } } - return(list(grid_layout = list(ncol = 2, nrow = 2), plots = plots)) + return(list( + overall_plots = list(grid_layout = list(ncol = 2, nrow = 2), plots = overall_plots), + delta_plots = list(grid_layout = list(ncol = 4, nrow = 3), plots = delta_plots) + )) } generate_rank_plot_configs <- function(df, variables, is_lm = FALSE, adjust = FALSE, overlap_color = FALSE) { @@ -1046,7 +1059,6 @@ main <- function() { # Each plots list corresponds to a file l_vs_k_plot_configs <- list( - grid_layout = list(ncol = 1, nrow = 1), plots = list( list( df = df, @@ -1063,7 +1075,6 @@ main <- function() { ) frequency_delta_bg_plot_configs <- list( - grid_layout = list(ncol = 1, nrow = 1), plots = list( list( df = df_stats, @@ -1093,7 +1104,6 @@ main <- function() { ) above_threshold_plot_configs <- list( - grid_layout = list(ncol = 1, nrow = 1), plots = list( list( df = df_above_tolerance, @@ -1145,7 +1155,6 @@ main <- function() { ) l_outside_2sd_k_plot_configs <- list( - grid_layout = list(ncol = 1, nrow = 1), # Ensures it's compatible with generate_and_save_plots plots = list( list( df = df_na_l_outside_2sd_k_stats, @@ -1170,7 +1179,6 @@ main <- function() { ) delta_bg_outside_2sd_k_plot_configs <- list( - grid_layout = list(ncol = 1, nrow = 1), # Ensures it's compatible with generate_and_save_plots plots = list( list( df = df_na_l_outside_2sd_k_stats, @@ -1195,37 +1203,34 @@ main <- function() { ) message("Generating quality control plots in parallel") - - generate_and_save_plots(out_dir_qc, "L_vs_K_before_quality_control", l_vs_k_plot_configs) - quit() # # future::plan(future::multicore, workers = parallel::detectCores()) - # future::plan(future::multisession, workers = 3) # generate 3 plots in parallel + future::plan(future::multisession, workers = 3) # generate 3 plots in parallel - # plot_configs <- list( - # list(out_dir = out_dir_qc, filename = "L_vs_K_before_quality_control", - # plot_configs = l_vs_k_plot_configs), - # list(out_dir = out_dir_qc, filename = "frequency_delta_background", - # plot_configs = frequency_delta_bg_plot_configs), - # list(out_dir = out_dir_qc, filename = "L_vs_K_above_threshold", - # plot_configs = above_threshold_plot_configs), - # list(out_dir = out_dir_qc, filename = "plate_analysis", - # plot_configs = plate_analysis_plot_configs), - # list(out_dir = out_dir_qc, filename = "plate_analysis_boxplots", - # plot_configs = plate_analysis_boxplot_configs), - # list(out_dir = out_dir_qc, filename = "plate_analysis_no_zeros", - # plot_configs = plate_analysis_no_zeros_plot_configs), - # list(out_dir = out_dir_qc, filename = "plate_analysis_no_zeros_boxplots", - # plot_configs = plate_analysis_no_zeros_boxplot_configs), - # list(out_dir = out_dir_qc, filename = "L_vs_K_for_strains_2SD_outside_mean_K", - # plot_configs = l_outside_2sd_k_plot_configs), - # list(out_dir = out_dir_qc, filename = "delta_background_vs_K_for_strains_2sd_outside_mean_K", - # plot_configs = delta_bg_outside_2sd_k_plot_configs) - # ) + plot_configs <- list( + list(out_dir = out_dir_qc, filename = "L_vs_K_before_quality_control", + plot_configs = l_vs_k_plot_configs), + list(out_dir = out_dir_qc, filename = "frequency_delta_background", + plot_configs = frequency_delta_bg_plot_configs), + list(out_dir = out_dir_qc, filename = "L_vs_K_above_threshold", + plot_configs = above_threshold_plot_configs), + list(out_dir = out_dir_qc, filename = "plate_analysis", + plot_configs = plate_analysis_plot_configs), + list(out_dir = out_dir_qc, filename = "plate_analysis_boxplots", + plot_configs = plate_analysis_boxplot_configs), + list(out_dir = out_dir_qc, filename = "plate_analysis_no_zeros", + plot_configs = plate_analysis_no_zeros_plot_configs), + list(out_dir = out_dir_qc, filename = "plate_analysis_no_zeros_boxplots", + plot_configs = plate_analysis_no_zeros_boxplot_configs), + list(out_dir = out_dir_qc, filename = "L_vs_K_for_strains_2SD_outside_mean_K", + plot_configs = l_outside_2sd_k_plot_configs), + list(out_dir = out_dir_qc, filename = "delta_background_vs_K_for_strains_2sd_outside_mean_K", + plot_configs = delta_bg_outside_2sd_k_plot_configs) + ) - # # Generating quality control plots in parallel - # furrr::future_map(plot_configs, function(config) { - # generate_and_save_plots(config$out_dir, config$filename, config$plot_configs) - # }, .options = furrr_options(seed = TRUE)) + # Generating quality control plots in parallel + furrr::future_map(plot_configs, function(config) { + generate_and_save_plots(config$out_dir, config$filename, config$plot_configs) + }, .options = furrr_options(seed = TRUE)) # Process background strains bg_strains <- c("YDL227C")