2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
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.
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.
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
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.archaeopteryx;
29 import java.io.IOException;
30 import java.net.MalformedURLException;
33 import javax.swing.JOptionPane;
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;
49 public class UrlTreeReader implements Runnable {
51 private final MainFrame _main_frame;
52 private final int _webservice_client_index;
54 UrlTreeReader( final MainFrame mf, final int webservice_client_index ) {
56 _webservice_client_index = webservice_client_index;
61 readPhylogeniesFromWebservice();
64 synchronized void readPhylogeniesFromWebservice() {
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, 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+", "" );
78 id = Integer.parseInt( identifier );
80 catch ( final NumberFormatException e ) {
84 JOptionPane.showMessageDialog( _main_frame,
85 "Identifier is expected to be a number",
87 JOptionPane.ERROR_MESSAGE );
92 boolean exception = false;
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();
103 parser = new NexusPhylogeniesParser();
104 ( ( NexusPhylogeniesParser ) parser ).setReplaceUnderscores( true );
107 parser = new NexusPhylogeniesParser();
108 ( ( NexusPhylogeniesParser ) parser ).setReplaceUnderscores( true );
109 ( ( NexusPhylogeniesParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
112 parser = new NexusPhylogeniesParser();
113 ( ( NexusPhylogeniesParser ) parser ).setReplaceUnderscores( true );
114 ( ( NexusPhylogeniesParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
117 parser = new NHXParser();
118 ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
119 ( ( NHXParser ) parser ).setReplaceUnderscores( true );
120 ( ( NHXParser ) parser ).setGuessRootedness( true );
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 );
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 );
136 parser = new NHXParser();
137 ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
138 ( ( NHXParser ) parser ).setReplaceUnderscores( false );
139 ( ( NHXParser ) parser ).setGuessRootedness( true );
142 parser = PhyloXmlParser.createPhyloXmlParserXsdValidating();
145 throw new IllegalArgumentException( "unknown format: " + client.getReturnFormat() );
147 if ( _main_frame.getMainPanel().getCurrentTreePanel() != null ) {
148 _main_frame.getMainPanel().getCurrentTreePanel().setWaitCursor();
151 _main_frame.getMainPanel().setWaitCursor();
153 trees = ForesterUtil.readPhylogeniesFromUrl( url, parser );
155 catch ( final MalformedURLException e ) {
157 JOptionPane.showMessageDialog( _main_frame,
158 "Malformed URL: " + url + "\n" + e.getLocalizedMessage(),
160 JOptionPane.ERROR_MESSAGE );
162 catch ( final IOException e ) {
164 JOptionPane.showMessageDialog( _main_frame,
165 "Could not read from " + url + "\n" + e.getLocalizedMessage(),
166 "Failed to read tree from " + client.getName() + " for " + identifier,
167 JOptionPane.ERROR_MESSAGE );
169 catch ( final NumberFormatException e ) {
171 JOptionPane.showMessageDialog( _main_frame,
172 "Could not read from " + url + "\n" + e.getLocalizedMessage(),
173 "Failed to read tree from " + client.getName() + " for " + identifier,
174 JOptionPane.ERROR_MESSAGE );
176 catch ( final Exception e ) {
179 JOptionPane.showMessageDialog( _main_frame,
180 e.getLocalizedMessage(),
181 "Unexpected Exception",
182 JOptionPane.ERROR_MESSAGE );
185 if ( _main_frame.getCurrentTreePanel() != null ) {
186 _main_frame.getCurrentTreePanel().setArrowCursor();
189 _main_frame.getMainPanel().setArrowCursor();
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 );
199 if ( client.getProcessingInstructions() != null ) {
201 WebserviceUtil.processInstructions( client, phylogeny );
203 catch ( final PhyloXmlDataFormatException e ) {
204 JOptionPane.showMessageDialog( _main_frame,
205 "Error:\n" + e.getLocalizedMessage(),
207 JOptionPane.ERROR_MESSAGE );
210 if ( client.getNodeField() != null ) {
212 PhylogenyMethods.transferNodeNameToField( phylogeny, client.getNodeField(), false );
214 catch ( final PhyloXmlDataFormatException e ) {
215 JOptionPane.showMessageDialog( _main_frame,
216 "Error:\n" + e.getLocalizedMessage(),
218 JOptionPane.ERROR_MESSAGE );
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(),
228 String my_name_for_file = "";
229 if ( !ForesterUtil.isEmpty( phylogeny.getName() ) ) {
230 my_name_for_file = new String( phylogeny.getName() ).replaceAll( " ", "_" );
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() );
238 sb.append( phylogeny.getIdentifier().getValue() );
239 my_name_for_file = new String( sb.toString().replaceAll( " ", "_" ) );
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();
248 else if ( !exception ) {
249 JOptionPane.showMessageDialog( null, ForesterUtil.wordWrap( "Failed to read in tree(s) from [" + url
250 + "]", 80 ), "Error", JOptionPane.ERROR_MESSAGE );
252 _main_frame.getContentPane().repaint();
253 if ( ( trees != null ) && ( trees.length > 0 ) ) {
255 JOptionPane.showMessageDialog( null,
256 ForesterUtil.wordWrap( "Successfully read in " + trees.length
257 + " tree(s) from [" + url + "]", 80 ),
259 JOptionPane.INFORMATION_MESSAGE );
261 catch ( final Exception e ) {
262 // Not important if this fails, do nothing.
264 _main_frame.getContentPane().repaint();
267 _main_frame.activateSaveAllIfNeeded();