initial commit
[jalview.git] / forester / java / src / org / forester / go / BasicGoTerm.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 // 
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 // 
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.go;
27
28 import java.io.IOException;
29 import java.io.Writer;
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import org.forester.phylogeny.data.PhylogenyData;
34 import org.forester.util.ForesterUtil;
35
36 public class BasicGoTerm implements GoTerm {
37
38     private final GoId           _id;
39     private final String         _name;
40     private final boolean        _is_obsolete;
41     private final GoNameSpace    _namespace;
42     private String               _definition;
43     private List<GoId>           _alt_ids;
44     private List<GoId>           _super_go_ids;
45     private List<GoXRef>         _go_xrefs;
46     private List<GoSubset>       _go_subsets;
47     private String               _comment;
48     private List<GoRelationship> _go_relationships;
49
50     public BasicGoTerm( final GoId id, final String name, final GoNameSpace namespace, final boolean is_obsolete ) {
51         if ( ( id == null ) || ForesterUtil.isEmpty( name ) || ( namespace == null ) ) {
52             throw new IllegalArgumentException( "attempt to create GO term with empty id, name, or namespace" );
53         }
54         _id = id;
55         _name = name;
56         _namespace = namespace;
57         _is_obsolete = is_obsolete;
58         init();
59     }
60
61     public BasicGoTerm( final String id, final String name, final String namespace, final boolean is_obsolete ) {
62         if ( ForesterUtil.isEmpty( id ) || ForesterUtil.isEmpty( name ) || ForesterUtil.isEmpty( namespace ) ) {
63             throw new IllegalArgumentException( "attempt to create GO term with empty id, name, or namespace" );
64         }
65         _id = new GoId( id );
66         _name = name;
67         _namespace = new GoNameSpace( namespace );
68         _is_obsolete = is_obsolete;
69         init();
70     }
71
72     public StringBuffer asSimpleText() {
73         return new StringBuffer( getGoId().toString() );
74     }
75
76     public StringBuffer asText() {
77         return new StringBuffer( toString() );
78     }
79
80     /**
81      * Compares based on GO id.
82      * 
83      */
84     public int compareTo( final GoTerm go_term ) {
85         return getGoId().compareTo( go_term.getGoId() );
86     }
87
88     /**
89      * Makes a shallow copy.
90      * 
91      * 
92      */
93     public PhylogenyData copy() {
94         final BasicGoTerm gt = new BasicGoTerm( getGoId(), getName(), getGoNameSpace(), isObsolete() );
95         gt.setGoXrefs( getGoXRefs() );
96         gt.setGoSubsets( getGoSubsets() );
97         gt.setSuperTerms( getSuperGoIds() );
98         gt.setAltIds( getAltIds() );
99         gt.setDefinition( getDefinition() );
100         return gt;
101     }
102
103     /**
104      * Return true if both GO id and namespace are equal.
105      * 
106      */
107     @Override
108     public boolean equals( final Object o ) {
109         if ( this == o ) {
110             return true;
111         }
112         else if ( o == null ) {
113             throw new IllegalArgumentException( "attempt to check go term equality to null" );
114         }
115         else if ( o.getClass() != this.getClass() ) {
116             throw new IllegalArgumentException( "attempt to check go term equality to " + o + " [" + o.getClass() + "]" );
117         }
118         else {
119             final GoTerm gt = ( GoTerm ) o;
120             return getGoNameSpace().equals( gt.getGoNameSpace() ) && getGoId().equals( gt.getGoId() );
121         }
122     }
123
124     public List<GoId> getAltIds() {
125         return _alt_ids;
126     }
127
128     @Override
129     public String getComment() {
130         return _comment;
131     }
132
133     @Override
134     public String getDefinition() {
135         return _definition;
136     }
137
138     public GoId getGoId() {
139         return _id;
140     }
141
142     public GoNameSpace getGoNameSpace() {
143         return _namespace;
144     }
145
146     @Override
147     public List<GoRelationship> getGoRelationships() {
148         return _go_relationships;
149     }
150
151     @Override
152     public List<GoSubset> getGoSubsets() {
153         return _go_subsets;
154     }
155
156     public List<GoXRef> getGoXRefs() {
157         return _go_xrefs;
158     }
159
160     public String getName() {
161         return _name;
162     }
163
164     public List<GoId> getSuperGoIds() {
165         return _super_go_ids;
166     }
167
168     /**
169      * Hashcode is based on hashcode of GO id.
170      * 
171      * 
172      */
173     @Override
174     public int hashCode() {
175         return getGoId().hashCode();
176     }
177
178     private void init() {
179         setGoXrefs( new ArrayList<GoXRef>() );
180         setSuperTerms( new ArrayList<GoId>() );
181         setAltIds( new ArrayList<GoId>() );
182         setGoRelationships( new ArrayList<GoRelationship>() );
183         setGoSubsets( new ArrayList<GoSubset>() );
184         setDefinition( "" );
185         setComment( "" );
186     }
187
188     public boolean isEqual( final PhylogenyData go_term ) {
189         return equals( go_term );
190     }
191
192     public boolean isObsolete() {
193         return _is_obsolete;
194     }
195
196     private void setAltIds( final List<GoId> alt_ids ) {
197         _alt_ids = alt_ids;
198     }
199
200     public void setComment( final String comment ) {
201         _comment = comment;
202     }
203
204     public void setDefinition( final String definition ) {
205         _definition = definition;
206     }
207
208     private void setGoRelationships( final List<GoRelationship> go_relationships ) {
209         _go_relationships = go_relationships;
210     }
211
212     public void setGoSubsets( final List<GoSubset> go_subsets ) {
213         _go_subsets = go_subsets;
214     }
215
216     private void setGoXrefs( final List<GoXRef> xrefs ) {
217         _go_xrefs = xrefs;
218     }
219
220     private void setSuperTerms( final List<GoId> super_terms ) {
221         _super_go_ids = super_terms;
222     }
223
224     public StringBuffer toNHX() {
225         throw new UnsupportedOperationException();
226     }
227
228     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
229         throw new UnsupportedOperationException();
230     }
231
232     @Override
233     public String toString() {
234         final StringBuffer sb = new StringBuffer();
235         sb.append( getGoId() );
236         sb.append( ": " );
237         sb.append( getName() );
238         sb.append( " [" );
239         sb.append( getGoNameSpace() );
240         sb.append( "]" );
241         if ( isObsolete() ) {
242             sb.append( " [is obsolete]" );
243         }
244         return sb.toString();
245     }
246 }