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: www.phylosoft.org/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> {
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;
48 public Annotation( final String ref ) {
49 if ( ForesterUtil.isEmpty( ref ) ) {
50 throw new IllegalArgumentException( "annotation reference is empty or null" );
52 if ( ( ref.indexOf( ':' ) < 1 ) || ( ref.indexOf( ':' ) > ref.length() - 2 ) || ( ref.length() < 3 ) ) {
53 throw new IllegalArgumentException( "illegal format for annotation reference: [" + ref + "]" );
65 public StringBuffer asSimpleText() {
66 return new StringBuffer( !ForesterUtil.isEmpty( getRef() ) ? getRef() : getDesc() );
70 public StringBuffer asText() {
71 return asSimpleText();
75 public PhylogenyData copy() {
76 final Annotation ann = new Annotation( new String( getRef() ) );
77 if ( getConfidence() != null ) {
78 ann.setConfidence( ( Confidence ) getConfidence().copy() );
81 ann.setConfidence( null );
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() );
91 ann.setProperties( null );
93 if ( getUris() != null ) {
94 ann.setUris( new ArrayList<Uri>() );
95 for( final Uri uri : getUris() ) {
97 ann.getUris().add( uri );
104 public Confidence getConfidence() {
108 public String getDesc() {
112 public String getEvidence() {
116 public PropertiesMap getProperties() {
120 public String getRef() {
124 public String getSource() {
128 public String getType() {
132 private void init() {
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() );
149 public void setConfidence( final Confidence confidence ) {
150 _confidence = confidence;
153 public void setDesc( final String desc ) {
157 public void setEvidence( final String evidence ) {
158 _evidence = evidence;
161 public void setProperties( final PropertiesMap property ) {
162 _properties = property;
165 // public void setRef( final String ref ) {
168 public void setSource( final String source ) {
172 public void setType( final String type ) {
177 public StringBuffer toNHX() {
178 throw new UnsupportedOperationException();
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,
191 PhyloXmlMapping.ANNOTATION_EVIDENCE_ATTR,
193 PhyloXmlMapping.ANNOTATION_TYPE_ATTR,
195 PhyloXmlMapping.ANNOTATION_SOURCE_ATTR,
197 if ( !ForesterUtil.isEmpty( getDesc() ) ) {
198 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.ANNOTATION_DESC, getDesc(), indentation );
200 if ( getConfidence() != null ) {
201 getConfidence().toPhyloXML( writer, level, indentation + PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
203 if ( getProperties() != null ) {
204 getProperties().toPhyloXML( writer, level, indentation );
206 if ( getUris() != null ) {
207 for( final Uri uri : getUris() ) {
209 uri.toPhyloXML( writer, level, indentation );
213 writer.write( ForesterUtil.LINE_SEPARATOR );
214 writer.write( indentation );
215 PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.ANNOTATION );
218 PhylogenyDataUtil.appendElement( writer,
219 PhyloXmlMapping.ANNOTATION,
220 PhyloXmlMapping.ANNOTATION_REF_ATTR,
222 PhyloXmlMapping.ANNOTATION_EVIDENCE_ATTR,
224 PhyloXmlMapping.ANNOTATION_TYPE_ATTR,
226 PhyloXmlMapping.ANNOTATION_SOURCE_ATTR,
233 public String toString() {
234 return asText().toString();
238 public void addUri( final Uri uri ) {
239 if ( getUris() == null ) {
240 setUris( new ArrayList<Uri>() );
242 getUris().add( uri );
246 public Uri getUri( final int index ) {
247 return getUris().get( index );
251 public List<Uri> getUris() {
256 public void setUris( final List<Uri> uris ) {
261 public boolean equals( final Object o ) {
265 else if ( o == null ) {
268 else if ( o.getClass() != this.getClass() ) {
269 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
270 + o.getClass() + "]" );
273 return isEqual( ( Annotation ) o );
278 public int compareTo( final Annotation o ) {
282 if ( getRef().equals( o.getRef() ) ) {
283 return getDesc().compareTo( o.getDesc() );
285 return getRef().compareTo( o.getRef() );