Consider the following example from bedtools intersect help page. Say, there are 4 bed files as follows
$ cat query.bed
chr1 1 20
chr1 40 45
chr1 70 90
chr1 105 120
chr2 1 20
chr2 40 45
chr2 70 90
chr2 105 120
chr3 1 20
chr3 40 45
chr3 70 90
chr3 105 120
chr3 150 200
chr4 10 20
--
$ cat d1.bed
chr1 5 25
chr1 65 75
chr1 95 100
chr2 5 25
chr2 65 75
chr2 95 100
chr3 5 25
chr3 65 75
chr3 95 100
--
$ cat d2.bed
chr1 40 50
chr1 110 125
chr2 40 50
chr2 110 125
chr3 40 50
chr3 110 125
--
$ cat d3.bed
chr1 85 115
chr2 85 115
chr3 85 115
If we use bedtools like this
$ bedtools intersect -a query.bed \
-b d1.bed d2.bed d3.bed
chr1 5 20
chr1 40 45
chr1 70 75
chr1 85 90
chr1 110 120
chr1 105 115
chr2 5 20
chr2 40 45
chr2 70 75
chr2 85 90
chr2 110 120
chr2 105 115
chr3 5 20
chr3 40 45
chr3 70 75
chr3 85 90
chr3 110 120
chr3 105 115
It reports the interval for e.g. chr1 5 20
, however, I don't want that to be reported, because it is not present in d3.bed
. I know that it is being reported because it is overlapping in query.bed
and d1.bed
. But my requirement is different. How can I tweak bedtools
to get ONLY those records which are overlapping across all files.
This works but I cannot keep on writing names of all bed files one by one when I have 100's of files!
I know right :) But you should have had in your post that you got more than 3 bed files
It was a toy example from bedtools page as I had mentioned. Thanks Bastien Hervé for a quick response. I highly appreciate that :D
I offered a solution that generalizes to many files but ended up deleting it. Sorry and good luck!