rossi_2021 / scripts /parse_pugh_genomecov_5p.R
cmatkhan's picture
major reorganization and reprocessing. scripts added
c329a72
## NOTE: The data is currently on /lts/mblab/downloaded_data/barkai_checseq
## and the parquet dataset is on the brentlab-strides aws at s3://yeast-binding-perturbation-data/barkai_checkseq
library(tidyverse)
library(here)
library(arrow)
# genomic feature harmonization table ----
# see https://huggingface.co/datasets/BrentLab/yeast_genome_resources
genomefeatures = arrow::open_dataset(here("data/genome_files/hf/features")) %>%
as_tibble() %>%
mutate(rownum = row_number())
chrmap = read_csv("~/projects/parsing_yeast_database_data/data/genome_files/chrmap.csv.gz")
# scp -r chasem@login.htcf.wustl.edu:/lts/mblab/downloaded_data/chipexo/paper_mimic_tag_count_20250718 .
genomecov_files = list.files(here("data/chip_exo/paper_mimic_tag_count_20250718"), full.names = TRUE)
genomecov_df = tibble(
path = genomecov_files,
tmp = str_remove(basename(genomecov_files), ".mLb.mkD.sorted_r1_5p_cov.txt")) %>%
separate(tmp,c('sample', 'replicate'), extra = "merge", remove = FALSE) %>%
mutate(replicate = ifelse(sample != "control",
as.integer(str_extract(str_extract(replicate, "REP\\d"), "\\d")),
as.integer(str_extract(str_extract(replicate, "T\\d+"), "\\d+"))))
nf_core_meta = read_csv(here("data/chip_exo/nfcore_chipseq_full_samplesheet.csv")) %>%
group_by(sample) %>%
mutate(tmp = row_number()) %>%
ungroup() %>%
mutate(replicate = ifelse(sample == "control", tmp, replicate)) %>%
select(-tmp)
genomecov_df_meta = genomecov_df %>%
left_join(nf_core_meta)
pugh_genomecov_tmp <- genomecov_df_meta %>%
select(sample, replicate, run_accession, yeastepigenome_id, path) %>%
mutate(sample = tolower(sample)) %>%
left_join(
genomefeatures %>%
select(symbol, locus_tag) %>%
distinct() %>%
mutate(sample = tolower(symbol)),
by = "sample"
)
filled_missing_locus_tag = pugh_genomecov_tmp %>%
filter(!complete.cases(.)) %>%
mutate(
alias_rownum = map_int(sample, function(s) {
idx <- which(str_detect(genomefeatures$alias, fixed(toupper(s))))
if (length(idx) > 0) idx[1] else NA_integer_
}),
alias_rownum = ifelse(
is.na(alias_rownum) & str_starts(sample, "y"),
map_int(sample, function(s) {
idx <- which(str_detect(genomefeatures$locus_tag, fixed(toupper(s))))
if (length(idx) > 0) idx[1] else NA_integer_
}),
alias_rownum
)
) %>%
select(-c(symbol, locus_tag)) %>%
left_join(genomefeatures %>%
select(rownum, symbol, locus_tag) %>%
dplyr::rename(alias_rownum=rownum)) %>%
select(-alias_rownum)
pugh_genomecov_meta = pugh_genomecov_tmp %>%
filter(!is.na(locus_tag)) %>%
bind_rows(
filled_missing_locus_tag) %>%
mutate(regulator_symbol = symbol, regulator_locus_tag = locus_tag) %>%
arrange(regulator_locus_tag) %>%
group_by(regulator_locus_tag) %>%
mutate(sample_id = cur_group_id())
# pugh_genomecov_meta %>%
# select(regulator_locus_tag, regulator_symbol,
# run_accession, yeastepigenome_id) %>%
# write_parquet("~/code/hf/rossi_2021/rossi_2021_metadata.parquet",
# compression = "zstd",
# write_statistics = TRUE,
# use_dictionary = c(
# regulator_locus_tag = TRUE,
# regulator_symbol = TRUE
# )
# )
process_chipexo_genomecov_file = function(covpath, accession_str){
data.table::fread(covpath, sep = "\t", col.names=c('chr', 'pos', 'pileup')) %>%
left_join(chrmap %>% select(refseq, ucsc) %>%
dplyr::rename(chr=refseq)) %>%
mutate(chr = ucsc,
accession = accession_str) %>%
select(chr, pos, pileup, accession)
}
# Output base directory for partitioned dataset
output_parquet_dir = file.path(here("data/chip_exo/"), "genome_map")
dir.create(output_parquet_dir)
# Write incrementally
# for (accession_str in pugh_genomecov_meta$run_accession) {
#
# sample = pugh_genomecov_meta %>% filter(run_accession == accession_str) %>% pull(sample)
# replicate_str = pugh_genomecov_meta %>% filter(run_accession == accession_str) %>% pull(replicate)
# path = pugh_genomecov_meta %>% filter(run_accession == accession_str) %>% pull(path)
#
#
# message(glue::glue("Processing {sample} ({replicate_str})"))
#
# df <- process_chipexo_genomecov_file(
# path,
# accession_str
# )
#
# # Write just this sample's data to the appropriate partition
# arrow::write_dataset(
# df,
# path = output_parquet_dir,
# format = "parquet",
# partitioning = c("accession"),
# existing_data_behavior = "overwrite",
# compression = "zstd",
# write_statistics = TRUE,
# use_dictionary = c(
# chr = TRUE
# )
# )
# }