Answers.Body: <p>Not only will you save disk space by converting to BAM, but BAM files are faster to manipulate than SAM. Source: <a href="https://davetang.org/wiki/tiki-index.php?page=SAMTools#Converting_a_SAM_file_to_a_BAM_file" rel="noreferrer">Dave Tang's SAMTools wiki</a>.</p>
  <p><code>sort</code> supports uncompressed SAM format from a file or <code>stdin</code>, though <code>index</code> requires BGZIP-compressed SAM or BAM. I don't think you can get around this.</p>
  <p>If you're dead-set on reading from and writing to uncompressed SAM format, you can use pipes to avoid writing BAM files. Specify the output of <code>view</code> to be an uncompressed bam, then pipe it to your next command, then pipe it to <code>view</code> again to output your sorted SAM file.</p>
  <pre><code>samtools view -u in.sam | samtools sort | samtools view -h &gt; sort.sam
  samtools view -u sort.sam | samtools index
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-04T13:09:47.643Z
Answers.Text: I suspect you could convince tabix to index a SAM file, though you're correct that using BAM is a MUCH better solution.
Answers.UserDisplayName: Devon Ryan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-03T15:43:10.813Z
Answers.LastActivityDate: 2022-02-03T16:00:55.447Z
Answers.LastEditDate: 2022-02-03T16:00:55.447Z
Answers.UserDisplayName: acvill
Body: <p>Generally, I use <code>samtools</code> to <code>sort</code> and <code>index</code> BAM files. Samtools works also with BGZF compressed SAM but not with plain ones.</p>
  <p>I have terabytes of plain SAMs at the moment and I would love to avoid compression/conversion. I have all the data in several external HD and I am not allowed to delete the original SAM files. I would need much more space than I have now to also store the BAM files.</p>
  <p>So, I was wondering if there is a way to avoid it. Is there some tool/trick to sort and index uncompressed SAM files, without converting them to BAM or compressing them?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-02-03T16:27:12.627Z
Comments.Text: Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
Comments.UserDisplayName: Community
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-02-04T22:14:16.753Z
Comments.Text: In general, avoid storing any SAM files on disk. Instead, use htslib/pysam/etc to directly read BAM files, or pipe SAM to other tools or your own scripts.
Comments.UserDisplayName: user172818
Tags: <samtools><sam><indexing>
Title: How to Sort and Index a SAM file without converting it to BAM?
ThreadText: # How to Sort and Index a SAM file without converting it to BAM?
  
  (Asked by: username_0 on 2022-02-03)
  
  Generally, I use `samtools` to `sort` and `index` BAM files. Samtools works also with BGZF compressed SAM but not with plain ones.
  
  I have terabytes of plain SAMs at the moment and I would love to avoid compression/conversion. I have all the data in several external HD and I am not allowed to delete the original SAM files. I would need much more space than I have now to also store the BAM files.
  
  So, I was wondering if there is a way to avoid it. Is there some tool/trick to sort and index uncompressed SAM files, without converting them to BAM or compressing them?
  
  **Tags:** samtools, sam, indexing
  
  > **Comments:**
  > **username_1**: Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.  
  > **username_2**: In general, avoid storing any SAM files on disk. Instead, use htslib/pysam/etc to directly read BAM files, or pipe SAM to other tools or your own scripts.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2022-02-03. Score: 7 | Accepted answer
  
  Not only will you save disk space by converting to BAM, but BAM files are faster to manipulate than SAM. Source: [Dave Tang's SAMTools wiki](https://davetang.org/wiki/tiki-index.php?page=SAMTools#Converting_a_SAM_file_to_a_BAM_file).
  
  `sort` supports uncompressed SAM format from a file or `stdin`, though `index` requires BGZIP-compressed SAM or BAM. I don't think you can get around this.
  
  If you're dead-set on reading from and writing to uncompressed SAM format, you can use pipes to avoid writing BAM files. Specify the output of `view` to be an uncompressed bam, then pipe it to your next command, then pipe it to `view` again to output your sorted SAM file.
  
  ```
  samtools view -u in.sam | samtools sort | samtools view -h > sort.sam
  samtools view -u sort.sam | samtools index
  
  ```
  
  > **Comments:**
  > **username_4**: I suspect you could convince tabix to index a SAM file, though you're correct that using BAM is a MUCH better solution.  
  
  ---
  
  