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;
62 synchronized void readPhylogeniesFromWebservice() {
63 final long start_time = new Date().getTime();
65 Phylogeny[] trees = null;
66 final WebservicesManager webservices_manager = WebservicesManager.getInstance();
67 final PhylogeniesWebserviceClient client = webservices_manager
68 .getAvailablePhylogeniesWebserviceClient( _webservice_client_index );
69 String identifier = JOptionPane.showInputDialog( _main_frame, client.getInstructions() + "\n(Reference: "
70 + client.getReference() + ")", client.getDescription(), JOptionPane.QUESTION_MESSAGE );
71 if ( ( identifier != null ) && ( identifier.trim().length() > 0 ) ) {
72 identifier = identifier.trim();
73 if ( client.isQueryInteger() ) {
74 identifier = identifier.replaceAll( "^\\D+", "" );
77 id = Integer.parseInt( identifier );
79 catch ( final NumberFormatException e ) {
83 JOptionPane.showMessageDialog( _main_frame,
84 "Identifier is expected to be a number",
86 JOptionPane.ERROR_MESSAGE );
92 String url_str = client.getUrl();
93 url_str = url_str.replaceFirst( PhylogeniesWebserviceClient.QUERY_PLACEHOLDER, identifier );
94 url = new URL( url_str );
95 PhylogenyParser parser = null;
96 switch ( client.getReturnFormat() ) {
97 case TOL_XML_RESPONSE:
98 parser = new TolParser();
101 parser = new NexusPhylogeniesParser();
102 ( ( NexusPhylogeniesParser ) parser ).setReplaceUnderscores( true );
105 parser = new NHXParser();
106 ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
107 ( ( NHXParser ) parser ).setReplaceUnderscores( true );
108 ( ( NHXParser ) parser ).setGuessRootedness( true );
110 case NH_EXTRACT_TAXONOMY:
111 parser = new NHXParser();
112 ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.PFAM_STYLE_ONLY );
113 ( ( NHXParser ) parser ).setReplaceUnderscores( false );
114 ( ( NHXParser ) parser ).setGuessRootedness( true );
117 parser = new NHXParser();
118 ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.PFAM_STYLE_ONLY );
119 ( ( NHXParser ) parser ).setReplaceUnderscores( false );
120 ( ( NHXParser ) parser ).setGuessRootedness( true );
123 parser = new NHXParser();
124 ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
125 ( ( NHXParser ) parser ).setReplaceUnderscores( false );
126 ( ( NHXParser ) parser ).setGuessRootedness( true );
129 parser = new PhyloXmlParser();
132 throw new IllegalArgumentException( "unknown format: " + client.getReturnFormat() );
134 if ( _main_frame.getMainPanel().getCurrentTreePanel() != null ) {
135 _main_frame.getMainPanel().getCurrentTreePanel().setWaitCursor();
138 _main_frame.getMainPanel().setWaitCursor();
140 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
141 trees = factory.create( url.openStream(), parser );
143 catch ( final MalformedURLException e ) {
144 JOptionPane.showMessageDialog( _main_frame,
145 "Malformed URL: " + url + "\n" + e.getLocalizedMessage(),
147 JOptionPane.ERROR_MESSAGE );
149 catch ( final IOException e ) {
150 JOptionPane.showMessageDialog( _main_frame,
151 "Could not read from " + url + "\n" + e.getLocalizedMessage(),
152 "Failed to read tree from " + client.getName() + " for " + identifier,
153 JOptionPane.ERROR_MESSAGE );
155 catch ( final NumberFormatException e ) {
156 JOptionPane.showMessageDialog( _main_frame,
157 "Could not read from " + url + "\n" + e.getLocalizedMessage(),
158 "Failed to read tree from " + client.getName() + " for " + identifier,
159 JOptionPane.ERROR_MESSAGE );
161 catch ( final Exception e ) {
163 JOptionPane.showMessageDialog( _main_frame,
164 e.getLocalizedMessage(),
165 "Unexpected Exception",
166 JOptionPane.ERROR_MESSAGE );
169 if ( _main_frame.getCurrentTreePanel() != null ) {
170 _main_frame.getCurrentTreePanel().setArrowCursor();
173 _main_frame.getMainPanel().setArrowCursor();
176 if ( ( trees != null ) && ( trees.length > 0 ) ) {
177 for( final Phylogeny phylogeny : trees ) {
178 if ( !phylogeny.isEmpty() ) {
179 if ( client.getName().equals( WebserviceUtil.TREE_FAM_NAME ) ) {
180 phylogeny.setRerootable( false );
181 phylogeny.setRooted( true );
183 if ( client.getName().equals( WebserviceUtil.PFAM_NAME ) ) {
184 phylogeny.setRerootable( false );
185 phylogeny.setRooted( true );
186 PhylogenyMethods.transferInternalNodeNamesToConfidence( phylogeny );
188 if ( client.getProcessingInstructions() != null ) {
190 WebserviceUtil.processInstructions( client, phylogeny );
192 catch ( final PhyloXmlDataFormatException e ) {
193 JOptionPane.showMessageDialog( _main_frame,
194 "Error:\n" + e.getLocalizedMessage(),
196 JOptionPane.ERROR_MESSAGE );
199 if ( client.getNodeField() != null ) {
201 PhylogenyMethods.transferNodeNameToField( phylogeny, client.getNodeField(), false );
203 catch ( final PhyloXmlDataFormatException e ) {
204 JOptionPane.showMessageDialog( _main_frame,
205 "Error:\n" + e.getLocalizedMessage(),
207 JOptionPane.ERROR_MESSAGE );
210 phylogeny.setIdentifier( new Identifier( identifier, client.getName() ) );
211 _main_frame.getJMenuBar().remove( _main_frame.getHelpMenu() );
212 _main_frame.getMenuBarOfMainFrame().add( _main_frame.getHelpMenu() );
213 _main_frame.getMainPanel().addPhylogenyInNewTab( phylogeny,
214 _main_frame.getConfiguration(),
215 new File( url.getFile() ).getName(),
217 String my_name_for_file = "";
218 if ( !ForesterUtil.isEmpty( phylogeny.getName() ) ) {
219 my_name_for_file = new String( phylogeny.getName() ).replaceAll( " ", "_" );
221 else if ( phylogeny.getIdentifier() != null ) {
222 final StringBuffer sb = new StringBuffer();
223 if ( !ForesterUtil.isEmpty( phylogeny.getIdentifier().getProvider() ) ) {
224 sb.append( phylogeny.getIdentifier().getProvider() );
227 sb.append( phylogeny.getIdentifier().getValue() );
228 my_name_for_file = new String( sb.toString().replaceAll( " ", "_" ) );
230 _main_frame.getMainPanel().getCurrentTreePanel().setTreeFile( new File( my_name_for_file ) );
231 AptxUtil.lookAtSomeTreePropertiesForAptxControlSettings( phylogeny, _main_frame.getMainPanel()
232 .getControlPanel(), _main_frame.getConfiguration() );
233 _main_frame.getMainPanel().getControlPanel().showWhole();
237 _main_frame.getContentPane().repaint();
238 if ( ( ( trees != null ) && ( trees.length > 0 ) ) && ( ( new Date().getTime() - start_time ) > 20000 ) ) {
240 JOptionPane.showMessageDialog( null,
241 ForesterUtil.wordWrap( "Successfully read in " + trees.length
242 + " evolutionry tree(s) from [" + url + "]", 80 ),
244 JOptionPane.INFORMATION_MESSAGE );
246 catch ( final Exception e ) {
247 // Not important if this fails, do nothing.
249 _main_frame.getContentPane().repaint();
252 _main_frame.activateSaveAllIfNeeded();
258 readPhylogeniesFromWebservice();