This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to merge and average BedGraph files

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

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.

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.

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!

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.

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

0 answers

No answers yet.

Log in to answer this question.