This is a test version of Biostars. For the public version, visit https://www.biostars.org.
samtools index: failed to create index. ([E::hts_idx_push] NO_COOR reads not in a single block )

I have run hisat2 on paired end samples to generate sorted bam file. I used following command:

for r1 in *_1.fastq.gz; do sample=${r1/_1.fastq.gz/}; \
echo "Processing sample: "$sample; \
hisat2 -p 12 --rna-strandness RF -x /media/genome \
-1 $sample"_1.fastq.gz" -2 $sample"_2.fastq.gz" | samtools view -Sbo $sample.bam; \
done

Next. I tried to index the sorted bam file using samtools:

samtools index Sample1.bam Sample1.bam.bai

But it thrown me an error as follows:

[E::hts_idx_push] NO_COOR reads not in a single block at the end 18 -1
samtools index: failed to create index for "Sample1.bam"

How to rectify this error? Any help appreciated

bam samtools hisat2

2 answers

The aim your code seems to generate sorted bam files. Not sure the code you have written sorts your file You can sort it directly:

for r1 in *_1.fastq.gz; do sample=${r1/_1.fastq.gz/}; \
echo "Processing sample: "$sample; \
hisat2 -p 12 --rna-strandness RF -x /media/genome \
-1 $sample"_1.fastq.gz" -2 $sample"_2.fastq.gz" \
| samtools sort -o $sample.bam; \
done

You can index your bam files in parallel:

ls *.bam | parallel samtools index '{}'

Are you sure the hisat2 results are coordinate sorted?

Anyway, if you replace your samtools view with:

samtools sort --write-index -o $sample.bam

That should sort, index and write out a bam file in one command.

Log in to answer this question.