Answers.Body: <p><a href="https://bedtools.readthedocs.io/en/latest/index.html" rel="nofollow noreferrer">bedtools</a> is my go-to program for operations on genomic intervals. In particular, the <a href="https://bedtools.readthedocs.io/en/latest/index.html" rel="nofollow noreferrer">bedtools intersect</a> operation is what you're looking for here.</p>
  
  <pre><code>$ cat fileA
  chr1    25      50
  chr1    75      200
  $ cat fileB
  chr1    10      60
  chr1    80      90
  $ bedtools intersect -a fileA -b fileB                                                           
  chr1    25      50
  chr1    80      90
  </code></pre>
  
  <p>Make sure that the values are separated by tabs rather than spaces, or bedtools will have trouble reading the files.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-09T20:38:59.933Z
Answers.LastActivityDate: 2019-07-09T20:38:59.933Z
Answers.UserDisplayName: Daniel Standage
Body: <p>I have two files of genomic coordinates. I am trying to output only the overlapping region of fileA if it intersects with fileB. Not the original fileA coordinates.</p>
  
  <p>fileA</p>
  
  <pre><code>chr1 25 50
  chr1 75 200
  </code></pre>
  
  <p>fileB</p>
  
  <pre><code>chr1 10 60
  chr1 80 90
  </code></pre>
  
  <p>desired output</p>
  
  <pre><code>chr1 25 50
  chr1 80 90
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-07-09T20:33:01.857Z
Comments.Text: Please provide a small reproducible example using `dput` so we can help you. All you need to do is run `dput(fileA)` in R and, paste the output in the terminal.
Comments.UserDisplayName: h3ab74
Tags: <genome><interval>
Title: How to output only the overlapping region of two files?
ThreadText: # How to output only the overlapping region of two files?
  
  (Asked by: username_0 on 2019-07-09)
  
  I have two files of genomic coordinates. I am trying to output only the overlapping region of fileA if it intersects with fileB. Not the original fileA coordinates.
  
  fileA
  
  ```
  chr1 25 50
  chr1 75 200
  
  ```
  
  fileB
  
  ```
  chr1 10 60
  chr1 80 90
  
  ```
  
  desired output
  
  ```
  chr1 25 50
  chr1 80 90
  
  ```
  
  **Tags:** genome, interval
  
  > **Comments:**
  > **username_1**: Please provide a small reproducible example using `dput` so we can help you. All you need to do is run `dput(fileA)` in R and, paste the output in the terminal.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-07-09. Score: 3
  
  [bedtools](https://bedtools.readthedocs.io/en/latest/index.html) is my go-to program for operations on genomic intervals. In particular, the [bedtools intersect](https://bedtools.readthedocs.io/en/latest/index.html) operation is what you're looking for here.
  
  ```
  $ cat fileA
  chr1    25      50
  chr1    75      200
  $ cat fileB
  chr1    10      60
  chr1    80      90
  $ bedtools intersect -a fileA -b fileB                                                           
  chr1    25      50
  chr1    80      90
  
  ```
  
  Make sure that the values are separated by tabs rather than spaces, or bedtools will have trouble reading the files.
  
  
  ---
  
  