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;
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";
47 public Accession( final String value ) {
51 _source_value = value;
54 public Accession( final String value, final String source ) {
58 if ( source != null ) {
59 _source_value = source + value;
62 _source_value = value;
66 public Accession( final String value, final String source, final String comment ) {
70 if ( source != null ) {
71 _source_value = source + value;
74 _source_value = value;
79 public StringBuffer asSimpleText() {
80 return new StringBuffer( getValue() );
84 public StringBuffer asText() {
85 final StringBuffer sb = new StringBuffer();
86 if ( !ForesterUtil.isEmpty( getSource() ) ) {
87 sb.append( getSource() );
90 sb.append( getValue() );
91 if ( !ForesterUtil.isEmpty( getComment() ) ) {
93 sb.append( getComment() );
100 public int compareTo( final Accession o ) {
104 return _source_value.compareTo( o._source_value );
108 public PhylogenyData copy() {
109 return new Accession( getValue(), getSource() );
113 public boolean equals( final Object o ) {
117 else if ( o == null ) {
120 else if ( o.getClass() != this.getClass() ) {
121 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
122 + o.getClass() + "]" );
125 return isEqual( ( Accession ) o );
129 public String getComment() {
133 public String getSource() {
137 public String getValue() {
142 public int hashCode() {
143 return _source_value.hashCode();
147 public boolean isEqual( final PhylogenyData data ) {
148 if ( this == data ) {
151 if ( ( data == null ) || ( getValue() == null ) ) {
154 final Accession a = ( Accession ) data;
155 if ( ( getSource() != null ) && ( a.getSource() != null ) ) {
156 return ( a.getValue().equals( getValue() ) && a.getSource().equals( getSource() ) );
158 return ( a.getValue().equals( getValue() ) );
162 public StringBuffer toNHX() {
163 final StringBuffer sb = new StringBuffer();
165 sb.append( NHXtags.SEQUENCE_ACCESSION );
166 sb.append( ForesterUtil.replaceIllegalNhxCharacters( getValue() ) );
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,
177 PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
182 PhylogenyDataUtil.appendElement( writer,
183 PhyloXmlMapping.ACCESSION,
185 PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
187 PhyloXmlMapping.ACCESSION_COMMENT_ATTR,
193 if ( ForesterUtil.isEmpty( getComment() ) ) {
194 PhylogenyDataUtil.appendElement( writer,
195 PhyloXmlMapping.ACCESSION,
197 PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
202 PhylogenyDataUtil.appendElement( writer,
203 PhyloXmlMapping.ACCESSION,
205 PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
207 PhyloXmlMapping.ACCESSION_COMMENT_ATTR,
215 public String toString() {
216 return asText().toString();