harbison_2004 / scripts /parse_harbison_data.R
cmatkhan's picture
Upload folder using huggingface_hub
ed1b6b1 verified
library(tidyverse)
library(readxl)
library(here)
library(arrow)
# genomic feature harmonization table ----
# see https://huggingface.co/datasets/BrentLab/yeast_genome_resources
genes_table = arrow::open_dataset(here("data/genome_files/hf/features")) %>%
as_tibble()
# function to read in the harbison raw data ----
read_in_harbison_data = function(xls_path, sheet_name, values_colname, n_max=6229){
read_excel(xls_path,
sheet=sheet_name,
skip=1,
n_max = n_max) %>%
dplyr::rename(locus_tag=`...1`,
gene_name=`...2`,
description=`...3`) %>%
mutate(across(!c(locus_tag, gene_name, description), as.numeric)) %>%
pivot_longer(!c(locus_tag, gene_name, description),
names_to='tf_cond',
values_to=values_colname) %>%
separate(tf_cond, c('tf', 'condition'), sep="_")
}
# read in the raw pvalue data ----
# note that n_max is set to 6229, which is the end of the genomic data. on some
# sheets, there is data accidently left in by the original researcher
harbison_pval_df_list = list(
ypd = read_in_harbison_data(
here('data/harbison/pvalbygene_forpaper_abbr.xls'),
'YPD',
'pvalue'),
other_conds = read_in_harbison_data(
here('data/harbison/pvalbygene_forpaper_abbr.xls'),
"other conditions",
"pvalue")
)
harbison_pval_df = bind_rows(harbison_pval_df_list)
# read in the raw effect ratio data ----
harbison_effect_df_list = list(
ypd = read_in_harbison_data(
here('data/harbison/ratiobygene_forpaper_abbr.xls'),
'YPD',
"effect"),
other_conds = read_in_harbison_data(
here('data/harbison/ratiobygene_forpaper_abbr.xls'),
'Other Conditions',
'effect'))
harbison_effect_df = bind_rows(harbison_effect_df_list)
# combine the effect and pvalue data ----
combined_harbison = harbison_effect_df %>%
select(-gene_name) %>%
left_join(select(harbison_pval_df, -gene_name)) %>%
# remove the control sample and remove locus tags that correspond to
# now deleted ORFs (these weren't merged into other annotations, they were
# simply complely removed from the annotation set)
filter(!locus_tag %in% c('YCL006C', 'YCR103C', 'YER187W-A')) %>%
dplyr::rename(harbison_locus_tag = locus_tag) %>%
mutate(tf = trimws(toupper(tf)))
# create a lookup map from the harbison targets to SGD 3-1 ----
locus_tags = combined_harbison %>%
select(harbison_locus_tag) %>%
distinct() %>%
left_join(genes_table, by = c("harbison_locus_tag" = "locus_tag")) %>%
mutate(locus_tag = harbison_locus_tag) %>%
select(harbison_locus_tag, locus_tag, symbol)
locus_tags_aliases = read_csv('data/harbison/locus_tags_aliases.csv') %>%
left_join(genes_table, by = c('curr_notation'='locus_tag')) %>%
dplyr::rename(locus_tag = curr_notation) %>%
select(harbison_locus_tag, locus_tag, symbol)
locus_tags_complete = locus_tags %>%
filter(!is.na(symbol)) %>%
bind_rows(locus_tags_aliases) %>%
dplyr::rename(target_locus_tag = locus_tag,
target_symbol = symbol)
stopifnot(nrow(locus_tags_complete) == nrow(locus_tags))
stopifnot(setequal(unique(combined_harbison$harbison_locus_tag),
locus_tags_complete$harbison_locus_tag))
# create a lookup map from the regulator names to SGD 3-1 ----
tf_symbol_to_locus_tag_df = combined_harbison %>%
select(tf) %>%
mutate(tf = trimws(toupper(tf))) %>%
distinct() %>%
left_join(genes_table, by = c('tf' = 'symbol')) %>%
select(tf, locus_tag) %>%
dplyr::rename(regulator_locus_tag = locus_tag) %>%
mutate(regulator_symbol = tf) %>%
select(tf, regulator_symbol, regulator_locus_tag)
tf_locus_tag_to_locus_tag_df = tf_symbol_to_locus_tag_df %>%
filter(!complete.cases(.)) %>%
select(tf) %>%
left_join(genes_table, by = c('tf' = 'locus_tag')) %>%
filter(!is.na(symbol)) %>%
select(tf, symbol) %>%
dplyr::rename(regulator_symbol = symbol) %>%
mutate(regulator_locus_tag = tf)
tf_name_df_na = read_csv("data/harbison/tf_name_aliases.csv") %>%
left_join(genes_table) %>%
# tf_main is the current SGD 3-1 symbol
select(tf, tf_main, locus_tag) %>%
dplyr::rename(regulator_symbol = tf_main, regulator_locus_tag = locus_tag) %>%
select(tf, regulator_symbol, regulator_locus_tag)
harbison_tf_map = bind_rows(
tf_symbol_to_locus_tag_df[complete.cases(tf_symbol_to_locus_tag_df),],
tf_locus_tag_to_locus_tag_df,
tf_name_df_na
)
stopifnot(setequal(harbison_tf_map$tf, unique(combined_harbison$tf)))
# create the harmonized data ----
combined_harbison_harmonized = combined_harbison %>%
left_join(harbison_tf_map) %>%
left_join(locus_tags_complete) %>%
dplyr::rename(harbison_regulator=tf) %>%
select(-description)
harbison_db_meta <- read_csv("data/promotersetsig_db_20251127.csv",
col_types = cols(composite_binding = col_character()))
db_id_map = harbison_db_meta %>%
filter(source_name == "harbison_chip") %>%
select(id, regulator_locus_tag, condition) %>%
distinct() %>%
dplyr::rename(db_id = id) %>%
bind_rows(
tibble(
regulator_locus_tag = c("YSC0017"),
db_id = 0,
condition = c('YPD')
))
combined_harbison_harmonized_for_parquet = combined_harbison_harmonized %>%
select(regulator_locus_tag, regulator_symbol,
condition, target_locus_tag, target_symbol,
effect, pvalue) %>%
left_join(db_id_map) %>%
arrange(db_id) %>%
group_by(db_id) %>%
mutate(sample_id = cur_group_id()) %>%
ungroup() %>%
select(sample_id, db_id, regulator_locus_tag, regulator_symbol,
condition,
target_locus_tag, target_symbol,
effect, pvalue)
# combined_harbison_harmonized_for_parquet %>%
# write_parquet("~/code/hf/harbison_2004/harbison_2004.parquet",
# compression = "zstd",
# write_statistics = TRUE,
# chunk_size = 6226,
# use_dictionary = c(
# sample_id = TRUE,
# condition = TRUE,
# regulator_locus_tag = TRUE,
# regulator_symbol = TRUE,
# target_locus_tag = TRUE,
# target_symbol = TRUE
# )
# )
#
#
# combined_harbison_harmonized %>%
# select(harbison_locus_tag, target_locus_tag) %>%
# distinct() %>%
# write_csv("~/code/hf/harbison_2004/scripts/harbison_locus_tag_to_target_locus_tag.csv")
#
# combined_harbison_harmonized %>%
# select(harbison_regulator, regulator_locus_tag) %>%
# distinct() %>%
# write_csv("~/code/hf/harbison_2004/scripts/harbison_regulator_to_regulator_locus_tag.csv")