inprogress
[jalview.git] / forester / ruby / evoruby / lib / evo / io / writer / phylip_sequential_writer.rb
1 #
2 # = lib/evo/io/writer/phylip_sequential_writer.rb - PhylipSequentialWriter class
3 #
4 # Copyright::  Copyright (C) 2006-2007 Christian M. Zmasek
5 # License::    GNU Lesser General Public License (LGPL)
6 #
7 # $Id: phylip_sequential_writer.rb,v 1.4 2008/09/03 00:31:38 cmzmasek Exp $
8 #
9 # last modified: 05/16/2007
10
11 require 'lib/evo/io/writer/msa_writer'
12
13 module Evoruby
14
15   class PhylipSequentialWriter < MsaWriter
16
17     MAX_NAME_LENGTH_DEFAULT = 10
18
19     def initialize()
20       @max_name_length     = MAX_NAME_LENGTH_DEFAULT
21       @clean               = false
22       @ex_if_name_too_long = false
23     end
24
25     def set_max_name_length( length = MAX_NAME_LENGTH_DEFAULT )
26       if length < 1
27         length = MAX_NAME_LENGTH_DEFAULT
28       end
29       @max_name_length = length
30     end
31
32     def clean( clean = true )
33       @clean = clean
34     end
35
36     def set_exception_if_name_too_long( exception_if_name_too_long )
37       @ex_if_name_too_long = exception_if_name_too_long
38     end
39
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
44       end
45
46
47       Util.check_file_for_writability( path )
48
49       f = File.open( path, "a" )
50
51       f.print( msa.get_number_of_seqs().to_s() )
52       f.print( " " )
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 )
62         f.print( name )
63         f.print( " " )
64         if ( @clean )
65           seq = Util.clean_seq_str( seq )
66         end
67         f.print( seq )
68         f.print( Evoruby::Constants::LINE_DELIMITER )
69       end
70       f.close()
71     end
72
73   end # class PhylipSequentialWriter
74
75 end # module Evoruby