Get overlap between 2 intervals using various functions from the IRanges package: findOverlaps, pintersect, queryHits and subjectHits.
For example, to get overlap between genes and TADs:
# Search for hit of genes in TADs
tad_gene_hits <- findOverlaps(query=gene_ranges_chr, subject= tad_ranges_chr)
# Compute percent overlap and filter the hits:
sizeOverlaps <- pintersect(gene_ranges_chr[queryHits(tad_gene_hits)], tad_ranges_chr[subjectHits(tad_gene_hits)])
sizeOverlapDT <- tad_gene_hits
# For each hit, get the bp of the overlap
metadata(sizeOverlapDT)$overlapBp <- width(sizeOverlaps)
metadata(sizeOverlapDT)$entrezID <- metadata(gene_ranges_chr)$entrezID[queryHits(tad_gene_hits)]
stopifnot(metadata(gene_ranges_chr)$chromo[queryHits(tad_gene_hits)] == chromo)
metadata(sizeOverlapDT)$region <- metadata(tad_ranges_chr)$region[subjectHits(tad_gene_hits)]
overlapDT <- data.frame(metadata(sizeOverlapDT), stringsAsFactors = FALSE)
# overlapBp entrezID region
(! See Hits objects from {S4Vectors} !)