Quellcode durchsuchen

Refactor generate_interaction_plot_configs() to use more precomputed values

Bryan Roessler vor 7 Monaten
Ursprung
Commit
53d4695428
1 geänderte Dateien mit 38 neuen und 40 gelöschten Zeilen
  1. 38 40
      qhtcp-workflow/apps/r/calculate_interaction_zscores.R

+ 38 - 40
qhtcp-workflow/apps/r/calculate_interaction_zscores.R

@@ -435,79 +435,77 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la
 generate_interaction_plot_configs <- function(df, variables) {
   configs <- list()
 
-  # Predefine y-limits and annotation y-values for each variable
-  variable_properties <- list(
-    "L" = list(ylim = c(-65, 65), annotations_y = c(45, 25, -25, -35, -45)),
-    "K" = list(ylim = c(-65, 65), annotations_y = c(45, 25, -25, -35, -45)),
-    "r" = list(ylim = c(-0.65, 0.65), annotations_y = c(0.45, 0.25, -0.25, -0.35, -0.45)),
-    "AUC" = list(ylim = c(-6500, 6500), annotations_y = c(4500, 2500, -2500, -3500, -4500))
-  )
+  # 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))
 
   for (variable in variables) {
-    props <- variable_properties[[variable]]
+    # Dynamically generate the names of the columns
+    var_info <- list(
+      ylim = limits_map[[variable]],
+      lm_model = df[[paste0("lm_", variable)]][[1]],    # Access the precomputed linear model
+      sd_col = paste0("WT_sd_", variable),
+      delta_var = paste0("Delta_", variable),
+      z_shift = paste0("Z_Shift_", variable),
+      z_lm = paste0("Z_lm_", variable)
+    )
     
-    # Dynamically generate column names
-    wt_sd_col <- paste0("WT_sd_", variable)
-    delta_var <- paste0("Delta_", variable)
-    z_shift <- paste0("Z_Shift_", variable)
-    z_lm <- paste0("Z_lm_", variable)
-    lm_score <- paste0("lm_Score_", variable)  # Precomputed lm score
-    r_squared <- paste0("r_squared_", variable)  # Precomputed R^2
-
-    # Create annotation list
-    annotation_labels <- c("ZShift =", "lm ZScore =", "NG =", "DB =", "SM =")
-    annotations <- lapply(seq_along(annotation_labels), function(i) {
-      list(x = 1, y = props$annotations_y[i], label = paste(annotation_labels[i], round(df[[c(z_shift, z_lm, "NG", "DB", "SM")[i]]], 2)))
-    })
+    # Extract the precomputed linear model coefficients
+    lm_line <- list(
+      intercept = coef(var_info$lm_model)[1],
+      slope = coef(var_info$lm_model)[2]
+    )
+
+    # Set annotations dynamically for ZShift, Z lm Score, NG, DB, SM
+    base_y <- if (variable == "L" || variable == "K") 45 else if (variable == "r") 0.45 else 4500
+    annotations <- list(
+      list(x = 1, y = base_y, label = paste("ZShift =", round(df[[var_info$z_shift]], 2))),
+      list(x = 1, y = base_y - 20, label = paste("lm ZScore =", round(df[[var_info$z_lm]], 2))),
+      list(x = 1, y = base_y - 70, label = paste("NG =", df$NG)),
+      list(x = 1, y = base_y - 80, label = paste("DB =", df$DB)),
+      list(x = 1, y = base_y - 90, label = paste("SM =", df$SM))
+    )
 
-    # Create scatter plot configuration using precomputed lm scores
-    scatter_config <- list(
+    # Add scatter plot configuration for this variable
+    configs[[length(configs) + 1]] <- list(
       df = df,
       x_var = "conc_num_factor",
-      y_var = delta_var,
+      y_var = var_info$delta_var,
       plot_type = "scatter",
       title = sprintf("%s      %s", df$OrfRep[1], df$Gene[1]),
-      ylim_vals = props$ylim,
+      ylim_vals = var_info$ylim,
       annotations = annotations,
+      lm_line = lm_line,  # Precomputed linear model
       error_bar = list(
-        ymin = 0 - (2 * df[[wt_sd_col]][1]),
-        ymax = 0 + (2 * df[[wt_sd_col]][1])
+        ymin = 0 - (2 * df[[var_info$sd_col]][1]),
+        ymax = 0 + (2 * df[[var_info$sd_col]][1])
       ),
       x_breaks = unique(df$conc_num_factor),
       x_labels = unique(as.character(df$conc_num)),
       x_label = unique(df$Drug[1]),
       shape = 3,
       size = 0.6,
-      position = "jitter",
-      lm_line = list(
-        intercept = coef(lm(df[[delta_var]] ~ df$conc_num_factor))[1],  # Intercept from lm model
-        slope = coef(lm(df[[delta_var]] ~ df$conc_num_factor))[2]      # Slope from lm model
-      )
+      position = "jitter"
     )
 
-    # Create box plot configuration for this variable
-    box_config <- list(
+    # Add box plot configuration for this variable
+    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 = props$ylim,
+      ylim_vals = var_info$ylim,
       annotations = annotations,
-      error_bar = FALSE,
+      error_bar = FALSE,  # Boxplots typically don't need error bars
       x_breaks = unique(df$conc_num_factor),
       x_labels = unique(as.character(df$conc_num)),
       x_label = unique(df$Drug[1])
     )
-
-    # Append both scatter and box plot configurations
-    configs <- append(configs, list(scatter_config, box_config))
   }
 
   return(configs)
 }
 
-
 # Adjust missing values and calculate ranks
 adjust_missing_and_rank <- function(df, variables) {