This is a test version of Biostars. For the public version, visit https://www.biostars.org.
samtools index error

Hello,

I am trying to index sorted bam files using samtools index. I created the sorted bam files by downsampling from higher depth sorted bam files using samtools depthandsamtools view. Samtools index works on the original high depth sorted bam files but it gives an error for downsampled sorted bam file:

samtools index: example is in a format that cannot be usefully indexed

Thank you for your help!

snp

could be good to look at the commands you are using to create the down-sampling, also some example of the file generated

I used these commands to downsample:

samtools view -h -s 0.5 E.sorted.bam > E.5x.sorted.bam

BAM is a compressed binary file, SAM is the same information in human readable form (text), whey you run:

samtools view -h -s 0.5 E.sorted.bam > E.5x.sorted.bam

with view -hyou are decompressing your BAM, so you need to compress it again, like this:

samtools view -h -s 0.5 E.sorted.bam > E.5x.sorted.sam
samtools -bS  E.5x.sorted.sam >  E.5x.sorted.bam
samtools index  E.5x.sorted.bam

It worked. Thank you!

Given the information provided here, should this discussion should be moved to an answer and accepted?

2 answers

Problem was identified as the commands used were incorrect, here are the proposed commands:

samtools view -h -s 0.5 E.sorted.bam > E.5x.sorted.sam
samtools -bS E.5x.sorted.sam > E.5x.sorted.bam
samtools index E.5x.sorted.bam

Are you sure that you can't write straight to bam?

samtools view -bh -s 0.5 E.sorted.bam > E.5x.sorted.bam

Probably, but I would prefer this syntax:

samtools view -s 0.5 E.sorted.bam -o E.5x.sorted.bam

Are you sure that you've output a BAM file from samtools view? You can confirm with samtools quickcheck [bam]; see here. Additionally, sometimes people forget to include the -h flag in view to include the header - just a thought.

Thank you! the file is intact and I used -h as well.

Great! Is there a possibility of running out of space in a temporary directory, or are you executing this on a network filesystem? See here for one possible avenue of investigation. Sorry it's getting a tad technical, but indexing is usually so dialed-in that it's strange if it doesn't work, barring BAM structure-related issues.

Thank you, I actually checked this post before posting my question. I am using a cluster and I believe space is not an issue for this job. I checked all the versions and everything is updated.

What about using a shared filesystem for, e.g., your home directory? Any NFSs mounted that could be indicative of a similar issue to the Github one above?

I am not sure how to check for that.

Log in to answer this question.