From 6c1318296137146e4e6082aff0eb8c0ca1f506d6 Mon Sep 17 00:00:00 2001 From: Bryan Roessler Date: Sun, 15 Sep 2024 12:48:36 -0400 Subject: [PATCH] Simplify annotation code --- .../apps/r/calculate_interaction_zscores.R | 99 +++++++++---------- 1 file changed, 47 insertions(+), 52 deletions(-) diff --git a/qhtcp-workflow/apps/r/calculate_interaction_zscores.R b/qhtcp-workflow/apps/r/calculate_interaction_zscores.R index 6ff188b2..20ca67ee 100644 --- a/qhtcp-workflow/apps/r/calculate_interaction_zscores.R +++ b/qhtcp-workflow/apps/r/calculate_interaction_zscores.R @@ -619,109 +619,104 @@ adjust_missing_and_rank <- function(df, variables) { generate_interaction_plot_configs <- function(df, variables) { configs <- list() - - # Define common y-limits and other attributes for each variable dynamically - limits_map <- list(L = c(-65, 65), K = c(-65, 65), r = c(-0.65, 0.65), AUC = c(-6500, 6500)) - - # Define annotation positions based on the variable being plotted - annotation_positions <- list( - L = list(Z_Shift_L = 45, lm_ZScore = 25, NG = -25, DB = -35, SM = -45), - K = list(Z_Shift_K = 45, lm_ZScore = 25, NG = -25, DB = -35, SM = -45), - r = list(Z_Shift_r = 0.45, lm_ZScore = 0.25, NG = -0.25, DB = -0.35, SM = -0.45), - AUC = list(Z_Shift_AUC = 4500, lm_ZScore = 2500, NG = -2500, DB = -3500, SM = -4500) + + # Define common y-limits for each variable + limits_map <- list( + L = c(-65, 65), + K = c(-65, 65), + r = c(-0.65, 0.65), + AUC = c(-6500, 6500) ) - - # Define which annotations to include for each plot + + # Define annotation positions and labels + annotation_positions <- list( + Z_Shift = 45, + Z_lm = 25, + NG = -25, + DB = -35, + SM = -45 + ) + + # Define functions to generate annotation labels annotation_labels <- list( - ZShift = function(df, var) { + Z_Shift = function(df, var) { val <- df[[paste0("Z_Shift_", var)]] - if (is.numeric(val)) { - paste("ZShift =", round(val, 2)) - } else { - paste("ZShift =", val) - } + paste("ZShift =", round(val, 2)) }, - lm_ZScore = function(df, var) { + Z_lm = function(df, var) { val <- df[[paste0("Z_lm_", var)]] - if (is.numeric(val)) { - paste("lm ZScore =", round(val, 2)) - } else { - paste("lm ZScore =", val) - } + paste("lm ZScore =", round(val, 2)) }, NG = function(df, var) paste("NG =", df$NG), DB = function(df, var) paste("DB =", df$DB), SM = function(df, var) paste("SM =", df$SM) ) - + for (variable in variables) { - # Dynamically generate the names of the columns - var_info <- list( - ylim = limits_map[[variable]], - sd_col = paste0("WT_sd_", variable) - ) - - # Extract the precomputed linear model coefficients + # Get y-limits for the variable + ylim_vals <- limits_map[[variable]] + + # Extract precomputed linear model coefficients lm_line <- list( intercept = df[[paste0("lm_intercept_", variable)]], slope = df[[paste0("lm_slope_", variable)]] ) - - annotations <- lapply(names(annotation_positions[[variable]]), function(annotation_name) { - message("Processing annotation: ", annotation_name, " for variable: ", variable) - y_pos <- annotation_positions[[variable]][[annotation_name]] - - # Check if the annotation_name exists in annotation_labels - if (!is.null(annotation_labels[[annotation_name]])) { - label <- annotation_labels[[annotation_name]](df, variable) + + # Generate annotations + annotations <- lapply(names(annotation_positions), function(annotation_name) { + y_pos <- annotation_positions[[annotation_name]] + label_func <- annotation_labels[[annotation_name]] + if (!is.null(label_func)) { + label <- label_func(df, variable) list(x = 1, y = y_pos, label = label) } else { message(paste("Warning: No annotation function found for", annotation_name)) NULL } }) - - # Filter out any NULL annotations + + # Remove NULL annotations annotations <- Filter(Negate(is.null), annotations) - - # Add scatter plot configuration for this variable + + # Create scatter plot config configs[[length(configs) + 1]] <- list( df = df, x_var = "conc_num_factor", y_var = variable, plot_type = "scatter", title = sprintf("%s %s", df$OrfRep[1], df$Gene[1]), - ylim_vals = var_info$ylim, + ylim_vals = ylim_vals, annotations = annotations, - lm_line = lm_line, # Precomputed linear model + lm_line = lm_line, error_bar = TRUE, x_breaks = unique(df$conc_num_factor), x_labels = unique(as.character(df$conc_num)), x_label = unique(df$Drug[1]), position = "jitter", - coord_cartesian = c(0, max(var_info$ylim)) # You can customize this per plot as needed + coord_cartesian = c(min(ylim_vals), max(ylim_vals)) ) - - # Add box plot configuration for this variable + + # Create box plot config configs[[length(configs) + 1]] <- list( df = df, x_var = "conc_num_factor", y_var = variable, plot_type = "box", title = sprintf("%s %s (Boxplot)", df$OrfRep[1], df$Gene[1]), - ylim_vals = var_info$ylim, + ylim_vals = ylim_vals, annotations = annotations, error_bar = FALSE, x_breaks = unique(df$conc_num_factor), x_labels = unique(as.character(df$conc_num)), x_label = unique(df$Drug[1]), - coord_cartesian = c(0, max(var_info$ylim)) # Customize this as needed + coord_cartesian = c(min(ylim_vals), max(ylim_vals)) ) } - + return(configs) } + generate_rank_plot_configs <- function(df, rank_var, zscore_var, var, is_lm = FALSE) { configs <- list()