2 # = lib/evo/io/writer/phylip_sequential_writer.rb - PhylipSequentialWriter class
4 # Copyright:: Copyright (C) 2006-2007 Christian M. Zmasek
5 # License:: GNU Lesser General Public License (LGPL)
7 # $Id: phylip_sequential_writer.rb,v 1.4 2008/09/03 00:31:38 cmzmasek Exp $
9 # last modified: 05/16/2007
11 require 'lib/evo/io/writer/msa_writer'
15 class PhylipSequentialWriter < MsaWriter
17 MAX_NAME_LENGTH_DEFAULT = 10
20 @max_name_length = MAX_NAME_LENGTH_DEFAULT
22 @ex_if_name_too_long = false
25 def set_max_name_length( length = MAX_NAME_LENGTH_DEFAULT )
27 length = MAX_NAME_LENGTH_DEFAULT
29 @max_name_length = length
32 def clean( clean = true )
36 def set_exception_if_name_too_long( exception_if_name_too_long )
37 @ex_if_name_too_long = exception_if_name_too_long
40 def write( msa, path )
41 if ( !msa.is_aligned() )
42 error_msg = "attempt to write unaligned msa in phylip sequential format"
43 raise StandardError, error_msg, caller
47 Util.check_file_for_writability( path )
49 f = File.open( path, "a" )
51 f.print( msa.get_number_of_seqs().to_s() )
53 f.print( msa.get_length().to_s() )
54 f.print( Evoruby::Constants::LINE_DELIMITER )
55 for i in 0 ... msa.get_number_of_seqs()
56 seq_obj = msa.get_sequence( i )
57 name = seq_obj.get_name()
58 seq = seq_obj.get_sequence_as_string()
59 name = name.gsub( /\s+$/, '')
60 name = name.gsub( /\s+/, '_')
61 name = Util.normalize_seq_name( name, @max_name_length, @ex_if_name_too_long )
65 seq = Util.clean_seq_str( seq )
68 f.print( Evoruby::Constants::LINE_DELIMITER )
73 end # class PhylipSequentialWriter