in progress (special coloring is still true)
[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: https://sites.google.com/site/cmzmasek/home/software/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     @Override
73     public StringBuffer asSimpleText() {
74         return new StringBuffer( getGoId().toString() );
75     }
76
77     @Override
78     public StringBuffer asText() {
79         return new StringBuffer( toString() );
80     }
81
82     /**
83      * Compares based on GO id.
84      * 
85      */
86     @Override
87     public int compareTo( final GoTerm go_term ) {
88         return getGoId().compareTo( go_term.getGoId() );
89     }
90
91     /**
92      * Makes a shallow copy.
93      * 
94      * 
95      */
96     @Override
97     public PhylogenyData copy() {
98         final BasicGoTerm gt = new BasicGoTerm( getGoId(), getName(), getGoNameSpace(), isObsolete() );
99         gt.setGoXrefs( getGoXRefs() );
100         gt.setGoSubsets( getGoSubsets() );
101         gt.setSuperTerms( getSuperGoIds() );
102         gt.setAltIds( getAltIds() );
103         gt.setDefinition( getDefinition() );
104         return gt;
105     }
106
107     /**
108      * Return true if both GO id and namespace are equal.
109      * 
110      */
111     @Override
112     public boolean equals( final Object o ) {
113         if ( this == o ) {
114             return true;
115         }
116         else if ( o == null ) {
117             throw new IllegalArgumentException( "attempt to check go term equality to null" );
118         }
119         else if ( o.getClass() != this.getClass() ) {
120             throw new IllegalArgumentException( "attempt to check go term equality to " + o + " [" + o.getClass() + "]" );
121         }
122         else {
123             final GoTerm gt = ( GoTerm ) o;
124             return getGoNameSpace().equals( gt.getGoNameSpace() ) && getGoId().equals( gt.getGoId() );
125         }
126     }
127
128     @Override
129     public List<GoId> getAltIds() {
130         return _alt_ids;
131     }
132
133     @Override
134     public String getComment() {
135         return _comment;
136     }
137
138     @Override
139     public String getDefinition() {
140         return _definition;
141     }
142
143     @Override
144     public GoId getGoId() {
145         return _id;
146     }
147
148     @Override
149     public GoNameSpace getGoNameSpace() {
150         return _namespace;
151     }
152
153     @Override
154     public List<GoRelationship> getGoRelationships() {
155         return _go_relationships;
156     }
157
158     @Override
159     public List<GoSubset> getGoSubsets() {
160         return _go_subsets;
161     }
162
163     @Override
164     public List<GoXRef> getGoXRefs() {
165         return _go_xrefs;
166     }
167
168     @Override
169     public String getName() {
170         return _name;
171     }
172
173     @Override
174     public List<GoId> getSuperGoIds() {
175         return _super_go_ids;
176     }
177
178     /**
179      * Hashcode is based on hashcode of GO id.
180      * 
181      * 
182      */
183     @Override
184     public int hashCode() {
185         return getGoId().hashCode();
186     }
187
188     private void init() {
189         setGoXrefs( new ArrayList<GoXRef>() );
190         setSuperTerms( new ArrayList<GoId>() );
191         setAltIds( new ArrayList<GoId>() );
192         setGoRelationships( new ArrayList<GoRelationship>() );
193         setGoSubsets( new ArrayList<GoSubset>() );
194         setDefinition( "" );
195         setComment( "" );
196     }
197
198     @Override
199     public boolean isEqual( final PhylogenyData go_term ) {
200         return equals( go_term );
201     }
202
203     @Override
204     public boolean isObsolete() {
205         return _is_obsolete;
206     }
207
208     private void setAltIds( final List<GoId> alt_ids ) {
209         _alt_ids = alt_ids;
210     }
211
212     public void setComment( final String comment ) {
213         _comment = comment;
214     }
215
216     public void setDefinition( final String definition ) {
217         _definition = definition;
218     }
219
220     private void setGoRelationships( final List<GoRelationship> go_relationships ) {
221         _go_relationships = go_relationships;
222     }
223
224     public void setGoSubsets( final List<GoSubset> go_subsets ) {
225         _go_subsets = go_subsets;
226     }
227
228     private void setGoXrefs( final List<GoXRef> xrefs ) {
229         _go_xrefs = xrefs;
230     }
231
232     private void setSuperTerms( final List<GoId> super_terms ) {
233         _super_go_ids = super_terms;
234     }
235
236     @Override
237     public StringBuffer toNHX() {
238         throw new UnsupportedOperationException();
239     }
240
241     @Override
242     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
243         throw new UnsupportedOperationException();
244     }
245
246     @Override
247     public String toString() {
248         final StringBuffer sb = new StringBuffer();
249         sb.append( getGoId() );
250         sb.append( ": " );
251         sb.append( getName() );
252         sb.append( " [" );
253         sb.append( getGoNameSpace() );
254         sb.append( "]" );
255         if ( isObsolete() ) {
256             sb.append( " [is obsolete]" );
257         }
258         return sb.toString();
259     }
260 }