inprogress
[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.MainFrameApplication;
35 import org.forester.archaeopteryx.TreePanel;
36 import org.forester.phylogeny.Phylogeny;
37 import org.forester.ws.seqdb.SequenceDbWsTools;
38
39 public class AncestralTaxonomyInferrer extends RunnableProcess {
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     public static String getBaseUrl() {
52         return SequenceDbWsTools.BASE_UNIPROT_URL;
53     }
54
55     private void inferTaxonomies() {
56         start( _mf, "ancestral taxonomy" );
57         try {
58             AncestralTaxonomyInference.inferTaxonomyFromDescendents( _phy );
59         }
60         catch ( final AncestralTaxonomyInferenceException e ) {
61             end( _mf );
62             JOptionPane.showMessageDialog( _mf,
63                                            e.getMessage(),
64                                            "Error during ancestral taxonomy inference",
65                                            JOptionPane.ERROR_MESSAGE );
66             return;
67         }
68         catch ( final UnknownHostException e ) {
69             end( _mf );
70             JOptionPane.showMessageDialog( _mf,
71                                            "Could not connect to \"" + getBaseUrl() + "\"",
72                                            "Network error during ancestral taxonomy inference",
73                                            JOptionPane.ERROR_MESSAGE );
74             return;
75         }
76         catch ( final Exception e ) {
77             end( _mf );
78             e.printStackTrace();
79             JOptionPane.showMessageDialog( _mf,
80                                            e.toString(),
81                                            "Unexpected exception during ancestral taxonomy inference",
82                                            JOptionPane.ERROR_MESSAGE );
83             return;
84         }
85         catch ( final Error e ) {
86             end( _mf );
87             JOptionPane.showMessageDialog( _mf,
88                                            e.toString(),
89                                            "Unexpected error during ancestral taxonomy inference",
90                                            JOptionPane.ERROR_MESSAGE );
91             return;
92         }
93         _phy.setRerootable( false );
94         _treepanel.setTree( _phy );
95         _mf.showWhole();
96         _treepanel.setEdited( true );
97         end( _mf );
98         try {
99             JOptionPane.showMessageDialog( _mf,
100                                            "Ancestral taxonomy inference successfully completed",
101                                            "Ancestral Taxonomy Inference Completed",
102                                            JOptionPane.INFORMATION_MESSAGE );
103         }
104         catch ( final Exception e ) {
105             // Not important if this fails, do nothing.
106         }
107     }
108
109     @Override
110     public void run() {
111         inferTaxonomies();
112     }
113 }