3
$\begingroup$

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?

$\endgroup$
2
  • 4
    $\begingroup$ What do you mean by "visualize bam files as text"? Do you search for something like IGV? $\endgroup$ Commented Sep 2, 2017 at 14:21
  • 1
    $\begingroup$ @Kamil S Jaron I was wondering how we can visualize as text, for example, 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$
    – andresito
    Commented Sep 6, 2017 at 0:08

4 Answers 4

8
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

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

enter image description here

$\endgroup$

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.