Replace BED intervals
2
0
Entering edit mode
10.0 years ago
mujupas ▴ 80

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 • 2.7k views
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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!

ADD REPLY
2
Entering edit mode
10.0 years ago
ole.tange ★ 4.5k
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
ADD COMMENT
0
Entering edit mode

Worked like a charm, thank you so much!

ADD REPLY
1
Entering edit mode
10.0 years ago

Consider using awk; generally:

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

Login before adding your answer.

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