text: user439: 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.
  
  
  user1461: 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 "parallel") union, intersection, (asymmetric!) difference and gap between each element in x and its corresponding element in y.
  
  
  "pintersect"(x, y, resolve.empty=c("none", "max.start", "start.x"), ...) # x, y   IRanges objects.
  
  hits <- findOverlaps(gr1, gr2)
  
  width(pintersect(gr1[queryHits(hits)],
                   gr2[subjectHits(hits)]))
  [1]  2  6  4  6 10
  
  
original_text: 439: <p>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.</p>
  <p>In cases with discontinuous ranges, <code>intersect()</code> works, but I am interested in the case where both sets contain contiguous ranges (no gaps).</p>
  <p>I am using the findOverlaps function as in the example below:</p>
  <pre class="lang-r prettyprint-override"><code>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)
  </code></pre>
  <p>This code yields the following intersections in a &quot;Hits&quot; object:</p>
  <pre><code>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
  </code></pre>
  <p>But I could not find a way to extract the intersections widths from the resulting object.</p>
  <p>It this the wrong approach ?</p>
  <blockquote>
  <p>Note: Using <code>intersect(gr1, gr2)</code> 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.</p>
  </blockquote>
   1461: <p>Whilst <code>intersect</code> 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 <code>Grange</code> function to do this out of the box, but you could go through all overlapping pairs resulting from <code>findOverlap</code> and calculate it's widths using <a href="https://www.rdocumentation.org/packages/IRanges/versions/2.0.1/topics/setops-methods" rel="nofollow noreferrer">pintersect</a>:</p>
  <blockquote>
  <p>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.</p>
  </blockquote>
  <blockquote>
  <p>&quot;pintersect&quot;(x, y, resolve.empty=c(&quot;none&quot;, &quot;max.start&quot;, &quot;start.x&quot;), ...) # x, y   IRanges objects.</p>
  </blockquote>
  <pre><code>hits &lt;- findOverlaps(gr1, gr2)
  
  width(pintersect(gr1[queryHits(hits)],
                   gr2[subjectHits(hits)]))
  [1]  2  6  4  6 10
  </code></pre>
  