Browse Source

Add configurable page dimensions

Bryan Roessler 6 tháng trước cách đây
mục cha
commit
20c6e94727

+ 34 - 45
qhtcp-workflow/apps/r/calculate_interaction_zscores.R

@@ -20,13 +20,9 @@ suppressPackageStartupMessages({
   library("purrr")
   library("purrr")
 })
 })
 
 
+# Turn all warnings into errors for development
 options(warn = 2)
 options(warn = 2)
 
 
-# Constants for configuration
-plot_width <- 14
-plot_height <- 9
-base_size <- 14
-
 parse_arguments <- function() {
 parse_arguments <- function() {
   args <- if (interactive()) {
   args <- if (interactive()) {
     c(
     c(
@@ -515,7 +511,7 @@ calculate_interaction_scores <- function(df, df_bg, group_vars, overlap_threshol
   ))
   ))
 }
 }
 
 
-generate_and_save_plots <- function(out_dir, filename, plot_configs) {
+generate_and_save_plots <- function(out_dir, filename, plot_configs, page_width = 12, page_height = 8) {
   message("Generating ", filename, ".pdf and ", filename, ".html")
   message("Generating ", filename, ".pdf and ", filename, ".html")
 
 
   # Check if we're dealing with multiple plot groups
   # Check if we're dealing with multiple plot groups
@@ -526,7 +522,7 @@ generate_and_save_plots <- function(out_dir, filename, plot_configs) {
   }
   }
 
 
   # Open the PDF device once for all plots
   # Open the PDF device once for all plots
-  pdf(file.path(out_dir, paste0(filename, ".pdf")), width = 16, height = 9)
+  pdf(file.path(out_dir, paste0(filename, ".pdf")), width = page_width, height = page_height)
 
 
   # Loop through each plot group
   # Loop through each plot group
   for (group in plot_groups) {
   for (group in plot_groups) {
@@ -1043,7 +1039,7 @@ generate_interaction_plot_configs <- function(df, type) {
   return(list(
   return(list(
     list(grid_layout = list(ncol = 2), plots = stats_plot_configs),
     list(grid_layout = list(ncol = 2), plots = stats_plot_configs),
     list(grid_layout = list(ncol = 2), plots = stats_boxplot_configs),
     list(grid_layout = list(ncol = 2), plots = stats_boxplot_configs),
-    list(grid_layout = list(ncol = 4), plots = delta_plot_configs)  # nrow calculated dynamically
+    list(grid_layout = list(ncol = 4), plots = delta_plot_configs[1:12])  # nrow calculated dynamically
   ))
   ))
 }
 }
 
 
@@ -1123,12 +1119,7 @@ generate_rank_plot_configs <- function(df, is_lm = FALSE, adjust = FALSE, overla
     }
     }
   }
   }
 
 
