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