This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to specify the sort based on name in samtools sort?

I would like to know how to issue the command when sorting a BAM file based on name. The problem is that when I used the command,

samtools sort -n -o WT.bam /home/exp/accepted_hits.bam

It run without any error. However, when tried to index the BAM file like,

samtools index WT.bam

I go the error,

[E::hts_idx_push] Unsorted positions on sequence #8: 29211960 followed by 29211761
samtools index: failed to create index for "WT.bam"

I noticed that sorting based on position was successful in indexing. I tried different form of inputing -n and -o , but failed.

bam sort rna-seq htseq samtools

1 answer

Hello,

you can only index the bam file when it ist coordinate-sorted. From the manual:

samtools index [-bc] [-m INT] aln.bam|aln.cram [out.index]

Index a **coordinate-sorted** BAM or CRAM file for fast random access. (Note that this does not work with SAM files even if they are bgzip compressed — to index such files, use tabix(1) instead.)

This index is needed when region arguments are used to limit samtools view and similar commands to particular regions of interest.

I don't know whether there are other ways to index bam files which are sorted by read name. Why do you need it?

fin swimmer

It was a doubt from my previous post. I think i need to use -r option in HTSeq since I will sort bam file using position and not by name.

Log in to answer this question.