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 # SAM format version v1.4-r962 - April 17, 2011
27 # http://samtools.sourceforge.net/SAM1.pdf
30 require 'maasha/filesys'
33 # Error class for all exceptions to do with Genbank.
34 class SamError < StandardError; end
36 REGEX_HEADER = Regexp.new(/^@[A-Za-z][A-Za-z](\t[A-Za-z][A-Za-z0-9]:[ -~]+)+$/)
37 REGEX_COMMENT = Regexp.new(/^@CO\t.*/)
39 # Class to parse and write SAM files.
43 # Method to initialize a Sam object.
44 def initialize(io = nil)
49 # Method to parse the header of a SAM file.
50 # Each header line should match:
51 # /^@[A-Za-z][A-Za-z](\t[A-Za-z][A-Za-z0-9]:[ -~]+)+$/ or /^@CO\t.*/.
52 # Tags containing lowercase letters are reserved for end users.
54 @io.each_line do |line|
55 if line =~ /^@([A-Za-z][A-Za-z])/
61 when 'HD' then parse_header(line)
62 when 'SQ' then parse_sequence(line)
63 when 'RG' then parse_read_group(line)
64 when 'PG' then parse_program(line)
65 when 'CO' then parse_comment(line)
67 raise SamError, "Unknown header tag: #{tag}"
75 return @header_hash.empty? ? nil : @header_hash
79 @io.each_line do |line|
81 entry = parse_alignment(line.chomp)
83 yield entry if block_given?
90 # Method to subparse header lines.
91 def parse_header(line)
93 fields = line.split("\t")
95 if fields[1] =~ /^VN:([0-9]+\.[0-9]+)$/
98 raise SamError, "Bad version number: #{fields[1]}"
102 if fields[2] =~ /^SO:(unknown|unsorted|queryname|coordinate)$/
105 raise SamError, "Bad sort order: #{fields[2]}"
109 @header_hash[:HD] = hash
112 # Method to subparse sequence lines.
113 def parse_sequence(line)
114 @header_hash[:SQ] = Hash.new unless @header_hash[:SQ].is_a? Hash
117 fields = line.split("\t")
119 if fields[1] =~ /^SN:([!-)+-<>-~][!-~]*)$/
122 raise SamError, "Bad sequence name: #{fields[1]}"
125 if fields[2] =~ /^LN:(\d+)$/
128 raise SamError, "Bad sequence length: #{fields[2]}"
131 (3 ... fields.size).each do |i|
132 if fields[i] =~ /^(AS|M5|SP|UR):([ -~]+)$/
135 raise SamError, "Bad sequence tag: #{fields[i]}"
139 @header_hash[:SQ][:SN] = Hash.new unless @header_hash[:SQ][:SN].is_a? Hash
141 if @header_hash[:SQ][:SN].has_key? seq_name
142 raise SamError, "Non-unique sequence name: #{seq_name}"
144 @header_hash[:SQ][:SN][seq_name] = hash
148 # Method to subparse read group lines.
149 def parse_read_group(line)
150 @header_hash[:RG] = Hash.new unless @header_hash[:RG].is_a? Hash
153 fields = line.split("\t")
155 if fields[1] =~ /^ID:([ -~]+)$/
158 raise SamError, "Bad read group identifier: #{fields[1]}"
161 (2 ... fields.size).each do |i|
162 if fields[i] =~ /^(CN|DS|DT|FO|KS|LB|PG|PI|PL|PU|SM):([ -~]+)$/
165 raise SamError, "Bad read group tag: #{fields[i]}"
170 unless hash[:FO] =~ /^\*|[ACMGRSVTWYHKDBN]+$/
171 raise SamError, "Bad flow order: #{hash[:FO]}"
176 unless hash[:PL] =~ /^(CAPILLARY|LS454|ILLUMINA|SOLID|HELICOS|IONTORRENT|PACBIO)$/
177 raise SamError, "Bad platform: #{hash[:PL]}"
181 @header_hash[:RG][:ID] = Hash.new unless @header_hash[:RG][:ID].is_a? Hash
183 if @header_hash[:RG][:ID].has_key? id
184 raise SamError, "Non-unique read group identifier: #{id}"
186 @header_hash[:RG][:ID][id] = hash
190 # Method to subparse program lines.
191 def parse_program(line)
192 @header_hash[:PG] = Hash.new unless @header_hash[:PG].is_a? Hash
195 fields = line.split("\t")
197 if fields[1] =~ /^ID:([ -~]+)$/
200 raise SamError, "Bad program record identifier: #{fields[1]}"
203 (2 ... fields.size).each do |i|
204 if fields[i] =~ /^(PN|CL|PP|VN):([ -~]+)$/
207 raise SamError, "Bad program record tag: #{fields[i]}"
211 @header_hash[:PG][:ID] = Hash.new unless @header_hash[:PG][:ID].is_a? Hash
213 if @header_hash[:PG][:ID].has_key? id
214 raise SamError, "Non-unique program record identifier: #{id}"
216 @header_hash[:PG][:ID][id] = hash
220 # Method to subparse comment lines.
221 def parse_comment(line)
222 @header_hash[:CO] = Array.new unless @header_hash[:CO].is_a? Array
224 if line =~ /^@CO\t(.+)/
225 @header_hash[:CO] << $1
227 raise SamError, "Bad comment line: #{line}"
231 # Method to subparse alignment lines.
232 def parse_alignment(line)
233 fields = line.split("\t")
235 raise SamError, "Bad number of fields: #{fields.size} < 11" if fields.size < 11
238 flag = fields[1].to_i
241 mapq = fields[4].to_i
244 pnext = fields[7].to_i
245 tlen = fields[8].to_i
249 raise SamError, "Bad qname: #{qname}" unless qname =~ /^[!-?A-~]{1,255}$/
250 raise SamError, "Bad flag: #{flag}" unless (0 .. 2**16 - 1).include? flag
251 raise SamError, "Bad rname: #{rname}" unless rname =~ /^(\*|[!-()+-<>-~][!-~]*)$/
252 raise SamError, "Bad pos: #{pos}" unless (0 .. 2**29 - 1).include? pos
253 raise SamError, "Bad mapq: #{mapq}" unless (0 .. 2**8 - 1).include? mapq
254 raise SamError, "Bad cigar: #{cigar}" unless cigar =~ /^(\*|([0-9]+[MIDNSHPX=])+)$/
255 raise SamError, "Bad rnext: #{rnext}" unless rnext =~ /^(\*|=|[!-()+-<>-~][!-~]*)$/
256 raise SamError, "Bad pnext: #{pnext}" unless (0 .. 2**29 - 1).include? pnext
257 raise SamError, "Bad tlen: #{tlen}" unless (-2**29 + 1 .. 2**29 - 1).include? tlen
258 raise SamError, "Bad seq: #{seq}" unless seq =~ /^(\*|[A-Za-z=.]+)$/
259 raise SamError, "Bad qual: #{qual}" unless qual =~ /^[!-~]+$/
262 entry[:QNAME] = qname
264 entry[:RNAME] = rname
267 entry[:CIGAR] = cigar
268 entry[:RNEXT] = rnext
269 entry[:PNEXT] = pnext
279 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<