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 return asSimpleText();
96 public int compareTo( final Annotation o ) {
100 if ( getRef().equals( o.getRef() ) ) {
101 return getDesc().compareTo( o.getDesc() );
103 return getRef().compareTo( o.getRef() );
107 public PhylogenyData copy() {
108 final Annotation ann = new Annotation( getRefSource(), getRefValue() );
109 if ( getConfidence() != null ) {
110 ann.setConfidence( ( Confidence ) getConfidence().copy() );
113 ann.setConfidence( null );
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() );
123 ann.setProperties( null );
125 if ( getUris() != null ) {
126 ann.setUris( new ArrayList<Uri>() );
127 for( final Uri uri : getUris() ) {
129 ann.getUris().add( uri );
137 public boolean equals( final Object o ) {
141 else if ( o == null ) {
144 else if ( o.getClass() != this.getClass() ) {
145 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
146 + o.getClass() + "]" );
149 return isEqual( ( Annotation ) o );
153 public Confidence getConfidence() {
157 public String getDesc() {
161 public String getEvidence() {
165 public PropertiesMap getProperties() {
169 public String getRef() {
170 if ( ForesterUtil.isEmpty( _ref_source ) ) {
173 final StringBuilder sb = new StringBuilder();
174 sb.append( _ref_source );
176 sb.append( _ref_value );
177 return sb.toString();
180 public final String getRefSource() {
184 public final String getRefValue() {
188 public String getSource() {
192 public String getType() {
197 public Uri getUri( final int index ) {
198 return getUris().get( index );
202 public List<Uri> getUris() {
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() );
213 public void setConfidence( final Confidence confidence ) {
214 _confidence = confidence;
217 public void setDesc( final String desc ) {
221 public void setEvidence( final String evidence ) {
222 _evidence = evidence;
225 public void setProperties( final PropertiesMap property ) {
226 _properties = property;
229 // public void setRef( final String ref ) {
232 public void setSource( final String source ) {
236 public void setType( final String type ) {
241 public void setUris( final List<Uri> uris ) {
246 public StringBuffer toNHX() {
247 throw new UnsupportedOperationException();
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,
260 PhyloXmlMapping.ANNOTATION_EVIDENCE_ATTR,
262 PhyloXmlMapping.ANNOTATION_TYPE_ATTR,
264 PhyloXmlMapping.ANNOTATION_SOURCE_ATTR,
266 if ( !ForesterUtil.isEmpty( getDesc() ) ) {
267 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.ANNOTATION_DESC, getDesc(), indentation );
269 if ( getConfidence() != null ) {
270 getConfidence().toPhyloXML( writer, level, indentation + PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
272 if ( getProperties() != null ) {
273 getProperties().toPhyloXML( writer, level, indentation );
275 if ( getUris() != null ) {
276 for( final Uri uri : getUris() ) {
278 uri.toPhyloXML( writer, level, indentation );
282 writer.write( ForesterUtil.LINE_SEPARATOR );
283 writer.write( indentation );
284 PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.ANNOTATION );
287 PhylogenyDataUtil.appendElement( writer,
288 PhyloXmlMapping.ANNOTATION,
289 PhyloXmlMapping.ANNOTATION_REF_ATTR,
291 PhyloXmlMapping.ANNOTATION_EVIDENCE_ATTR,
293 PhyloXmlMapping.ANNOTATION_TYPE_ATTR,
295 PhyloXmlMapping.ANNOTATION_SOURCE_ATTR,
302 public String toString() {
303 return asText().toString();
306 private void init() {