in progress
[jalview.git] / forester / java / src / org / forester / archaeopteryx / UrlTreeReader.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
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.
13 //
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.
18 //
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
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.archaeopteryx;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.net.MalformedURLException;
31 import java.net.URL;
32
33 import javax.swing.JOptionPane;
34
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;
50
51 public class UrlTreeReader implements Runnable {
52
53     private final MainFrame _main_frame;
54     private final int       _webservice_client_index;
55
56     UrlTreeReader( final MainFrame mf, final int webservice_client_index ) {
57         _main_frame = mf;
58         _webservice_client_index = webservice_client_index;
59     }
60
61     @Override
62     public void run() {
63         readPhylogeniesFromWebservice();
64     }
65
66     synchronized void readPhylogeniesFromWebservice() {
67         URL url = null;
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+", "" );
78                 int id = -1;
79                 try {
80                     id = Integer.parseInt( identifier );
81                 }
82                 catch ( final NumberFormatException e ) {
83                     id = -1;
84                 }
85                 if ( id < 1 ) {
86                     JOptionPane.showMessageDialog( _main_frame,
87                                                    "Identifier is expected to be a number",
88                                                    "Can not open URL",
89                                                    JOptionPane.ERROR_MESSAGE );
90                     return;
91                 }
92                 identifier = id + "";
93             }
94             boolean exception = false;
95             try {
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();
103                         break;
104                     case NEXUS:
105                         parser = new NexusPhylogeniesParser();
106                         ( ( NexusPhylogeniesParser ) parser ).setReplaceUnderscores( true );
107                         break;
108                     case TREEBASE_TREE:
109                         parser = new NexusPhylogeniesParser();
110                         ( ( NexusPhylogeniesParser ) parser ).setReplaceUnderscores( true );
111                         ( ( NexusPhylogeniesParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
112                         break;
113                     case TREEBASE_STUDY:
114                         parser = new NexusPhylogeniesParser();
115                         ( ( NexusPhylogeniesParser ) parser ).setReplaceUnderscores( true );
116                         ( ( NexusPhylogeniesParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
117                         break;
118                     case NH:
119                         parser = new NHXParser();
120                         ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
121                         ( ( NHXParser ) parser ).setReplaceUnderscores( true );
122                         ( ( NHXParser ) parser ).setGuessRootedness( true );
123                         break;
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 );
129                         break;
130                     case PFAM:
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 );
136                         break;
137                     case NHX:
138                         parser = new NHXParser();
139                         ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
140                         ( ( NHXParser ) parser ).setReplaceUnderscores( false );
141                         ( ( NHXParser ) parser ).setGuessRootedness( true );
142                         break;
143                     case PHYLOXML:
144                         parser = PhyloXmlParser.createPhyloXmlParserXsdValidating();
145                         break;
146                     default:
147                         throw new IllegalArgumentException( "unknown format: " + client.getReturnFormat() );
148                 }
149                 if ( _main_frame.getMainPanel().getCurrentTreePanel() != null ) {
150                     _main_frame.getMainPanel().getCurrentTreePanel().setWaitCursor();
151                 }
152                 else {
153                     _main_frame.getMainPanel().setWaitCursor();
154                 }
155                 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
156                 trees = factory.create( url.openStream(), parser );
157             }
158             catch ( final MalformedURLException e ) {
159                 exception = true;
160                 JOptionPane.showMessageDialog( _main_frame,
161                                                "Malformed URL: " + url + "\n" + e.getLocalizedMessage(),
162                                                "Malformed URL",
163                                                JOptionPane.ERROR_MESSAGE );
164             }
165             catch ( final IOException e ) {
166                 exception = true;
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 );
171             }
172             catch ( final NumberFormatException e ) {
173                 exception = true;
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 );
178             }
179             catch ( final Exception e ) {
180                 exception = true;
181                 e.printStackTrace();
182                 JOptionPane.showMessageDialog( _main_frame,
183                                                e.getLocalizedMessage(),
184                                                "Unexpected Exception",
185                                                JOptionPane.ERROR_MESSAGE );
186             }
187             finally {
188                 if ( _main_frame.getCurrentTreePanel() != null ) {
189                     _main_frame.getCurrentTreePanel().setArrowCursor();
190                 }
191                 else {
192                     _main_frame.getMainPanel().setArrowCursor();
193                 }
194             }
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 );
201                         }
202                         if ( client.getProcessingInstructions() != null ) {
203                             try {
204                                 WebserviceUtil.processInstructions( client, phylogeny );
205                             }
206                             catch ( final PhyloXmlDataFormatException e ) {
207                                 JOptionPane.showMessageDialog( _main_frame,
208                                                                "Error:\n" + e.getLocalizedMessage(),
209                                                                "Error",
210                                                                JOptionPane.ERROR_MESSAGE );
211                             }
212                         }
213                         if ( client.getNodeField() != null ) {
214                             try {
215                                 PhylogenyMethods.transferNodeNameToField( phylogeny, client.getNodeField(), false );
216                             }
217                             catch ( final PhyloXmlDataFormatException e ) {
218                                 JOptionPane.showMessageDialog( _main_frame,
219                                                                "Error:\n" + e.getLocalizedMessage(),
220                                                                "Error",
221                                                                JOptionPane.ERROR_MESSAGE );
222                             }
223                         }
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(),
230                                                                          url.toString() );
231                         String my_name_for_file = "";
232                         if ( !ForesterUtil.isEmpty( phylogeny.getName() ) ) {
233                             my_name_for_file = new String( phylogeny.getName() ).replaceAll( " ", "_" );
234                         }
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() );
239                                 sb.append( "_" );
240                             }
241                             sb.append( phylogeny.getIdentifier().getValue() );
242                             my_name_for_file = new String( sb.toString().replaceAll( " ", "_" ) );
243                         }
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();
248                     }
249                 }
250             }
251             else if ( !exception ) {
252                 JOptionPane.showMessageDialog( null, ForesterUtil.wordWrap( "Failed to read in tree(s) from [" + url
253                         + "]", 80 ), "Error", JOptionPane.ERROR_MESSAGE );
254             }
255             _main_frame.getContentPane().repaint();
256             if ( ( trees != null ) && ( trees.length > 0 ) ) {
257                 try {
258                     JOptionPane.showMessageDialog( null,
259                                                    ForesterUtil.wordWrap( "Successfully read in " + trees.length
260                                                            + " tree(s) from [" + url + "]", 80 ),
261                                                    "Success",
262                                                    JOptionPane.INFORMATION_MESSAGE );
263                 }
264                 catch ( final Exception e ) {
265                     // Not important if this fails, do nothing.
266                 }
267                 _main_frame.getContentPane().repaint();
268             }
269         }
270         _main_frame.activateSaveAllIfNeeded();
271         System.gc();
272     }
273 }