inprogress
[jalview.git] / forester / java / src / org / forester / archaeopteryx / tools / SequenceDataRetriver.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.io.IOException;
29 import java.net.UnknownHostException;
30 import java.util.SortedSet;
31
32 import javax.swing.JOptionPane;
33
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 final class SequenceDataRetriver extends RunnableProcess {
40
41     private final Phylogeny            _phy;
42     private final MainFrameApplication _mf;
43     private final TreePanel            _treepanel;
44     public final static boolean        DEBUG = false;
45
46     public SequenceDataRetriver( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
47         _phy = phy;
48         _mf = mf;
49         _treepanel = treepanel;
50     }
51
52     @Override
53     public void run() {
54         execute();
55     }
56
57     private void execute() {
58         start( _mf, "sequence data" );
59         SortedSet<String> not_found = null;
60         try {
61             not_found = SequenceDbWsTools.obtainSeqInformation( _phy,
62                                                                 false,
63                                                                 true,
64                                                                 SequenceDbWsTools.DEFAULT_LINES_TO_RETURN );
65         }
66         catch ( final UnknownHostException e ) {
67             JOptionPane.showMessageDialog( _mf,
68                                            e.getLocalizedMessage(),
69                                            "Network error during sequence data gathering",
70                                            JOptionPane.ERROR_MESSAGE );
71             return;
72         }
73         catch ( final IOException e ) {
74             e.printStackTrace();
75             JOptionPane.showMessageDialog( _mf,
76                                            e.toString(),
77                                            "Failed to obtain sequence data",
78                                            JOptionPane.ERROR_MESSAGE );
79             return;
80         }
81         finally {
82             end( _mf );
83         }
84         _treepanel.setTree( _phy );
85         _mf.showWhole();
86         _treepanel.setEdited( true );
87         if ( ( not_found != null ) && ( not_found.size() > 0 ) ) {
88             int max = not_found.size();
89             boolean more = false;
90             if ( max > 20 ) {
91                 more = true;
92                 max = 20;
93             }
94             final StringBuffer sb = new StringBuffer();
95             if ( not_found.size() == 1 ) {
96                 sb.append( "For the following node no data was found:\n" );
97             }
98             else {
99                 sb.append( "For the following nodes no data was found (total: " + not_found.size() + "):\n" );
100             }
101             int i = 0;
102             for( final String string : not_found ) {
103                 if ( i > 19 ) {
104                     break;
105                 }
106                 sb.append( string );
107                 sb.append( "\n" );
108                 ++i;
109             }
110             if ( more ) {
111                 sb.append( "..." );
112             }
113             try {
114                 JOptionPane.showMessageDialog( _mf,
115                                                sb.toString(),
116                                                "Sequence Tool Completed",
117                                                JOptionPane.WARNING_MESSAGE );
118             }
119             catch ( final Exception e ) {
120                 // Not important if this fails, do nothing. 
121             }
122         }
123         else {
124             try {
125                 JOptionPane.showMessageDialog( _mf,
126                                                "Sequence tool successfully completed",
127                                                "Sequence Tool Completed",
128                                                JOptionPane.INFORMATION_MESSAGE );
129             }
130             catch ( final Exception e ) {
131                 // Not important if this fails, do nothing.
132             }
133         }
134     }
135 }