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, UNKNOWN;
46 public String toString() {
61 throw new IllegalArgumentException();
66 public Accession( final String value ) {
70 _source_value = value;
73 public Accession( final String value, final String source ) {
77 if ( source != null ) {
78 _source_value = source + value;
81 _source_value = value;
85 public Accession( final String value, final Source source ) {
87 _source = source.toString();
89 _source_value = source + value;
92 public Accession( final String value, final String source, final String comment ) {
96 if ( source != null ) {
97 _source_value = source + value;
100 _source_value = value;
105 public StringBuffer asSimpleText() {
106 return new StringBuffer( getValue() );
110 public StringBuffer asText() {
111 final StringBuffer sb = new StringBuffer();
112 if ( !ForesterUtil.isEmpty( getSource() ) ) {
113 sb.append( getSource() );
116 sb.append( getValue() );
117 if ( !ForesterUtil.isEmpty( getComment() ) ) {
119 sb.append( getComment() );
126 public int compareTo( final Accession o ) {
130 return _source_value.compareTo( o._source_value );
134 public PhylogenyData copy() {
135 return new Accession( getValue(), getSource() );
139 public boolean equals( final Object o ) {
143 else if ( o == null ) {
146 else if ( o.getClass() != this.getClass() ) {
147 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
148 + o.getClass() + "]" );
151 return isEqual( ( Accession ) o );
155 public String getComment() {
159 public String getSource() {
163 public String getValue() {
168 public int hashCode() {
169 return _source_value.hashCode();
173 public boolean isEqual( final PhylogenyData data ) {
174 if ( this == data ) {
177 if ( ( data == null ) || ( getValue() == null ) ) {
180 final Accession a = ( Accession ) data;
181 if ( ( getSource() != null ) && ( a.getSource() != null ) ) {
182 return ( a.getValue().equals( getValue() ) && a.getSource().equals( getSource() ) );
184 return ( a.getValue().equals( getValue() ) );
188 public StringBuffer toNHX() {
189 final StringBuffer sb = new StringBuffer();
191 sb.append( NHXtags.SEQUENCE_ACCESSION );
192 sb.append( ForesterUtil.replaceIllegalNhxCharacters( getValue() ) );
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,
203 PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
208 PhylogenyDataUtil.appendElement( writer,
209 PhyloXmlMapping.ACCESSION,
211 PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
213 PhyloXmlMapping.ACCESSION_COMMENT_ATTR,
219 if ( ForesterUtil.isEmpty( getComment() ) ) {
220 PhylogenyDataUtil.appendElement( writer,
221 PhyloXmlMapping.ACCESSION,
223 PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
228 PhylogenyDataUtil.appendElement( writer,
229 PhyloXmlMapping.ACCESSION,
231 PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
233 PhyloXmlMapping.ACCESSION_COMMENT_ATTR,
241 public String toString() {
242 return asText().toString();