added a close button
[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: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.archaeopteryx.tools;
27
28 import java.net.UnknownHostException;
29
30 import javax.swing.JOptionPane;
31
32 import org.forester.analysis.AncestralTaxonomyInference;
33 import org.forester.analysis.AncestralTaxonomyInferenceException;
34 import org.forester.archaeopteryx.MainFrame;
35 import org.forester.archaeopteryx.MainFrameApplication;
36 import org.forester.archaeopteryx.TreePanel;
37 import org.forester.phylogeny.Phylogeny;
38 import org.forester.ws.seqdb.SequenceDbWsTools;
39
40 public class AncestralTaxonomyInferrer extends RunnableProcess {
41
42     private final Phylogeny            _phy;
43     private final MainFrame _mf;
44     private final TreePanel            _treepanel;
45
46     public AncestralTaxonomyInferrer( final MainFrame mf, final TreePanel treepanel, final Phylogeny phy ) {
47         _phy = phy;
48         _mf = mf;
49         _treepanel = treepanel;
50     }
51
52     public static String getBaseUrl() {
53         return SequenceDbWsTools.BASE_UNIPROT_URL;
54     }
55
56     private void inferTaxonomies() {
57         start( _mf, "ancestral taxonomy" );
58         try {
59             AncestralTaxonomyInference.inferTaxonomyFromDescendents( _phy );
60         }
61         catch ( final AncestralTaxonomyInferenceException e ) {
62             end( _mf );
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             end( _mf );
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             end( _mf );
79             e.printStackTrace();
80             JOptionPane.showMessageDialog( _mf,
81                                            e.toString(),
82                                            "Unexpected exception during ancestral taxonomy inference",
83                                            JOptionPane.ERROR_MESSAGE );
84             return;
85         }
86         catch ( final Error e ) {
87             end( _mf );
88             JOptionPane.showMessageDialog( _mf,
89                                            e.toString(),
90                                            "Unexpected error during ancestral taxonomy inference",
91                                            JOptionPane.ERROR_MESSAGE );
92             return;
93         }
94         _phy.setRerootable( false );
95         _treepanel.setTree( _phy );
96         _mf.showWhole();
97         _treepanel.setEdited( true );
98         end( _mf );
99         try {
100             JOptionPane.showMessageDialog( _mf,
101                                            "Ancestral taxonomy inference successfully completed",
102                                            "Ancestral Taxonomy Inference Completed",
103                                            JOptionPane.INFORMATION_MESSAGE );
104         }
105         catch ( final Exception e ) {
106             // Not important if this fails, do nothing.
107         }
108     }
109
110     @Override
111     public void run() {
112         inferTaxonomies();
113     }
114 }