1 # Copyright (C) 2007-2011 Martin A. Hansen.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # http://www.gnu.org/copyleft/gpl.html
19 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
21 # This software is part of the Biopieces framework (www.biopieces.org).
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
25 # Methods to handle extended CIGAR format used in the
26 # SAM format version v1.4-r962 - April 17, 2011
28 # http://samtools.sourceforge.net/SAM1.pdf
32 # Error class for all exceptions to do with CIGAR.
33 class CigarError < StandardError; end
35 # Class to parse and write SAM files.
46 cigar.scan(/(\d+)([MIDNSHPX=])/).each do |len, op|
54 self.each do |len, op|
55 total += len unless op == 'I'
64 self.each do |len, op|
65 total += len if op == 'M'
74 self.each do |len, op|
75 total += len if op == 'I'
84 self.each do |len, op|
85 total += len if op == 'D'
94 self.each do |len, op|
95 total += len if op == 'H'
104 self.each do |len, op|
105 total += len if op == 'S'
114 unless cigar =~ /^(\*|([0-9]+[MIDNSHPX=])+)$/
115 raise CigarError, "Bad cigar format: #{cigar}"
118 if cigar.gsub(/^[0-9]+H|[0-9]+H$/, "").match('H')
119 raise CigarError, "Bad cigar with internal H: #{cigar}"
122 if cigar.gsub(/^[0-9]+H|[0-9]+H$/, "").gsub(/^[0-9]+S|[0-9]+S$/, "").match('S')
123 raise CigarError, "Bad cigar with internal S: #{cigar}"