avoid multiple same dcs
[jalview.git] / forester / java / src / org / forester / surfacing / 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: www.phylosoft.org/forester
26
27 package org.forester.surfacing;
28
29 import org.forester.go.GoId;
30 import org.forester.util.ForesterUtil;
31
32 public class BasicDomain implements Domain {
33
34     final private DomainId _id;
35     final private int      _from;
36     final private int      _to;
37     final private short    _number;
38     final private short    _total_count;
39     final private double   _per_sequence_evalue;
40     final private double   _per_sequence_score;
41     final private double   _per_domain_evalue;
42     final private double   _per_domain_score;
43
44     public BasicDomain( final String id_str ) {
45         if ( ForesterUtil.isEmpty( id_str ) ) {
46             throw new IllegalArgumentException( "attempt to create protein domain with null or empty id" );
47         }
48         _id = new DomainId( id_str );
49         _from = -1;
50         _to = -1;
51         _number = -1;
52         _total_count = -1;
53         _per_sequence_evalue = -1;
54         _per_sequence_score = -1;
55         _per_domain_evalue = -1;
56         _per_domain_score = -1;
57     }
58
59     public BasicDomain( final String id_str,
60                         final int from,
61                         final int to,
62                         final short number,
63                         final short total_count,
64                         final double per_sequence_evalue,
65                         final double per_sequence_score ) {
66         if ( ( from >= to ) || ( from < 0 ) ) {
67             throw new IllegalArgumentException( "attempt to create protein domain from " + from + " to " + to );
68         }
69         if ( ForesterUtil.isEmpty( id_str ) ) {
70             throw new IllegalArgumentException( "attempt to create protein domain with null or empty id" );
71         }
72         if ( ( number > total_count ) || ( number < 0 ) ) {
73             throw new IllegalArgumentException( "attempt to create protein domain number " + number + " out of "
74                     + total_count );
75         }
76         if ( per_sequence_evalue < 0.0 ) {
77             throw new IllegalArgumentException( "attempt to create protein domain with E-value" );
78         }
79         _id = new DomainId( id_str );
80         _from = from;
81         _to = to;
82         _number = number;
83         _total_count = total_count;
84         _per_sequence_evalue = per_sequence_evalue;
85         _per_sequence_score = per_sequence_score;
86         _per_domain_evalue = -1;
87         _per_domain_score = -1;
88     }
89
90     public BasicDomain( final String id_str,
91                         final int from,
92                         final int to,
93                         final short number,
94                         final short total_count,
95                         final double per_sequence_evalue,
96                         final double per_sequence_score,
97                         final double per_domain_evalue,
98                         final double per_domain_score ) {
99         if ( ( from >= to ) || ( from < 0 ) ) {
100             throw new IllegalArgumentException( "attempt to create protein domain from " + from + " to " + to );
101         }
102         if ( ForesterUtil.isEmpty( id_str ) ) {
103             throw new IllegalArgumentException( "attempt to create protein domain with null or empty id" );
104         }
105         if ( ( number > total_count ) || ( number < 0 ) ) {
106             throw new IllegalArgumentException( "attempt to create protein domain number " + number + " out of "
107                     + total_count );
108         }
109         if ( ( per_sequence_evalue < 0.0 ) || ( per_domain_evalue < 0.0 ) ) {
110             throw new IllegalArgumentException( "attempt to create protein domain with E-value" );
111         }
112         _id = new DomainId( id_str );
113         _from = from;
114         _to = to;
115         _number = number;
116         _total_count = total_count;
117         _per_sequence_evalue = per_sequence_evalue;
118         _per_sequence_score = per_sequence_score;
119         _per_domain_evalue = per_domain_evalue;
120         _per_domain_score = per_domain_score;
121     }
122
123     @Override
124     public void addGoId( final GoId go_id ) {
125         getDomainId().getGoIds().add( go_id );
126     }
127
128     /**
129      * Basic domains are compared/sorted based upon their identifiers (case
130      * insensitive) and their numbers.
131      * 
132      */
133     @Override
134     public int compareTo( final Domain domain ) {
135         if ( domain.getClass() != this.getClass() ) {
136             throw new IllegalArgumentException( "attempt to compare [" + domain.getClass() + "] to " + "["
137                     + this.getClass() + "]" );
138         }
139         if ( this == domain ) {
140             return 0;
141         }
142         return getDomainId().compareTo( domain.getDomainId() );
143     }
144
145     /**
146      * Basic domains are considered equal if they have the same identifier (case
147      * sensitive).
148      * 
149      */
150     @Override
151     public boolean equals( final Object o ) {
152         if ( this == o ) {
153             return true;
154         }
155         else if ( o == null ) {
156             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to null" );
157         }
158         else if ( o.getClass() != this.getClass() ) {
159             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
160                     + o.getClass() + "]" );
161         }
162         else {
163             return getDomainId().equals( ( ( Domain ) o ).getDomainId() );
164         }
165     }
166
167     @Override
168     public DomainId getDomainId() {
169         return _id;
170     }
171
172     @Override
173     public int getFrom() {
174         return _from;
175     }
176
177     @Override
178     public GoId getGoId( final int i ) {
179         return getDomainId().getGoIds().get( i );
180     }
181
182     @Override
183     public short getNumber() {
184         return _number;
185     }
186
187     @Override
188     public int getNumberOfGoIds() {
189         return getDomainId().getGoIds().size();
190     }
191
192     @Override
193     public double getPerDomainEvalue() {
194         return _per_domain_evalue;
195     }
196
197     @Override
198     public double getPerDomainScore() {
199         return _per_domain_score;
200     }
201
202     @Override
203     public double getPerSequenceEvalue() {
204         return _per_sequence_evalue;
205     }
206
207     @Override
208     public double getPerSequenceScore() {
209         return _per_sequence_score;
210     }
211
212     @Override
213     public int getTo() {
214         return _to;
215     }
216
217     @Override
218     public short getTotalCount() {
219         return _total_count;
220     }
221
222     @Override
223     public int hashCode() {
224         return getDomainId().getId().hashCode();
225     }
226
227     @Override
228     public String toString() {
229         return toStringBuffer().toString();
230     }
231
232     public StringBuffer toStringBuffer() {
233         return new StringBuffer( getDomainId().getId() );
234     }
235 }