ad9ee250d2fff9962c6c2be13388471ce2579a19
[jalview.git] / forester / java / src / org / forester / analysis / TaxonomyDataManager.java
1 // $Id:
2 //
3 // forester -- software libraries and applications
4 // for genomics and evolutionary biology research.
5 //
6 // Copyright (C) 2010 Christian M Zmasek
7 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
8 // All rights reserved
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26
27 package org.forester.analysis;
28
29 import java.io.IOException;
30 import java.net.UnknownHostException;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.SortedSet;
35 import java.util.TreeSet;
36
37 import javax.swing.JOptionPane;
38
39 import org.forester.archaeopteryx.MainFrameApplication;
40 import org.forester.archaeopteryx.TreePanel;
41 import org.forester.archaeopteryx.tools.AncestralTaxonomyInferrer;
42 import org.forester.archaeopteryx.tools.RunnableProcess;
43 import org.forester.io.parsers.nhx.NHXParser;
44 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
45 import org.forester.io.parsers.util.ParserUtils;
46 import org.forester.phylogeny.Phylogeny;
47 import org.forester.phylogeny.PhylogenyNode;
48 import org.forester.phylogeny.data.Identifier;
49 import org.forester.phylogeny.data.Taxonomy;
50 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
51 import org.forester.util.ForesterUtil;
52 import org.forester.util.TaxonomyUtil;
53 import org.forester.ws.seqdb.SequenceDbWsTools;
54 import org.forester.ws.seqdb.UniProtTaxonomy;
55
56 public final class TaxonomyDataManager extends RunnableProcess {
57
58     enum QUERY_TYPE {
59         CODE, SN, CN, ID, LIN;
60     }
61     private static final int                              MAX_CACHE_SIZE           = 100000;
62     private static final int                              MAX_TAXONOMIES_TO_RETURN = 2000;
63     private static final HashMap<String, UniProtTaxonomy> _sn_up_cache_map         = new HashMap<String, UniProtTaxonomy>();
64     private static final HashMap<String, UniProtTaxonomy> _lineage_up_cache_map    = new HashMap<String, UniProtTaxonomy>();
65     private static final HashMap<String, UniProtTaxonomy> _code_up_cache_map       = new HashMap<String, UniProtTaxonomy>();
66     private static final HashMap<String, UniProtTaxonomy> _cn_up_cache_map         = new HashMap<String, UniProtTaxonomy>();
67     private static final HashMap<String, UniProtTaxonomy> _id_up_cache_map         = new HashMap<String, UniProtTaxonomy>();
68     private final Phylogeny                               _phy;
69     private final MainFrameApplication                    _mf;
70     private final TreePanel                               _treepanel;
71     private final boolean                                 _delete;
72     private final boolean                                 _allow_simple_names;
73
74     public TaxonomyDataManager( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
75         _phy = phy;
76         _mf = mf;
77         _treepanel = treepanel;
78         _delete = false;
79         _allow_simple_names = false;
80     }
81
82     public TaxonomyDataManager( final MainFrameApplication mf,
83                                 final TreePanel treepanel,
84                                 final Phylogeny phy,
85                                 final boolean delete,
86                                 final boolean allow_simple_name ) {
87         _phy = phy;
88         _mf = mf;
89         _treepanel = treepanel;
90         _delete = delete;
91         _allow_simple_names = allow_simple_name;
92     }
93
94     synchronized static void clearCachesIfTooLarge() {
95         if ( getSnTaxCacheMap().size() > MAX_CACHE_SIZE ) {
96             getSnTaxCacheMap().clear();
97         }
98         if ( getLineageTaxCacheMap().size() > MAX_CACHE_SIZE ) {
99             getLineageTaxCacheMap().clear();
100         }
101         if ( getCnTaxCacheMap().size() > MAX_CACHE_SIZE ) {
102             getCnTaxCacheMap().clear();
103         }
104         if ( getCodeTaxCacheMap().size() > MAX_CACHE_SIZE ) {
105             getCodeTaxCacheMap().clear();
106         }
107         if ( getIdTaxCacheMap().size() > MAX_CACHE_SIZE ) {
108             getIdTaxCacheMap().clear();
109         }
110     }
111
112     synchronized final static HashMap<String, UniProtTaxonomy> getCnTaxCacheMap() {
113         return _cn_up_cache_map;
114     }
115
116     synchronized final static HashMap<String, UniProtTaxonomy> getCodeTaxCacheMap() {
117         return _code_up_cache_map;
118     }
119
120     synchronized final static HashMap<String, UniProtTaxonomy> getIdTaxCacheMap() {
121         return _id_up_cache_map;
122     }
123
124     synchronized final static HashMap<String, UniProtTaxonomy> getLineageTaxCacheMap() {
125         return _lineage_up_cache_map;
126     }
127
128     synchronized final static HashMap<String, UniProtTaxonomy> getSnTaxCacheMap() {
129         return _sn_up_cache_map;
130     }
131
132     private final static UniProtTaxonomy obtainTaxonomy( final HashMap<String, UniProtTaxonomy> cache,
133                                                          final Object query,
134                                                          final QUERY_TYPE qt ) throws IOException,
135             AncestralTaxonomyInferenceException {
136         if ( cache.containsKey( query ) ) {
137             return cache.get( query ).copy();
138         }
139         else {
140             List<UniProtTaxonomy> up_taxonomies = null;
141             switch ( qt ) {
142                 case ID:
143                     up_taxonomies = getTaxonomiesFromId( ( String ) query );
144                     break;
145                 case CODE:
146                     up_taxonomies = getTaxonomiesFromTaxonomyCode( ( String ) query );
147                     break;
148                 case SN:
149                     up_taxonomies = getTaxonomiesFromScientificName( ( String ) query );
150                     break;
151                 case CN:
152                     up_taxonomies = getTaxonomiesFromCommonName( ( String ) query );
153                     break;
154                 case LIN:
155                     return obtainUniProtTaxonomyFromLineage( ( List<String> ) query );
156                 default:
157                     throw new RuntimeException();
158             }
159             if ( ( up_taxonomies != null ) && ( up_taxonomies.size() == 1 ) ) {
160                 final UniProtTaxonomy up_tax = up_taxonomies.get( 0 );
161                 if ( !ForesterUtil.isEmpty( up_tax.getScientificName() ) ) {
162                     TaxonomyDataManager.getSnTaxCacheMap().put( up_tax.getScientificName(), up_tax );
163                 }
164                 if ( !ForesterUtil.isEmpty( up_tax.getCode() ) ) {
165                     TaxonomyDataManager.getCodeTaxCacheMap().put( up_tax.getCode(), up_tax );
166                 }
167                 if ( !ForesterUtil.isEmpty( up_tax.getCommonName() ) ) {
168                     TaxonomyDataManager.getCnTaxCacheMap().put( up_tax.getCommonName(), up_tax );
169                 }
170                 if ( !ForesterUtil.isEmpty( up_tax.getId() ) ) {
171                     TaxonomyDataManager.getIdTaxCacheMap().put( up_tax.getId(), up_tax );
172                 }
173                 return up_tax;
174             }
175             else {
176                 return null;
177             }
178         }
179     }
180
181     private final static List<UniProtTaxonomy> getTaxonomiesFromCommonName( final String query ) throws IOException {
182         return SequenceDbWsTools.getTaxonomiesFromCommonNameStrict( query, MAX_TAXONOMIES_TO_RETURN );
183     }
184
185     private final static List<UniProtTaxonomy> getTaxonomiesFromId( final String query ) throws IOException {
186         return SequenceDbWsTools.getTaxonomiesFromId( query, MAX_TAXONOMIES_TO_RETURN );
187     }
188
189     private final static List<UniProtTaxonomy> getTaxonomiesFromScientificName( final String query ) throws IOException {
190         if ( query.equalsIgnoreCase( UniProtTaxonomy.BACTERIA ) || query.equalsIgnoreCase( UniProtTaxonomy.ARCHAEA )
191                 || query.equalsIgnoreCase( UniProtTaxonomy.VIRUSES )
192                 || query.equalsIgnoreCase( UniProtTaxonomy.EUKARYOTA ) || query.equalsIgnoreCase( UniProtTaxonomy.X ) ) {
193             final List<UniProtTaxonomy> l = new ArrayList<UniProtTaxonomy>();
194             l.add( UniProtTaxonomy.createSpecialFromScientificName( query ) );
195             return l;
196         }
197         return SequenceDbWsTools.getTaxonomiesFromScientificNameStrict( query, MAX_TAXONOMIES_TO_RETURN );
198     }
199
200     private final static List<UniProtTaxonomy> getTaxonomiesFromTaxonomyCode( final String query ) throws IOException {
201         if ( ( query.indexOf( "XX" ) == 3 ) && TaxonomyUtil.isHasTaxIdFromFakeTaxCode( query ) ) {
202             final int id = TaxonomyUtil.getTaxIdFromFakeTaxCode( query );
203             return SequenceDbWsTools.getTaxonomiesFromId( String.valueOf( id ), MAX_TAXONOMIES_TO_RETURN );
204         }
205         return SequenceDbWsTools.getTaxonomiesFromTaxonomyCode( query, MAX_TAXONOMIES_TO_RETURN );
206     }
207
208     static final boolean isHasAppropriateId( final Taxonomy tax ) {
209         return ( ( tax.getIdentifier() != null ) && ( !ForesterUtil.isEmpty( tax.getIdentifier().getValue() ) && ( tax
210                 .getIdentifier().getProvider().equalsIgnoreCase( "ncbi" )
211                 || tax.getIdentifier().getProvider().equalsIgnoreCase( "uniprot" ) || tax.getIdentifier().getProvider()
212                 .equalsIgnoreCase( "uniprotkb" ) ) ) );
213     }
214
215     synchronized final private static SortedSet<String> obtainDetailedTaxonomicInformation( final Phylogeny phy,
216                                                                                             final boolean delete,
217                                                                                             final boolean allow_to_use_basic_node_names )
218             throws IOException, AncestralTaxonomyInferenceException {
219         clearCachesIfTooLarge();
220         final SortedSet<String> not_found = new TreeSet<String>();
221         List<PhylogenyNode> not_found_external_nodes = null;
222         if ( delete ) {
223             not_found_external_nodes = new ArrayList<PhylogenyNode>();
224         }
225         for( final PhylogenyNodeIterator iter = phy.iteratorPostorder(); iter.hasNext(); ) {
226             final PhylogenyNode node = iter.next();
227             final QUERY_TYPE qt = null;
228             Taxonomy tax = null;
229             if ( node.getNodeData().isHasTaxonomy() ) {
230                 tax = node.getNodeData().getTaxonomy();
231             }
232             else if ( allow_to_use_basic_node_names && !ForesterUtil.isEmpty( node.getName() ) ) {
233                 // Nothing to be done.
234             }
235             else if ( node.isExternal() ) {
236                 if ( !ForesterUtil.isEmpty( node.getName() ) ) {
237                     not_found.add( node.getName() );
238                 }
239                 else {
240                     not_found.add( node.toString() );
241                 }
242                 if ( delete ) {
243                     not_found_external_nodes.add( node );
244                 }
245             }
246             UniProtTaxonomy uniprot_tax = null;
247             if ( ( ( tax != null ) && ( isHasAppropriateId( tax ) || !ForesterUtil.isEmpty( tax.getScientificName() )
248                     || !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) || !ForesterUtil.isEmpty( tax.getCommonName() ) ) )
249                     || ( allow_to_use_basic_node_names && !ForesterUtil.isEmpty( node.getName() ) ) ) {
250                 if ( ( ( tax != null ) && ( isHasAppropriateId( tax )
251                         || !ForesterUtil.isEmpty( tax.getScientificName() )
252                         || !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) || !ForesterUtil
253                         .isEmpty( tax.getCommonName() ) ) ) ) {
254                     uniprot_tax = obtainUniProtTaxonomy( tax, null, qt );
255                 }
256                 else {
257                     uniprot_tax = obtainUniProtTaxonomy( node.getName(), qt );
258                 }
259                 if ( uniprot_tax != null ) {
260                     if ( tax == null ) {
261                         tax = new Taxonomy();
262                         node.getNodeData().addTaxonomy( tax );
263                     }
264                     updateTaxonomy( qt, node, tax, uniprot_tax );
265                 }
266                 else {
267                     if ( tax != null ) {
268                         not_found.add( tax.toString() );
269                     }
270                     else {
271                         not_found.add( node.getName() );
272                     }
273                     if ( delete && node.isExternal() ) {
274                         not_found_external_nodes.add( node );
275                     }
276                 }
277             }
278         }
279         if ( delete ) {
280             for( final PhylogenyNode node : not_found_external_nodes ) {
281                 phy.deleteSubtree( node, true );
282             }
283             phy.externalNodesHaveChanged();
284             phy.clearHashIdToNodeMap();
285             phy.recalculateNumberOfExternalDescendants( true );
286         }
287         return not_found;
288     }
289
290     public final static UniProtTaxonomy obtainUniProtTaxonomy( final Taxonomy tax, Object query, QUERY_TYPE qt )
291             throws IOException, AncestralTaxonomyInferenceException {
292         if ( tax == null ) {
293             throw new IllegalArgumentException( "illegal attempt to use empty taxonomy object" );
294         }
295         if ( TaxonomyDataManager.isHasAppropriateId( tax ) ) {
296             query = tax.getIdentifier().getValue();
297             qt = QUERY_TYPE.ID;
298             return obtainTaxonomy( TaxonomyDataManager.getIdTaxCacheMap(), query, qt );
299         }
300         else if ( !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
301             if ( !ForesterUtil.isEmpty( tax.getLineage() ) ) {
302                 query = tax.getLineage();
303                 qt = QUERY_TYPE.LIN;
304                 return obtainTaxonomy( TaxonomyDataManager.getLineageTaxCacheMap(), query, qt );
305             }
306             else {
307                 query = tax.getScientificName();
308                 qt = QUERY_TYPE.SN;
309                 return obtainTaxonomy( TaxonomyDataManager.getSnTaxCacheMap(), query, qt );
310             }
311         }
312         else if ( !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
313             query = tax.getTaxonomyCode();
314             qt = QUERY_TYPE.CODE;
315             return obtainTaxonomy( TaxonomyDataManager.getCodeTaxCacheMap(), query, qt );
316         }
317         else {
318             query = tax.getCommonName();
319             qt = QUERY_TYPE.CN;
320             return obtainTaxonomy( TaxonomyDataManager.getCnTaxCacheMap(), query, qt );
321         }
322     }
323
324     public final static UniProtTaxonomy obtainUniProtTaxonomy( final String simple_name, QUERY_TYPE qt )
325             throws IOException, AncestralTaxonomyInferenceException {
326         if ( ForesterUtil.isEmpty( simple_name ) ) {
327             throw new IllegalArgumentException( "illegal attempt to use empty simple name" );
328         }
329         UniProtTaxonomy ut = null;
330         final String code = ParserUtils.extractTaxonomyCodeFromNodeName( simple_name,
331                                                                          NHXParser.TAXONOMY_EXTRACTION.AGGRESSIVE );
332         if ( !ForesterUtil.isEmpty( code ) ) {
333             qt = QUERY_TYPE.CODE;
334             ut = obtainTaxonomy( TaxonomyDataManager.getCodeTaxCacheMap(), code, qt );
335         }
336         if ( ut == null ) {
337             final String sn = ParserUtils.extractScientificNameFromNodeName( simple_name );
338             if ( !ForesterUtil.isEmpty( sn ) ) {
339                 qt = QUERY_TYPE.SN;
340                 ut = obtainTaxonomy( TaxonomyDataManager.getSnTaxCacheMap(), sn, qt );
341             }
342         }
343         if ( ut == null ) {
344             final String id = ParserUtils
345                     .extractUniprotTaxonomyIdFromNodeName( simple_name,
346                                                            NHXParser.TAXONOMY_EXTRACTION.PFAM_STYLE_RELAXED );
347             if ( !ForesterUtil.isEmpty( id ) ) {
348                 qt = QUERY_TYPE.ID;
349                 ut = obtainTaxonomy( TaxonomyDataManager.getIdTaxCacheMap(), id, qt );
350             }
351         }
352         //
353         //        qt = QUERY_TYPE.SN;
354         //        UniProtTaxonomy ut = obtainTaxonomy( TaxonomyDataManager.getSnTaxCacheMap(), simple_name, qt );
355         //        if ( ut == null ) {
356         //            qt = QUERY_TYPE.CODE;
357         //            ut = obtainTaxonomy( TaxonomyDataManager.getCodeTaxCacheMap(), simple_name, qt );
358         //        }
359         //        if ( ut == null ) {
360         //            qt = QUERY_TYPE.CN;
361         //            ut = obtainTaxonomy( TaxonomyDataManager.getCnTaxCacheMap(), simple_name, qt );
362         //        }
363         return ut;
364     }
365
366     static final UniProtTaxonomy obtainUniProtTaxonomyFromLineage( final List<String> lineage )
367             throws AncestralTaxonomyInferenceException, IOException {
368         final String lineage_str = ForesterUtil.stringListToString( lineage, ">" );
369         if ( TaxonomyDataManager.getLineageTaxCacheMap().containsKey( lineage_str ) ) {
370             return TaxonomyDataManager.getLineageTaxCacheMap().get( lineage_str ).copy();
371         }
372         else {
373             final List<UniProtTaxonomy> matching_taxonomies = new ArrayList<UniProtTaxonomy>();
374             final List<UniProtTaxonomy> up_taxonomies = getTaxonomiesFromScientificName( lineage
375                     .get( lineage.size() - 1 ) );
376             if ( ( up_taxonomies != null ) && ( up_taxonomies.size() > 0 ) ) {
377                 for( final UniProtTaxonomy up_taxonomy : up_taxonomies ) {
378                     boolean match = true;
379                     I: for( int i = 0; i < lineage.size(); ++i ) {
380                         if ( ( i == up_taxonomy.getLineage().size() )
381                                 || !lineage.get( i ).equalsIgnoreCase( up_taxonomy.getLineage().get( i ) ) ) {
382                             match = false;
383                             break I;
384                         }
385                     }
386                     if ( match ) {
387                         matching_taxonomies.add( up_taxonomy );
388                     }
389                 }
390                 if ( matching_taxonomies.isEmpty() ) {
391                     throw new AncestralTaxonomyInferenceException( "lineage \""
392                             + ForesterUtil.stringListToString( lineage, " > " ) + "\" not found" );
393                 }
394                 //in case of more than one (e.g. "Xenopus" Genus and Subgenus), keep shorter, less specific  one:
395                 int shortest = Integer.MAX_VALUE;
396                 UniProtTaxonomy least_specific_up_tax = null;
397                 for( final UniProtTaxonomy m : matching_taxonomies ) {
398                     final int s = m.getLineage().size();
399                     if ( s < shortest ) {
400                         shortest = s;
401                         least_specific_up_tax = m;
402                     }
403                 }
404                 TaxonomyDataManager.getLineageTaxCacheMap().put( lineage_str, least_specific_up_tax );
405                 if ( !ForesterUtil.isEmpty( least_specific_up_tax.getScientificName() ) ) {
406                     TaxonomyDataManager.getSnTaxCacheMap().put( least_specific_up_tax.getScientificName(),
407                                                                 least_specific_up_tax );
408                 }
409                 if ( !ForesterUtil.isEmpty( least_specific_up_tax.getCode() ) ) {
410                     TaxonomyDataManager.getCodeTaxCacheMap().put( least_specific_up_tax.getCode(),
411                                                                   least_specific_up_tax );
412                 }
413                 if ( !ForesterUtil.isEmpty( least_specific_up_tax.getCommonName() ) ) {
414                     TaxonomyDataManager.getCnTaxCacheMap().put( least_specific_up_tax.getCommonName(),
415                                                                 least_specific_up_tax );
416                 }
417                 if ( !ForesterUtil.isEmpty( least_specific_up_tax.getId() ) ) {
418                     TaxonomyDataManager.getIdTaxCacheMap().put( least_specific_up_tax.getId(), least_specific_up_tax );
419                 }
420                 return least_specific_up_tax;
421             }
422             else {
423                 throw new AncestralTaxonomyInferenceException( "taxonomy \"" + ( lineage.get( lineage.size() - 1 ) )
424                         + "\" not found" );
425             }
426         }
427     }
428
429     synchronized final private static void updateTaxonomy( final QUERY_TYPE qt,
430                                                            final PhylogenyNode node,
431                                                            final Taxonomy tax,
432                                                            final UniProtTaxonomy up_tax )
433             throws PhyloXmlDataFormatException {
434         if ( ( qt != QUERY_TYPE.SN ) && !ForesterUtil.isEmpty( up_tax.getScientificName() )
435                 && ForesterUtil.isEmpty( tax.getScientificName() ) ) {
436             tax.setScientificName( up_tax.getScientificName() );
437         }
438         if ( node.isExternal() && ( qt != QUERY_TYPE.CODE ) && !ForesterUtil.isEmpty( up_tax.getCode() )
439                 && ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
440             tax.setTaxonomyCode( up_tax.getCode() );
441         }
442         if ( ( qt != QUERY_TYPE.CN ) && !ForesterUtil.isEmpty( up_tax.getCommonName() )
443                 && ForesterUtil.isEmpty( tax.getCommonName() ) ) {
444             tax.setCommonName( up_tax.getCommonName() );
445         }
446         if ( !ForesterUtil.isEmpty( up_tax.getSynonym() ) && !tax.getSynonyms().contains( up_tax.getSynonym() ) ) {
447             tax.getSynonyms().add( up_tax.getSynonym() );
448         }
449         if ( !ForesterUtil.isEmpty( up_tax.getRank() ) && ForesterUtil.isEmpty( tax.getRank() ) ) {
450             try {
451                 tax.setRank( up_tax.getRank().toLowerCase() );
452             }
453             catch ( final PhyloXmlDataFormatException ex ) {
454                 tax.setRank( "" );
455             }
456         }
457         if ( ( qt != QUERY_TYPE.ID ) && !ForesterUtil.isEmpty( up_tax.getId() )
458                 && ( ( tax.getIdentifier() == null ) || ForesterUtil.isEmpty( tax.getIdentifier().getValue() ) ) ) {
459             tax.setIdentifier( new Identifier( up_tax.getId(), "uniprot" ) );
460         }
461         if ( up_tax.getLineage() != null ) {
462             tax.setLineage( new ArrayList<String>() );
463             for( final String lin : up_tax.getLineage() ) {
464                 if ( !ForesterUtil.isEmpty( lin ) ) {
465                     tax.getLineage().add( lin );
466                 }
467             }
468         }
469     }
470
471     private final void execute() {
472         start( _mf, "taxonomy data" );
473         SortedSet<String> not_found = null;
474         try {
475             not_found = obtainDetailedTaxonomicInformation( _phy, _delete, _allow_simple_names );
476         }
477         catch ( final UnknownHostException e ) {
478             JOptionPane.showMessageDialog( _mf,
479                                            "Could not connect to \"" + getBaseUrl() + "\"",
480                                            "Network error during taxonomic information gathering",
481                                            JOptionPane.ERROR_MESSAGE );
482             return;
483         }
484         catch ( final IOException e ) {
485             e.printStackTrace();
486             JOptionPane.showMessageDialog( _mf,
487                                            e.toString(),
488                                            "Failed to obtain taxonomic information",
489                                            JOptionPane.ERROR_MESSAGE );
490             return;
491         }
492         catch ( final AncestralTaxonomyInferenceException e ) {
493             e.printStackTrace();
494             JOptionPane.showMessageDialog( _mf,
495                                            e.toString(),
496                                            "Failed to obtain taxonomic information",
497                                            JOptionPane.ERROR_MESSAGE );
498             return;
499         }
500         finally {
501             end( _mf );
502         }
503         if ( ( _phy == null ) || _phy.isEmpty() ) {
504             try {
505                 JOptionPane.showMessageDialog( _mf,
506                                                "None of the external node taxonomies could be resolved",
507                                                "Taxonomy Tool Failed",
508                                                JOptionPane.WARNING_MESSAGE );
509             }
510             catch ( final Exception e ) {
511                 // Not important if this fails, do nothing. 
512             }
513             return;
514         }
515         _treepanel.setTree( _phy );
516         _mf.showWhole();
517         _treepanel.setEdited( true );
518         if ( ( not_found != null ) && ( not_found.size() > 0 ) ) {
519             int max = not_found.size();
520             boolean more = false;
521             if ( max > 20 ) {
522                 more = true;
523                 max = 20;
524             }
525             final StringBuffer sb = new StringBuffer();
526             sb.append( "Not all taxonomies could be resolved.\n" );
527             if ( not_found.size() == 1 ) {
528                 if ( _delete ) {
529                     sb.append( "The following taxonomy was not found and deleted (if external):\n" );
530                 }
531                 else {
532                     sb.append( "The following taxonomy was not found:\n" );
533                 }
534             }
535             else {
536                 if ( _delete ) {
537                     sb.append( "The following taxonomies were not found and deleted (if external) (total: "
538                             + not_found.size() + "):\n" );
539                 }
540                 else {
541                     sb.append( "The following taxonomies were not found (total: " + not_found.size() + "):\n" );
542                 }
543             }
544             int i = 0;
545             for( final String string : not_found ) {
546                 if ( i > 19 ) {
547                     break;
548                 }
549                 sb.append( string );
550                 sb.append( "\n" );
551                 ++i;
552             }
553             if ( more ) {
554                 sb.append( "..." );
555             }
556             try {
557                 JOptionPane.showMessageDialog( _mf,
558                                                sb.toString(),
559                                                "Taxonomy Tool Completed",
560                                                JOptionPane.WARNING_MESSAGE );
561             }
562             catch ( final Exception e ) {
563                 // Not important if this fails, do nothing. 
564             }
565         }
566         else {
567             try {
568                 JOptionPane.showMessageDialog( _mf,
569                                                "Taxonomy tool successfully completed",
570                                                "Taxonomy Tool Completed",
571                                                JOptionPane.INFORMATION_MESSAGE );
572             }
573             catch ( final Exception e ) {
574                 // Not important if this fails, do nothing.
575             }
576         }
577     }
578
579     private final String getBaseUrl() {
580         return AncestralTaxonomyInferrer.getBaseUrl();
581     }
582
583     @Override
584     public void run() {
585         execute();
586     }
587 }