This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to filter out chromosome regions from a .bam file

Hello,

I know that I can create a subset of a bam file for a desired region using samtools view:

samtools view -b UWNsub100k_int.bam chrXII:450000-470000 >  UWNsub100k_ribos.bam

What I want to do, however, is create a .bam file that excludes these regions. Perhaps something similar to -v for grep. Is there a samtools command for this?

Thanks,

rna-seq alignment

use bedtools complement to generate the complement of chrXII:450000-470000

2 answers

samtools view -U file will output reads not selected by filters to file. This does not apply to regions specified on the command line (after the input file name) or with -M, as using the index to jump to the specified regions is separate from “filtering the reads” — however it does apply to -L.

So construct unwanted.bed from your regions to be excluded and use

samtools view -L unwanted.bed -U remaining.bam -o /dev/null UWNsub100k_int.bam

This worked well. Thank you!

Samtools view -L will let you give samtools a bed files of the regions you want. I don't think there is a -v equivalent.

Log in to answer this question.