We all have been using samtools to convert between sam and bam files. SAM is tab-delimited file. My question resides on the ability to visualize bam files as text. Is there any way to visualize any bam file as text in linux? Why not work only with sam files and we need to convert them to bam?
Why not work only with sam files and we need to convert them to bam?
SAM is for human only, a binary format like BAM is smaller and parsed much faster by a program (and we want speed and optimize spaces for those huge files, don't we ?).
Furthermore, bam files are compressed using a method that allow them to be accessed using random-access, that is how you can query a sub-part of bam file for a given genomic location.
you can always go back to SAM using samtools.
bam file is a compressed sam file, comparable with zip or tar. They are binary, which means we (humans) cannot read them. So you need to unpack them (convert back to sam) to read it as a text file.
bam files are smaller than sam files, which is the reason most people like to use bam files instead of sam files.
The way to visualise BAM files as text in Linux is with tools like samtools.
If you want to see the raw binary information as slightly more human-readable, you can run
hexdump -C <file.bam>, but that type of text output is completely useless for further processing.
If you want more specifics about the BAM file format, you can have a look at the SAM file format specification, which also includes details on the related BAM and CRAM formats.