Added getValues and write to DistanceMatrix interface for broader use
[jalview.git] / forester / java / src / org / forester / phylogeny / data / Taxonomy.java
index 026d68b..8548fe6 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
 // 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
 //
 // Contact: phylosoft @ gmail . com
-// WWW: www.phylosoft.org/forester
+// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
 
 package org.forester.phylogeny.data;
 
@@ -35,6 +35,7 @@ import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
 import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
 import org.forester.util.ForesterUtil;
+import org.forester.util.TaxonomyUtil;
 
 public class Taxonomy implements PhylogenyData, MultipleUris, Comparable<Taxonomy> {
 
@@ -46,19 +47,23 @@ public class Taxonomy implements PhylogenyData, MultipleUris, Comparable<Taxonom
     private String       _taxonomy_code;
     private String       _rank;
     private List<Uri>    _uris;
+    private List<String> _lineage;
 
     public Taxonomy() {
         init();
     }
 
+    @Override
     public StringBuffer asSimpleText() {
         return asText();
     }
 
+    @Override
     public Uri getUri( final int index ) {
         return getUris().get( index );
     }
 
+    @Override
     public void addUri( final Uri uri ) {
         if ( getUris() == null ) {
             setUris( new ArrayList<Uri>() );
@@ -66,6 +71,7 @@ public class Taxonomy implements PhylogenyData, MultipleUris, Comparable<Taxonom
         getUris().add( uri );
     }
 
+    @Override
     public StringBuffer asText() {
         final StringBuffer sb = new StringBuffer();
         if ( getIdentifier() != null ) {
@@ -101,9 +107,15 @@ public class Taxonomy implements PhylogenyData, MultipleUris, Comparable<Taxonom
         return sb;
     }
 
+    @Override
     public PhylogenyData copy() {
         final Taxonomy t = new Taxonomy();
-        t.setTaxonomyCode( getTaxonomyCode() );
+        try {
+            t.setTaxonomyCode( getTaxonomyCode() );
+        }
+        catch ( final PhyloXmlDataFormatException e ) {
+            e.printStackTrace();
+        }
         t.setScientificName( getScientificName() );
         t.setCommonName( getCommonName() );
         t.setAuthority( getAuthority() );
@@ -116,7 +128,12 @@ public class Taxonomy implements PhylogenyData, MultipleUris, Comparable<Taxonom
         else {
             t.setIdentifier( null );
         }
-        t.setRank( new String( getRank() ) );
+        try {
+            t.setRank( new String( getRank() ) );
+        }
+        catch ( final PhyloXmlDataFormatException e ) {
+            e.printStackTrace();
+        }
         if ( getUris() != null ) {
             t.setUris( new ArrayList<Uri>() );
             for( final Uri uri : getUris() ) {
@@ -125,6 +142,14 @@ public class Taxonomy implements PhylogenyData, MultipleUris, Comparable<Taxonom
                 }
             }
         }
+        if ( getLineage() != null ) {
+            t.setLineage( new ArrayList<String>() );
+            for( final String l : getLineage() ) {
+                if ( l != null ) {
+                    t.getLineage().add( l );
+                }
+            }
+        }
         return t;
     }
 
@@ -176,13 +201,14 @@ public class Taxonomy implements PhylogenyData, MultipleUris, Comparable<Taxonom
         return _taxonomy_code;
     }
 
+    @Override
     public List<Uri> getUris() {
         return _uris;
     }
 
     @Override
     public int hashCode() {
-        if ( getIdentifier() != null ) {
+        if ( ( getIdentifier() != null ) && !ForesterUtil.isEmpty( getIdentifier().getValue() ) ) {
             return getIdentifier().hashCode();
         }
         else if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
@@ -203,22 +229,32 @@ public class Taxonomy implements PhylogenyData, MultipleUris, Comparable<Taxonom
         setScientificName( "" );
         setCommonName( "" );
         setIdentifier( null );
-        setRank( "" );
-        setTaxonomyCode( "" );
+        try {
+            setRank( "" );
+        }
+        catch ( final PhyloXmlDataFormatException e ) {
+            e.printStackTrace();
+        }
+        try {
+            setTaxonomyCode( "" );
+        }
+        catch ( final PhyloXmlDataFormatException e ) {
+            e.printStackTrace();
+        }
         setAuthority( "" );
         setSynonyms( null );
         setUris( null );
+        setLineage( null );
     }
 
     public boolean isEmpty() {
         return ( ( getIdentifier() == null ) && ForesterUtil.isEmpty( getTaxonomyCode() )
                 && ForesterUtil.isEmpty( getCommonName() ) && ForesterUtil.isEmpty( getScientificName() )
-                && ForesterUtil.isEmpty( getRank() ) && ForesterUtil.isEmpty( _uris )
-                && ForesterUtil.isEmpty( getAuthority() ) && ForesterUtil.isEmpty( _synonyms ) );
+                && ForesterUtil.isEmpty( _lineage ) );
     }
 
     /**
-     * 
+     *
      * If this and taxonomy 'data' has an identifier, comparison will be based on that.
      * Otherwise,  if this and taxonomy 'data' has a code, comparison will be based on that.
      * Otherwise,  if Taxonomy 'data' has a scientific name, comparison will be
@@ -226,14 +262,17 @@ public class Taxonomy implements PhylogenyData, MultipleUris, Comparable<Taxonom
      * Otherwise,  if Taxonomy 'data' has a common  name, comparison will be
      * based on that (case insensitive!).
      * (Note. This is important and should not be change without a very good reason.)
-     * 
+     *
      */
+    @Override
     public boolean isEqual( final PhylogenyData data ) {
         if ( this == data ) {
             return true;
         }
         final Taxonomy tax = ( Taxonomy ) data;
-        if ( ( getIdentifier() != null ) && ( tax.getIdentifier() != null ) ) {
+        if ( ( getIdentifier() != null ) && ( tax.getIdentifier() != null )
+                && !ForesterUtil.isEmpty( getIdentifier().getValue() )
+                && !ForesterUtil.isEmpty( tax.getIdentifier().getValue() ) ) {
             return getIdentifier().isEqual( tax.getIdentifier() );
         }
         else if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) && !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
@@ -249,13 +288,8 @@ public class Taxonomy implements PhylogenyData, MultipleUris, Comparable<Taxonom
         else if ( !ForesterUtil.isEmpty( getCommonName() ) && !ForesterUtil.isEmpty( tax.getCommonName() ) ) {
             return getCommonName().equalsIgnoreCase( tax.getCommonName() );
         }
-        else if ( !ForesterUtil.isEmpty( getScientificName() ) && !ForesterUtil.isEmpty( tax.getCommonName() ) ) {
-            return getScientificName().equalsIgnoreCase( tax.getCommonName() );
-        }
-        else if ( !ForesterUtil.isEmpty( getCommonName() ) && !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
-            return getCommonName().equalsIgnoreCase( tax.getScientificName() );
-        }
-        throw new RuntimeException( "comparison not possible with empty fields" );
+        //throw new RuntimeException( "comparison not possible with empty fields" );
+        return false;
     }
 
     public void setAuthority( final String authority ) {
@@ -270,8 +304,8 @@ public class Taxonomy implements PhylogenyData, MultipleUris, Comparable<Taxonom
         _identifier = identifier;
     }
 
-    public void setRank( final String rank ) {
-        if ( !ForesterUtil.isEmpty( rank ) && !PhyloXmlUtil.TAXONOMY_RANKS.contains( rank ) ) {
+    public void setRank( final String rank ) throws PhyloXmlDataFormatException {
+        if ( !ForesterUtil.isEmpty( rank ) && !TaxonomyUtil.TAXONOMY_RANKS_SET.contains( rank ) ) {
             throw new PhyloXmlDataFormatException( "illegal rank: [" + rank + "]" );
         }
         _rank = rank;
@@ -285,7 +319,7 @@ public class Taxonomy implements PhylogenyData, MultipleUris, Comparable<Taxonom
         _synonyms = synonyms;
     }
 
-    public void setTaxonomyCode( final String taxonomy_code ) {
+    public void setTaxonomyCode( String taxonomy_code ) throws PhyloXmlDataFormatException {
         if ( !ForesterUtil.isEmpty( taxonomy_code )
                 && !PhyloXmlUtil.TAXOMONY_CODE_PATTERN.matcher( taxonomy_code ).matches() ) {
             throw new PhyloXmlDataFormatException( "illegal taxonomy code: [" + taxonomy_code + "]" );
@@ -293,10 +327,12 @@ public class Taxonomy implements PhylogenyData, MultipleUris, Comparable<Taxonom
         _taxonomy_code = taxonomy_code;
     }
 
+    @Override
     public void setUris( final List<Uri> uris ) {
         _uris = uris;
     }
 
+    @Override
     public StringBuffer toNHX() {
         final StringBuffer sb = new StringBuffer();
         if ( getIdentifier() != null ) {
@@ -322,6 +358,7 @@ public class Taxonomy implements PhylogenyData, MultipleUris, Comparable<Taxonom
         return sb;
     }
 
+    @Override
     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
         if ( isEmpty() ) {
             return;
@@ -345,8 +382,10 @@ public class Taxonomy implements PhylogenyData, MultipleUris, Comparable<Taxonom
             PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_AUTHORITY, getAuthority(), indentation );
         }
         if ( !ForesterUtil.isEmpty( getCommonName() ) ) {
-            PhylogenyDataUtil
-                    .appendElement( writer, PhyloXmlMapping.TAXONOMY_COMMON_NAME, getCommonName(), indentation );
+            PhylogenyDataUtil.appendElement( writer,
+                                             PhyloXmlMapping.TAXONOMY_COMMON_NAME,
+                                             getCommonName(),
+                                             indentation );
         }
         if ( _synonyms != null ) {
             for( final String syn : getSynonyms() ) {
@@ -380,15 +419,36 @@ public class Taxonomy implements PhylogenyData, MultipleUris, Comparable<Taxonom
         if ( equals( o ) ) {
             return 0;
         }
-        else if ( !ForesterUtil.isEmpty( getScientificName() ) && !ForesterUtil.isEmpty( o.getScientificName() ) ) {
+        if ( ( getIdentifier() != null ) && ( o.getIdentifier() != null )
+                && !ForesterUtil.isEmpty( getIdentifier().getValue() )
+                && !ForesterUtil.isEmpty( o.getIdentifier().getValue() ) ) {
+            final int x = getIdentifier().getValuePlusProvider().compareTo( o.getIdentifier().getValuePlusProvider() );
+            if ( x != 0 ) {
+                return x;
+            }
+        }
+        if ( !ForesterUtil.isEmpty( getScientificName() ) && !ForesterUtil.isEmpty( o.getScientificName() ) ) {
             return getScientificName().compareToIgnoreCase( o.getScientificName() );
         }
-        else if ( !ForesterUtil.isEmpty( getCommonName() ) && !ForesterUtil.isEmpty( o.getCommonName() ) ) {
+        if ( !ForesterUtil.isEmpty( getCommonName() ) && !ForesterUtil.isEmpty( o.getCommonName() ) ) {
             return getCommonName().compareToIgnoreCase( o.getCommonName() );
         }
-        else if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) && !ForesterUtil.isEmpty( o.getTaxonomyCode() ) ) {
+        if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) && !ForesterUtil.isEmpty( o.getTaxonomyCode() ) ) {
             return getTaxonomyCode().compareToIgnoreCase( o.getTaxonomyCode() );
         }
-        return 0;
+        if ( ( getIdentifier() != null ) && ( o.getIdentifier() != null )
+                && !ForesterUtil.isEmpty( getIdentifier().getValue() )
+                && !ForesterUtil.isEmpty( o.getIdentifier().getValue() ) ) {
+            return getIdentifier().getValuePlusProvider().compareTo( o.getIdentifier().getValuePlusProvider() );
+        }
+        return 1;
+    }
+
+    public void setLineage( final List<String> lineage ) {
+        _lineage = lineage;
+    }
+
+    public List<String> getLineage() {
+        return _lineage;
     }
 }