13-DMR-overlap-CTmask ================ Kathleen Durkin 2026-11-11 - [1 Setup](#1-setup) - [2 Load DMRs](#2-load-dmrs) - [3 Load tested loci](#3-load-tested-loci) - [4 Identify overlapping DMRs](#4-identify-overlapping-dmrs) - [5 Multi-way DMR overlap](#5-multi-way-dmr-overlap) - [6 Summary](#6-summary) - [7 Plot](#7-plot) - [8 Save outputs](#8-save-outputs) In `10.01-diff-methyl-DSS-CTmask` and `10.11-diff-methyl-DSS-parents-CTmask` we identified DMRs (based on parental OA exposure vs. control) in the offspring and parental gametes, after masking C/T SNPs. In those two files I only checked for parent-offspring overlap at the DML-level (single CpGs position matches). However, I also want to check for overlap at the *region* level, i.e. DMRs. Since a DMR is a region, two DMRs could technically “overlap” by a single base pair, which isn’t very meaningful. For now I’ll define an overlap using the same criteria DSS uses to call DMRs: (`callDMR(minlen = 50, minCG = 3)`): - minimum overlap of **\>= 50bp**, and - overlap must contain **\>= 3 CpG sites** For the CpG count, I’ll only include CpGs that were tested in *both* the offspring stage and the parental sex being compared (i.e., CpGs that could have contributed to both DMRs). NOTE: Because the BSseq objects were built with `strandCollapse = FALSE`, the two cytosines of a CpG dinucleotide (+ and - strand) are separate loci. DSS’s `minCG` counts these loci, not dinucleotides, so I’m doing the same here to keep the overlap definition identical to the DMR definition. In practice, “\>= 3 CpGs” could be as few as 2 CpG dinucleotides. Inputs: - Per-stage/per-sex DMRs from the smoothed two-group `DMLtest()` (score column = `diff.Methy`, exposed - control): - `../output/10.01-diff-methyl-DSS-CTmask/[zygote/larvae]_DMR.bed` - `../output/10.11-diff-methyl-DSS-parents-CTmask/[female/male]_DMR.bed` - Tested loci (used to count CpGs within overlaps): - `../output/10.01-diff-methyl-DSS-CTmask/tested_loci.bed.gz` - `../output/10.11-diff-methyl-DSS-parents-CTmask/[female/male]_tested_loci.bed.gz` Note on coordinates: `callDMR()` reports `start`/`end` as the positions of the first/last CpG in the region (1-based, inclusive), and `save_bed()` in `10.01`/`10.11` wrote those as-is. The tested-loci files, on the other hand, are 0-based BEDs (`start = pos - 1`, `end = pos`), so the CpG position is the `end` column. I’ll read everything into 1-based, closed `GRanges` to keep this consistent. # 1 Setup ``` r # Install if needed: # if (!require("BiocManager", quietly = TRUE)) install.packages("BiocManager") # BiocManager::install("GenomicRanges") library(GenomicRanges) library(readr) library(dplyr) library(tidyr) library(purrr) library(stringr) library(tibble) library(ggplot2) # Overlap criteria (same as DSS DMR definition) min_len <- 50 min_cg <- 3 ``` # 2 Load DMRs ``` r # Read a DMR BED (chr, start, end, score) into GRanges # start/end are first/last CpG positions (1-based) read_dmr <- function(path) { # Only use file if not empty (sample had DMRs identified) if (!file.exists(path) || file.size(path) == 0) return(GRanges()) #read in df <- read_tsv(path, col_names = c("chr", "start", "end", "score"), col_types = "cddd") GRanges(seqnames = df$chr, ranges = IRanges(start = as.integer(df$start), end = as.integer(df$end)), score = df$score) } # full dmrs list dmrs <- list( Zygote = read_dmr("../output/10.01-diff-methyl-DSS-CTmask/zygote_DMR.bed"), Larvae = read_dmr("../output/10.01-diff-methyl-DSS-CTmask/larvae_DMR.bed"), Egg = read_dmr("../output/10.11-diff-methyl-DSS-parents-CTmask/female_DMR.bed"), Sperm = read_dmr("../output/10.11-diff-methyl-DSS-parents-CTmask/male_DMR.bed") ) # summary knitr::kable(tibble(set = names(dmrs), n_DMRs = map_int(dmrs, length), median_bp = map_dbl(dmrs, ~ median(width(.x))), pct_hyper = map_dbl(dmrs, ~ mean(.x$score > 0) * 100)), digits = 1) ``` | set | n_DMRs | median_bp | pct_hyper | |:-------|-------:|----------:|----------:| | Zygote | 5128 | 166 | 31.3 | | Larvae | 4590 | 211 | 53.7 | | Egg | 11143 | 238 | 50.0 | | Sperm | 18493 | 256 | 53.3 | # 3 Load tested loci The tested-loci files are large (~18M CpGs each), so I’ll only keep CpGs that fall within a DMR from that dataset – those are the only ones that can land in an overlap. ``` r # Read a 0-based tested-loci BED and return 1-based CpG positions as GRanges # only keep CpGs within `keep_gr` read_tested <- function(path, keep_gr) { df <- read_tsv(path, col_names = c("chr", "start", "end"), col_types = "cii") gr <- GRanges(seqnames = df$chr, ranges = IRanges(start = df$end, width = 1)) rm(df); gc() subsetByOverlaps(gr, keep_gr) } # for offspring use pooled post-coverage-filter set of tested loci off_tested_path <- function(stage) { pooled_tested <- "../output/10.01-diff-methyl-DSS-CTmask/tested_loci.bed.gz" } off_dmrs_all <- c(dmrs$Zygote, dmrs$Larvae) par_dmrs_all <- c(dmrs$Egg, dmrs$Sperm) tested <- list( Zygote = read_tested(off_tested_path("Zygote"), off_dmrs_all), Larvae = read_tested(off_tested_path("Larvae"), off_dmrs_all), Egg = read_tested("../output/10.11-diff-methyl-DSS-parents-CTmask/female_tested_loci.bed.gz", par_dmrs_all), Sperm = read_tested("../output/10.11-diff-methyl-DSS-parents-CTmask/male_tested_loci.bed.gz", par_dmrs_all) ) map_int(tested, length) ``` ## Zygote Larvae Egg Sperm ## 106146 106146 505467 504739 # 4 Identify overlapping DMRs For each parent x offspring pair of DMR sets, want to: - Find all pairs of DMRs that overlap (any amount of overlap permissible here) - Get the overlapping portion of each pair (pintersect) - Count \# of CpGs in that portion that were tested in both datasets - Keep only pairs where the overlap is \>= 50bp and it contains \>= 3 CpGs - Check for directional concordance (sign of the offspring score == sign of the parent score) ``` r overlap_dmrs <- function(off_name, par_name) { off <- dmrs[[off_name]] par <- dmrs[[par_name]] if (length(off) == 0 || length(par) == 0) return(tibble()) hits <- findOverlaps(off, par) if (length(hits) == 0) return(tibble()) off_hit <- off[queryHits(hits)] par_hit <- par[subjectHits(hits)] ovl <- pintersect(off_hit, par_hit) # CpGs tested in both datasets shared_cpg <- subsetByOverlaps(tested[[off_name]], tested[[par_name]]) tibble(offspring = off_name, parent = par_name, chr = as.character(seqnames(ovl)), overlap_start = start(ovl), overlap_end = end(ovl), overlap_bp = width(ovl), overlap_nCG = countOverlaps(ovl, shared_cpg), off_DMR_start = start(off_hit), off_DMR_end = end(off_hit), off_score = off_hit$score, par_DMR_start = start(par_hit), par_DMR_end = end(par_hit), par_score = par_hit$score) %>% mutate(passes_criteria = overlap_bp >= min_len & overlap_nCG >= min_cg, concordant = sign(off_score) == sign(par_score)) } comparisons <- tribble( ~off_name, ~par_name, "Zygote", "Egg", "Zygote", "Sperm", "Larvae", "Egg", "Larvae", "Sperm" ) all_ovl <- pmap_dfr(comparisons, overlap_dmrs) # Overlapping DMRs that meet the >=50bp, >=3 CpG criteria dmr_ovl <- filter(all_ovl, passes_criteria) ``` # 5 Multi-way DMR overlap Also want overlap among larger group sets: - all offspring (Zygote + Larvae) x Sperm - all offspring (Zygote + Larvae) x Egg - all parents (Egg + Sperm) x Zygote - all parents (Egg + Sperm) x Larvae - all parents x all offspring (Zygote + Larvae + Egg + Sperm) same overlap criteria as before ``` r overlap_multi <- function(set_names) { gr_list <- dmrs[set_names] if (any(lengths(gr_list) == 0)) return(tibble()) dmr_cols <- function(gr, s) { DataFrame(setNames(list(start(gr), end(gr), gr$score), paste0(s, c("_DMR_start", "_DMR_end", "_score")))) } ovl <- gr_list[[1]] mcols(ovl) <- dmr_cols(ovl, set_names[1]) # iteratively intersect w/ each subsequent set, keeping only shared portion for (s in set_names[-1]) { nxt <- gr_list[[s]] hits <- findOverlaps(ovl, nxt) if (length(hits) == 0) return(tibble()) ovl_hit <- ovl[queryHits(hits)] nxt_hit <- nxt[subjectHits(hits)] new_ovl <- pintersect(ovl_hit, nxt_hit) mcols(new_ovl) <- cbind(mcols(ovl_hit), dmr_cols(nxt_hit, s)) ovl <- new_ovl } # CpGs tested in all datasets shared_cpg <- Reduce(subsetByOverlaps, tested[set_names]) tibble(chr = as.character(seqnames(ovl)), overlap_start = start(ovl), overlap_end = end(ovl), overlap_bp = width(ovl), overlap_nCG = countOverlaps(ovl, shared_cpg)) %>% bind_cols(as_tibble(as.data.frame(mcols(ovl)))) %>% mutate(passes_criteria = overlap_bp >= min_len & overlap_nCG >= min_cg, # concordant if all DMRs have the same sign concordant = apply(sign(as.matrix(pick(ends_with("_score")))), 1, function(x) n_distinct(x) == 1)) } multi_comparisons <- list( offspring_sperm = c("Zygote", "Larvae", "Sperm"), offspring_egg = c("Zygote", "Larvae", "Egg"), parents_zygote = c("Egg", "Sperm", "Zygote"), parents_larvae = c("Egg", "Sperm", "Larvae"), parents_offspring = c("Zygote", "Larvae", "Egg", "Sperm") ) multi_ovl <- map(multi_comparisons, overlap_multi) multi_dmr_ovl <- map(multi_ovl, ~ if (nrow(.x) == 0) .x else filter(.x, passes_criteria)) ``` # 6 Summary Want to know: - how many DMRs overlap at all (\>=1bp), and how many *still* overlap after applying the \>=50bp/\>=3 CpG criteria - For the set of overlaps that pass, what portion are directionally concordant ``` r ovl_summary <- comparisons %>% mutate(n_off_DMRs = map_int(off_name, ~ length(dmrs[[.x]])), n_par_DMRs = map_int(par_name, ~ length(dmrs[[.x]]))) %>% left_join(all_ovl %>% group_by(offspring, parent) %>% summarise(n_any_overlap = n(), n_pass = sum(passes_criteria), n_pass_concord = sum(passes_criteria & concordant), # distinct DMRs involved (one DMR can overlap >1 DMR in the other set) n_off_DMRs_pass = n_distinct(paste(chr, off_DMR_start)[passes_criteria]), n_par_DMRs_pass = n_distinct(paste(chr, par_DMR_start)[passes_criteria]), .groups = "drop"), by = c("off_name" = "offspring", "par_name" = "parent")) %>% mutate(across(starts_with("n_"), ~ replace_na(.x, 0L)), pct_off_DMRs_shared = 100 * n_off_DMRs_pass / n_off_DMRs, pct_concordant = 100 * n_pass_concord / n_pass) knitr::kable(ovl_summary, digits = 1) ``` | off_name | par_name | n_off_DMRs | n_par_DMRs | n_any_overlap | n_pass | n_pass_concord | n_off_DMRs_pass | n_par_DMRs_pass | pct_off_DMRs_shared | pct_concordant | |:---------|:---------|-----------:|-----------:|--------------:|-------:|---------------:|----------------:|----------------:|--------------------:|---------------:| | Zygote | Egg | 5128 | 11143 | 766 | 629 | 517 | 613 | 584 | 12.0 | 82.2 | | Zygote | Sperm | 5128 | 18493 | 1376 | 1186 | 1111 | 1151 | 1079 | 22.4 | 93.7 | | Larvae | Egg | 4590 | 11143 | 916 | 751 | 678 | 726 | 714 | 15.8 | 90.3 | | Larvae | Sperm | 4590 | 18493 | 1643 | 1390 | 1329 | 1346 | 1292 | 29.3 | 95.6 | For the multi-overlap ``` r multi_summary <- imap_dfr(multi_ovl, function(df, comp) { # comparisons w/ no overlaps return an empty tibble if (nrow(df) == 0) { return(tibble(comparison = comp, sets = paste(multi_comparisons[[comp]], collapse = " x "), n_any_overlap = 0L, n_pass = 0L, n_pass_concord = 0L)) } tibble(comparison = comp, sets = paste(multi_comparisons[[comp]], collapse = " x "), n_any_overlap = nrow(df), n_pass = sum(df$passes_criteria), n_pass_concord = sum(df$passes_criteria & df$concordant)) }) %>% mutate(pct_concordant = 100 * n_pass_concord / n_pass) knitr::kable(multi_summary, digits = 1) ``` | comparison | sets | n_any_overlap | n_pass | n_pass_concord | pct_concordant | |:------------------|:------------------------------|--------------:|-------:|---------------:|---------------:| | offspring_sperm | Zygote x Larvae x Sperm | 398 | 359 | 340 | 94.7 | | offspring_egg | Zygote x Larvae x Egg | 255 | 216 | 191 | 88.4 | | parents_zygote | Egg x Sperm x Zygote | 281 | 222 | 134 | 60.4 | | parents_larvae | Egg x Sperm x Larvae | 399 | 287 | 218 | 76.0 | | parents_offspring | Zygote x Larvae x Egg x Sperm | 129 | 108 | 90 | 83.3 | How many overlaps were dropped by each criterion? ``` r all_ovl %>% mutate(reason = case_when(passes_criteria ~ "Passes", overlap_bp < min_len & overlap_nCG < min_cg ~ "< 50bp and < 3 CpGs", overlap_bp < min_len ~ "< 50bp", TRUE ~ "< 3 CpGs")) %>% count(offspring, parent, reason) %>% pivot_wider(names_from = reason, values_from = n, values_fill = 0) %>% knitr::kable() ``` | offspring | parent | \< 3 CpGs | \< 50bp | \< 50bp and \< 3 CpGs | Passes | |:----------|:-------|----------:|--------:|----------------------:|-------:| | Larvae | Egg | 90 | 28 | 47 | 751 | | Larvae | Sperm | 123 | 40 | 90 | 1390 | | Zygote | Egg | 71 | 30 | 36 | 629 | | Zygote | Sperm | 94 | 36 | 60 | 1186 | # 7 Plot ``` r # combine pairwise and multi-way summaries all_summary <- bind_rows( ovl_summary %>% transmute(comparison = paste(off_name, "x", par_name), type = "Pairwise", n_pass, n_pass_concord), multi_summary %>% transmute(comparison = sets, type = "Multi-way", n_pass, n_pass_concord) ) %>% mutate(type = factor(type, levels = c("Pairwise", "Multi-way"))) all_summary %>% mutate(Discordant = n_pass - n_pass_concord) %>% dplyr::select(comparison, type, Concordant = n_pass_concord, Discordant) %>% pivot_longer(c(Concordant, Discordant), names_to = "direction", values_to = "n") %>% ggplot(aes(x = n, y = reorder(comparison, n), fill = direction)) + geom_col() + scale_fill_manual(values = c(Concordant = "steelblue", Discordant = "gray")) + # separate pairwise and multi-way, w/ panel heights proportional to # of comparisons facet_grid(type ~ ., scales = "free_y", space = "free_y") + labs(x = "Overlapping DMRs (>= 50bp, >= 3 CpGs)", y = NULL, fill = NULL, title = "Parent-offspring DMR overlap (CT-masked)") + theme_bw() + theme(legend.position = "bottom") ``` ![](13-DMR-overlap-CTmask_files/figure-gfm/unnamed-chunk-8-1.png) Distribution of overlap sizes for the overlaps that pass: ``` r # combine pairwise and multi-way passing overlaps all_dmr_ovl <- bind_rows( dmr_ovl %>% transmute(comparison = paste(offspring, "x", parent), overlap_bp, overlap_nCG), multi_dmr_ovl %>% # drop comparisons w/ no overlaps (empty tibbles have no columns) keep(~ nrow(.x) > 0) %>% imap_dfr(~ transmute(.x, comparison = paste(multi_comparisons[[.y]], collapse = " x "), overlap_bp, overlap_nCG)) ) %>% mutate(comparison = factor(comparison, levels = all_summary$comparison)) all_dmr_ovl %>% ggplot(aes(x = overlap_bp, y = overlap_nCG)) + geom_point(alpha = 0.3) + scale_x_log10() + scale_y_log10() + facet_wrap(~ comparison, ncol = 3) + labs(x = "Overlap length (bp)", y = "Tested CpGs in overlap") + theme_bw() ``` ![](13-DMR-overlap-CTmask_files/figure-gfm/unnamed-chunk-9-1.png) # 8 Save outputs Coordinates need to be converted back to 0-based BED starts ``` r dmr_ovl %>% mutate(bed_start = overlap_start - 1L) %>% group_by(offspring, parent) %>% group_walk(~ write_tsv( .x %>% select(chr, start = bed_start, end = overlap_end, overlap_bp, overlap_nCG, off_score, par_score, concordant, off_DMR_start, off_DMR_end, par_DMR_start, par_DMR_end), file.path("../output/13-DMR-overlap-CTmask", paste0("shared_", tolower(.y$offspring), "_", tolower(.y$parent), "_DMR.bed")), col_names = TRUE)) write_tsv(ovl_summary, "../output/13-DMR-overlap-CTmask/DMR_overlap_summary.tsv") iwalk(multi_dmr_ovl, function(df, comp) { if (nrow(df) == 0) return(invisible(NULL)) write_tsv(df %>% mutate(start = overlap_start - 1L) %>% dplyr::select(chr, start, end = overlap_end, overlap_bp, overlap_nCG, ends_with("_score"), concordant, ends_with("_DMR_start"), ends_with("_DMR_end")), file.path("../output/13-DMR-overlap-CTmask", paste0("shared_", comp, "_DMR.bed")), col_names = TRUE) }) write_tsv(multi_summary, "../output/13-DMR-overlap-CTmask/DMR_multi_overlap_summary.tsv") ``` NExt steps: Still need to test whether overlap exceeds what we’d expect by chance (e.g., by shuffling DMR positions within the set of tested loci and recounting overlaps).