library(tidyverse) library(here) library(httr) library(arrow) library(readxl) # genomic feature harmonization table ---- # see https://huggingface.co/datasets/BrentLab/yeast_genome_resources gene_table = arrow::open_dataset(here("data/genome_files/hf/features")) %>% as_tibble() ## there is a mislabeling in both regulator and target ## with YDR022C. The common name is given as CIS1. However, ## SGD reports YDR022C as ATG31. CIS1 systematic ID is YLR346C ## That is labeled as ATG31. ## This appears to be a swap ## regulator and geneSymbol LUG1 is actually YCR087C-A, which was ## made an alias but only documented on SGD, not actually in the ## GFF/GTF. This needs to be updated in the gene_table as an alias add_datatype_to_colnames = function(df, skip_indicies){ # Suffixes to append suffixes <- c("_M", "_A", "_pval") # Repeat the suffixes to match the length of my_vector repeated_suffixes <- rep(suffixes, length.out = length(colnames(df)[-skip_indicies])) # Append the suffixes to each element of my_vector modified_vector <- paste0(colnames(df)[-skip_indicies], repeated_suffixes) colnames(df)[-skip_indicies] = modified_vector # drop the first row, which is the "data type" row in the original data # where the entries are M, A and P_value. These entries are added to the # colname df[-1,] } get_clean_headers = function(path, skip_indicies = 1:3) { headers <- read_tsv(path, n_max = 1, name_repair = "minimal") %>% add_datatype_to_colnames(skip_indicies = skip_indicies) %>% colnames() # Replace " vs" (optionally followed by ".") with ";" headers <- str_replace(headers, " vs\\.? ", ";") # Find empty (or NA) headers and replace with X1, X2, ... empties <- which(is.na(headers) | headers == "") if (length(empties) > 0) { headers[empties] <- paste0("X", seq_along(empties)) } headers } read_in_kemmeren_data = function(path, ...){ read.delim(path, sep='\t', skip=2, check.names=FALSE, col.names=get_clean_headers(path, ...)) %>% as_tibble() } stopifnot(identical(get_clean_headers(here('data/kemmeren/deleteome_all_mutants_ex_wt_var_controls.txt.xz')), get_clean_headers(here('data/kemmeren/deleteome_all_mutants_controls.txt.xz')))) deleteome_all_mutants_controls = read_in_kemmeren_data(here('data/kemmeren/deleteome_all_mutants_controls.txt.xz')) deleteome_ex_wt_var_controls = read_in_kemmeren_data(here('data/kemmeren/deleteome_all_mutants_ex_wt_var_controls.txt.xz')) by_hand_locustag_map = tibble( systematicName = c('YAR062W', 'YDL038C', 'snR10', 'YGR272C', 'YIL080W', 'YIL168W', 'YIR044C'), locus_tag = c('YAR061W', 'YDL039C', 'YNCG0013W', 'YGR271C-A', 'YIL082W-A', 'YIL167W', 'YIR043C')) %>% deframe() by_hand_symbol_map = gene_table %>% filter(locus_tag %in% by_hand_locustag_map) %>% select(locus_tag, symbol) %>% deframe() # note that by using the target_locus_tag and target_symbol, # the YCR087C-A, YLR352W nomenclature is fixed (in original, # YCR087C-A was called LUG1, but that name was removed in 2012 per SGD.) # Additionally, the YDR022C/CIS1 error is corrected by using target_locus_tag # and target_symbol, and aligns with the deletion evidence (the TF labelled # ATG31/YDR022C is KOed at YDR022C, not YLR346C, which is what is # currently labeled CIS1) target_df = deleteome_all_mutants_controls %>% select(reporterId, systematicName, geneSymbol) %>% distinct() %>% left_join(select(gene_table, locus_tag, symbol) %>% mutate(systematicName = locus_tag)) %>% mutate(locus_tag = ifelse(is.na(locus_tag), by_hand_locustag_map[systematicName], locus_tag)) %>% mutate(symbol = ifelse(is.na(symbol), by_hand_symbol_map[locus_tag], symbol)) %>% group_by(locus_tag) %>% mutate(multiple_probes = n()>1) %>% ungroup() %>% mutate(variable_in_wt = reporterId %in% setdiff(deleteome_all_mutants_controls$reporterId, deleteome_ex_wt_var_controls$reporterId)) %>% dplyr::rename(target_locus_tag = locus_tag, target_symbol = symbol) rm(deleteome_ex_wt_var_controls) gc() deleteome_all_mutants_controls_long = deleteome_all_mutants_controls %>% pivot_longer(-c(reporterId, systematicName, geneSymbol), names_to='sample_metric', values_to='values') %>% separate(sample_metric, c('sample', 'metric'), sep="_") %>% pivot_wider(names_from='metric', values_from='values') %>% separate_wider_delim(cols=sample, names=c('kemmeren_regulator', 'control'), delim=";") %>% mutate(kemmeren_regulator = toupper(str_remove(tolower(kemmeren_regulator), "-del-1$|-del-mata$|-del$"))) %>% mutate(kemmeren_regulator = ifelse(kemmeren_regulator == "ARG5,6", "ARG56", kemmeren_regulator)) kem_sup1_regulator_info = read_excel(here("data/kemmeren/supplemental_table1_strain_info.xlsx")) %>% mutate(`profile first published` = str_replace(`profile first published`, ", ", ",")) kem_sup1_regulator_info_straininfo = read_excel(here("data/kemmeren/supplemental_table1_strain_info_origins.xlsx")) kem_sup1_regulator_info = kem_sup1_regulator_info %>% left_join(kem_sup1_regulator_info_straininfo) %>% mutate(`profile first published` = citation) %>% select(-citation) parsed_regulators = deleteome_all_mutants_controls_long %>% select(kemmeren_regulator) %>% distinct() regulators_munging_list = list() regulators_munging_list$x1 = parsed_regulators %>% mutate(gene = kemmeren_regulator) %>% left_join(kem_sup1_regulator_info) %>% filter(complete.cases(.)) regulators_munging_list$x2 = parsed_regulators %>% filter(!kemmeren_regulator %in% regulators_munging_list$x1$kemmeren_regulator) %>% mutate(`orf name` = kemmeren_regulator) %>% left_join(kem_sup1_regulator_info) %>% filter(complete.cases(.)) stopifnot(length(intersect(regulators_munging_list$x1$kemmeren_regulator, regulators_munging_list$x2)) == 0) regulators_munging_list$x3 = read_csv(here("data/kemmeren/supplement_failure_regulator_mapping.csv.gz")) stopifnot(length(intersect(regulators_munging_list$x2$kemmeren_regulator, regulators_munging_list$x3$kemmeren_regulator)) == 0) regulators_munging_df = bind_rows(regulators_munging_list) %>% # the orf name for these two was the symbol mutate(`orf name` = case_when( `orf name` == "TLC1" ~ "YNCB0010W", `orf name` == "CMS1" ~ "YLR003C", .default = `orf name` )) %>% filter(kemmeren_regulator != "LUG1") %>% bind_rows(tibble( kemmeren_regulator = "LUG1", gene = "YCR087C-A", `orf name` = "YCR087C-A", description = paste0("Protein of unknown function; binds zinc; phosphomutants ", "exhibit phenotypes, suggesting functionality of phosphosites; green ", "fluorescent protein (GFP)-fusion protein localizes to the nucleolus; ", "YCR087C-A is not an essential gene"), `functional category` = "unknown", `slide(s)` = "THM_00005835_S01 / THM_00005836_S01", `mating type` = "MATalpha", `source of deletion mutant(s)` = "Open Biosystems / Open Biosystems", `primary Hybset(s)` = "THM006 / THM006", `responsive/non-responsive` = "responsive mutant", chase_notes = paste0("This was originally called LUG1. However, that name ", "for this locus was removed in 2012 per SGD. The expression confirms ", "that the KO locus is YCR087C-A, not YLR352W, which is the locus ", "currently called LUG1 in 2025"))) %>% left_join( select(gene_table, locus_tag, symbol) %>% mutate(`orf name` = locus_tag) %>% dplyr::rename(regulator_locus_tag = locus_tag, regulator_symbol = symbol)) %>% replace_na(list(chase_notes = "none")) %>% mutate(regulator_locus_tag = ifelse(str_detect(kemmeren_regulator, "^WT-"), kemmeren_regulator, regulator_locus_tag), regulator_symbol = ifelse(str_detect(kemmeren_regulator, "^WT-"), kemmeren_regulator, regulator_symbol)) %>% janitor::clean_names() stopifnot(setequal(regulators_munging_df$kemmeren_regulator, unique(deleteome_all_mutants_controls_long$kemmeren_regulator))) deleteome_all_mutants_svd_transforms = read_tsv(here("data/kemmeren/deleteome_all_mutants_svd_transformed.txt.xz"), name_repair = "minimal") colnames(deleteome_all_mutants_svd_transforms)[1] = "systematicName" colnames(deleteome_all_mutants_svd_transforms) = str_replace(colnames(deleteome_all_mutants_svd_transforms), "mf.alpha.1", "mf(alpha)1") colnames(deleteome_all_mutants_svd_transforms) = str_replace(colnames(deleteome_all_mutants_svd_transforms), "mf.alpha.2", "mf(alpha)2") colnames(deleteome_all_mutants_svd_transforms) = str_replace(colnames(deleteome_all_mutants_svd_transforms), "arg5.6", "arg56") deleteome_all_mutants_svd_transforms_long = deleteome_all_mutants_svd_transforms %>% dplyr::rename(geneSymbol = commonName) %>% pivot_longer(-c(systematicName, geneSymbol), names_to = "condition", values_to = "Madj") %>% separate_wider_delim(condition, names = c("kemmeren_regulator", "tmp"), delim = ".", too_many = "merge") %>% mutate(kemmeren_regulator = toupper(kemmeren_regulator)) %>% mutate( # these regulators are missing appropriate suffixes kemmeren_regulator = recode(kemmeren_regulator, "YIL014C" = "YIL014C-A", "YOL086W" = "YOL086W-A", "YDR034W" = "YDR034W-B", "YAL044W" = "YAL044W-A" ), # these targets are incorrectly labeled with symbols rather than systematic IDs systematicName = recode(systematicName, "ANR2" = "YKL047W", "CMS1" = "YLR003C" ) ) %>% # this is not in the other kemmeren data filter(systematicName != "Q0010") stopifnot(length(setdiff(deleteome_all_mutants_svd_transforms_long$kemmeren_regulator, regulators_munging_df$kemmeren_regulator)) == 0) stopifnot(length(setdiff(deleteome_all_mutants_svd_transforms_long$systematicName, target_df$systematicName)) == 0) final_parsed_list = list( all = deleteome_all_mutants_controls_long %>% select(reporterId, kemmeren_regulator, M, A, pval) %>% left_join(select(target_df, reporterId, target_locus_tag, target_symbol, multiple_probes, variable_in_wt)), slow_growth = deleteome_all_mutants_svd_transforms_long %>% select(kemmeren_regulator, systematicName, Madj) %>% # necessary to wrap in distinct to eliminate cases where there are two reporterId left_join(distinct(select(target_df, systematicName, target_locus_tag, target_symbol))) %>% select(-systematicName) ) final_parsed_df = Reduce(left_join, final_parsed_list) %>% group_by(kemmeren_regulator) %>% # since the slow growth removed data identifies records by systematicName # and not reporterId, there is a many-to-many join and one reporterId is # duplicated to mulitple Madj. This removes those duplicates distinct(reporterId, .keep_all = TRUE) %>% ungroup() %>% left_join(select(regulators_munging_df, -c(gene, `orf_name`))) %>% dplyr::rename(nr_sign_changes = nr_sign_changes_p_0_05_fc_1_7, primary_hybsets = primary_hybset_s, source_of_deletion_mutants = source_of_deletion_mutant_s, slides = slide_s, regulator_desc = description) %>% arrange(regulator_locus_tag) # select(regulator_locus_tag, regulator_symbol, reporterId, # target_locus_tag, target_symbol, M, Madj, A, pval, # variable_in_wt, multiple_probes) db_kemmeren_meta = read_csv("data/kemmeren/db_kemmeren_meta_20251126.csv") %>% mutate(id = ifelse(regulator_locus_tag == 'YLR352W', 0, id)) %>% select(id, regulator_locus_tag) %>% distinct() %>% mutate(id = as.integer(id)) %>% dplyr::rename(db_id = id) final_df_parsed_with_ids = final_parsed_df %>% left_join(db_kemmeren_meta) %>% replace_na(list(db_id = 0)) %>% arrange(regulator_locus_tag) %>% group_by(regulator_locus_tag) %>% mutate(sample_id = cur_group_id()) %>% relocate(sample_id, db_id, regulator_locus_tag, regulator_symbol, reporterId, target_locus_tag, target_symbol, M, Madj, A, pval, variable_in_wt, multiple_probes) # note! verify before overwriting that the sample_id for the unique sample # tuple is the same as it is in the current hackett_2020, or that any changes # are intentional # final_df_parsed_with_ids %>% # write_parquet("~/code/hf/kemmeren_2014/kemmeren_2014.parquet", # compression = "zstd", # chunk_size = 6181, # write_statistics = TRUE, # use_dictionary = c( # regulator_locus_tag = TRUE, # target_locus_tag = TRUE # ) # )