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