in progress
[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
30 import javax.swing.JOptionPane;
31
32 import org.forester.analysis.AncestralTaxonomyInference;
33 import org.forester.analysis.AncestralTaxonomyInferenceException;
34 import org.forester.archaeopteryx.Constants;
35 import org.forester.archaeopteryx.MainFrameApplication;
36 import org.forester.archaeopteryx.TreePanel;
37 import org.forester.phylogeny.Phylogeny;
38 import org.forester.util.ForesterUtil;
39 import org.forester.ws.uniprot.UniProtWsTools;
40
41 public class AncestralTaxonomyInferrer implements Runnable {
42
43     private final Phylogeny            _phy;
44     private final MainFrameApplication _mf;
45     private final TreePanel            _treepanel;
46     private long                       _process_id;
47
48     private long getProcessId() {
49         return _process_id;
50     }
51
52     private void setProcessId( final long process_id ) {
53         _process_id = process_id;
54     }
55
56     public AncestralTaxonomyInferrer( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
57         _phy = phy;
58         _mf = mf;
59         _treepanel = treepanel;
60     }
61
62     private String getBaseUrl() {
63         return UniProtWsTools.BASE_URL;
64     }
65
66     private void start() {
67         _mf.getMainPanel().getCurrentTreePanel().setWaitCursor();
68         setProcessId( _mf.getProcessPool().addProcess( "ancestral taxonomy" ) );
69     }
70
71     private void end() {
72         final boolean removed = _mf.getProcessPool().removeProcess( getProcessId() );
73         if ( !removed ) {
74             ForesterUtil.printWarningMessage( Constants.PRG_NAME, "could not remove process " + getProcessId()
75                     + " from process pool" );
76         }
77         _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
78     }
79
80     private void inferTaxonomies() {
81         start();
82         try {
83             AncestralTaxonomyInference.inferTaxonomyFromDescendents( _phy );
84         }
85         catch ( final AncestralTaxonomyInferenceException e ) {
86             end();
87             JOptionPane.showMessageDialog( _mf,
88                                            e.getMessage(),
89                                            "Error during ancestral taxonomy inference",
90                                            JOptionPane.ERROR_MESSAGE );
91             return;
92         }
93         catch ( final UnknownHostException e ) {
94             end();
95             JOptionPane.showMessageDialog( _mf,
96                                            "Could not connect to \"" + getBaseUrl() + "\"",
97                                            "Network error during ancestral taxonomy inference",
98                                            JOptionPane.ERROR_MESSAGE );
99             return;
100         }
101         catch ( final Exception e ) {
102             end();
103             e.printStackTrace();
104             JOptionPane.showMessageDialog( _mf,
105                                            e.toString(),
106                                            "Unexpected exception during ancestral taxonomy inference",
107                                            JOptionPane.ERROR_MESSAGE );
108             return;
109         }
110         catch ( final Error e ) {
111             end();
112             e.printStackTrace();
113             JOptionPane.showMessageDialog( _mf,
114                                            e.toString(),
115                                            "Unexpected error during ancestral taxonomy inference",
116                                            JOptionPane.ERROR_MESSAGE );
117             return;
118         }
119         _phy.setRerootable( false );
120         _treepanel.setTree( _phy );
121         _mf.showWhole();
122         _treepanel.setEdited( true );
123         end();
124         try {
125             JOptionPane.showMessageDialog( _mf,
126                                            "Ancestral taxonomy inference successfully completed",
127                                            "Ancestral Taxonomy Inference Completed",
128                                            JOptionPane.INFORMATION_MESSAGE );
129         }
130         catch ( final Exception e ) {
131             // Not important if this fails, do nothing.
132         }
133     }
134
135     @Override
136     public void run() {
137         inferTaxonomies();
138     }
139 }