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