This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Bedtools subtract on a line-by-line basis

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

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

1 answer

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.

Log in to answer this question.