hu_2007_reimand_2010 / scripts /parse_hu_reiman_data.R
cmatkhan's picture
updating the parsing script accordingly
11f503b
library(tidyverse)
library(httr)
library(here)
library(arrow)
library(readxl)
## NOTES:
##
## Hu had a heat shock and tetracycline condition. Reimand says they
## use:
## Further analysis was performed separately for the following
## three cases: A) same-strain replicates (BY4741);
## B) replicates of different strains (BY4741 and S288C);
## c) replicates of the induced promoter strain (R1158+TET)
##
## which i interpreted to mean (given some inspection from the original Hu
## metadata), that it should be !heat_shock and !tetracycline except
## for the regulators which did not have those conditions, in which
## case I kept the tetracyclie condition.
# 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()
hu_data = list(
de = here("data/hu/gkq232_SuppTable1C_KOTargetGenes_Matrix_LogFoldChange.dat.xz"),
pval = here("data/hu/gkq232_SuppTable1B_KOTargetGenes_Matrix_PValue.dat.xz")
)
hu_de_target_genes = as.character(read_delim(hu_data$de,
delim=' ',
col_names = FALSE,
n_max = 1)[1,])
hu_de_target_genes = str_replace(hu_de_target_genes, "\\.", "-")
hu_de_colnames = c('hu_regulator', hu_de_target_genes)
hu_pval_target_genes = as.character(read_delim(hu_data$pval,
delim=' ',
col_names = FALSE,
n_max = 1)[1,])
hu_pval_target_genes = str_replace(hu_pval_target_genes, "\\.", "-")
hu_pval_colnames = c('hu_regulator', hu_pval_target_genes)
stopifnot(setequal(hu_pval_colnames, hu_de_colnames))
# Note: The warning is expected. this is being parsed
# correctly despite the warning
de_df = read.delim(
hu_data$de,
sep=' ',
col.names = hu_de_colnames,
check.names=FALSE) %>%
as_tibble()
# Note: The warning is expected. this is being parsed
# correctly despite the warning
pval_df = read.delim(
hu_data$pval,
sep=' ',
col.names = hu_pval_colnames,
check.names=FALSE) %>%
as_tibble()
hu_df = de_df %>%
pivot_longer(-hu_regulator,
names_to='hu_target',
values_to='effect') %>%
left_join(pval_df %>%
pivot_longer(-hu_regulator,
names_to='hu_target',
values_to='pval')) %>%
mutate(hu_regulator = toupper(hu_regulator)) %>%
# remove deleted orfs
filter(!hu_target %in% c('YER187W-A', 'YCR103C', 'YCL006C', "YIL080W"))
tf_map = list()
tf_map$init = hu_df %>%
distinct(hu_regulator) %>%
mutate(regulator_symbol = hu_regulator) %>%
# these are labeled with aliases in the hu data
mutate(regulator_symbol = case_when(
regulator_symbol == "CAF17" ~ "IBA57",
regulator_symbol == "RCS1" ~ "AFT1",
regulator_symbol == "RLR1" ~ "THO2",
regulator_symbol == "ZMS1" ~ "RSF2",
regulator_symbol == "RIS1" ~ "ULS1",
.default = regulator_symbol
)) %>%
left_join(gene_table %>%
select(symbol,locus_tag) %>%
dplyr::rename(regulator_locus_tag = locus_tag,
regulator_symbol = symbol)) %>%
mutate(regulator_symbol = ifelse(is.na(regulator_locus_tag),
NA,
regulator_symbol))
tf_map$locus_tags = tf_map$init %>%
filter(!complete.cases(.)) %>%
select(hu_regulator) %>%
mutate(regulator_locus_tag = hu_regulator) %>%
left_join(gene_table %>%
select(symbol,locus_tag) %>%
dplyr::rename(regulator_locus_tag = locus_tag,
regulator_symbol = symbol))
tf_map_df = bind_rows(
tf_map$init %>% filter(complete.cases(.)),
tf_map$locus_tags
)
stopifnot(setequal(tf_map$init$hu_regulator, tf_map_df$hu_regulator))
target_map = list()
target_map$init = hu_df %>%
distinct(hu_target) %>%
mutate(target_locus_tag = hu_target) %>%
left_join(select(gene_table, locus_tag, symbol)
%>% dplyr::rename(target_locus_tag = locus_tag,
target_symbol = symbol))
target_map$alias_df = gene_table %>%
mutate(hu_target = map_chr(alias, ~ {
alias_i <- .x
match <- keep(target_map$init %>%
filter(!complete.cases(.)) %>%
pull(hu_target),
~ str_detect(alias_i, .x))
if (length(match) > 0) match[[1]] else NA_character_
})) %>%
filter(!is.na(hu_target)) %>%
select(hu_target, locus_tag, symbol) %>%
dplyr::rename(target_locus_tag = locus_tag,
target_symbol = symbol)
target_map_df = bind_rows(target_map$init %>% filter(complete.cases(.)),
target_map$alias_df)
stopifnot(setequal(target_map_df$hu_target,
unique(hu_df$hu_target)))
final_df = hu_df %>%
left_join(tf_map_df) %>%
left_join(target_map_df)
# never used -- don't bother
# hu_reimand_from_db = read_csv("data/hu/hu_2007_reimand_db_20251127.csv")
# from https://www.nature.com/articles/ng2012#Sec10
supplement_table_1 = readxl::read_excel("data/hu/41588_2007_BFng2012_MOESM20_ESM.xlsx") %>%
janitor::clean_names() %>%
dplyr::rename(regulator_locus_tag = `tf_systematic_name`) %>%
select(-tf_gene_name) %>%
mutate(heat_shock = str_detect(growth_condition, "heat-shock"),
tetracycline_treatment = str_detect(growth_condition, "tetracycline treatment")) %>%
select(-growth_condition)
supplement_table_1_filt = supplement_table_1 %>%
filter(!heat_shock, !tetracycline_treatment)
supplement_table_1_metadata = supplement_table_1_filt %>%
bind_rows(
supplement_table_1 %>%
filter(
!heat_shock,
regulator_locus_tag %in%
setdiff(final_df$regulator_locus_tag,
supplement_table_1_filt$regulator_locus_tag)))
final_df_for_parquet = final_df %>%
mutate(sample_id = cur_group_id()) %>%
select(sample_id,
regulator_locus_tag, regulator_symbol,
target_locus_tag, target_symbol,
effect, pval) %>%
ungroup() %>%
arrange(regulator_locus_tag) %>%
left_join(supplement_table_1_metadata)
final_df_for_parquet %>%
write_parquet(here("~/code/hf/hu_2007_reimand_2010/hu_2007_reimand_2010.parquet"),
compression = "zstd",
chunk_size = 6249,
write_statistics = TRUE,
use_dictionary = c(
sample_id = TRUE,
regulator_locus_tag = TRUE,
regulator_symbol = TRUE,
target_locus_tag = TRUE,
target_symbol = TRUE,
heat_shock = TRUE,
tetracycline_treatment = TRUE
))