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