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