This is a test version of Biostars. For the public version, visit https://www.biostars.org.
samtools view by chromosome region chr:1000-1001000 creates "unknown reference name" error

I'm trying to create truncated versions of my bams for testing. The samtools docs say:

REGIONS:

Regions can be specified as: RNAME[:STARTPOS[-ENDPOS]] and all position coordinates are 1-based.

Important note: when multiple regions are given, some alignments may be output multiple times if they overlap more than one of the
  

specified regions.

Examples of region specifications:

chr1

    Output all alignments mapped to the reference sequence named `chr1' (i.e. @SQ SN:chr1). 
chr2:1000000

    The region on chr2 beginning at base position 1,000,000 and ending at the end of the chromosome. 
chr3:1000-2000

    The 1001bp region on chr3 beginning at base position 1,000 and ending at base position 2,000 (including both end positions).
  

This is what I'm trying:

for i in *.bam; do echo $i; name=${i%%.*}; echo $name; samtools view -b -h -1 -@ 4 $i chr2:1000-1001000 > $name_chr2-1000000-2000000.bam; done

I get the error:

[main_samview] region "chr2:1000-1001000" specifies an unknown reference name. Continue anyway.
samtools

1 answer

there is no such chromosome "chr2" in the bam. try "2".

To get the list of chromosomes, try:

samtools view -H your.bam

Yup! That did it. I thought I had tried that... Thanks!

Log in to answer this question.