Simplify annotation code

This commit is contained in:
2024-09-15 12:48:36 -04:00
parent ee986fbd60
commit 6c13182961

View File

@@ -619,109 +619,104 @@ adjust_missing_and_rank <- function(df, variables) {
generate_interaction_plot_configs <- function(df, variables) { generate_interaction_plot_configs <- function(df, variables) {
configs <- list() configs <- list()
# Define common y-limits and other attributes for each variable dynamically # Define common y-limits for each variable
limits_map <- list(L = c(-65, 65), K = c(-65, 65), r = c(-0.65, 0.65), AUC = c(-6500, 6500)) limits_map <- list(
L = c(-65, 65),
# Define annotation positions based on the variable being plotted K = c(-65, 65),
annotation_positions <- list( r = c(-0.65, 0.65),
L = list(Z_Shift_L = 45, lm_ZScore = 25, NG = -25, DB = -35, SM = -45), AUC = c(-6500, 6500)
K = list(Z_Shift_K = 45, lm_ZScore = 25, NG = -25, DB = -35, SM = -45),
r = list(Z_Shift_r = 0.45, lm_ZScore = 0.25, NG = -0.25, DB = -0.35, SM = -0.45),
AUC = list(Z_Shift_AUC = 4500, lm_ZScore = 2500, NG = -2500, DB = -3500, SM = -4500)
) )
# Define which annotations to include for each plot # Define annotation positions and labels
annotation_positions <- list(
Z_Shift = 45,
Z_lm = 25,
NG = -25,
DB = -35,
SM = -45
)
# Define functions to generate annotation labels
annotation_labels <- list( annotation_labels <- list(
ZShift = function(df, var) { Z_Shift = function(df, var) {
val <- df[[paste0("Z_Shift_", var)]] val <- df[[paste0("Z_Shift_", var)]]
if (is.numeric(val)) { paste("ZShift =", round(val, 2))
paste("ZShift =", round(val, 2))
} else {
paste("ZShift =", val)
}
}, },
lm_ZScore = function(df, var) { Z_lm = function(df, var) {
val <- df[[paste0("Z_lm_", var)]] val <- df[[paste0("Z_lm_", var)]]
if (is.numeric(val)) { paste("lm ZScore =", round(val, 2))
paste("lm ZScore =", round(val, 2))
} else {
paste("lm ZScore =", val)
}
}, },
NG = function(df, var) paste("NG =", df$NG), NG = function(df, var) paste("NG =", df$NG),
DB = function(df, var) paste("DB =", df$DB), DB = function(df, var) paste("DB =", df$DB),
SM = function(df, var) paste("SM =", df$SM) SM = function(df, var) paste("SM =", df$SM)
) )
for (variable in variables) { for (variable in variables) {
# Dynamically generate the names of the columns # Get y-limits for the variable
var_info <- list( ylim_vals <- limits_map[[variable]]
ylim = limits_map[[variable]],
sd_col = paste0("WT_sd_", variable) # Extract precomputed linear model coefficients
)
# Extract the precomputed linear model coefficients
lm_line <- list( lm_line <- list(
intercept = df[[paste0("lm_intercept_", variable)]], intercept = df[[paste0("lm_intercept_", variable)]],
slope = df[[paste0("lm_slope_", variable)]] slope = df[[paste0("lm_slope_", variable)]]
) )
annotations <- lapply(names(annotation_positions[[variable]]), function(annotation_name) { # Generate annotations
message("Processing annotation: ", annotation_name, " for variable: ", variable) annotations <- lapply(names(annotation_positions), function(annotation_name) {
y_pos <- annotation_positions[[variable]][[annotation_name]] y_pos <- annotation_positions[[annotation_name]]
label_func <- annotation_labels[[annotation_name]]
# Check if the annotation_name exists in annotation_labels if (!is.null(label_func)) {
if (!is.null(annotation_labels[[annotation_name]])) { label <- label_func(df, variable)
label <- annotation_labels[[annotation_name]](df, variable)
list(x = 1, y = y_pos, label = label) list(x = 1, y = y_pos, label = label)
} else { } else {
message(paste("Warning: No annotation function found for", annotation_name)) message(paste("Warning: No annotation function found for", annotation_name))
NULL NULL
} }
}) })
# Filter out any NULL annotations # Remove NULL annotations
annotations <- Filter(Negate(is.null), annotations) annotations <- Filter(Negate(is.null), annotations)
# Add scatter plot configuration for this variable # Create scatter plot config
configs[[length(configs) + 1]] <- list( configs[[length(configs) + 1]] <- list(
df = df, df = df,
x_var = "conc_num_factor", x_var = "conc_num_factor",
y_var = variable, y_var = variable,
plot_type = "scatter", plot_type = "scatter",
title = sprintf("%s %s", df$OrfRep[1], df$Gene[1]), title = sprintf("%s %s", df$OrfRep[1], df$Gene[1]),
ylim_vals = var_info$ylim, ylim_vals = ylim_vals,
annotations = annotations, annotations = annotations,
lm_line = lm_line, # Precomputed linear model lm_line = lm_line,
error_bar = TRUE, error_bar = TRUE,
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]),
position = "jitter", position = "jitter",
coord_cartesian = c(0, max(var_info$ylim)) # You can customize this per plot as needed coord_cartesian = c(min(ylim_vals), max(ylim_vals))
) )
# Add box plot configuration for this variable # Create box plot config
configs[[length(configs) + 1]] <- list( configs[[length(configs) + 1]] <- list(
df = df, df = df,
x_var = "conc_num_factor", x_var = "conc_num_factor",
y_var = variable, y_var = variable,
plot_type = "box", plot_type = "box",
title = sprintf("%s %s (Boxplot)", df$OrfRep[1], df$Gene[1]), title = sprintf("%s %s (Boxplot)", df$OrfRep[1], df$Gene[1]),
ylim_vals = var_info$ylim, ylim_vals = ylim_vals,
annotations = annotations, annotations = annotations,
error_bar = FALSE, error_bar = FALSE,
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]),
coord_cartesian = c(0, max(var_info$ylim)) # Customize this as needed coord_cartesian = c(min(ylim_vals), max(ylim_vals))
) )
} }
return(configs) return(configs)
} }
generate_rank_plot_configs <- function(df, rank_var, zscore_var, var, is_lm = FALSE) { generate_rank_plot_configs <- function(df, rank_var, zscore_var, var, is_lm = FALSE) {
configs <- list() configs <- list()