14.1-functional-annotation-DMLs-DMRs ================ Kathleen Durkin 2026-11-11 - [1 Setup](#1-setup) - [2 Load gene annotation](#2-load-gene-annotation) - [3 Load DML/DMR sets](#3-load-dmldmr-sets) - [4 Assign features to genes](#4-assign-features-to-genes) - [5 Join functional annotation](#5-join-functional-annotation) - [6 Summary](#6-summary) - [7 GOslim summary](#7-goslim-summary) - [8 Save outputs](#8-save-outputs) Use lookup table built in `14-functional-annotation` to functionally annotate our DML/DMR features of interest # 1 Setup ``` r library(GenomicRanges) library(readr) library(dplyr) library(tidyr) library(purrr) library(stringr) library(tibble) library(ggplot2) ``` # 2 Load gene annotation Gene coordinates from the NCBI GFF (`gene` features only). The `gene=` attribute contains the `LOC` ID that matches `gene_id` in the `14-functional-annotation` table. ``` r genes <- read_tsv("../data/GCF_002022765.2_C_virginica-3.0_genomic.gff.gz", comment = "#", col_types = "ccciicccc", col_names = c("chr", "source", "type", "start", "end", "score", "strand", "phase", "attributes")) %>% filter(type == "gene") %>% mutate(gene_id = str_extract(attributes, "(?<=gene=)[^;]+")) %>% dplyr::select(chr, start, end, strand, gene_id) %>% distinct(gene_id, .keep_all = TRUE) genes_gr <- GRanges(seqnames = genes$chr, ranges = IRanges(start = genes$start, end = genes$end), strand = genes$strand, gene_id = genes$gene_id) # strand-aware promoters (2kb upstream of TSS, not including the TSS/gene body) promoters_gr <- promoters(genes_gr, upstream = 2000, downstream = 0) length(genes_gr) ``` ## [1] 38322 Functional annotation lookup table from `14-functional-annotation` ``` r gene_annot <- read_tsv("../output/14-functional-annotation/Cvirginica_gene_annotation.tsv", col_types = cols(.default = "c")) %>% mutate(across(c(pident, evalue, bitscore), as.numeric)) %>% dplyr::select(gene_id, SPID, pident, evalue, protein_names, organism, GO_BP, GO_CC, GO_MF, GO_IDs, GOslim_BP_IDs, GOslim_BP_terms) nrow(gene_annot) ``` ## [1] 21123 # 3 Load DML/DMR sets - `coord`: `bed0` = 0-based BED start, `dss1` = 1-based start written as-is from `callDMR()` - `header`: whether file has column names ``` r feature_sets <- tribble( ~set, ~type, ~coord, ~header, ~path, # per-stage/per-sex DMLs "zygote_DML", "DML", "bed0", FALSE, "../output/10.01-diff-methyl-DSS-CTmask/zygote_DML.bed", "larvae_DML", "DML", "bed0", FALSE, "../output/10.01-diff-methyl-DSS-CTmask/larvae_DML.bed", "egg_DML", "DML", "bed0", FALSE, "../output/10.11-diff-methyl-DSS-parents-CTmask/female_DML.bed", "sperm_DML", "DML", "bed0", FALSE, "../output/10.11-diff-methyl-DSS-parents-CTmask/male_DML.bed", # per-stage/per-sex DMRs "zygote_DMR", "DMR", "dss1", FALSE, "../output/10.01-diff-methyl-DSS-CTmask/zygote_DMR.bed", "larvae_DMR", "DMR", "dss1", FALSE, "../output/10.01-diff-methyl-DSS-CTmask/larvae_DMR.bed", "egg_DMR", "DMR", "dss1", FALSE, "../output/10.11-diff-methyl-DSS-parents-CTmask/female_DMR.bed", "sperm_DMR", "DMR", "dss1", FALSE, "../output/10.11-diff-methyl-DSS-parents-CTmask/male_DMR.bed", # shared parent-offspring DMLs (exact CpG matches) "shared_zygote_egg_DML", "DML", "bed0", TRUE, "../output/10.11-diff-methyl-DSS-parents-CTmask/shared_zygote_egg_DML.bed", "shared_zygote_sperm_DML", "DML", "bed0", TRUE, "../output/10.11-diff-methyl-DSS-parents-CTmask/shared_zygote_sperm_DML.bed", "shared_larvae_egg_DML", "DML", "bed0", TRUE, "../output/10.11-diff-methyl-DSS-parents-CTmask/shared_larvae_egg_DML.bed", "shared_larvae_sperm_DML", "DML", "bed0", TRUE, "../output/10.11-diff-methyl-DSS-parents-CTmask/shared_larvae_sperm_DML.bed", "shared_zygote_parents_DML", "DML", "bed0", TRUE, "../output/10.11-diff-methyl-DSS-parents-CTmask/shared_zygote_parents_DML.bed", "shared_larvae_parents_DML", "DML", "bed0", TRUE, "../output/10.11-diff-methyl-DSS-parents-CTmask/shared_larvae_parents_DML.bed", "shared_egg_offspring_DML", "DML", "bed0", TRUE, "../output/10.11-diff-methyl-DSS-parents-CTmask/shared_egg_offspring_DML.bed", "shared_sperm_offspring_DML", "DML", "bed0", TRUE, "../output/10.11-diff-methyl-DSS-parents-CTmask/shared_sperm_offspring_DML.bed", # shared parent-offspring DMRs (>= 50bp, >= 3 CpG overlap; coords = overlapping portion) "shared_zygote_egg_DMR", "DMR", "bed0", TRUE, "../output/13-DMR-overlap-CTmask/shared_zygote_egg_DMR.bed", "shared_zygote_sperm_DMR", "DMR", "bed0", TRUE, "../output/13-DMR-overlap-CTmask/shared_zygote_sperm_DMR.bed", "shared_larvae_egg_DMR", "DMR", "bed0", TRUE, "../output/13-DMR-overlap-CTmask/shared_larvae_egg_DMR.bed", "shared_larvae_sperm_DMR", "DMR", "bed0", TRUE, "../output/13-DMR-overlap-CTmask/shared_larvae_sperm_DMR.bed", "shared_offspring_sperm_DMR", "DMR", "bed0", TRUE, "../output/13-DMR-overlap-CTmask/shared_offspring_sperm_DMR.bed", "shared_offspring_egg_DMR", "DMR", "bed0", TRUE, "../output/13-DMR-overlap-CTmask/shared_offspring_egg_DMR.bed", "shared_parents_zygote_DMR", "DMR", "bed0", TRUE, "../output/13-DMR-overlap-CTmask/shared_parents_zygote_DMR.bed", "shared_parents_larvae_DMR", "DMR", "bed0", TRUE, "../output/13-DMR-overlap-CTmask/shared_parents_larvae_DMR.bed", "shared_parents_offspring_DMR", "DMR", "bed0", TRUE, "../output/13-DMR-overlap-CTmask/shared_parents_offspring_DMR.bed" ) ``` ``` r # Read a DML/DMR file and return a tibble w/ 1-based, closed start/end # Retains any extra columns (score, *_diff, concordant, etc.) read_features <- function(path, coord, header) { # Only use file if not empty (features were identified) if (!file.exists(path) || file.size(path) == 0) return(tibble()) df <- if (header) { read_tsv(path, show_col_types = FALSE) } else { read_tsv(path, col_names = c("chr", "start", "end", "score"), show_col_types = FALSE) } # convert 0-based BED starts to 1-based if (coord == "bed0") df <- mutate(df, start = start + 1) mutate(df, start = as.integer(start), end = as.integer(end)) } # all features in a single long table, w/ unique ID per feature features <- feature_sets %>% mutate(data = pmap(list(path, coord, header), read_features)) %>% dplyr::select(set, type, data) %>% unnest(data) %>% mutate(feature_id = row_number()) feat_gr <- GRanges(seqnames = features$chr, ranges = IRanges(start = features$start, end = features$end), feature_id = features$feature_id) # summary features %>% count(set, type, name = "n_features") %>% knitr::kable() ``` | set | type | n_features | |:-----------------------------|:-----|-----------:| | egg_DML | DML | 14224 | | egg_DMR | DMR | 11143 | | larvae_DML | DML | 5214 | | larvae_DMR | DMR | 4590 | | shared_egg_offspring_DML | DML | 41 | | shared_larvae_egg_DML | DML | 308 | | shared_larvae_egg_DMR | DMR | 751 | | shared_larvae_parents_DML | DML | 29 | | shared_larvae_sperm_DML | DML | 887 | | shared_larvae_sperm_DMR | DMR | 1390 | | shared_offspring_egg_DMR | DMR | 216 | | shared_offspring_sperm_DMR | DMR | 359 | | shared_parents_larvae_DMR | DMR | 287 | | shared_parents_offspring_DMR | DMR | 108 | | shared_parents_zygote_DMR | DMR | 222 | | shared_sperm_offspring_DML | DML | 101 | | shared_zygote_egg_DML | DML | 122 | | shared_zygote_egg_DMR | DMR | 629 | | shared_zygote_parents_DML | DML | 21 | | shared_zygote_sperm_DML | DML | 1027 | | shared_zygote_sperm_DMR | DMR | 1186 | | sperm_DML | DML | 56725 | | sperm_DMR | DMR | 18493 | | zygote_DML | DML | 7192 | | zygote_DMR | DMR | 5128 | # 4 Assign features to genes Gene body and promoter overlaps ``` r # Return feature-gene pairs for all overlaps between features and `target` regions overlap_genes <- function(target, region_label) { hits <- findOverlaps(feat_gr, target) tibble(feature_id = feat_gr$feature_id[queryHits(hits)], gene_id = target$gene_id[subjectHits(hits)], region = region_label, distance = 0L) } genic_hits <- bind_rows(overlap_genes(genes_gr, "gene body"), overlap_genes(promoters_gr, "promoter")) %>% # if a feature is in both the body and promoter of the same gene, keep gene body mutate(region = factor(region, levels = c("gene body", "promoter"))) %>% arrange(feature_id, gene_id, region) %>% distinct(feature_id, gene_id, .keep_all = TRUE) %>% mutate(region = as.character(region)) ``` Nearest gene for features with no gene body/promoter overlap ``` r no_hit_gr <- feat_gr[!feat_gr$feature_id %in% genic_hits$feature_id] nearest <- distanceToNearest(no_hit_gr, genes_gr, ignore.strand = TRUE) intergenic_hits <- tibble(feature_id = no_hit_gr$feature_id[queryHits(nearest)], gene_id = genes_gr$gene_id[subjectHits(nearest)], region = "intergenic", distance = mcols(nearest)$distance) feature_genes <- bind_rows(genic_hits, intergenic_hits) ``` # 5 Join functional annotation ``` r features_annot <- features %>% left_join(feature_genes, by = "feature_id", relationship = "one-to-many") %>% left_join(gene_annot, by = "gene_id") ``` # 6 Summary Want to know, for each set: - how many features fall in gene bodies, promoters, or intergenic regions - how many unique genes the genic (gene body or promoter) features map to, and how many of those genes have a SwissProt hit/GO terms ``` r # feature-level region (if a feature maps to multiple genes, count it under its "best" region) region_counts <- features_annot %>% mutate(region = factor(region, levels = c("gene body", "promoter", "intergenic"), ordered = TRUE)) %>% group_by(set, feature_id) %>% summarise(region = min(region), .groups = "drop") %>% count(set, region) %>% # fill in 0s for regions with no features in a set complete(set, region, fill = list(n = 0)) %>% group_by(set) %>% mutate(pct = 100 * n / sum(n)) %>% ungroup() # table w/ counts and relative %s region_counts %>% pivot_wider(names_from = region, values_from = c(n, pct), names_vary = "slowest") %>% mutate(n_total = rowSums(across(starts_with("n_")))) %>% knitr::kable(digits = 1) ``` | set | n_gene body | pct_gene body | n_promoter | pct_promoter | n_intergenic | pct_intergenic | n_total | |:-----------------------------|------------:|--------------:|-----------:|-------------:|-------------:|---------------:|--------:| | egg_DML | 9696 | 68.2 | 690 | 4.9 | 3838 | 27.0 | 14224 | | egg_DMR | 7709 | 69.2 | 518 | 4.6 | 2916 | 26.2 | 11143 | | larvae_DML | 3439 | 66.0 | 279 | 5.4 | 1496 | 28.7 | 5214 | | larvae_DMR | 3046 | 66.4 | 264 | 5.8 | 1280 | 27.9 | 4590 | | shared_egg_offspring_DML | 20 | 48.8 | 0 | 0.0 | 21 | 51.2 | 41 | | shared_larvae_egg_DML | 162 | 52.6 | 9 | 2.9 | 137 | 44.5 | 308 | | shared_larvae_egg_DMR | 518 | 69.0 | 43 | 5.7 | 190 | 25.3 | 751 | | shared_larvae_parents_DML | 25 | 86.2 | 0 | 0.0 | 4 | 13.8 | 29 | | shared_larvae_sperm_DML | 619 | 69.8 | 37 | 4.2 | 231 | 26.0 | 887 | | shared_larvae_sperm_DMR | 915 | 65.8 | 77 | 5.5 | 398 | 28.6 | 1390 | | shared_offspring_egg_DMR | 153 | 70.8 | 13 | 6.0 | 50 | 23.1 | 216 | | shared_offspring_sperm_DMR | 230 | 64.1 | 19 | 5.3 | 110 | 30.6 | 359 | | shared_parents_larvae_DMR | 193 | 67.2 | 14 | 4.9 | 80 | 27.9 | 287 | | shared_parents_offspring_DMR | 73 | 67.6 | 7 | 6.5 | 28 | 25.9 | 108 | | shared_parents_zygote_DMR | 152 | 68.5 | 14 | 6.3 | 56 | 25.2 | 222 | | shared_sperm_offspring_DML | 83 | 82.2 | 1 | 1.0 | 17 | 16.8 | 101 | | shared_zygote_egg_DML | 88 | 72.1 | 5 | 4.1 | 29 | 23.8 | 122 | | shared_zygote_egg_DMR | 416 | 66.1 | 27 | 4.3 | 186 | 29.6 | 629 | | shared_zygote_parents_DML | 19 | 90.5 | 2 | 9.5 | 0 | 0.0 | 21 | | shared_zygote_sperm_DML | 638 | 62.1 | 104 | 10.1 | 285 | 27.8 | 1027 | | shared_zygote_sperm_DMR | 774 | 65.3 | 59 | 5.0 | 353 | 29.8 | 1186 | | sperm_DML | 39553 | 69.7 | 2571 | 4.5 | 14601 | 25.7 | 56725 | | sperm_DMR | 12611 | 68.2 | 895 | 4.8 | 4987 | 27.0 | 18493 | | zygote_DML | 4791 | 66.6 | 358 | 5.0 | 2043 | 28.4 | 7192 | | zygote_DMR | 3304 | 64.4 | 267 | 5.2 | 1557 | 30.4 | 5128 | ``` r region_counts %>% # keep sets in the same order as `feature_sets` mutate(set = factor(set, levels = feature_sets$set)) %>% group_by(set) %>% mutate(n_total = sum(n)) %>% ungroup() %>% ggplot(aes(x = region, y = n, fill = region)) + geom_col() + geom_blank(aes(y = n_total)) + geom_text(aes(label = paste0(round(pct, 1), "%")), vjust = -0.3, size = 3) + # headroom for labels scale_y_continuous(expand = expansion(mult = c(0, 0.1))) + scale_fill_manual(values = c("gene body" = "steelblue", "promoter" = "darkorange", "intergenic" = "gray")) + facet_wrap(~ set, scales = "free_y", ncol = 3) + labs(x = NULL, y = "Features (axis max = total features in set)", fill = NULL, title = "Genomic location of DMLs/DMRs (CT-masked)") + theme_bw() + theme(legend.position = "bottom", axis.text.x = element_blank(), axis.ticks.x = element_blank()) ``` ![](14.1-functional-annotation-DMLs-DMRs_files/figure-gfm/unnamed-chunk-1-1.png) ``` r genic_genes <- features_annot %>% filter(region %in% c("gene body", "promoter")) %>% distinct(set, gene_id, SPID, GO_IDs, GOslim_BP_IDs) genic_genes %>% group_by(set) %>% summarise(n_genes = n(), n_SwissProt_hit = sum(!is.na(SPID)), n_with_GO = sum(!is.na(GO_IDs)), n_with_GOslim_BP = sum(!is.na(GOslim_BP_IDs)), pct_annotated = 100 * n_SwissProt_hit / n_genes) %>% knitr::kable(digits = 1) ``` | set | n_genes | n_SwissProt_hit | n_with_GO | n_with_GOslim_BP | pct_annotated | |:-----------------------------|--------:|----------------:|----------:|-----------------:|--------------:| | egg_DML | 1173 | 803 | 786 | 673 | 68.5 | | egg_DMR | 5672 | 3918 | 3821 | 3223 | 69.1 | | larvae_DML | 855 | 594 | 580 | 486 | 69.5 | | larvae_DMR | 2750 | 1858 | 1805 | 1491 | 67.6 | | shared_egg_offspring_DML | 2 | 1 | 1 | 1 | 50.0 | | shared_larvae_egg_DML | 25 | 12 | 12 | 9 | 48.0 | | shared_larvae_egg_DMR | 525 | 355 | 342 | 275 | 67.6 | | shared_larvae_parents_DML | 6 | 3 | 3 | 2 | 50.0 | | shared_larvae_sperm_DML | 132 | 86 | 82 | 69 | 65.2 | | shared_larvae_sperm_DMR | 880 | 589 | 570 | 461 | 66.9 | | shared_offspring_egg_DMR | 155 | 107 | 106 | 86 | 69.0 | | shared_offspring_sperm_DMR | 245 | 160 | 157 | 125 | 65.3 | | shared_parents_larvae_DMR | 203 | 128 | 123 | 98 | 63.1 | | shared_parents_offspring_DMR | 82 | 53 | 53 | 44 | 64.6 | | shared_parents_zygote_DMR | 161 | 94 | 90 | 74 | 58.4 | | shared_sperm_offspring_DML | 20 | 14 | 13 | 10 | 70.0 | | shared_zygote_egg_DML | 19 | 16 | 15 | 14 | 84.2 | | shared_zygote_egg_DMR | 388 | 253 | 246 | 208 | 65.2 | | shared_zygote_parents_DML | 5 | 4 | 4 | 4 | 80.0 | | shared_zygote_sperm_DML | 98 | 69 | 67 | 57 | 70.4 | | shared_zygote_sperm_DMR | 743 | 493 | 479 | 403 | 66.4 | | sperm_DML | 3463 | 2387 | 2320 | 1935 | 68.9 | | sperm_DMR | 8017 | 5366 | 5217 | 4320 | 66.9 | | zygote_DML | 1046 | 706 | 684 | 568 | 67.5 | | zygote_DMR | 2875 | 1939 | 1881 | 1571 | 67.4 | # 7 GOslim summary Count unique genes per BP GOslim term for each set (genic features only) ``` r goslim_counts <- features_annot %>% filter(region %in% c("gene body", "promoter"), !is.na(GOslim_BP_terms)) %>% distinct(set, gene_id, GOslim_BP_terms) %>% separate_rows(GOslim_BP_terms, sep = ";") %>% count(set, GOslim_term = GOslim_BP_terms, name = "n_genes") %>% arrange(set, desc(n_genes)) write_tsv(goslim_counts, "../output/14.1-functional-annotation-DMLs-DMRs/GOslim_BP_counts_by_set.tsv") ``` Top 10 GOslims for the per-stage/sex DMRs and shared DMRs ``` r goslim_counts %>% filter(set %in% c("zygote_DMR", "larvae_DMR", "egg_DMR", "sperm_DMR", "shared_zygote_egg_DMR", "shared_zygote_sperm_DMR", "shared_larvae_egg_DMR", "shared_larvae_sperm_DMR", "shared_parents_offspring_DMR")) %>% group_by(set) %>% slice_max(n_genes, n = 10, with_ties = FALSE) %>% ungroup() %>% ggplot(aes(x = n_genes, y = tidytext::reorder_within(GOslim_term, n_genes, set))) + geom_col(fill = "steelblue") + tidytext::scale_y_reordered() + facet_wrap(~ set, scales = "free", ncol = 2) + labs(x = "Genes (gene body/promoter features)", y = NULL, title = "Top Biological Process GOslims (CT-masked)") + theme_bw() ``` ![](14.1-functional-annotation-DMLs-DMRs_files/figure-gfm/goslim-plot-1.png) # 8 Save outputs Drop columns that are all `NA` for that set Coordinates are converted back to 0-based BED starts ``` r features_annot %>% mutate(start = start - 1L) %>% group_by(set) %>% group_walk(~ write_tsv( .x %>% dplyr::select(where(~ !all(is.na(.x)))) %>% dplyr::select(-feature_id), file.path("../output/14.1-functional-annotation-DMLs-DMRs", paste0(.y$set, "_annotated.tsv")), na = "")) # all sets combined write_tsv(features_annot %>% mutate(start = start - 1L) %>% dplyr::select(-feature_id), "../output/14.1-functional-annotation-DMLs-DMRs/all_features_annotated.tsv.gz", na = "") ``` This only describes which functions are represented among DM features, so next steps are to ask whether any functions are *over*-represented, using an enrichment test (e.g. `topGO` or `goseq`). (NOte should use all *tested* CpGs (`tested_loci.bed.gz`) as the background, rather than the whole genome, since only genes with sufficient coverage could have been called as DM)