I want to retrieve all overlaps between two sets of genomic intervals (GRanges) using Bioconductor. I am interested in the widths of the resulting overlapping ranges.
In cases with discontinuous ranges, intersect()
works, but I am interested in the case where both sets contain contiguous ranges (no gaps).
I am using the findOverlaps function as in the example below:
gr1 <- GRanges(seqnames="chr2", ranges=IRanges(c(1, 11, 21, 31), c(10, 20, 30, 40)))
gr2 <- GRanges(seqnames="chr2", ranges=IRanges(c(1, 5, 15), c(4, 14, 30)))
hits <- findOverlaps(gr1, gr2)
This code yields the following intersections in a "Hits" object:
Hits object with 5 hits and 0 metadata columns:
queryHits subjectHits
<integer> <integer>
[1] 1 1
[2] 1 2
[3] 2 2
[4] 2 3
[5] 3 3
-------
queryLength: 4 / subjectLength: 3
But I could not find a way to extract the intersections widths from the resulting object.
It this the wrong approach ?
Note: Using
intersect(gr1, gr2)
in this case yields a single contiguous overlap range spanning the whole chromosome, but I want each overlap to be returned independently, just like findOverlaps does.