JAL-2797 the big switch, all GUI method calls now go via the AptxFrame
[jalview.git] / forester / java / src / org / forester / archaeopteryx / UrlTreeReader.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
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;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.net.MalformedURLException;
31 import java.net.URL;
32
33 import javax.swing.JOptionPane;
34
35 import org.forester.archaeopteryx.webservices.PhylogeniesWebserviceClient;
36 import org.forester.archaeopteryx.webservices.WebserviceUtil;
37 import org.forester.archaeopteryx.webservices.WebservicesManager;
38 import org.forester.io.parsers.PhylogenyParser;
39 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
40 import org.forester.io.parsers.nhx.NHXParser;
41 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
42 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
43 import org.forester.io.parsers.tol.TolParser;
44 import org.forester.phylogeny.Phylogeny;
45 import org.forester.phylogeny.PhylogenyMethods;
46 import org.forester.phylogeny.data.Identifier;
47 import org.forester.util.ForesterUtil;
48
49 public class UrlTreeReader implements Runnable {
50
51     private final MainFrame _main_frame;
52     private final int       _webservice_client_index;
53
54     UrlTreeReader( final MainFrame mf, final int webservice_client_index ) {
55         _main_frame = mf;
56         _webservice_client_index = webservice_client_index;
57     }
58
59     @Override
60     public void run() {
61         readPhylogeniesFromWebservice();
62     }
63
64     synchronized void readPhylogeniesFromWebservice() {
65         URL url = null;
66         Phylogeny[] trees = null;
67         final WebservicesManager webservices_manager = WebservicesManager.getInstance();
68         final PhylogeniesWebserviceClient client = webservices_manager
69                 .getAvailablePhylogeniesWebserviceClient( _webservice_client_index );
70         String identifier = JOptionPane.showInputDialog( _main_frame.getThisFrame(), client.getInstructions() + "\n(Reference: "
71                 + client.getReference() + ")", client.getDescription(), JOptionPane.QUESTION_MESSAGE );
72         if ( ( identifier != null ) && ( identifier.trim().length() > 0 ) ) {
73             identifier = identifier.trim();
74             if ( client.isQueryInteger() ) {
75                 identifier = identifier.replaceAll( "^\\D+", "" );
76                 int id = -1;
77                 try {
78                     id = Integer.parseInt( identifier );
79                 }
80                 catch ( final NumberFormatException e ) {
81                     id = -1;
82                 }
83                 if ( id < 1 ) {
84                     JOptionPane.showMessageDialog( _main_frame.getThisFrame(),
85                                                    "Identifier is expected to be a number",
86                                                    "Can not open URL",
87                                                    JOptionPane.ERROR_MESSAGE );
88                     return;
89                 }
90                 identifier = id + "";
91             }
92             boolean exception = false;
93             try {
94                 String url_str = client.getUrl();
95                 url_str = url_str.replaceFirst( PhylogeniesWebserviceClient.QUERY_PLACEHOLDER, identifier );
96                 url = new URL( url_str );
97                 PhylogenyParser parser = null;
98                 switch ( client.getReturnFormat() ) {
99                     case TOL_XML_RESPONSE:
100                         parser = new TolParser();
101                         break;
102                     case NEXUS:
103                         parser = new NexusPhylogeniesParser();
104                         ( ( NexusPhylogeniesParser ) parser ).setReplaceUnderscores( true );
105                         break;
106                     case TREEBASE_TREE:
107                         parser = new NexusPhylogeniesParser();
108                         ( ( NexusPhylogeniesParser ) parser ).setReplaceUnderscores( true );
109                         ( ( NexusPhylogeniesParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
110                         break;
111                     case TREEBASE_STUDY:
112                         parser = new NexusPhylogeniesParser();
113                         ( ( NexusPhylogeniesParser ) parser ).setReplaceUnderscores( true );
114                         ( ( NexusPhylogeniesParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
115                         break;
116                     case NH:
117                         parser = new NHXParser();
118                         ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
119                         ( ( NHXParser ) parser ).setReplaceUnderscores( true );
120                         ( ( NHXParser ) parser ).setGuessRootedness( true );
121                         break;
122                     case NH_EXTRACT_TAXONOMY:
123                         parser = new NHXParser();
124                         ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.AGGRESSIVE );
125                         ( ( NHXParser ) parser ).setReplaceUnderscores( false );
126                         ( ( NHXParser ) parser ).setGuessRootedness( true );
127                         break;
128                     case PFAM:
129                         parser = new NHXParser();
130                         ( ( NHXParser ) parser )
131                                 .setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.PFAM_STYLE_STRICT );
132                         ( ( NHXParser ) parser ).setReplaceUnderscores( false );
133                         ( ( NHXParser ) parser ).setGuessRootedness( true );
134                         break;
135                     case NHX:
136                         parser = new NHXParser();
137                         ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
138                         ( ( NHXParser ) parser ).setReplaceUnderscores( false );
139                         ( ( NHXParser ) parser ).setGuessRootedness( true );
140                         break;
141                     case PHYLOXML:
142                         parser = PhyloXmlParser.createPhyloXmlParserXsdValidating();
143                         break;
144                     default:
145                         throw new IllegalArgumentException( "unknown format: " + client.getReturnFormat() );
146                 }
147                 if ( _main_frame.getMainPanel().getCurrentTreePanel() != null ) {
148                     _main_frame.getMainPanel().getCurrentTreePanel().setWaitCursor();
149                 }
150                 else {
151                     _main_frame.getMainPanel().setWaitCursor();
152                 }
153                 trees = ForesterUtil.readPhylogeniesFromUrl( url, parser );
154             }
155             catch ( final MalformedURLException e ) {
156                 exception = true;
157                 JOptionPane.showMessageDialog( _main_frame.getThisFrame(),
158                                                "Malformed URL: " + url + "\n" + e.getLocalizedMessage(),
159                                                "Malformed URL",
160                                                JOptionPane.ERROR_MESSAGE );
161             }
162             catch ( final IOException e ) {
163                 exception = true;
164                 JOptionPane.showMessageDialog( _main_frame.getThisFrame(),
165                                                "Could not read from " + url + "\n" + e.getLocalizedMessage(),
166                                                "Failed to read tree from " + client.getName() + " for " + identifier,
167                                                JOptionPane.ERROR_MESSAGE );
168             }
169             catch ( final NumberFormatException e ) {
170                 exception = true;
171                 JOptionPane.showMessageDialog( _main_frame.getThisFrame(),
172                                                "Could not read from " + url + "\n" + e.getLocalizedMessage(),
173                                                "Failed to read tree from " + client.getName() + " for " + identifier,
174                                                JOptionPane.ERROR_MESSAGE );
175             }
176             catch ( final Exception e ) {
177                 exception = true;
178                 e.printStackTrace();
179                 JOptionPane.showMessageDialog( _main_frame.getThisFrame(),
180                                                e.getLocalizedMessage(),
181                                                "Unexpected Exception",
182                                                JOptionPane.ERROR_MESSAGE );
183             }
184             finally {
185                 if ( _main_frame.getCurrentTreePanel() != null ) {
186                     _main_frame.getCurrentTreePanel().setArrowCursor();
187                 }
188                 else {
189                     _main_frame.getMainPanel().setArrowCursor();
190                 }
191             }
192             if ( ( trees != null ) && ( trees.length > 0 ) ) {
193                 for( final Phylogeny phylogeny : trees ) {
194                     if ( !phylogeny.isEmpty() ) {
195                         if ( client.getName().equals( WebserviceUtil.TREE_FAM_NAME ) ) {
196                             phylogeny.setRerootable( false );
197                             phylogeny.setRooted( true );
198                         }
199                         if ( client.getProcessingInstructions() != null ) {
200                             try {
201                                 WebserviceUtil.processInstructions( client, phylogeny );
202                             }
203                             catch ( final PhyloXmlDataFormatException e ) {
204                                 JOptionPane.showMessageDialog( _main_frame.getThisFrame(),
205                                                                "Error:\n" + e.getLocalizedMessage(),
206                                                                "Error",
207                                                                JOptionPane.ERROR_MESSAGE );
208                             }
209                         }
210                         if ( client.getNodeField() != null ) {
211                             try {
212                                 PhylogenyMethods.transferNodeNameToField( phylogeny, client.getNodeField(), false );
213                             }
214                             catch ( final PhyloXmlDataFormatException e ) {
215                                 JOptionPane.showMessageDialog( _main_frame.getThisFrame(),
216                                                                "Error:\n" + e.getLocalizedMessage(),
217                                                                "Error",
218                                                                JOptionPane.ERROR_MESSAGE );
219                             }
220                         }
221                         phylogeny.setIdentifier( new Identifier( identifier, client.getName() ) );
222                         _main_frame.getJMenuBar().remove( _main_frame.getHelpMenu() );
223                         _main_frame.getMenuBarOfMainFrame().add( _main_frame.getHelpMenu() );
224                         _main_frame.getMainPanel().addPhylogenyInNewTab( phylogeny,
225                                                                          _main_frame.getConfiguration(),
226                                                                          new File( url.getFile() ).getName(),
227                                                                          url.toString() );
228                         String my_name_for_file = "";
229                         if ( !ForesterUtil.isEmpty( phylogeny.getName() ) ) {
230                             my_name_for_file = new String( phylogeny.getName() ).replaceAll( " ", "_" );
231                         }
232                         else if ( phylogeny.getIdentifier() != null ) {
233                             final StringBuffer sb = new StringBuffer();
234                             if ( !ForesterUtil.isEmpty( phylogeny.getIdentifier().getProvider() ) ) {
235                                 sb.append( phylogeny.getIdentifier().getProvider() );
236                                 sb.append( "_" );
237                             }
238                             sb.append( phylogeny.getIdentifier().getValue() );
239                             my_name_for_file = new String( sb.toString().replaceAll( " ", "_" ) );
240                         }
241                         _main_frame.getMainPanel().getCurrentTreePanel().setTreeFile( new File( my_name_for_file ) );
242                         AptxUtil.lookAtSomeTreePropertiesForAptxControlSettings( phylogeny, _main_frame.getMainPanel()
243                                 .getControlPanel(), _main_frame.getConfiguration() );
244                         _main_frame.getMainPanel().getControlPanel().showWhole();
245                     }
246                 }
247             }
248             else if ( !exception ) {
249                 JOptionPane.showMessageDialog( null, ForesterUtil.wordWrap( "Failed to read in tree(s) from [" + url
250                         + "]", 80 ), "Error", JOptionPane.ERROR_MESSAGE );
251             }
252             _main_frame.getContentPane().repaint();
253             if ( ( trees != null ) && ( trees.length > 0 ) ) {
254                 try {
255                     JOptionPane.showMessageDialog( null,
256                                                    ForesterUtil.wordWrap( "Successfully read in " + trees.length
257                                                            + " tree(s) from [" + url + "]", 80 ),
258                                                    "Success",
259                                                    JOptionPane.INFORMATION_MESSAGE );
260                 }
261                 catch ( final Exception e ) {
262                     // Not important if this fails, do nothing.
263                 }
264                 _main_frame.getContentPane().repaint();
265             }
266         }
267         _main_frame.activateSaveAllIfNeeded();
268         System.gc();
269     }
270 }