2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
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.
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.
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
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.phylogeny.data;
28 import java.io.IOException;
29 import java.io.Writer;
30 import java.util.ArrayList;
31 import java.util.List;
33 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
34 import org.forester.io.writers.PhylogenyWriter;
35 import org.forester.util.ForesterUtil;
37 public class Annotation implements PhylogenyData, MultipleUris, Comparable<Annotation> {
39 private Confidence _confidence;
41 private String _evidence;
42 private PropertiesMap _properties;
43 private final String _ref_source;
44 private final String _ref_value;
45 private String _source;
47 private List<Uri> _uris;
55 public Annotation( final String ref ) {
56 if ( ForesterUtil.isEmpty( ref ) ) {
57 throw new IllegalArgumentException( "annotation reference is empty or null" );
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 + "]" );
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" );
72 _ref_source = ref_source;
73 _ref_value = ref_value;
78 public void addUri( final Uri uri ) {
79 if ( getUris() == null ) {
80 setUris( new ArrayList<Uri>() );
86 public StringBuffer asSimpleText() {
87 return new StringBuffer( !ForesterUtil.isEmpty( getRef() ) ? getRef() : getDesc() );
91 public StringBuffer asText() {
92 final StringBuffer sb = new StringBuffer();
93 if ( !ForesterUtil.isEmpty( getDesc() ) && !ForesterUtil.isEmpty( getRef() ) ) {
94 sb.append( getDesc() );
96 sb.append( getRef() );
99 else if ( !ForesterUtil.isEmpty( getDesc() ) ) {
100 sb.append( getDesc() );
102 else if ( !ForesterUtil.isEmpty( getRef() ) ) {
103 sb.append( getRef() );
109 public int compareTo( final Annotation o ) {
113 if ( getRef().equals( o.getRef() ) ) {
114 return getDesc().compareTo( o.getDesc() );
116 return getRef().compareTo( o.getRef() );
120 public PhylogenyData copy() {
121 final Annotation ann = new Annotation( getRefSource(), getRefValue() );
122 if ( getConfidence() != null ) {
123 ann.setConfidence( ( Confidence ) getConfidence().copy() );
126 ann.setConfidence( null );
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( ( PropertiesMap ) getProperties().copy() );
136 ann.setProperties( null );
138 if ( getUris() != null ) {
139 ann.setUris( new ArrayList<Uri>() );
140 for( final Uri uri : getUris() ) {
142 ann.getUris().add( uri );
150 public boolean equals( final Object o ) {
154 else if ( o == null ) {
157 else if ( o.getClass() != this.getClass() ) {
158 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
159 + o.getClass() + "]" );
162 return isEqual( ( Annotation ) o );
166 public Confidence getConfidence() {
170 public String getDesc() {
174 public String getEvidence() {
178 public PropertiesMap getProperties() {
182 public String getRef() {
183 if ( ForesterUtil.isEmpty( _ref_source ) ) {
186 final StringBuilder sb = new StringBuilder();
187 sb.append( _ref_source );
189 sb.append( _ref_value );
190 return sb.toString();
193 public final String getRefSource() {
197 public final String getRefValue() {
201 public String getSource() {
205 public String getType() {
210 public Uri getUri( final int index ) {
211 return getUris().get( index );
215 public List<Uri> getUris() {
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() );
226 public void setConfidence( final Confidence confidence ) {
227 _confidence = confidence;
230 public void setDesc( final String desc ) {
234 public void setEvidence( final String evidence ) {
235 _evidence = evidence;
238 public void setProperties( final PropertiesMap property ) {
239 _properties = property;
242 // public void setRef( final String ref ) {
245 public void setSource( final String source ) {
249 public void setType( final String type ) {
254 public void setUris( final List<Uri> uris ) {
259 public StringBuffer toNHX() {
260 throw new UnsupportedOperationException();
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,
273 PhyloXmlMapping.ANNOTATION_EVIDENCE_ATTR,
275 PhyloXmlMapping.ANNOTATION_TYPE_ATTR,
277 PhyloXmlMapping.ANNOTATION_SOURCE_ATTR,
279 if ( !ForesterUtil.isEmpty( getDesc() ) ) {
280 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.ANNOTATION_DESC, getDesc(), indentation );
282 if ( getConfidence() != null ) {
283 getConfidence().toPhyloXML( writer, level, indentation + PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
285 if ( getProperties() != null ) {
286 getProperties().toPhyloXML( writer, level, indentation );
288 if ( getUris() != null ) {
289 for( final Uri uri : getUris() ) {
291 uri.toPhyloXML( writer, level, indentation );
295 writer.write( ForesterUtil.LINE_SEPARATOR );
296 writer.write( indentation );
297 PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.ANNOTATION );
300 PhylogenyDataUtil.appendElement( writer,
301 PhyloXmlMapping.ANNOTATION,
302 PhyloXmlMapping.ANNOTATION_REF_ATTR,
304 PhyloXmlMapping.ANNOTATION_EVIDENCE_ATTR,
306 PhyloXmlMapping.ANNOTATION_TYPE_ATTR,
308 PhyloXmlMapping.ANNOTATION_SOURCE_ATTR,
315 public String toString() {
316 return asText().toString();
319 private void init() {