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.phylogeny.factories.ParserBasedPhylogenyFactory;
48 import org.forester.phylogeny.factories.PhylogenyFactory;
49 import org.forester.util.ForesterUtil;
51 public class UrlTreeReader implements Runnable {
53 private final MainFrame _main_frame;
54 private final int _webservice_client_index;
56 UrlTreeReader( final MainFrame mf, final int webservice_client_index ) {
58 _webservice_client_index = webservice_client_index;
63 readPhylogeniesFromWebservice();
66 synchronized void readPhylogeniesFromWebservice() {
68 Phylogeny[] trees = null;
69 final WebservicesManager webservices_manager = WebservicesManager.getInstance();
70 final PhylogeniesWebserviceClient client = webservices_manager
71 .getAvailablePhylogeniesWebserviceClient( _webservice_client_index );
72 String identifier = JOptionPane.showInputDialog( _main_frame, client.getInstructions() + "\n(Reference: "
73 + client.getReference() + ")", client.getDescription(), JOptionPane.QUESTION_MESSAGE );
74 if ( ( identifier != null ) && ( identifier.trim().length() > 0 ) ) {
75 identifier = identifier.trim();
76 if ( client.isQueryInteger() ) {
77 identifier = identifier.replaceAll( "^\\D+", "" );
80 id = Integer.parseInt( identifier );
82 catch ( final NumberFormatException e ) {
86 JOptionPane.showMessageDialog( _main_frame,
87 "Identifier is expected to be a number",
89 JOptionPane.ERROR_MESSAGE );
94 boolean exception = false;
96 String url_str = client.getUrl();
97 url_str = url_str.replaceFirst( PhylogeniesWebserviceClient.QUERY_PLACEHOLDER, identifier );
98 url = new URL( url_str );
99 PhylogenyParser parser = null;
100 switch ( client.getReturnFormat() ) {
101 case TOL_XML_RESPONSE:
102 parser = new TolParser();
105 parser = new NexusPhylogeniesParser();
106 ( ( NexusPhylogeniesParser ) parser ).setReplaceUnderscores( true );
109 parser = new NexusPhylogeniesParser();
110 ( ( NexusPhylogeniesParser ) parser ).setReplaceUnderscores( true );
111 ( ( NexusPhylogeniesParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
114 parser = new NexusPhylogeniesParser();
115 ( ( NexusPhylogeniesParser ) parser ).setReplaceUnderscores( true );
116 ( ( NexusPhylogeniesParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
119 parser = new NHXParser();
120 ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
121 ( ( NHXParser ) parser ).setReplaceUnderscores( true );
122 ( ( NHXParser ) parser ).setGuessRootedness( true );
124 case NH_EXTRACT_TAXONOMY:
125 parser = new NHXParser();
126 ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.AGGRESSIVE );
127 ( ( NHXParser ) parser ).setReplaceUnderscores( false );
128 ( ( NHXParser ) parser ).setGuessRootedness( true );
131 parser = new NHXParser();
132 ( ( NHXParser ) parser )
133 .setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.PFAM_STYLE_STRICT );
134 ( ( NHXParser ) parser ).setReplaceUnderscores( false );
135 ( ( NHXParser ) parser ).setGuessRootedness( true );
138 parser = new NHXParser();
139 ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
140 ( ( NHXParser ) parser ).setReplaceUnderscores( false );
141 ( ( NHXParser ) parser ).setGuessRootedness( true );
144 parser = PhyloXmlParser.createPhyloXmlParserXsdValidating();
147 throw new IllegalArgumentException( "unknown format: " + client.getReturnFormat() );
149 if ( _main_frame.getMainPanel().getCurrentTreePanel() != null ) {
150 _main_frame.getMainPanel().getCurrentTreePanel().setWaitCursor();
153 _main_frame.getMainPanel().setWaitCursor();
155 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
156 trees = factory.create( url.openStream(), parser );
158 catch ( final MalformedURLException e ) {
160 JOptionPane.showMessageDialog( _main_frame,
161 "Malformed URL: " + url + "\n" + e.getLocalizedMessage(),
163 JOptionPane.ERROR_MESSAGE );
165 catch ( final IOException e ) {
167 JOptionPane.showMessageDialog( _main_frame,
168 "Could not read from " + url + "\n" + e.getLocalizedMessage(),
169 "Failed to read tree from " + client.getName() + " for " + identifier,
170 JOptionPane.ERROR_MESSAGE );
172 catch ( final NumberFormatException e ) {
174 JOptionPane.showMessageDialog( _main_frame,
175 "Could not read from " + url + "\n" + e.getLocalizedMessage(),
176 "Failed to read tree from " + client.getName() + " for " + identifier,
177 JOptionPane.ERROR_MESSAGE );
179 catch ( final Exception e ) {
182 JOptionPane.showMessageDialog( _main_frame,
183 e.getLocalizedMessage(),
184 "Unexpected Exception",
185 JOptionPane.ERROR_MESSAGE );
188 if ( _main_frame.getCurrentTreePanel() != null ) {
189 _main_frame.getCurrentTreePanel().setArrowCursor();
192 _main_frame.getMainPanel().setArrowCursor();
195 if ( ( trees != null ) && ( trees.length > 0 ) ) {
196 for( final Phylogeny phylogeny : trees ) {
197 if ( !phylogeny.isEmpty() ) {
198 if ( client.getName().equals( WebserviceUtil.TREE_FAM_NAME ) ) {
199 phylogeny.setRerootable( false );
200 phylogeny.setRooted( true );
202 if ( client.getProcessingInstructions() != null ) {
204 WebserviceUtil.processInstructions( client, phylogeny );
206 catch ( final PhyloXmlDataFormatException e ) {
207 JOptionPane.showMessageDialog( _main_frame,
208 "Error:\n" + e.getLocalizedMessage(),
210 JOptionPane.ERROR_MESSAGE );
213 if ( client.getNodeField() != null ) {
215 PhylogenyMethods.transferNodeNameToField( phylogeny, client.getNodeField(), false );
217 catch ( final PhyloXmlDataFormatException e ) {
218 JOptionPane.showMessageDialog( _main_frame,
219 "Error:\n" + e.getLocalizedMessage(),
221 JOptionPane.ERROR_MESSAGE );
224 phylogeny.setIdentifier( new Identifier( identifier, client.getName() ) );
225 _main_frame.getJMenuBar().remove( _main_frame.getHelpMenu() );
226 _main_frame.getMenuBarOfMainFrame().add( _main_frame.getHelpMenu() );
227 _main_frame.getMainPanel().addPhylogenyInNewTab( phylogeny,
228 _main_frame.getConfiguration(),
229 new File( url.getFile() ).getName(),
231 String my_name_for_file = "";
232 if ( !ForesterUtil.isEmpty( phylogeny.getName() ) ) {
233 my_name_for_file = new String( phylogeny.getName() ).replaceAll( " ", "_" );
235 else if ( phylogeny.getIdentifier() != null ) {
236 final StringBuffer sb = new StringBuffer();
237 if ( !ForesterUtil.isEmpty( phylogeny.getIdentifier().getProvider() ) ) {
238 sb.append( phylogeny.getIdentifier().getProvider() );
241 sb.append( phylogeny.getIdentifier().getValue() );
242 my_name_for_file = new String( sb.toString().replaceAll( " ", "_" ) );
244 _main_frame.getMainPanel().getCurrentTreePanel().setTreeFile( new File( my_name_for_file ) );
245 AptxUtil.lookAtSomeTreePropertiesForAptxControlSettings( phylogeny, _main_frame.getMainPanel()
246 .getControlPanel(), _main_frame.getConfiguration() );
247 _main_frame.getMainPanel().getControlPanel().showWhole();
251 else if ( !exception ) {
252 JOptionPane.showMessageDialog( null, ForesterUtil.wordWrap( "Failed to read in tree(s) from [" + url
253 + "]", 80 ), "Error", JOptionPane.ERROR_MESSAGE );
255 _main_frame.getContentPane().repaint();
256 if ( ( trees != null ) && ( trees.length > 0 ) ) {
258 JOptionPane.showMessageDialog( null,
259 ForesterUtil.wordWrap( "Successfully read in " + trees.length
260 + " tree(s) from [" + url + "]", 80 ),
262 JOptionPane.INFORMATION_MESSAGE );
264 catch ( final Exception e ) {
265 // Not important if this fails, do nothing.
267 _main_frame.getContentPane().repaint();
270 _main_frame.activateSaveAllIfNeeded();