Question: Bedtools subtract on a line-by-line basis
0
gravatar for mujupas
9 months ago by
mujupas50
Japan
mujupas50 wrote:

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 • 478 views
ADD COMMENTlink modified 9 months ago • written 9 months ago by mujupas50

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

ADD REPLYlink written 9 months ago by mujupas50
0
gravatar for Alex Reynolds
9 months ago by
Alex Reynolds24k
Seattle, WA USA
Alex Reynolds24k wrote:

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 COMMENTlink modified 9 months ago • written 9 months ago by Alex Reynolds24k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 569 users visited in the last hour