JAL-2805 Jalview sequence binding interface expanded
[jalview.git] / src / jalview / ext / archaeopteryx / ArchaeopteryxInit.java
index 4381a83..bd7de87 100644 (file)
-/*******************************************************************************
- * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
- * Copyright (C) 2017 The Jalview Authors
- *
- * This file is part of Jalview.
- *  
- * Jalview is free software: you can redistribute it and/or
- * modify it under the terms of the GNU General Public License 
- * as published by the Free Software Foundation, either version 3
- * of the License, or (at your option) any later version.
- *   
- * Jalview is distributed in the hope that it will be useful, but 
- * WITHOUT ANY WARRANTY; without even the implied warranty 
- * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
- * PURPOSE.  See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
- * The Jalview Authors are detailed in the 'AUTHORS' file.
- *******************************************************************************/
-
 package jalview.ext.archaeopteryx;
 
-import jalview.analysis.TreeModel;
-import jalview.io.NewickFile;
+import jalview.analysis.TreeBuilder;
+import jalview.datamodel.SequenceI;
+import jalview.gui.Desktop;
+import jalview.viewmodel.AlignmentViewport;
 
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.PrintWriter;
+import java.awt.Dimension;
+import java.util.Map;
 
 import org.forester.archaeopteryx.Archaeopteryx;
-
-public class ArchaeopteryxInit
+import org.forester.archaeopteryx.MainFrame;
+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 ArchaeopteryxInit
 {
-  private String filePath = "/tmp/jalviewtree.nwk";
-
-  private TreeModel tree;
-
-  private NewickFile newickTree;
-
-  public ArchaeopteryxInit(TreeModel jalviewTreeModel)
-  {
-    this.tree = jalviewTreeModel;
-    this.newickTree = treeToNewick(tree);
-  }
-
-  public ArchaeopteryxInit(NewickFile newickTreeModel)
+  /**
+   * 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)
   {
-    this.newickTree = newickTreeModel;
+    Phylogeny[] aptxTrees = { aptxTree };
+    return createAptxFrame(aptxTrees);
   }
 
-  public void startArchaeopteryx()
+  // 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
   {
-    String newickOutput = newickTree.print(newickTree.hasBootstrap(),
-            newickTree.hasDistances(), newickTree.hasRootDistance());
-
-    File newickFile = new File(filePath);
-    PrintWriter writer;
+    ExternalTreeBuilderI<Phylogeny, PhylogenyNode> aptxTreeBuilder = new ArchaeopteryxTreeBuilder(
+            calculatedTree);
 
-    try
-    {
-      writer = new PrintWriter(newickFile);
-      writer.println(newickOutput);
+    Phylogeny aptxTree = aptxTreeBuilder.buildTree();
+    Phylogeny[] aptxTrees = { aptxTree }; // future possibility to load in
+                                          // several trees simultaneously
 
-      // System.out.println(writer.checkError());
-      writer.close();
+    MainFrame aptxApp = createAptxFrame(aptxTrees);
+            
+    bindNodesToJalviewSequences(aptxApp, calculatedTree.getAvport(),
+            aptxTreeBuilder.getAlignmentBoundNodes(),
+            aptxTreeBuilder.getNodesBoundAlignment());
+    return bindFrameToJalview(aptxApp);
 
-      String[] commandLineArgs = { "-open", newickFile.getCanonicalPath() };
-      Archaeopteryx.main(commandLineArgs);
-
-    } catch (FileNotFoundException e)
-    {
-      // TODO Auto-generated catch block
-      e.printStackTrace();
-    } catch (IOException e)
-    {
-      // TODO Auto-generated catch block
-      e.printStackTrace();
-    }
+  }
 
 
-  }
 
 
-  public String getFilePath()
+  public static MainFrame createAptxFrame(
+          final Phylogeny[] aptxTrees)
   {
-    return filePath;
+    MainFrame aptxApp = Archaeopteryx.createApplication(aptxTrees,
+            "_aptx_jalview_configuration_file", null);
+    return aptxApp;
   }
 
-  public void setFilePath(String newFilePath)
+  public static JalviewTreeViewerBindingI<?> bindNodesToJalviewSequences(
+          final MainFrame aptxApp,
+          final AlignmentViewport jalviewAlignViewport,
+          final Map<SequenceI, PhylogenyNode> alignMappedToNodes,
+          final Map<PhylogenyNode, SequenceI> nodesMappedToAlign)
   {
-    this.filePath = newFilePath;
+    return new JalviewAptxBinding(aptxApp, jalviewAlignViewport,
+            alignMappedToNodes, nodesMappedToAlign);
   }
 
-  public TreeModel getTree()
-  {
-    return tree;
-  }
 
-  public void setTree(TreeModel newTree)
+  public static MainFrame bindFrameToJalview(final MainFrame aptxApp)
   {
-    this.tree = newTree;
-  }
+    int width = 400;
+    int height = 550;
+    aptxApp.setMinimumSize(new Dimension(width, height));
 
-  public NewickFile getNewickTree()
-  {
-    return newickTree;
-  }
+    Desktop.addInternalFrame(aptxApp, "Archaeopteryx Tree View", true,
+            width, height, true, true);
 
-  public void setNewickTree(NewickFile newNewickTree)
-  {
-    this.newickTree = newNewickTree;
-  }
+    return aptxApp;
 
-  public static NewickFile treeToNewick(TreeModel tree)
-  {
-    NewickFile newickTree = new NewickFile(tree.getTopNode(),
-            tree.hasBootstrap(), tree.hasDistances(),
-            tree.hasRootDistance());
-      /*     System.out.println(newickTree.print(tree.hasBootstrap(),
-             tree.hasDistances(), tree.hasRootDistance()));
-      
-           System.out.println(newickTree.print(newickTree.hasBootstrap(),
-             newickTree.hasDistances(), newickTree.hasRootDistance()));*/
-
-    return newickTree;
   }
 
-}
\ No newline at end of file
+
+}