library(tidyverse) library(DESeq2) library(arrow) library(here) run_degron_deseq2 <- function(regulator_sym, env_cond, timepoint_val, degron_data = degron, baseline_data = baseline_control) { # Filter degron data for the specified regulator, condition, and timepoint degron_meta_filtered <- degron_data$meta %>% filter( regulator_symbol == regulator_sym, env_condition == env_cond, timepoint == timepoint_val) %>% mutate(degron_treatment = factor(degron_treatment, levels = c("DMSO", "IAA"))) if (nrow(degron_meta_filtered) != 6) { stop(sprintf("We expect exactly 3 replicates in each conditition for a total of 6 samples. These parameters result in %s", nrow(degron_meta_filtered))) } # Get the IAA and DMSO samples iaa_samples <- degron_meta_filtered %>% filter(degron_treatment == "IAA") %>% pull(sra_accession) dmso_samples <- degron_meta_filtered %>% filter(degron_treatment == "DMSO") %>% pull(sra_accession) if (length(iaa_samples) != 3 || length(dmso_samples) != 3) { stop("Missing IAA or DMSO samples for the specified parameters") } # Get counts for these samples counts_filtered <- degron_data$counts_long %>% filter(sra_accession %in% c(iaa_samples, dmso_samples)) # Convert to wide format (genes x samples) counts_wide <- counts_filtered %>% select(target_locus_tag, sra_accession, count) %>% pivot_wider( names_from = sra_accession, values_from = count) %>% column_to_rownames("target_locus_tag") # Create sample metadata sample_meta <- degron_meta_filtered %>% select(sra_accession, degron_treatment) %>% column_to_rownames("sra_accession") # Ensure counts columns match metadata rows counts_wide <- counts_wide[, rownames(sample_meta)] # Create DESeq2 dataset dds <- DESeqDataSetFromMatrix( countData = counts_wide, colData = sample_meta, design = ~ degron_treatment ) # Run DESeq2 dds <- DESeq(dds) # Get results res <- results(dds) # Return both the DESeq object and results return(list( dds = dds, results = res %>% as_tibble(rownames="target_locus_tag") %>% mutate(sample_id = paste(unique(degron_meta_filtered$sample_id), collapse="_")) %>% left_join(distinct(degron$counts_long %>% select(target_locus_tag, target_symbol))) %>% mutate(regulator_locus_tag = unique(degron_meta_filtered$regulator_locus_tag), regulator_symbol = unique(degron_meta_filtered$regulator_symbol), env_condition = unique(degron_meta_filtered$env_condition), timepoint = unique(degron_meta_filtered$timepoint)) %>% dplyr::relocate(sample_id, regulator_locus_tag, regulator_symbol, env_condition, timepoint), metadata = degron_meta_filtered )) } baseline_control = list( counts_long = arrow::read_parquet( "~/code/hf/mahendrawada_2025/wt_baseline_counts.parquet"), meta = arrow::read_parquet( "~/code/hf/mahendrawada_2025/wt_baseline_counts_meta.parquet") ) degron = list( counts_long = arrow::read_parquet("~/code/hf/mahendrawada_2025/degron_counts.parquet"), meta = arrow::read_parquet("~/code/hf/mahendrawada_2025/degron_counts_meta.parquet") ) z = run_degron_deseq2("CYC8", 'standard_30C', 30, degron, baseline_control) x = map(unique(degron$meta$regulator_symbol), ~run_degron_deseq2(., 'standard_30C', 30, degron, baseline_control)) results_df <- map_dfr(x, ~.$results) # results_df %>% # write_parquet("~/code/hf/mahendrawada_2025/rnaseq_reprocessed.parquet", # compression = "zstd", # write_statistics = TRUE, # chunk_size = 6708, # use_dictionary = c( # sample_id = TRUE, # regulator_locus_tag = TRUE, # regulator_symbol = TRUE, # target_locus_tag = TRUE, # target_symbol = TRUE # ) # )