# Class to parse and write SAM files.
class Sam < Filesys
- attr_accessor :io
+ attr_accessor :io, :header
# Method to initialize a Sam object.
def initialize(io = nil)
- @io = io
- @header_hash = {}
+ @io = io
+ @header = {}
+
+ parse_header
end
- # Method to parse the header of a SAM file.
+ def each
+ @io.each_line do |line|
+ unless line[0] == '@'
+ entry = parse_alignment(line.chomp)
+
+ yield entry if block_given?
+ end
+ end
+ end
+
+ private
+
+ # Method to parse the header section of a SAM file.
# Each header line should match:
# /^@[A-Za-z][A-Za-z](\t[A-Za-z][A-Za-z0-9]:[ -~]+)+$/ or /^@CO\t.*/.
# Tags containing lowercase letters are reserved for end users.
- def header
+ def parse_header
@io.each_line do |line|
if line =~ /^@([A-Za-z][A-Za-z])/
line.chomp!
tag = $1
case tag
- when 'HD' then parse_header(line)
- when 'SQ' then parse_sequence(line)
- when 'RG' then parse_read_group(line)
- when 'PG' then parse_program(line)
- when 'CO' then parse_comment(line)
+ when 'HD' then subparse_header(line)
+ when 'SQ' then subparse_sequence(line)
+ when 'RG' then subparse_read_group(line)
+ when 'PG' then subparse_program(line)
+ when 'CO' then subparse_comment(line)
else
raise SamError, "Unknown header tag: #{tag}"
end
end
end
- return @header_hash.empty? ? nil : @header_hash
- end
-
- def each
- @io.each_line do |line|
- unless line[0] == '@'
- entry = parse_alignment(line.chomp)
-
- yield entry if block_given?
- end
- end
+ return @header.empty? ? nil : @header
end
- private
-
# Method to subparse header lines.
- def parse_header(line)
+ def subparse_header(line)
hash = {}
fields = line.split("\t")
end
end
- @header_hash[:HD] = hash
+ @header[:HD] = hash
end
# Method to subparse sequence lines.
- def parse_sequence(line)
- @header_hash[:SQ] = Hash.new unless @header_hash[:SQ].is_a? Hash
+ def subparse_sequence(line)
+ @header[:SQ] = Hash.new unless @header[:SQ].is_a? Hash
hash = {}
fields = line.split("\t")
end
end
- @header_hash[:SQ][:SN] = Hash.new unless @header_hash[:SQ][:SN].is_a? Hash
+ @header[:SQ][:SN] = Hash.new unless @header[:SQ][:SN].is_a? Hash
- if @header_hash[:SQ][:SN].has_key? seq_name
+ if @header[:SQ][:SN].has_key? seq_name
raise SamError, "Non-unique sequence name: #{seq_name}"
else
- @header_hash[:SQ][:SN][seq_name] = hash
+ @header[:SQ][:SN][seq_name] = hash
end
end
# Method to subparse read group lines.
- def parse_read_group(line)
- @header_hash[:RG] = Hash.new unless @header_hash[:RG].is_a? Hash
+ def subparse_read_group(line)
+ @header[:RG] = Hash.new unless @header[:RG].is_a? Hash
hash = {}
fields = line.split("\t")
end
end
- @header_hash[:RG][:ID] = Hash.new unless @header_hash[:RG][:ID].is_a? Hash
+ @header[:RG][:ID] = Hash.new unless @header[:RG][:ID].is_a? Hash
- if @header_hash[:RG][:ID].has_key? id
+ if @header[:RG][:ID].has_key? id
raise SamError, "Non-unique read group identifier: #{id}"
else
- @header_hash[:RG][:ID][id] = hash
+ @header[:RG][:ID][id] = hash
end
end
# Method to subparse program lines.
- def parse_program(line)
- @header_hash[:PG] = Hash.new unless @header_hash[:PG].is_a? Hash
+ def subparse_program(line)
+ @header[:PG] = Hash.new unless @header[:PG].is_a? Hash
hash = {}
fields = line.split("\t")
end
end
- @header_hash[:PG][:ID] = Hash.new unless @header_hash[:PG][:ID].is_a? Hash
+ @header[:PG][:ID] = Hash.new unless @header[:PG][:ID].is_a? Hash
- if @header_hash[:PG][:ID].has_key? id
+ if @header[:PG][:ID].has_key? id
raise SamError, "Non-unique program record identifier: #{id}"
else
- @header_hash[:PG][:ID][id] = hash
+ @header[:PG][:ID][id] = hash
end
end
# Method to subparse comment lines.
- def parse_comment(line)
- @header_hash[:CO] = Array.new unless @header_hash[:CO].is_a? Array
+ def subparse_comment(line)
+ @header[:CO] = Array.new unless @header[:CO].is_a? Array
if line =~ /^@CO\t(.+)/
- @header_hash[:CO] << $1
+ @header[:CO] << $1
else
raise SamError, "Bad comment line: #{line}"
end
raise SamError, "Bad seq: #{seq}" unless seq =~ /^(\*|[A-Za-z=.]+)$/
raise SamError, "Bad qual: #{qual}" unless qual =~ /^[!-~]+$/
+ check_rname(rname)
+
entry = {}
entry[:QNAME] = qname
entry[:FLAG] = flag
entry
end
+
+ # Method to check if rname, when not '*' and
+ # @SQ header lines are present, is located in
+ # the header hash.
+ def check_rname(rname)
+ unless @header.empty? or rname == '*'
+ unless @header[:SQ][:SN].has_key? rname.to_sym
+ raise SamError, "rname not found in header hash: #{rname}"
+ end
+ end
+ end
end