refactored
[jalview.git] / forester / java / src / org / forester / archaeopteryx / tools / TaxonomyDataObtainer.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: www.phylosoft.org/forester
26
27 package org.forester.archaeopteryx.tools;
28
29 import java.io.IOException;
30 import java.net.UnknownHostException;
31 import java.util.SortedSet;
32
33 import javax.swing.JOptionPane;
34
35 import org.forester.analysis.AncestralTaxonomyInference;
36 import org.forester.archaeopteryx.MainFrameApplication;
37 import org.forester.archaeopteryx.TreePanel;
38 import org.forester.phylogeny.Phylogeny;
39 import org.forester.ws.uniprot.UniProtWsTools;
40
41 public class TaxonomyDataObtainer implements Runnable {
42
43     private final Phylogeny            _phy;
44     private final MainFrameApplication _mf;
45     private final TreePanel            _treepanel;
46
47     public TaxonomyDataObtainer( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
48         _phy = phy;
49         _mf = mf;
50         _treepanel = treepanel;
51     }
52
53     private String getBaseUrl() {
54         return UniProtWsTools.BASE_URL;
55     }
56
57     private void execute() {
58         _mf.getMainPanel().getCurrentTreePanel().setWaitCursor();
59         SortedSet<String> not_found = null;
60         try {
61             not_found = AncestralTaxonomyInference.obtainDetailedTaxonomicInformation( _phy );
62         }
63         catch ( final UnknownHostException e ) {
64             _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
65             JOptionPane.showMessageDialog( _mf,
66                                            "Could not connect to \"" + getBaseUrl() + "\"",
67                                            "Network error during taxonomic information gathering",
68                                            JOptionPane.ERROR_MESSAGE );
69             return;
70         }
71         catch ( final IOException e ) {
72             _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
73             e.printStackTrace();
74             JOptionPane.showMessageDialog( _mf,
75                                            e.toString(),
76                                            "Failed to obtain taxonomic information",
77                                            JOptionPane.ERROR_MESSAGE );
78             return;
79         }
80         finally {
81             _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
82         }
83         _treepanel.setTree( _phy );
84         _mf.showWhole();
85         _treepanel.setEdited( true );
86         if ( ( not_found != null ) && ( not_found.size() > 0 ) ) {
87             int max = not_found.size();
88             boolean more = false;
89             if ( max > 20 ) {
90                 more = true;
91                 max = 20;
92             }
93             final StringBuffer sb = new StringBuffer();
94             sb.append( "Not all taxonomies could be resolved.\n" );
95             if ( not_found.size() == 1 ) {
96                 sb.append( "The following taxonomy was not found:\n" );
97             }
98             else {
99                 sb.append( "The following taxonomies were not found (total: " + not_found.size() + "):\n" );
100             }
101             int i = 0;
102             for( final String string : not_found ) {
103                 if ( i > 19 ) {
104                     break;
105                 }
106                 sb.append( string );
107                 sb.append( "\n" );
108                 ++i;
109             }
110             if ( more ) {
111                 sb.append( "..." );
112             }
113             try {
114                 JOptionPane.showMessageDialog( _mf,
115                                                sb.toString(),
116                                                "Taxonomy Tool Completed",
117                                                JOptionPane.WARNING_MESSAGE );
118             }
119             catch ( final Exception e ) {
120                 // Not important if this fails, do nothing. 
121             }
122         }
123         else {
124             try {
125                 JOptionPane.showMessageDialog( _mf,
126                                                "Taxonomy tool successfully completed",
127                                                "Taxonomy Tool Completed",
128                                                JOptionPane.INFORMATION_MESSAGE );
129             }
130             catch ( final Exception e ) {
131                 // Not important if this fails, do nothing.
132             }
133         }
134     }
135
136     @Override
137     public void run() {
138         execute();
139     }
140 }