check_rname(rname)
check_pos(pos)
check_mapq(mapq)
- check_cigar(cigar)
+ check_cigar(cigar, seq)
check_rnext(rnext)
check_pnext(pnext)
check_tlen(tlen)
# Method to check mapq.
def check_mapq(mapq)
- raise SamError, "Bad mapq: #{mapq}" unless (0 .. 2**8 - 1).include? mapq
+ raise SamError, "Bad mapq: #{mapq}" unless (0 .. 2**8 - 1).include? mapq
end
# Method to check cigar.
- def check_cigar(cigar)
+ def check_cigar(cigar, seq)
raise SamError, "Bad cigar: #{cigar}" unless cigar =~ /^(\*|([0-9]+[MIDNSHPX=])+)$/
+
+ # Check cigar length matches sequence length.
+ unless cigar == '*' or seq == '*'
+ cigar_len = 0
+
+ cigar.scan(/([0-9]+)([MIDNSHPX=])/).each do |len, op|
+ cigar_len += len.to_i if op =~ /[MISX=]/
+ end
+
+ if cigar_len != seq.length
+ raise SamError, "cigar and sequence length mismatch: #{cigar_len} != #{seq.length}"
+ end
+ end
end
# Method to check if rnext, when not '*' or '='