Break plate analysis out of main()
This commit is contained in:
@@ -355,7 +355,7 @@ calculate_interaction_scores <- function(df, max_conc, variables, group_vars) {
|
|||||||
calculations_joined = calculations_joined))
|
calculations_joined = calculations_joined))
|
||||||
}
|
}
|
||||||
|
|
||||||
generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_layout = NULL) {
|
generate_and_save_plots <- function(out_dir, file_name, plot_configs, grid_layout = NULL) {
|
||||||
message("Generating ", file_name, ".pdf and ", file_name, ".html")
|
message("Generating ", file_name, ".pdf and ", file_name, ".html")
|
||||||
|
|
||||||
# Prepare lists to collect plots
|
# Prepare lists to collect plots
|
||||||
@@ -410,19 +410,9 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Add interactive tooltips for plotly plots
|
# Add interactive tooltips for plotly plots
|
||||||
tooltip_vars <- c("x", "y")
|
tooltip_vars <- c()
|
||||||
if (!is.null(config$tooltip_vars)) {
|
if (config$plot_type == "scatter") {
|
||||||
tooltip_vars <- config$tooltip_vars
|
tooltip_vars <- c(config$x_var, config$y_var)
|
||||||
} else {
|
|
||||||
# Include default variables based on config
|
|
||||||
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 {
|
|
||||||
# Include x and y variables by default
|
|
||||||
tooltip_vars <- c("x", "y")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Convert to plotly object
|
# Convert to plotly object
|
||||||
@@ -437,7 +427,7 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Save static PDF plots
|
# Save static PDF plots
|
||||||
pdf(file.path(output_dir, paste0(file_name, ".pdf")), width = 14, height = 9)
|
pdf(file.path(out_dir, paste0(file_name, ".pdf")), width = 14, height = 9)
|
||||||
lapply(static_plots, print)
|
lapply(static_plots, print)
|
||||||
dev.off()
|
dev.off()
|
||||||
|
|
||||||
@@ -445,16 +435,11 @@ generate_and_save_plots <- function(output_dir, file_name, plot_configs, grid_la
|
|||||||
combined_plot <- subplot(plotly_plots,
|
combined_plot <- subplot(plotly_plots,
|
||||||
nrows = ifelse(is.null(grid_layout$nrow), length(plotly_plots), grid_layout$nrow),
|
nrows = ifelse(is.null(grid_layout$nrow), length(plotly_plots), grid_layout$nrow),
|
||||||
margin = 0.05)
|
margin = 0.05)
|
||||||
saveWidget(combined_plot, file = file.path(output_dir, paste0(file_name, ".html")), selfcontained = TRUE)
|
saveWidget(combined_plot, file = file.path(out_dir, paste0(file_name, ".html")), selfcontained = TRUE)
|
||||||
}
|
}
|
||||||
|
|
||||||
generate_scatter_plot <- function(plot, config) {
|
generate_scatter_plot <- function(plot, config) {
|
||||||
|
|
||||||
# Build the aes mapping with color if specified
|
|
||||||
if (!is.null(config$color_var)) {
|
|
||||||
plot <- plot + aes(color = .data[[config$color_var]])
|
|
||||||
}
|
|
||||||
|
|
||||||
# Determine Shape, Size, and Position for geom_point
|
# Determine Shape, Size, and Position for geom_point
|
||||||
shape <- if (!is.null(config$shape)) config$shape else 3
|
shape <- if (!is.null(config$shape)) config$shape else 3
|
||||||
size <- if (!is.null(config$size)) config$size else 0.1
|
size <- if (!is.null(config$size)) config$size else 0.1
|
||||||
@@ -462,7 +447,6 @@ generate_scatter_plot <- function(plot, config) {
|
|||||||
|
|
||||||
# Add geom_point with determined parameters
|
# Add geom_point with determined parameters
|
||||||
plot <- plot + geom_point(
|
plot <- plot + geom_point(
|
||||||
aes(color = .data[[config$color_var]]),
|
|
||||||
shape = shape,
|
shape = shape,
|
||||||
size = size,
|
size = size,
|
||||||
position = position
|
position = position
|
||||||
@@ -470,7 +454,6 @@ generate_scatter_plot <- function(plot, config) {
|
|||||||
|
|
||||||
if (!is.null(config$cyan_points)) {
|
if (!is.null(config$cyan_points)) {
|
||||||
plot <- plot + geom_point(
|
plot <- plot + geom_point(
|
||||||
data = subset(config$df, is_cyan_point == TRUE),
|
|
||||||
aes(x = .data[[config$x_var]], y = .data[[config$y_var]]),
|
aes(x = .data[[config$x_var]], y = .data[[config$y_var]]),
|
||||||
color = "cyan",
|
color = "cyan",
|
||||||
shape = 3,
|
shape = 3,
|
||||||
@@ -618,6 +601,38 @@ generate_box_plot <- function(plot, config) {
|
|||||||
return(plot)
|
return(plot)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
generate_plate_analysis_plot_configs <- function(variables, stages = c("before", "after"),
|
||||||
|
df_before = NULL, df_after = NULL, plot_type = "scatter") {
|
||||||
|
plots <- list()
|
||||||
|
for (var in variables) {
|
||||||
|
for (stage in stages) {
|
||||||
|
df_plot <- if (stage == "before") df_before else df_after
|
||||||
|
|
||||||
|
# Adjust settings based on plot_type
|
||||||
|
if (plot_type == "scatter") {
|
||||||
|
error_bar <- TRUE
|
||||||
|
position <- "jitter"
|
||||||
|
} else if (plot_type == "box") {
|
||||||
|
error_bar <- FALSE
|
||||||
|
position <- NULL
|
||||||
|
}
|
||||||
|
|
||||||
|
config <- list(
|
||||||
|
df = df_plot,
|
||||||
|
x_var = "scan",
|
||||||
|
y_var = var,
|
||||||
|
plot_type = plot_type,
|
||||||
|
title = paste("Plate analysis by Drug Conc for", var, stage, "quality control"),
|
||||||
|
error_bar = error_bar,
|
||||||
|
color_var = "conc_num_factor",
|
||||||
|
position = position
|
||||||
|
)
|
||||||
|
plots <- append(plots, list(config))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return(plots)
|
||||||
|
}
|
||||||
|
|
||||||
generate_interaction_plot_configs <- function(df, variables) {
|
generate_interaction_plot_configs <- function(df, variables) {
|
||||||
|
|
||||||
configs <- list()
|
configs <- list()
|
||||||
@@ -842,7 +857,7 @@ generate_rank_plot_configs <- function(df_filtered, variables, is_lm = FALSE) {
|
|||||||
color_var = "Overlap",
|
color_var = "Overlap",
|
||||||
x_label = x_var,
|
x_label = x_var,
|
||||||
y_label = y_var,
|
y_label = y_var,
|
||||||
rectangles = rectangles # Add rectangles configuration
|
rectangles = rectangles
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -871,7 +886,7 @@ generate_correlation_plot_configs <- function(df) {
|
|||||||
config <- list(
|
config <- list(
|
||||||
df = df,
|
df = df,
|
||||||
x_var = rel$x,
|
x_var = rel$x,
|
||||||
y_var = rel.y,
|
y_var = rel$y,
|
||||||
plot_type = "scatter",
|
plot_type = "scatter",
|
||||||
title = rel$label,
|
title = rel$label,
|
||||||
x_label = paste("z-score", gsub("Z_lm_", "", rel$x)),
|
x_label = paste("z-score", gsub("Z_lm_", "", rel$x)),
|
||||||
@@ -1137,79 +1152,31 @@ main <- function() {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
plate_analysis_plots <- list()
|
plate_analysis_plot_configs <- generate_plate_analysis_plot_configs(
|
||||||
for (var in summary_vars) {
|
variables = summary_vars,
|
||||||
for (stage in c("before", "after")) {
|
df_before = df_filtered_stats,
|
||||||
if (stage == "before") {
|
df_after = df_na_filtered_stats,
|
||||||
df_plot <- df_filtered_stats
|
)
|
||||||
} else {
|
|
||||||
df_plot <- df_na_filtered_stats
|
|
||||||
}
|
|
||||||
|
|
||||||
config <- list(
|
|
||||||
df = df_plot,
|
|
||||||
x_var = "scan",
|
|
||||||
y_var = var,
|
|
||||||
plot_type = "scatter",
|
|
||||||
title = paste("Plate analysis by Drug Conc for", var, stage, "quality control"),
|
|
||||||
error_bar = TRUE,
|
|
||||||
color_var = "conc_num_factor",
|
|
||||||
position = "jitter")
|
|
||||||
|
|
||||||
plate_analysis_plots <- append(plate_analysis_plots, list(config))
|
plate_analysis_box_plot_configs <- generate_plate_analysis_plot_configs(
|
||||||
}
|
variables = summary_vars,
|
||||||
}
|
df_before = df_filtered_stats,
|
||||||
|
df_after = df_na_filtered_stats,
|
||||||
|
plot_type = "box"
|
||||||
|
)
|
||||||
|
|
||||||
plate_analysis_boxplots <- list()
|
plate_analysis_no_zeros_plot_configs <- generate_plate_analysis_plot_configs(
|
||||||
for (var in summary_vars) {
|
variables = summary_vars,
|
||||||
for (stage in c("before", "after")) {
|
stages = c("after"), # Only after QC
|
||||||
if (stage == "before") {
|
df_after = df_no_zeros_filtered_stats,
|
||||||
df_plot <- df_filtered_stats
|
)
|
||||||
} else {
|
|
||||||
df_plot <- df_na_filtered_stats
|
|
||||||
}
|
|
||||||
|
|
||||||
config <- list(
|
|
||||||
df = df_plot,
|
|
||||||
x_var = "scan",
|
|
||||||
y_var = var,
|
|
||||||
plot_type = "box",
|
|
||||||
title = paste("Plate analysis by Drug Conc for", var, stage, "quality control"),
|
|
||||||
error_bar = FALSE,
|
|
||||||
color_var = "conc_num_factor")
|
|
||||||
|
|
||||||
plate_analysis_boxplots <- append(plate_analysis_boxplots, list(config))
|
plate_analysis_no_zeros_boxplot_configs <- generate_plate_analysis_plot_configs(
|
||||||
}
|
variables = summary_vars,
|
||||||
}
|
stages = c("after"), # Only after QC
|
||||||
|
df_after = df_no_zeros_filtered_stats,
|
||||||
plate_analysis_no_zeros_plots <- list()
|
plot_type = "box"
|
||||||
for (var in summary_vars) {
|
)
|
||||||
config <- list(
|
|
||||||
df = df_no_zeros_filtered_stats,
|
|
||||||
x_var = "scan",
|
|
||||||
y_var = var,
|
|
||||||
plot_type = "scatter",
|
|
||||||
title = paste("Plate analysis by Drug Conc for", var, "after quality control"),
|
|
||||||
error_bar = TRUE,
|
|
||||||
color_var = "conc_num_factor",
|
|
||||||
position = "jitter")
|
|
||||||
|
|
||||||
plate_analysis_no_zeros_plots <- append(plate_analysis_no_zeros_plots, list(config))
|
|
||||||
}
|
|
||||||
|
|
||||||
plate_analysis_no_zeros_boxplots <- list()
|
|
||||||
for (var in summary_vars) {
|
|
||||||
config <- list(
|
|
||||||
df = df_no_zeros_filtered_stats,
|
|
||||||
x_var = "scan",
|
|
||||||
y_var = var,
|
|
||||||
plot_type = "box",
|
|
||||||
title = paste("Plate analysis by Drug Conc for", var, "after quality control"),
|
|
||||||
error_bar = FALSE,
|
|
||||||
color_var = "conc_num_factor"
|
|
||||||
)
|
|
||||||
plate_analysis_no_zeros_boxplots <- append(plate_analysis_no_zeros_boxplots, list(config))
|
|
||||||
}
|
|
||||||
|
|
||||||
l_outside_2sd_k_plots <- list(
|
l_outside_2sd_k_plots <- list(
|
||||||
list(
|
list(
|
||||||
@@ -1239,16 +1206,16 @@ main <- function() {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
# message("Generating quality control plots")
|
message("Generating quality control 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, "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, "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_plot_configs)
|
||||||
# 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_boxplot_configs)
|
||||||
# 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_plot_configs)
|
||||||
# generate_and_save_plots(out_dir_qc, "plate_analysis_no_zeros_boxplots", plate_analysis_no_zeros_boxplots)
|
generate_and_save_plots(out_dir_qc, "plate_analysis_no_zeros_boxplots", plate_analysis_no_zeros_boxplot_configs)
|
||||||
# generate_and_save_plots(out_dir_qc, "L_vs_K_for_strains_2SD_outside_mean_K", l_outside_2sd_k_plots)
|
generate_and_save_plots(out_dir_qc, "L_vs_K_for_strains_2SD_outside_mean_K", l_outside_2sd_k_plots)
|
||||||
# generate_and_save_plots(out_dir_qc, "delta_background_vs_K_for_strains_2sd_outside_mean_K", delta_bg_outside_2sd_k_plots)
|
generate_and_save_plots(out_dir_qc, "delta_background_vs_K_for_strains_2sd_outside_mean_K", delta_bg_outside_2sd_k_plots)
|
||||||
|
|
||||||
# Process background strains
|
# Process background strains
|
||||||
bg_strains <- c("YDL227C")
|
bg_strains <- c("YDL227C")
|
||||||
@@ -1399,7 +1366,7 @@ main <- function() {
|
|||||||
variables = interaction_vars,
|
variables = interaction_vars,
|
||||||
is_lm = FALSE
|
is_lm = FALSE
|
||||||
)
|
)
|
||||||
generate_and_save_plots(output_dir = out_dir, file_name = "RankPlots",
|
generate_and_save_plots(out_dir = out_dir, file_name = "RankPlots",
|
||||||
plot_configs = rank_plot_configs, grid_layout = list(ncol = 3, nrow = 2))
|
plot_configs = rank_plot_configs, grid_layout = list(ncol = 3, nrow = 2))
|
||||||
|
|
||||||
message("Generating ranked linear model plots")
|
message("Generating ranked linear model plots")
|
||||||
@@ -1408,7 +1375,7 @@ main <- function() {
|
|||||||
variables = interaction_vars,
|
variables = interaction_vars,
|
||||||
is_lm = TRUE
|
is_lm = TRUE
|
||||||
)
|
)
|
||||||
generate_and_save_plots(output_dir = out_dir, file_name = "RankPlots_lm",
|
generate_and_save_plots(out_dir = out_dir, file_name = "RankPlots_lm",
|
||||||
plot_configs = rank_lm_plot_configs, grid_layout = list(ncol = 3, nrow = 2))
|
plot_configs = rank_lm_plot_configs, grid_layout = list(ncol = 3, nrow = 2))
|
||||||
|
|
||||||
message("Filtering and reranking plots")
|
message("Filtering and reranking plots")
|
||||||
@@ -1449,7 +1416,7 @@ main <- function() {
|
|||||||
|
|
||||||
message("Generating filtered ranked plots")
|
message("Generating filtered ranked plots")
|
||||||
generate_and_save_plots(
|
generate_and_save_plots(
|
||||||
output_dir = out_dir,
|
out_dir = out_dir,
|
||||||
file_name = "RankPlots_na_rm",
|
file_name = "RankPlots_na_rm",
|
||||||
plot_configs = rank_plot_filtered_configs,
|
plot_configs = rank_plot_filtered_configs,
|
||||||
grid_layout = list(ncol = 3, nrow = 2))
|
grid_layout = list(ncol = 3, nrow = 2))
|
||||||
@@ -1461,7 +1428,7 @@ main <- function() {
|
|||||||
is_lm = TRUE
|
is_lm = TRUE
|
||||||
)
|
)
|
||||||
generate_and_save_plots(
|
generate_and_save_plots(
|
||||||
output_dir = out_dir,
|
out_dir = out_dir,
|
||||||
file_name = "RankPlots_lm_na_rm",
|
file_name = "RankPlots_lm_na_rm",
|
||||||
plot_configs = rank_plot_lm_filtered_configs,
|
plot_configs = rank_plot_lm_filtered_configs,
|
||||||
grid_layout = list(ncol = 3, nrow = 2))
|
grid_layout = list(ncol = 3, nrow = 2))
|
||||||
@@ -1469,7 +1436,7 @@ main <- function() {
|
|||||||
message("Generating correlation plots")
|
message("Generating correlation plots")
|
||||||
correlation_plot_configs <- generate_correlation_plot_configs(zscores_interactions_filtered)
|
correlation_plot_configs <- generate_correlation_plot_configs(zscores_interactions_filtered)
|
||||||
generate_and_save_plots(
|
generate_and_save_plots(
|
||||||
output_dir = out_dir,
|
out_dir = out_dir,
|
||||||
file_name = "Avg_Zscore_vs_lm_NA_rm",
|
file_name = "Avg_Zscore_vs_lm_NA_rm",
|
||||||
plot_configs = correlation_plot_configs,
|
plot_configs = correlation_plot_configs,
|
||||||
grid_layout = list(ncol = 2, nrow = 2))
|
grid_layout = list(ncol = 2, nrow = 2))
|
||||||
|
|||||||
Reference in New Issue
Block a user