2 # = lib/evo/io/parser/fasta_parser - FastaParser class
4 # Copyright:: Copyright (C) 2006-2007 Christian M. Zmasek
5 # License:: GNU Lesser General Public License (LGPL)
7 # $Id: fasta_parser.rb,v 1.11 2010/10/08 22:04:17 cmzmasek Exp $
9 # last modified: 05/17/2007
11 require 'lib/evo/io/parser/msa_parser'
12 require 'lib/evo/msa/msa'
18 class FastaParser < MsaParser
24 Util.check_file_for_readability( path )
26 current_seq = String.new()
29 ic = Iconv.new( 'UTF-8//IGNORE', 'UTF-8' )
30 File.open( path ) do | file |
31 while line = file.gets
32 line = ic.iconv( line )
33 if can_ignore?( line, saw_first_seq )
35 elsif line =~ /^\s*>\s*(.+)/
37 add_seq( name, current_seq, msa )
39 current_seq = String.new()
40 elsif line =~ /^\s*(.+)/
42 error_msg = "format error at: " + line
43 raise IOError, error_msg
45 # was: seq = $1.rstrip
46 seq = $1.gsub(/\s+/, '')
49 error_msg = "Unexpected line: " + line
50 raise IOError, error_msg
54 add_seq( name, current_seq, msa )
60 def add_seq( name, seq, msa )
61 if name.length > 0 && seq.length > 0
66 def can_ignore?( line, saw_first_seq )
67 return ( line !~ /\S/ ||
69 ( !saw_first_seq && line =~/^\s*[^>]/ ) )
72 end # class FastaParser