mahendrawada_2025 / scripts /mahendrawada_peak_analysis.R
Chase Mateusiak
adding peaks and other data related to reproducing the authors results
a203fb4
Raw
History Blame Contribute Delete
6.69 kB
# keep promoter sets in columns
# rows are signal method (enrichment, MACS2, orig)
# intersect peaks with the promoter definitions to annotate the peaks
library(tidyverse)
library(janitor)
library(GenomicRanges)
library(here)
brentlab_features <- read_csv("~/projects/huggingface/yeast_genome_resources/brentlab_features.csv.gz")
intergenic_meta <- read_csv("~/projects/huggingface/yeast_genome_resources/intergenic_regions_metadata_5_1.csv")
promoters <- list(
bp500 = rtracklayer::import("~/projects/huggingface/yeast_genome_resources/start_codon_500bp_upstream_promoters.bed"),
mindel = GenomicRanges::GRanges(read_csv("~/projects/huggingface/yeast_genome_resources/mindel_promoters.csv.gz")),
kang = rtracklayer::import("~/projects/huggingface/yeast_genome_resources/yiming_promoters.bed"),
intergenic = rtracklayer::import("~/projects/huggingface/yeast_genome_resources/intergenic_regions_5_1.bed")
)
GenomicRanges::mcols(promoters$mindel) <- GenomicRanges::mcols(promoters$mindel) |>
as.data.frame() |>
dplyr::transmute(name = target_locus_tag) |>
S4Vectors::DataFrame()
read_in_annotated_peaks <- function(peak_path) {
read_tsv(peak_path,
comment = "#",
col_names = c(
"chr", "start", "end",
"name", "score", "strand"
)
)
}
score_targets_replicates <- function(regulator, peaks_list, promoters_gr, score_thresh = -log10(0.1)) {
# anchor to avoid ABF1 matching ABF10, ABF1L, etc.
matched <- peaks_list[str_detect(names(peaks_list), paste0("^", regulator))]
if (length(matched) == 0) {
warning(sprintf("No replicates found for regulator: %s", regulator))
return(NULL)
}
bind_rows(matched, .id = "sample_id") |>
mutate(replicate = str_extract(sample_id, "[^_]+$")) |>
filter(score > score_thresh) |>
annotate_bed_peaks_to_promoters(promoters_gr) |>
group_by(promoter_id) |>
reframe(
n_replicates = n_distinct(replicate),
n_peaks = n(),
nearest_score = score[which.min(distance_to_tss)],
median_score = median(score),
max_score = max(score)
)
}
annotate_bed_peaks_to_promoters <- function(peaks_df, promoters_gr) {
peaks_gr <- GenomicRanges::GRanges(
seqnames = peaks_df$chr,
ranges = IRanges::IRanges(start = peaks_df$start, end = peaks_df$end)
)
hits <- GenomicRanges::findOverlaps(peaks_gr, promoters_gr, ignore.strand = TRUE)
if (length(hits) == 0) {
return(peaks_df |> dplyr::slice(0) |> dplyr::mutate(promoter_id = character(), distance_to_tss = numeric()))
}
promoter_strand <- as.character(GenomicRanges::strand(promoters_gr))
promoter_start <- GenomicRanges::start(promoters_gr)
promoter_end <- GenomicRanges::end(promoters_gr)
promoter_name <- promoters_gr$name
peaks_df[S4Vectors::queryHits(hits), ] |>
dplyr::mutate(
promoter_id = promoter_name[S4Vectors::subjectHits(hits)],
.promoter_strand = promoter_strand[S4Vectors::subjectHits(hits)],
.promoter_start = promoter_start[S4Vectors::subjectHits(hits)],
.promoter_end = promoter_end[S4Vectors::subjectHits(hits)],
.peak_mid = (start + end) / 2,
.tss_pos = dplyr::if_else(.promoter_strand == "+", .promoter_end, .promoter_start),
distance_to_tss = abs(.peak_mid - .tss_pos)
) |>
dplyr::select(-dplyr::starts_with("."))
}
annotated_peaks <- list(
files = list.files(here("data/reprocessed_mahendrawada_results/peaks"),
"_peaks.bed",
full.names = TRUE,
recursive = TRUE
)
)
names(annotated_peaks$files) <- str_remove(
basename(annotated_peaks$files),
"_peaks.bed"
)
annotated_peaks$df <- map(annotated_peaks$files, read_in_annotated_peaks)
regulators <- unique(str_remove(names(annotated_peaks$df), "_[A,B,C]$"))
target_scores <- list()
for (pset in names(promoters)) {
target_scores[[pset]] <- list()
for (r in regulators) {
rdf <-
score_targets_replicates(
regulator = r,
peaks_list = annotated_peaks$df,
promoters_gr = promoters[[pset]]
)
if (pset == "intergenic") {
rdf <- rdf |>
left_join(intergenic_meta |>
dplyr::select(
promoter_id = ir_name,
feature_left,
feature_right
) |>
pivot_longer(-promoter_id, values_to = "target_locus_tag") |>
dplyr::select(-name), relationship = "many-to-many") |>
dplyr::select(-promoter_id) |>
mutate(promoter_id = target_locus_tag) |>
dplyr::select(-target_locus_tag)
}
target_scores[[pset]][[r]] <- rdf
}
}
reformat_tmp <- function(df) {
df |>
separate_wider_delim(tmp,
delim = "x", names = c(
"regulator_symbol",
"condition"
),
too_few = "align_start"
) |>
mutate(target_locus_tag = promoter_id) |>
dplyr::select(-promoter_id) |>
left_join(dplyr::select(brentlab_features,
regulator_symbol = symbol,
regulator_locus_tag = locus_tag
)) |>
left_join(dplyr::select(brentlab_features,
target_locus_tag = locus_tag,
target_symbol = symbol
)) |>
dplyr::relocate(regulator_locus_tag, regulator_symbol, condition, target_locus_tag, target_symbol) |>
group_by(regulator_locus_tag, condition) |>
arrange(desc(max_score)) |>
ungroup() |>
filter(
!is.na(target_locus_tag),
!is.na(target_symbol)
)
}
gm_meta <- arrow::read_parquet("~/projects/huggingface/mahendrawada_2025/chec_genome_map_meta.parquet")
target_scores_df <- purrr::map(
target_scores,
~ dplyr::bind_rows(.x, .id = "tmp")
) |>
dplyr::bind_rows(.id = "promoter_set") |>
reformat_tmp() |>
left_join(
gm_meta |>
dplyr::select(sample_id, regulator_locus_tag, condition) |>
distinct()
) |>
dplyr::relocate(sample_id)
# target_scores_df_split <- target_scores_df |>
# filter(n_replicates > 1) |>
# group_by(promoter_set) |>
# group_walk(~ arrow::write_parquet(
# dplyr::select(ungroup(.x), -c(regulator_locus_tag,regulator_symbol,condition)), file.path("~/projects/huggingface/mahendrawada_2025",
# paste0(.y$promoter_set, "_peaks.parquet"))))