samtools

Is there any way to pass a bed file or similar with target regions to samtools coverage?


I would like to get the coverage and meandepth of different regions from a bam file. I guess samtools coverage is a good way to do that but I wasn't able to find a way to pass a file with my target regions.

Is there any way to do that?


Solution

  • samtools coverage does not accept BED files, samtools bedcov does but the output is different. As an alternative, you can try to embed samtools coverage inside a while loop that runs across the intervals (-r option) and store the output inside a file (myfile.cov in the examle below).

    here is the bash code:

    while read -r line
    do
     chr=$(echo $line | cut -d" " -f1)
     start=$(echo $line | cut -d" " -f2)
     end=$(echo $line | cut -d" " -f3)
     samtools coverage -q5 -Q20 --ff UNMAP,SECONDARY,QCFAIL,DUP -r $chr:$start-$end sample.bam | grep -v "#" >> myfile.cov
    done < path/to/my.bed
    

    I hope they will implement the BED input soon because it is really useful.