Retrieve overlap widths with Bioconductor 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 &lt;- GRanges(seqnames=&quot;chr2&quot;, ranges=IRanges(c(1, 11, 21, 31), c(10, 20, 30, 40)))
gr2 &lt;- GRanges(seqnames=&quot;chr2&quot;, ranges=IRanges(c(1, 5, 15), c(4, 14, 30)))
hits &lt;- findOverlaps(gr1, gr2)

This code yields the following intersections in a &quot;Hits&quot; object:
Hits object with 5 hits and 0 metadata columns:
      queryHits subjectHits
      &lt;integer&gt;   &lt;integer&gt;
  [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.
Whilst intersect is sort of doing what you want, it merges adjacent overlaps together which is not what you want I suppose. There doesn't seem to be dedicated Grange function to do this out of the box, but you could go through all overlapping pairs resulting from findOverlap and calculate it's widths using pintersect:

punion, pintersect, psetdiff and pgap are generic functions that compute the element-wise (aka &quot;parallel&quot;) union, intersection, (asymmetric!) difference and gap between each element in x and its corresponding element in y.


&quot;pintersect&quot;(x, y, resolve.empty=c(&quot;none&quot;, &quot;max.start&quot;, &quot;start.x&quot;), ...) # x, y   IRanges objects.

hits &lt;- findOverlaps(gr1, gr2)

width(pintersect(gr1[queryHits(hits)],
                 gr2[subjectHits(hits)]))
[1]  2  6  4  6 10