This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Convert per base bedgraph to Bed format

Hi everyone,

I have a bed graph file which contains par base intervals. I would like to convert this file to Bed format where we have longer intervals. Here is the BedGraph file.

PvP01_01_v1   0     1   0
PvP01_01_v1   1     2   3.39882
PvP01_01_v1   2     5   3.81385
PvP01_01_v1   5     7   2.81385
PvP01_01_v1   7     10  3.55081

Is there any tool that does the job?

Thanks,

bedgraph perbase bed bedtools

2 answers

You can use bedtools merge. On your example data, this will create a single entry in the format:

PvP01_01_v1   0   10

It's not clear whether the information in your fourth column here is relevant for your future analyses, but you will likely lose that information.

Just for information, it is possible to retain the information of the 4th column by using bedtools merge options -c 4 and -o mean (among other possible operators). It will take the mean of the original 4th column values that overlap the new interval.

Hell yea, thanks Carlo!

Convert to sorted bed:

$ awk -v FS="\t" -v OFS="\t" '{ print $1, $2, $3, ".", $4 }' in.bedGraph | sort-bed - > in.bed 

To merge:

$ bedops --merge in.bed > merged_intervals.bed

To map and apply signal operations:

$ bedmap --echo --sum merged_intervals.bed in.bed > signal_sum_over_merged_intervals.bed
$ bedmap --echo --mean merged_intervals.bed in.bed > signal_mean_over_merged_intervals.bed
$ bedmap --echo --median merged_intervals.bed in.bed > signal_median_over_merged_intervals.bed

Etc.

If you don't need to preserve signal, you can just strip it out:

$ sort-bed <(cut -f1-3 in.bedGraph) | bedops --merge - > merged_intervals.bed

If doing set operations that require signal, however, the ideal is to generate five-column BED, in order to follow UCSC specifications.

Ref:

Log in to answer this question.