inprogress
[jalview.git] / forester / java / src / org / forester / phylogeny / data / Accession.java
index 295b8b7..8d9739a 100644 (file)
@@ -5,7 +5,7 @@
 // Copyright (C) 2008-2009 Christian M. Zmasek
 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
 // All rights reserved
-// 
+//
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
@@ -15,7 +15,7 @@
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 // Lesser General Public License for more details.
-// 
+//
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
@@ -32,33 +32,81 @@ import org.forester.io.parsers.nhx.NHXtags;
 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
 import org.forester.util.ForesterUtil;
 
-public class Accession implements PhylogenyData {
+public final class Accession implements PhylogenyData, Comparable<Accession> {
 
-    final String _value;
-    final String _source;
+    final private String       _comment;
+    final private String       _source;
+    final private String       _source_value;
+    final private String       _value;
+    final public static String NCBI    = "ncbi";
+    final public static String REFSEQ  = "refseq";
+    final public static String UNIPROT = "uniprot";
+    final public static String GI      = "gi";
+    public static final String EMBL    = "embl";
+
+    public Accession( final String value ) {
+        _value = value;
+        _source = "";
+        _comment = "";
+        _source_value = value;
+    }
 
     public Accession( final String value, final String source ) {
         _value = value;
         _source = source;
+        _comment = "";
+        if ( source != null ) {
+            _source_value = source + value;
+        }
+        else {
+            _source_value = value;
+        }
+    }
+
+    public Accession( final String value, final String source, final String comment ) {
+        _value = value;
+        _source = source;
+        _comment = comment;
+        if ( source != null ) {
+            _source_value = source + value;
+        }
+        else {
+            _source_value = value;
+        }
     }
 
+    @Override
     public StringBuffer asSimpleText() {
         return new StringBuffer( getValue() );
     }
 
+    @Override
     public StringBuffer asText() {
         final StringBuffer sb = new StringBuffer();
         if ( !ForesterUtil.isEmpty( getSource() ) ) {
-            sb.append( "[" );
             sb.append( getSource() );
-            sb.append( "] " );
+            sb.append( ": " );
         }
         sb.append( getValue() );
+        if ( !ForesterUtil.isEmpty( getComment() ) ) {
+            sb.append( " (" );
+            sb.append( getComment() );
+            sb.append( ")" );
+        }
         return sb;
     }
 
+    @Override
+    public int compareTo( final Accession o ) {
+        if ( equals( o ) ) {
+            return 0;
+        }
+        return _source_value.compareTo( o._source_value );
+    }
+
+    @Override
     public PhylogenyData copy() {
-        return new Accession( new String( getValue() ), new String( getSource() ) );
+        return new Accession( getValue(), getSource() );
     }
 
     @Override
@@ -78,6 +126,10 @@ public class Accession implements PhylogenyData {
         }
     }
 
+    public String getComment() {
+        return _comment;
+    }
+
     public String getSource() {
         return _source;
     }
@@ -88,12 +140,10 @@ public class Accession implements PhylogenyData {
 
     @Override
     public int hashCode() {
-        if ( getSource() != null ) {
-            return ( getSource() + getValue() ).hashCode();
-        }
-        return getValue().hashCode();
+        return _source_value.hashCode();
     }
 
+    @Override
     public boolean isEqual( final PhylogenyData data ) {
         if ( this == data ) {
             return true;
@@ -108,6 +158,7 @@ public class Accession implements PhylogenyData {
         return ( a.getValue().equals( getValue() ) );
     }
 
+    @Override
     public StringBuffer toNHX() {
         final StringBuffer sb = new StringBuffer();
         sb.append( ":" );
@@ -116,22 +167,47 @@ public class Accession implements PhylogenyData {
         return sb;
     }
 
+    @Override
     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
         if ( ForesterUtil.isEmpty( getSource() ) ) {
-            PhylogenyDataUtil.appendElement( writer,
-                                             PhyloXmlMapping.ACCESSION,
-                                             getValue(),
-                                             PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
-                                             "unknown",
-                                             indentation );
+            if ( ForesterUtil.isEmpty( getComment() ) ) {
+                PhylogenyDataUtil.appendElement( writer,
+                                                 PhyloXmlMapping.ACCESSION,
+                                                 getValue(),
+                                                 PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
+                                                 "unknown",
+                                                 indentation );
+            }
+            else {
+                PhylogenyDataUtil.appendElement( writer,
+                                                 PhyloXmlMapping.ACCESSION,
+                                                 getValue(),
+                                                 PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
+                                                 "unknown",
+                                                 PhyloXmlMapping.ACCESSION_COMMENT_ATTR,
+                                                 getComment(),
+                                                 indentation );
+            }
         }
         else {
-            PhylogenyDataUtil.appendElement( writer,
-                                             PhyloXmlMapping.ACCESSION,
-                                             getValue(),
-                                             PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
-                                             getSource(),
-                                             indentation );
+            if ( ForesterUtil.isEmpty( getComment() ) ) {
+                PhylogenyDataUtil.appendElement( writer,
+                                                 PhyloXmlMapping.ACCESSION,
+                                                 getValue(),
+                                                 PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
+                                                 getSource(),
+                                                 indentation );
+            }
+            else {
+                PhylogenyDataUtil.appendElement( writer,
+                                                 PhyloXmlMapping.ACCESSION,
+                                                 getValue(),
+                                                 PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
+                                                 getSource(),
+                                                 PhyloXmlMapping.ACCESSION_COMMENT_ATTR,
+                                                 getComment(),
+                                                 indentation );
+            }
         }
     }