Преглед на файлове

Add configurable plot filtering

Bryan Roessler преди 8 месеца
родител
ревизия
f335b414e1
променени са 1 файла, в които са добавени 20 реда и са изтрити 9 реда
  1. 20 9
      qhtcp-workflow/apps/r/calculate_interaction_zscores.R

+ 20 - 9
qhtcp-workflow/apps/r/calculate_interaction_zscores.R

@@ -545,6 +545,13 @@ generate_and_save_plots <- function(out_dir, filename, plot_configs, page_width
           )
       }
 
+      # Filter NAs if specified
+      if (!is.null(config$na_rm) && config$na_rm) {
+        df <- df %>%
+          filter(!is.na(.data[[config$x_var]])) %>%
+          filter(!is.na(.data[[config$y_var]]))
+      }
+
       # Set up aes mapping based on plot type
       aes_mapping <- if (config$plot_type == "bar") {
         if (!is.null(config$color_var)) {
@@ -1092,7 +1099,7 @@ generate_interaction_plot_configs <- function(df_summary, df_interactions, type)
   ))
 }
 
-generate_rank_plot_configs <- function(df, is_lm = FALSE, adjust = FALSE, overlap_color = FALSE) {
+generate_rank_plot_configs <- function(df, is_lm = FALSE, adjust = FALSE, na_rm = FALSE, overlap_color = FALSE) {
   sd_bands <- c(1, 2, 3)
   plot_configs <- list()
   
@@ -1109,7 +1116,7 @@ generate_rank_plot_configs <- function(df, is_lm = FALSE, adjust = FALSE, overla
   }
 
   # Helper function to create a rank plot configuration
-  create_plot_config <- function(variable, rank_var, zscore_var, y_label, sd_band, with_annotations = TRUE) {
+  create_plot_config <- function(variable, rank_var, zscore_var, y_label, sd_band, na_rm, with_annotations = TRUE) {
     num_enhancers <- sum(df[[zscore_var]] >= sd_band, na.rm = TRUE)
     num_suppressors <- sum(df[[zscore_var]] <= -sd_band, na.rm = TRUE)
 
@@ -1119,6 +1126,7 @@ generate_rank_plot_configs <- function(df, is_lm = FALSE, adjust = FALSE, overla
       x_var = rank_var,
       y_var = zscore_var,
       x_label = "Rank",
+      y_label = y_label,
       plot_type = "scatter",
       title = paste(y_label, "vs. Rank for", variable, "above", sd_band, "SD"),
       sd_band = sd_band,
@@ -1128,8 +1136,7 @@ generate_rank_plot_configs <- function(df, is_lm = FALSE, adjust = FALSE, overla
       alpha_negative = 0.3,
       shape = 3,
       size = 0.1,
-      y_label = y_label,
-      x_label = "Rank",
+      na_rm = na_rm,
       legend_position = "none"
     )
     
@@ -1161,10 +1168,12 @@ generate_rank_plot_configs <- function(df, is_lm = FALSE, adjust = FALSE, overla
     # Loop through SD bands
     for (sd_band in sd_bands) {
       # Create plot with annotations
-      plot_configs[[length(plot_configs) + 1]] <- create_plot_config(variable, rank_var, zscore_var, y_label, sd_band, with_annotations = TRUE)
+      plot_configs[[length(plot_configs) + 1]] <-
+        create_plot_config(variable, rank_var, zscore_var, y_label, sd_band, na_rm, with_annotations = TRUE)
       
       # Create plot without annotations
-      plot_configs[[length(plot_configs) + 1]] <- create_plot_config(variable, rank_var, zscore_var, y_label, sd_band, with_annotations = FALSE)
+      plot_configs[[length(plot_configs) + 1]] <-
+        create_plot_config(variable, rank_var, zscore_var, y_label, sd_band, na_rm, with_annotations = FALSE)
     }
   }
 
@@ -1650,21 +1659,23 @@ main <- function() {
       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 overlapped ranked plots")
       rank_plot_filtered_configs <- generate_rank_plot_configs(
         df_interactions,
         is_lm = FALSE,
         adjust = FALSE,
+        na_rm = TRUE,
         overlap_color = TRUE
       )
       generate_and_save_plots(out_dir, "rank_plots_na_rm", rank_plot_filtered_configs,
         page_width = 18, page_height = 12)
 
-      message("Generating filtered ranked linear model plots")
+      message("Generating overlapped ranked linear model plots")
       rank_plot_lm_filtered_configs <- generate_rank_plot_configs(
         df_interactions,
         is_lm = TRUE,
         adjust = FALSE,
+        na_rm = TRUE,
         overlap_color = TRUE
       )
       generate_and_save_plots(out_dir, "rank_plots_lm_na_rm", rank_plot_lm_filtered_configs,
@@ -1672,7 +1683,7 @@ main <- function() {
 
       message("Generating correlation curve parameter pair plots")
       correlation_plot_configs <- generate_correlation_plot_configs(
-        df_interactions_joined
+        df_interactions
       )
       generate_and_save_plots(out_dir, "correlation_cpps", correlation_plot_configs,
         page_width = 10, page_height = 7)