This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How Can I Include One Bed File In Another Bed File ?

Hello, I have 2 bedfiles that share some common features let's call the first file A.bed (bigger file) and the second B.bed (smaller file). I would like to have a new bed file that includes everything in B.bed in the A.bed file. I don't need the intersect, I more like need the merge option I checked bedtools's manual... couldn't find an answer for merging 2 bedfiles. Can someone help?

Thanks in advance

bedtools merge

3 answers

You can use -

cat A.bed B.bed > AB.bed

mergeBed -i AB.bed

mergeBed will do the trick, but bear in mind that it requires that the input file be sorted by chromosome, then by start position. Since AB.bed probably won't be like that by default, try cat A.bed B.bed | sort -k1,1 -k2,2n | mergeBed > AB.bed instead.

use bedops --merge a.bed b.bed > merged.bed

the requirements are that both bed files are sorted, which is easy and smart to do, as it makes all downstream analyses more efficient. The output is already sorted, so you only need to do this once for initial, unsorted files.

sort-bed A.bed > a.bed && sort-bed B.bed > b.bed

the suite offers the fastest execution times and least memory overhead. You can use any number of any size inputs at once.

This is classic file-handling, no need to look for a BedTools command, this is enough:

cat A.bed B.bed > C.bed

If your bed files have headers, you can remove them with sed '1d' X.bed.

but how will it deal with the overlapping sequences?

It won't, it just concatenates the files (i.e., it copies one onto the end of the other). You may want mergeBed, as suggested above, instead, depending on what you mean by merge.

I assumed you wanted to preserve all features as originally, but you can merge them with mergeBed as suggested if that's your goal.

Log in to answer this question.