This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Export sequence from specific region from bam file.

I have a bam file and I wanted to extract the sequence of a specific region. for example ch1:1-337, the sequence starts EXACTLY at position 1 and ends EXACTLY at position 337 and I don't want anything else that starts and ends between them.

I tried:

samtools view -b input.bam ch1:1-337 > output.bam

and:

samtools view -b -L regions.bed input.bam > output.bam

but the result was every sequence that starts in the region or ends in it.

I can extract the sequence from igv but that will take too much time to do for multiple regions (> 150 regions).

Any ideas?

bam sam samtools igv

1 answer

samtools view -b  --expr 'pos==1 && endpos==337' input.bam "chr1:1:337" > output.bam

thank you for the answer, but i got an empty bam file. i also replaced "ch1:1-337" with "chr1:1-337" it didn't work either.

i also replaced "ch1:1-337" with "chr1:1-337"

ah yes, fixed.

but i got an empty bam file

are you sure such read exists ? can you please show us one alignment ?

this read starts at position 29304 not at 1 as you asked.

Log in to answer this question.