Added getValues and write to DistanceMatrix interface for broader use
[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 PropertiesList _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         final StringBuffer sb = new StringBuffer();
93         if ( !ForesterUtil.isEmpty( getDesc() ) && !ForesterUtil.isEmpty( getRef() ) ) {
94             sb.append( getDesc() );
95             sb.append( " (" );
96             sb.append( getRef() );
97             sb.append( ")" );
98         }
99         else if ( !ForesterUtil.isEmpty( getDesc() ) ) {
100             sb.append( getDesc() );
101         }
102         else if ( !ForesterUtil.isEmpty( getRef() ) ) {
103             sb.append( getRef() );
104         }
105         return sb;
106     }
107
108     @Override
109     public int compareTo( final Annotation o ) {
110         if ( equals( o ) ) {
111             return 0;
112         }
113         if ( getRef().equals( o.getRef() ) ) {
114             return getDesc().compareTo( o.getDesc() );
115         }
116         return getRef().compareTo( o.getRef() );
117     }
118
119     @Override
120     public PhylogenyData copy() {
121         final Annotation ann = new Annotation( getRefSource(), getRefValue() );
122         if ( getConfidence() != null ) {
123             ann.setConfidence( ( Confidence ) getConfidence().copy() );
124         }
125         else {
126             ann.setConfidence( null );
127         }
128         ann.setType( getType() );
129         ann.setDesc( getDesc() );
130         ann.setEvidence( getEvidence() );
131         ann.setSource( new String( getSource() ) );
132         if ( getProperties() != null ) {
133             ann.setProperties( ( PropertiesList ) getProperties().copy() );
134         }
135         else {
136             ann.setProperties( null );
137         }
138         if ( getUris() != null ) {
139             ann.setUris( new ArrayList<Uri>() );
140             for( final Uri uri : getUris() ) {
141                 if ( uri != null ) {
142                     ann.getUris().add( uri );
143                 }
144             }
145         }
146         return ann;
147     }
148
149     @Override
150     public boolean equals( final Object o ) {
151         if ( this == o ) {
152             return true;
153         }
154         else if ( o == null ) {
155             return false;
156         }
157         else if ( o.getClass() != this.getClass() ) {
158             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
159                     + o.getClass() + "]" );
160         }
161         else {
162             return isEqual( ( Annotation ) o );
163         }
164     }
165
166     public Confidence getConfidence() {
167         return _confidence;
168     }
169
170     public String getDesc() {
171         return _desc;
172     }
173
174     public String getEvidence() {
175         return _evidence;
176     }
177
178     public PropertiesList getProperties() {
179         return _properties;
180     }
181
182     public String getRef() {
183         if ( ForesterUtil.isEmpty( _ref_source ) ) {
184             return "";
185         }
186         final StringBuilder sb = new StringBuilder();
187         sb.append( _ref_source );
188         sb.append( ':' );
189         sb.append( _ref_value );
190         return sb.toString();
191     }
192
193     public final String getRefSource() {
194         return _ref_source;
195     }
196
197     public final String getRefValue() {
198         return _ref_value;
199     }
200
201     public String getSource() {
202         return _source;
203     }
204
205     public String getType() {
206         return _type;
207     }
208
209     @Override
210     public Uri getUri( final int index ) {
211         return getUris().get( index );
212     }
213
214     @Override
215     public List<Uri> getUris() {
216         return _uris;
217     }
218
219     @Override
220     public boolean isEqual( final PhylogenyData data ) {
221         final Annotation other = ( Annotation ) data;
222         return getDesc().equalsIgnoreCase( other.getDesc() ) && getType().equals( other.getType() )
223                 && getSource().equals( other.getSource() ) && getRef().equals( other.getRef() );
224     }
225
226     public void setConfidence( final Confidence confidence ) {
227         _confidence = confidence;
228     }
229
230     public void setDesc( final String desc ) {
231         _desc = desc;
232     }
233
234     public void setEvidence( final String evidence ) {
235         _evidence = evidence;
236     }
237
238     public void setProperties( final PropertiesList property ) {
239         _properties = property;
240     }
241
242     // public void setRef( final String ref ) {
243     //     _ref = ref;
244     // }
245     public void setSource( final String source ) {
246         _source = source;
247     }
248
249     public void setType( final String type ) {
250         _type = type;
251     }
252
253     @Override
254     public void setUris( final List<Uri> uris ) {
255         _uris = uris;
256     }
257
258     @Override
259     public StringBuffer toNHX() {
260         throw new UnsupportedOperationException();
261     }
262
263     @Override
264     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
265         if ( ( getConfidence() != null ) || ( getProperties() != null )
266                 || ( ( getUris() != null ) && !getUris().isEmpty() ) || !ForesterUtil.isEmpty( getDesc() ) ) {
267             writer.write( ForesterUtil.LINE_SEPARATOR );
268             writer.write( indentation );
269             PhylogenyDataUtil.appendOpen( writer,
270                                           PhyloXmlMapping.ANNOTATION,
271                                           PhyloXmlMapping.ANNOTATION_REF_ATTR,
272                                           getRef(),
273                                           PhyloXmlMapping.ANNOTATION_EVIDENCE_ATTR,
274                                           getEvidence(),
275                                           PhyloXmlMapping.ANNOTATION_TYPE_ATTR,
276                                           getType(),
277                                           PhyloXmlMapping.ANNOTATION_SOURCE_ATTR,
278                                           getSource() );
279             if ( !ForesterUtil.isEmpty( getDesc() ) ) {
280                 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.ANNOTATION_DESC, getDesc(), indentation );
281             }
282             if ( getConfidence() != null ) {
283                 getConfidence().toPhyloXML( writer, level, indentation + PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
284             }
285             if ( getProperties() != null ) {
286                 getProperties().toPhyloXML( writer, level, indentation );
287             }
288             if ( getUris() != null ) {
289                 for( final Uri uri : getUris() ) {
290                     if ( uri != null ) {
291                         uri.toPhyloXML( writer, level, indentation );
292                     }
293                 }
294             }
295             writer.write( ForesterUtil.LINE_SEPARATOR );
296             writer.write( indentation );
297             PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.ANNOTATION );
298         }
299         else {
300             PhylogenyDataUtil.appendElement( writer,
301                                              PhyloXmlMapping.ANNOTATION,
302                                              PhyloXmlMapping.ANNOTATION_REF_ATTR,
303                                              getRef(),
304                                              PhyloXmlMapping.ANNOTATION_EVIDENCE_ATTR,
305                                              getEvidence(),
306                                              PhyloXmlMapping.ANNOTATION_TYPE_ATTR,
307                                              getType(),
308                                              PhyloXmlMapping.ANNOTATION_SOURCE_ATTR,
309                                              getSource(),
310                                              indentation );
311         }
312     }
313
314     @Override
315     public String toString() {
316         return asText().toString();
317     }
318
319     private void init() {
320         _desc = "";
321         _type = "";
322         _source = "";
323         _evidence = "";
324         _confidence = null;
325         _properties = null;
326         setUris( null );
327     }
328 }