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
24 def set_max_name_length( length = MAX_NAME_LENGTH_DEFAULT )
26 length = MAX_NAME_LENGTH_DEFAULT
28 @max_name_length = length
31 def clean( clean = true )
35 def write( msa, path )
36 if ( !msa.is_aligned() )
37 error_msg = "attempt to write unaligned msa in phylip sequential format"
38 raise StandardError, error_msg, caller
42 Util.check_file_for_writability( path )
44 f = File.open( path, "a" )
46 f.print( msa.get_number_of_seqs().to_s() )
48 f.print( msa.get_length().to_s() )
49 f.print( Evoruby::Constants::LINE_DELIMITER )
50 for i in 0 ... msa.get_number_of_seqs()
51 seq_obj = msa.get_sequence( i )
52 name = seq_obj.get_name()
53 seq = seq_obj.get_sequence_as_string()
54 name = name.gsub( /\s+$/, '')
55 name = name.gsub( /\s+/, '_')
56 name = Util.normalize_seq_name( name, @max_name_length )
60 seq = Util.clean_seq_str( seq )
63 f.print( Evoruby::Constants::LINE_DELIMITER )
68 end # class PhylipSequentialWriter