This is a test version of Biostars. For the public version, visit https://www.biostars.org.
samtools index error - "Unsorted positions on sequence" even though file is sorted

I am a little confused about what I am doing wrong. I have two WGS datasets I have aligned and sorted using Samtools:

du ./Lane7/A471_1/aligned/A471_1_posSort_dedup.bam
6195440 Lane7/A471_1/A471_1_posSort_dedup.bam
du ./Lane8/A471_1/aligned/A471_1_posSort_dedup.bam
6099412 Lane8/A471_1/aligned/A471_1_posSort_dedup.bam

I then merge the two BAM files together:

samtools merge -@ 10 -o ./CombinedLanes/A471_1/aligned/A471_1_combined.bam ./Lane7/A471_1/aligned/A471_1_posSort_dedup.bam ./Lane8/A471_1/aligned/A471_1_posSort_dedup.bam

This appears to have worked.

du C.ombinedLanes/A471_1/aligned/A471_1_combined.bam 
12208415    CombinedLanes/A471_1/aligned/A471_1_combined.bam

I then try and sort the merged BAM files:

samtools sort -n -@ 10 -O bam ./CombinedLanes/A471_1/aligned/A471_1_combined.bam > ./CombinedLanes/A471_1/aligned/A471_1_combined_sorted.bam

du ./CombinedLanes/A471_1/aligned/A471_1_combined_sorted.bam 
20880086    ./CombinedLanes/A471_1/aligned/A471_1_combined_sorted.bam

However when I try an index the sorted merged BAM I get an error:

samtools index ./CombinedLanes/A471_1/aligned/A471_1_combined_sorted.bam 
[E::hts_idx_push] Unsorted positions on sequence #3: 18237437 followed by 18237335
[E::sam_index] Read 'LH00309:33:22CN3LLT3:7:1101:1000:1641' with ref_name='Chr3', ref_length=23459830, flags=163, pos=18237335 cannot be indexed
samtools index: failed to create index for "./CombinedLanes/A471_1/aligned/A471_1_combined_sorted.bam": No such file or directory

It suggests the BAM I have just sorted is not sorted and also that it doesn't exist. Where am I likely to have gone wrong?

samtools --version
samtools 1.16
Using htslib 1.16
samtools alignment bam

what is that file "A471_1_combined_sorted2.bam " ? how was it created ?

Sorry, its the same as A471_1_combined_sorted.bam. I just ran the sort and index commands again to see if the same thing happened.

1 answer

you used samtools sort -n which sorts on READ NAMES, NOT on read positions.

Furthermore, there is no need to re-sort the reads after "samtools merge"

Log in to answer this question.