2 # = lib/evo/io/parser/fasta_parser - FastaParser class
4 # Copyright:: Copyright (C) 2017 Christian M. Zmasek
5 # License:: GNU Lesser General Public License (LGPL)
7 # Last modified: 2017/02/07
9 require 'lib/evo/io/parser/msa_parser'
10 require 'lib/evo/msa/msa'
14 class FastaParser < MsaParser
20 Util.check_file_for_readability( path )
22 current_seq = String.new()
25 File.open( path ) do | file |
26 while line = file.gets
28 line.encode!("UTF-8", :invalid => :replace, :undef => :replace, :replace => "?")
29 if can_ignore?( line, saw_first_seq )
31 elsif line =~ /^\s*>\s*(.+)/
33 add_seq( name, current_seq, msa )
35 current_seq = String.new()
36 elsif line =~ /^\s*(.+)/
38 error_msg = "format error at: " + line
39 raise IOError, error_msg
41 # was: seq = $1.rstrip
42 seq = $1.gsub(/\s+/, '')
45 error_msg = "Unexpected line: " + line
46 raise IOError, error_msg
50 add_seq( name, current_seq, msa )
56 def add_seq( name, seq, msa )
57 if name.length > 0 && seq.length > 0
62 def can_ignore?( line, saw_first_seq )
63 return ( line !~ /\S/ ||
65 ( !saw_first_seq && line =~/^\s*[^>]/ ) )
68 end # class FastaParser