Add more aes to generate_and_save_plots()

This commit is contained in:
2024-09-11 22:07:51 -04:00
parent e3bc5a2792
commit 2313c48358

View File

@@ -339,70 +339,71 @@ 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) { generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_layout = NULL) {
message("Generating html and pdf plots for: ", file_name, ".pdf|html") message("Generating html and pdf plots for: ", file_name, ".pdf|html")
plots <- lapply(plot_configs, function(config) { plots <- lapply(plot_configs, function(config) {
# Log configuration details # Log details and setup
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 df <- config$df
aes_mapping <-
# Build the aes mapping depending on whether y_var is present if (is.null(config$y_var))
aes_mapping <- if (is.null(config$y_var)) { aes(x = !!sym(config$x_var), color = as.factor(!!sym(config$color_var)))
aes(x = !!sym(config$x_var), color = as.factor(!!sym(config$color_var))) else
} else { aes(x = !!sym(config$x_var), y = !!sym(config$y_var), color = as.factor(!!sym(config$color_var)))
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) plot <- ggplot(df, aes_mapping)
# Handle plot types explicitly # Plot type handling
if (config$plot_type == "scatter") { plot <- switch(config$plot_type,
plot <- plot + geom_point(shape = 3)
"scatter" = {
# Add geom_smooth only if specified plot + geom_point(aes(ORF = ORF, Gene = Gene, !!sym(config$x_var) := !!sym(config$x_var)),
if (!is.null(config$add_smooth) && config$add_smooth) { shape = config$shape %||% 3, size = config$size %||% 0.6) +
plot <- plot + geom_smooth(method = "lm", se = FALSE) (if (!is.null(config$add_smooth) && config$add_smooth)
} geom_smooth(method = "lm", se = FALSE)
else NULL) +
} else if (config$plot_type == "rank") { (if (!is.null(config$position) && config$position == "jitter")
plot <- plot + geom_point(size = 0.1, shape = 3) geom_point(position = "jitter")
else NULL) +
if (!is.null(config$sd_band)) { # Use precalculated mean and sd for error bars
for (i in seq_len(config$sd_band)) { geom_errorbar(aes(
plot <- plot + ymin = !!sym(paste0("mean_", config$y_var)) - !!sym(paste0("sd_", config$y_var)),
annotate("rect", xmin = -Inf, xmax = Inf, ymin = i, ymax = Inf, fill = "#542788", alpha = 0.3) + ymax = !!sym(paste0("mean_", config$y_var)) + !!sym(paste0("sd_", config$y_var))), width = 0.1) +
annotate("rect", xmin = -Inf, xmax = Inf, ymin = -i, ymax = -Inf, fill = "orange", alpha = 0.3) + geom_point(aes(y = !!sym(paste0("mean_", config$y_var))), size = 0.6)
geom_hline(yintercept = c(-i, i), color = "gray") },
}
} "rank" = {
plot + geom_point(size = config$size %||% 0.1, shape = config$shape %||% 3) +
if (!is.null(config$enhancer_label)) { (if (!is.null(config$sd_band))
plot <- plot + annotate("text", x = config$enhancer_label$x, y = config$enhancer_label$y, label = config$enhancer_label$label) + Reduce(`+`, lapply(seq_len(config$sd_band), function(i) {
annotate("text", x = config$suppressor_label$x, y = config$suppressor_label$y, label = config$suppressor_label$label) list(
} annotate("rect", xmin = -Inf, xmax = Inf, ymin = i, ymax = Inf, fill = "#542788", alpha = 0.3),
} else if (config$plot_type == "correlation") { annotate("rect", xmin = -Inf, xmax = Inf, ymin = -i, ymax = -Inf, fill = "orange", alpha = 0.3),
plot <- plot + geom_point(shape = 3, color = "gray70") + geom_hline(yintercept = c(-i, i), color = "gray")
)
})) else NULL) +
(if (!is.null(config$enhancer_label))
annotate("text", x = config$enhancer_label$x, y = config$enhancer_label$y, label = config$enhancer_label$label)
else NULL) +
(if (!is.null(config$suppressor_label))
annotate("text", x = config$suppressor_label$x, y = config$suppressor_label$y, label = config$suppressor_label$label)
else NULL)
},
"correlation" = plot + geom_point(shape = config$shape %||% 3, color = "gray70") +
geom_smooth(method = "lm", color = "tomato3") + geom_smooth(method = "lm", color = "tomato3") +
annotate("text", x = 0, y = 0, label = config$correlation_text) annotate("text", x = 0, y = 0, label = config$correlation_text),
} else if (config$plot_type == "box") {
plot <- plot + geom_boxplot() "box" = plot + geom_boxplot(),
} else if (config$plot_type == "density") { "density" = plot + geom_density(),
plot <- plot + geom_density() "bar" = plot + geom_bar(),
} else if (config$plot_type == "bar") {
plot <- plot + geom_bar() # Default case (scatter with smooth line)
} else { plot + geom_point(shape = config$shape %||% 3) + geom_smooth(method = "lm", se = FALSE)
plot <- plot + geom_point(shape = 3) + geom_smooth(method = "lm", se = FALSE) )
}
# Error bars using pre-calculated mean and sd columns
# Handle error bars if needed
if (!is.null(config$error_bar) && config$error_bar) { if (!is.null(config$error_bar) && config$error_bar) {
y_mean_col <- paste0("mean_", config$y_var) y_mean_col <- paste0("mean_", config$y_var)
y_sd_col <- paste0("sd_", config$y_var) y_sd_col <- paste0("sd_", config$y_var)
@@ -411,37 +412,30 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la
ymax = !!sym(y_mean_col) + !!sym(y_sd_col)), width = 0.1) + ymax = !!sym(y_mean_col) + !!sym(y_sd_col)), width = 0.1) +
geom_point(aes(y = !!sym(y_mean_col)), size = 0.6) geom_point(aes(y = !!sym(y_mean_col)), size = 0.6)
} }
# Apply y-limits if provided # Y-limits and labels
if (!is.null(config$ylim_vals)) { plot <- plot + (if (!is.null(config$ylim_vals)) coord_cartesian(ylim = config$ylim_vals) else NULL) +
plot <- plot + coord_cartesian(ylim = config$ylim_vals) ggtitle(config$title) +
} theme_publication(legend_position = config$legend_position %||% "bottom") +
# 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 %||% "") xlab(config$x_label %||% "") + ylab(config$y_label %||% "")
# Add any annotations # Annotations
if (!is.null(config$annotations)) { if (!is.null(config$annotations)) {
for (annotation in config$annotations) { for (annotation in config$annotations) {
plot <- plot + geom_text(aes(x = annotation$x, y = annotation$y, label = annotation$label)) plot <- plot + geom_text(aes(x = annotation$x, y = annotation$y, label = annotation$label))
} }
} }
return(plot) return(plot)
}) })
# Save the plots as PDF # Save plots to PDF and HTML
pdf(file.path(output_dir, paste0(file_name, ".pdf")), width = 14, height = 9) pdf(file.path(output_dir, paste0(file_name, ".pdf")), width = 14, height = 9)
lapply(plots, print) lapply(plots, print)
dev.off() dev.off()
# Convert ggplot to plotly for interactive HTML output
plotly_plots <- lapply(plots, function(plot) suppressWarnings(ggplotly(plot) %>% layout(legend = list(orientation = "h")))) plotly_plots <- lapply(plots, function(plot) suppressWarnings(ggplotly(plot) %>% layout(legend = list(orientation = "h"))))
combined_plot <- subplot(plotly_plots, nrows = grid_layout$nrow %||% length(plots), margin = 0.05)
# 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) saveWidget(combined_plot, file = file.path(output_dir, paste0(file_name, ".html")), selfcontained = TRUE)
} }
@@ -492,7 +486,10 @@ generate_interaction_plot_configs <- function(df, variables) {
), ),
x_breaks = unique(df$conc_num_factor), x_breaks = unique(df$conc_num_factor),
x_labels = unique(as.character(df$conc_num)), x_labels = unique(as.character(df$conc_num)),
x_label = unique(df$Drug[1]) x_label = unique(df$Drug[1]),
shape = 3,
size = 0.6,
position = "jitter"
) )
# Add box plot configuration for this variable # Add box plot configuration for this variable
@@ -551,10 +548,11 @@ generate_rank_plot_configs <- function(df, rank_var, zscore_var, var, is_lm = FA
suppressor_label = list( suppressor_label = list(
x = nrow(df) / 2, y = -10, x = nrow(df) / 2, y = -10,
label = paste("Deletion Suppressors =", nrow(df[df[[zscore_var]] <= -sd_band, ])) label = paste("Deletion Suppressors =", nrow(df[df[[zscore_var]] <= -sd_band, ]))
) ),
shape = 3,
size = 0.1,
position = "jitter"
) )
return(configs)
} }
# Non-annotated version (_notext) # Non-annotated version (_notext)
@@ -567,7 +565,10 @@ generate_rank_plot_configs <- function(df, rank_var, zscore_var, var, is_lm = FA
title = paste(plot_title_prefix, var, "above", sd_band, "SD"), title = paste(plot_title_prefix, var, "above", sd_band, "SD"),
sd_band = sd_band, sd_band = sd_band,
enhancer_label = NULL, # No annotations for _notext enhancer_label = NULL, # No annotations for _notext
suppressor_label = NULL # No annotations for _notext suppressor_label = NULL, # No annotations for _notext
shape = 3,
size = 0.1,
position = "jitter"
) )
} }
@@ -590,6 +591,8 @@ generate_correlation_plot_configs <- function(df, variables) {
title = paste("Avg Zscore vs lm", variable), title = paste("Avg Zscore vs lm", variable),
color_var = "Overlap", color_var = "Overlap",
correlation_text = paste("R-squared =", round(df[[lm_r_squared_col]][1], 2)), correlation_text = paste("R-squared =", round(df[[lm_r_squared_col]][1], 2)),
shape = 3,
geom_smooth = TRUE,
legend_position = "right" legend_position = "right"
) )
} }
@@ -626,7 +629,7 @@ main <- function() {
# Remove rows with 0 values in L # Remove rows with 0 values in L
df_no_zeros <- df_na %>% filter(L > 0) df_no_zeros <- df_na %>% filter(L > 0)
# Set some constants # Save some constants
max_conc <- max(df$conc_num_factor) max_conc <- max(df$conc_num_factor)
l_half_median <- (median(df_above_tolerance$L, na.rm = TRUE)) / 2 l_half_median <- (median(df_above_tolerance$L, na.rm = TRUE)) / 2
k_half_median <- (median(df_above_tolerance$K, na.rm = TRUE)) / 2 k_half_median <- (median(df_above_tolerance$K, na.rm = TRUE)) / 2
@@ -705,46 +708,56 @@ main <- function() {
plot_type = "scatter", plot_type = "scatter",
title = "Raw L vs K before quality control", title = "Raw L vs K before quality control",
color_var = "conc_num", color_var = "conc_num",
position = "jitter",
legend_position = "right" legend_position = "right"
) )
) )
frequency_delta_bg_plots <- list(
list(
df = df_filtered_stats,
x_var = "delta_bg",
y_var = NULL,
plot_type = "density",
title = "Plate analysis by Drug Conc for Delta Background before quality control",
color_var = "conc_num",
x_label = "Delta Background",
y_label = "Density",
error_bar = FALSE,
legend_position = "right"),
list(
df = df_filtered_stats,
x_var = "delta_bg",
y_var = NULL,
plot_type = "bar",
title = "Plate analysis by Drug Conc for Delta Background before quality control",
color_var = "conc_num",
x_label = "Delta Background",
y_label = "Count",
error_bar = FALSE,
legend_position = "right")
)
above_threshold_plots <- list( above_threshold_plots <- list(
list( list(
df = df_above_tolerance, df = df_above_tolerance,
x_var = "L", x_var = "L",
y_var = "K", y_var = "K",
plot_type = "scatter", plot_type = "scatter",
title = paste("Raw L vs K for strains above delta background threshold of", title = paste("Raw L vs K for strains above Delta Background threshold of",
df_above_tolerance$delta_bg_tolerance[[1]], "or above"), df_above_tolerance$delta_bg_tolerance[[1]], "or above"),
color_var = "conc_num", color_var = "conc_num",
position = "jitter",
annotations = list( annotations = list(
x = l_half_median, x = l_half_median,
y = k_half_median, y = k_half_median,
label = paste("Strains above delta background tolerance =", nrow(df_above_tolerance)) label = paste("# strains above Delta Background tolerance =", nrow(df_above_tolerance))
), ),
error_bar = FALSE, error_bar = FALSE,
legend_position = "right" legend_position = "right"
) )
) )
frequency_delta_bg_plots <- list(
list(df = df_filtered_stats, x_var = "delta_bg", y_var = NULL, plot_type = "density",
title = "Plate analysis by Drug Conc for delta background before quality control",
color_var = "conc_num",
x_label = "Delta Background",
y_label = "Density",
error_bar = FALSE,
legend_position = "right"),
list(df = df_filtered_stats, x_var = "delta_bg", y_var = NULL, plot_type = "bar",
title = "Plate analysis by Drug Conc for delta background before quality control",
color_var = "conc_num",
x_label = "Delta Background",
y_label = "Count",
error_bar = FALSE,
legend_position = "right")
)
plate_analysis_plots <- list() plate_analysis_plots <- list()
for (var in summary_vars) { for (var in summary_vars) {
for (stage in c("before", "after")) { for (stage in c("before", "after")) {
@@ -760,7 +773,9 @@ main <- function() {
y_var = var, y_var = var,
plot_type = "scatter", plot_type = "scatter",
title = paste("Plate analysis by Drug Conc for", var, stage, "quality control"), title = paste("Plate analysis by Drug Conc for", var, stage, "quality control"),
error_bar = TRUE, color_var = "conc_num") error_bar = TRUE,
color_var = "conc_num",
position = "jitter")
plate_analysis_plots <- append(plate_analysis_plots, list(config)) plate_analysis_plots <- append(plate_analysis_plots, list(config))
} }
@@ -797,7 +812,8 @@ main <- function() {
plot_type = "scatter", plot_type = "scatter",
title = paste("Plate analysis by Drug Conc for", var, "after quality control"), title = paste("Plate analysis by Drug Conc for", var, "after quality control"),
error_bar = TRUE, error_bar = TRUE,
color_var = "conc_num") color_var = "conc_num",
position = "jitter")
plate_analysis_no_zeros_plots <- append(plate_analysis_no_zeros_plots, list(config)) plate_analysis_no_zeros_plots <- append(plate_analysis_no_zeros_plots, list(config))
} }
@@ -827,6 +843,7 @@ main <- function() {
plot_type = "scatter", plot_type = "scatter",
title = "Raw L vs K for strains falling outside 2SD of the K mean at each Conc", title = "Raw L vs K for strains falling outside 2SD of the K mean at each Conc",
color_var = "conc_num", color_var = "conc_num",
position = "jitter",
legend_position = "right" legend_position = "right"
) )
) )
@@ -839,14 +856,15 @@ main <- function() {
plot_type = "scatter", plot_type = "scatter",
title = "Delta Background vs K for strains falling outside 2SD of the K mean at each Conc", title = "Delta Background vs K for strains falling outside 2SD of the K mean at each Conc",
color_var = "conc_num", color_var = "conc_num",
position = "jitter",
legend_position = "right" legend_position = "right"
) )
) )
message("Generating QC plots") message("Generating QC plots")
generate_and_save_plots(out_dir_qc, "L_vs_K_before_quality_control", l_vs_k_plots) generate_and_save_plots(out_dir_qc, "L_vs_K_before_quality_control", l_vs_k_plots)
generate_and_save_plots(out_dir_qc, "L_vs_K_above_threshold", above_threshold_plots)
generate_and_save_plots(out_dir_qc, "frequency_delta_background", frequency_delta_bg_plots) generate_and_save_plots(out_dir_qc, "frequency_delta_background", frequency_delta_bg_plots)
generate_and_save_plots(out_dir_qc, "L_vs_K_above_threshold", above_threshold_plots)
generate_and_save_plots(out_dir_qc, "plate_analysis", plate_analysis_plots) generate_and_save_plots(out_dir_qc, "plate_analysis", plate_analysis_plots)
generate_and_save_plots(out_dir_qc, "plate_analysis_boxplots", plate_analysis_boxplots) generate_and_save_plots(out_dir_qc, "plate_analysis_boxplots", plate_analysis_boxplots)
generate_and_save_plots(out_dir_qc, "plate_analysis_no_zeros", plate_analysis_no_zeros_plots) generate_and_save_plots(out_dir_qc, "plate_analysis_no_zeros", plate_analysis_no_zeros_plots)