(no commit message)
[jalview.git] / forester / java / src / org / forester / protein / BasicDomain.java
1 // $Id:
2 //
3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
5 //
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
8 // All rights reserved
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26
27 package org.forester.protein;
28
29 import java.util.HashMap;
30 import java.util.Map;
31
32 import org.forester.util.ForesterUtil;
33
34 public class BasicDomain implements Domain {
35
36     private static short                    COUNT        = 0;
37     private final static Map<Short, String> ID_TO_STRING = new HashMap<Short, String>();
38     private final static Map<String, Short> STRING_TO_ID = new HashMap<String, Short>();
39     final private int                       _from;
40     final private short                     _id;
41     final private short                     _number;
42     final private double                    _per_domain_evalue;
43     final private double                    _per_domain_score;
44     final private int                       _to;
45     final private short                     _total_count;
46
47     public BasicDomain( final String id ) {
48         if ( ForesterUtil.isEmpty( id ) ) {
49             throw new IllegalArgumentException( "attempt to create protein domain with null or empty id" );
50         }
51         _id = obtainIdAsShort( id );
52         _from = -1;
53         _to = -1;
54         _number = -1;
55         _total_count = -1;
56         _per_domain_evalue = -1;
57         _per_domain_score = -1;
58     }
59
60     public BasicDomain( final String id,
61                         final int from,
62                         final int to,
63                         final short number,
64                         final short total_count,
65                         final double per_domain_evalue,
66                         final double per_domain_score ) {
67         if ( ( from >= to ) || ( from < 0 ) ) {
68             throw new IllegalArgumentException( "attempt to create protein domain from " + from + " to " + to );
69         }
70         if ( ForesterUtil.isEmpty( id ) ) {
71             throw new IllegalArgumentException( "attempt to create protein domain with null or empty id" );
72         }
73         if ( ( number > total_count ) || ( number < 0 ) ) {
74             throw new IllegalArgumentException( "attempt to create protein domain number " + number + " out of "
75                     + total_count );
76         }
77         if ( per_domain_evalue < 0.0 ) {
78             throw new IllegalArgumentException( "attempt to create protein domain with negative E-value" );
79         }
80         _id = obtainIdAsShort( id );
81         _from = from;
82         _to = to;
83         _number = number;
84         _total_count = total_count;
85         _per_domain_evalue = per_domain_evalue;
86         _per_domain_score = per_domain_score;
87     }
88
89     /**
90      * Basic domains are compared/sorted based upon their identifiers (case
91      * insensitive) and their numbers.
92      *
93      */
94     @Override
95     public int compareTo( final Domain domain ) {
96         if ( domain.getClass() != this.getClass() ) {
97             throw new IllegalArgumentException( "attempt to compare [" + domain.getClass() + "] to " + "["
98                     + this.getClass() + "]" );
99         }
100         if ( this == domain ) {
101             return 0;
102         }
103         return getDomainId().compareTo( domain.getDomainId() );
104     }
105
106     /**
107      * Basic domains are considered equal if they have the same identifier (case
108      * sensitive).
109      *
110      */
111     @Override
112     public boolean equals( final Object o ) {
113         if ( this == o ) {
114             return true;
115         }
116         else if ( o == null ) {
117             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to null" );
118         }
119         else if ( o.getClass() != this.getClass() ) {
120             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
121                     + o.getClass() + "]" );
122         }
123         else {
124             return getDomainId().equals( ( ( Domain ) o ).getDomainId() );
125         }
126     }
127
128     @Override
129     public String getDomainId() {
130         return obtainIdFromShort( _id );
131     }
132
133     @Override
134     public int getFrom() {
135         return _from;
136     }
137
138     @Override
139     public int getLength() {
140         return ( 1 + getTo() ) - getFrom();
141     }
142
143     @Override
144     public short getNumber() {
145         return _number;
146     }
147
148     @Override
149     public double getPerDomainEvalue() {
150         return _per_domain_evalue;
151     }
152
153     @Override
154     public double getPerDomainScore() {
155         return _per_domain_score;
156     }
157
158     @Override
159     public int getTo() {
160         return _to;
161     }
162
163     @Override
164     public short getTotalCount() {
165         return _total_count;
166     }
167
168     @Override
169     public int hashCode() {
170         return getDomainId().hashCode();
171     }
172
173     @Override
174     public String toString() {
175         return getDomainId();
176     }
177
178     public StringBuffer toStringBuffer() {
179         return new StringBuffer( getDomainId() );
180     }
181
182     public final static short obtainIdAsShort( final String id ) {
183         if ( !STRING_TO_ID.containsKey( id ) ) {
184             if ( COUNT >= ( Short.MAX_VALUE - 2 ) ) {
185                 throw new RuntimeException( "too many domain ids!" );
186             }
187             ID_TO_STRING.put( COUNT, id );
188             STRING_TO_ID.put( id, COUNT );
189             ++COUNT;
190         }
191         return STRING_TO_ID.get( id );
192     }
193
194     public final static String obtainIdFromShort( final short id ) {
195         return ID_TO_STRING.get( id );
196     }
197 }