Output The Overlapping Regions To New File
2
0
Entering edit mode
10.3 years ago

Hi I have 2 files

file1

contig1 10037203    10038203    blah
contig1    10037203    10038203    blah
contig1    10037203    10038203    blah

file2

contig1    997329    938329    blab11
contig1    10037329    10038329    blah11
contig1    10037329    10038329    blah11

I want to get my output as below

contig1 10037203    10038203    blah
contig1    10037203    10038203    blah
contig1    10037203    10038203    blah
contig1    10037329    10038329    blah11
contig1    10037329    10038329    blah11

i,e., if the overlap exists first output all the overlapping ones of file1 and then of file2. Can anybody help me know how to do that?

bed • 2.3k views
ADD COMMENT
2
Entering edit mode

No Pierre, I have already checked with bedtools. It gives m*n lines and I don't want that. For example if 3 lines in file 1 overlap with 2 lines in file 2 the output that bedtools produces is 3*2=6 lines but I want only 5 - 3 from the first file followed by 2 from the second

ADD REPLY
1
Entering edit mode

You just need to modify the options, -w -a -o to get what you need.

ADD REPLY
3
Entering edit mode
10.3 years ago
sjneph ▴ 690
sort-bed file1 > file1.sorted
sort-bed file2 > file2.sorted
bedops -e -1 file1.sorted file2.sorted > answer.bed    
bedops -e -1 file2.sorted file1.sorted >> answer.bed

The answer.bed file will not be sorted per sort-bed since you catted on the results. If you want to maintain sorted order, then change the final line above to:

bedops -e -1 file2.sorted file1.sorted | bedops -u - answer.bed > final.answer.bed
ADD COMMENT
2
Entering edit mode

Thank you very much it worked

ADD REPLY
0
Entering edit mode
10.3 years ago

Use BEDOPS bedmap and set operations for this problem:

$ sort-bed file1 > file1.sorted
$ sort-bed file2 > file2.sorted
$ bedmap --echo-map file1.sorted file2.sorted > 2_overlapped_by_1
$ bedmap --echo-map file2.sorted file1.sorted > 1_overlapped_by_2
$ bedops --everything 1_overlapped_by_2 2_overlapped_by_1 > answer.bed
ADD COMMENT

Login before adding your answer.

Traffic: 3020 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