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