inprogress
[jalview.git] / forester / java / src / org / forester / phylogeny / data / Accession.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
25
26 package org.forester.phylogeny.data;
27
28 import java.io.IOException;
29 import java.io.Writer;
30
31 import org.forester.io.parsers.nhx.NHXtags;
32 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
33 import org.forester.util.ForesterUtil;
34
35 public final class Accession implements PhylogenyData, Comparable<Accession> {
36
37     final private String _comment;
38     final private String _source;
39     final private String _source_value;
40     final private String _value;
41
42     public Accession( final String value, final String source ) {
43         _value = value;
44         _source = source;
45         _comment = "";
46         if ( source != null ) {
47             _source_value = source + value;
48         }
49         else {
50             _source_value = value;
51         }
52     }
53
54     public Accession( final String value, final String source, final String comment ) {
55         _value = value;
56         _source = source;
57         _comment = comment;
58         if ( source != null ) {
59             _source_value = source + value;
60         }
61         else {
62             _source_value = value;
63         }
64     }
65
66     @Override
67     public StringBuffer asSimpleText() {
68         return new StringBuffer( getValue() );
69     }
70
71     @Override
72     public StringBuffer asText() {
73         final StringBuffer sb = new StringBuffer();
74         if ( !ForesterUtil.isEmpty( getSource() ) ) {
75             sb.append( getSource() );
76             sb.append( ": " );
77         }
78         sb.append( getValue() );
79         if ( !ForesterUtil.isEmpty( getComment() ) ) {
80             sb.append( " (" );
81             sb.append( getComment() );
82             sb.append( ")" );
83         }
84         return sb;
85     }
86
87     @Override
88     public int compareTo( final Accession o ) {
89         if ( equals( o ) ) {
90             return 0;
91         }
92         return _source_value.compareTo( o._source_value );
93     }
94
95     @Override
96     public PhylogenyData copy() {
97         return new Accession( getValue(), getSource() );
98     }
99
100     @Override
101     public boolean equals( final Object o ) {
102         if ( this == o ) {
103             return true;
104         }
105         else if ( o == null ) {
106             return false;
107         }
108         else if ( o.getClass() != this.getClass() ) {
109             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
110                     + o.getClass() + "]" );
111         }
112         else {
113             return isEqual( ( Accession ) o );
114         }
115     }
116
117     public String getComment() {
118         return _comment;
119     }
120
121     public String getSource() {
122         return _source;
123     }
124
125     public String getValue() {
126         return _value;
127     }
128
129     @Override
130     public int hashCode() {
131         return _source_value.hashCode();
132     }
133
134     @Override
135     public boolean isEqual( final PhylogenyData data ) {
136         if ( this == data ) {
137             return true;
138         }
139         if ( ( data == null ) || ( getValue() == null ) ) {
140             return false;
141         }
142         final Accession a = ( Accession ) data;
143         if ( ( getSource() != null ) && ( a.getSource() != null ) ) {
144             return ( a.getValue().equals( getValue() ) && a.getSource().equals( getSource() ) );
145         }
146         return ( a.getValue().equals( getValue() ) );
147     }
148
149     @Override
150     public StringBuffer toNHX() {
151         final StringBuffer sb = new StringBuffer();
152         sb.append( ":" );
153         sb.append( NHXtags.SEQUENCE_ACCESSION );
154         sb.append( ForesterUtil.replaceIllegalNhxCharacters( getValue() ) );
155         return sb;
156     }
157
158     @Override
159     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
160         if ( ForesterUtil.isEmpty( getSource() ) ) {
161             if ( ForesterUtil.isEmpty( getComment() ) ) {
162                 PhylogenyDataUtil.appendElement( writer,
163                                                  PhyloXmlMapping.ACCESSION,
164                                                  getValue(),
165                                                  PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
166                                                  "unknown",
167                                                  indentation );
168             }
169             else {
170                 PhylogenyDataUtil.appendElement( writer,
171                                                  PhyloXmlMapping.ACCESSION,
172                                                  getValue(),
173                                                  PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
174                                                  "unknown",
175                                                  PhyloXmlMapping.ACCESSION_COMMENT_ATTR,
176                                                  getComment(),
177                                                  indentation );
178             }
179         }
180         else {
181             if ( ForesterUtil.isEmpty( getComment() ) ) {
182                 PhylogenyDataUtil.appendElement( writer,
183                                                  PhyloXmlMapping.ACCESSION,
184                                                  getValue(),
185                                                  PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
186                                                  getSource(),
187                                                  indentation );
188             }
189             else {
190                 PhylogenyDataUtil.appendElement( writer,
191                                                  PhyloXmlMapping.ACCESSION,
192                                                  getValue(),
193                                                  PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
194                                                  getSource(),
195                                                  PhyloXmlMapping.ACCESSION_COMMENT_ATTR,
196                                                  getComment(),
197                                                  indentation );
198             }
199         }
200     }
201
202     @Override
203     public String toString() {
204         return asText().toString();
205     }
206 }