suppressMessages({ library("dplyr") library("data.table") library("readr") library("stringr") }) # Function to parse arguments parse_arguments <- function() { if (interactive()) { args <- c( "/home/bryan/documents/develop/hartmanlab/workflow/out/20240116_jhartman2_DoxoHLD", 3, # sd value "/home/bryan/documents/develop/hartmanlab/workflow/out/20240116_jhartman2_DoxoHLD/20240822_jhartman2_DoxoHLD/exp1", "Experiment 1: Doxo versus HLD", "/home/bryan/documents/develop/hartmanlab/workflow/out/20240116_jhartman2_DoxoHLD/20240822_jhartman2_DoxoHLD/exp2", "Experiment 2: HLD versus Doxo" ) } else { args <- commandArgs(trailingOnly = TRUE) } out_dir <- normalizePath(args[1], mustWork = FALSE) sd <- as.numeric(args[2]) paths <- normalizePath(args[seq(3, length(args), by = 2)], mustWork = FALSE) names <- args[seq(4, length(args), by = 2)] experiments <- setNames(paths, names) list( out_dir = out_dir, sd = sd, experiments = experiments ) } args <- parse_arguments() # Ensure main output directory exists dir.create(args$out_dir, showWarnings = FALSE, recursive = TRUE) # Function to read and combine z-score interaction files combine_zscores <- function(experiments, out_dir) { combined_data <- lapply(names(experiments), function(exp_name) { exp_dir <- experiments[[exp_name]] zscore_file <- file.path(exp_dir, "zscores", "zscore_interactions.csv") if (!file.exists(zscore_file)) { stop("Z-score file does not exist for ", exp_name, " at ", zscore_file) } message("Reading z-score file for ", exp_name, " from ", zscore_file) data <- fread(zscore_file) data$Experiment <- exp_name return(data) }) %>% bind_rows() combined_output_file <- file.path(out_dir, "combined_zscores.csv") fwrite(combined_data, combined_output_file, row.names = FALSE) message("Combined z-score file saved to: ", combined_output_file) } # Function to read and combine summary statistics files combine_summary_stats <- function(experiments, out_dir) { combined_stats <- lapply(names(experiments), function(exp_name) { exp_dir <- experiments[[exp_name]] summary_file <- file.path(exp_dir, "zscores", "summary_stats_all_strains.csv") if (!file.exists(summary_file)) { stop("Summary stats file does not exist for ", exp_name, " at ", summary_file) } message("Reading summary stats file for ", exp_name, " from ", summary_file) data <- fread(summary_file) data$Experiment <- exp_name return(data) }) %>% bind_rows() combined_output_file <- file.path(out_dir, "combined_summary_stats.csv") fwrite(combined_stats, combined_output_file, row.names = FALSE) message("Combined summary stats file saved to: ", combined_output_file) } # Function to generate final summary report generate_final_report <- function(out_dir) { combined_zscores <- file.path(out_dir, "combined_zscores.csv") combined_stats <- file.path(out_dir, "combined_summary_stats.csv") if (!file.exists(combined_zscores) || !file.exists(combined_stats)) { stop("Combined z-scores or summary stats files do not exist.") } zscores_data <- fread(combined_zscores) stats_data <- fread(combined_stats) message("Merging z-score and summary stats data...") final_report <- merge(zscores_data, stats_data, by = c("OrfRep", "Experiment"), all = TRUE, allow.cartesian = TRUE) final_report_file <- file.path(out_dir, "final_combined_report.csv") fwrite(final_report, final_report_file, row.names = FALSE) message("Final combined report saved to: ", final_report_file) } # Process all experiments and generate outputs combine_zscores(args$experiments, args$out_dir) combine_summary_stats(args$experiments, args$out_dir) generate_final_report(args$out_dir)