How to merge and average BedGraph files
0
0
Entering edit mode
3.6 years ago
a_bis ▴ 40

Hello, I am trying to merge and average three BedGraph files using the following code on terminal: bedtools unionbedg -i file1.bedgraph file2.bedgraph file3.bedgraph | awk 'OFS="/t" {sum=0; for (col=4; col<=NF; col++) sum += $col; print $1, $2, $3, sum/(NF-4+1); }' > averaged_file.bedgraph

I get the following error message:

bedtools: src/unionBedGraphs/unionBedGraphs.cpp:99: CHRPOS UnionBedGraphs::ConsumeNextCoordinate(): Assertion `!queue.empty()' failed.

Aborted (core dumped)

I have tried to sort the files using

sort -c -k1,1 -k2,2n <file.bedgraph>

But I get the following error message:

sort: file1.bedgraph:2: disorder: . -1 -1 -1

The head of my BedGraph files (image below) looks like this, so it appears to be in tab-delimited format:

enter image description here

Any advice on why this is failing and possible solutions will be really appreciated! Thanks.

bedtools terminal bg bedgraph • 3.5k views
ADD COMMENT
1
Entering edit mode

Try deeptools it has many tools for bedGraph and bigWig files.

ADD REPLY
0
Entering edit mode

Hi, thanks for the suggestion! Based on deeptools documentation, it looks like multiBigwigSummary might be the one for me:

Given typically two or more bigWig files, multiBigwigSummary computes the average scores for each of the files in every genomic region.

but it gives a compressed numpy array output (.npz). Do you know whether it's possible to convert .npz to other formats, such as .bg, .bed, or .bw? I'm struggling to even find exactly what data ends up in this .npz file, so I can't figure out a way to convert it to other file types. Thanks again!

ADD REPLY
0
Entering edit mode

It seems that my comment was misleading. There is --outRawCounts option but it's output is tab-delimited, I have not tested it. I don't know if it's possible to convert npz to other formats. I used it to plotting. There is bigwigCompare command which have --operation option and it can be mean but it works only for two files.

Consult this post , it seems that wiggletools averages wig files. So you can convert bedGraph to bigWig and after averaging make bedGraph. There are tools in deeptools and kentutils which can be helpful.

ADD REPLY
0
Entering edit mode

Thanks very much, I also found this post yesterday and it seems it might be just the thing. Thanks again for your help!

ADD REPLY
0
Entering edit mode

I have tried to sort the files using sort -c -k1,1 -k2,2n <file.bedgraph>

you're not sorting anything here. -c is just for checking the file is already sorted.

ADD REPLY
0
Entering edit mode

-1 is not a valid bed coordinate.

ADD REPLY
0
Entering edit mode

Thank you, I thought it looked weird. These were in the bed file from which I generated the BedGraph -- do you have an idea of how they've arisen?

Also, thank you for pointing out the "-c" flag error above.

ADD REPLY

Login before adding your answer.

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