inprogress
[jalview.git] / forester / ruby / evoruby / lib / evo / sequence / domain_structure.rb
1 #
2 # = lib/evo/sequence/domain_structure.rb - DomainStructure class
3 #
4 # Copyright::  Copyright (C) 2006-2007 Christian M. Zmasek
5 # License::    GNU Lesser General Public License (LGPL)
6 #
7 # $Id: domain_structure.rb,v 1.2 2007/06/12 04:51:33 cmzmasek Exp $
8 #
9 # last modified: 05/16/2007
10
11 require 'lib/evo/util/constants'
12
13 module Evoruby
14
15     class DomainStructure
16
17         def initialize( total_length )
18             @domains = Hash.new
19             @total_length = total_length
20         end
21
22         def add_domain( domain, overwrite_if_same_from_to )
23             key = domain.get_from
24             if ( @domains.has_key?( key ) )
25                 prev_domain = @domains[ key ]
26                 if ( prev_domain.get_to == domain.get_to )
27                     puts( "WARNING: more than one domain at the same location [" +
28                         key.to_s + "-" + domain.get_to.to_s + "]: " + prev_domain.get_name + " and " + domain.get_name)
29                     if ( overwrite_if_same_from_to )
30                         puts( "         ignored the one with higher E-value [" +
31                         prev_domain.get_confidence().to_s + " vs " + domain.get_confidence().to_s + "]" )
32                         if prev_domain.get_confidence() < domain.get_confidence()
33                             return # keep previous one
34                         else
35                             @domains[ key ] = domain
36                             return
37                         end
38                     end
39                 end
40
41                 while ( @domains.has_key?( key ) )
42                     key = key + 0.0001
43                 end
44
45             end
46             @domains[ key ] = domain
47         end
48
49         def to_NHX
50             str = String.new
51             str << get_total_length.to_s
52             a = @domains.sort
53             for d in a
54                 domain = d[ 1 ]
55                 str << Evoruby::Constants::DOMAIN_STRUCTURE_NHX_SEPARATOR
56                 str << domain.get_from.to_s
57                 str << Evoruby::Constants::DOMAIN_STRUCTURE_NHX_SEPARATOR
58                 str << domain.get_to.to_s
59                 str << Evoruby::Constants::DOMAIN_STRUCTURE_NHX_SEPARATOR
60                 str << domain.get_confidence.to_s
61                 str << Evoruby::Constants::DOMAIN_STRUCTURE_NHX_SEPARATOR
62                 str << domain.get_name
63             end
64             return str
65         end
66
67         def get_total_length
68             return @total_length
69         end
70
71     end # class DomainStructure
72
73 end # module Evoruby