in progress
[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.analysis.AncestralTaxonomyInferenceException;
37 import org.forester.archaeopteryx.MainFrameApplication;
38 import org.forester.archaeopteryx.TreePanel;
39 import org.forester.phylogeny.Phylogeny;
40 import org.forester.ws.uniprot.UniProtWsTools;
41
42 public class TaxonomyDataObtainer implements Runnable {
43
44     private final Phylogeny            _phy;
45     private final MainFrameApplication _mf;
46     private final TreePanel            _treepanel;
47     private final boolean              _delete;
48
49     public TaxonomyDataObtainer( final MainFrameApplication mf,
50                                  final TreePanel treepanel,
51                                  final Phylogeny phy,
52                                  final boolean delete ) {
53         _phy = phy;
54         _mf = mf;
55         _treepanel = treepanel;
56         _delete = delete;
57     }
58
59     public TaxonomyDataObtainer( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
60         _phy = phy;
61         _mf = mf;
62         _treepanel = treepanel;
63         _delete = false;
64     }
65
66     private String getBaseUrl() {
67         return UniProtWsTools.BASE_URL;
68     }
69
70     private void execute() {
71         _mf.getMainPanel().getCurrentTreePanel().setWaitCursor();
72         SortedSet<String> not_found = null;
73         try {
74             not_found = AncestralTaxonomyInference.obtainDetailedTaxonomicInformation( _phy, _delete );
75         }
76         catch ( final UnknownHostException e ) {
77             _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
78             JOptionPane.showMessageDialog( _mf,
79                                            "Could not connect to \"" + getBaseUrl() + "\"",
80                                            "Network error during taxonomic information gathering",
81                                            JOptionPane.ERROR_MESSAGE );
82             return;
83         }
84         catch ( final IOException e ) {
85             _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
86             e.printStackTrace();
87             JOptionPane.showMessageDialog( _mf,
88                                            e.toString(),
89                                            "Failed to obtain taxonomic information",
90                                            JOptionPane.ERROR_MESSAGE );
91             return;
92         }
93         catch ( final AncestralTaxonomyInferenceException e ) {
94             _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
95             e.printStackTrace();
96             JOptionPane.showMessageDialog( _mf,
97                                            e.toString(),
98                                            "Failed to obtain taxonomic information",
99                                            JOptionPane.ERROR_MESSAGE );
100             return;
101         }
102         finally {
103             _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
104         }
105         _treepanel.setTree( _phy );
106         _mf.showWhole();
107         _treepanel.setEdited( true );
108         if ( ( not_found != null ) && ( not_found.size() > 0 ) ) {
109             int max = not_found.size();
110             boolean more = false;
111             if ( max > 20 ) {
112                 more = true;
113                 max = 20;
114             }
115             final StringBuffer sb = new StringBuffer();
116             sb.append( "Not all taxonomies could be resolved.\n" );
117             if ( not_found.size() == 1 ) {
118                 if ( _delete ) {
119                     sb.append( "The following taxonomy was not found and deleted (if external):\n" );
120                 }
121                 else {
122                     sb.append( "The following taxonomy was not found:\n" );
123                 }
124             }
125             else {
126                 if ( _delete ) {
127                     sb.append( "The following taxonomies were not found and deleted (if external) (total: "
128                             + not_found.size() + "):\n" );
129                 }
130                 else {
131                     sb.append( "The following taxonomies were not found (total: " + not_found.size() + "):\n" );
132                 }
133             }
134             int i = 0;
135             for( final String string : not_found ) {
136                 if ( i > 19 ) {
137                     break;
138                 }
139                 sb.append( string );
140                 sb.append( "\n" );
141                 ++i;
142             }
143             if ( more ) {
144                 sb.append( "..." );
145             }
146             try {
147                 JOptionPane.showMessageDialog( _mf,
148                                                sb.toString(),
149                                                "Taxonomy Tool Completed",
150                                                JOptionPane.WARNING_MESSAGE );
151             }
152             catch ( final Exception e ) {
153                 // Not important if this fails, do nothing. 
154             }
155         }
156         else {
157             try {
158                 JOptionPane.showMessageDialog( _mf,
159                                                "Taxonomy tool successfully completed",
160                                                "Taxonomy Tool Completed",
161                                                JOptionPane.INFORMATION_MESSAGE );
162             }
163             catch ( final Exception e ) {
164                 // Not important if this fails, do nothing.
165             }
166         }
167     }
168
169     @Override
170     public void run() {
171         execute();
172     }
173 }