| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| library(tidyverse) |
| library(GenomicRanges) |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| calculate_enrichment <- function(total_background_hops, |
| total_experiment_hops, |
| background_hops, |
| experiment_hops, |
| pseudocount = 0.1) { |
|
|
| |
| if (!all(is.numeric(c(total_background_hops, total_experiment_hops, |
| background_hops, experiment_hops)))) { |
| stop("All inputs must be numeric") |
| } |
|
|
| |
| n_regions <- length(background_hops) |
|
|
| |
| if (length(experiment_hops) != n_regions) { |
| stop("background_hops and experiment_hops must be the same length") |
| } |
|
|
| |
| if (length(total_background_hops) == 1) { |
| total_background_hops <- rep(total_background_hops, n_regions) |
| } |
| if (length(total_experiment_hops) == 1) { |
| total_experiment_hops <- rep(total_experiment_hops, n_regions) |
| } |
|
|
| |
| if (length(total_background_hops) != n_regions || |
| length(total_experiment_hops) != n_regions) { |
| stop("All input vectors must be the same length or scalars") |
| } |
|
|
| |
| numerator <- experiment_hops / total_experiment_hops |
| denominator <- (background_hops + pseudocount) / total_background_hops |
| enrichment <- numerator / denominator |
|
|
| |
| if (any(enrichment < 0, na.rm = TRUE)) { |
| stop("Enrichment values must be non-negative") |
| } |
| if (any(is.na(enrichment))) { |
| stop("Enrichment values must not be NA") |
| } |
| if (any(is.infinite(enrichment))) { |
| stop("Enrichment values must not be infinite") |
| } |
|
|
| return(enrichment) |
| } |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| calculate_poisson_pval <- function(total_background_hops, |
| total_experiment_hops, |
| background_hops, |
| experiment_hops, |
| pseudocount = 0.1, |
| ...) { |
|
|
| |
| if (!all(is.numeric(c(total_background_hops, total_experiment_hops, |
| background_hops, experiment_hops)))) { |
| stop("All inputs must be numeric") |
| } |
|
|
| |
| n_regions <- length(background_hops) |
|
|
| |
| if (length(experiment_hops) != n_regions) { |
| stop("background_hops and experiment_hops must be the same length") |
| } |
|
|
| |
| if (length(total_background_hops) == 1) { |
| total_background_hops <- rep(total_background_hops, n_regions) |
| } |
| if (length(total_experiment_hops) == 1) { |
| total_experiment_hops <- rep(total_experiment_hops, n_regions) |
| } |
|
|
| |
| if (length(total_background_hops) != n_regions || |
| length(total_experiment_hops) != n_regions) { |
| stop("All input vectors must be the same length or scalars") |
| } |
|
|
| |
| hop_ratio <- total_experiment_hops / total_background_hops |
|
|
| |
| |
| mu <- (background_hops + pseudocount) * hop_ratio |
|
|
| |
| x <- experiment_hops |
|
|
| |
| |
| |
| pval <- ppois(x - 1, lambda = mu, lower.tail = FALSE, ...) |
|
|
| return(pval) |
| } |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| calculate_hypergeom_pval <- function(total_background_hops, |
| total_experiment_hops, |
| background_hops, |
| experiment_hops, |
| ...) { |
|
|
| |
| if (!all(is.numeric(c(total_background_hops, total_experiment_hops, |
| background_hops, experiment_hops)))) { |
| stop("All inputs must be numeric") |
| } |
|
|
| |
| n_regions <- length(background_hops) |
|
|
| |
| if (length(experiment_hops) != n_regions) { |
| stop("background_hops and experiment_hops must be the same length") |
| } |
|
|
| |
| if (length(total_background_hops) == 1) { |
| total_background_hops <- rep(total_background_hops, n_regions) |
| } |
| if (length(total_experiment_hops) == 1) { |
| total_experiment_hops <- rep(total_experiment_hops, n_regions) |
| } |
|
|
| |
| if (length(total_background_hops) != n_regions || |
| length(total_experiment_hops) != n_regions) { |
| stop("All input vectors must be the same length or scalars") |
| } |
|
|
| |
| |
| M <- total_background_hops + total_experiment_hops |
| |
| n <- total_experiment_hops |
| |
| N <- background_hops + experiment_hops |
| |
| x <- experiment_hops - 1 |
|
|
| |
| valid <- (M >= 1) & (N >= 1) |
| pval <- rep(1, length(M)) |
|
|
| |
| if (any(valid)) { |
| pval[valid] <- phyper(x[valid], n[valid], M[valid] - n[valid], N[valid], |
| lower.tail = FALSE, ...) |
| } |
|
|
| return(pval) |
| } |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| bed_to_granges <- function(bed_df, zero_indexed = TRUE) { |
|
|
| if (!all(c("chr", "start", "end") %in% names(bed_df))) { |
| stop("bed_df must have columns: chr, start, end") |
| } |
|
|
| |
| if (zero_indexed) { |
| gr_start <- bed_df$start + 1 |
| gr_end <- bed_df$end |
| } else { |
| gr_start <- bed_df$start |
| gr_end <- bed_df$end |
| } |
|
|
| |
| gr <- GRanges( |
| seqnames = bed_df$chr, |
| ranges = IRanges(start = gr_start, end = gr_end), |
| strand = "*" |
| ) |
|
|
| |
| extra_cols <- setdiff(names(bed_df), c("chr", "start", "end", "strand")) |
| if (length(extra_cols) > 0) { |
| mcols(gr) <- bed_df[, extra_cols, drop = FALSE] |
| } |
|
|
| return(gr) |
| } |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| deduplicate_granges <- function(gr) { |
| |
| unique_ranges <- !duplicated(granges(gr)) |
| gr[unique_ranges] |
| } |
|
|
|
|
| |
| |
| |
| |
| |
| |
| count_overlaps <- function(insertions_gr, regions_gr, deduplicate = TRUE) { |
|
|
| |
| if (deduplicate) { |
| n_before <- length(insertions_gr) |
| insertions_gr <- deduplicate_granges(insertions_gr) |
| n_after <- length(insertions_gr) |
| if (n_before != n_after) { |
| message(" Deduplicated: ", n_before, " -> ", n_after, |
| " (removed ", n_before - n_after, " duplicates)") |
| } |
| } |
|
|
| |
| |
| counts <- countOverlaps(regions_gr, insertions_gr) |
|
|
| return(counts) |
| } |
|
|
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| enrichment_analysis <- function(experiment_gr, |
| background_gr, |
| regions_gr, |
| deduplicate_experiment = TRUE, |
| pseudocount = 0.1) { |
|
|
| message("Starting enrichment analysis...") |
|
|
| |
| if (!inherits(experiment_gr, "GRanges")) { |
| stop("experiment_gr must be a GRanges object") |
| } |
| if (!inherits(background_gr, "GRanges")) { |
| stop("background_gr must be a GRanges object") |
| } |
| if (!inherits(regions_gr, "GRanges")) { |
| stop("regions_gr must be a GRanges object") |
| } |
|
|
| |
| message("Counting experiment overlaps...") |
| if (deduplicate_experiment) { |
| message(" Deduplication: ON") |
| } else { |
| message(" Deduplication: OFF") |
| } |
|
|
| experiment_counts <- count_overlaps( |
| experiment_gr, regions_gr, |
| deduplicate = deduplicate_experiment |
| ) |
|
|
| |
| message("Counting background overlaps...") |
| message(" Deduplication: OFF (background should not be deduplicated)") |
|
|
| background_counts <- count_overlaps( |
| background_gr, regions_gr, |
| deduplicate = FALSE |
| ) |
|
|
| |
| if (deduplicate_experiment) { |
| experiment_gr_dedup <- deduplicate_granges(experiment_gr) |
| total_experiment_hops <- length(experiment_gr_dedup) |
| } else { |
| total_experiment_hops <- length(experiment_gr) |
| } |
|
|
| total_background_hops <- length(background_gr) |
|
|
| message("Total experiment hops: ", total_experiment_hops) |
| message("Total background hops: ", total_background_hops) |
|
|
| if (total_experiment_hops == 0) { |
| stop("Experiment data is empty") |
| } |
| if (total_background_hops == 0) { |
| stop("Background data is empty") |
| } |
|
|
| |
| mcols(regions_gr)$experiment_hops <- as.integer(experiment_counts) |
| mcols(regions_gr)$background_hops <- as.integer(background_counts) |
| mcols(regions_gr)$total_experiment_hops <- as.integer(total_experiment_hops) |
| mcols(regions_gr)$total_background_hops <- as.integer(total_background_hops) |
|
|
| |
| message("Calculating enrichment scores...") |
| mcols(regions_gr)$callingcards_enrichment <- calculate_enrichment( |
| total_background_hops = total_background_hops, |
| total_experiment_hops = total_experiment_hops, |
| background_hops = background_counts, |
| experiment_hops = experiment_counts, |
| pseudocount = pseudocount |
| ) |
|
|
| message("Calculating Poisson p-values...") |
| mcols(regions_gr)$poisson_pval <- calculate_poisson_pval( |
| total_background_hops = total_background_hops, |
| total_experiment_hops = total_experiment_hops, |
| background_hops = background_counts, |
| experiment_hops = experiment_counts, |
| pseudocount = pseudocount |
| ) |
|
|
| message("Calculating log Poisson p-values...") |
| mcols(regions_gr)$log_poisson_pval <- calculate_poisson_pval( |
| total_background_hops = total_background_hops, |
| total_experiment_hops = total_experiment_hops, |
| background_hops = background_counts, |
| experiment_hops = experiment_counts, |
| pseudocount = pseudocount, |
| log.p = TRUE |
| ) |
|
|
| message("Calculating hypergeometric p-values...") |
| mcols(regions_gr)$hypergeometric_pval <- calculate_hypergeom_pval( |
| total_background_hops = total_background_hops, |
| total_experiment_hops = total_experiment_hops, |
| background_hops = background_counts, |
| experiment_hops = experiment_counts |
| ) |
|
|
| message("Calculating log hypergeometric p-values...") |
| mcols(regions_gr)$log_hypergeometric_pval <- calculate_hypergeom_pval( |
| total_background_hops = total_background_hops, |
| total_experiment_hops = total_experiment_hops, |
| background_hops = background_counts, |
| experiment_hops = experiment_counts, |
| log.p = TRUE |
| ) |
|
|
| |
| message("Calculating adjusted p-values...") |
| mcols(regions_gr)$poisson_qval <- p.adjust(mcols(regions_gr)$poisson_pval, method = "fdr") |
| mcols(regions_gr)$hypergeometric_qval <- p.adjust(mcols(regions_gr)$hypergeometric_pval, method = "fdr") |
|
|
| message("Analysis complete!") |
|
|
| return(regions_gr) |
| } |
|
|
|
|
| |
|
|
| PROMOTERS = 'start_codon_500bp' |
|
|
| genomic_features = arrow::read_parquet("~/code/hf/yeast_genome_resources/brentlab_features.parquet") |
|
|
| genome_map_replicate_ds = arrow::open_dataset("~/code/hf/callingcards/genome_map", unify_schemas = TRUE) |
| genome_map_replicate_meta = arrow::read_parquet("~/code/hf/callingcards/genome_map_meta.parquet") |
|
|
| background_gr <- read_tsv("~/code/hf/callingcards/adh1_background_ucsc.qbed") |> |
| mutate(id = "adh1_bg", |
| score = scales::rescale(depth, to = c(1,1000))) |> |
| dplyr::select(chr, start, end, id, score, strand) |> |
| bed_to_granges() |
|
|
|
|
| regions_list = list( |
| yiming = read_tsv("~/code/hf/yeast_genome_resources/yiming_promoters.bed", |
| col_names = c('chr', 'start', 'end', 'locus_tag', 'score', 'strand')) %>% |
| bed_to_granges(), |
| mindel = arrow::read_parquet("~/code/hf/yeast_genome_resources/mindel_promoters.parquet", |
| col_names = c('chr', 'start', 'end', 'locus_tag', 'score', 'strand')) %>% |
| bed_to_granges(), |
| start_codon_500bp = rtracklayer::import("~/code/hf/yeast_genome_resources/start_codon_500bp_upstream_promoters.bed"), |
| intergenic = rtracklayer::import("~/code/hf/yeast_genome_resources/intergenic_regions_5_1.bed") |
| ) |
|
|
| |
| colnames(GenomicRanges::mcols(regions_list$yiming))[1] <- "target_locus_tag" |
| colnames(GenomicRanges::mcols(regions_list$start_codon_500bp))[1] <- "target_locus_tag" |
|
|
| intergenic_meta = read_csv("~/code/hf/yeast_genome_resources/intergenic_regions_metadata_5_1.csv") |> |
| dplyr::select(ir_name, chr, start, end, feature_left, feature_right) |> |
| pivot_longer(-c(ir_name, chr, start, end), names_to = 'side', values_to = 'target_locus_tag') |> |
| left_join(dplyr::select(genomic_features, target_locus_tag = locus_tag, |
| target_symbol = symbol, target_strand = strand, |
| target_start = start, target_end = end)) |> |
| filter(chr != "chrM") |> |
| filter(!is.na(target_strand)) |> |
| mutate(valid = (target_strand == '-' & target_end <= start) |
| | (target_strand == '+' & target_start >= end)) |> |
| filter(valid) |
|
|
| regions_gr <- regions_list[[PROMOTERS]] |
|
|
| |
| if(PROMOTERS != "intergenic"){ |
| |
| regions_gr = regions_gr[!is.na(regions_gr$target_locus_tag)] |
| } else if(PROMOTERS == "intergenic"){ |
| regions_gr = regions_gr[regions_gr$name %in% unique(intergenic_meta$ir_name)] |
| } |
| library(parallel) |
|
|
| n_cores = 25 |
|
|
|
|
| results_replicates <- mclapply( |
| genome_map_replicate_meta$id, |
| \(x) enrichment_analysis( |
| experiment_gr = genome_map_replicate_ds |> |
| filter(id == x) |> |
| collect() |> |
| dplyr::rename(score = depth) |> |
| relocate(chr, start, end, id, score, strand) |> |
| mutate(depth = scales::rescale(score, to = c(1, 1000))) |> |
| bed_to_granges(), |
| background_gr = background_gr, |
| regions_gr = regions_gr, |
| deduplicate_experiment = TRUE, |
| pseudocount = 0.1 |
| ), |
| mc.cores = n_cores |
| ) |
| names(results_replicates) = genome_map_replicate_meta$id |
|
|
|
|
| if(PROMOTERS == "intergenic"){ |
|
|
| results_replicates_df = bind_rows(map(results_replicates, as_tibble), .id = "genome_map_id") |> |
| mutate(genome_map_id = as.integer(genome_map_id)) |> |
| dplyr::rename(ir_name = name) |> |
| left_join(dplyr::select(intergenic_meta, ir_name, target_locus_tag, target_symbol), |
| relationship = "many-to-many") |> |
| left_join(genome_map_replicate_meta, |
| by = c("genome_map_id" = "id")) |> |
| select(genome_map_id, batch, |
| target_locus_tag, target_symbol, ir_name, |
| experiment_hops, background_hops, |
| total_background_hops, total_experiment_hops, |
| callingcards_enrichment, |
| poisson_pval, log_poisson_pval, poisson_qval, |
| hypergeometric_pval, log_hypergeometric_pval, hypergeometric_qval) |> |
| mutate(genome_map_id = as.integer(genome_map_id)) |
|
|
| arrow::write_dataset( |
| results_replicates_df, |
| path = "/home/chase/code/hf/callingcards/annotated_feature_reprocess_intergenic", |
| format = "parquet", |
| partitioning = c("batch"), |
| existing_data_behavior = "overwrite", |
| compression = "zstd", |
| write_statistics = TRUE, |
| use_dictionary = c( |
| genome_map_id = TRUE |
| ) |
| ) |
|
|
| } else if(PROMOTERS == "start_codon_500bp"){ |
|
|
|
|
| results_replicates_df = bind_rows(map(results_replicates, as_tibble), .id = "genome_map_id") |> |
| mutate(genome_map_id = as.integer(genome_map_id)) |> |
| left_join(dplyr::select(genomic_features, target_locus_tag = locus_tag, target_symbol = symbol)) |> |
| left_join(genome_map_replicate_meta, |
| by = c("genome_map_id" = "id")) |> |
| select(genome_map_id, batch, |
| target_locus_tag, target_symbol, |
| experiment_hops, background_hops, |
| total_background_hops, total_experiment_hops, |
| callingcards_enrichment, |
| poisson_pval, log_poisson_pval, poisson_qval, |
| hypergeometric_pval, log_hypergeometric_pval, hypergeometric_qval) |> |
| mutate(genome_map_id = as.integer(genome_map_id)) |
|
|
| arrow::write_dataset( |
| results_replicates_df, |
| path = "/home/chase/code/hf/callingcards/annotated_feature_reprocess_start_codon_500", |
| format = "parquet", |
| partitioning = c("batch"), |
| existing_data_behavior = "overwrite", |
| compression = "zstd", |
| write_statistics = TRUE, |
| use_dictionary = c( |
| genome_map_id = TRUE |
| ) |
| ) |
|
|
| } |
|
|
| |
|
|
| passing_rep_gm_ids <- unlist(strsplit(unique(arrow::read_parquet("~/code/hf/callingcards/2026_analysis_set.parquet")$gm_id), "-")) |
|
|
| genome_map_replicate_meta_combined = genome_map_replicate_meta |> |
| filter(id %in% passing_rep_gm_ids) |> |
| group_by(regulator_locus_tag, regulator_symbol, condition) |> |
| mutate(id = as.character(id)) |> |
| reframe(combined_id = if (n() > 1) paste(id, collapse = "-") else id) |
|
|
| |
| results_combined <- map(genome_map_replicate_meta_combined$combined_id, ~{ |
| |
| ids <- as.integer(strsplit(.x, "-")[[1]]) |
| ids <- ids[!is.na(ids)] |
|
|
| |
| combined_passing_set <- map(ids, ~{ |
| genome_map_replicate_ds |> |
| filter(id == .x) |> |
| collect() |> |
| dplyr::rename(score = depth) |> |
| relocate(chr, start, end, id, score, strand) |> |
| mutate(depth = scales::rescale(score, to = c(1, 1000))) |> |
| bed_to_granges() |> |
| deduplicate_granges() |
| }) |
|
|
| |
| combined_gr <- do.call(c, combined_passing_set) |
|
|
| enrichment_analysis( |
| experiment_gr = combined_gr, |
| background_gr = background_gr, |
| regions_gr = regions_gr, |
| deduplicate_experiment = FALSE, |
| pseudocount = 0.1 |
| ) |
| }) |
| names(results_combined) = genome_map_replicate_meta_combined$combined_id |
|
|
| if(PROMOTERS == 'intergenic'){ |
| results_combined_analysis_df = bind_rows(map(results_combined, as_tibble), .id = "combined_id") |> |
| dplyr::rename(ir_name = name) |> |
| left_join(dplyr::select(intergenic_meta, ir_name, target_locus_tag, target_symbol), |
| relationship = "many-to-many") |> |
| left_join(genome_map_replicate_meta_combined) |> |
| select(combined_id, |
| regulator_locus_tag, regulator_symbol, condition, |
| target_locus_tag, target_symbol, experiment_hops, background_hops, |
| total_background_hops, total_experiment_hops, |
| callingcards_enrichment, |
| poisson_pval, log_poisson_pval, poisson_qval, |
| hypergeometric_pval, log_hypergeometric_pval, hypergeometric_qval) |> |
| filter(condition == "standard") |
|
|
| results_combined_analysis_df |> |
| ungroup() |> |
| dplyr::select(-condition) |> |
| arrange(regulator_locus_tag) |> |
| arrow::write_parquet( |
| "/home/chase/code/hf/callingcards/annotated_feature_reprocess_intergenic_analysis.parquet", |
| compression = "zstd", |
| write_statistics = TRUE, |
| use_dictionary = TRUE |
| ) |
|
|
| } else if(PROMOTERS == "start_codon_500bp"){ |
| results_combined_analysis_df = bind_rows(map(results_combined, as_tibble), .id = "combined_id") |> |
| left_join(dplyr::select(genomic_features, target_locus_tag = locus_tag, target_symbol = symbol)) |> |
| left_join(genome_map_replicate_meta_combined) |> |
| select(combined_id, |
| regulator_locus_tag, regulator_symbol, condition, |
| target_locus_tag, target_symbol, experiment_hops, background_hops, |
| total_background_hops, total_experiment_hops, |
| callingcards_enrichment, |
| poisson_pval, log_poisson_pval, poisson_qval, |
| hypergeometric_pval, log_hypergeometric_pval, hypergeometric_qval) |> |
| filter(condition == "standard") |
|
|
| results_combined_analysis_df |> |
| ungroup() |> |
| dplyr::select(-condition) |> |
| arrange(regulator_locus_tag) |> |
| arrow::write_parquet( |
| "/home/chase/code/hf/callingcards/annotated_feature_reprocess_start_codon_500bp_analysis.parquet", |
| compression = "zstd", |
| write_statistics = TRUE, |
| use_dictionary = TRUE |
| ) |
|
|
| } |
|
|
|
|