Created: 2011-07-06 22:44
Updated: 2013-10-07 14:41


This gem provides a lightweight class, Sam, which can be used to parse SAM files by giving it one SAM line at a time.

To install, just install the sam gem:

sudo gem install sam


To print the XT:A tag of each entry, you might do this: require 'sam'

for line in gets
  next if line[0] == "@" # Skip header if it exists

You can do something similar with a one-liner at the command line

cat test.sam | ruby -r sam -ne 'next if $_[0] == "@"; puts$_).tags["XT:A"]' 

You can also use the parse_line method to reuse the same Sam object

require 'sam'
s =

for line in gets
  if s.parse_line(line) # Returns false if line starts with @ (a header line)
    puts s.tags["XT:A"]


The Sam class has the following member variables:

  • name: a string
  • flag: an integer or a string if samtools view -X was used
  • chrom: a string
  • pos: an integer (-1 if unmapped)
  • mapq: an integer (-1 if unspecified)
  • cigar: a string
  • mchrom: a string
  • mpos: an integer (-1 if unmapped)
  • insert: an integer (or the raw string if not set)
  • seq: a string
  • qual: a string
  • tags: a hash mapping tag names to tag values
Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more