+
+ # 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
+ end