# bedtools: get name column of alignment file A

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?

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.


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.