in progress....
[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     final private short                     _hmm_len;
47     final private short                     _hmm_from;
48     final private short                     _hmm_to;
49
50     public BasicDomain( final String id ) {
51         if ( ForesterUtil.isEmpty( id ) ) {
52             throw new IllegalArgumentException( "attempt to create protein domain with null or empty id" );
53         }
54         _id = obtainIdAsShort( id );
55         _from = -1;
56         _to = -1;
57         _number = -1;
58         _total_count = -1;
59         _per_domain_evalue = -1;
60         _per_domain_score = -1;
61         _hmm_len = -1;
62         _hmm_from= -1;
63         _hmm_to= -1;
64
65     }
66
67     public BasicDomain( final String id,
68                         final int from,
69                         final int to,
70                         final short number,
71                         final short total_count,
72                         final double per_domain_evalue,
73                         final double per_domain_score ) {
74         if ( ( from >= to ) || ( from < 0 ) ) {
75             throw new IllegalArgumentException( "attempt to create protein domain from " + from + " to " + to );
76         }
77         if ( ForesterUtil.isEmpty( id ) ) {
78             throw new IllegalArgumentException( "attempt to create protein domain with null or empty id" );
79         }
80         if ( ( number > total_count ) || ( number < 0 ) ) {
81             throw new IllegalArgumentException( "attempt to create protein domain number " + number + " out of "
82                     + total_count );
83         }
84         if ( per_domain_evalue < 0.0 ) {
85             throw new IllegalArgumentException( "attempt to create protein domain with negative E-value" );
86         }
87         _id = obtainIdAsShort( id );
88         _from = from;
89         _to = to;
90         _number = number;
91         _total_count = total_count;
92         _per_domain_evalue = per_domain_evalue;
93         _per_domain_score = per_domain_score;
94         _hmm_len = -1;
95         _hmm_from= -1;
96         _hmm_to= -1;
97     }
98     
99     public BasicDomain( final String id,
100                         final int from,
101                         final int to,
102                         final short number,
103                         final short total_count,
104                         final double per_domain_evalue,
105                         final double per_domain_score,
106                         final short hmm_len,
107                         final short hmm_from,
108                         final short hmm_to) {
109         if ( ( from >= to ) || ( from < 0 ) ) {
110             throw new IllegalArgumentException( "attempt to create protein domain from " + from + " to " + to );
111         }
112         if ( ForesterUtil.isEmpty( id ) ) {
113             throw new IllegalArgumentException( "attempt to create protein domain with null or empty id" );
114         }
115         if ( ( number > total_count ) || ( number < 0 ) ) {
116             throw new IllegalArgumentException( "attempt to create protein domain number " + number + " out of "
117                     + total_count );
118         }
119         if ( per_domain_evalue < 0.0 ) {
120             throw new IllegalArgumentException( "attempt to create protein domain with negative E-value" );
121         }
122         if ( ( hmm_from >= hmm_to ) || (  hmm_from < 0 ) ) {
123             throw new IllegalArgumentException( "attempt to create protein domain matching hmm from " + from + " to " + to );
124         }
125         if ( hmm_len <= 0 ) {
126             throw new IllegalArgumentException( "attempt to create protein domain with zero or negative hmm length" );
127         }
128         _id = obtainIdAsShort( id );
129         _from = from;
130         _to = to;
131         _number = number;
132         _total_count = total_count;
133         _per_domain_evalue = per_domain_evalue;
134         _per_domain_score = per_domain_score;
135         _hmm_len = hmm_len;
136         _hmm_from= hmm_from;
137         _hmm_to= hmm_to;
138     }
139
140     /**
141      * Basic domains are compared/sorted based upon their identifiers (case
142      * insensitive) and their numbers.
143      *
144      */
145     @Override
146     public int compareTo( final Domain domain ) {
147         if ( domain.getClass() != this.getClass() ) {
148             throw new IllegalArgumentException( "attempt to compare [" + domain.getClass() + "] to " + "["
149                     + this.getClass() + "]" );
150         }
151         if ( this == domain ) {
152             return 0;
153         }
154         return getDomainId().compareTo( domain.getDomainId() );
155     }
156
157     /**
158      * Basic domains are considered equal if they have the same identifier (case
159      * sensitive).
160      *
161      */
162     @Override
163     public boolean equals( final Object o ) {
164         if ( this == o ) {
165             return true;
166         }
167         else if ( o == null ) {
168             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to null" );
169         }
170         else if ( o.getClass() != this.getClass() ) {
171             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
172                     + o.getClass() + "]" );
173         }
174         else {
175             return getDomainId().equals( ( ( Domain ) o ).getDomainId() );
176         }
177     }
178
179     @Override
180     public String getDomainId() {
181         return obtainIdFromShort( _id );
182     }
183
184     @Override
185     public int getFrom() {
186         return _from;
187     }
188
189     @Override
190     public int getLength() {
191         return ( 1 + getTo() ) - getFrom();
192     }
193
194     @Override
195     public short getNumber() {
196         return _number;
197     }
198
199     @Override
200     public double getPerDomainEvalue() {
201         return _per_domain_evalue;
202     }
203
204     @Override
205     public double getPerDomainScore() {
206         return _per_domain_score;
207     }
208
209     @Override
210     public int getTo() {
211         return _to;
212     }
213
214     @Override
215     public short getTotalCount() {
216         return _total_count;
217     }
218
219     @Override
220     public final short getHmmLen() {
221         return _hmm_len;
222     }
223
224     @Override
225     public final short getHmmFrom() {
226         return _hmm_from;
227     }
228
229     @Override
230     public final short getHmmTo() {
231         return _hmm_to;
232     }
233     
234     @Override
235     public int hashCode() {
236         return getDomainId().hashCode();
237     }
238
239     @Override
240     public String toString() {
241         return getDomainId();
242     }
243
244     public StringBuffer toStringBuffer() {
245         return new StringBuffer( getDomainId() );
246     }
247
248     public final static short obtainIdAsShort( final String id ) {
249         if ( !STRING_TO_ID.containsKey( id ) ) {
250             if ( COUNT >= ( Short.MAX_VALUE - 2 ) ) {
251                 throw new RuntimeException( "too many domain ids!" );
252             }
253             ID_TO_STRING.put( COUNT, id );
254             STRING_TO_ID.put( id, COUNT );
255             ++COUNT;
256         }
257         return STRING_TO_ID.get( id );
258     }
259
260     public final static String obtainIdFromShort( final short id ) {
261         return ID_TO_STRING.get( id );
262     }
263
264     
265 }