samtools index error - "Unsorted positions on sequence" even though file is sorted
1
0
Entering edit mode
12 weeks ago
Geoffrey • 0

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 • 530 views
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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.

ADD REPLY
5
Entering edit mode
12 weeks ago

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"

ADD COMMENT

Login before adding your answer.

Traffic: 2319 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