This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Extracting Reads by position

I have an alignment file for a protein encoded by about ~3000 bases (I have both bam and sam files), and I am looking to extract all the reads that from bases 877-981. Is there any way to do this with samtools?

I tried using this command:

samtools view 8.2alnCorrected.bam "877-981" > filteredreads.bam

However I got this message with an empty file:

region "877-981" specifies an unknown reference name. Continue anyway.
bam sam alignment

1 answer

You need to specify the chromosome or contig as well. Your bam will have lines that look like:

chr1     12345    ...

So for reads are mapped to that chromosome or contig, you want:

samtools view 8.2alnCorrected.bam chr1:877-981

Glad to hear it! Consider hitting the "Accept" button next to the answer - that lets anyone else who stumbles across this question find the solution easily.

Log in to answer this question.