Bedtools subtract on a line-by-line basis
1
0
Entering edit mode
7.2 years ago
mujupas ▴ 80

Hi,

is there a way to restrict the behaviour of bedtools to a line-by-line comparison?

To be more clear, if I have 2 BED files:

File_A:

chr1    1   100
chr2    1   100

and

File_B:

chr2    50  70
chr3    1   100

I'd like bedtools subtract to eliminate from File_A the interval contained in one line only if the interval in the corresponding line in file_B overlap. Therefore, in the present example no intervals should be subtracted from File_A, but I think by default bedtools is scanning one interval of File_A against all other intervals of File_B.

Is this possible?

Thank you in advance!

bedtools • 2.3k views
ADD COMMENT
0
Entering edit mode

Thanks a lot Alex, I'll try this out. Cheers!

ADD REPLY
0
Entering edit mode
7.2 years ago

To do this with BEDOPS bedops --difference and bash:

$ while read A <&3 && read B <&4; do bedops --difference <(echo -e "${A}") <(echo -e "${B}"); done 3<A.bed 4<B.bed

Given:

$ cat A.bed
chr1    1   100
chr2    1   100

$ cat B.bed
chr2    50  70
chr3    1   100

This gives the following result:

$ while read A <&3 && read B <&4; do bedops --difference <(echo -e "${A}") <(echo -e "${B}"); done 3<A.bed 4<B.bed
chr1    1   100
chr2    1   100

To confirm the line-by-line comparison, you could replace bedops --difference with paste:

$ while read A <&3 && read B <&4; do paste <(echo -e "${A}") <(echo -e "${B}"); done 3<A.bed 4<B.bed
chr1    1   100 chr2    50  70
chr2    1   100 chr3    1   100

Note that bedops --difference is directional, i.e. it depends upon the ordering of inputs. For instance:

$ while read A <&3 && read B <&4; do bedops --difference <(echo -e "${B}") <(echo -e "${A}"); done 3<A.bed 4<B.bed
chr2    50  70
chr3    1   100

Of course, to make this work reliably, you will first want to verify that both A.bed and B.bed have the same number of lines before you do any of this. You could, for example, test and compare with wc -l and a numerical comparison test operation in bash.

ADD COMMENT

Login before adding your answer.

Traffic: 1955 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6