(no commit message)
[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 extends RunnableProcess {
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         start( _mf, "taxonomy data" );
72         SortedSet<String> not_found = null;
73         try {
74             not_found = AncestralTaxonomyInference.obtainDetailedTaxonomicInformation( _phy, _delete );
75         }
76         catch ( final UnknownHostException e ) {
77             JOptionPane.showMessageDialog( _mf,
78                                            "Could not connect to \"" + getBaseUrl() + "\"",
79                                            "Network error during taxonomic information gathering",
80                                            JOptionPane.ERROR_MESSAGE );
81             return;
82         }
83         catch ( final IOException e ) {
84             e.printStackTrace();
85             JOptionPane.showMessageDialog( _mf,
86                                            e.toString(),
87                                            "Failed to obtain taxonomic information",
88                                            JOptionPane.ERROR_MESSAGE );
89             return;
90         }
91         catch ( final AncestralTaxonomyInferenceException e ) {
92             e.printStackTrace();
93             JOptionPane.showMessageDialog( _mf,
94                                            e.toString(),
95                                            "Failed to obtain taxonomic information",
96                                            JOptionPane.ERROR_MESSAGE );
97             return;
98         }
99         finally {
100             end( _mf );
101         }
102         if ( _phy == null || _phy.isEmpty() ) {
103             try {
104                 JOptionPane.showMessageDialog( _mf,
105                                                "None of the external node taxonomies could be resolved",
106                                                "Taxonomy Tool Failed",
107                                                JOptionPane.WARNING_MESSAGE );
108             }
109             catch ( final Exception e ) {
110                 // Not important if this fails, do nothing. 
111             }
112             return;
113         }
114         _treepanel.setTree( _phy );
115         _mf.showWhole();
116         _treepanel.setEdited( true );
117         if ( ( not_found != null ) && ( not_found.size() > 0 ) ) {
118             int max = not_found.size();
119             boolean more = false;
120             if ( max > 20 ) {
121                 more = true;
122                 max = 20;
123             }
124             final StringBuffer sb = new StringBuffer();
125             sb.append( "Not all taxonomies could be resolved.\n" );
126             if ( not_found.size() == 1 ) {
127                 if ( _delete ) {
128                     sb.append( "The following taxonomy was not found and deleted (if external):\n" );
129                 }
130                 else {
131                     sb.append( "The following taxonomy was not found:\n" );
132                 }
133             }
134             else {
135                 if ( _delete ) {
136                     sb.append( "The following taxonomies were not found and deleted (if external) (total: "
137                             + not_found.size() + "):\n" );
138                 }
139                 else {
140                     sb.append( "The following taxonomies were not found (total: " + not_found.size() + "):\n" );
141                 }
142             }
143             int i = 0;
144             for( final String string : not_found ) {
145                 if ( i > 19 ) {
146                     break;
147                 }
148                 sb.append( string );
149                 sb.append( "\n" );
150                 ++i;
151             }
152             if ( more ) {
153                 sb.append( "..." );
154             }
155             try {
156                 JOptionPane.showMessageDialog( _mf,
157                                                sb.toString(),
158                                                "Taxonomy Tool Completed",
159                                                JOptionPane.WARNING_MESSAGE );
160             }
161             catch ( final Exception e ) {
162                 // Not important if this fails, do nothing. 
163             }
164         }
165         else {
166             try {
167                 JOptionPane.showMessageDialog( _mf,
168                                                "Taxonomy tool successfully completed",
169                                                "Taxonomy Tool Completed",
170                                                JOptionPane.INFORMATION_MESSAGE );
171             }
172             catch ( final Exception e ) {
173                 // Not important if this fails, do nothing.
174             }
175         }
176     }
177
178     @Override
179     public void run() {
180         execute();
181     }
182 }