I have multiple (n) bed files with identical intervals and one unique information column. I would like to combine these into one bed file with n information columns.
file1.bed:
chr start stop col.foo
chr1 1 10 foo
chr1 20 30 foo
file2.bed:
chr start stop col.bar
chr1 1 10 bar
chr1 20 30 bar
desired_output.bed:
chr start stop col.foo col.bar
chr1 1 10 foo bar
chr1 20 30 foo bar
I tried using bedtools merge, but don't see an obvious solution. This only keeps the information column of one of the files:
bedtools merge -c 4 -o collapse -delim = "\t" -i file1.bed -i file2.bed
3 answers
If you're comfortable using R, you can load the .bed files as tab delimited files, then full_join using chr, start, and stop as your join columns.
e.g
full_join(df_file1, df_file2, by = c("chr","start","stop"))
Since the files are in the same order and same intervals you can simply cut the information columns from each of the files and paste them onto the first threee locus columns from the first file.
In Linux there are utilities cut and paste.
Something like:
paste <(cut -f 1-3 file1.bed) <(cut -f 4 *.bed)
I ended up figuring out a way to use bedtools merge:
sort -k1,1 -k2,2n -s file*.bed | mergeBed -c 4 -o collapse -delim DELIM | sed 's/DELIM/\t/g'
Log in to answer this question.