2 # = lib/evo/io/parser/fasta_parser - FastaParser class
4 # Copyright:: Copyright (C) 20017 Christian M. Zmasek
5 # License:: GNU Lesser General Public License (LGPL)
7 # last modified: 05/17/2007
9 require 'lib/evo/io/parser/msa_parser'
10 require 'lib/evo/msa/msa'
16 class FastaParser < MsaParser
22 Util.check_file_for_readability( path )
24 current_seq = String.new()
27 ic = Iconv.new( 'UTF-8//IGNORE', 'UTF-8' )
28 File.open( path ) do | file |
29 while line = file.gets
30 line = ic.iconv( line )
31 if can_ignore?( line, saw_first_seq )
33 elsif line =~ /^\s*>\s*(.+)/
35 add_seq( name, current_seq, msa )
37 current_seq = String.new()
38 elsif line =~ /^\s*(.+)/
40 error_msg = "format error at: " + line
41 raise IOError, error_msg
43 # was: seq = $1.rstrip
44 seq = $1.gsub(/\s+/, '')
47 error_msg = "Unexpected line: " + line
48 raise IOError, error_msg
52 add_seq( name, current_seq, msa )
58 def add_seq( name, seq, msa )
59 if name.length > 0 && seq.length > 0
64 def can_ignore?( line, saw_first_seq )
65 return ( line !~ /\S/ ||
67 ( !saw_first_seq && line =~/^\s*[^>]/ ) )
70 end # class FastaParser