<p>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.</p>

<p>bam files are smaller than sam files, which is the reason most people like to use bam files instead of sam files.</p>

CC BY-SA 3.0
2017-09-01T20:57:52.540Z
2017-09-01T20:57:52.540Z
benn
<p>The way to visualise BAM files as text in Linux is with tools like samtools.</p>

<p>If you want to see the raw binary information as slightly more human-readable, you can run <code>hexdump -C &lt;file.bam&gt;</code>, but that type of text output is completely useless for further processing.</p>

<p>If you want more specifics about the BAM file format, you can have a look at the <a href="https://samtools.github.io/hts-specs/SAMv1.pdf" rel="nofollow noreferrer">SAM file format specification</a>, which also includes details on the related BAM and CRAM formats.</p>

CC BY-SA 3.0
2017-09-02T03:14:28.830Z
2017-09-02T03:52:52.650Z
2017-09-02T03:52:52.650Z
gringer
<p>Assuming you want to visualize the pileup of reads across the reference, you may want to look into <a href="https://github.com/dariober/ASCIIGenome" rel="nofollow noreferrer">ASCIIgenome</a>, a command line visualization tool for sam/bam/cram files, similar in spirit as <a href="http://software.broadinstitute.org/software/igv/" rel="nofollow noreferrer">IGV</a>, but working in the command line.</p>

<pre><code>ASCIIGenome my_sorted.bam
</code></pre>

<p><a href="https://i.sstatic.net/ouCp4.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ouCp4.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2017-09-04T15:15:06.783Z
2017-09-04T15:15:06.783Z
mgalardini
<blockquote>
  <p>Why not work only with sam files and we need to convert them to bam?</p>
</blockquote>

<p>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 ?).</p>

<p>Furthermore, bam files are compressed using a  method that allow them to be accessed using <strong>random-access</strong>, that is how you can query a sub-part of bam file for a given genomic location.</p>

<p>you can always go back to SAM using samtools.</p>

CC BY-SA 3.0
2017-09-01T21:11:53.110Z
2017-09-01T21:11:53.110Z
Pierre
<p>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?</p>

CC BY-SA 3.0
2017-09-02T14:21:15.510Z
What do you mean by "visualize bam files as text"? Do you search for something like [IGV](http://software.broadinstitute.org/software/igv/bam)?
Kamil S Jaron
CC BY-SA 3.0
2017-09-06T00:08:39.863Z
@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](http://software.broadinstitute.org/software/igv/bam) being one of the widely used. Thank you, guys. Great answers.
andresito
<sam>
True bam file encoding and viewing as text
# True bam file encoding and viewing as text

(Asked by: username_0 on 2017-09-01)

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?

**Tags:** sam
> **Comments:**
> **username_1**: What do you mean by "visualize bam files as text"? Do you search for something like [IGV](http://software.broadinstitute.org/software/igv/bam)?  
> **username_0**: @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](http://software.broadinstitute.org/software/igv/bam) being one of the widely used. Thank you, guys. Great answers.  

---

## 4 answers

### Answer by username_5 on 2017-09-01. Score: 8 | Accepted answer

> 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.


---

### Answer by username_2 on 2017-09-01. Score: 3

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.


---

### Answer by username_3 on 2017-09-02. Score: 3

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` , 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](https://samtools.github.io/hts-specs/SAMv1.pdf), which also includes details on the related BAM and CRAM formats.


---

### Answer by username_4 on 2017-09-04. Score: 3

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


---

