- # Method to check cigar string.
- def check_cigar(cigar, seq)
- raise SamError, "Bad cigar: #{cigar}" unless cigar =~ /^(\*|([0-9]+[MIDNSHPX=])+)$/
-
- unless cigar == '*'
- check_cigar_hard_clip(cigar)
- check_cigar_soft_clip(cigar)
- check_cigar_seq_len(cigar, seq) unless seq == '*'
- end
- end
-
- # Method to check cigar hard clipping only at ends.
- def check_cigar_hard_clip(cigar)
- if cigar.gsub(/^[0-9]+H|[0-9]+H$/, "").match('H')
- raise SamError, "Bad cigar with internal H: #{cigar}"
- end
- end
-
- # Method to check cigar soft clipping only at ends or H.
- def check_cigar_soft_clip(cigar)
- if cigar.gsub(/^[0-9]+H|[0-9]+H$/, "").gsub(/^[0-9]+S|[0-9]+S$/, "").match('S')
- raise SamError, "Bad cigar with internal S: #{cigar}"
- end
- end
-
- # Method to check cigar length matches sequence length.
- def check_cigar_seq_len(cigar, 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
-