JAL-2799 ALL trees are now loaded in as separate Aptx instances
[jalview.git] / src / jalview / ext / archaeopteryx / AptxInit.java
index ce34573..7dbddad 100644 (file)
@@ -5,15 +5,29 @@ import jalview.datamodel.SequenceI;
 import jalview.ext.treeviewer.ExternalTreeBuilderI;
 import jalview.ext.treeviewer.ExternalTreeViewerBindingI;
 import jalview.gui.Desktop;
+import jalview.gui.JvOptionPane;
+import jalview.util.MessageManager;
 import jalview.viewmodel.AlignmentViewport;
 
 import java.awt.Dimension;
+import java.io.File;
+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.PhylogenyParser;
+import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
+import org.forester.io.parsers.nhx.NHXParser;
+import org.forester.io.parsers.util.ParserUtils;
 import org.forester.phylogeny.Phylogeny;
+import org.forester.phylogeny.PhylogenyMethods;
 import org.forester.phylogeny.PhylogenyNode;
+import org.forester.util.ForesterUtil;
 
 /**
  * Static class for creating Archaeopteryx tree viewer instances from calculated
@@ -24,6 +38,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
@@ -33,8 +66,7 @@ public final class AptxInit
    */
   public static MainFrame createUnboundInstance(final Phylogeny aptxTree)
   {
-    Phylogeny[] aptxTrees = { aptxTree };
-    return createAptxFrame(aptxTrees);
+    return createAptxFrame(aptxTree);
   }
 
   // public static MainFrame createInstance(final Phylogeny[] aptxTrees,
@@ -60,29 +92,133 @@ public final class AptxInit
             calculatedTree);
 
     Phylogeny aptxTree = aptxTreeBuilder.buildTree();
-    Phylogeny[] aptxTrees = { aptxTree }; // future possibility to load in
-                                          // several trees simultaneously
 
-    MainFrame aptxApp = createAptxFrame(aptxTrees);
+    MainFrame aptxApp = createAptxFrame(aptxTree);
             
     bindNodesToJalviewSequences(aptxApp, calculatedTree.getAvport(),
             aptxTreeBuilder.getAlignmentBoundNodes(),
             aptxTreeBuilder.getNodesBoundAlignment());
+
     return bindFrameToJalview(aptxApp);
 
   }
 
+  /**
+   * Refactored from Archaeopteryx.main
+   * 
+   * @param filePath
+   * @param viewport
+   * @return
+   * @throws IOException
+   * @throws FileNotFoundException
+   */
+  public static MainFrame[] createInstancesFromFile(String filePath,
+          AlignmentViewport viewport)
+          throws FileNotFoundException, IOException
+  {
+    File treeFile = new File(filePath);
+    final String err = ForesterUtil.isReadableFile(treeFile);
+    if (!ForesterUtil.isEmpty(err))
+    {
+      JvOptionPane.showMessageDialog(Desktop.desktop, err,
+              MessageManager.getString("label.problem_reading_tree_file"),
+              JvOptionPane.WARNING_MESSAGE);
+    }
+    boolean nhx_or_nexus = false;
+    final PhylogenyParser p = ParserUtils.createParserDependingOnFileType(
+            treeFile, VALIDATE_PHYLOXML_XSD);
+    if (p instanceof NHXParser)
+    {
+      nhx_or_nexus = true;
+      final NHXParser nhx = (NHXParser) p;
+      nhx.setReplaceUnderscores(REPLACE_NHX_UNDERSCORES);
+      nhx.setIgnoreQuotes(false);
+      nhx.setTaxonomyExtraction(TAXONOMY_EXTRACTION);
+    }
+    else if (p instanceof NexusPhylogeniesParser)
+    {
+      nhx_or_nexus = true;
+      final NexusPhylogeniesParser nex = (NexusPhylogeniesParser) p;
+      nex.setReplaceUnderscores(REPLACE_NHX_UNDERSCORES);
+      nex.setIgnoreQuotes(false);
+    }
+//    else if (p instanceof PhyloXmlParser)
+//    {
+//      MainFrameApplication.warnIfNotPhyloXmlValidation(APTX_CONFIG);
+//    }
+    Phylogeny[] phylogenies = PhylogenyMethods.readPhylogenies(p, treeFile);
+    MainFrame[] aptxFrames = new MainFrame[phylogenies.length];
+    String treeTitle = treeFile.getName();
+
+    for (int i = 0; i < phylogenies.length; i++)
+      {
+      Phylogeny tree = phylogenies[i];
+      if (nhx_or_nexus && INTERNAL_NUMBERS_AS_CONFIDENCE)
+      {
+        PhylogenyMethods.transferInternalNodeNamesToConfidence(tree, "");
+      }
+
+      MainFrame aptxApp = Archaeopteryx.createApplication(tree, APTX_CONFIG,
+              treeTitle);
+      LoadedTreeAssociation bindAptxNodes = new LoadedTreeAssociation(
+              viewport.getAlignment().getSequencesArray(), tree);
+
+      bindAptxNodes.associateLeavesToSequences();
+      bindNodesToJalviewSequences(aptxApp, viewport,
+              bindAptxNodes.getAlignmentWithNodes(),
+              bindAptxNodes.getNodesWithAlignment());
+      bindFrameToJalview(aptxApp);
+      aptxFrames[i] = aptxApp;
+    }
+
+    return aptxFrames;
+    }
+
+
+  public static MainFrame[] createInstancesFromUrl(URL treeUrl,
+          AlignmentViewport viewport)
+          throws FileNotFoundException, IOException, RuntimeException
+  {
+    
+    String treeTitle = treeUrl.getFile();
+    Phylogeny[] trees = AptxUtil.readPhylogeniesFromUrl(treeUrl,
+            VALIDATE_PHYLOXML_XSD,
+             REPLACE_NHX_UNDERSCORES, INTERNAL_NUMBERS_AS_CONFIDENCE,
+            TAXONOMY_EXTRACTION, MIDPOINT_REROOT);
+
+    MainFrame[] aptxFrames = new MainFrame[trees.length];
+    for (int i = 0; i < trees.length; i++)
+    {
+      Phylogeny tree = trees[i];
+      MainFrame aptxApp = Archaeopteryx.createApplication(tree, APTX_CONFIG,
+              treeTitle);
+      LoadedTreeAssociation bindAptxNodes = new LoadedTreeAssociation(
+              viewport.getAlignment().getSequencesArray(), tree);
+
+      bindAptxNodes.associateLeavesToSequences();
+      bindNodesToJalviewSequences(aptxApp, viewport,
+              bindAptxNodes.getAlignmentWithNodes(),
+              bindAptxNodes.getNodesWithAlignment());
+      bindFrameToJalview(aptxApp);
+      aptxFrames[i] = aptxApp;
+    }
+
+    return aptxFrames;
+
+  }
+
 
 
 
   public static MainFrame createAptxFrame(
-          final Phylogeny[] aptxTrees)
+          final Phylogeny aptxTree)
   {
-    MainFrame aptxApp = Archaeopteryx.createApplication(aptxTrees,
-            "_aptx_jalview_configuration_file", null);
+    MainFrame aptxApp = Archaeopteryx.createApplication(aptxTree,
+            APTX_CONFIG, null);
     return aptxApp;
   }
 
+
   public static ExternalTreeViewerBindingI<?> bindNodesToJalviewSequences(
           final MainFrame aptxApp,
           final AlignmentViewport jalviewAlignViewport,
@@ -110,4 +246,5 @@ public final class AptxInit
   }
 
 
+
 }