Answers.Body: <p>Presuming you don't have cases of multiple ovelaps</p>
  
  <pre class="lang-r prettyprint-override"><code>&gt; library(GenomicRanges)
  &gt; gr1 = GRanges(c("chrI", "chrI"), IRanges(start=c(1, 101), width=c(100, 100)), mcols=data.frame(foo=c(0.1, 0.2)))
  &gt; gr2 = GRanges(c("chrI"), IRanges(start=c(50), width=c(101)))
  &gt; o = findOverlaps(gr1, gr2)
  &gt; grl1 = split(gr1[queryHits(o)], 1:length(o)) # You can't mendoapply on a GRanges object
  &gt; grl2 = split(gr2[subjectHits(o)], 1:length(o))
  &gt; foo = function(x, y) {
  +     rv = x
  +     start(rv) = max(start(x), start(y))
  +     end(rv) = min(end(x), end(y))
  +     return(rv)
  + }
  &gt; unlist(mendoapply(foo, grl1, y=grl2))
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>GRanges object with 2 ranges and 1 metadata column:
      seqnames     ranges strand | mcols.foo
         &lt;Rle&gt;  &lt;IRanges&gt;  &lt;Rle&gt; | &lt;numeric&gt;
    1     chrI [ 50, 100]      * |       0.1
    2     chrI [101, 150]      * |       0.2
    -------
    seqinfo: 1 sequence from an unspecified genome; no seqlengths
  </code></pre>
  
  <p>It'd be more memory efficient to iterate over <code>o</code>, but this gives you the idea.</p>
  
  <p>In cases of multiple overlaps, one could use <code>intersect()</code>, then <code>findOverlaps()</code> with that and then continue on with the procedure demonstrated above (or iterate over the overlaps).</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-22T11:14:42.393Z
Answers.LastActivityDate: 2017-06-22T11:32:27.683Z
Answers.LastEditDate: 2017-06-22T11:32:27.683Z
Answers.UserDisplayName: Devon Ryan
Body: <p>I am trying to find intersection of two genomic ranges (gr1 and gr2) and keep metadata from one of them</p>
  
  <pre><code>gr1
  chrI [1, 100] * | 0.1
  chrI [101, 200] * | 0.2
  
  gr2
  chrI [50, 150] + | 
  </code></pre>
  
  <p><code>intersect(gr1, gr2)</code> will remove metadata, where
  <code>subsetByOverlaps(gr1, gr2)</code> return:</p>
  
  <pre><code>chrI [1, 100] * | 0.1
  chrI [101, 200] * | 0.2
  </code></pre>
  
  <p>where metadata is saved, but intersection is bigger than real (the same as in initial gr1 start/end)</p>
  
  <p>How can I get an intersection range and keep metadata?
  to get the range:</p>
  
  <pre><code>chrI [50, 100] * | 0.1
  chrI [101, 150] * | 0.2
  </code></pre>
  
Tags: <r><bioconductor>
Title: Intersection of two genomic ranges to keep metadata
ThreadText: # Intersection of two genomic ranges to keep metadata
  
  (Asked by: username_0 on 2017-06-22)
  
  I am trying to find intersection of two genomic ranges (gr1 and gr2) and keep metadata from one of them
  
  ```
  gr1
  chrI [1, 100] * | 0.1
  chrI [101, 200] * | 0.2
  
  gr2
  chrI [50, 150] + | 
  
  ```
  
  `intersect(gr1, gr2)` will remove metadata, where
  `subsetByOverlaps(gr1, gr2)` return:
  
  ```
  chrI [1, 100] * | 0.1
  chrI [101, 200] * | 0.2
  
  ```
  
  where metadata is saved, but intersection is bigger than real (the same as in initial gr1 start/end)
  
  How can I get an intersection range and keep metadata?
  to get the range:
  
  ```
  chrI [50, 100] * | 0.1
  chrI [101, 150] * | 0.2
  
  ```
  
  **Tags:** r, bioconductor
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-06-22. Score: 4 | Accepted answer
  
  Presuming you don't have cases of multiple ovelaps
  
  ```r
  > library(GenomicRanges)
  > gr1 = GRanges(c("chrI", "chrI"), IRanges(start=c(1, 101), width=c(100, 100)), mcols=data.frame(foo=c(0.1, 0.2)))
  > gr2 = GRanges(c("chrI"), IRanges(start=c(50), width=c(101)))
  > o = findOverlaps(gr1, gr2)
  > grl1 = split(gr1[queryHits(o)], 1:length(o)) # You can't mendoapply on a GRanges object
  > grl2 = split(gr2[subjectHits(o)], 1:length(o))
  > foo = function(x, y) {
  +     rv = x
  +     start(rv) = max(start(x), start(y))
  +     end(rv) = min(end(x), end(y))
  +     return(rv)
  + }
  > unlist(mendoapply(foo, grl1, y=grl2))
  
  ```
  
  The output is:
  
  ```
  GRanges object with 2 ranges and 1 metadata column:
      seqnames     ranges strand | mcols.foo
         <Rle>  <IRanges>  <Rle> | <numeric>
    1     chrI [ 50, 100]      * |       0.1
    2     chrI [101, 150]      * |       0.2
    -------
    seqinfo: 1 sequence from an unspecified genome; no seqlengths
  
  ```
  
  It'd be more memory efficient to iterate over `o`, but this gives you the idea.
  
  In cases of multiple overlaps, one could use `intersect()`, then `findOverlaps()` with that and then continue on with the procedure demonstrated above (or iterate over the overlaps).
  
  
  ---
  
  