hackett_2020 / scripts /parse_mcisaac_data.R
cmatkhan's picture
Update scripts/parse_mcisaac_data.R
a6d16ea verified
## NOTE: gene_table and genomicfeature_tbl are confirmed identical
library(tidyverse)
library(here)
library(arrow)
# 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()
# see the README in the mcisaac data directory
df = read_tsv(here("data/mcisaac/idea_tall_expression_data/idea_tall_expression_data.tsv")) %>%
# filter(!TF %in% c("GEV","Z3EV")) %>%
mutate(GeneName = str_remove(GeneName,',')) %>%
mutate(GeneName = str_remove(GeneName, '\''))
tf_table = df %>%
select(TF) %>%
distinct() %>%
filter(TF != 'YLL054C') %>%
left_join(gene_table, by = c('TF' = 'symbol')) %>%
select(TF, locus_tag) %>%
bind_rows(tibble(TF='YLL054C', locus_tag="YLL054C")) %>%
dplyr::rename(regulator_locus_tag = locus_tag) %>%
mutate(regulator_symbol = TF) %>%
mutate(regulator_locus_tag = ifelse(TF %in% c("GEV", "Z3EV"), TF, regulator_locus_tag))
stopifnot(setequal(tf_table$TF, unique(df$TF)))
mcisaac_gene_table_incomplete = df %>%
select(GeneName) %>%
distinct() %>%
left_join(gene_table, by = c('GeneName' = 'symbol')) %>%
filter(complete.cases(.)) %>%
mutate(symbol = GeneName) %>%
select(GeneName, locus_tag, symbol) %>%
bind_rows(df %>%
select(GeneName) %>%
distinct() %>%
left_join(gene_table,
by = c('GeneName' = 'locus_tag')) %>%
filter(complete.cases(.)) %>%
mutate(locus_tag = GeneName) %>%
select(GeneName, locus_tag, symbol)) %>%
distinct() %>%
dplyr::rename(target_locus_tag = locus_tag, target_symbol = symbol)
# df %>%
# select(GeneName) %>%
# distinct(GeneName) %>%
# filter(!GeneName %in% tmp$GeneName) %>%
# mutate(id=NA) %>%
# select(GeneName, id) %>%
# dplyr::rename(gene_id=id) %>%
# write_csv("data/mcisaac/mcisaac_gene_lookup.txt")
# This is sort of an interesting aside. I'm looking back at how I parsed the mcisaac dataset as I add it to huggingface. As I do this, I am reminded of little details like the fact that in their probe set, they have a prob for AAD6 and AAD16 . In the current annotation set, it is noted that AAD16 is a -1 frameshift of AAD6 and it is removed. AAD6 itself is labeled a pseudogene.
# that made me think that what would be interesting is how correlated they are (plotted, generally correlated).
# I looked at the absolute of the differences wondering which genes showed the greatest difference -- possibly these two loci are differentially regulated, who knows.
# More interesting, I looked at regulators where the signs differed for AAD6 and AAD16. Possibly this is due to differential regulation. But it is also possible that this might give insight into what the minimum reliable threshold of the effect is.
# The maximum unshrunken effect of a pair with different signs was 0.54. For DTO we set a threshold of 0.6, so this isn't really useful in any way. But I thought I'd share anyway
mcisaac_gene_aliases = read_csv("data/mcisaac/mcisaac_gene_lookup.txt") %>%
# this is a -1 frameshift to AAD6 (also in mcisaac) and is not in 3-1. Added
# by hand -- this * might * be an interesting locus to look at for variance?
bind_rows(tibble(GeneName="AAD16", locus_tag="YFL056C")) %>%
dplyr::rename(target_locus_tag=locus_tag) %>%
mutate(target_symbol=GeneName)
mcisaac_gene_table = mcisaac_gene_table_incomplete %>%
bind_rows(mcisaac_gene_aliases)
stopifnot(setequal(mcisaac_gene_table$GeneName, unique(df$GeneName)))
mcisaac_for_parquet = df %>%
mutate(date=str_remove_all(as.Date(date, format='%m/%d/%Y'),'-')) %>%
left_join(tf_table) %>%
left_join(mcisaac_gene_table)
mcisaac_tf_to_regulator_locus_tag = mcisaac_for_parquet %>%
select(TF, regulator_locus_tag) %>%
distinct()
mcisaac_GeneName_to_target_locus_tag = mcisaac_for_parquet %>%
select(GeneName, target_locus_tag) %>%
distinct()
# mcisaac_for_parquet %>%
# select(regulator_locus_tag, time, mechanism, restriction, date, strain,
# target_locus_tag, ends_with("median"), starts_with("log2")) %>%
# group_by(regulator_locus_tag, time, mechanism, restriction, date, strain) %>%
# write_dataset(path = here("data/mcisaac/hf/data"), format = "parquet")
db_mcisaac_data = read_csv("data/mcisaac/database_mcisaac_20251126.csv") %>%
select(id, regulator_locus_tag, mechanism, restriction, time, notes) %>%
mutate(
strain = trimws(str_extract(notes, "(?<=strain_id:)[^;]+")),
date = trimws(str_extract(notes, "(?<=date:)[^;]+")),
mechanism = toupper(mechanism)) %>%
select(-notes) %>%
dplyr::rename(db_id = id) %>%
bind_rows(
mcisaac_for_parquet %>%
filter(regulator_locus_tag %in% c("GEV", "Z3EV")) %>%
select(regulator_locus_tag, time, mechanism,
restriction, date, strain) %>%
distinct() %>%
mutate(db_id = 0)) %>%
arrange(db_id) %>%
mutate(sample_id = row_number())
final_mcisaac = mcisaac_for_parquet %>%
left_join(db_mcisaac_data) %>%
select(sample_id, db_id,
regulator_locus_tag, regulator_symbol,
target_locus_tag, target_symbol,
time, mechanism, restriction, date, strain,
ends_with("median"), starts_with("log2"))
# 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_mcisaac %>%
# write_parquet("~/code/hf/hackett_2020/hackett_2020.parquet",
# compression = "zstd",
# chunk_size = 6175,
# write_statistics = TRUE,
# use_dictionary = c(
# sample_id = TRUE,
# regulator_locus_tag = TRUE,
# regulator_symbol = TRUE,
# target_locus_tag = TRUE,
# target_symbol = TRUE,
# time = TRUE,
# mechanism = TRUE,
# restriction = TRUE,
# date = TRUE,
# strain = TRUE))