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?

  • $\begingroup$ 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. $\endgroup$
    – Community Bot
    Feb 3, 2022 at 16:27
  • 1
    $\begingroup$ 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. $\endgroup$
    – user172818
    Feb 4, 2022 at 22:14

1 Answer 1


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.

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
  • $\begingroup$ I suspect you could convince tabix to index a SAM file, though you're correct that using BAM is a MUCH better solution. $\endgroup$
    – Devon Ryan
    Feb 4, 2022 at 13:09
  • $\begingroup$ Tabix has a preset for sam -p sam, but yeah, use bam, there is no advantage using sam anyway and barely any downstream tool swallows sam.gz. $\endgroup$
    – user3051
    Feb 6, 2022 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.