inprogress
[jalview.git] / forester / java / src / org / forester / phylogeny / data / Annotation.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.phylogeny.data;
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.io.parsers.phyloxml.PhyloXmlMapping;
34 import org.forester.io.writers.PhylogenyWriter;
35 import org.forester.util.ForesterUtil;
36
37 public class Annotation implements PhylogenyData, MultipleUris, Comparable<Annotation> {
38
39     private Confidence    _confidence;
40     private String        _desc;
41     private String        _evidence;
42     private PropertiesMap _properties;
43     private final String  _ref_source;
44     private final String  _ref_value;
45     private String        _source;
46     private String        _type;
47     private List<Uri>     _uris;
48
49     public Annotation() {
50         _ref_value = "";
51         _ref_source = "";
52         init();
53     }
54
55     public Annotation( final String ref ) {
56         if ( ForesterUtil.isEmpty( ref ) ) {
57             throw new IllegalArgumentException( "annotation reference is empty or null" );
58         }
59         final String s[] = ref.split( ":" );
60         if ( ( s.length != 2 ) || ForesterUtil.isEmpty( s[ 0 ] ) || ForesterUtil.isEmpty( s[ 1 ] ) ) {
61             throw new IllegalArgumentException( "illegal format for annotation reference: [" + ref + "]" );
62         }
63         _ref_source = s[ 0 ];
64         _ref_value = s[ 1 ];
65         init();
66     }
67
68     public Annotation( final String ref_source, final String ref_value ) {
69         if ( ForesterUtil.isEmpty( ref_source ) || ForesterUtil.isEmpty( ref_value ) ) {
70             throw new IllegalArgumentException( "illegal format for annotation reference" );
71         }
72         _ref_source = ref_source;
73         _ref_value = ref_value;
74         init();
75     }
76
77     @Override
78     public void addUri( final Uri uri ) {
79         if ( getUris() == null ) {
80             setUris( new ArrayList<Uri>() );
81         }
82         getUris().add( uri );
83     }
84
85     @Override
86     public StringBuffer asSimpleText() {
87         return new StringBuffer( !ForesterUtil.isEmpty( getRef() ) ? getRef() : getDesc() );
88     }
89
90     @Override
91     public StringBuffer asText() {
92         return asSimpleText();
93     }
94
95     @Override
96     public int compareTo( final Annotation o ) {
97         if ( equals( o ) ) {
98             return 0;
99         }
100         if ( getRef().equals( o.getRef() ) ) {
101             return getDesc().compareTo( o.getDesc() );
102         }
103         return getRef().compareTo( o.getRef() );
104     }
105
106     @Override
107     public PhylogenyData copy() {
108         final Annotation ann = new Annotation( getRefSource(), getRefValue() );
109         if ( getConfidence() != null ) {
110             ann.setConfidence( ( Confidence ) getConfidence().copy() );
111         }
112         else {
113             ann.setConfidence( null );
114         }
115         ann.setType( getType() );
116         ann.setDesc( getDesc() );
117         ann.setEvidence( getEvidence() );
118         ann.setSource( new String( getSource() ) );
119         if ( getProperties() != null ) {
120             ann.setProperties( ( PropertiesMap ) getProperties().copy() );
121         }
122         else {
123             ann.setProperties( null );
124         }
125         if ( getUris() != null ) {
126             ann.setUris( new ArrayList<Uri>() );
127             for( final Uri uri : getUris() ) {
128                 if ( uri != null ) {
129                     ann.getUris().add( uri );
130                 }
131             }
132         }
133         return ann;
134     }
135
136     @Override
137     public boolean equals( final Object o ) {
138         if ( this == o ) {
139             return true;
140         }
141         else if ( o == null ) {
142             return false;
143         }
144         else if ( o.getClass() != this.getClass() ) {
145             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
146                     + o.getClass() + "]" );
147         }
148         else {
149             return isEqual( ( Annotation ) o );
150         }
151     }
152
153     public Confidence getConfidence() {
154         return _confidence;
155     }
156
157     public String getDesc() {
158         return _desc;
159     }
160
161     public String getEvidence() {
162         return _evidence;
163     }
164
165     public PropertiesMap getProperties() {
166         return _properties;
167     }
168
169     public String getRef() {
170         if ( ForesterUtil.isEmpty( _ref_source ) ) {
171             return "";
172         }
173         final StringBuilder sb = new StringBuilder();
174         sb.append( _ref_source );
175         sb.append( ':' );
176         sb.append( _ref_value );
177         return sb.toString();
178     }
179
180     public final String getRefSource() {
181         return _ref_source;
182     }
183
184     public final String getRefValue() {
185         return _ref_value;
186     }
187
188     public String getSource() {
189         return _source;
190     }
191
192     public String getType() {
193         return _type;
194     }
195
196     @Override
197     public Uri getUri( final int index ) {
198         return getUris().get( index );
199     }
200
201     @Override
202     public List<Uri> getUris() {
203         return _uris;
204     }
205
206     @Override
207     public boolean isEqual( final PhylogenyData data ) {
208         final Annotation other = ( Annotation ) data;
209         return getDesc().equalsIgnoreCase( other.getDesc() ) && getType().equals( other.getType() )
210                 && getSource().equals( other.getSource() ) && getRef().equals( other.getRef() );
211     }
212
213     public void setConfidence( final Confidence confidence ) {
214         _confidence = confidence;
215     }
216
217     public void setDesc( final String desc ) {
218         _desc = desc;
219     }
220
221     public void setEvidence( final String evidence ) {
222         _evidence = evidence;
223     }
224
225     public void setProperties( final PropertiesMap property ) {
226         _properties = property;
227     }
228
229     // public void setRef( final String ref ) {
230     //     _ref = ref;
231     // }
232     public void setSource( final String source ) {
233         _source = source;
234     }
235
236     public void setType( final String type ) {
237         _type = type;
238     }
239
240     @Override
241     public void setUris( final List<Uri> uris ) {
242         _uris = uris;
243     }
244
245     @Override
246     public StringBuffer toNHX() {
247         throw new UnsupportedOperationException();
248     }
249
250     @Override
251     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
252         if ( ( getConfidence() != null ) || ( getProperties() != null )
253                 || ( ( getUris() != null ) && !getUris().isEmpty() ) || !ForesterUtil.isEmpty( getDesc() ) ) {
254             writer.write( ForesterUtil.LINE_SEPARATOR );
255             writer.write( indentation );
256             PhylogenyDataUtil.appendOpen( writer,
257                                           PhyloXmlMapping.ANNOTATION,
258                                           PhyloXmlMapping.ANNOTATION_REF_ATTR,
259                                           getRef(),
260                                           PhyloXmlMapping.ANNOTATION_EVIDENCE_ATTR,
261                                           getEvidence(),
262                                           PhyloXmlMapping.ANNOTATION_TYPE_ATTR,
263                                           getType(),
264                                           PhyloXmlMapping.ANNOTATION_SOURCE_ATTR,
265                                           getSource() );
266             if ( !ForesterUtil.isEmpty( getDesc() ) ) {
267                 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.ANNOTATION_DESC, getDesc(), indentation );
268             }
269             if ( getConfidence() != null ) {
270                 getConfidence().toPhyloXML( writer, level, indentation + PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
271             }
272             if ( getProperties() != null ) {
273                 getProperties().toPhyloXML( writer, level, indentation );
274             }
275             if ( getUris() != null ) {
276                 for( final Uri uri : getUris() ) {
277                     if ( uri != null ) {
278                         uri.toPhyloXML( writer, level, indentation );
279                     }
280                 }
281             }
282             writer.write( ForesterUtil.LINE_SEPARATOR );
283             writer.write( indentation );
284             PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.ANNOTATION );
285         }
286         else {
287             PhylogenyDataUtil.appendElement( writer,
288                                              PhyloXmlMapping.ANNOTATION,
289                                              PhyloXmlMapping.ANNOTATION_REF_ATTR,
290                                              getRef(),
291                                              PhyloXmlMapping.ANNOTATION_EVIDENCE_ATTR,
292                                              getEvidence(),
293                                              PhyloXmlMapping.ANNOTATION_TYPE_ATTR,
294                                              getType(),
295                                              PhyloXmlMapping.ANNOTATION_SOURCE_ATTR,
296                                              getSource(),
297                                              indentation );
298         }
299     }
300
301     @Override
302     public String toString() {
303         return asText().toString();
304     }
305
306     private void init() {
307         _desc = "";
308         _type = "";
309         _source = "";
310         _evidence = "";
311         _confidence = null;
312         _properties = null;
313         setUris( null );
314     }
315 }