refactored
[jalview.git] / forester / java / src / org / forester / archaeopteryx / tools / AncestralTaxonomyInferrer.java
1 // Exp $
2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
4 //
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.archaeopteryx.tools;
27
28 import java.net.UnknownHostException;
29 import java.util.SortedSet;
30
31 import javax.swing.JOptionPane;
32
33 import org.forester.analysis.AncestralTaxonomyInference;
34 import org.forester.archaeopteryx.MainFrameApplication;
35 import org.forester.archaeopteryx.TreePanel;
36 import org.forester.phylogeny.Phylogeny;
37 import org.forester.ws.uniprot.UniProtWsTools;
38
39 public class AncestralTaxonomyInferrer implements Runnable {
40
41     private final Phylogeny            _phy;
42     private final MainFrameApplication _mf;
43     private final TreePanel            _treepanel;
44
45     public AncestralTaxonomyInferrer( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
46         _phy = phy;
47         _mf = mf;
48         _treepanel = treepanel;
49     }
50
51     private String getBaseUrl() {
52         return UniProtWsTools.BASE_URL;
53     }
54
55     private void inferTaxonomies() {
56         _mf.getMainPanel().getCurrentTreePanel().setWaitCursor();
57         SortedSet<String> not_found = null;
58         try {
59             not_found = AncestralTaxonomyInference.inferTaxonomyFromDescendents( _phy );
60         }
61         catch ( final IllegalArgumentException e ) {
62             _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
63             JOptionPane.showMessageDialog( _mf,
64                                            e.getMessage(),
65                                            "Error during ancestral taxonomy inference",
66                                            JOptionPane.ERROR_MESSAGE );
67             return;
68         }
69         catch ( final UnknownHostException e ) {
70             _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
71             JOptionPane.showMessageDialog( _mf,
72                                            "Could not connect to \"" + getBaseUrl() + "\"",
73                                            "Network error during ancestral taxonomy inference",
74                                            JOptionPane.ERROR_MESSAGE );
75             return;
76         }
77         catch ( final Exception e ) {
78             _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
79             e.printStackTrace();
80             JOptionPane.showMessageDialog( _mf,
81                                            e.toString(),
82                                            "Unexpected error during ancestral taxonomy inference",
83                                            JOptionPane.ERROR_MESSAGE );
84             return;
85         }
86         _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
87         _phy.setRerootable( false );
88         _treepanel.setTree( _phy );
89         _mf.showWhole();
90         _treepanel.setEdited( true );
91         if ( ( not_found != null ) && ( not_found.size() > 0 ) ) {
92             int max = not_found.size();
93             boolean more = false;
94             if ( max > 20 ) {
95                 more = true;
96                 max = 20;
97             }
98             final StringBuffer sb = new StringBuffer();
99             sb.append( "Not all taxonomies could be resolved.\n" );
100             sb.append( "The result is incomplete, and, possibly, misleading.\n" );
101             if ( not_found.size() == 1 ) {
102                 sb.append( "The following taxonomy was not found:\n" );
103             }
104             else {
105                 sb.append( "The following taxonomies were not found (total: " + not_found.size() + "):\n" );
106             }
107             int i = 0;
108             for( final String string : not_found ) {
109                 if ( i > 19 ) {
110                     break;
111                 }
112                 sb.append( string );
113                 sb.append( "\n" );
114                 ++i;
115             }
116             if ( more ) {
117                 sb.append( "..." );
118             }
119             try {
120                 JOptionPane.showMessageDialog( _mf,
121                                                sb.toString(),
122                                                "Ancestral Taxonomy Inference Completed",
123                                                JOptionPane.WARNING_MESSAGE );
124             }
125             catch ( final Exception e ) {
126                 // Not important if this fails, do nothing. 
127             }
128         }
129         else {
130             try {
131                 JOptionPane.showMessageDialog( _mf,
132                                                "Ancestral taxonomy inference successfully completed",
133                                                "Ancestral Taxonomy Inference Completed",
134                                                JOptionPane.INFORMATION_MESSAGE );
135             }
136             catch ( final Exception e ) {
137                 // Not important if this fails, do nothing.
138             }
139         }
140     }
141
142     @Override
143     public void run() {
144         inferTaxonomies();
145     }
146 }