JAL-2797 the big switch, all GUI method calls now go via the AptxFrame
[jalview.git] / forester / java / src / org / forester / archaeopteryx / UrlTreeReader.java
index 729d569..e3c81f9 100644 (file)
@@ -5,7 +5,7 @@
 // Copyright (C) 2008-2009 Christian M. Zmasek
 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
 // All rights reserved
-// 
+//
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 // Lesser General Public License for more details.
-// 
+//
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 //
 // Contact: phylosoft @ gmail . com
-// WWW: www.phylosoft.org/forester
+// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
 
 package org.forester.archaeopteryx;
 
@@ -29,7 +29,6 @@ import java.io.File;
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
-import java.util.Date;
 
 import javax.swing.JOptionPane;
 
@@ -39,12 +38,12 @@ import org.forester.archaeopteryx.webservices.WebservicesManager;
 import org.forester.io.parsers.PhylogenyParser;
 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
 import org.forester.io.parsers.nhx.NHXParser;
+import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
 import org.forester.io.parsers.tol.TolParser;
 import org.forester.phylogeny.Phylogeny;
+import org.forester.phylogeny.PhylogenyMethods;
 import org.forester.phylogeny.data.Identifier;
-import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
-import org.forester.phylogeny.factories.PhylogenyFactory;
 import org.forester.util.ForesterUtil;
 
 public class UrlTreeReader implements Runnable {
@@ -57,14 +56,18 @@ public class UrlTreeReader implements Runnable {
         _webservice_client_index = webservice_client_index;
     }
 
+    @Override
+    public void run() {
+        readPhylogeniesFromWebservice();
+    }
+
     synchronized void readPhylogeniesFromWebservice() {
-        final long start_time = new Date().getTime();
         URL url = null;
         Phylogeny[] trees = null;
         final WebservicesManager webservices_manager = WebservicesManager.getInstance();
         final PhylogeniesWebserviceClient client = webservices_manager
                 .getAvailablePhylogeniesWebserviceClient( _webservice_client_index );
-        String identifier = JOptionPane.showInputDialog( _main_frame, client.getInstructions() + "\n(Reference: "
+        String identifier = JOptionPane.showInputDialog( _main_frame.getThisFrame(), client.getInstructions() + "\n(Reference: "
                 + client.getReference() + ")", client.getDescription(), JOptionPane.QUESTION_MESSAGE );
         if ( ( identifier != null ) && ( identifier.trim().length() > 0 ) ) {
             identifier = identifier.trim();
@@ -78,7 +81,7 @@ public class UrlTreeReader implements Runnable {
                     id = -1;
                 }
                 if ( id < 1 ) {
-                    JOptionPane.showMessageDialog( _main_frame,
+                    JOptionPane.showMessageDialog( _main_frame.getThisFrame(),
                                                    "Identifier is expected to be a number",
                                                    "Can not open URL",
                                                    JOptionPane.ERROR_MESSAGE );
@@ -86,6 +89,7 @@ public class UrlTreeReader implements Runnable {
                 }
                 identifier = id + "";
             }
+            boolean exception = false;
             try {
                 String url_str = client.getUrl();
                 url_str = url_str.replaceFirst( PhylogeniesWebserviceClient.QUERY_PLACEHOLDER, identifier );
@@ -99,34 +103,43 @@ public class UrlTreeReader implements Runnable {
                         parser = new NexusPhylogeniesParser();
                         ( ( NexusPhylogeniesParser ) parser ).setReplaceUnderscores( true );
                         break;
+                    case TREEBASE_TREE:
+                        parser = new NexusPhylogeniesParser();
+                        ( ( NexusPhylogeniesParser ) parser ).setReplaceUnderscores( true );
+                        ( ( NexusPhylogeniesParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
+                        break;
+                    case TREEBASE_STUDY:
+                        parser = new NexusPhylogeniesParser();
+                        ( ( NexusPhylogeniesParser ) parser ).setReplaceUnderscores( true );
+                        ( ( NexusPhylogeniesParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
+                        break;
                     case NH:
                         parser = new NHXParser();
-                        ( ( NHXParser ) parser ).setTaxonomyExtraction( ForesterUtil.TAXONOMY_EXTRACTION.NO );
+                        ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
                         ( ( NHXParser ) parser ).setReplaceUnderscores( true );
                         ( ( NHXParser ) parser ).setGuessRootedness( true );
                         break;
                     case NH_EXTRACT_TAXONOMY:
                         parser = new NHXParser();
-                        ( ( NHXParser ) parser )
-                                .setTaxonomyExtraction( ForesterUtil.TAXONOMY_EXTRACTION.PFAM_STYLE_ONLY );
+                        ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.AGGRESSIVE );
                         ( ( NHXParser ) parser ).setReplaceUnderscores( false );
                         ( ( NHXParser ) parser ).setGuessRootedness( true );
                         break;
                     case PFAM:
                         parser = new NHXParser();
                         ( ( NHXParser ) parser )
-                                .setTaxonomyExtraction( ForesterUtil.TAXONOMY_EXTRACTION.PFAM_STYLE_ONLY );
+                                .setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.PFAM_STYLE_STRICT );
                         ( ( NHXParser ) parser ).setReplaceUnderscores( false );
                         ( ( NHXParser ) parser ).setGuessRootedness( true );
                         break;
                     case NHX:
                         parser = new NHXParser();
-                        ( ( NHXParser ) parser ).setTaxonomyExtraction( ForesterUtil.TAXONOMY_EXTRACTION.NO );
+                        ( ( NHXParser ) parser ).setTaxonomyExtraction( NHXParser.TAXONOMY_EXTRACTION.NO );
                         ( ( NHXParser ) parser ).setReplaceUnderscores( false );
                         ( ( NHXParser ) parser ).setGuessRootedness( true );
                         break;
                     case PHYLOXML:
-                        parser = new PhyloXmlParser();
+                        parser = PhyloXmlParser.createPhyloXmlParserXsdValidating();
                         break;
                     default:
                         throw new IllegalArgumentException( "unknown format: " + client.getReturnFormat() );
@@ -137,28 +150,33 @@ public class UrlTreeReader implements Runnable {
                 else {
                     _main_frame.getMainPanel().setWaitCursor();
                 }
-                final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
-                trees = factory.create( url.openStream(), parser );
+                trees = ForesterUtil.readPhylogeniesFromUrl( url, parser );
             }
             catch ( final MalformedURLException e ) {
-                JOptionPane.showMessageDialog( _main_frame,
+                exception = true;
+                JOptionPane.showMessageDialog( _main_frame.getThisFrame(),
                                                "Malformed URL: " + url + "\n" + e.getLocalizedMessage(),
                                                "Malformed URL",
                                                JOptionPane.ERROR_MESSAGE );
             }
             catch ( final IOException e ) {
-                JOptionPane.showMessageDialog( _main_frame, "Could not read from " + url + "\n"
-                        + e.getLocalizedMessage(), "Failed to read tree from " + client.getName() + " for "
-                        + identifier, JOptionPane.ERROR_MESSAGE );
+                exception = true;
+                JOptionPane.showMessageDialog( _main_frame.getThisFrame(),
+                                               "Could not read from " + url + "\n" + e.getLocalizedMessage(),
+                                               "Failed to read tree from " + client.getName() + " for " + identifier,
+                                               JOptionPane.ERROR_MESSAGE );
             }
             catch ( final NumberFormatException e ) {
-                JOptionPane.showMessageDialog( _main_frame, "Could not read from " + url + "\n"
-                        + e.getLocalizedMessage(), "Failed to read tree from " + client.getName() + " for "
-                        + identifier, JOptionPane.ERROR_MESSAGE );
+                exception = true;
+                JOptionPane.showMessageDialog( _main_frame.getThisFrame(),
+                                               "Could not read from " + url + "\n" + e.getLocalizedMessage(),
+                                               "Failed to read tree from " + client.getName() + " for " + identifier,
+                                               JOptionPane.ERROR_MESSAGE );
             }
             catch ( final Exception e ) {
+                exception = true;
                 e.printStackTrace();
-                JOptionPane.showMessageDialog( _main_frame,
+                JOptionPane.showMessageDialog( _main_frame.getThisFrame(),
                                                e.getLocalizedMessage(),
                                                "Unexpected Exception",
                                                JOptionPane.ERROR_MESSAGE );
@@ -178,16 +196,27 @@ public class UrlTreeReader implements Runnable {
                             phylogeny.setRerootable( false );
                             phylogeny.setRooted( true );
                         }
-                        if ( client.getName().equals( WebserviceUtil.PFAM_NAME ) ) {
-                            phylogeny.setRerootable( false );
-                            phylogeny.setRooted( true );
-                            ForesterUtil.transferInternalNodeNamesToConfidence( phylogeny );
-                        }
                         if ( client.getProcessingInstructions() != null ) {
-                            WebserviceUtil.processInstructions( client, phylogeny );
+                            try {
+                                WebserviceUtil.processInstructions( client, phylogeny );
+                            }
+                            catch ( final PhyloXmlDataFormatException e ) {
+                                JOptionPane.showMessageDialog( _main_frame.getThisFrame(),
+                                                               "Error:\n" + e.getLocalizedMessage(),
+                                                               "Error",
+                                                               JOptionPane.ERROR_MESSAGE );
+                            }
                         }
                         if ( client.getNodeField() != null ) {
-                            ForesterUtil.transferNodeNameToField( phylogeny, client.getNodeField() );
+                            try {
+                                PhylogenyMethods.transferNodeNameToField( phylogeny, client.getNodeField(), false );
+                            }
+                            catch ( final PhyloXmlDataFormatException e ) {
+                                JOptionPane.showMessageDialog( _main_frame.getThisFrame(),
+                                                               "Error:\n" + e.getLocalizedMessage(),
+                                                               "Error",
+                                                               JOptionPane.ERROR_MESSAGE );
+                            }
                         }
                         phylogeny.setIdentifier( new Identifier( identifier, client.getName() ) );
                         _main_frame.getJMenuBar().remove( _main_frame.getHelpMenu() );
@@ -210,18 +239,22 @@ public class UrlTreeReader implements Runnable {
                             my_name_for_file = new String( sb.toString().replaceAll( " ", "_" ) );
                         }
                         _main_frame.getMainPanel().getCurrentTreePanel().setTreeFile( new File( my_name_for_file ) );
-                        Util.lookAtSomeTreePropertiesForAptxControlSettings( phylogeny, _main_frame.getMainPanel()
+                        AptxUtil.lookAtSomeTreePropertiesForAptxControlSettings( phylogeny, _main_frame.getMainPanel()
                                 .getControlPanel(), _main_frame.getConfiguration() );
                         _main_frame.getMainPanel().getControlPanel().showWhole();
                     }
                 }
             }
+            else if ( !exception ) {
+                JOptionPane.showMessageDialog( null, ForesterUtil.wordWrap( "Failed to read in tree(s) from [" + url
+                        + "]", 80 ), "Error", JOptionPane.ERROR_MESSAGE );
+            }
             _main_frame.getContentPane().repaint();
-            if ( ( ( trees != null ) && ( trees.length > 0 ) ) && ( ( new Date().getTime() - start_time ) > 20000 ) ) {
+            if ( ( trees != null ) && ( trees.length > 0 ) ) {
                 try {
                     JOptionPane.showMessageDialog( null,
                                                    ForesterUtil.wordWrap( "Successfully read in " + trees.length
-                                                           + " evolutionry tree(s) from [" + url + "]", 80 ),
+                                                           + " tree(s) from [" + url + "]", 80 ),
                                                    "Success",
                                                    JOptionPane.INFORMATION_MESSAGE );
                 }
@@ -234,9 +267,4 @@ public class UrlTreeReader implements Runnable {
         _main_frame.activateSaveAllIfNeeded();
         System.gc();
     }
-
-    @Override
-    public void run() {
-        readPhylogeniesFromWebservice();
-    }
 }