Fork me on GitHub


sambamba-view - tool for extracting information from SAM/BAM files


sambamba view OPTIONS <input.bam | input.sam> [region1 [...]]


sambamba view allows to efficiently filter BAM file for alignments satisfying various conditions, as well as access its SAM header and information about reference sequences. In order to make these data readily available for consumption by scripts in Perl/Python/Ruby, JSON output is provided.

By default, the tool expects BAM file as an input. In order to work with SAM file, specify -S|--sam-input command-line option, the tool does NOT try to guess file format from extension. Beware that when reading SAM, the tool will skip tags which don't conform to the SAM/BAM specification, and set invalid fields to their default values. However, only syntax is checked, use --valid for full validation.


Filtering is presented in two ways. First, you can specify a condition with -F option, using a special language for filtering, described at

Second, if you have an indexed BAM file, several regions can be specified as well. The syntax for regions is the same as in samtools: chr:beg-end where beg and end are 1-based start and end of a closed-end interval on the reference chr.


Alignment record JSON representation is a hash with keys 'qname', 'flag', 'rname', 'pos', 'mapq', 'cigar', 'rnext', 'qual', 'tags', e.g.


JSON representation mimics SAM format except quality is given as an array of integers. This format might change in the future.


MessagePack output format is more effective with respect to performance, while almost as easy to parse as JSON representation. Libraries for parsing MessagePack are available for all popular languages. See for more information.

Alignment record representation is an array, per-element description is given below.

[ 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 ]
  • 0 - read name (string)
  • 1 - flag (integer)
  • 2 - reference sequence ID (integer)
  • 3 - 1-based leftmost mapping position (integer)
  • 4 - mapping quality (integer)
  • 5 - lengths of CIGAR operations (array of integers)
  • 6 - types of CIGAR operations (array of chars)
  • 7 - mate reference sequence ID (integer)
  • 8 - 1-based mate leftmost mapping position (integer)
  • 9 - template length (integer)
  • 10 - segment sequence (string)
  • 11 - phred-base quality (array of integers)
  • 12 - tags (map : string -> value)

Header representation is also an array containing the following:

[ 0 | 1 | 2 | 3 | 4 ]
  • 0 - format version (string)
  • 1 - sorting order (string)
  • 2 - @SQ lines (array of maps : string -> value)
  • 3 - @RG lines (ditto)
  • 4 - @PG lines (ditto)


-F, --filter=FILTER

Set custom filter for alignments.

-f, --format=FORMAT

Specify output format. FORMAT must be one of sam, bam, json, or msgpack (in lowercase). Default one is SAM.

-h, --with-header

Print SAM header before reads. This is always done for BAM output.

-H, --header

Print only SAM header to STDOUT. If FORMAT is sam or bam, its text version is printed, otherwise JSON object is written.

-I, --reference-info

Output to STDOUT reference sequence names and lengths in JSON (see EXAMPLES).

-c, --count

Output to STDOUT only the number of matching records, -hHI options are ignored.

-v, --valid

Output only valid reads.

-S, --sam-input

Specify that the input is SAM file (default is BAM for all operations).

-p, --show-progress

Show progressbar in STDERR. Works only for BAM files, and with no regions specified, i.e. only when reading full file.

-l, --compression-level=COMPRESSION_LEVEL

Set compression level for BAM output, a number from 0 to 9.

-o, --output-filename=FILENAME

Specify output filename (by default everything is written to STDOUT).

-t, --nthreads=NTHREADS

Number of threads to use.


Print basic reference sequence information:

 $ sambamba view --reference-info ex1_header.bam

Count reads with mapping quality not less than 50:

 $ sambamba view -c -F "mapping_quality >= 50" ex1_header.bam

Count properly paired reads overlapping 100..200 on chr1:

 $ sambamba view -c -F "proper_pair" ex1_header.bam chr1:100-200

Output header in JSON format:

 $ sambamba view --header --format=json ex1_header.bam


There's no way to see validation error messages or to set validation stringency at the moment.