Reformat rank plot configs
This commit is contained in:
@@ -721,140 +721,132 @@ generate_interaction_plot_configs <- function(df, variables) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
generate_rank_plot_configs <- function(df_filtered, variables, is_lm = FALSE) {
|
generate_rank_plot_configs <- function(df_filtered, variables, is_lm = FALSE) {
|
||||||
# Define SD bands
|
|
||||||
sd_bands <- c(1, 2, 3)
|
|
||||||
|
|
||||||
# Initialize list to store plot configurations
|
sd_bands <- c(1, 2, 3)
|
||||||
configs <- list()
|
|
||||||
|
|
||||||
# SD-based plots for L and K
|
configs <- list()
|
||||||
for (variable in c("L", "K")) {
|
|
||||||
for (sd_band in sd_bands) {
|
|
||||||
# Determine columns based on whether it's lm or not
|
|
||||||
if (is_lm) {
|
|
||||||
rank_var <- paste0(variable, "_Rank_lm")
|
|
||||||
zscore_var <- paste0("Z_lm_", variable)
|
|
||||||
y_label <- paste("Int Z score", variable)
|
|
||||||
} else {
|
|
||||||
rank_var <- paste0(variable, "_Rank")
|
|
||||||
zscore_var <- paste0("Avg_Zscore_", variable)
|
|
||||||
y_label <- paste("Avg Z score", variable)
|
|
||||||
}
|
|
||||||
|
|
||||||
# Calculate counts for annotations
|
# SD-based plots for L and K
|
||||||
num_enhancers <- sum(df_filtered[[zscore_var]] >= sd_band, na.rm = TRUE)
|
for (variable in c("L", "K")) {
|
||||||
num_suppressors <- sum(df_filtered[[zscore_var]] <= -sd_band, na.rm = TRUE)
|
for (sd_band in sd_bands) {
|
||||||
|
# Determine columns based on whether it's lm or not
|
||||||
|
if (is_lm) {
|
||||||
|
rank_var <- paste0(variable, "_Rank_lm")
|
||||||
|
zscore_var <- paste0("Z_lm_", variable)
|
||||||
|
y_label <- paste("Int Z score", variable)
|
||||||
|
} else {
|
||||||
|
rank_var <- paste0(variable, "_Rank")
|
||||||
|
zscore_var <- paste0("Avg_Zscore_", variable)
|
||||||
|
y_label <- paste("Avg Z score", variable)
|
||||||
|
}
|
||||||
|
|
||||||
# Annotated Plot Configuration
|
num_enhancers <- sum(df_filtered[[zscore_var]] >= sd_band, na.rm = TRUE)
|
||||||
configs[[length(configs) + 1]] <- list(
|
num_suppressors <- sum(df_filtered[[zscore_var]] <= -sd_band, na.rm = TRUE)
|
||||||
df = df_filtered,
|
|
||||||
x_var = rank_var,
|
|
||||||
y_var = zscore_var,
|
|
||||||
plot_type = "scatter",
|
|
||||||
title = paste(y_label, "vs. Rank for", variable, "above", sd_band, "SD"),
|
|
||||||
sd_band = sd_band,
|
|
||||||
annotations = list(
|
|
||||||
list(
|
|
||||||
x = median(df_filtered[[rank_var]], na.rm = TRUE),
|
|
||||||
y = 10,
|
|
||||||
label = paste("Deletion Enhancers =", num_enhancers)
|
|
||||||
),
|
|
||||||
list(
|
|
||||||
x = median(df_filtered[[rank_var]], na.rm = TRUE),
|
|
||||||
y = -10,
|
|
||||||
label = paste("Deletion Suppressors =", num_suppressors)
|
|
||||||
)
|
|
||||||
),
|
|
||||||
sd_band_values = sd_band,
|
|
||||||
shape = 3,
|
|
||||||
size = 0.1,
|
|
||||||
y_label = y_label,
|
|
||||||
x_label = "Rank",
|
|
||||||
legend_position = "none"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Non-Annotated Plot Configuration
|
# Annotated plot configuration
|
||||||
configs[[length(configs) + 1]] <- list(
|
configs[[length(configs) + 1]] <- list(
|
||||||
df = df_filtered,
|
df = df_filtered,
|
||||||
x_var = rank_var,
|
x_var = rank_var,
|
||||||
y_var = zscore_var,
|
y_var = zscore_var,
|
||||||
plot_type = "scatter",
|
plot_type = "scatter",
|
||||||
title = paste(y_label, "vs. Rank for", variable, "above", sd_band, "SD No Annotations"),
|
title = paste(y_label, "vs. Rank for", variable, "above", sd_band, "SD"),
|
||||||
sd_band = sd_band,
|
sd_band = sd_band,
|
||||||
annotations = NULL,
|
annotations = list(
|
||||||
sd_band_values = sd_band,
|
list(
|
||||||
shape = 3,
|
x = median(df_filtered[[rank_var]], na.rm = TRUE),
|
||||||
size = 0.1,
|
y = 10,
|
||||||
y_label = y_label,
|
label = paste("Deletion Enhancers =", num_enhancers)
|
||||||
x_label = "Rank",
|
),
|
||||||
legend_position = "none"
|
list(
|
||||||
)
|
x = median(df_filtered[[rank_var]], na.rm = TRUE),
|
||||||
}
|
y = -10,
|
||||||
|
label = paste("Deletion Suppressors =", num_suppressors)
|
||||||
|
)
|
||||||
|
),
|
||||||
|
sd_band_values = sd_band,
|
||||||
|
shape = 3,
|
||||||
|
size = 0.1,
|
||||||
|
y_label = y_label,
|
||||||
|
x_label = "Rank",
|
||||||
|
legend_position = "none"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Non-Annotated Plot Configuration
|
||||||
|
configs[[length(configs) + 1]] <- list(
|
||||||
|
df = df_filtered,
|
||||||
|
x_var = rank_var,
|
||||||
|
y_var = zscore_var,
|
||||||
|
plot_type = "scatter",
|
||||||
|
title = paste(y_label, "vs. Rank for", variable, "above", sd_band, "SD No Annotations"),
|
||||||
|
sd_band = sd_band,
|
||||||
|
annotations = NULL,
|
||||||
|
sd_band_values = sd_band,
|
||||||
|
shape = 3,
|
||||||
|
size = 0.1,
|
||||||
|
y_label = y_label,
|
||||||
|
x_label = "Rank",
|
||||||
|
legend_position = "none"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
# Avg ZScore and Rank Avg ZScore Plots for r, L, K, and AUC
|
# Avg ZScore and Rank Avg ZScore Plots for r, L, K, and AUC
|
||||||
for (variable in variables) {
|
for (variable in variables) {
|
||||||
for (plot_type in c("Avg_Zscore_vs_lm", "Rank_Avg_Zscore_vs_lm")) {
|
for (plot_type in c("Avg_Zscore_vs_lm", "Rank_Avg_Zscore_vs_lm")) {
|
||||||
# Define x and y variables based on plot type
|
# Define specific variables based on plot type
|
||||||
if (plot_type == "Avg_Zscore_vs_lm") {
|
if (plot_type == "Avg_Zscore_vs_lm") {
|
||||||
x_var <- paste0("Avg_Zscore_", variable)
|
x_var <- paste0("Avg_Zscore_", variable)
|
||||||
y_var <- paste0("Z_lm_", variable)
|
y_var <- paste0("Z_lm_", variable)
|
||||||
title_suffix <- paste("Avg Zscore vs lm", variable)
|
title_suffix <- paste("Avg Zscore vs lm", variable)
|
||||||
|
rectangles <- list(
|
||||||
|
list(xmin = -2, xmax = 2, ymin = -2, ymax = 2,
|
||||||
|
fill = NA, color = "grey20", alpha = 0.1
|
||||||
|
)
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
x_var <- paste0(variable, "_Rank")
|
||||||
|
y_var <- paste0(variable, "_Rank_lm")
|
||||||
|
title_suffix <- paste("Rank Avg Zscore vs lm", variable)
|
||||||
|
rectangles <- NULL
|
||||||
|
}
|
||||||
|
|
||||||
# Add rectangles configuration for Avg_Zscore_vs_lm
|
# Fit linear model
|
||||||
rectangles <- list(
|
lm_fit <- lm(df_filtered[[y_var]] ~ df_filtered[[x_var]], data = df_filtered)
|
||||||
list(
|
|
||||||
xmin = -2, xmax = 2, ymin = -2, ymax = 2,
|
|
||||||
fill = NA, color = "grey20", alpha = 0.1
|
|
||||||
)
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
x_var <- paste0(variable, "_Rank")
|
|
||||||
y_var <- paste0(variable, "_Rank_lm")
|
|
||||||
title_suffix <- paste("Rank Avg Zscore vs lm", variable)
|
|
||||||
rectangles <- NULL
|
|
||||||
}
|
|
||||||
|
|
||||||
# Fit linear model
|
# Check for perfect fit
|
||||||
lm_fit <- lm(df_filtered[[y_var]] ~ df_filtered[[x_var]], data = df_filtered)
|
if (summary(lm_fit)$sigma == 0) {
|
||||||
|
next # Skip this iteration if the fit is perfect
|
||||||
|
}
|
||||||
|
|
||||||
# Check for perfect fit
|
r_squared <- summary(lm_fit)$r.squared
|
||||||
if (summary(lm_fit)$sigma == 0) {
|
|
||||||
next # Skip this iteration if the fit is perfect
|
|
||||||
}
|
|
||||||
|
|
||||||
# Calculate R-squared
|
configs[[length(configs) + 1]] <- list(
|
||||||
r_squared <- summary(lm_fit)$r.squared
|
df = df_filtered,
|
||||||
|
x_var = x_var,
|
||||||
# Plot Configuration
|
y_var = y_var,
|
||||||
configs[[length(configs) + 1]] <- list(
|
plot_type = "scatter",
|
||||||
df = df_filtered,
|
title = title_suffix,
|
||||||
x_var = x_var,
|
annotations = list(
|
||||||
y_var = y_var,
|
list(
|
||||||
plot_type = "scatter",
|
x = 0,
|
||||||
title = title_suffix,
|
y = 0,
|
||||||
annotations = list(
|
label = paste("R-squared =", round(r_squared, 2))
|
||||||
list(
|
)
|
||||||
x = 0,
|
),
|
||||||
y = 0,
|
sd_band_values = NULL, # Not applicable
|
||||||
label = paste("R-squared =", round(r_squared, 2))
|
shape = 3,
|
||||||
)
|
size = 0.1,
|
||||||
),
|
add_smooth = TRUE,
|
||||||
sd_band_values = NULL, # Not applicable
|
lm_line = list(intercept = coef(lm_fit)[1], slope = coef(lm_fit)[2]),
|
||||||
shape = 3,
|
legend_position = "right",
|
||||||
size = 0.1,
|
color_var = "Overlap",
|
||||||
add_smooth = TRUE,
|
x_label = x_var,
|
||||||
lm_line = list(intercept = coef(lm_fit)[1], slope = coef(lm_fit)[2]),
|
y_label = y_var,
|
||||||
legend_position = "right",
|
rectangles = rectangles # Add rectangles configuration
|
||||||
color_var = "Overlap",
|
)
|
||||||
x_label = x_var,
|
|
||||||
y_label = y_var,
|
|
||||||
rectangles = rectangles # Add rectangles configuration
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return(configs)
|
return(configs)
|
||||||
}
|
}
|
||||||
|
|
||||||
generate_correlation_plot_configs <- function(df) {
|
generate_correlation_plot_configs <- function(df) {
|
||||||
@@ -1247,16 +1239,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_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)
|
||||||
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_boxplots)
|
||||||
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")
|
||||||
|
|||||||
Reference in New Issue
Block a user