-  # Calculate dynamic grid layout based on the number of plots
-  grid_ncol <- 3
-  num_plots <- length(plot_configs)
-  grid_nrow <- ceiling(num_plots / grid_ncol)  # Automatically calculate the number of rows
-
-  return(list(grid_layout = list(ncol = grid_ncol, nrow = grid_nrow), plots = plot_configs))
+  return(list(grid_layout = list(ncol = 3), plots = plot_configs))
 }
 }
 
 
 generate_correlation_plot_configs <- function(df, correlation_stats) {
 generate_correlation_plot_configs <- function(df, correlation_stats) {
@@ -1418,29 +1409,34 @@ main <- function() {
 
 
     plot_configs <- list(
     plot_configs <- list(
       list(out_dir = out_dir_qc, filename = "L_vs_K_before_quality_control",
       list(out_dir = out_dir_qc, filename = "L_vs_K_before_quality_control",
-        plot_configs = l_vs_k_plot_configs),
+        plot_configs = l_vs_k_plot_configs, page_width = 12, page_height = 8),
       list(out_dir = out_dir_qc, filename = "frequency_delta_background",
       list(out_dir = out_dir_qc, filename = "frequency_delta_background",
-        plot_configs = frequency_delta_bg_plot_configs),
+        plot_configs = frequency_delta_bg_plot_configs, page_width = 12, page_height = 8),
       list(out_dir = out_dir_qc, filename = "L_vs_K_above_threshold",
       list(out_dir = out_dir_qc, filename = "L_vs_K_above_threshold",
-        plot_configs = above_threshold_plot_configs),
+        plot_configs = above_threshold_plot_configs, page_width = 12, page_height = 8),
       list(out_dir = out_dir_qc, filename = "plate_analysis",
       list(out_dir = out_dir_qc, filename = "plate_analysis",
-        plot_configs = plate_analysis_plot_configs),
+        plot_configs = plate_analysis_plot_configs, page_width = 14, page_height = 9),
       list(out_dir = out_dir_qc, filename = "plate_analysis_boxplots",
       list(out_dir = out_dir_qc, filename = "plate_analysis_boxplots",
-        plot_configs = plate_analysis_boxplot_configs),
+        plot_configs = plate_analysis_boxplot_configs, page_width = 18, page_height = 9),
       list(out_dir = out_dir_qc, filename = "plate_analysis_no_zeros",
       list(out_dir = out_dir_qc, filename = "plate_analysis_no_zeros",
-        plot_configs = plate_analysis_no_zeros_plot_configs),
+        plot_configs = plate_analysis_no_zeros_plot_configs, page_width = 12, page_height = 8),
       list(out_dir = out_dir_qc, filename = "plate_analysis_no_zeros_boxplots",
       list(out_dir = out_dir_qc, filename = "plate_analysis_no_zeros_boxplots",
-        plot_configs = plate_analysis_no_zeros_boxplot_configs),
+        plot_configs = plate_analysis_no_zeros_boxplot_configs, page_width = 18, page_height = 9),
       list(out_dir = out_dir_qc, filename = "L_vs_K_for_strains_2SD_outside_mean_K",
       list(out_dir = out_dir_qc, filename = "L_vs_K_for_strains_2SD_outside_mean_K",
-        plot_configs = l_outside_2sd_k_plot_configs),
+        plot_configs = l_outside_2sd_k_plot_configs, page_width = 10, page_height = 8),
       list(out_dir = out_dir_qc, filename = "delta_background_vs_K_for_strains_2SD_outside_mean_K",
       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 = delta_bg_outside_2sd_k_plot_configs, page_width = 10, page_height = 8)
     )
     )
 
 
-    # furrr::future_map(plot_configs, function(config) {
-    #   generate_and_save_plots(config$out_dir, config$filename, config$plot_configs)
-    # }, .options = furrr_options(seed = TRUE))
+    # Parallelize background and quality control plot generation
+    furrr::future_map(plot_configs, function(config) {
+      generate_and_save_plots(config$out_dir, config$filename, config$plot_configs,
+        page_width = config$page_width, page_height = config$page_height)
+    }, .options = furrr_options(seed = TRUE))
 
 
+    # Loop over background strains
+    # TODO currently only tested against one strain, if we want to do multiple strains we'll
+    # have to rename or group the output files by dir or something so they don't get clobbered
     bg_strains <- c("YDL227C")
     bg_strains <- c("YDL227C")
     lapply(bg_strains, function(strain) {
     lapply(bg_strains, function(strain) {
       message("Processing background strain: ", strain)
       message("Processing background strain: ", strain)
@@ -1496,7 +1492,7 @@ main <- function() {
 
 
       message("Generating reference interaction plots")
       message("Generating reference interaction plots")
       reference_plot_configs <- generate_interaction_plot_configs(df_interactions_reference_joined, "reference")
       reference_plot_configs <- generate_interaction_plot_configs(df_interactions_reference_joined, "reference")
-      generate_and_save_plots(out_dir, "interaction_plots_reference", reference_plot_configs)
+      generate_and_save_plots(out_dir, "interaction_plots_reference", reference_plot_configs, page_width = 16, page_height = 16)
 
 
       message("Setting missing deletion values to the highest theoretical value at each drug conc for L")
       message("Setting missing deletion values to the highest theoretical value at each drug conc for L")
       df_deletion <- df_na_stats %>% # formerly X2
       df_deletion <- df_na_stats %>% # formerly X2
@@ -1527,7 +1523,7 @@ main <- function() {
 
 
       message("Generating deletion interaction plots")
       message("Generating deletion interaction plots")
       deletion_plot_configs <- generate_interaction_plot_configs(df_interactions_joined, "deletion")
       deletion_plot_configs <- generate_interaction_plot_configs(df_interactions_joined, "deletion")
-      generate_and_save_plots(out_dir, "interaction_plots", deletion_plot_configs)
+      generate_and_save_plots(out_dir, "interaction_plots", deletion_plot_configs, page_width = 16, page_height = 16)
 
 
       message("Writing enhancer/suppressor csv files")
       message("Writing enhancer/suppressor csv files")
       interaction_threshold <- 2  # TODO add to study config?
       interaction_threshold <- 2  # TODO add to study config?
@@ -1567,8 +1563,8 @@ main <- function() {
         is_lm = FALSE,
         is_lm = FALSE,
         adjust = TRUE
         adjust = TRUE
       )
       )
-      generate_and_save_plots(out_dir = out_dir, filename = "rank_plots",
-        plot_configs = rank_plot_configs)
+      generate_and_save_plots(out_dir, "rank_plots", rank_plot_configs,
+        page_width = 18, page_height = 12)
 
 
       message("Generating ranked linear model plots")
       message("Generating ranked linear model plots")
       rank_lm_plot_configs <- generate_rank_plot_configs(
       rank_lm_plot_configs <- generate_rank_plot_configs(
@@ -1576,8 +1572,8 @@ main <- function() {
         is_lm = TRUE,
         is_lm = TRUE,
         adjust = TRUE
         adjust = TRUE
       )
       )
-      generate_and_save_plots(out_dir = out_dir, filename = "rank_plots_lm",
-        plot_configs = rank_lm_plot_configs)
+      generate_and_save_plots(out_dir, "rank_plots_lm", rank_lm_plot_configs,
+        page_width = 18, page_height = 12)
 
 
       message("Generating filtered ranked plots")
       message("Generating filtered ranked plots")
       rank_plot_filtered_configs <- generate_rank_plot_configs(
       rank_plot_filtered_configs <- generate_rank_plot_configs(
@@ -1586,10 +1582,8 @@ main <- function() {
         adjust = FALSE,
         adjust = FALSE,
         overlap_color = TRUE
         overlap_color = TRUE
       )
       )
-      generate_and_save_plots(
-        out_dir = out_dir,
-        filename = "RankPlots_na_rm",
-        plot_configs = rank_plot_filtered_configs)
+      generate_and_save_plots(out_dir, "RankPlots_na_rm", rank_plot_filtered_configs,
+        page_width = 18, page_height = 12)
 
 
       message("Generating filtered ranked linear model plots")
       message("Generating filtered ranked linear model plots")
       rank_plot_lm_filtered_configs <- generate_rank_plot_configs(
       rank_plot_lm_filtered_configs <- generate_rank_plot_configs(
@@ -1598,20 +1592,15 @@ main <- function() {
         adjust = FALSE,
         adjust = FALSE,
         overlap_color = TRUE
         overlap_color = TRUE
       )
       )
-      generate_and_save_plots(
-        out_dir = out_dir,
-        filename = "rank_plots_lm_na_rm",
-        plot_configs = rank_plot_lm_filtered_configs)
+      generate_and_save_plots(out_dir, "rank_plots_lm_na_rm", rank_plot_lm_filtered_configs,
+        page_width = 18, page_height = 12)
 
 
       message("Generating correlation curve parameter pair plots")
       message("Generating correlation curve parameter pair plots")
       correlation_plot_configs <- generate_correlation_plot_configs(
       correlation_plot_configs <- generate_correlation_plot_configs(
         df_interactions_joined
         df_interactions_joined
       )
       )
-      generate_and_save_plots(
-        out_dir = out_dir,
-        filename = "correlation_cpps",
-        plot_configs = correlation_plot_configs,
-      )
+      generate_and_save_plots(out_dir, "correlation_cpps", correlation_plot_configs,
+        page_width = 10, page_height = 7)
     })
     })
   })
   })
 }
 }