From 728449efb4ccc09dc2db85520b2955d9ba48eae6 Mon Sep 17 00:00:00 2001 From: Bryan Roessler Date: Wed, 18 Sep 2024 14:26:41 -0400 Subject: [PATCH] Cleanup plot inconsistincies --- .../apps/r/calculate_interaction_zscores.R | 82 +++++++++++++------ 1 file changed, 57 insertions(+), 25 deletions(-) diff --git a/qhtcp-workflow/apps/r/calculate_interaction_zscores.R b/qhtcp-workflow/apps/r/calculate_interaction_zscores.R index 4f9a4899..e911620d 100644 --- a/qhtcp-workflow/apps/r/calculate_interaction_zscores.R +++ b/qhtcp-workflow/apps/r/calculate_interaction_zscores.R @@ -466,20 +466,21 @@ generate_and_save_plots <- function(out_dir, file_name, plot_configs, grid_layou } generate_scatter_plot <- function(plot, config) { - - # Determine Shape, Size, and Position for geom_point shape <- if (!is.null(config$shape)) config$shape else 3 size <- if (!is.null(config$size)) config$size else 0.1 - position <- if (!is.null(config$position) && config$position == "jitter") "jitter" else "identity" - - # Add geom_point with determined parameters + position <- + if (!is.null(config$position) && config$position == "jitter") { + position_jitter(width = 0.1, height = 0) + } else { + "identity" + } plot <- plot + geom_point( shape = shape, size = size, position = position ) - if (!is.null(config$cyan_points)) { + if (!is.null(config$cyan_points) && config$cyan_points) { plot <- plot + geom_point( aes(x = .data[[config$x_var]], y = .data[[config$y_var]]), color = "cyan", @@ -514,19 +515,19 @@ generate_scatter_plot <- function(plot, config) { "rect", xmin = -Inf, xmax = Inf, ymin = config$sd_band, ymax = Inf, - fill = "#542788", - alpha = 0.3 + fill = ifelse(!is.null(config$fill_positive), config$fill_positive, "#542788"), + alpha = ifelse(!is.null(config$alpha_positive), config$alpha_positive, 0.3) ) + annotate( "rect", xmin = -Inf, xmax = Inf, ymin = -config$sd_band, ymax = -Inf, - fill = "orange", - alpha = 0.3 + fill = ifelse(!is.null(config$fill_negative), config$fill_negative, "orange"), + alpha = ifelse(!is.null(config$alpha_negative), config$alpha_negative, 0.3) ) + geom_hline( yintercept = c(-config$sd_band, config$sd_band), - color = "gray" + color = ifelse(!is.null(config$hl_color), config$hl_color, "gray") ) } @@ -590,7 +591,10 @@ generate_scatter_plot <- function(plot, config) { x = annotation$x, y = annotation$y, label = annotation$label, - na.rm = TRUE + hjust = ifelse(is.null(annotation$hjust), 0.5, annotation$hjust), + vjust = ifelse(is.null(annotation$vjust), 0.5, annotation$vjust), + size = ifelse(is.null(annotation$size), 4, annotation$size), + color = ifelse(is.null(annotation$color), "black", annotation$color) ) } } @@ -768,7 +772,7 @@ generate_rank_plot_configs <- function(df_filtered, variables, is_lm = FALSE) { # SD-based plots for L and K for (variable in c("L", "K")) { - + if (is_lm) { rank_var <- paste0("Rank_lm_", variable) zscore_var <- paste0("Z_lm_", variable) @@ -778,9 +782,9 @@ generate_rank_plot_configs <- function(df_filtered, variables, is_lm = FALSE) { zscore_var <- paste0("Avg_Zscore_", variable) y_label <- paste("Avg Z score", variable) } - + for (sd_band in sd_bands) { - + num_enhancers <- sum(df_filtered[[zscore_var]] >= sd_band, na.rm = TRUE) num_suppressors <- sum(df_filtered[[zscore_var]] <= -sd_band, na.rm = TRUE) @@ -792,16 +796,24 @@ generate_rank_plot_configs <- function(df_filtered, variables, is_lm = FALSE) { plot_type = "scatter", title = paste(y_label, "vs. Rank for", variable, "above", sd_band, "SD"), sd_band = sd_band, + fill_positive = "#542788", + fill_negative = "orange", + alpha_positive = 0.3, + alpha_negative = 0.3, annotations = list( list( x = median(df_filtered[[rank_var]], na.rm = TRUE), y = 10, - label = paste("Deletion Enhancers =", num_enhancers) + label = paste("Deletion Enhancers =", num_enhancers), + hjust = 0.5, + vjust = 1 ), list( x = median(df_filtered[[rank_var]], na.rm = TRUE), y = -10, - label = paste("Deletion Suppressors =", num_suppressors) + label = paste("Deletion Suppressors =", num_suppressors), + hjust = 0.5, + vjust = 0 ) ), shape = 3, @@ -819,6 +831,10 @@ generate_rank_plot_configs <- function(df_filtered, variables, is_lm = FALSE) { plot_type = "scatter", title = paste(y_label, "vs. Rank for", variable, "above", sd_band, "SD No Annotations"), sd_band = sd_band, + fill_positive = "#542788", + fill_negative = "orange", + alpha_positive = 0.3, + alpha_negative = 0.3, annotations = NULL, shape = 3, size = 0.1, @@ -868,9 +884,18 @@ generate_rank_plot_configs <- function(df_filtered, variables, is_lm = FALSE) { title = title, annotations = list( list( - x = 0, - y = 0, - label = paste("R-squared =", round(lm_summary$r.squared, 2)) + x = median(df_filtered[[rank_var]], na.rm = TRUE), + y = 10, + label = paste("Deletion Enhancers =", num_enhancers), + hjust = 0.5, + vjust = 1 + ), + list( + x = median(df_filtered[[rank_var]], na.rm = TRUE), + y = -10, + label = paste("Deletion Suppressors =", num_suppressors), + hjust = 0.5, + vjust = 0 ) ), shape = 3, @@ -916,7 +941,15 @@ generate_correlation_plot_configs <- function(df) { x_label = paste("z-score", gsub("Z_lm_", "", rel$x)), y_label = paste("z-score", gsub("Z_lm_", "", rel$y)), annotations = list( - list(x = 0, y = 0, label = paste("R-squared =", round(lm_summary$r.squared, 3))) + list( + x = Inf, + y = Inf, + label = paste("R-squared =", round(lm_summary$r.squared, 3)), + hjust = 1.1, + vjust = 2, + size = 4, + color = "black" + ) ), add_smooth = TRUE, # Add regression line lm_line = list(intercept = coef(lm_model)[1], slope = coef(lm_model)[2]), @@ -1380,15 +1413,14 @@ main <- function() { ) # Re-rank - zscores_interactions_filtered <- process_data( + zscores_interactions_filtered_ranked <- process_data( df = zscores_interactions_filtered, variables = interaction_vars, - filter_na = TRUE, # TODO what I'm currently having issues with rank = TRUE ) rank_plot_filtered_configs <- generate_rank_plot_configs( - df = zscores_interactions_filtered, + df = zscores_interactions_filtered_ranked, variables = interaction_vars, is_lm = FALSE ) @@ -1402,7 +1434,7 @@ main <- function() { message("Generating filtered ranked linear model plots") rank_plot_lm_filtered_configs <- generate_rank_plot_configs( - df = zscores_interactions_filtered, + df = zscores_interactions_filtered_ranked, variables = interaction_vars, is_lm = TRUE )