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?
4 Answers
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.
Assuming you want to visualize the pileup of reads across the reference, you may want to look into ASCIIgenome, a command line visualization tool for sam/bam/cram files, similar in spirit as IGV, but working in the command line.
ASCIIGenome my_sorted.bam
less filename.sam
would allow me to see SAM file as tab delimited. Everyone has a good perspective on this topic. For example @Pierre and @b.nota both mentioned that this is binary compressed file, therefore we can not see it directly without a program that interprets the bam binary encoding. An example of that program/interpreter is presented by @mgalardini and @gringer. And obviously, IGV being one of the widely used. Thank you, guys. Great answers. $\endgroup$