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( "illegal attempt to create Annotation with null or empty reference" );
52 if ( ( ref.indexOf( ':' ) < 1 ) || ( ref.length() < 3 ) ) {
53 throw new IllegalArgumentException( "illegal format for Annotation reference: [" + ref + "]" );
60 public StringBuffer asSimpleText() {
61 return new StringBuffer( getDesc() );
65 public StringBuffer asText() {
66 return new StringBuffer( getDesc() );
70 public PhylogenyData copy() {
71 final Annotation ann = new Annotation( new String( getRef() ) );
72 if ( getConfidence() != null ) {
73 ann.setConfidence( ( Confidence ) getConfidence().copy() );
76 ann.setConfidence( null );
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() );
86 ann.setProperties( null );
88 if ( getUris() != null ) {
89 ann.setUris( new ArrayList<Uri>() );
90 for( final Uri uri : getUris() ) {
92 ann.getUris().add( uri );
99 public Confidence getConfidence() {
103 public String getDesc() {
107 public String getEvidence() {
111 public PropertiesMap getProperties() {
115 public String getRef() {
119 public String getSource() {
123 public String getType() {
127 private void init() {
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() );
144 public void setConfidence( final Confidence confidence ) {
145 _confidence = confidence;
148 public void setDesc( final String desc ) {
152 public void setEvidence( final String evidence ) {
153 _evidence = evidence;
156 public void setProperties( final PropertiesMap property ) {
157 _properties = property;
160 // public void setRef( final String ref ) {
163 public void setSource( final String source ) {
167 public void setType( final String type ) {
172 public StringBuffer toNHX() {
173 throw new UnsupportedOperationException();
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,
186 PhyloXmlMapping.ANNOTATION_EVIDENCE_ATTR,
188 PhyloXmlMapping.ANNOTATION_TYPE_ATTR,
190 PhyloXmlMapping.ANNOTATION_SOURCE_ATTR,
192 if ( !ForesterUtil.isEmpty( getDesc() ) ) {
193 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.ANNOTATION_DESC, getDesc(), indentation );
195 if ( getConfidence() != null ) {
196 getConfidence().toPhyloXML( writer, level, indentation + PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
198 if ( getProperties() != null ) {
199 getProperties().toPhyloXML( writer, level, indentation );
201 if ( getUris() != null ) {
202 for( final Uri uri : getUris() ) {
204 uri.toPhyloXML( writer, level, indentation );
208 writer.write( ForesterUtil.LINE_SEPARATOR );
209 writer.write( indentation );
210 PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.ANNOTATION );
213 PhylogenyDataUtil.appendElement( writer,
214 PhyloXmlMapping.ANNOTATION,
215 PhyloXmlMapping.ANNOTATION_REF_ATTR,
217 PhyloXmlMapping.ANNOTATION_EVIDENCE_ATTR,
219 PhyloXmlMapping.ANNOTATION_TYPE_ATTR,
221 PhyloXmlMapping.ANNOTATION_SOURCE_ATTR,
228 public String toString() {
229 return asText().toString();
233 public void addUri( final Uri uri ) {
234 if ( getUris() == null ) {
235 setUris( new ArrayList<Uri>() );
237 getUris().add( uri );
241 public Uri getUri( final int index ) {
242 return getUris().get( index );
246 public List<Uri> getUris() {
251 public void setUris( final List<Uri> uris ) {
256 public boolean equals( final Object o ) {
260 else if ( o == null ) {
263 else if ( o.getClass() != this.getClass() ) {
264 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
265 + o.getClass() + "]" );
268 return isEqual( ( Annotation ) o );
273 public int compareTo( final Annotation o ) {
277 if ( getRef().equals( o.getRef() ) ) {
278 return getDesc().compareTo( o.getDesc() );
280 return getRef().compareTo( o.getRef() );