"moving average" function for "coverage" signal in bed files?
2
2
Entering edit mode
8 weeks ago
Shicheng Guo ★ 9.1k

Dear Team,

I am wondering is there an option/function to perform "moving average" to coverage signal with bedtools or other related tools?

Something like: bedtools movingAverage -i a.bed --start --end --bin 200bp --out

Thanks.

Shicheng

bedtools • 286 views
2
Entering edit mode

Hi, I was looking into this a while ago and didn't find nothing comparable, either. I ended up loading it into a R dataframe/tibble and apply a rolling average, something along this (from here):

require(RcppRoll)
airquality$d7_rollavg <- roll_mean(airquality$Wind, n = 7, align = "right", fill = NA)
airquality$d7_rollavg <- format(airquality$d7_rollavg, digits = 3)

1
Entering edit mode
8 weeks ago

Hi there

What I do for this is use a combination of three bedtools functions, genomecov, makewindows and map.

In addition to the coverage bed file, I also require a bed file of the genome assembly (chromosome name in first column, and size in second, I usually just take the first two columns from the .fai file after samtools faidx, 'genome.bed' below)

First, my coverage input:

bedtools genomecov -d -ibam sample.bam > sample.cov.tsv


Next is to split up the assembly bed file into the windows with makewindows by giving it the window size and how far to slide each time

bedtools makewindows -w sizeofwindow -s sizeofslide -g genome.bed > genome.windows.bed


Then next thing is bedtools map which essentially takes all the windows created before and calculates what ever average asked for (-o)

First though I just have to modify the raw output from genomecov, to repeat the second column twice (the basepair position) essentially saying that the 'window' in which the coverage comes from is actually single basepair position

cat sample.cov.tsv | awk '{print $1"\t"$2"\t"$2"\t"$3}' | bedtools map -b - -a genome.windows.bed -c 4 -o median > sample.cov.movingaverage.tsv


Hope that helps

0
Entering edit mode
7 weeks ago

This is one of the BEDOPS recipes, with one small change:

Just replace --count with --mean to do a moving average over the sliding window. Other parameters let you change the size of the window and the sliding step.