This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Merge overlapping and adjacent features in the BED file having the same label in the name (4-th) column

Hi there!

My BED file looks like:

chr1   10   20   A
chr1   15   20   B
chr1   19   30   A
chr1   10   20   C
chr1   21   30   C

I'd like to merge overlapping or adjacent (i.e. having just a 1bp distance) features with the same label in the name (the 4-th) column of the BED file to get in result:

chr1   10   30   A
chr1   15   20   B
chr1   10   30   C

I've found a bedtools merge utilite, but it does not take a label into account when try to merge features in the BED file.

Thanks!

genome r

Split by "label" then reduce.

I'm wondering which tool i can use to do that?

2 answers

cut -f 4 input.bed | sort | uniq | while read C
do
     awk -v C=${C} '($4==C)' input.bed | sort -t $'\t' -k1,1 -k2,2n | bedtools merge >> result.bed
done

BEDOPS bedmap + bash + awk:

$ bedmap --echo-map-range --echo-map-id-uniq --delim '\t' <(awk -v FS="\t" -v OFS="\t" '{ id=$4; $4=$1; $1=id; print $0; }' in.bed | sort-bed - | bedops --range 1 --merge -) <(awk -v FS="\t" -v OFS="\t" '{ id=$4; $4=$1; $1=id; print $0; }' in.bed | sort-bed -) | awk -v FS="\t" -v OFS="\t" '{ chrom=$4; $4=$1; $1=chrom; print $0; }' | sort-bed -
chr1    10  30  A
chr1    10  30  C
chr1    15  20  B

Thanks for your quick reply! Just because i already have a bedtools installed on my PC, i've used solution suggested by Pierre Lindenbaum.

Log in to answer this question.