average depth of each region in a BED file calculated from a BAM file
2
0
Entering edit mode
3.7 years ago
Floydian_slip ▴ 170

Hi, I have a list of regions in a BED file and I want to know the average depth of each region as calculated from a BAM file, ideally as the fourth column in the output file (like an extra column in the BED file). Is there some combination of Samtools/Bedtools and awk that I can use to get this? Thanks!

BED BAM coverage NGS • 2.6k views
ADD COMMENT
0
Entering edit mode
3.7 years ago
Ram 43k

I'd recommend taking a look at mosdepth. It has an option where you can give it a BED file and it will calculate depth per interval. Plus, it is a LOT faster than samtools. It doesn't count every single read though (unlike samtools), but that shouldn't matter as it only excludes reads that you shouldn't be counting anyway. Give the manual a good read.

ADD COMMENT
0
Entering edit mode

Thanks for the reply, RamRS! Running into installation issues with mosdepth. Any way either samtools or bedtools can do this for me?

ADD REPLY
0
Entering edit mode

Yeah samtools should be able to do this, but trust me - mosdepth is far quicker. What problems are you running into? If you have access to conda, you should just

conda create -n mosdepth_env -c bioconda mosdepth
conda activate mosdepth_env
ADD REPLY
0
Entering edit mode

Thanks, RamRS. I was able to install it using docker but ran into error:

$docker run -v ~/Documents/ quay.io/biocontainers/mosdepth:0.2.4--he527e40_0 mosdepth  -n --fast-mode -b ~/Documents/redesign_sorted.bed ~/Documents/sample ~/Documents/ecs-ready.bam
[E::hts_open_format] Failed to open file ~/Documents/ecs-ready.bam
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

I also installed the latest htslib from http://www.htslib.org/download/

Any idea how I can fix this? Thanks so much for your help.

ADD REPLY
0
Entering edit mode

Sorry, I don't know Docker, so I can't say if that error is Docker based or mosdepth based. I recognize the HTSLIB error though. What is the output of

samtools view -H ~/Documents/ecs-ready.bam | head
ADD REPLY
0
Entering edit mode
3.6 years ago
Floydian_slip ▴ 170

I ended up using "samtools bedcov" and then writing a quick script to divide the total base pairs (last column) by the length of the region. I matched that up with Samtools depth and it matches. Thanks for your help, RamRS!

ADD COMMENT

Login before adding your answer.

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