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
26 package org.forester.phylogeny.data;
28 import java.io.IOException;
29 import java.io.Writer;
31 import org.forester.io.parsers.nhx.NHXtags;
32 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
33 import org.forester.util.ForesterUtil;
35 public final class Accession implements PhylogenyData, Comparable<Accession> {
37 final private String _comment;
38 final private String _source;
39 final private String _source_value;
40 final private String _value;
43 NCBI, REFSEQ, UNIPROT, GI, EMBL, ENSEMBL, UNKNOWN;
46 public String toString() {
63 throw new IllegalArgumentException();
68 public Accession( final String value ) {
72 _source_value = value;
75 public Accession( final String value, final String source ) {
79 if ( source != null ) {
80 _source_value = source + value;
83 _source_value = value;
87 public Accession( final String value, final Source source ) {
89 _source = source.toString();
91 _source_value = source + value;
94 public Accession( final String value, final String source, final String comment ) {
98 if ( source != null ) {
99 _source_value = source + value;
102 _source_value = value;
107 public StringBuffer asSimpleText() {
108 return new StringBuffer( getValue() );
112 public StringBuffer asText() {
113 final StringBuffer sb = new StringBuffer();
114 if ( !ForesterUtil.isEmpty( getSource() ) ) {
115 sb.append( getSource() );
118 sb.append( getValue() );
119 if ( !ForesterUtil.isEmpty( getComment() ) ) {
121 sb.append( getComment() );
128 public int compareTo( final Accession o ) {
132 return _source_value.compareTo( o._source_value );
136 public PhylogenyData copy() {
137 return new Accession( getValue(), getSource() );
141 public boolean equals( final Object o ) {
145 else if ( o == null ) {
148 else if ( o.getClass() != this.getClass() ) {
149 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
150 + o.getClass() + "]" );
153 return isEqual( ( Accession ) o );
157 public String getComment() {
161 public String getSource() {
165 public String getValue() {
170 public int hashCode() {
171 return _source_value.hashCode();
175 public boolean isEqual( final PhylogenyData data ) {
176 if ( this == data ) {
179 if ( ( data == null ) || ( getValue() == null ) ) {
182 final Accession a = ( Accession ) data;
183 if ( ( getSource() != null ) && ( a.getSource() != null ) ) {
184 return ( a.getValue().equals( getValue() ) && a.getSource().equals( getSource() ) );
186 return ( a.getValue().equals( getValue() ) );
190 public StringBuffer toNHX() {
191 final StringBuffer sb = new StringBuffer();
193 sb.append( NHXtags.SEQUENCE_ACCESSION );
194 sb.append( ForesterUtil.replaceIllegalNhxCharacters( getValue() ) );
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,
205 PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
210 PhylogenyDataUtil.appendElement( writer,
211 PhyloXmlMapping.ACCESSION,
213 PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
215 PhyloXmlMapping.ACCESSION_COMMENT_ATTR,
221 if ( ForesterUtil.isEmpty( getComment() ) ) {
222 PhylogenyDataUtil.appendElement( writer,
223 PhyloXmlMapping.ACCESSION,
225 PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
230 PhylogenyDataUtil.appendElement( writer,
231 PhyloXmlMapping.ACCESSION,
233 PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
235 PhyloXmlMapping.ACCESSION_COMMENT_ATTR,
243 public String toString() {
244 return asText().toString();