diff --git a/qhtcp-workflow/apps/r/calculate_interaction_zscores.R b/qhtcp-workflow/apps/r/calculate_interaction_zscores.R index af25da6e..fc35904f 100644 --- a/qhtcp-workflow/apps/r/calculate_interaction_zscores.R +++ b/qhtcp-workflow/apps/r/calculate_interaction_zscores.R @@ -370,68 +370,18 @@ generate_and_save_plots <- function(out_dir, file_name, plot_configs, grid_layou config <- plot_configs[[i]] df <- config$df - # Initialize tooltip_text - tooltip_text <- NULL - - # Define aes_mapping based on plot type - if (config$plot_type == "scatter") { - # Check if y_var is provided + aes_mapping <- if (is.null(config$color_var)) { if (is.null(config$y_var)) { - warning(paste("Plot", i, "of type 'scatter' is missing 'y_var'. Skipping this plot.")) - next - } - - # Construct tooltip_text based on configuration flags - if (!is.null(config$delta_bg_point) && config$delta_bg_point) { - # Ensure 'delta_bg' exists - if (!"delta_bg" %in% names(df)) { - warning(paste("Plot", i, "requires 'delta_bg' column for tooltip, but it's missing.")) - tooltip_text <- paste("OrfRep:", df$OrfRep, "
Gene:", df$Gene) - } else { - tooltip_text <- paste("OrfRep:", df$OrfRep, "
Gene:", df$Gene, "
delta_bg:", df$delta_bg) - } - } else if (!is.null(config$gene_point) && config$gene_point) { - tooltip_text <- paste("OrfRep:", df$OrfRep, "
Gene:", df$Gene) + aes(x = .data[[config$x_var]]) } else { - tooltip_text <- paste("x:", df[[config$x_var]], "
y:", df[[config$y_var]]) - } - - # Define aesthetic mapping with or without color_var - aes_mapping <- if (is.null(config$color_var)) { - aes(x = .data[[config$x_var]], y = .data[[config$y_var]], text = tooltip_text) - } else { - aes(x = .data[[config$x_var]], y = .data[[config$y_var]], - color = as.factor(.data[[config$color_var]]), text = tooltip_text) + aes(x = .data[[config$x_var]], y = .data[[config$y_var]]) } } else { - # Handle other plot types - # For 'box' plots, y_var is required - if (config$plot_type == "box") { - if (is.null(config$y_var)) { - warning(paste("Plot", i, "of type 'box' is missing 'y_var'. Skipping this plot.")) - next - } - } - - # Define aes_mapping for non-scatter plots - aes_mapping <- if (is.null(config$color_var)) { - if (config$plot_type %in% c("density", "bar")) { - aes(x = .data[[config$x_var]]) - } else { - aes(x = .data[[config$x_var]], y = .data[[config$y_var]]) - } + if (is.null(config$y_var)) { + aes(x = .data[[config$x_var]], color = as.factor(.data[[config$color_var]])) } else { - if (config$plot_type %in% c("density", "bar")) { - aes(x = .data[[config$x_var]], - color = as.factor(.data[[config$color_var]])) - } else { - aes(x = .data[[config$x_var]], y = .data[[config$y_var]], - color = as.factor(.data[[config$color_var]])) - } + aes(x = .data[[config$x_var]], y = .data[[config$y_var]], color = as.factor(.data[[config$color_var]])) } - - # No tooltip for non-scatter plots - tooltip_text <- NULL } # Start building the plot with aes_mapping @@ -443,10 +393,7 @@ generate_and_save_plots <- function(out_dir, file_name, plot_configs, grid_layou "box" = generate_box_plot(plot_base, config), "density" = plot_base + geom_density(), "bar" = plot_base + geom_bar(), - { - warning(paste("Unknown plot_type:", config$plot_type, "- using base plot")) - plot_base - } + plot_base # default case if no type matches ) # Apply additional settings if provided @@ -465,11 +412,27 @@ generate_and_save_plots <- function(out_dir, file_name, plot_configs, grid_layou plot <- plot + ylab(config$y_label) } - # Convert to plotly object + # Apply scale_color_discrete(guide = FALSE) when color_var is NULL + if (is.null(config$color_var)) { + plot <- plot + scale_color_discrete(guide = FALSE) + } + + # Add interactive tooltips for plotly + tooltip_vars <- c() if (config$plot_type == "scatter") { - plotly_plot <- ggplotly(plot, tooltip = "text") + if (!is.null(config$delta_bg_point) && config$delta_bg_point) { + tooltip_vars <- c(tooltip_vars, "OrfRep", "Gene", "delta_bg") + } else if (!is.null(config$gene_point) && config$gene_point) { + tooltip_vars <- c(tooltip_vars, "OrfRep", "Gene") + } else if (!is.null(config$y_var) && !is.null(config$x_var)) { + tooltip_vars <- c(config$x_var, config$y_var) + } + } + + # Convert to plotly object + if (length(tooltip_vars) > 0) { + plotly_plot <- ggplotly(plot, tooltip = tooltip_vars) } else { - # For non-scatter plots, decide if tooltips are needed plotly_plot <- ggplotly(plot, tooltip = "none") } @@ -489,9 +452,16 @@ generate_and_save_plots <- function(out_dir, file_name, plot_configs, grid_layou dev.off() # Combine and save interactive HTML plots - combined_plot <- subplot(plotly_plots, - nrows = ifelse(is.null(grid_layout$nrow), length(plotly_plots), grid_layout$nrow), - margin = 0.05) + combined_plot <- subplot( + plotly_plots, + nrows = if (!is.null(grid_layout) && !is.null(grid_layout$nrow)) { + grid_layout$nrow + } else { + # Calculate nrow based on the length of plotly_plots (default 1 row if only one plot) + ceiling(length(plotly_plots) / ifelse(!is.null(grid_layout) && !is.null(grid_layout$ncol), grid_layout$ncol, 1)) + }, + margin = 0.05 + ) saveWidget(combined_plot, file = file.path(out_dir, paste0(file_name, ".html")), selfcontained = TRUE) }