text: user-1: I am currently working on my own Metagenomic pipeline, utilizing Bowtie 2 to map. Bowtie 2 outputs a SAM file, which I convert to a .BAM and sort it using Samtools. When I try to utilize Samtools to index my .BAM file it gives me this error:
  [E::hts_idx_push] NO_COOR reads not in a single block at the end 1652 -1.
  
  Here is my code:
  source /opt/asn/etc/asn-bash-profiles-special/modules.sh
  module load samtools/1.3.1
  
  cd ~/gz_files/soapdenovo
  r=20
  ###SAM to BAM and sort 
  
  samtools faidx soapdenovo.fa 
  samtools view -b -T soapdenovo.fa -o sample${r}bowtiemapping.bam sample${r}bowtiemapping.sam
  samtools sort -n -T ~/gz_files/soapdenovo/temp -o sample${r}bowtiemapping_sorte$
  samtools index sample${r}bowtiemapping_sorted.bam
  
  ################LETS MAKE THE ABUNDANCE TABLES###########################$
  samtools view -c -f 4  sample${r}bowtiemapping_sorted.bam > sample${r}alignment$
  
  samtools idxstats sample${r}bowtiemapping_sorted.bam >  sample${r}idxstats.txt
  
  #git clone https://github.com/metajinomics/mapping_tools.git
  #python mapping_tools/get_count_table.py *.idxstats.txt > contig_counts.tsv
  ##less contig_counts.tsv
  
  Any help would be appreciated!
  
  Notes on pipeline
  
  samtools import was replaced with samtools view
  faidxcorrected
  Instructions used are here on github
  Suggestion for future trouble shooting is given here  by @winni2k
  
  
  user134: By now this will be of little interest to the original poster, but for future reference:
  $ samtools sort -n -T ~/[etc] -o sample${r}bowtiemapping_sorted.bam [etc]
  $ samtools index sample${r}bowtiemapping_sorted.bam
  [E::hts_idx_push] NO_COOR reads not in a single block at the end 1652 -1.
  
  This error message indicates (admittedly somewhat obscurely) that the reads are not sorted by coordinate — in particular, there are mapped reads appearing after the unplaced unmapped reads (those with RNAME *) that should be at the very end of the file.
  This is because samtools sort -n has been used to sort the reads by name instead. Remove -n to sort by position, which is what is needed to prepare a BAM file for indexing with samtools index.
  
original_text: -1: <p>I am currently working on my own Metagenomic pipeline, utilizing Bowtie 2 to map. Bowtie 2 outputs a SAM file, which I convert to a .BAM and sort it using Samtools. When I try to utilize Samtools to index my .BAM file it gives me this error:</p>
  
  <pre class="lang-html prettyprint-override"><code>[E::hts_idx_push] NO_COOR reads not in a single block at the end 1652 -1.
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre class="lang-html prettyprint-override"><code>source /opt/asn/etc/asn-bash-profiles-special/modules.sh
  module load samtools/1.3.1
  
  cd ~/gz_files/soapdenovo
  r=20
  ###SAM to BAM and sort 
  
  samtools faidx soapdenovo.fa 
  samtools view -b -T soapdenovo.fa -o sample<span class="math-container">${r}bowtiemapping.bam sample$</span>{r}bowtiemapping.sam
  samtools sort -n -T ~/gz_files/soapdenovo/temp -o sample<span class="math-container">${r}bowtiemapping_sorte$</span>
  samtools index sample${r}bowtiemapping_sorted.bam
  
  ################LETS MAKE THE ABUNDANCE TABLES###########################<span class="math-container">$
  samtools view -c -f 4  sample$</span>{r}bowtiemapping_sorted.bam &gt; sample<span class="math-container">${r}alignment$</span>
  
  samtools idxstats sample<span class="math-container">${r}bowtiemapping_sorted.bam &gt;  sample$</span>{r}idxstats.txt
  
  #git clone https://github.com/metajinomics/mapping_tools.git
  #python mapping_tools/get_count_table.py *.idxstats.txt &gt; contig_counts.tsv
  ##less contig_counts.tsv
  </code></pre>
  
  <p>Any help would be appreciated!</p>
  <hr />
  <p><em>Notes on pipeline</em></p>
  <ul>
  <li><code>samtools import</code> was replaced with <code>samtools view</code></li>
  <li><code>faidx</code>corrected</li>
  <li>Instructions used are <a href="https://github.com/edamame-course/2018-Tutorials/wiki" rel="nofollow noreferrer">here on github</a></li>
  <li>Suggestion for future trouble shooting is given <a href="https://stackoverflow.com/help/mcve">here</a>  by @winni2k</li>
  </ul>
   134: <p>By now this will be of little interest to the original poster, but for future reference:</p>
  <pre><code>$ samtools sort -n -T ~/[etc] -o sample${r}bowtiemapping_sorted.bam [etc]
  $ samtools index sample${r}bowtiemapping_sorted.bam
  [E::hts_idx_push] NO_COOR reads not in a single block at the end 1652 -1.
  </code></pre>
  <p>This error message indicates (admittedly somewhat obscurely) that the reads are not sorted by coordinate — in particular, there are mapped reads appearing after the unplaced unmapped reads (those with RNAME <code>*</code>) that should be at the very end of the file.</p>
  <p>This is because <code>samtools sort -n</code> has been used to sort the reads by name instead. Remove <code>-n</code> to sort by position, which is what is needed to prepare a BAM file for indexing with <code>samtools index</code>.</p>
  