+ # Method to subparse alignment lines.
+ def parse_alignment(line)
+ fields = line.split("\t")
+
+ raise SamError, "Bad number of fields: #{fields.size} < 11" if fields.size < 11
+
+ qname = fields[0]
+ flag = fields[1].to_i
+ rname = fields[2]
+ pos = fields[3].to_i
+ mapq = fields[4].to_i
+ cigar = fields[5]
+ rnext = fields[6]
+ pnext = fields[7].to_i
+ tlen = fields[8].to_i
+ seq = fields[9]
+ qual = fields[10]
+
+ check_qname(qname)
+ check_flag(flag)
+ check_rname(rname)
+ check_pos(pos)
+ check_mapq(mapq)
+ check_rnext(rnext)
+ check_pnext(pnext)
+ check_tlen(tlen)
+ check_seq(seq)
+ check_qual(qual)
+
+ entry = {}
+ entry[:QNAME] = qname
+ entry[:FLAG] = Flag.new(flag)
+ entry[:RNAME] = rname
+ entry[:POS] = pos
+ entry[:MAPQ] = mapq
+ entry[:CIGAR] = Cigar.new(cigar)
+ entry[:RNEXT] = rnext
+ entry[:PNEXT] = pnext
+ entry[:TLEN] = tlen
+ entry[:SEQ] = (qual == '*') ? Seq.new(seq_name: qname, seq: seq) : Seq.new(seq_name: qname, seq: seq, qual: qual)
+ entry[:QUAL] = qual
+
+ # Optional fields - where some are really important! HATE HATE HATE SAM!!!
+
+ fields[11 .. -1].each do |field|
+ tag, type, val = field.split(':')
+
+ raise SamError, "Non-unique optional tag: #{tag}" if entry[tag.to_sym]
+
+ # A [!-~] Printable character
+
+ # i [-+]?[0-9]+ Singed 32-bit integer
+ if type == 'i'
+ raise SamError, "Bad tag in optional field: #{field}" unless val =~ /^[-+]?[0-9]+$/
+ val = val.to_i
+ end
+
+ # f [-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)? Single-precision floating number
+ # Z [ !-~]+ Printable string, including space
+ # H [0-9A-F]+ Byte array in the Hex format
+ # B [cCsSiIf](,[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)+ Integer or numeric array
+
+ entry[tag.to_sym] = val
+ end
+
+ entry
+ end
+
+ # Method to check qname.
+ def check_qname(qname)
+ raise SamError, "Bad qname: #{qname}" unless qname =~ /^[!-?A-~]{1,255}$/
+ end
+
+ # Method to check flag.
+ def check_flag(flag)
+ raise SamError, "Bad flag: #{flag}" unless (0 .. 2**16 - 1).include? flag
+ end
+
+ # Method to check if rname, when not '*' and
+ # @SQ header lines are present, is located in
+ # the header hash.
+ def check_rname(rname)
+ raise SamError, "Bad rname: #{rname}" unless rname =~ /^(\*|[!-()+-<>-~][!-~]*)$/
+
+ unless @header.empty? or rname == '*'
+ unless @header[:SQ][:SN][rname.to_sym]
+ raise SamError, "rname not found in header hash: #{rname}"
+ end
+ end
+ end
+
+ # Method to check pos.
+ def check_pos(pos)
+ raise SamError, "Bad pos: #{pos}" unless (0 .. 2**29 - 1).include? pos
+ end
+
+ # Method to check mapq.
+ def check_mapq(mapq)
+ raise SamError, "Bad mapq: #{mapq}" unless (0 .. 2**8 - 1).include? mapq
+ end
+
+ # Method to check if rnext, when not '*' or '='
+ # and @SQ header lines are present, is located
+ # in the header hash.
+ def check_rnext(rnext)
+ raise SamError, "Bad rnext: #{rnext}" unless rnext =~ /^(\*|=|[!-()+-<>-~][!-~]*)$/
+
+ unless @header.empty? or rnext == '*' or rnext == '='
+ unless @header[:SQ][:SN][rnext.to_sym]
+ raise SamError, "rnext not found in header hash: #{rnext}"
+ end
+ end
+ end
+
+ # Method to check pnext.
+ def check_pnext(pnext)
+ raise SamError, "Bad pnext: #{pnext}" unless (0 .. 2**29 - 1).include? pnext
+ end
+
+ # Method to check tlen.
+ def check_tlen(tlen)
+ raise SamError, "Bad tlen: #{tlen}" unless (-2**29 + 1 .. 2**29 - 1).include? tlen
+ end
+
+ # Method to check seq.
+ def check_seq(seq)
+ raise SamError, "Bad seq: #{seq}" unless seq =~ /^(\*|[A-Za-z=.]+)$/
+ end
+
+ # Method to check qual.
+ def check_qual(qual)
+ raise SamError, "Bad qual: #{qual}" unless qual =~ /^[!-~]+$/
+ end
+
+ # Method to deconvolute the SAM flag field.
+ class Flag
+ attr_reader :flag
+
+ # Method to initialize a Flag object.
+ def initialize(flag)
+ @flag = flag
+ end
+
+ # Method to test if template have
+ # multiple fragments in sequencing.
+ def multi?
+ (flag & FLAG_MULTI) == 0
+ end
+
+ # Method to test if each fragment
+ # properly aligned according to the aligner.
+ def aligned?
+ (flag & FLAG_ALIGNED) == 0
+ end
+
+ # Method to test if the fragment was unmapped.
+ def unmapped?
+ (flag & FLAG_UNMAPPED) == 0
+ end
+
+ # Method to test if the next fragment was unmapped.
+ def next_unmapped?
+ (flag & FLAG_NEXT_UNMAPPED) == 0
+ end
+
+ # Method to test if the fragment was reverse complemented.
+ def revcomp?
+ (flag & FLAG_REVCOMP) == 0
+ end
+
+ # Method to test if the next fragment was reverse complemented.
+ def next_revcomp?
+ (flag & FLAG_NEXT_REVCOMP) == 0
+ end
+
+ # Method to test if the fragment was first in the template.
+ def first?
+ (flag & FLAG_FIRST) == 0
+ end
+
+ # Method to test if the fragment was last in the template.
+ def last?
+ (flag & FLAG_LAST) == 0
+ end
+
+ # Method to test for secondary alignment.
+ def secondary_alignment?
+ (flag & FLAG_SECONDARY_ALIGNMENT) == 0
+ end
+
+ # Method to test for quality fail.
+ def quality_fail?
+ (flag & FLAG_QUALITY_FAIL) == 0
+ end
+
+ # Method to test for PCR or optical duplicates.
+ def duplicates?
+ (flag & FLAG_DUPLICATES) == 0
+ end