
Spatial mapping of 1D genomic features to 3D chromatin interaction targets
Source:R/annotation.R
annotate_peaks_and_loops.RdA function for loop annotation and target mapping, designed to integrate 1D genomic features with 3D chromatin architecture.
Loop Annotation: Classifies 3D spatial interactions (e.g., distal-to-promoter, promoter-to-promoter) using positional anchor labels relative to gene annotations. Important: anchor type
"E"denotes a positional distal/intergenic classification – it does not imply functional enhancer activity. Orthogonal chromatin data are required for functional interpretation.Feature-to-Target Mapping: Links 1D genomic features (e.g., GWAS risk SNPs, ATAC-seq peaks, ChIP-seq binding sites) to putative target genes via 3D chromatin contacts, providing a loop-based alternative to linear proximity-based assignments.
Usage
annotate_peaks_and_loops(
bedpe_file,
target_bed = NULL,
txdb = NULL,
org_db = NULL,
species = "hg38",
tss_region = c(-2000, 2000),
anchor_merge_gap = 0,
out_dir = "./results",
expr_matrix_file = NULL,
sample_columns = NULL,
project_name = "HiChIP",
color_palette = "Set2",
karyo_bin_size = 1e+05,
neighbor_hop = 0,
hub_percentile = 0.95,
hub_metric = c("unique_contacts", "total_loops"),
min_expr = 0,
conflict_strategy = c("biotype_first", "expression_first"),
co_dominance_ratio = 0.1,
biotype_order = c("protein", "small_ncRNA", "antisense", "lncRNA", "pseudogene"),
anchor_gap = -1L,
anchor_min_overlap = 1L,
anchor_min_frac = 0,
write_output = FALSE,
quiet = FALSE,
target_priority = c("promoter_then_distance", "distance_then_role")
)Arguments
- bedpe_file
Character. Path to a BEDPE file (at least 6 columns: chr1, start1, end1, chr2, start2, end2). Additional columns beyond 6 are retained in the output; anchor swapping only affects columns 1-6.
- target_bed
Optional path to a BED file of genomic features (e.g., ChIP-seq peaks, GWAS SNPs). When provided, these 1D regions are mapped to 3D target genes. Default:
NULL.- txdb
A
TxDbobject, a package name string, orNULLto auto-resolve fromspecies. Default:NULL.- org_db
An
OrgDbobject, a package name string, orNULLto auto-resolve fromspecies. Default:NULL.- species
Character. Genome assembly string (e.g.
"hg38","mm10","danRer11","dm6"). Default:"hg38". Whentxdbandorg_dbareNULL, auto-resolved from built-in species (hg38/hg19/mm10/mm9); for any other species you must passtxdbandorg_dbas objects or package name strings directly (e.g.txdb = TxDb.Dmelanogaster.UCSC.dm6.ensGene,org_db = "org.Dm.eg.db"). Thespeciesstring is also used for karyotype ideograms and JASPAR motif species filtering.- tss_region
Numeric vector of length 2. Promoter window around the TSS in bp. Default:
c(-2000, 2000)(+/-2 kb; typical for mammalian protein-coding genes; may need widening for broad domains like HOX clusters, or narrowing for compact genomes).- anchor_merge_gap
Integer. Merge loop anchors within this many bp on the same chromosome before building the connectivity graph. Default
0(no merging; anchors must match exactly on chr, start, end). Set to50--200when input BEDPE comes directly from a loop caller without prior replicate consolidation – the same biological anchor may appear with slightly different boundaries in different loop rows, which would otherwise fragment the graph. Not needed when input is pre-consolidated viaconsolidate_chromatin_loops.- out_dir
Character. Output directory for the Excel results file. Default:
"./results".- expr_matrix_file
Optional path to a normalised expression matrix (genes x samples). Accepts steady-state RNA-seq (TPM/FPKM), nascent transcription data (NET-seq, PRO-seq, GRO-seq, TT-seq), or CAGE-seq. Enables expression-aware conflict resolution. Default:
NULL.- sample_columns
Character vector or integer indices. Columns in
expr_matrix_fileto average for baseline expression. Default:NULL.- project_name
Character. Prefix for output files and plot titles. Default:
"HiChIP".- color_palette
Character. RColorBrewer palette name. Default:
"Set2".- karyo_bin_size
Integer. Bin width in base pairs (bp) for karyotype heatmaps. Default:
1e5(100 kb). Typical range: 5e4-5e5 depending on genome size and resolution.- neighbor_hop
Integer. k-hop ego-network expansion order via
igraph::ego().0(default) restricts to direct loop contacts.1additionally includes 2-hop expanded targets for exploratory network analysis. Values greater than1are not supported. Target gene assignment searches one additional hop (neighbor_hop + 1) to capture genes at the opposite anchor of directly connected loops.- hub_percentile
Numeric (0-1). Loop-count quantile for hub detection. Default:
0.95. Genes or distal elements with connectivity at or above this quantile are flagged as hubs. A minimum floor of 3 (promoter-centric) or 2 (distal) is applied to avoid false hubs in small datasets.- hub_metric
Character. Which connectivity count to use for hub detection.
"unique_contacts"(default): counts distinct neighbour anchor IDs, robust to duplicate/replicate loop rows."total_loops": counts all loop rows (backward compatible; may inflate hub calls when input contains unconsolidated replicates).- min_expr
Numeric. Minimum expression value for a gene to be considered active during anchor-level conflict resolution. Used only when
expr_matrix_fileis provided. Default:0(any non-zero expression). Increase to1or higher to require stronger evidence. Note: whenmin_expr = 0, the code uses a strict greater-than comparison (> 0) to exclude truly undetected genes; whenmin_expr >= 1, it uses>= min_expr. For nascent transcription data (NET-seq, PRO-seq), gene-body aggregated signal should be used as the quantitative input. Seerefine_loop_anchors_by_expressionfor the separatethresholdparameter that controls promoter reclassification.- conflict_strategy
Character. Conflict resolution order for overlapping gene assignments.
"biotype_first"(default): select the best biotype tier first, then apply expression filtering within that tier."expression_first": apply expression filtering across all biotypes first, then pick the best biotype among expressed candidates.- co_dominance_ratio
Numeric (0-1). In the expression tiebreaker step, genes with expression >=
co_dominance_ratio x max(expression)in the group are retained together. Default:0.1(i.e. within one order of magnitude). Lower values (e.g.0.01) retain more co-dominant candidates; higher values (e.g.0.5) are more stringent.- biotype_order
Character vector. Custom ordering of biotype categories for gene conflict resolution (passed to
resolve_gene_conflicts). Five keywords:"protein","small_ncRNA","antisense","lncRNA","pseudogene". Listed categories get top priority; unlisted keep their default relative order appended at the bottom. Default:c("protein", "small_ncRNA", "antisense", "lncRNA", "pseudogene").- anchor_gap
Integer. Candidate search radius in bp between a peak and a loop anchor.
-1L(default): strict physical overlap required. When>= 0, expands the candidate search window (e.g.200for cross-experiment coordinate shifts). Note: final retention always requires at leastanchor_min_overlapbp of physical overlap; this parameter only controls which candidates are evaluated, not whether proximity-only pairs are retained.- anchor_min_overlap
Integer. Minimum required physical overlap (bp) between a peak and an anchor. Default
1L: at least 1 bp of actual sequence overlap required – proximity-only pairs within theanchor_gapwindow but without physical overlap are excluded. Increase to10-100for broad peaks to avoid spurious boundary overlaps.- anchor_min_frac
Numeric (0-1). After the first two filters, what fraction of the peak width must physically overlap the anchor? Default
0: any fraction accepted. Set to0.1-0.5when peaks are broad (e.g. H3K27ac domains, 2-5 kb) so a 1 bp overlap does not link the entire broad peak. Ignored for point features (SNPs, eQTLs). Applied last, only to pairs that passedanchor_gapandanchor_min_overlap.- write_output
Logical. If
TRUE, write the Excel workbook toout_dir(default:FALSE). IfFALSE, return results without creating directories or files.- quiet
Logical. If
TRUE, suppress progress messages while preserving warnings. Default:FALSE.- target_priority
Character. How to prioritise among multiple candidate target genes per input feature. The policy applies only within primary target links (default
path_length <= 1); longer paths are reported separately asExpanded_Target_Genesand do not participate inAssigned_Target_Genesselection."promoter_then_distance"(default): within primary links, strict promoter genes outrank strict gene-body genes, which outrank structurally supported strict enhancer candidates; within each tier shorter paths win. Exception: direct strict promoter–promoter contacts (same loop, path0 + path1) co-assign both endpoints via union, because the technical endpoint orientation does not reflect biological regulatory direction."distance_then_role": within primary links, path-length dominates – the closest linked gene wins; at equal distance promoter beats gene-body (legacy behaviour). The policy affectsAssigned_Target_Genesonly.Regulated_promoter_genesalways reports all promoter-linked genes regardless of the chosen policy.
Value
A named list:
target_annotation– Target features (peaks) with gene assignments. Key columns include:All_Loop_Connected_Genes: Inclusive provenance union of all loop-anchor gene links. May include strict assignment-eligible targets and non-strict positional/enhancer candidates. Not a confirmed target-gene set.Regulated_promoter_genes: Promoter genes supported by loop-anchor context.Assigned_Target_Genes: Policy-based 3D assignment (default: promoter-first, then shorter path wins; seetarget_priority).*_Filledvariants: Linear nearest-gene fallback when strict 3D assignments are empty.Regulated_promoter_Evidence: Provenance ofRegulated_promoter_genes(e.g.,local_promoter_overlap,distal_promoter). Read withRegulated_promoter_genes; do not cross-reference withAssigned_Target_Genesor other columns.Regulated_promoter_Fallback_Evidence: Provenance ofRegulated_promoter_genes_Filled. Read withRegulated_promoter_genes_Filled; indicates which*_Filledcolumn supplied the fallback gene.
target_gene_links– Long-format peak-gene provenance table. Each row records one peak-gene linkage with full provenance. Readevidence,anchor_role, andgene_roletogether as a group – they jointly describe how each gene was assigned to each peak; do not interpret any one column in isolation.input_id,loop_ID,anchor_id: Identifiers.gene: Linked gene symbol.gene_role:"promoter","gene_body","enhancer_candidate","positional_candidate", or"linear_annotation".source:"loop_anchor"(3D-derived) or"linear_annotation"(nearest gene).evidence: Provenance label –"local_promoter_overlap"(peak overlaps anchor promoter),"distal_promoter"(promoter on the distal loop anchor),"gene_body_context"/"distal_gene_body_context"(gene body linkage),"local_enhancer_candidate"/"distal_enhancer_candidate"/"expanded_enhancer_candidate"(enhancer-associated linkage),"expanded_promoter_loop"(via ego-network expansion),"linear_annotation"(direct nearest gene), or"linear_fallback"(filled when 3D assignment was empty).anchor_role:"local_anchor","opposite_anchor","expanded_anchor", or"linear_annotation".used_as_fallback: Logical.TRUEwhen this link was added via the*_Filledlinear nearest-gene fallback mechanism.in_regulated_promoterthroughin_assigned_target_filled: Logical membership flags indicating which target annotation column(s) this gene appears in.
loop_annotation– Annotated 3D interactome withPutative_Target_Genes(all P/G anchor genes connected through the loop network) andPromoter_Target_Genes(promoter-only subset, P-side genes). G–P/P–G asymmetry is respected. Does not include linear nearest-gene fallback.anchor_loci_annotation– Non-redundant anchor-locus genomic classifications after within-cluster interval reduction.anchor_annotation– Backward-compatible alias ofanchor_loci_annotation.promoter_centric_stats– Gene-level connectivity statistics.distal_element_stats– Distal anchor connectivity (E, dual, G, eG).plots– Named list of ggplot/grob objects:Basic_Donut,Basic_Circular,Basic_Flower,Karyo_LoopGenes,Karyo_Anchors,Anchor_Genomic_Distribution, and (whentarget_bedis provided)Karyo_TargetGenes,Target_Rose,Target_Genomic_Distribution,Target_Loop_Genomic_Distribution.plot_list– Backward-compatible alias ofplots.metadata– Internal metadata list (parameters, versions). Not intended for direct use.
The returned object carries a looplook_anchor_state attribute (access via
attr(result, "looplook_anchor_state")) containing internal anchor
topology data required by refine_loop_anchors_by_chromatin
with recompute_targets = TRUE.
If write_output = TRUE, also writes a multi-sheet Excel workbook to out_dir.
Details
Mapping Strategy and Fallback Mechanism
The method prioritizes physical 3D chromatin contacts while keeping strict
and fallback semantics separate. Regulated_promoter_genes reports
promoter genes supported by loop-anchor context, Assigned_Target_Genes
preserves the historical promoter-first 3D assignment, and *_Filled
columns add a linear nearest-gene fallback only when strict 3D assignments are
empty. Regulated_promoter_Evidence,
Regulated_promoter_Fallback_Evidence, and target_gene_links
provide row-level provenance for these decisions.
Hierarchical Conflict Resolution To address complex loci where a single anchor overlaps multiple promoters (e.g., dense gene clusters or bidirectional promoters), the function executes a 3-step resolution:
Biotype Prioritization: Selects the highest-priority candidates by functional class:
Protein Coding > small-ncRNA (miRNA, snoRNA, snRNA, rRNA, scaRNA) > Antisense > lncRNA/ncRNA > Pseudogene.Expression Filter: Within the selected biotype tier, excludes transcriptionally silent genes using a user-provided expression matrix. If no gene in the tier is expressed, all candidates in that tier are retained.
Expression Tiebreaker: Among remaining candidates of equal biotype priority, retains all genes whose expression >=
co_dominance_ratiox the group maximum. Default0.1(one order of magnitude; ~3.3-fold in log2 space). This co-dominant rule preserves functionally redundant candidates such as bidirectional promoter pairs, where co-expressed partners typically fall within 2-3 fold of each other. Edge case: when all candidates in the best biotype tier have TPM = 0, all are retained (the tiebreaker cannot distinguish them), which may produce multi-gene assignments at transcriptionally silent loci.
Network Topology Analysis
Ego-Network Expansion (
neighbor_hop): Implements k-hop neighborhood expansion viaigraph::ego(). A value of0restricts to direct contacts, while1includes secondary contacts to capture broader regulatory cliques.Hub Detection: Utilizes a node-degree quantile threshold (
hub_percentile) to identify highly connected regulatory elements.
Peak-Anchor Overlap Control
When target_bed is provided, three parameters control how target peaks
are matched to loop anchors. They act as a cascade of increasingly stringent
filters:
anchor_gap– expands the search radius around each anchor.anchor_min_overlap– requires a minimum physical overlap in bp.anchor_min_frac– requires the overlap to cover a minimum fraction of the peak.
The table below lists suggested starting points for common experimental designs.
| Experimental design | anchor_gap | anchor_min_overlap | anchor_min_frac |
| Same-experiment HiChIP / ChIA-PET (default) | -1 | 1 | 0 |
| Cross-experiment (e.g. ATAC-seq peaks x HiChIP loops) | 200-500 | 10 | 0 |
| Broad histone-mark peaks (H3K27ac, 2-5 kb) | 0 | 100 | 0.1 |
| Super-enhancers / wide domains (20-80 kb) | 0 | 500 | 0.05 |
| Point features (GWAS SNPs, eQTLs, 1 bp) | 0 | 1 | 0 |
| Stringent (high-confidence only) | 0 | 100 | 0.5 |
When quiet = FALSE (default), the function prints a diagnostic line
reporting how many peaks overlapped loop anchors after filtering, helping you
tune these thresholds for your data.
See also
refine_loop_anchors_by_expression for expression-aware refinement,
refine_loop_anchors_by_chromatin for chromatin-aware reclassification,
profile_target_genes for automated functional profiling.
Examples
# Minimal runnable example for package checks
if (requireNamespace("org.Hs.eg.db", quietly = TRUE)) {
txdb_example <- AnnotationDbi::loadDb(
system.file("extdata", "hg19_knownGene_sample.sqlite", package = "GenomicFeatures")
)
bedpe_path <- tempfile(fileext = ".bedpe")
writeLines(
"chr6\t10412000\t10412600\tchr6\t10415000\t10415600",
bedpe_path
)
res <- annotate_peaks_and_loops(
bedpe_file = bedpe_path,
txdb = txdb_example,
org_db = "org.Hs.eg.db",
species = "hg19",
out_dir = tempdir(),
project_name = "Quick_Example",
write_output = FALSE,
quiet = TRUE
)
head(res$loop_annotation, 1)
}
#>
#> Loading required package: GenomicFeatures
#> Loading required package: BiocGenerics
#> Loading required package: generics
#>
#> Attaching package: 'generics'
#> The following objects are masked from 'package:base':
#>
#> as.difftime, as.factor, as.ordered, intersect, is.element, setdiff,
#> setequal, union
#>
#> Attaching package: 'BiocGenerics'
#> The following objects are masked from 'package:stats':
#>
#> IQR, mad, sd, var, xtabs
#> The following objects are masked from 'package:base':
#>
#> Filter, Find, Map, Position, Reduce, anyDuplicated, aperm, append,
#> as.data.frame, basename, cbind, colnames, dirname, do.call,
#> duplicated, eval, evalq, get, grep, grepl, is.unsorted, lapply,
#> mapply, match, mget, order, paste, pmax, pmax.int, pmin, pmin.int,
#> rank, rbind, rownames, sapply, saveRDS, table, tapply, unique,
#> unsplit, which.max, which.min
#> Loading required package: S4Vectors
#> Loading required package: stats4
#>
#> Attaching package: 'S4Vectors'
#> The following object is masked from 'package:utils':
#>
#> findMatches
#> The following objects are masked from 'package:base':
#>
#> I, expand.grid, unname
#> Loading required package: IRanges
#> Loading required package: Seqinfo
#> Loading required package: GenomicRanges
#> Loading required package: AnnotationDbi
#> Loading required package: Biobase
#> Welcome to Bioconductor
#>
#> Vignettes contain introductory material; view with
#> 'browseVignettes()'. To cite Bioconductor, see
#> 'citation("Biobase")', and for packages 'citation("pkgname")'.
#>
#> Loading required package: org.Hs.eg.db
#> 'select()' returned 1:1 mapping between keys and columns
#> # A tibble: 1 × 19
#> loop_ID chr1 start1 end1 chr2 start2 end2 cluster_id loop_type
#> <chr> <chr> <dbl> <dbl> <chr> <dbl> <dbl> <chr> <chr>
#> 1 L1 chr6 10412001 10412600 chr6 10415001 10415600 1 P-P
#> # ℹ 10 more variables: anchor1_gene <chr>, anchor1_type <chr>,
#> # anchor2_gene <chr>, anchor2_type <chr>, Cluster_All_Genes <chr>,
#> # All_Anchor_Genes <chr>, Putative_Target_Genes <chr>,
#> # Promoter_Target_Genes <chr>, a1_id <chr>, a2_id <chr>