Samtools view does not recognize that I already have index file
1
2
Entering edit mode
8.2 years ago

Hello, I am trying to extract reads at 1 base pair from many bam files. Each bam file has a bai file with the same name+.bai in the same folder.

When I run

samtools view myfile.bam

I get the output, but when I add coordinates such as

samtools view myfile.bam "chr1:1-100000"

I get the following error:

[bam_index_load] fail to load BAM index.
[main_samview] random alignment retrieval only works for indexed BAM files.

My understanding is that the bai file is the index it needs. Am I missing something?

Thanks in advance.

samtools • 3.7k views
ADD COMMENT
2
Entering edit mode
8.2 years ago
Brice Sarver ★ 3.8k

Some things to consider:

  1. Your index may old or generated with a program other than samtools. You might want to regenerate it.
  2. Make sure the BAM itself is sorted by coordinate. Also make sure that the names are correct between the two files, i.e., you didn't change the name of the BAM after indexing or something.
  3. The index itself also needs to be accessible and readable by samtools.
ADD COMMENT
0
Entering edit mode

Thanks for the quick response. Is there an easy way to be sure that the BAM is sorted? I've started trying to sort and it's going to be a long while (files are 30+ GB each), so I'd rather not resort to re-sorting if I don't have to.

Also, any way to tell how the index was generated? Again, indexing is a long process so if I can easily get these working, I'd prefer this.

ADD REPLY
0
Entering edit mode

To see if things are sorted, take a look at this post: Is My Bam File Sorted ?

One way to attempt to check and see if the index might have been generated using another program (which, in this case, basically just means an earlier version of samtools, though I'm not exactly sure where backwards compatibility would break) would be to check the dates on the files with ls -la. If the BAM is much older than your index, you may have a problem. It's much more likely that your indices might be named the same but were actually from other BAMs - I've seen it happen a few times with several people working on a project. You don't need to test all of them; just test a single one and see if it fixes the issue. It shouldn't take too much time, and you'll know if it works.

ADD REPLY

Login before adding your answer.

Traffic: 2072 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6