Answers.Body: <p>Sort the BAM file again with <code>samtools sort</code> and then run <code>samtools index</code> on the result. That should fix the issue.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-09-11T09:13:07.630Z
Answers.LastActivityDate: 2021-09-11T09:13:07.630Z
Answers.UserDisplayName: Devon Ryan
Answers.Body: <p><a href="https://bioinformatics.stackexchange.com/a/17689/298">What Devon said.</a> Alternatively, you could also just convert the bam to sam and then look for lines where the 3rd field (the reference the sequence is aligned to) is what you want:</p>
  <pre><code>samtools view pseudoalignments.bam | awk '$3==&quot;ENST00000367969.8&quot;'
  </code></pre>
  <p>But really, it's far better to sort and index the file as Devon suggested so  you can use dedicated tools in future.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-09-11T12:28:15.980Z
Answers.LastActivityDate: 2021-10-01T22:25:05.390Z
Answers.LastEditDate: 2021-10-01T22:25:05.390Z
Answers.UserDisplayName: terdon
Body: <p><em>This question has also been asked on <a href="https://www.biostars.org/p/9488897/" rel="nofollow noreferrer">Biostars</a></em></p>
  <p>I ran Kallisto with the --pseudobam option. How do I extract all the short reads that are mapped to a single transcript (e.g. ENST00000367969.8)?  As a person without any previous SAM/BAM experience, I tried the following things without success. Really appreciate some help here.</p>
  <p>I first tried:</p>
  <pre><code>samtools view -S pseudoalignments.bam ENST00000367969.8
  </code></pre>
  <p>but got the following error:</p>
  <pre><code>[E::idx_find_and_load] Could not retrieve index file for 'pseudoalignments.bam'
  [main_samview] random alignment retrieval only works for indexed BAM or CRAM files.
  </code></pre>
  <p>I tried to index the file using:</p>
  <pre><code>samtools index pseudoalignments.bam
  </code></pre>
  <p>but get the following error:</p>
  <pre><code>[E::hts_idx_push] NO_COOR reads not in a single block at the end 79729 -1
  [E::sam_index] Read 'A00742:184:HLTF2DSXY:4:1101:4417:1000' with ref_name='ENST00000556431.1', ref_length=4142, flags=83, pos=2098 cannot be indexed
  samtools index: failed to create index for &quot;pseudoalignments.bam&quot;
  </code></pre>
  
Tags: <sam><samtools><kallisto>
Title: How to extract all sequences mapped to a transcript from Kallisto output
ThreadText: # How to extract all sequences mapped to a transcript from Kallisto output
  
  (Asked by: username_0 on 2021-09-10)
  
  *This question has also been asked on [Biostars](https://www.biostars.org/p/9488897/)*
  
  I ran Kallisto with the --pseudobam option. How do I extract all the short reads that are mapped to a single transcript (e.g. ENST00000367969.8)? As a person without any previous SAM/BAM experience, I tried the following things without success. Really appreciate some help here.
  
  I first tried:
  
  ```
  samtools view -S pseudoalignments.bam ENST00000367969.8
  
  ```
  
  but got the following error:
  
  ```
  [E::idx_find_and_load] Could not retrieve index file for 'pseudoalignments.bam'
  [main_samview] random alignment retrieval only works for indexed BAM or CRAM files.
  
  ```
  
  I tried to index the file using:
  
  ```
  samtools index pseudoalignments.bam
  
  ```
  
  but get the following error:
  
  ```
  [E::hts_idx_push] NO_COOR reads not in a single block at the end 79729 -1
  [E::sam_index] Read 'A00742:184:HLTF2DSXY:4:1101:4417:1000' with ref_name='ENST00000556431.1', ref_length=4142, flags=83, pos=2098 cannot be indexed
  samtools index: failed to create index for "pseudoalignments.bam"
  
  ```
  
  **Tags:** sam, samtools, kallisto
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2021-09-11. Score: 1 | Accepted answer
  
  Sort the BAM file again with `samtools sort` and then run `samtools index` on the result. That should fix the issue.
  
  
  ---
  
  ### Answer by username_2 on 2021-09-11. Score: 1
  
  [What username_1 said.](https://bioinformatics.stackexchange.com/a/17689/298) Alternatively, you could also just convert the bam to sam and then look for lines where the 3rd field (the reference the sequence is aligned to) is what you want:
  
  ```
  samtools view pseudoalignments.bam | awk '$3=="ENST00000367969.8"'
  
  ```
  
  But really, it's far better to sort and index the file as username_1 suggested so you can use dedicated tools in future.
  
  
  ---
  
  