<p>I would recommend using <a href="https://bedtools.readthedocs.io/en/latest/content/tools/intersect.html" rel="nofollow noreferrer">bedtools intersect</a> for that type of comparison.  This will allow you to see the specific segments that overlap.  bedtools coverage is used more for determining how many things cover a region but not which ones.  </p>

<p>The documentation linked above will provide a much better desciption of intersect than I would be able to in this post but I would recommend using the <code>-loj</code> or <code>-u</code> depending on what you want.</p>

<pre><code>-loj    Perform a “left outer join”. That is, for each feature in A report each overlap with B. If no overlaps are found, report a NULL feature for B.

-u  Write original A entry once if any overlaps found in B. In other words, just report the fact at least one overlap was found in B. Restricted by -f and -r.
</code></pre>

CC BY-SA 4.0
2019-03-06T03:32:19.697Z
2019-03-06T03:32:19.697Z
Bioathlete
<p>If you're working with <code>fileB.bed</code> (a <a href="https://bedops.readthedocs.io/en/latest/content/reference/file-management/sorting/sort-bed.html" rel="nofollow noreferrer">sorted</a> BED file) and <code>fileA.txt</code> (a three-column text file, where the name is in the third column), then you could pipe single-base pseudo-BED elements to <a href="https://bedops.readthedocs.io/en/latest/content/reference/statistics/bedmap.html" rel="nofollow noreferrer"><code>bedmap</code></a>. </p>

<pre><code><span class="math-container">$ bedmap --echo --echo-map-id --delim '\t' fileB.bed &lt;(awk -vFS="\t" -vOFS="\t" '{ print $1, $2, ($2+1), $</span>3 }' fileA.txt) &gt; answer.bed
</code></pre>

<p>The <code>--echo-map-id</code> option reports the list of marker IDs from <code>fileA.txt</code>, where the single-base marker interval from <code>fileA.txt</code> overlaps a region in <code>fileB.bed</code>.</p>

<p>If all the marker IDs are not unique (if there are duplicate marker IDs) it may make sense to instead use <code>--echo-map-id-uniq</code>.</p>

<p>This assumes that marker positions in the second column and that the intervals in <code>fileB.bed</code> are using a 0-based index scheme. If marker positions are 1-based and <code>fileB.bed</code> is 0-based, then a small adjustment is needed to make both inputs use the same indexing, to calculate correct overlaps:</p>

<pre><code><span class="math-container">$ bedmap --echo --echo-map-id --delim '\t' fileB.bed &lt;(awk -vFS="\t" -vOFS="\t" '{ print $1, ($2-1), $2, $</span>3 }' fileA.txt) &gt; answer.bed
</code></pre>

<p>I mention this, as SNVs (such as those from VCF) can often be reported with a 1-based index scheme, which adds some nuance to doing set operations against BED files.</p>

CC BY-SA 4.0
2019-03-06T07:26:23.707Z
2019-03-07T18:54:32.140Z
2019-03-07T18:54:32.140Z
Alex Reynolds
<p>I'm using the <code>coverage</code> function of <code>bedtools</code> to check whether a certain set of regions (file B) has any overlap with known regions of epigenetic markers (file A). When that is the case, I want to access the name of that marker, which is given as the third column (name) in file A. </p>

<p>However, when I run <code>bedtools coverage -a fileA.bed -b fileB.bed</code>, I only get the columns of file B as an output, together with the overlap fraction. Is there a built-in way of accessing also the columns of the file A to which I aligned, or an easy way of cross-referencing to it?</p>

<read-mapping><bedtools>
bedtools: get name column of alignment file A
# bedtools: get name column of alignment file A

(Asked by: username_0 on 2019-03-05)

I'm using the `coverage` function of `bedtools` to check whether a certain set of regions (file B) has any overlap with known regions of epigenetic markers (file A). When that is the case, I want to access the name of that marker, which is given as the third column (name) in file A.

However, when I run `bedtools coverage -a fileA.bed -b fileB.bed`, I only get the columns of file B as an output, together with the overlap fraction. Is there a built-in way of accessing also the columns of the file A to which I aligned, or an easy way of cross-referencing to it?

**Tags:** read-mapping, bedtools

---

## 2 answers

### Answer by username_1 on 2019-03-06. Score: 2 | Accepted answer

I would recommend using bedtools intersect for that type of comparison.  This will allow you to see the specific segments that overlap.  bedtools coverage is used more for determining how many things cover a region but not which ones.  
The documentation linked above will provide a much better desciption of intersect than I would be able to in this post but I would recommend using the -loj or -u depending on what you want.
-loj    Perform a “left outer join”. That is, for each feature in A report each overlap with B. If no overlaps are found, report a NULL feature for B.

-u  Write original A entry once if any overlaps found in B. In other words, just report the fact at least one overlap was found in B. Restricted by -f and -r.


---

### Answer by username_2 on 2019-03-06. Score: 2

If you're working with fileB.bed (a sorted BED file) and fileA.txt (a three-column text file, where the name is in the third column), then you could pipe single-base pseudo-BED elements to bedmap. 
$ bedmap --echo --echo-map-id --delim '\t' fileB.bed <(awk -vFS="\t" -vOFS="\t" '{ print $1, $2, ($2+1), $3 }' fileA.txt) > answer.bed

The --echo-map-id option reports the list of marker IDs from fileA.txt, where the single-base marker interval from fileA.txt overlaps a region in fileB.bed.
If all the marker IDs are not unique (if there are duplicate marker IDs) it may make sense to instead use --echo-map-id-uniq.
This assumes that marker positions in the second column and that the intervals in fileB.bed are using a 0-based index scheme. If marker positions are 1-based and fileB.bed is 0-based, then a small adjustment is needed to make both inputs use the same indexing, to calculate correct overlaps:
$ bedmap --echo --echo-map-id --delim '\t' fileB.bed <(awk -vFS="\t" -vOFS="\t" '{ print $1, ($2-1), $2, $3 }' fileA.txt) > answer.bed

I mention this, as SNVs (such as those from VCF) can often be reported with a 1-based index scheme, which adds some nuance to doing set operations against BED files.


---

