text: user9393: I am doing an RNA seq analysis with mouse samples. I had my reads in several lanes, so I concatenated those fastq reads. Then I've mapped my reads with Hisat2 against the new version of mouse  "GCA_000001635.9_GRCm39_genomic.fna".
  Apparently everything works well up to here. Now I'm using samtools 1.9 to convert sam to bam, to sort those bam and create the index, just like this:
  samtools view -bS ${file} > ${file}.bam
  
  samtools sort -n ${file} -o ${file}_sorted.bam
  
  samtools index  ${file}_sorted.bam
  
  But when indexing I get this error:
  
  [bam_sort_core] merging from 11 files and 1 in-memory blocks...
  [E::hts_idx_push] NO_COOR reads not in a single block at the end 0 -1
  samtools index: failed to create index for
  "CD4_1A_concat.bam_sorted.bam": No such file or directory
  [bam_sort_core] merging from 11 files and 1 in-memory blocks...
  [E::hts_idx_push] NO_COOR reads not in a single block at the end 23 -1
  samtools index: failed to create index for
  "CD4_1B_concat.bam_sorted.bam": No such file or directory
  [bam_sort_core] merging from 11 files and 1 in-memory blocks...
  
  And more...
  Any help would be appreciated!
  
  user134: This is essentially a duplicate of Samtools Index: Chromosome Blocks not Continuous but with different error messages, and a duplicate of Error given while trying to index a BAM file with Samtools Index - NO COOR?.
  samtools sort -n ${file} -o ${file}_sorted.bam
  
  These error messages also indicate that the reads are not sorted by coordinate — in particular, that the unplaced unmapped reads with RNAME * are not all together at the 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: 9393: <p>I am doing an RNA seq analysis with mouse samples. I had my reads in several lanes, so I concatenated those fastq reads. Then I've mapped my reads with Hisat2 against the new version of mouse  &quot;GCA_000001635.9_GRCm39_genomic.fna&quot;.</p>
  <p>Apparently everything works well up to here. Now I'm using samtools 1.9 to convert sam to bam, to sort those bam and create the index, just like this:</p>
  <pre><code>samtools view -bS <span class="math-container">${file} &gt; $</span>{file}.bam
  
  samtools sort -n <span class="math-container">${file} -o $</span>{file}_sorted.bam
  
  samtools index  ${file}_sorted.bam
  </code></pre>
  <p>But when indexing I get this error:</p>
  <blockquote>
  <p>[bam_sort_core] merging from 11 files and 1 in-memory blocks...
  [E::hts_idx_push] NO_COOR reads not in a single block at the end 0 -1
  samtools index: failed to create index for
  &quot;CD4_1A_concat.bam_sorted.bam&quot;: No such file or directory</p>
  <p>[bam_sort_core] merging from 11 files and 1 in-memory blocks...
  [E::hts_idx_push] NO_COOR reads not in a single block at the end 23 -1
  samtools index: failed to create index for
  &quot;CD4_1B_concat.bam_sorted.bam&quot;: No such file or directory
  [bam_sort_core] merging from 11 files and 1 in-memory blocks...</p>
  </blockquote>
  <p>And more...</p>
  <p>Any help would be appreciated!</p>
   134: <p>This is essentially a duplicate of <a href="https://bioinformatics.stackexchange.com/questions/13997/samtools-index-chromosome-blocks-not-continuous">Samtools Index: Chromosome Blocks not Continuous</a> but with different error messages, and a duplicate of <a href="https://bioinformatics.stackexchange.com/questions/5352/error-given-while-trying-to-index-a-bam-file-with-samtools-index-no-coor">Error given while trying to index a BAM file with Samtools Index - NO COOR?</a>.</p>
  <pre><code>samtools sort -n ${file} -o ${file}_sorted.bam
  </code></pre>
  <p>These error messages also indicate that the reads are not sorted by coordinate — in particular, that the unplaced unmapped reads with RNAME <code>*</code> are not all together at the 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>
  