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 enum Source {
43         NCBI, REFSEQ, UNIPROT, GI, EMBL, UNKNOWN;
44
45         @Override
46         public String toString() {
47             switch ( this ) {
48                 case NCBI:
49                     return "ncbi";
50                 case REFSEQ:
51                     return "refseq";
52                 case UNIPROT:
53                     return "uniprot";
54                 case GI:
55                     return "gi";
56                 case EMBL:
57                     return "embl";
58                 case UNKNOWN:
59                     return "unknown";
60                 default:
61                     throw new IllegalArgumentException();
62             }
63         }
64     }
65
66     public Accession( final String value ) {
67         _value = value;
68         _source = "";
69         _comment = "";
70         _source_value = value;
71     }
72
73     public Accession( final String value, final String source ) {
74         _value = value;
75         _source = source;
76         _comment = "";
77         if ( source != null ) {
78             _source_value = source + value;
79         }
80         else {
81             _source_value = value;
82         }
83     }
84
85     public Accession( final String value, final Source source ) {
86         _value = value;
87         _source = source.toString();
88         _comment = "";
89         _source_value = source + value;
90     }
91
92     public Accession( final String value, final String source, final String comment ) {
93         _value = value;
94         _source = source;
95         _comment = comment;
96         if ( source != null ) {
97             _source_value = source + value;
98         }
99         else {
100             _source_value = value;
101         }
102     }
103
104     @Override
105     public StringBuffer asSimpleText() {
106         return new StringBuffer( getValue() );
107     }
108
109     @Override
110     public StringBuffer asText() {
111         final StringBuffer sb = new StringBuffer();
112         if ( !ForesterUtil.isEmpty( getSource() ) ) {
113             sb.append( getSource() );
114             sb.append( ": " );
115         }
116         sb.append( getValue() );
117         if ( !ForesterUtil.isEmpty( getComment() ) ) {
118             sb.append( " (" );
119             sb.append( getComment() );
120             sb.append( ")" );
121         }
122         return sb;
123     }
124
125     @Override
126     public int compareTo( final Accession o ) {
127         if ( equals( o ) ) {
128             return 0;
129         }
130         return _source_value.compareTo( o._source_value );
131     }
132
133     @Override
134     public PhylogenyData copy() {
135         return new Accession( getValue(), getSource() );
136     }
137
138     @Override
139     public boolean equals( final Object o ) {
140         if ( this == o ) {
141             return true;
142         }
143         else if ( o == null ) {
144             return false;
145         }
146         else if ( o.getClass() != this.getClass() ) {
147             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
148                     + o.getClass() + "]" );
149         }
150         else {
151             return isEqual( ( Accession ) o );
152         }
153     }
154
155     public String getComment() {
156         return _comment;
157     }
158
159     public String getSource() {
160         return _source;
161     }
162
163     public String getValue() {
164         return _value;
165     }
166
167     @Override
168     public int hashCode() {
169         return _source_value.hashCode();
170     }
171
172     @Override
173     public boolean isEqual( final PhylogenyData data ) {
174         if ( this == data ) {
175             return true;
176         }
177         if ( ( data == null ) || ( getValue() == null ) ) {
178             return false;
179         }
180         final Accession a = ( Accession ) data;
181         if ( ( getSource() != null ) && ( a.getSource() != null ) ) {
182             return ( a.getValue().equals( getValue() ) && a.getSource().equals( getSource() ) );
183         }
184         return ( a.getValue().equals( getValue() ) );
185     }
186
187     @Override
188     public StringBuffer toNHX() {
189         final StringBuffer sb = new StringBuffer();
190         sb.append( ":" );
191         sb.append( NHXtags.SEQUENCE_ACCESSION );
192         sb.append( ForesterUtil.replaceIllegalNhxCharacters( getValue() ) );
193         return sb;
194     }
195
196     @Override
197     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
198         if ( ForesterUtil.isEmpty( getSource() ) ) {
199             if ( ForesterUtil.isEmpty( getComment() ) ) {
200                 PhylogenyDataUtil.appendElement( writer,
201                                                  PhyloXmlMapping.ACCESSION,
202                                                  getValue(),
203                                                  PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
204                                                  "unknown",
205                                                  indentation );
206             }
207             else {
208                 PhylogenyDataUtil.appendElement( writer,
209                                                  PhyloXmlMapping.ACCESSION,
210                                                  getValue(),
211                                                  PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
212                                                  "unknown",
213                                                  PhyloXmlMapping.ACCESSION_COMMENT_ATTR,
214                                                  getComment(),
215                                                  indentation );
216             }
217         }
218         else {
219             if ( ForesterUtil.isEmpty( getComment() ) ) {
220                 PhylogenyDataUtil.appendElement( writer,
221                                                  PhyloXmlMapping.ACCESSION,
222                                                  getValue(),
223                                                  PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
224                                                  getSource(),
225                                                  indentation );
226             }
227             else {
228                 PhylogenyDataUtil.appendElement( writer,
229                                                  PhyloXmlMapping.ACCESSION,
230                                                  getValue(),
231                                                  PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
232                                                  getSource(),
233                                                  PhyloXmlMapping.ACCESSION_COMMENT_ATTR,
234                                                  getComment(),
235                                                  indentation );
236             }
237         }
238     }
239
240     @Override
241     public String toString() {
242         return asText().toString();
243     }
244 }