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.net.ssl.HttpsURLConnection;
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.archaeopteryx.webservices.WebservicesManager.WsPhylogenyFormat;
40 import org.forester.io.parsers.PhylogenyParser;
41 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
42 import org.forester.io.parsers.nhx.NHXParser;
43 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
44 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
45 import org.forester.io.parsers.tol.TolParser;
46 import org.forester.phylogeny.Phylogeny;
47 import org.forester.phylogeny.PhylogenyMethods;
48 import org.forester.phylogeny.data.Identifier;
49 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
50 import org.forester.phylogeny.factories.PhylogenyFactory;
51 import org.forester.util.ForesterUtil;
52 import org.forester.util.TrustManager;
54 public class UrlTreeReader implements Runnable {
56 private final MainFrame _main_frame;
57 private final int _webservice_client_index;
59 UrlTreeReader( final MainFrame mf, final int webservice_client_index ) {
61 _webservice_client_index = webservice_client_index;
66 readPhylogeniesFromWebservice();
69 synchronized void readPhylogeniesFromWebservice() {
71 Phylogeny[] trees = null;
72 final WebservicesManager webservices_manager = WebservicesManager.getInstance();
73 final PhylogeniesWebserviceClient client = webservices_manager
74 .getAvailablePhylogeniesWebserviceClient( _webservice_client_index );
75 String identifier = JOptionPane.showInputDialog( _main_frame, client.getInstructions() + "\n(Reference: "
76 + client.getReference() + ")", client.getDescription(), JOptionPane.QUESTION_MESSAGE );
77 if ( ( identifier != null ) && ( identifier.trim().length() > 0 ) ) {
78 identifier = identifier.trim();
79 if ( client.isQueryInteger() ) {
80 identifier = identifier.replaceAll( "^\\D+", "" );
83 id = Integer.parseInt( identifier );
85 catch ( final NumberFormatException e ) {
89 JOptionPane.showMessageDialog( _main_frame,
90 "Identifier is expected to be a number",
92 JOptionPane.ERROR_MESSAGE );
97 boolean exception = false;
99 String url_str = client.getUrl();
100 url_str = url_str.replaceFirst( PhylogeniesWebserviceClient.QUERY_PLACEHOLDER, identifier );
101 url = new URL( url_str );
102 PhylogenyParser parser = null;
103 switch ( client.getReturnFormat() ) {
104 case TOL_XML_RESPONSE:
105 parser = new TolParser();
108 parser = new NexusPhylogeniesParser();
109 ( ( NexusPhylogeniesParser ) parser ).setReplaceUnderscores( true );
112 parser = new NexusPhylogeniesParser();
113 ( ( NexusPhylogeniesParser ) parser ).setReplaceUnderscores( true );
114 ( ( NexusPhylogeniesParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
117 parser = new NexusPhylogeniesParser();
118 ( ( NexusPhylogeniesParser ) parser ).setReplaceUnderscores( true );
119 ( ( NexusPhylogeniesParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
122 parser = new NHXParser();
123 ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
124 ( ( NHXParser ) parser ).setReplaceUnderscores( true );
125 ( ( NHXParser ) parser ).setGuessRootedness( true );
127 case NH_EXTRACT_TAXONOMY:
128 parser = new NHXParser();
129 ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.AGGRESSIVE );
130 ( ( NHXParser ) parser ).setReplaceUnderscores( false );
131 ( ( NHXParser ) parser ).setGuessRootedness( true );
134 parser = new NHXParser();
135 ( ( NHXParser ) parser )
136 .setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.PFAM_STYLE_STRICT );
137 ( ( NHXParser ) parser ).setReplaceUnderscores( false );
138 ( ( NHXParser ) parser ).setGuessRootedness( true );
141 parser = new NHXParser();
142 ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
143 ( ( NHXParser ) parser ).setReplaceUnderscores( false );
144 ( ( NHXParser ) parser ).setGuessRootedness( true );
147 parser = PhyloXmlParser.createPhyloXmlParserXsdValidating();
150 throw new IllegalArgumentException( "unknown format: " + client.getReturnFormat() );
152 if ( _main_frame.getMainPanel().getCurrentTreePanel() != null ) {
153 _main_frame.getMainPanel().getCurrentTreePanel().setWaitCursor();
156 _main_frame.getMainPanel().setWaitCursor();
158 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
160 if (client.getReturnFormat()== WebservicesManager.WsPhylogenyFormat.TREEBASE_TREE
161 || client.getReturnFormat()== WebservicesManager.WsPhylogenyFormat.TREEBASE_STUDY) {
163 final HttpsURLConnection con = TrustManager.makeHttpsURLConnection( url );
164 trees = factory.create( con.getInputStream(), parser );
167 trees = factory.create( url.openStream(), parser );
170 catch ( final MalformedURLException e ) {
172 JOptionPane.showMessageDialog( _main_frame,
173 "Malformed URL: " + url + "\n" + e.getLocalizedMessage(),
175 JOptionPane.ERROR_MESSAGE );
177 catch ( final IOException e ) {
179 JOptionPane.showMessageDialog( _main_frame,
180 "Could not read from " + url + "\n" + e.getLocalizedMessage(),
181 "Failed to read tree from " + client.getName() + " for " + identifier,
182 JOptionPane.ERROR_MESSAGE );
184 catch ( final NumberFormatException e ) {
186 JOptionPane.showMessageDialog( _main_frame,
187 "Could not read from " + url + "\n" + e.getLocalizedMessage(),
188 "Failed to read tree from " + client.getName() + " for " + identifier,
189 JOptionPane.ERROR_MESSAGE );
191 catch ( final Exception e ) {
194 JOptionPane.showMessageDialog( _main_frame,
195 e.getLocalizedMessage(),
196 "Unexpected Exception",
197 JOptionPane.ERROR_MESSAGE );
200 if ( _main_frame.getCurrentTreePanel() != null ) {
201 _main_frame.getCurrentTreePanel().setArrowCursor();
204 _main_frame.getMainPanel().setArrowCursor();
207 if ( ( trees != null ) && ( trees.length > 0 ) ) {
208 for( final Phylogeny phylogeny : trees ) {
209 if ( !phylogeny.isEmpty() ) {
210 if ( client.getName().equals( WebserviceUtil.TREE_FAM_NAME ) ) {
211 phylogeny.setRerootable( false );
212 phylogeny.setRooted( true );
214 if ( client.getProcessingInstructions() != null ) {
216 WebserviceUtil.processInstructions( client, phylogeny );
218 catch ( final PhyloXmlDataFormatException e ) {
219 JOptionPane.showMessageDialog( _main_frame,
220 "Error:\n" + e.getLocalizedMessage(),
222 JOptionPane.ERROR_MESSAGE );
225 if ( client.getNodeField() != null ) {
227 PhylogenyMethods.transferNodeNameToField( phylogeny, client.getNodeField(), false );
229 catch ( final PhyloXmlDataFormatException e ) {
230 JOptionPane.showMessageDialog( _main_frame,
231 "Error:\n" + e.getLocalizedMessage(),
233 JOptionPane.ERROR_MESSAGE );
236 phylogeny.setIdentifier( new Identifier( identifier, client.getName() ) );
237 _main_frame.getJMenuBar().remove( _main_frame.getHelpMenu() );
238 _main_frame.getMenuBarOfMainFrame().add( _main_frame.getHelpMenu() );
239 _main_frame.getMainPanel().addPhylogenyInNewTab( phylogeny,
240 _main_frame.getConfiguration(),
241 new File( url.getFile() ).getName(),
243 String my_name_for_file = "";
244 if ( !ForesterUtil.isEmpty( phylogeny.getName() ) ) {
245 my_name_for_file = new String( phylogeny.getName() ).replaceAll( " ", "_" );
247 else if ( phylogeny.getIdentifier() != null ) {
248 final StringBuffer sb = new StringBuffer();
249 if ( !ForesterUtil.isEmpty( phylogeny.getIdentifier().getProvider() ) ) {
250 sb.append( phylogeny.getIdentifier().getProvider() );
253 sb.append( phylogeny.getIdentifier().getValue() );
254 my_name_for_file = new String( sb.toString().replaceAll( " ", "_" ) );
256 _main_frame.getMainPanel().getCurrentTreePanel().setTreeFile( new File( my_name_for_file ) );
257 AptxUtil.lookAtSomeTreePropertiesForAptxControlSettings( phylogeny, _main_frame.getMainPanel()
258 .getControlPanel(), _main_frame.getConfiguration() );
259 _main_frame.getMainPanel().getControlPanel().showWhole();
263 else if ( !exception ) {
264 JOptionPane.showMessageDialog( null, ForesterUtil.wordWrap( "Failed to read in tree(s) from [" + url
265 + "]", 80 ), "Error", JOptionPane.ERROR_MESSAGE );
267 _main_frame.getContentPane().repaint();
268 if ( ( trees != null ) && ( trees.length > 0 ) ) {
270 JOptionPane.showMessageDialog( null,
271 ForesterUtil.wordWrap( "Successfully read in " + trees.length
272 + " tree(s) from [" + url + "]", 80 ),
274 JOptionPane.INFORMATION_MESSAGE );
276 catch ( final Exception e ) {
277 // Not important if this fails, do nothing.
279 _main_frame.getContentPane().repaint();
282 _main_frame.activateSaveAllIfNeeded();