This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Filtering bam file based on depth determined through samtools depth

Hi All,

I have a bam file and I calculated read depth using samtools depth and I now want to filter the bam file to have only the contigs that have a depth between a certain value. I was able to filter the samtools output to create a txt file that only contains the depth region im looking for, but Im struggling to then filter the bam file with this information. The depth file has three columns, the contig name, the POS, and the depth

Screen_Shot_of_Depth_File

I attempted to remove the depth column and use grep to search the bam file, but my output was empty. I also tried to find answers with awk, but quickly got confused as I am still a fairly novice coder. Any ideas on how to proceed would be greatly appreciated.

sam bam filter

1 answer

awk -F '\t' '(int($3)> 123 ) {printf("%s\t%d\t%s\n",$1,int($2)-1,$2);}' depth.output | sort -t $'\t' -k1,1 -k2,2n | bedtools merge > select.bed

samtools view -M -L select.bed in.bam

Log in to answer this question.