This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Replace BED intervals

Hi!

I want to make all the intervals in a series of BED files equal to 1bp by taking the middle position of each interval.

Any suggestion about how to do it (possibly also with GNU Parallel?)

Example:

chr1   150   170   +

I'd like to replace it as:

chr1   160   161   +

Thank you in advance!

script bed

Ok, thanks a lot, I'll give it a try!

Thanks, it worked.

The only problem is that now the new 1bp BED files are using spaces as separators and not TAB.

I've tried the following command but it doesn't work in replacing spaces with tabs:

awk=$'\'{OFS="\\t"}{print $1"\t"int(($3-$2)/2)+$2"\t"int(($3-$2)/2)+$2+1}\''
[me@me]$ echo $awk
'{OFS="\t"}{print $1" "int(($3-$2)/2)+$2" "int(($3-$2)/2)+$2+1}'
parallel 'cat {} | awk '$awk' {} > {.}.output' ::: *.input

Any idea?

Thanks in advance!

2 answers

midbed() { 
  perl -ane '$F[1]=int(($F[1]+$F[2])/2);$F[2]=$F[1]+1;print join("\t",@F)."\n"' $1;
}
export -f midbed
parallel midbed {} '>' {.}.mbed ::: *.bed

Worked like a charm, thank you so much!

Consider using awk; generally:

$ awk '{print $1"\t"int(($3-$2)/2)+$2"\t"int(($3-$2)/2)+$2+1}' foo.bed > bar.bed

Log in to answer this question.