Merge branch 'kjvdh/features/PhylogenyViewer_tabbedsupport' into merge/2_11_2/kjvdh...
[jalview.git] / src / jalview / ext / archaeopteryx / AptxInit.java
diff --git a/src/jalview/ext/archaeopteryx/AptxInit.java b/src/jalview/ext/archaeopteryx/AptxInit.java
new file mode 100644 (file)
index 0000000..e58a38d
--- /dev/null
@@ -0,0 +1,199 @@
+package jalview.ext.archaeopteryx;
+
+import jalview.analysis.TreeBuilder;
+import jalview.datamodel.SequenceI;
+import jalview.ext.treeviewer.ExternalTreeBuilderI;
+import jalview.ext.treeviewer.ExternalTreeViewerBindingI;
+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;
+
+/**
+ * Static class for creating Archaeopteryx tree viewer instances from calculated
+ * trees and letting them be bound to Jalview.
+ * 
+ * @author kjvanderheide
+ *
+ */
+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
+   * 
+   * @param aptxTrees
+   * @return
+   */
+  public static MainFrame createUnboundInstance(final Phylogeny aptxTree)
+  {
+    Phylogeny[] aptxTrees = { aptxTree };
+    return createAptxFrame(aptxTrees);
+  }
+
+  // public static MainFrame createInstance(final Phylogeny[] aptxTrees,
+  // AlignmentViewport jalviewAlignmentView)
+  // {
+  // return createAptxFrameInJalview(aptxTrees);
+  //
+  // }
+  //
+  // public static MainFrame createInstance(final Phylogeny aptxTree,
+  // final AlignmentViewport jalviewAlignmentView)
+  // {
+  // Phylogeny[] aptxTrees = { aptxTree }; // future possibility to load in
+  // // several trees simultaneously
+  // return createAptxFrameInJalview(aptxTrees);
+  //
+  // }
+
+  public static MainFrame createInstance(
+          final TreeBuilder calculatedTree) // very dense method, to be split up
+  {
+    ExternalTreeBuilderI<Phylogeny, PhylogenyNode> aptxTreeBuilder = new AptxTreeBuilder(
+            calculatedTree);
+
+    Phylogeny aptxTree = aptxTreeBuilder.buildTree();
+    Phylogeny[] aptxTrees = { aptxTree }; // future possibility to load in
+                                          // several trees simultaneously
+
+    MainFrame aptxApp = createAptxFrame(aptxTrees);
+            
+    bindNodesToJalviewSequences(aptxApp, calculatedTree.getAvport(),
+            aptxTreeBuilder.getAlignmentBoundNodes(),
+            aptxTreeBuilder.getNodesBoundAlignment());
+
+    return bindFrameToJalview(aptxApp);
+
+  }
+
+  public static MainFrame createInstanceFromFile(String filePath,
+          AlignmentViewport viewport)
+  {
+    String[] AptxArgs = new String[] { "-c",
+        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(),
+              tree);
+
+    bindAptxNodes.associateLeavesToSequences();
+
+    bindNodesToJalviewSequences(aptxApp, viewport,
+            bindAptxNodes.getAlignmentWithNodes(),
+            bindAptxNodes.getNodesWithAlignment());
+    }
+    return bindFrameToJalview(aptxApp);
+  }
+
+  public static MainFrame createInstanceFromUrl(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 aptxApp = Archaeopteryx.createApplication(trees, APTX_CONFIG,
+            treeTitle);
+
+    for (Phylogeny tree : trees)
+    {
+      LoadedTreeAssociation bindAptxNodes = new LoadedTreeAssociation(
+              viewport.getAlignment().getSequencesArray(), tree);
+
+      bindAptxNodes.associateLeavesToSequences();
+      bindNodesToJalviewSequences(aptxApp, viewport,
+              bindAptxNodes.getAlignmentWithNodes(),
+              bindAptxNodes.getNodesWithAlignment());
+
+    }
+
+    return bindFrameToJalview(aptxApp);
+
+  }
+
+
+
+
+  public static MainFrame createAptxFrame(
+          final Phylogeny[] aptxTrees)
+  {
+    MainFrame aptxApp = Archaeopteryx.createApplication(aptxTrees,
+            APTX_CONFIG, null);
+    return aptxApp;
+  }
+
+
+  public static ExternalTreeViewerBindingI<?> bindNodesToJalviewSequences(
+          final MainFrame aptxApp,
+          final AlignmentViewport jalviewAlignViewport,
+          final Map<SequenceI, PhylogenyNode> alignMappedToNodes,
+          final Map<PhylogenyNode, SequenceI> nodesMappedToAlign)
+  {
+    return new JalviewBinding(aptxApp, jalviewAlignViewport,
+            alignMappedToNodes, nodesMappedToAlign);
+  }
+
+
+  public static MainFrame bindFrameToJalview(final MainFrame aptxApp)
+  {
+    int width = 400;
+    int height = 550;
+    aptxApp.setMinimumSize(new Dimension(width, height));
+    // aptxApp.setFont(Desktop.instance.getFont());
+    // aptxApp.getMainPanel().setFont(Desktop.instance.getFont());
+
+    Desktop.addInternalFrame(aptxApp, "Archaeopteryx Tree View", true,
+            width, height, true, true);
+
+    return aptxApp;
+
+  }
+
+
+
+}