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