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;
32 import java.util.Date;
34 import javax.swing.JOptionPane;
36 import org.forester.archaeopteryx.webservices.PhylogeniesWebserviceClient;
37 import org.forester.archaeopteryx.webservices.WebserviceUtil;
38 import org.forester.archaeopteryx.webservices.WebservicesManager;
39 import org.forester.io.parsers.PhylogenyParser;
40 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
41 import org.forester.io.parsers.nhx.NHXParser;
42 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
43 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
44 import org.forester.io.parsers.tol.TolParser;
45 import org.forester.phylogeny.Phylogeny;
46 import org.forester.phylogeny.PhylogenyMethods;
47 import org.forester.phylogeny.data.Identifier;
48 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
49 import org.forester.phylogeny.factories.PhylogenyFactory;
50 import org.forester.util.ForesterUtil;
52 public class UrlTreeReader implements Runnable {
54 private final MainFrame _main_frame;
55 private final int _webservice_client_index;
57 UrlTreeReader( final MainFrame mf, final int webservice_client_index ) {
59 _webservice_client_index = webservice_client_index;
64 readPhylogeniesFromWebservice();
67 synchronized void readPhylogeniesFromWebservice() {
68 final long start_time = new Date().getTime();
70 Phylogeny[] trees = null;
71 final WebservicesManager webservices_manager = WebservicesManager.getInstance();
72 final PhylogeniesWebserviceClient client = webservices_manager
73 .getAvailablePhylogeniesWebserviceClient( _webservice_client_index );
74 String identifier = JOptionPane.showInputDialog( _main_frame, client.getInstructions() + "\n(Reference: "
75 + client.getReference() + ")", client.getDescription(), JOptionPane.QUESTION_MESSAGE );
76 if ( ( identifier != null ) && ( identifier.trim().length() > 0 ) ) {
77 identifier = identifier.trim();
78 if ( client.isQueryInteger() ) {
79 identifier = identifier.replaceAll( "^\\D+", "" );
82 id = Integer.parseInt( identifier );
84 catch ( final NumberFormatException e ) {
88 JOptionPane.showMessageDialog( _main_frame,
89 "Identifier is expected to be a number",
91 JOptionPane.ERROR_MESSAGE );
96 boolean exception = false;
98 String url_str = client.getUrl();
99 url_str = url_str.replaceFirst( PhylogeniesWebserviceClient.QUERY_PLACEHOLDER, identifier );
100 url = new URL( url_str );
101 PhylogenyParser parser = null;
102 switch ( client.getReturnFormat() ) {
103 case TOL_XML_RESPONSE:
104 parser = new TolParser();
107 parser = new NexusPhylogeniesParser();
108 ( ( NexusPhylogeniesParser ) parser ).setReplaceUnderscores( true );
111 parser = new NexusPhylogeniesParser();
112 ( ( NexusPhylogeniesParser ) parser ).setReplaceUnderscores( true );
113 ( ( NexusPhylogeniesParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
116 parser = new NexusPhylogeniesParser();
117 ( ( NexusPhylogeniesParser ) parser ).setReplaceUnderscores( true );
118 ( ( NexusPhylogeniesParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
121 parser = new NHXParser();
122 ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
123 ( ( NHXParser ) parser ).setReplaceUnderscores( true );
124 ( ( NHXParser ) parser ).setGuessRootedness( true );
126 case NH_EXTRACT_TAXONOMY:
127 parser = new NHXParser();
128 ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.AGGRESSIVE );
129 ( ( NHXParser ) parser ).setReplaceUnderscores( false );
130 ( ( NHXParser ) parser ).setGuessRootedness( true );
133 parser = new NHXParser();
134 ( ( NHXParser ) parser )
135 .setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.PFAM_STYLE_STRICT );
136 ( ( NHXParser ) parser ).setReplaceUnderscores( false );
137 ( ( NHXParser ) parser ).setGuessRootedness( true );
140 parser = new NHXParser();
141 ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
142 ( ( NHXParser ) parser ).setReplaceUnderscores( false );
143 ( ( NHXParser ) parser ).setGuessRootedness( true );
146 parser = PhyloXmlParser.createPhyloXmlParserXsdValidating();
149 throw new IllegalArgumentException( "unknown format: " + client.getReturnFormat() );
151 if ( _main_frame.getMainPanel().getCurrentTreePanel() != null ) {
152 _main_frame.getMainPanel().getCurrentTreePanel().setWaitCursor();
155 _main_frame.getMainPanel().setWaitCursor();
157 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
158 trees = factory.create( url.openStream(), parser );
160 catch ( final MalformedURLException e ) {
162 JOptionPane.showMessageDialog( _main_frame,
163 "Malformed URL: " + url + "\n" + e.getLocalizedMessage(),
165 JOptionPane.ERROR_MESSAGE );
167 catch ( final IOException e ) {
169 JOptionPane.showMessageDialog( _main_frame,
170 "Could not read from " + url + "\n" + e.getLocalizedMessage(),
171 "Failed to read tree from " + client.getName() + " for " + identifier,
172 JOptionPane.ERROR_MESSAGE );
174 catch ( final NumberFormatException e ) {
176 JOptionPane.showMessageDialog( _main_frame,
177 "Could not read from " + url + "\n" + e.getLocalizedMessage(),
178 "Failed to read tree from " + client.getName() + " for " + identifier,
179 JOptionPane.ERROR_MESSAGE );
181 catch ( final Exception e ) {
184 JOptionPane.showMessageDialog( _main_frame,
185 e.getLocalizedMessage(),
186 "Unexpected Exception",
187 JOptionPane.ERROR_MESSAGE );
190 if ( _main_frame.getCurrentTreePanel() != null ) {
191 _main_frame.getCurrentTreePanel().setArrowCursor();
194 _main_frame.getMainPanel().setArrowCursor();
197 if ( ( trees != null ) && ( trees.length > 0 ) ) {
198 for( final Phylogeny phylogeny : trees ) {
199 if ( !phylogeny.isEmpty() ) {
200 if ( client.getName().equals( WebserviceUtil.TREE_FAM_NAME ) ) {
201 phylogeny.setRerootable( false );
202 phylogeny.setRooted( true );
204 if ( client.getProcessingInstructions() != null ) {
206 WebserviceUtil.processInstructions( client, phylogeny );
208 catch ( final PhyloXmlDataFormatException e ) {
209 JOptionPane.showMessageDialog( _main_frame,
210 "Error:\n" + e.getLocalizedMessage(),
212 JOptionPane.ERROR_MESSAGE );
215 if ( client.getNodeField() != null ) {
217 PhylogenyMethods.transferNodeNameToField( phylogeny, client.getNodeField(), false );
219 catch ( final PhyloXmlDataFormatException e ) {
220 JOptionPane.showMessageDialog( _main_frame,
221 "Error:\n" + e.getLocalizedMessage(),
223 JOptionPane.ERROR_MESSAGE );
226 phylogeny.setIdentifier( new Identifier( identifier, client.getName() ) );
227 _main_frame.getJMenuBar().remove( _main_frame.getHelpMenu() );
228 _main_frame.getMenuBarOfMainFrame().add( _main_frame.getHelpMenu() );
229 _main_frame.getMainPanel().addPhylogenyInNewTab( phylogeny,
230 _main_frame.getConfiguration(),
231 new File( url.getFile() ).getName(),
233 String my_name_for_file = "";
234 if ( !ForesterUtil.isEmpty( phylogeny.getName() ) ) {
235 my_name_for_file = new String( phylogeny.getName() ).replaceAll( " ", "_" );
237 else if ( phylogeny.getIdentifier() != null ) {
238 final StringBuffer sb = new StringBuffer();
239 if ( !ForesterUtil.isEmpty( phylogeny.getIdentifier().getProvider() ) ) {
240 sb.append( phylogeny.getIdentifier().getProvider() );
243 sb.append( phylogeny.getIdentifier().getValue() );
244 my_name_for_file = new String( sb.toString().replaceAll( " ", "_" ) );
246 _main_frame.getMainPanel().getCurrentTreePanel().setTreeFile( new File( my_name_for_file ) );
247 AptxUtil.lookAtSomeTreePropertiesForAptxControlSettings( phylogeny, _main_frame.getMainPanel()
248 .getControlPanel(), _main_frame.getConfiguration() );
249 _main_frame.getMainPanel().getControlPanel().showWhole();
253 else if ( !exception ) {
254 JOptionPane.showMessageDialog( null, ForesterUtil.wordWrap( "Failed to read in tree(s) from [" + url
255 + "]", 80 ), "Error", JOptionPane.ERROR_MESSAGE );
257 _main_frame.getContentPane().repaint();
258 if ( ( trees != null ) && ( trees.length > 0 ) ) {
260 JOptionPane.showMessageDialog( null,
261 ForesterUtil.wordWrap( "Successfully read in " + trees.length
262 + " tree(s) from [" + url + "]", 80 ),
264 JOptionPane.INFORMATION_MESSAGE );
266 catch ( final Exception e ) {
267 // Not important if this fails, do nothing.
269 _main_frame.getContentPane().repaint();
272 _main_frame.activateSaveAllIfNeeded();