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