sorting BAM file error using samtools

I have few bam files and would like to get read counts using

samtools idxstats


[Data is aligned to hg19 transcriptome]. To use that command I need a sorted bam file. So to sort them I gave the following command.

samtools sort -T /tmp/input.sorted -o input.sorted.bam input.bam


This ended up showing:

[W::bam_hdr_read] EOF marker is absent. The input is probably truncated.
[bam_sort_core] truncated file. Aborting.


And the tmp dir has 6 input.sorted.nnnn.bam files.

The cause of the error is that your input file is truncated or /tmp is running out of space. If you can do samtools view -H input.bam without error (reading the header also checks for the magic number at the end of the file) then it's like the that /tmp is filling up.

• I did "samtools view -H input.bam" there is no error. It ended up showing all the header things which I asked here [bioinformatics.stackexchange.com/questions/3080/… – stack_learner Dec 14 '17 at 14:17
• Then either /tmp is full or otherwise one of the files there is somehow corrupt (you can test them with samtools view -H too). – Devon Ryan Dec 14 '17 at 14:29
• Ok. looks like that file is corrupt. And I tried with other bam file and showed - [bam_sort_core] merging from 24 files... and I see a sorted.bam file. I used "samtools idxstats sorted.bam" - This is what I see - [bam_idxstats] fail to load the index. – stack_learner Dec 14 '17 at 15:21
• Don't forget to samtools index sorted.bam – Devon Ryan Dec 14 '17 at 15:21
• Ok. So, after the indexing I used idxstats and it showed 4 columns without any headers. NR_039978 984 38 0 NM_130786 1766 1 0 NM_138932 9293 37 0 NM_033110 2678 2 0 NM_000014 4678 4 0 NM_144670 5192 18 0 NR_040112 1201 2 0 NM_017436 2106 76 0 NM_016161 1771 0 0 – stack_learner Dec 14 '17 at 15:33