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