Answers.Body: <p>The cause of the error is that your input file is truncated or /tmp is running out of space. If you can do <code>samtools view -H input.bam</code> 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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-14T14:17:30.077Z
Answers.Text: I did "samtools view -H input.bam" there is no error. It ended up showing all the header things which I asked here [https://bioinformatics.stackexchange.com/questions/3080/read-counts-from-bam-file/3081?noredirect=1#comment4889_3081]
Answers.UserDisplayName: stack_learner
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-14T14:29:15.423Z
Answers.Text: Then either /tmp is full or otherwise one of the files there is somehow corrupt (you can test them with `samtools view -H` too).
Answers.UserDisplayName: Devon Ryan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-14T15:21:05.483Z
Answers.Text: 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.
Answers.UserDisplayName: stack_learner
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-14T15:21:56.557Z
Answers.Text: Don't forget to `samtools index sorted.bam`
Answers.UserDisplayName: Devon Ryan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-14T15:33:11.700Z
Answers.Text: 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
Answers.UserDisplayName: stack_learner
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-14T15:34:03.020Z
Answers.Text: They are (in order): contig name, contig length, number of mapped reads, number of unmapped reads (this will typically be 0).
Answers.UserDisplayName: Devon Ryan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-14T15:38:17.440Z
Answers.Text: Ok. Thankyou. So, for DE analysis what I need is mapped reads. how do I get the gene names? Can I use edgeR for DE analysis using the mapped reads?
Answers.UserDisplayName: stack_learner
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-14T15:38:47.920Z
Answers.Text: You mapped to the transcriptome, so they're the first column.
Answers.UserDisplayName: Devon Ryan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-14T15:42:17.383Z
Answers.Text: Yes, I see that. Is there any way to get the gene id/ transcript id from this results. And may I know the answer for DE analysis edgeR?
Answers.UserDisplayName: stack_learner
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-14T16:00:23.090Z
Answers.Text: If you want to get annotation (like gene symbols) coupled to your accession numbers, please read the [biomaRt](https://bioconductor.org/packages/release/bioc/vignettes/biomaRt/inst/doc/biomaRt.html) user guide. If you have questions after reading it, please start a new question in stead of discussing it in the comments.
Answers.UserDisplayName: benn
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-14T14:01:55.807Z
Answers.LastActivityDate: 2017-12-14T14:08:33.120Z
Answers.LastEditDate: 2017-12-14T14:08:33.120Z
Answers.UserDisplayName: Devon Ryan
Body: <p>I have few bam files and would like to get read counts using </p>
  
  <pre><code>samtools idxstats
  </code></pre>
  
  <p>[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.</p>
  
  <pre><code>samtools sort -T /tmp/input.sorted -o input.sorted.bam input.bam
  </code></pre>
  
  <p>This ended up showing:</p>
  
  <pre><code>[W::bam_hdr_read] EOF marker is absent. The input is probably truncated.
  [E::bgzf_read] bgzf_read_block error -1 after 0 of 4 bytes
  [bam_sort_core] truncated file. Aborting.
  </code></pre>
  
  <p>And the tmp dir has 6 input.sorted.nnnn.bam files.</p>
  
Tags: <rna-seq><sam><samtools>
Title: sorting BAM file error using samtools
ThreadText: # sorting BAM file error using samtools
  
  (Asked by: username_0 on 2017-12-14)
  
  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.
  [E::bgzf_read] bgzf_read_block error -1 after 0 of 4 bytes
  [bam_sort_core] truncated file. Aborting.
  
  ```
  
  And the tmp dir has 6 input.sorted.nnnn.bam files.
  
  **Tags:** rna-seq, sam, samtools
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-12-14. Score: 4 | Accepted answer
  
  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.
  
  > **Comments:**
  > **username_0**: I did "samtools view -H input.bam" there is no error. It ended up showing all the header things which I asked here [https://bioinformatics.stackexchange.com/questions/3080/read-counts-from-bam-file/3081?noredirect=1#comment4889_3081]  
  > **username_1**: Then either /tmp is full or otherwise one of the files there is somehow corrupt (you can test them with `samtools view -H` too).  
  > **username_0**: 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.  
  > **username_1**: Don't forget to `samtools index sorted.bam`  
  > **username_0**: 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  
  > **username_1**: They are (in order): contig name, contig length, number of mapped reads, number of unmapped reads (this will typically be 0).  
  > **username_0**: Ok. Thankyou. So, for DE analysis what I need is mapped reads. how do I get the gene names? Can I use edgeR for DE analysis using the mapped reads?  
  > **username_1**: You mapped to the transcriptome, so they're the first column.  
  > **username_0**: Yes, I see that. Is there any way to get the gene id/ transcript id from this results. And may I know the answer for DE analysis edgeR?  
  > **username_2**: If you want to get annotation (like gene symbols) coupled to your accession numbers, please read the [biomaRt](https://bioconductor.org/packages/release/bioc/vignettes/biomaRt/inst/doc/biomaRt.html) user guide. If you have questions after reading it, please start a new question in stead of discussing it in the comments.  
  
  ---
  
  