barkai_compendium / scripts /parse_barkai_checseq.R
cmatkhan's picture
Upload folder using huggingface_hub
a220dd5 verified
## 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)
library(GEOquery)
# genomic feature harmonization table ----
# see https://huggingface.co/datasets/BrentLab/yeast_genome_resources
genomicfeatures = arrow::open_dataset(here("data/genome_files/hf/features")) %>%
as_tibble()
# sacCer3_genome = rtracklayer::import("~/ref/sacCer3/ucsc/sacCer3.fa.gz", format="fasta")
#
# sacCer3_seqnames = unlist(map(str_split(names(sacCer3_genome), " "), ~.[[1]]))
#
# sacCer3_genome_df = tibble(
# seqnames = rep(sacCer3_seqnames, Biostrings::width(sacCer3_genome))
# ) %>%
# group_by(seqnames) %>%
# mutate(start = row_number()-1,
# end = row_number()) %>%
# ungroup()
#
# retrieve_series_paths = function(series_id){
# sra_meta_path = file.path("data/barkai_checseq", series_id, "SraRunTable.csv")
# stopifnot(file.exists(sra_meta_path))
# df = read_csv(sra_meta_path)
#
# data_files = list.files(here("data/barkai_checseq", series_id), "*.txt.gz", full.names = TRUE)
#
# stopifnot(nrow(df) == length(data_files))
#
# names(data_files) = str_extract(basename(data_files), "GSM\\d+")
#
# list(
# meta = sra_meta_path,
# files = data_files
# )
# }
#
#
# add_genomic_coordinate = function(checseqpath){
#
# bind_cols(sacCer3_genome_df,
# data.table::fread(checseqpath, sep = "\t", col.names='pileup'))
#
# }
#
# process_checseq_files = function(file){
#
# add_genomic_coordinate(file) %>%
# filter(pileup != 0)
# }
#
# series_list = map(set_names(c("GSE179430", "GSE209631", "GSE222268")), retrieve_series_paths)
#
# dataset_basepath = here("data/barkai_checseq/hf/genome_map")
#
# # Create output directory
# dir.create(dataset_basepath, recursive = TRUE, showWarnings = FALSE)
#
# for (series_id in names(series_list)) {
#
# message(glue::glue("Processing series {series_id}"))
#
# for (accession_id in names(series_list[[series_id]]$files)) {
#
# message(glue::glue(" Processing {accession_id}"))
#
# df <- process_checseq_files(
# series_list[[series_id]]$files[[accession_id]]
# ) %>%
# mutate(accession = accession_id, series = series_id)
#
# df %>%
# group_by(seqnames) %>%
# write_dataset(
# path = dataset_basepath,
# format = "parquet",
# partitioning = c("series", "accession"),
# existing_data_behavior = "overwrite",
# compression = "zstd",
# write_statistics = TRUE,
# use_dictionary = c(
# seqnames = TRUE
# )
# )
#
# gc()
# }
# }
# the following code was used to parse an entire series to DF and then save
# to a parquet dataset. that was too large and I chose the dataset partitioning
# instead.
split_manipulation <- function(manipulation_str) {
parts <- str_split(manipulation_str, "::")[[1]]
if (length(parts) != 2) {
stop("Unexpected format. Expected 'LOCUS::TAGGED_CONSTRUCT'")
}
tagged_locus <- parts[1]
rhs <- parts[2]
# default
dbd_donor_symbol_str <- "none"
ortholog <- "none"
# Check for paralog DBD
if (str_detect(rhs, "-[A-Za-z0-9]+DBD-Mnase$")) {
dbd_donor_symbol_str <- toupper(str_remove(str_split(rhs, "-", simplify = TRUE)[[2]], "DBD"))
} else if (str_detect(rhs, "^K\\.lactis .*?-Mnase$")) {
ortholog <- rhs
}
list(
mnase_tagged_symbol = tagged_locus,
dbd_donor_symbol = dbd_donor_symbol_str,
ortholog_donor = ortholog
)
}
split_deletion <- function(deletion_str) {
parts <- str_split(deletion_str, "::", simplify = TRUE)
list(
paralog_deletion_symbol = parts[1],
paralog_resistance_cassette = if (ncol(parts) >= 2) parts[2] else "none"
)
}
split_construct_to_tibble = function(split_list){
background = list(background=split_list[[1]])
manipulation_list = split_manipulation(split_list[[2]])
deletion_list = split_deletion(tryCatch(split_list[[3]], error = function(e) "none"))
bind_cols(map(list(background, manipulation_list, deletion_list), as_tibble))
}
split_constructs <- function(s) {
s <- str_trim(s)
if (s == "" || is.na(s)) return(character(0))
# split on spaces ONLY when the next token starts a new locus "XYZ::"
split_geno = str_split(s, "\\s+(?=[A-Za-z0-9_.()\\-]+::)")[[1]]
bind_cols(tibble(genotype = s), split_construct_to_tibble(split_geno))
}
gse178430_meta = read_csv("data/barkai_checseq/GSE179430/SraRunTable.csv") %>%
mutate(genotype = str_replace(genotype, "Yap2", "Cad1")) %>%
mutate(genotype = str_replace(genotype, "Yap4", "Cin5"))
gse178430_parsed_meta = bind_cols(
select(gse178430_meta, `Sample Name`, strainid, Instrument) %>%
dplyr::rename(accession = `Sample Name`,
instrument = Instrument),
bind_rows(map(gse178430_meta$genotype, split_constructs))) %>%
left_join(select(genomicfeatures, locus_tag, symbol) %>%
dplyr::rename(mnase_tagged_symbol = symbol)) %>%
dplyr::rename(regulator_locus_tag = locus_tag,
regulator_symbol = mnase_tagged_symbol) %>%
select(accession, regulator_locus_tag, regulator_symbol, strainid,
instrument, genotype, dbd_donor_symbol, ortholog_donor,
paralog_deletion_symbol, paralog_resistance_cassette) %>%
arrange(accession) %>%
mutate(series = "GSE178430") %>%
mutate(sample_id = row_number()) %>%
relocate(sample_id, series, accession)
gse209631_meta = read_csv("data/barkai_checseq/GSE209631/SraRunTable.csv")
gse209631_parsed_meta = gse209631_meta %>%
select(`Sample Name`, tagged_tf, Instrument, `variant-type`) %>%
janitor::clean_names() %>%
dplyr::rename(accession = sample_name) %>%
arrange(tagged_tf, variant_type) %>%
left_join(select(genomicfeatures, locus_tag, symbol) %>% dplyr::rename(tagged_tf = symbol)) %>%
dplyr::rename(regulator_symbol = tagged_tf, regulator_locus_tag = locus_tag) %>%
select(accession, regulator_locus_tag, regulator_symbol, variant_type) %>%
arrange(accession) %>%
mutate(series = "GSE209631") %>%
mutate(sample_id = row_number()) %>%
relocate(sample_id, series, accession)
gse222268 = GEOquery::getGEO(filename=here("data/barkai_checseq/GSE222268_series_matrix.txt"))
gse222268_meta = Biobase::pData(gse222268@phenoData) %>% as_tibble() %>%
select(title, geo_accession, extract_protocol_ch1, description,
instrument_model, library_selection) %>%
mutate(description = ifelse(description == "", library_selection, description)) %>%
dplyr::rename(accession = geo_accession) %>%
select(-library_selection, -instrument_model, -extract_protocol_ch1)
gse222268_parsed_meta <- gse222268_meta %>%
mutate(
regulator_symbol = str_extract(title, "^[^_]+"),
experiment_details = str_remove(title, "^[^_]+_")) %>%
mutate(regulator_symbol = toupper(regulator_symbol)) %>%
mutate(regulator_symbol = str_replace(regulator_symbol, "UGA3C", "UGA3")) %>%
mutate(regulator_symbol = ifelse(str_detect(regulator_symbol, "MNASE"),
"none", regulator_symbol)) %>%
left_join(
select(genomicfeatures, locus_tag, symbol) %>%
dplyr::rename(regulator_locus_tag = locus_tag,
regulator_symbol = symbol)) %>%
replace_na(list(regulator_locus_tag = "none")) %>%
select(-title) %>%
arrange(accession) %>%
mutate(series = "GSE222268") %>%
mutate(sample_id = row_number()) %>%
relocate(sample_id, series, accession)
# gse178430_parsed_meta %>%
# write_parquet(here("/home/chase/code/hf/barkai_compendium/GSE178430_metadata.parquet"),
# compression = "zstd",
# write_statistics = TRUE,
# use_dictionary = c(
# sample_id = TRUE,
# accession = TRUE,
# regulator_locus_tag = TRUE,
# regulator_symbol = TRUE
# )
# )
#
# gse209631_parsed_meta %>%
# write_parquet(here("/home/chase/code/hf/barkai_compendium/GSE209631_metadata.parquet"),
# compression = "zstd",
# write_statistics = TRUE,
# use_dictionary = c(
# sample_id = TRUE,
# accession = TRUE,
# regulator_locus_tag = TRUE,
# regulator_symbol = TRUE,
# variant_type = TRUE
# )
# )
#
# gse222268_parsed_meta %>%
# write_parquet(here("/home/chase/code/hf/barkai_compendium/GSE222268_metadata.parquet"),
# compression = "zstd",
# write_statistics = TRUE,
# use_dictionary = c(
# sample_id = TRUE,
# regulator_locus_tag = TRUE,
# regulator_symbol = TRUE,
# accession = TRUE
# )
# )