JAL-2799 JAL-2805 JAL-281 multiple trees can now be loaded in
[jalview.git] / src / jalview / ext / archaeopteryx / AptxInit.java
index 107aeca..f6d9fd6 100644 (file)
@@ -8,11 +8,16 @@ import jalview.gui.Desktop;
 import jalview.viewmodel.AlignmentViewport;
 
 import java.awt.Dimension;
+import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.net.URL;
 import java.util.Map;
 
+import org.forester.archaeopteryx.AptxUtil;
 import org.forester.archaeopteryx.Archaeopteryx;
+import org.forester.archaeopteryx.Configuration;
 import org.forester.archaeopteryx.MainFrame;
+import org.forester.io.parsers.nhx.NHXParser;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyNode;
 
@@ -25,6 +30,25 @@ import org.forester.phylogeny.PhylogenyNode;
  */
 public final class AptxInit
 {
+
+  private final static Configuration APTX_CONFIG = new Configuration(
+          "_aptx_jalview_configuration_file", false, false, false);
+
+  private final static boolean VALIDATE_PHYLOXML_XSD = APTX_CONFIG
+          .isValidatePhyloXmlAgainstSchema();
+
+  private final static boolean REPLACE_NHX_UNDERSCORES = APTX_CONFIG
+          .isReplaceUnderscoresInNhParsing();
+
+  private final static boolean INTERNAL_NUMBERS_AS_CONFIDENCE = APTX_CONFIG
+          .isInternalNumberAreConfidenceForNhParsing();
+
+  private final static boolean MIDPOINT_REROOT = APTX_CONFIG
+          .isMidpointReroot();
+
+  private final static NHXParser.TAXONOMY_EXTRACTION TAXONOMY_EXTRACTION = APTX_CONFIG
+          .getTaxonomyExtraction();
+
   /**
    * Test method, should generally not be used as it does not bind the tree to
    * its alignment
@@ -78,110 +102,58 @@ public final class AptxInit
           AlignmentViewport viewport)
   {
     String[] AptxArgs = new String[] { "-c",
-        "_aptx_jalview_configuration_file", filePath };
+        APTX_CONFIG.getConfigFilename(), filePath };
     MainFrame aptxApp = Archaeopteryx.main(AptxArgs);
 
+    int tabCount = aptxApp.getMainPanel().getTabbedPane().getTabCount();
+
+    for (int i = 0; i < tabCount; i++)
+    {
+      // roundabout way to select each tree because getComponentAt(i) requires
+      // casting to TreePanel which doesn't work
+      aptxApp.getMainPanel().getTabbedPane().setSelectedIndex(i);
+      Phylogeny tree = aptxApp.getMainPanel().getCurrentTreePanel()
+              .getPhylogeny();
+
     LoadedTreeAssociation bindAptxNodes = new LoadedTreeAssociation(
             viewport.getAlignment().getSequencesArray(),
-            aptxApp.getMainPanel().getCurrentTreePanel().getPhylogeny());
+              tree);
 
     bindAptxNodes.associateLeavesToSequences();
 
     bindNodesToJalviewSequences(aptxApp, viewport,
             bindAptxNodes.getAlignmentWithNodes(),
             bindAptxNodes.getNodesWithAlignment());
-
+    }
     return bindFrameToJalview(aptxApp);
   }
 
-  public static MainFrame createInstanceFromUrl(URL url)
+  public static MainFrame createInstanceFromUrl(URL treeUrl,
+          AlignmentViewport viewport)
+          throws FileNotFoundException, IOException
   {
+    
+    Phylogeny[] trees = AptxUtil.readPhylogeniesFromUrl(treeUrl,
+            VALIDATE_PHYLOXML_XSD,
+             REPLACE_NHX_UNDERSCORES, INTERNAL_NUMBERS_AS_CONFIDENCE,
+            TAXONOMY_EXTRACTION, MIDPOINT_REROOT);
+    MainFrame aptxApp = Archaeopteryx.createApplication(trees, APTX_CONFIG,
+            null);
+
+    for (Phylogeny tree : trees)
+    {
+      LoadedTreeAssociation bindAptxNodes = new LoadedTreeAssociation(
+              viewport.getAlignment().getSequencesArray(), tree);
+
+      bindAptxNodes.associateLeavesToSequences();
+      bindNodesToJalviewSequences(aptxApp, viewport,
+              bindAptxNodes.getAlignmentWithNodes(),
+              bindAptxNodes.getNodesWithAlignment());
+
+    }
+
+    return bindFrameToJalview(aptxApp);
 
-    return null;
-    // void readPhylogeniesFromURL() {
-    // URL url = null;
-    // Phylogeny[] phys = null;
-    // final String message = "Please enter a complete URL, for example
-    // \"http://purl.org/phylo/treebase/phylows/study/TB2:S15480?format=nexus\"";
-    // final String url_string = JOptionPane
-    // .showInputDialog( this,
-    // message,
-    // "Use URL/webservice to obtain a phylogeny",
-    // JOptionPane.QUESTION_MESSAGE );
-    // boolean nhx_or_nexus = false;
-    // if ( ( url_string != null ) && ( url_string.length() > 0 ) ) {
-    // try {
-    // url = new URL( url_string );
-    // PhylogenyParser parser = null;
-    // if ( url.getHost().toLowerCase().indexOf( "tolweb" ) >= 0 ) {
-    // parser = new TolParser();
-    // }
-    // else {
-    // parser = ParserUtils
-    // .createParserDependingOnUrlContents( url,
-    // getConfiguration().isValidatePhyloXmlAgainstSchema() );
-    // }
-    // if ( parser instanceof NexusPhylogeniesParser ) {
-    // nhx_or_nexus = true;
-    // }
-    // else if ( parser instanceof NHXParser ) {
-    // nhx_or_nexus = true;
-    // }
-    // if ( _mainpanel.getCurrentTreePanel() != null ) {
-    // _mainpanel.getCurrentTreePanel().setWaitCursor();
-    // }
-    // else {
-    // _mainpanel.setWaitCursor();
-    // }
-    // final PhylogenyFactory factory =
-    // ParserBasedPhylogenyFactory.getInstance();
-    // phys = factory.create( url.openStream(), parser );
-    // }
-    // catch ( final MalformedURLException e ) {
-    // JOptionPane.showMessageDialog( this,
-    // "Malformed URL: " + url + "\n" + e.getLocalizedMessage(),
-    // "Malformed URL",
-    // JOptionPane.ERROR_MESSAGE );
-    // }
-    // catch ( final IOException e ) {
-    // JOptionPane.showMessageDialog( this,
-    // "Could not read from " + url + "\n"
-    // + ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ),
-    // "Failed to read URL",
-    // JOptionPane.ERROR_MESSAGE );
-    // }
-    // catch ( final Exception e ) {
-    // JOptionPane.showMessageDialog( this,
-    // ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ),
-    // "Unexpected Exception",
-    // JOptionPane.ERROR_MESSAGE );
-    // }
-    // finally {
-    // if ( _mainpanel.getCurrentTreePanel() != null ) {
-    // _mainpanel.getCurrentTreePanel().setArrowCursor();
-    // }
-    // else {
-    // _mainpanel.setArrowCursor();
-    // }
-    // }
-    // if ( ( phys != null ) && ( phys.length > 0 ) ) {
-    // if ( nhx_or_nexus &&
-    // getOptions().isInternalNumberAreConfidenceForNhParsing() ) {
-    // for( final Phylogeny phy : phys ) {
-    // PhylogenyMethods.transferInternalNodeNamesToConfidence( phy, "" );
-    // }
-    // }
-    // AptxUtil.addPhylogeniesToTabs( phys,
-    // new File( url.getFile() ).getName(),
-    // new File( url.getFile() ).toString(),
-    // getConfiguration(),
-    // getMainPanel() );
-    // _mainpanel.getControlPanel().showWhole();
-    // }
-    // }
-    // activateSaveAllIfNeeded();
-    // System.gc();
-    // }
 
   }
 
@@ -191,7 +163,7 @@ public final class AptxInit
           final Phylogeny[] aptxTrees)
   {
     MainFrame aptxApp = Archaeopteryx.createApplication(aptxTrees,
-            "_aptx_jalview_configuration_file", null);
+            APTX_CONFIG, null);
     return aptxApp;
   }