in progress
authorcmzmasek <cmzmasek@ca865154-3058-d1c3-3e42-d8f55a55bdbd>
Mon, 4 Jun 2012 15:11:14 +0000 (15:11 +0000)
committercmzmasek <cmzmasek@ca865154-3058-d1c3-3e42-d8f55a55bdbd>
Mon, 4 Jun 2012 15:11:14 +0000 (15:11 +0000)
forester/java/src/org/forester/analysis/AncestralTaxonomyInference.java
forester/java/src/org/forester/analysis/TaxonomyDataObtainer.java
forester/java/src/org/forester/archaeopteryx/AptxUtil.java

index 2166110..6999f8b 100644 (file)
@@ -26,10 +26,8 @@ package org.forester.analysis;
 
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
 
-import org.forester.analysis.TaxonomyDataObtainer.QUERY_TYPE;
 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyNode;
@@ -38,80 +36,9 @@ import org.forester.phylogeny.data.Taxonomy;
 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
 import org.forester.util.ForesterUtil;
 import org.forester.ws.uniprot.UniProtTaxonomy;
-import org.forester.ws.uniprot.UniProtWsTools;
 
 public final class AncestralTaxonomyInference {
 
-    private static final int                              MAX_TAXONOMIES_TO_RETURN = 10;
-   
-    private static UniProtTaxonomy getTaxonomies( final HashMap<String, UniProtTaxonomy> cache,
-                                                               final Object query,
-                                                               final QUERY_TYPE qt ) throws IOException,
-            AncestralTaxonomyInferenceException {
-        if ( cache.containsKey( query ) ) {
-            return cache.get( query ).copy();
-        }
-        else {
-            List<UniProtTaxonomy> up_taxonomies = null;
-            switch ( qt ) {
-                case ID:
-                    up_taxonomies = getTaxonomiesFromId( ( String ) query );
-                    break;
-                case CODE:
-                    up_taxonomies = getTaxonomiesFromTaxonomyCode( ( String ) query );
-                    break;
-                case SN:
-                    up_taxonomies = getTaxonomiesFromScientificName( ( String ) query );
-                    break;
-                case CN:
-                    up_taxonomies = getTaxonomiesFromCommonName( ( String ) query );
-                    break;
-                case LIN:
-                    return obtainUniProtTaxonomyFromLineage( ( List<String> ) query );
-                default:
-                    throw new RuntimeException();
-            }
-            if ( ( up_taxonomies != null ) && ( up_taxonomies.size() == 1 ) ) {
-                final UniProtTaxonomy up_tax = up_taxonomies.get( 0 );
-                if ( !ForesterUtil.isEmpty( up_tax.getScientificName() ) ) {
-                    TaxonomyDataObtainer.getSnTaxCacheMap().put( up_tax.getScientificName(), up_tax );
-                }
-                if ( !ForesterUtil.isEmpty( up_tax.getCode() ) ) {
-                    TaxonomyDataObtainer.getCodeTaxCacheMap().put( up_tax.getCode(), up_tax );
-                }
-                if ( !ForesterUtil.isEmpty( up_tax.getCommonName() ) ) {
-                    TaxonomyDataObtainer.getCnTaxCacheMap().put( up_tax.getCommonName(), up_tax );
-                }
-                if ( !ForesterUtil.isEmpty( up_tax.getId() ) ) {
-                    TaxonomyDataObtainer.getIdTaxCacheMap().put( up_tax.getId(), up_tax );
-                }
-                return up_tax;
-            }
-            else {
-                return null;
-            }
-        }
-    }
-
-    private static List<UniProtTaxonomy> getTaxonomiesFromCommonName( final String query )
-            throws IOException {
-        return UniProtWsTools.getTaxonomiesFromCommonNameStrict( query, MAX_TAXONOMIES_TO_RETURN );
-    }
-
-    private static List<UniProtTaxonomy> getTaxonomiesFromId( final String query ) throws IOException {
-        return UniProtWsTools.getTaxonomiesFromId( query, MAX_TAXONOMIES_TO_RETURN );
-    }
-
-    private static List<UniProtTaxonomy> getTaxonomiesFromScientificName( final String query )
-            throws IOException {
-        return UniProtWsTools.getTaxonomiesFromScientificNameStrict( query, MAX_TAXONOMIES_TO_RETURN );
-    }
-
-    private static List<UniProtTaxonomy> getTaxonomiesFromTaxonomyCode( final String query )
-            throws IOException {
-        return UniProtWsTools.getTaxonomiesFromTaxonomyCode( query, MAX_TAXONOMIES_TO_RETURN );
-    }
-
     public static void inferTaxonomyFromDescendents( final Phylogeny phy ) throws IOException,
             AncestralTaxonomyInferenceException {
         TaxonomyDataObtainer.clearCachesIfTooLarge();
@@ -123,7 +50,7 @@ public final class AncestralTaxonomyInference {
         }
     }
 
-     private static void inferTaxonomyFromDescendents( final PhylogenyNode n ) throws IOException,
+    private static void inferTaxonomyFromDescendents( final PhylogenyNode n ) throws IOException,
             AncestralTaxonomyInferenceException {
         if ( n.isExternal() ) {
             throw new IllegalArgumentException( "attempt to infer taxonomy from descendants of external node" );
@@ -139,7 +66,8 @@ public final class AncestralTaxonomyInference {
                             || !ForesterUtil.isEmpty( desc.getNodeData().getTaxonomy().getLineage() )
                             || !ForesterUtil.isEmpty( desc.getNodeData().getTaxonomy().getTaxonomyCode() ) || !ForesterUtil
                             .isEmpty( desc.getNodeData().getTaxonomy().getCommonName() ) ) ) {
-                final UniProtTaxonomy up_tax = obtainUniProtTaxonomy( desc.getNodeData().getTaxonomy(), null, null );
+                final UniProtTaxonomy up_tax = TaxonomyDataObtainer.obtainUniProtTaxonomy( desc.getNodeData()
+                        .getTaxonomy(), null, null );
                 if ( ( up_tax == null ) && ForesterUtil.isEmpty( desc.getNodeData().getTaxonomy().getLineage() ) ) {
                     String desc_str = "";
                     if ( !ForesterUtil.isEmpty( desc.getName() ) ) {
@@ -241,7 +169,7 @@ public final class AncestralTaxonomyInference {
         final Taxonomy tax = new Taxonomy();
         n.getNodeData().setTaxonomy( tax );
         tax.setScientificName( last_common );
-        final UniProtTaxonomy up_tax = obtainUniProtTaxonomyFromLineage( last_common_lineage );
+        final UniProtTaxonomy up_tax = TaxonomyDataObtainer.obtainUniProtTaxonomyFromLineage( last_common_lineage );
         if ( up_tax != null ) {
             if ( !ForesterUtil.isEmpty( up_tax.getRank() ) ) {
                 try {
@@ -284,59 +212,4 @@ public final class AncestralTaxonomyInference {
             }
         }
     }
-
-   
-
-    
-    private static UniProtTaxonomy obtainUniProtTaxonomyFromLineage( final List<String> lineage )
-            throws AncestralTaxonomyInferenceException, IOException {
-        final String lineage_str = ForesterUtil.stringListToString( lineage, ">" );
-        UniProtTaxonomy up_tax = null;
-        if ( TaxonomyDataObtainer.getLineageTaxCacheMap().containsKey( lineage_str ) ) {
-            up_tax = TaxonomyDataObtainer.getLineageTaxCacheMap().get( lineage_str ).copy();
-        }
-        else {
-            final List<UniProtTaxonomy> up_taxonomies = getTaxonomiesFromScientificName( lineage
-                    .get( lineage.size() - 1 ) );
-            if ( ( up_taxonomies != null ) && ( up_taxonomies.size() > 0 ) ) {
-                for( final UniProtTaxonomy up_taxonomy : up_taxonomies ) {
-                    boolean match = true;
-                    I: for( int i = 0; i < lineage.size(); ++i ) {
-                        if ( !lineage.get( i ).equalsIgnoreCase( up_taxonomy.getLineage().get( i ) ) ) {
-                            match = false;
-                            break I;
-                        }
-                    }
-                    if ( match ) {
-                        if ( up_tax != null ) {
-                            throw new AncestralTaxonomyInferenceException( "lineage \""
-                                    + ForesterUtil.stringListToString( lineage, " > " ) + "\" is not unique" );
-                        }
-                        up_tax = up_taxonomy;
-                    }
-                }
-                if ( up_tax == null ) {
-                    throw new AncestralTaxonomyInferenceException( "lineage \""
-                            + ForesterUtil.stringListToString( lineage, " > " ) + "\" not found" );
-                }
-                TaxonomyDataObtainer.getLineageTaxCacheMap().put( lineage_str, up_tax );
-                if ( !ForesterUtil.isEmpty( up_tax.getScientificName() ) ) {
-                    TaxonomyDataObtainer.getSnTaxCacheMap().put( up_tax.getScientificName(), up_tax );
-                }
-                if ( !ForesterUtil.isEmpty( up_tax.getCode() ) ) {
-                    TaxonomyDataObtainer.getCodeTaxCacheMap().put( up_tax.getCode(), up_tax );
-                }
-                if ( !ForesterUtil.isEmpty( up_tax.getCommonName() ) ) {
-                    TaxonomyDataObtainer.getCnTaxCacheMap().put( up_tax.getCommonName(), up_tax );
-                }
-                if ( !ForesterUtil.isEmpty( up_tax.getId() ) ) {
-                    TaxonomyDataObtainer.getIdTaxCacheMap().put( up_tax.getId(), up_tax );
-                }
-            }
-        }
-        return up_tax;
-    }
-
-   
-  
 }
index 003cb9c..9222c44 100644 (file)
@@ -51,170 +51,131 @@ import org.forester.ws.uniprot.UniProtWsTools;
 
 public class TaxonomyDataObtainer extends RunnableProcess {
 
+    enum QUERY_TYPE {
+        CODE, SN, CN, ID, LIN;
+    }
     private static final int                              MAX_CACHE_SIZE           = 100000;
-    
+    private static final int                              MAX_TAXONOMIES_TO_RETURN = 10;
     private static final HashMap<String, UniProtTaxonomy> _sn_up_cache_map         = new HashMap<String, UniProtTaxonomy>();
     private static final HashMap<String, UniProtTaxonomy> _lineage_up_cache_map    = new HashMap<String, UniProtTaxonomy>();
     private static final HashMap<String, UniProtTaxonomy> _code_up_cache_map       = new HashMap<String, UniProtTaxonomy>();
     private static final HashMap<String, UniProtTaxonomy> _cn_up_cache_map         = new HashMap<String, UniProtTaxonomy>();
     private static final HashMap<String, UniProtTaxonomy> _id_up_cache_map         = new HashMap<String, UniProtTaxonomy>();
 
-    
-    private final Phylogeny            _phy;
-    private final MainFrameApplication _mf;
-    private final TreePanel            _treepanel;
-    private final boolean              _delete;
+    synchronized static void clearCachesIfTooLarge() {
+        if ( getSnTaxCacheMap().size() > MAX_CACHE_SIZE ) {
+            getSnTaxCacheMap().clear();
+        }
+        if ( getLineageTaxCacheMap().size() > MAX_CACHE_SIZE ) {
+            getLineageTaxCacheMap().clear();
+        }
+        if ( getCnTaxCacheMap().size() > MAX_CACHE_SIZE ) {
+            getCnTaxCacheMap().clear();
+        }
+        if ( getCodeTaxCacheMap().size() > MAX_CACHE_SIZE ) {
+            getCodeTaxCacheMap().clear();
+        }
+        if ( getIdTaxCacheMap().size() > MAX_CACHE_SIZE ) {
+            getIdTaxCacheMap().clear();
+        }
+    }
 
-    public TaxonomyDataObtainer( final MainFrameApplication mf,
-                                 final TreePanel treepanel,
-                                 final Phylogeny phy,
-                                 final boolean delete ) {
-        _phy = phy;
-        _mf = mf;
-        _treepanel = treepanel;
-        _delete = delete;
+    synchronized static HashMap<String, UniProtTaxonomy> getCnTaxCacheMap() {
+        return _cn_up_cache_map;
     }
 
-    public TaxonomyDataObtainer( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
-        _phy = phy;
-        _mf = mf;
-        _treepanel = treepanel;
-        _delete = false;
+    synchronized static HashMap<String, UniProtTaxonomy> getCodeTaxCacheMap() {
+        return _code_up_cache_map;
     }
 
-    private String getBaseUrl() {
-        return UniProtWsTools.BASE_URL;
+    synchronized static HashMap<String, UniProtTaxonomy> getIdTaxCacheMap() {
+        return _id_up_cache_map;
     }
 
-    private void execute() {
-        start( _mf, "taxonomy data" );
-        SortedSet<String> not_found = null;
-        try {
-            not_found = obtainDetailedTaxonomicInformation( _phy, _delete );
-        }
-        catch ( final UnknownHostException e ) {
-            JOptionPane.showMessageDialog( _mf,
-                                           "Could not connect to \"" + getBaseUrl() + "\"",
-                                           "Network error during taxonomic information gathering",
-                                           JOptionPane.ERROR_MESSAGE );
-            return;
-        }
-        catch ( final IOException e ) {
-            e.printStackTrace();
-            JOptionPane.showMessageDialog( _mf,
-                                           e.toString(),
-                                           "Failed to obtain taxonomic information",
-                                           JOptionPane.ERROR_MESSAGE );
-            return;
-        }
-        catch ( final AncestralTaxonomyInferenceException e ) {
-            e.printStackTrace();
-            JOptionPane.showMessageDialog( _mf,
-                                           e.toString(),
-                                           "Failed to obtain taxonomic information",
-                                           JOptionPane.ERROR_MESSAGE );
-            return;
-        }
-        finally {
-            end( _mf );
-        }
-        if ( ( _phy == null ) || _phy.isEmpty() ) {
-            try {
-                JOptionPane.showMessageDialog( _mf,
-                                               "None of the external node taxonomies could be resolved",
-                                               "Taxonomy Tool Failed",
-                                               JOptionPane.WARNING_MESSAGE );
-            }
-            catch ( final Exception e ) {
-                // Not important if this fails, do nothing. 
-            }
-            return;
+    synchronized static HashMap<String, UniProtTaxonomy> getLineageTaxCacheMap() {
+        return _lineage_up_cache_map;
+    }
+
+    synchronized static HashMap<String, UniProtTaxonomy> getSnTaxCacheMap() {
+        return _sn_up_cache_map;
+    }
+
+    private static UniProtTaxonomy getTaxonomies( final HashMap<String, UniProtTaxonomy> cache,
+                                                  final Object query,
+                                                  final QUERY_TYPE qt ) throws IOException,
+            AncestralTaxonomyInferenceException {
+        if ( cache.containsKey( query ) ) {
+            return cache.get( query ).copy();
         }
-        _treepanel.setTree( _phy );
-        _mf.showWhole();
-        _treepanel.setEdited( true );
-        if ( ( not_found != null ) && ( not_found.size() > 0 ) ) {
-            int max = not_found.size();
-            boolean more = false;
-            if ( max > 20 ) {
-                more = true;
-                max = 20;
+        else {
+            List<UniProtTaxonomy> up_taxonomies = null;
+            switch ( qt ) {
+                case ID:
+                    up_taxonomies = getTaxonomiesFromId( ( String ) query );
+                    break;
+                case CODE:
+                    up_taxonomies = getTaxonomiesFromTaxonomyCode( ( String ) query );
+                    break;
+                case SN:
+                    up_taxonomies = getTaxonomiesFromScientificName( ( String ) query );
+                    break;
+                case CN:
+                    up_taxonomies = getTaxonomiesFromCommonName( ( String ) query );
+                    break;
+                case LIN:
+                    return obtainUniProtTaxonomyFromLineage( ( List<String> ) query );
+                default:
+                    throw new RuntimeException();
             }
-            final StringBuffer sb = new StringBuffer();
-            sb.append( "Not all taxonomies could be resolved.\n" );
-            if ( not_found.size() == 1 ) {
-                if ( _delete ) {
-                    sb.append( "The following taxonomy was not found and deleted (if external):\n" );
+            if ( ( up_taxonomies != null ) && ( up_taxonomies.size() == 1 ) ) {
+                final UniProtTaxonomy up_tax = up_taxonomies.get( 0 );
+                if ( !ForesterUtil.isEmpty( up_tax.getScientificName() ) ) {
+                    TaxonomyDataObtainer.getSnTaxCacheMap().put( up_tax.getScientificName(), up_tax );
                 }
-                else {
-                    sb.append( "The following taxonomy was not found:\n" );
+                if ( !ForesterUtil.isEmpty( up_tax.getCode() ) ) {
+                    TaxonomyDataObtainer.getCodeTaxCacheMap().put( up_tax.getCode(), up_tax );
                 }
-            }
-            else {
-                if ( _delete ) {
-                    sb.append( "The following taxonomies were not found and deleted (if external) (total: "
-                            + not_found.size() + "):\n" );
+                if ( !ForesterUtil.isEmpty( up_tax.getCommonName() ) ) {
+                    TaxonomyDataObtainer.getCnTaxCacheMap().put( up_tax.getCommonName(), up_tax );
                 }
-                else {
-                    sb.append( "The following taxonomies were not found (total: " + not_found.size() + "):\n" );
-                }
-            }
-            int i = 0;
-            for( final String string : not_found ) {
-                if ( i > 19 ) {
-                    break;
+                if ( !ForesterUtil.isEmpty( up_tax.getId() ) ) {
+                    TaxonomyDataObtainer.getIdTaxCacheMap().put( up_tax.getId(), up_tax );
                 }
-                sb.append( string );
-                sb.append( "\n" );
-                ++i;
-            }
-            if ( more ) {
-                sb.append( "..." );
-            }
-            try {
-                JOptionPane.showMessageDialog( _mf,
-                                               sb.toString(),
-                                               "Taxonomy Tool Completed",
-                                               JOptionPane.WARNING_MESSAGE );
-            }
-            catch ( final Exception e ) {
-                // Not important if this fails, do nothing. 
-            }
-        }
-        else {
-            try {
-                JOptionPane.showMessageDialog( _mf,
-                                               "Taxonomy tool successfully completed",
-                                               "Taxonomy Tool Completed",
-                                               JOptionPane.INFORMATION_MESSAGE );
+                return up_tax;
             }
-            catch ( final Exception e ) {
-                // Not important if this fails, do nothing.
+            else {
+                return null;
             }
         }
     }
 
-    
-    synchronized static void clearCachesIfTooLarge() {
-        if ( getSnTaxCacheMap().size() > MAX_CACHE_SIZE ) {
-            getSnTaxCacheMap().clear();
-        }
-        if ( getLineageTaxCacheMap().size() > MAX_CACHE_SIZE ) {
-            getLineageTaxCacheMap().clear();
-        }
-        if ( getCnTaxCacheMap().size() > MAX_CACHE_SIZE ) {
-            getCnTaxCacheMap().clear();
-        }
-        if ( getCodeTaxCacheMap().size() > MAX_CACHE_SIZE ) {
-            getCodeTaxCacheMap().clear();
-        }
-        if ( getIdTaxCacheMap().size() > MAX_CACHE_SIZE ) {
-            getIdTaxCacheMap().clear();
-        }
+    private static List<UniProtTaxonomy> getTaxonomiesFromCommonName( final String query ) throws IOException {
+        return UniProtWsTools.getTaxonomiesFromCommonNameStrict( query, MAX_TAXONOMIES_TO_RETURN );
+    }
+
+    private static List<UniProtTaxonomy> getTaxonomiesFromId( final String query ) throws IOException {
+        return UniProtWsTools.getTaxonomiesFromId( query, MAX_TAXONOMIES_TO_RETURN );
+    }
+
+    //
+    private static List<UniProtTaxonomy> getTaxonomiesFromScientificName( final String query ) throws IOException {
+        return UniProtWsTools.getTaxonomiesFromScientificNameStrict( query, MAX_TAXONOMIES_TO_RETURN );
     }
-    
+
+    //
+    private static List<UniProtTaxonomy> getTaxonomiesFromTaxonomyCode( final String query ) throws IOException {
+        return UniProtWsTools.getTaxonomiesFromTaxonomyCode( query, MAX_TAXONOMIES_TO_RETURN );
+    }
+
+    static boolean isHasAppropriateId( final Taxonomy tax ) {
+        return ( ( tax.getIdentifier() != null ) && ( !ForesterUtil.isEmpty( tax.getIdentifier().getValue() ) && ( tax
+                .getIdentifier().getProvider().equalsIgnoreCase( "ncbi" )
+                || tax.getIdentifier().getProvider().equalsIgnoreCase( "uniprot" ) || tax.getIdentifier().getProvider()
+                .equalsIgnoreCase( "uniprotkb" ) ) ) );
+    }
+
     synchronized private static SortedSet<String> obtainDetailedTaxonomicInformation( final Phylogeny phy,
-                                                                                     final boolean delete )
+                                                                                      final boolean delete )
             throws IOException, AncestralTaxonomyInferenceException {
         clearCachesIfTooLarge();
         final SortedSet<String> not_found = new TreeSet<String>();
@@ -267,9 +228,6 @@ public class TaxonomyDataObtainer extends RunnableProcess {
         }
         return not_found;
     }
-    
-    
-
 
     public static UniProtTaxonomy obtainUniProtTaxonomy( final Taxonomy tax, Object query, QUERY_TYPE qt )
             throws IOException, AncestralTaxonomyInferenceException {
@@ -302,17 +260,55 @@ public class TaxonomyDataObtainer extends RunnableProcess {
         }
     }
 
-    
-    
-    
-    static boolean isHasAppropriateId( final Taxonomy tax ) {
-        return ( ( tax.getIdentifier() != null ) && ( !ForesterUtil.isEmpty( tax.getIdentifier().getValue() ) && ( tax
-                .getIdentifier().getProvider().equalsIgnoreCase( "ncbi" )
-                || tax.getIdentifier().getProvider().equalsIgnoreCase( "uniprot" ) || tax.getIdentifier().getProvider()
-                .equalsIgnoreCase( "uniprotkb" ) ) ) );
+    static UniProtTaxonomy obtainUniProtTaxonomyFromLineage( final List<String> lineage )
+            throws AncestralTaxonomyInferenceException, IOException {
+        final String lineage_str = ForesterUtil.stringListToString( lineage, ">" );
+        UniProtTaxonomy up_tax = null;
+        if ( TaxonomyDataObtainer.getLineageTaxCacheMap().containsKey( lineage_str ) ) {
+            up_tax = TaxonomyDataObtainer.getLineageTaxCacheMap().get( lineage_str ).copy();
+        }
+        else {
+            final List<UniProtTaxonomy> up_taxonomies = getTaxonomiesFromScientificName( lineage
+                    .get( lineage.size() - 1 ) );
+            if ( ( up_taxonomies != null ) && ( up_taxonomies.size() > 0 ) ) {
+                for( final UniProtTaxonomy up_taxonomy : up_taxonomies ) {
+                    boolean match = true;
+                    I: for( int i = 0; i < lineage.size(); ++i ) {
+                        if ( !lineage.get( i ).equalsIgnoreCase( up_taxonomy.getLineage().get( i ) ) ) {
+                            match = false;
+                            break I;
+                        }
+                    }
+                    if ( match ) {
+                        if ( up_tax != null ) {
+                            throw new AncestralTaxonomyInferenceException( "lineage \""
+                                    + ForesterUtil.stringListToString( lineage, " > " ) + "\" is not unique" );
+                        }
+                        up_tax = up_taxonomy;
+                    }
+                }
+                if ( up_tax == null ) {
+                    throw new AncestralTaxonomyInferenceException( "lineage \""
+                            + ForesterUtil.stringListToString( lineage, " > " ) + "\" not found" );
+                }
+                TaxonomyDataObtainer.getLineageTaxCacheMap().put( lineage_str, up_tax );
+                if ( !ForesterUtil.isEmpty( up_tax.getScientificName() ) ) {
+                    TaxonomyDataObtainer.getSnTaxCacheMap().put( up_tax.getScientificName(), up_tax );
+                }
+                if ( !ForesterUtil.isEmpty( up_tax.getCode() ) ) {
+                    TaxonomyDataObtainer.getCodeTaxCacheMap().put( up_tax.getCode(), up_tax );
+                }
+                if ( !ForesterUtil.isEmpty( up_tax.getCommonName() ) ) {
+                    TaxonomyDataObtainer.getCnTaxCacheMap().put( up_tax.getCommonName(), up_tax );
+                }
+                if ( !ForesterUtil.isEmpty( up_tax.getId() ) ) {
+                    TaxonomyDataObtainer.getIdTaxCacheMap().put( up_tax.getId(), up_tax );
+                }
+            }
+        }
+        return up_tax;
     }
-    
-   
+
     synchronized private static void updateTaxonomy( final QUERY_TYPE qt,
                                                      final PhylogenyNode node,
                                                      final Taxonomy tax,
@@ -353,33 +349,140 @@ public class TaxonomyDataObtainer extends RunnableProcess {
             }
         }
     }
+    private final Phylogeny            _phy;
+    private final MainFrameApplication _mf;
+    private final TreePanel            _treepanel;
+    private final boolean              _delete;
 
-
-    synchronized  static HashMap<String, UniProtTaxonomy> getCnTaxCacheMap() {
-        return _cn_up_cache_map;
-    }
-
-    synchronized  static HashMap<String, UniProtTaxonomy> getCodeTaxCacheMap() {
-        return _code_up_cache_map;
+    public TaxonomyDataObtainer( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
+        _phy = phy;
+        _mf = mf;
+        _treepanel = treepanel;
+        _delete = false;
     }
 
-    synchronized  static HashMap<String, UniProtTaxonomy> getIdTaxCacheMap() {
-        return _id_up_cache_map;
+    public TaxonomyDataObtainer( final MainFrameApplication mf,
+                                 final TreePanel treepanel,
+                                 final Phylogeny phy,
+                                 final boolean delete ) {
+        _phy = phy;
+        _mf = mf;
+        _treepanel = treepanel;
+        _delete = delete;
     }
 
-    synchronized  static HashMap<String, UniProtTaxonomy> getSnTaxCacheMap() {
-        return _sn_up_cache_map;
+    private void execute() {
+        start( _mf, "taxonomy data" );
+        SortedSet<String> not_found = null;
+        try {
+            not_found = obtainDetailedTaxonomicInformation( _phy, _delete );
+        }
+        catch ( final UnknownHostException e ) {
+            JOptionPane.showMessageDialog( _mf,
+                                           "Could not connect to \"" + getBaseUrl() + "\"",
+                                           "Network error during taxonomic information gathering",
+                                           JOptionPane.ERROR_MESSAGE );
+            return;
+        }
+        catch ( final IOException e ) {
+            e.printStackTrace();
+            JOptionPane.showMessageDialog( _mf,
+                                           e.toString(),
+                                           "Failed to obtain taxonomic information",
+                                           JOptionPane.ERROR_MESSAGE );
+            return;
+        }
+        catch ( final AncestralTaxonomyInferenceException e ) {
+            e.printStackTrace();
+            JOptionPane.showMessageDialog( _mf,
+                                           e.toString(),
+                                           "Failed to obtain taxonomic information",
+                                           JOptionPane.ERROR_MESSAGE );
+            return;
+        }
+        finally {
+            end( _mf );
+        }
+        if ( ( _phy == null ) || _phy.isEmpty() ) {
+            try {
+                JOptionPane.showMessageDialog( _mf,
+                                               "None of the external node taxonomies could be resolved",
+                                               "Taxonomy Tool Failed",
+                                               JOptionPane.WARNING_MESSAGE );
+            }
+            catch ( final Exception e ) {
+                // Not important if this fails, do nothing. 
+            }
+            return;
+        }
+        _treepanel.setTree( _phy );
+        _mf.showWhole();
+        _treepanel.setEdited( true );
+        if ( ( not_found != null ) && ( not_found.size() > 0 ) ) {
+            int max = not_found.size();
+            boolean more = false;
+            if ( max > 20 ) {
+                more = true;
+                max = 20;
+            }
+            final StringBuffer sb = new StringBuffer();
+            sb.append( "Not all taxonomies could be resolved.\n" );
+            if ( not_found.size() == 1 ) {
+                if ( _delete ) {
+                    sb.append( "The following taxonomy was not found and deleted (if external):\n" );
+                }
+                else {
+                    sb.append( "The following taxonomy was not found:\n" );
+                }
+            }
+            else {
+                if ( _delete ) {
+                    sb.append( "The following taxonomies were not found and deleted (if external) (total: "
+                            + not_found.size() + "):\n" );
+                }
+                else {
+                    sb.append( "The following taxonomies were not found (total: " + not_found.size() + "):\n" );
+                }
+            }
+            int i = 0;
+            for( final String string : not_found ) {
+                if ( i > 19 ) {
+                    break;
+                }
+                sb.append( string );
+                sb.append( "\n" );
+                ++i;
+            }
+            if ( more ) {
+                sb.append( "..." );
+            }
+            try {
+                JOptionPane.showMessageDialog( _mf,
+                                               sb.toString(),
+                                               "Taxonomy Tool Completed",
+                                               JOptionPane.WARNING_MESSAGE );
+            }
+            catch ( final Exception e ) {
+                // Not important if this fails, do nothing. 
+            }
+        }
+        else {
+            try {
+                JOptionPane.showMessageDialog( _mf,
+                                               "Taxonomy tool successfully completed",
+                                               "Taxonomy Tool Completed",
+                                               JOptionPane.INFORMATION_MESSAGE );
+            }
+            catch ( final Exception e ) {
+                // Not important if this fails, do nothing.
+            }
+        }
     }
 
-    synchronized  static HashMap<String, UniProtTaxonomy> getLineageTaxCacheMap() {
-        return _lineage_up_cache_map;
+    private String getBaseUrl() {
+        return UniProtWsTools.BASE_URL;
     }
 
-    
-    enum QUERY_TYPE {
-        CODE, SN, CN, ID, LIN;
-    }
-    
     @Override
     public void run() {
         execute();
index 41b7ce4..5f8aa34 100644 (file)
@@ -63,6 +63,7 @@ import javax.swing.JOptionPane;
 import javax.swing.text.MaskFormatter;
 
 import org.forester.analysis.AncestralTaxonomyInference;
+import org.forester.analysis.TaxonomyDataObtainer;
 import org.forester.io.parsers.PhylogenyParser;
 import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
 import org.forester.io.parsers.tol.TolParser;
@@ -464,7 +465,7 @@ public final class AptxUtil {
                         else {
                             UniProtTaxonomy up = null;
                             try {
-                                up = AncestralTaxonomyInference.obtainUniProtTaxonomy( temp_tax, null, null );
+                                up = TaxonomyDataObtainer.obtainUniProtTaxonomy( temp_tax, null, null );
                             }
                             catch ( final Exception e ) {
                                 e.printStackTrace();