Quellcode durchsuchen

Generalize generate_and_save_plots() further

Bryan Roessler vor 8 Monaten
Ursprung
Commit
e3bc5a2792
1 geänderte Dateien mit 49 neuen und 28 gelöschten Zeilen
  1. 49 28
      qhtcp-workflow/apps/r/calculate_interaction_zscores.R

+ 49 - 28
qhtcp-workflow/apps/r/calculate_interaction_zscores.R

@@ -306,6 +306,7 @@ calculate_interaction_scores <- function(df, max_conc, variables, group_vars = c
       Z_lm_AUC = (lm_Score_AUC - mean(lm_Score_AUC, na.rm = TRUE)) / sd(lm_Score_AUC, na.rm = TRUE)
     )
 
+  # Declare column order for output
   calculations <- calculations %>%
     select("OrfRep", "Gene", "num", "conc_num", "conc_num_factor",
       "mean_L", "mean_K", "mean_r", "mean_AUC",
@@ -320,7 +321,7 @@ calculate_interaction_scores <- function(df, max_conc, variables, group_vars = c
       "NG", "SM", "DB") %>%
     ungroup()
 
-  # Arrange results by Z_lm_L and NG
+  # Also arrange results by Z_lm_L and NG
   interactions <- interactions %>%
     select("OrfRep", "Gene", "num", "Raw_Shift_L", "Raw_Shift_K",  "Raw_Shift_AUC", "Raw_Shift_r",
       "Z_Shift_L", "Z_Shift_K", "Z_Shift_r", "Z_Shift_AUC",
@@ -339,23 +340,41 @@ calculate_interaction_scores <- function(df, max_conc, variables, group_vars = c
 
 generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_layout = NULL) {
 
-  message("Generating html and pdf plots for: ", file_name)
+  message("Generating html and pdf plots for: ", file_name, ".pdf|html")
 
   plots <- lapply(plot_configs, function(config) {
+
+    # Log configuration details
+    message("title: ", config$title)
+    message("plot_type: ", config$plot_type)
+    message("x_var: ", config$x_var)
+    message("y_var: ", config$y_var)
+    message("error_bar: ", config$error_bar)
+
     df <- config$df
 
-    # Check if y_var is NULL and adjust the aes mapping
+    # Build the aes mapping depending on whether y_var is present
     aes_mapping <- if (is.null(config$y_var)) {
       aes(x = !!sym(config$x_var), color = as.factor(!!sym(config$color_var)))
     } else {
       aes(x = !!sym(config$x_var), y = !!sym(config$y_var), color = as.factor(!!sym(config$color_var)))
     }
-    
+
+    # Initialize the plot with ggplot
     plot <- ggplot(df, aes_mapping)
-    
-    # Handle plot types like "rank", "correlation", and default scatter/box/density
-    if (config$plot_type == "rank") {
+
+    # Handle plot types explicitly
+    if (config$plot_type == "scatter") {
+      plot <- plot + geom_point(shape = 3)
+
+      # Add geom_smooth only if specified
+      if (!is.null(config$add_smooth) && config$add_smooth) {
+        plot <- plot + geom_smooth(method = "lm", se = FALSE)
+      }
+
+    } else if (config$plot_type == "rank") {
       plot <- plot + geom_point(size = 0.1, shape = 3)
+
       if (!is.null(config$sd_band)) {
         for (i in seq_len(config$sd_band)) {
           plot <- plot +
@@ -364,32 +383,31 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la
             geom_hline(yintercept = c(-i, i), color = "gray")
         }
       }
+
       if (!is.null(config$enhancer_label)) {
-        plot <- plot + annotate("text", x = config$enhancer_label$x, y = config$enhancer_label$y,
-          label = config$enhancer_label$label) +
+        plot <- plot + annotate("text", x = config$enhancer_label$x, y = config$enhancer_label$y, label = config$enhancer_label$label) +
           annotate("text", x = config$suppressor_label$x, y = config$suppressor_label$y, label = config$suppressor_label$label)
       }
     } else if (config$plot_type == "correlation") {
-      plot <- plot + geom_point(shape = 3, color = "gray70") + geom_smooth(method = "lm", color = "tomato3") +
+      plot <- plot + geom_point(shape = 3, color = "gray70") +
+        geom_smooth(method = "lm", color = "tomato3") +
         annotate("text", x = 0, y = 0, label = config$correlation_text)
+    } else if (config$plot_type == "box") {
+      plot <- plot + geom_boxplot()
+    } else if (config$plot_type == "density") {
+      plot <- plot + geom_density()
+    } else if (config$plot_type == "bar") {
+      plot <- plot + geom_bar()
     } else {
-      # Adjust based on plot types that may or may not need y_var
-      if (config$plot_type == "box") {
-        plot <- plot + geom_boxplot()
-      } else if (config$plot_type == "density") {
-        plot <- plot + geom_density()
-      } else if (config$plot_type == "bar") {
-        plot <- plot + geom_bar()
-      } else {
-        plot <- plot + geom_point(shape = 3) + geom_smooth(method = "lm", se = FALSE)
-      }
+      plot <- plot + geom_point(shape = 3) + geom_smooth(method = "lm", se = FALSE)
     }
 
-    # Add error bars for "delta_bg" or general cases
+    # Handle error bars if needed
     if (!is.null(config$error_bar) && config$error_bar) {
       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),
+      plot <- plot + geom_errorbar(aes(
+        ymin = !!sym(y_mean_col) - !!sym(y_sd_col),
         ymax = !!sym(y_mean_col) + !!sym(y_sd_col)), width = 0.1) +
         geom_point(aes(y = !!sym(y_mean_col)), size = 0.6)
     }
@@ -399,27 +417,30 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la
       plot <- plot + coord_cartesian(ylim = config$ylim_vals)
     }
 
-    # Apply labels, titles, and legends
+    # Apply titles, labels, and legends
     plot <- plot + ggtitle(config$title) +
       theme_publication(legend_position = if (!is.null(config$legend_position)) config$legend_position else "bottom") +
       xlab(config$x_label %||% "") + ylab(config$y_label %||% "")
 
-    # Add annotations if available
+    # Add any annotations
     if (!is.null(config$annotations)) {
-      plot <- plot + geom_text(aes(x = config$annotations$x, y = config$annotations$y, label = config$annotations$label))
+      for (annotation in config$annotations) {
+        plot <- plot + geom_text(aes(x = annotation$x, y = annotation$y, label = annotation$label))
+      }
     }
 
     return(plot)
   })
 
-  # Save the plots
+  # Save the plots as PDF
   pdf(file.path(output_dir, paste0(file_name, ".pdf")), width = 14, height = 9)
   lapply(plots, print)
   dev.off()
 
+  # Convert ggplot to plotly for interactive HTML output
   plotly_plots <- lapply(plots, function(plot) suppressWarnings(ggplotly(plot) %>% layout(legend = list(orientation = "h"))))
 
-  # Handle grid layout
+  # Combine plots in grid layout if applicable
   combined_plot <- subplot(plotly_plots, nrows = if (!is.null(grid_layout)) grid_layout$nrow else length(plots), margin = 0.05)
   saveWidget(combined_plot, file = file.path(output_dir, paste0(file_name, ".html")), selfcontained = TRUE)
 }
@@ -996,7 +1017,7 @@ main <- function() {
         group_by(across(all_of(group_vars))) %>%
           filter(!is.na(Z_lm_L) | !is.na(Avg_Zscore_L))
       
-      # Final filtered correaltion calculations and plots
+      # Final filtered correlation calculations and plots
       zscores_interactions_filtered <- zscores_interactions_filtered %>%
         mutate(
           Overlap = case_when(