in progress
[jalview.git] / forester / ruby / evoruby / lib / evo / sequence / sequence.rb
1 #
2 # = lib/evo/sequence/sequence.rb - Sequence class
3 #
4 # Copyright::  Copyright (C) 2006-2007 Christian M. Zmasek
5 # License::    GNU Lesser General Public License (LGPL)
6 #
7 # $Id: sequence.rb,v 1.10 2009/01/07 02:48:20 cmzmasek Exp $
8
9 require 'set'
10
11 module Evoruby
12
13     class Sequence
14
15         def initialize( name,
16                 molecular_sequence_str,
17                 accession = nil,
18                 accession_source = nil,
19                 taxonomy = nil,
20                 symbol = nil,
21                 secondary_accession = nil,
22                 secondary_accession_source = nil )
23             @name               = String.new( name.strip() )
24             @molecular_sequence = String.new( molecular_sequence_str )
25             if ( accession == nil )
26                 @accession = String.new()
27             else
28                 @accession = String.new( accession.strip() )
29             end
30             if ( accession_source == nil )
31                 @accession_source = String.new()
32             else
33                 @accession_source = String.new( accession_source.strip() )
34             end
35             @taxonomy = taxonomy
36             if ( symbol == nil )
37                 @symbol = String.new()
38             else
39                 @symbol = String.new( symbol.strip() )
40             end
41             if ( secondary_accession == nil )
42                 @secondary_accession = String.new()
43             else
44                 @secondary_accession = String.new( secondary_accession.strip() )
45             end
46             if ( secondary_accession_source == nil )
47                 @secondary_accession_source = String.new()
48             else
49                 @secondary_accession_source = String.new( secondary_accession_source.strip() )
50             end
51         end
52
53         def copy
54             if get_taxonomy == nil
55                 Sequence.new( get_name, get_sequence_as_string, get_accession, get_accession_source, nil, get_symbol, get_secondary_accession, get_secondary_accession_source )
56             else
57                 Sequence.new( get_name, get_sequence_as_string, get_accession, get_accession_source, get_taxonomy.copy, get_symbol, get_secondary_accession, get_secondary_accession_source )
58             end
59         end
60
61         def get_name()
62             @name
63         end
64
65         def set_name( name )
66             @name = name
67         end
68
69         def get_sequence_as_string()
70             @molecular_sequence
71         end
72
73         def get_accession()
74             @accession
75         end
76
77         def get_accession_source()
78             @accession_source
79         end
80
81         def get_secondary_accession()
82             @secondary_accession
83         end
84
85         def get_secondary_accession_source()
86             @secondary_accession_source
87         end
88
89         def get_symbol()
90             @symbol
91         end
92
93         def get_taxonomy()
94             @taxonomy
95         end
96
97         def get_length()
98             @molecular_sequence.length
99         end
100
101         def get_residue( position )
102             get_slice( position, 1 )
103         end
104
105         def get_character_code( position )
106             @molecular_sequence.getbyte( position )
107         end
108
109         def get_gap_ratio()
110             return get_gap_length().to_f / get_length()
111         end
112
113         def get_gap_length()
114             counter = 0
115             for i in 0 ... get_length()
116                 if ( Util.is_aa_gap_character?( get_character_code( i ) ) )
117                     counter += 1
118                 end
119             end
120             return counter;
121         end
122
123         def delete_residue!( position )
124             if ( position < 0 || position >= get_length() )
125                 error_msg = "attempt to delete residue at postion out of range"
126                 raise ArgumentError, error_msg
127             end
128             @molecular_sequence.slice!( position )
129         end
130
131         def get_slice( start, length )
132             if ( start < 0 || start + length > get_length() )
133                 error_msg = "attempt to get sequence residue(s) at postion out of range"
134                 raise ArgumentError, error_msg
135             end
136             @molecular_sequence.slice( start, length )
137         end
138
139         def get_slice!( start, length )
140             if ( start < 0 || start + length > get_length() )
141                 error_msg = "attempt to get sequence residue(s) at postion out of range"
142                 raise ArgumentError, error_msg
143             end
144             @molecular_sequence.slice!( start, length )
145         end
146
147         def get_subsequence( first, last )
148             if ( last < first )
149                 error_msg = "attempt to get subsequence from " + first + " to " + last
150                 raise ArgumentError, error_msg
151             end
152             return Sequence.new( get_name, @molecular_sequence.slice( first, last - first + 1 ) )
153         end
154
155         def append!( molecular_sequence_str )
156             @molecular_sequence.concat( molecular_sequence_str )
157         end
158
159         def to_str
160             return "[" + @name + "] " + @molecular_sequence
161         end
162
163         def to_fasta
164             return ">" + @name + Constants::LINE_DELIMITER  + @molecular_sequence
165         end
166
167
168     end # class Sequence
169
170 end # module Evoruby