library(tidyverse) library(GEOquery) count_parent_dir = "~/htcf_lts/downloaded_data/mahendrawada_2024/GSE236948_RAW" counts_files = list.files(count_parent_dir, ".txt.gz") names(counts_files) = str_extract(counts_files, "GSM\\d+") genomic_features = arrow::read_parquet("~/code/hf/yeast_genome_resources/brentlab_features.parquet") test_count_df = read_tsv(file.path(count_parent_dir, counts_files[[1]]), col_names = c('orig_locus_tag', 'count')) find_matching_locus <- function(target, genomic_df) { match_idx <- which(str_detect(genomic_df$alias, fixed(target))) if (length(match_idx) == 0) { return(tibble(locus_tag = NA_character_, chr = NA_character_, start = NA_real_, end = NA_real_, strand = NA_character_)) } else if (length(match_idx) > 1) { # Handle multiple matches - take first or warn message(paste("Multiple matches for", target, "- taking first")) match_idx <- match_idx[1] } genomic_df %>% slice(match_idx) %>% select(locus_tag, chr, start, end, strand) } alias_matched_locus_tags <- test_count_df %>% mutate(target_locus_tag = case_when( orig_locus_tag == "LSR1" ~ "YNCB0019C", orig_locus_tag == "SCR1" ~ "YNCE0024W", orig_locus_tag == "snR18" ~ "YNCA0003W", orig_locus_tag == "snR19" ~ "YNCN0005C", orig_locus_tag == "snR3" ~ "YNCJ0030W", orig_locus_tag == "snR39" ~ "YNCG0014C", orig_locus_tag == "snR4" ~ "YNCE0019W", orig_locus_tag == "snR5" ~ "YNCO0028W", orig_locus_tag == "snR6" ~ "YNCL0006W", orig_locus_tag == "snR8" ~ "YNCO0026W", .default = orig_locus_tag)) %>% left_join(genomic_features, by = c('target_locus_tag' = 'locus_tag')) %>% filter(is.na(chr), str_detect(target_locus_tag, "^__", negate=TRUE)) %>% select(orig_locus_tag, target_locus_tag) %>% mutate(target_locus_tag = str_replace_all(target_locus_tag, "\\(|\\)", "_")) %>% mutate(match_info = map(target_locus_tag, ~find_matching_locus(.x, genomic_features))) %>% unnest(match_info) %>% select(orig_locus_tag, locus_tag) locus_tag_map = test_count_df %>% mutate(locus_tag = case_when( orig_locus_tag == "LSR1" ~ "YNCB0019C", orig_locus_tag == "SCR1" ~ "YNCE0024W", orig_locus_tag == "snR18" ~ "YNCA0003W", orig_locus_tag == "snR19" ~ "YNCN0005C", orig_locus_tag == "snR3" ~ "YNCJ0030W", orig_locus_tag == "snR39" ~ "YNCG0014C", orig_locus_tag == "snR4" ~ "YNCE0019W", orig_locus_tag == "snR5" ~ "YNCO0028W", orig_locus_tag == "snR6" ~ "YNCL0006W", orig_locus_tag == "snR8" ~ "YNCO0026W", .default = orig_locus_tag)) %>% filter(locus_tag %in% genomic_features$locus_tag) %>% select(-count) %>% bind_rows(alias_matched_locus_tags) %>% left_join(select(genomic_features, locus_tag, symbol)) %>% dplyr::rename(target_locus_tag = locus_tag, target_symbol = symbol) gse_soft <- getGEO("GSE236947", GSEMatrix = FALSE) # Extract all sample metadata including protocols samples <- lapply(GSMList(gse_soft), function(gsm) { meta <- Meta(gsm) data.frame( gsm_id = meta$geo_accession, title = meta$title, sra = { rel <- meta$relation sra_rel <- rel[grep("SRA", rel)] if(length(sra_rel) > 0) sub(".*term=", "", sra_rel[1]) else NA }, genotype = ifelse(!is.null(meta$characteristics_ch1), { # Extract genotype from characteristics char_lines <- meta$characteristics_ch1 genotype_line <- char_lines[grep("genotype:", char_lines)] if(length(genotype_line) > 0) { sub("genotype: ", "", genotype_line[1]) } else { NA } }, NA), growth_protocol = ifelse(!is.null(meta$growth_protocol), paste(meta$growth_protocol, collapse = " "), NA), treatment_protocol = ifelse(!is.null(meta$treatment_protocol), paste(meta$treatment_protocol, collapse = " "), NA), stringsAsFactors = FALSE ) }) metadata <- do.call(rbind, samples) %>% as_tibble() %>% dplyr::rename(sra_accession = sra) %>% # NOTE: this is a typo. It is correct in the # genotpye. There is no FHK2 mutate(title = str_replace(title, "FHK2", "FKH2")) %>% # only GSM8316241, 2, 3 have Gal4 rather than GAL4 in # the title. The genotypes are exactly the same as the # other mnase construct RNA-seq samples. This appears # to be a typo, so it is being corrected mutate(title = str_replace(title, "Gal4", "GAL4")) stopifnot(nrow(metadata) == length(counts_files)) metadata_parsed <- metadata %>% mutate( timepoint = str_extract(title, "_(10|30|60|120)_"), timepoint = as.integer(str_remove_all(timepoint, "_")), # Parse treatment_protocol into degron vs non-degron has_degron_system = str_detect(treatment_protocol, "DMSO|IAA"), # Extract degron treatment (DMSO vs IAA) degron_treatment = case_when( str_detect(title, "_DMSO_") ~ "DMSO", str_detect(title, "_3IAA_") ~ "IAA", TRUE ~ NA_character_ ), # Detect any IAA7-based degron tag has_degron_tag = str_detect(genotype, "IAA7|mini-N-deg|mini-degron"), # Extract degron variant degron_variant = case_when( str_detect(genotype, "mini-N-deg") ~ "mini_N_terminal_IAA7", str_detect(genotype, "IAA7") ~ "full_or_short_IAA7", TRUE ~ NA_character_ ), # Check for AID system components has_ostir1 = str_detect(genotype, "pGPD1-OSTIR"), # Parse genotype components # Extract TF-MNase fusion if present tf_mnase_fusion = str_extract(genotype, "[A-Z0-9]+(?=-MNase)"), # Extract regulator_symbol/TF name from title regulator_symbol = str_extract(title, "^[A-Z0-9]+(?=_)"), # Determine strain type strain_type = case_when( !is.na(regulator_symbol) ~ "degron_experiment", !is.na(tf_mnase_fusion) ~ "mnase_fusion", TRUE ~ "parent_strain" ), # Parse environmental condition from title env_condition = case_when( str_detect(title, "_SM_") ~ "SM", str_detect(title, "_no_gal_") ~ "no_galactose", # Add this first str_detect(title, "_gal_") ~ "galactose", str_detect(title, "_raf_") ~ "raffinose", str_detect(title, "_37") | str_detect(title, "heat") ~ "heat_shock_37C", TRUE ~ "standard_30C" ), # Determine if this is WT control is_wt_control = str_detect(title, "^WT_"), # Extract replicate replicate = str_extract(title, "(?<=_)[ABC](?=_)"), # Classify experiment type experiment_type = case_when( has_degron_system & degron_treatment %in% c("DMSO", "IAA") & regulator_symbol == "WT" ~ "wt_degron_control", has_degron_system & degron_treatment %in% c("DMSO", "IAA") & regulator_symbol != "WT" ~ "degron", !has_degron_system & !is.na(tf_mnase_fusion) ~ "mnase_fusion_rnaseq", !has_degron_system & is_wt_control ~ "wt_baseline", TRUE ~ "other" )) metadata_parsed_grouped = metadata_parsed %>% group_by(experiment_type) metadata_parsed_split = metadata_parsed_grouped %>% group_split(.keep=FALSE) names(metadata_parsed_split) = group_keys(metadata_parsed_grouped)$experiment_type # for the degron samples, if the timepoint is not specified, it is 30 minutes. # see mahnedrawada 2025 paper metadata_parsed_split$degron = metadata_parsed_split$degron %>% replace_na(list(timepoint = 30)) get_counts_add_meta = function(gsmid){ df = read_tsv( file.path(count_parent_dir, counts_files[[gsmid]]), col_names = c('orig_locus_tag', 'count')) counts_meta = df %>% filter(str_detect(orig_locus_tag, "^__")) %>% mutate(orig_locus_tag = str_remove(orig_locus_tag, "__")) %>% dplyr::rename(tmp = orig_locus_tag) %>% pivot_wider(names_from = tmp, values_from = count) %>% mutate(gsm_id = gsmid) %>% dplyr::relocate(gsm_id) counts_df = df %>% filter(str_detect(orig_locus_tag, "^__", negate=TRUE)) %>% left_join(locus_tag_map) %>% mutate(gsm_id = gsmid) %>% select(gsm_id, target_locus_tag, target_symbol, orig_locus_tag, count) list( meta = counts_meta, counts = counts_df ) } counts_parsed_list = map(metadata_parsed_split, ~{ temp_list = map(.x$gsm_id, get_counts_add_meta) list( meta = map_df(temp_list, "meta") %>% left_join(select(.x, gsm_id, sra_accession), by = "gsm_id") %>% dplyr::relocate(sra_accession, gsm_id), counts = map_df(temp_list, "counts") %>% left_join(select(.x, gsm_id, sra_accession), by = "gsm_id") %>% dplyr::relocate(sra_accession) %>% dplyr::select(-gsm_id) ) }) counts_common_cols = c('sra_accession', 'gsm_id', 'replicate', 'no_feature', 'ambiguous', 'too_low_aQual', 'alignment_not_unique') final_metadata_columns = list( degron = c('degron_treatment', 'degron_variant', 'regulator_symbol', 'env_condition', 'timepoint'), mnase_fusion_rnaseq = c('sra_accession', 'gsm_id', 'regulator_symbol', 'env_condition'), wt_baseline = c('env_condition'), wt_degron_control = c('degron_treatment') ) metadata_with_counts_meta = map(names(metadata_parsed_split), ~{ metadata_parsed_split[[.x]] %>% left_join(counts_parsed_list[[.x]]$meta) %>% select(all_of(c(final_metadata_columns[[.x]], counts_common_cols))) %>% dplyr::relocate(sra_accession, gsm_id) %>% dplyr::rename(gsm_accession = gsm_id) }) names(metadata_with_counts_meta) = names(metadata_parsed_split) set_names_grouping_col_list = list( wt_baseline = 'env_condition', wt_degron_control = 'degron_treatment', degron = c('degron_treatment', 'env_condition', 'regulator_symbol', 'timepoint'), mnase_fusion_rnaseq = c('regulator_symbol', 'env_condition')) prev_largest_sample_id = 0 metadata_with_counts_meta_with_sample_id = metadata_with_counts_meta for(n in names(set_names_grouping_col_list)){ metadata_with_counts_meta_with_sample_id[[n]] = metadata_with_counts_meta[[n]] %>% group_by(!!!syms(set_names_grouping_col_list[[n]])) %>% mutate(sample_id = cur_group_id() + prev_largest_sample_id) %>% arrange(sample_id) prev_largest_sample_id = max(metadata_with_counts_meta_with_sample_id[[n]]$sample_id) } metadata_with_counts_meta_with_sample_id$mnase_fusion_rnaseq = metadata_with_counts_meta_with_sample_id$mnase_fusion_rnaseq %>% left_join(select(genomic_features, symbol, locus_tag) %>% dplyr::rename(regulator_symbol = symbol, regulator_locus_tag = locus_tag)) %>% dplyr::relocate(sra_accession, gsm_accession, regulator_locus_tag, regulator_symbol) metadata_with_counts_meta_with_sample_id$degron = metadata_with_counts_meta_with_sample_id$degron %>% mutate(regulator_symbol = str_replace(regulator_symbol, "YNR063W", "PUL4")) %>% left_join(select(genomic_features, symbol, locus_tag) %>% dplyr::rename(regulator_symbol = symbol, regulator_locus_tag = locus_tag)) %>% dplyr::relocate(sra_accession, gsm_accession, regulator_locus_tag, regulator_symbol) for(n in names(metadata_with_counts_meta_with_sample_id)){ meta = list( path = file.path("/home/chase/code/hf/mahendrawada_2025", paste0(n, "_counts_meta.parquet")), data = metadata_with_counts_meta_with_sample_id[[n]] ) counts = list( path = file.path("/home/chase/code/hf/mahendrawada_2025", paste0(n, "_counts.parquet")), data = counts_parsed_list[[n]]$counts ) arrow::write_parquet( meta$data, meta$path, compression = "zstd", write_statistics = TRUE) arrow::write_parquet( counts$data, counts$path, chunk_size = 7036, compression = "zstd", write_statistics = TRUE, use_dictionary = c( sra_accession = TRUE)) }