3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
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.
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.
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
24 // Contact: phylosoft @ gmail . com
25 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
27 package org.forester.protein;
29 import java.util.HashMap;
32 import org.forester.util.ForesterUtil;
34 public class BasicDomain implements Domain {
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;
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" );
54 _id = obtainIdAsShort( id );
59 _per_domain_evalue = -1;
60 _per_domain_score = -1;
67 public BasicDomain( final String id,
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 );
77 if ( ForesterUtil.isEmpty( id ) ) {
78 throw new IllegalArgumentException( "attempt to create protein domain with null or empty id" );
80 if ( ( number > total_count ) || ( number < 0 ) ) {
81 throw new IllegalArgumentException( "attempt to create protein domain number " + number + " out of "
84 if ( per_domain_evalue < 0.0 ) {
85 throw new IllegalArgumentException( "attempt to create protein domain with negative E-value" );
87 _id = obtainIdAsShort( id );
91 _total_count = total_count;
92 _per_domain_evalue = per_domain_evalue;
93 _per_domain_score = per_domain_score;
99 public BasicDomain( final String id,
103 final short total_count,
104 final double per_domain_evalue,
105 final double per_domain_score,
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 );
112 if ( ForesterUtil.isEmpty( id ) ) {
113 throw new IllegalArgumentException( "attempt to create protein domain with null or empty id" );
115 if ( ( number > total_count ) || ( number < 0 ) ) {
116 throw new IllegalArgumentException( "attempt to create protein domain number " + number + " out of "
119 if ( per_domain_evalue < 0.0 ) {
120 throw new IllegalArgumentException( "attempt to create protein domain with negative E-value" );
122 if ( ( hmm_from >= hmm_to ) || ( hmm_from < 0 ) ) {
123 throw new IllegalArgumentException( "attempt to create protein domain matching hmm from " + from + " to " + to );
125 if ( hmm_len <= 0 ) {
126 throw new IllegalArgumentException( "attempt to create protein domain with zero or negative hmm length" );
128 _id = obtainIdAsShort( id );
132 _total_count = total_count;
133 _per_domain_evalue = per_domain_evalue;
134 _per_domain_score = per_domain_score;
141 * Basic domains are compared/sorted based upon their identifiers (case
142 * insensitive) and their numbers.
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() + "]" );
151 if ( this == domain ) {
154 return getDomainId().compareTo( domain.getDomainId() );
158 * Basic domains are considered equal if they have the same identifier (case
163 public boolean equals( final Object o ) {
167 else if ( o == null ) {
168 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to null" );
170 else if ( o.getClass() != this.getClass() ) {
171 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
172 + o.getClass() + "]" );
175 return getDomainId().equals( ( ( Domain ) o ).getDomainId() );
180 public String getDomainId() {
181 return obtainIdFromShort( _id );
185 public int getFrom() {
190 public int getLength() {
191 return ( 1 + getTo() ) - getFrom();
195 public short getNumber() {
200 public double getPerDomainEvalue() {
201 return _per_domain_evalue;
205 public double getPerDomainScore() {
206 return _per_domain_score;
215 public short getTotalCount() {
220 public final short getHmmLen() {
225 public final short getHmmFrom() {
230 public final short getHmmTo() {
235 public int hashCode() {
236 return getDomainId().hashCode();
240 public String toString() {
241 return getDomainId();
244 public StringBuffer toStringBuffer() {
245 return new StringBuffer( getDomainId() );
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!" );
253 ID_TO_STRING.put( COUNT, id );
254 STRING_TO_ID.put( id, COUNT );
257 return STRING_TO_ID.get( id );
260 public final static String obtainIdFromShort( final short id ) {
261 return ID_TO_STRING.get( id );