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;
47 public BasicDomain( final String id ) {
48 if ( ForesterUtil.isEmpty( id ) ) {
49 throw new IllegalArgumentException( "attempt to create protein domain with null or empty id" );
51 _id = obtainIdAsShort( id );
56 _per_domain_evalue = -1;
57 _per_domain_score = -1;
60 public BasicDomain( final String id,
64 final short total_count,
65 final double per_domain_evalue,
66 final double per_domain_score ) {
67 if ( ( from >= to ) || ( from < 0 ) ) {
68 throw new IllegalArgumentException( "attempt to create protein domain from " + from + " to " + to );
70 if ( ForesterUtil.isEmpty( id ) ) {
71 throw new IllegalArgumentException( "attempt to create protein domain with null or empty id" );
73 if ( ( number > total_count ) || ( number < 0 ) ) {
74 throw new IllegalArgumentException( "attempt to create protein domain number " + number + " out of "
77 if ( per_domain_evalue < 0.0 ) {
78 throw new IllegalArgumentException( "attempt to create protein domain with negative E-value" );
80 _id = obtainIdAsShort( id );
84 _total_count = total_count;
85 _per_domain_evalue = per_domain_evalue;
86 _per_domain_score = per_domain_score;
90 * Basic domains are compared/sorted based upon their identifiers (case
91 * insensitive) and their numbers.
95 public int compareTo( final Domain domain ) {
96 if ( domain.getClass() != this.getClass() ) {
97 throw new IllegalArgumentException( "attempt to compare [" + domain.getClass() + "] to " + "["
98 + this.getClass() + "]" );
100 if ( this == domain ) {
103 return getDomainId().compareTo( domain.getDomainId() );
107 * Basic domains are considered equal if they have the same identifier (case
112 public boolean equals( final Object o ) {
116 else if ( o == null ) {
117 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to null" );
119 else if ( o.getClass() != this.getClass() ) {
120 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
121 + o.getClass() + "]" );
124 return getDomainId().equals( ( ( Domain ) o ).getDomainId() );
129 public String getDomainId() {
130 return obtainIdFromShort( _id );
134 public int getFrom() {
139 public int getLength() {
140 return ( 1 + getTo() ) - getFrom();
144 public short getNumber() {
149 public double getPerDomainEvalue() {
150 return _per_domain_evalue;
154 public double getPerDomainScore() {
155 return _per_domain_score;
164 public short getTotalCount() {
169 public int hashCode() {
170 return getDomainId().hashCode();
174 public String toString() {
175 return getDomainId();
178 public StringBuffer toStringBuffer() {
179 return new StringBuffer( getDomainId() );
182 public final static short obtainIdAsShort( final String id ) {
183 if ( !STRING_TO_ID.containsKey( id ) ) {
184 if ( COUNT >= ( Short.MAX_VALUE - 2 ) ) {
185 throw new RuntimeException( "too many domain ids!" );
187 ID_TO_STRING.put( COUNT, id );
188 STRING_TO_ID.put( id, COUNT );
191 return STRING_TO_ID.get( id );
194 public final static String obtainIdFromShort( final short id ) {
195 return ID_TO_STRING.get( id );