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