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