JAL-2794 Aptx instances get their parent alignment, start on binding
authorkjvdheide <kjvanderheide@dundee.ac.uk>
Wed, 8 Nov 2017 17:59:24 +0000 (17:59 +0000)
committerkjvdheide <kjvanderheide@dundee.ac.uk>
Wed, 8 Nov 2017 17:59:24 +0000 (17:59 +0000)
Binding from Alignment to Aptx is now functional (including shift and/or
ctrl presses for multiple sequences), other way around is WIP

src/jalview/ext/archaeopteryx/ArchaeopteryxInit.java
src/jalview/ext/archaeopteryx/JalviewAptxBinding.java [new file with mode: 0644]
test/jalview/ext/archaeopteryx/AptxPhylogenyTreeTest.java

index 3bd9d19..9b654f0 100644 (file)
@@ -1,51 +1,86 @@
 package jalview.ext.archaeopteryx;
 
 import jalview.analysis.TreeBuilder;
+import jalview.datamodel.SequenceI;
 import jalview.gui.Desktop;
+import jalview.viewmodel.AlignmentViewport;
 
 import java.awt.Dimension;
+import java.util.Map;
 
 import org.forester.archaeopteryx.Archaeopteryx;
 import org.forester.archaeopteryx.MainFrame;
 import org.forester.phylogeny.Phylogeny;
+import org.forester.phylogeny.PhylogenyNode;
 
 public final class ArchaeopteryxInit
 {
-  public static MainFrame createInstance(Phylogeny[] aptxTrees)
+  /**
+   * This method should generally not be used as it does not bind the tree to
+   * its alignment
+   * 
+   * @param aptxTrees
+   * @return
+   */
+  public static MainFrame createUnboundInstance(Phylogeny aptxTree)
   {
-    return createBoundAptxFrame(aptxTrees);
+    Phylogeny[] aptxTrees = { aptxTree };
+    return createBoundAptxFrame(aptxTrees, null);
+  }
+
+  public static MainFrame createInstance(Phylogeny[] aptxTrees,
+          AlignmentViewport jalviewAlignmentView)
+  {
+    return createBoundAptxFrame(aptxTrees, jalviewAlignmentView);
 
   }
 
-  public static MainFrame createInstance(Phylogeny aptxTree)
+  public static MainFrame createInstance(Phylogeny aptxTree,
+          AlignmentViewport jalviewAlignmentView)
   {
     Phylogeny[] aptxTrees = { aptxTree }; // future possibility to load in
     // several trees simultaneously
-    return createBoundAptxFrame(aptxTrees);
+    return createBoundAptxFrame(aptxTrees, jalviewAlignmentView);
 
   }
 
   public static MainFrame createInstance(
-          TreeBuilder calculatedTree)
+          TreeBuilder calculatedTree) // very dense method, to be split up
   {
     ArchaeopteryxTreeConverter aptxTreeBuilder = new ArchaeopteryxTreeConverter(
             calculatedTree);
+
     Phylogeny aptxTree = aptxTreeBuilder.buildAptxTree();
     Phylogeny[] aptxTrees = { aptxTree }; // future possibility to load in
                                           // several trees simultaneously
 
-    return createBoundAptxFrame(aptxTrees);
+    MainFrame aptxApp = createBoundAptxFrame(aptxTrees,
+            calculatedTree.getAvport());
+    bindNodesToJalviewSequences(aptxApp, calculatedTree.getAvport(),
+            aptxTreeBuilder.getAlignmentBoundNodes());
+    return bindFrameToJalview(aptxApp);
 
   }
 
 
-  private static MainFrame createBoundAptxFrame(Phylogeny[] aptxTrees)
+
+
+  private static MainFrame createBoundAptxFrame(Phylogeny[] aptxTrees,
+          AlignmentViewport jalviewAlignmentView)
   {
     MainFrame aptxApp = Archaeopteryx.createApplication(aptxTrees,
             "_aptx_jalview_configuration_file", null);
-    return bindFrameToJalview(aptxApp);
+
+    return aptxApp;
   }
 
+  private static void bindNodesToJalviewSequences(MainFrame aptxApp,
+          AlignmentViewport jalviewAlignViewport,
+          Map<SequenceI, PhylogenyNode> alignMappedToNodes)
+  {
+    new JalviewAptxBinding(aptxApp, jalviewAlignViewport,
+            alignMappedToNodes);
+  }
 
 
   private static MainFrame bindFrameToJalview(MainFrame aptxApp)
diff --git a/src/jalview/ext/archaeopteryx/JalviewAptxBinding.java b/src/jalview/ext/archaeopteryx/JalviewAptxBinding.java
new file mode 100644 (file)
index 0000000..c2b48ab
--- /dev/null
@@ -0,0 +1,126 @@
+package jalview.ext.archaeopteryx;
+
+import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.HiddenColumns;
+import jalview.datamodel.SequenceGroup;
+import jalview.datamodel.SequenceI;
+import jalview.structure.SelectionSource;
+import jalview.structure.StructureSelectionManager;
+import jalview.viewmodel.AlignmentViewport;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.InputEvent;
+import java.awt.event.MouseEvent;
+import java.util.HashSet;
+import java.util.Map;
+
+import org.forester.archaeopteryx.MainFrame;
+import org.forester.phylogeny.PhylogenyNode;
+
+public class JalviewAptxBinding implements JalviewTreeViewerBindingI
+{
+  org.forester.archaeopteryx.TreePanel treeView;
+
+  AlignmentViewport parentAvport;
+
+  StructureSelectionManager ssm;
+
+  Map<SequenceI, PhylogenyNode> sequencesBelongingToNodes;
+
+
+  public JalviewAptxBinding(MainFrame archaeopteryx,
+          AlignmentViewport jalviewAlignmentViewport,
+          Map<SequenceI, PhylogenyNode> alignMappedToNodes)
+  {
+    parentAvport = jalviewAlignmentViewport;
+    sequencesBelongingToNodes = alignMappedToNodes;
+    treeView = archaeopteryx.getMainPanel().getCurrentTreePanel();
+    ssm = parentAvport.getStructureSelectionManager();
+    ssm.addSelectionListener(this);
+    treeView.addMouseListener(this);
+  }
+
+  @Override
+  public void actionPerformed(ActionEvent e)
+  {
+
+  }
+
+  @Override
+  public void mouseClicked(MouseEvent e)
+  {
+
+  }
+
+  @Override
+  public void mousePressed(MouseEvent e)
+  {
+    showNodeSelectionOnAlign(e);
+
+  }
+
+  @Override
+  public void mouseReleased(MouseEvent e)
+  {
+  }
+
+  @Override
+  public void mouseEntered(MouseEvent e)
+  {
+
+  }
+
+  @Override
+  public void mouseExited(MouseEvent e)
+  {
+
+  }
+
+  @Override
+  public void selection(SequenceGroup seqsel, ColumnSelection colsel,
+          HiddenColumns hidden, SelectionSource source)
+  {
+    if (source == parentAvport) // check if source is alignment from where the
+                                // tree originates
+    {
+      treeView.setFoundNodes0(
+              new HashSet<Long>(seqsel.getSequences().size()));
+
+      for (SequenceI sequence : seqsel.getSequences())
+      {
+        PhylogenyNode matchingNode = sequencesBelongingToNodes.get(sequence);
+        treeView.getFoundNodes0().add(matchingNode.getId());
+
+      }
+      treeView.repaint();
+
+    }
+
+
+  }
+
+  @Override
+  public void showNodeSelectionOnAlign(MouseEvent e)
+  {
+    final PhylogenyNode node = treeView.findNode(e.getX(), e.getY());
+    if (node != null && node.isExternal())
+    {
+
+      if ((e.getModifiers() & InputEvent.SHIFT_MASK) != 0) // shift is pressed
+      // (so multiple nodes
+      // can be selected)
+      {
+
+        System.out.println(treeView.getFoundNodes0().toString());
+      }
+    }
+
+  }
+
+
+
+
+  }
+
+
+
index 10e56c9..921150c 100644 (file)
@@ -61,7 +61,7 @@ public class AptxPhylogenyTreeTest extends TreeViewTest
   @BeforeClass(dependsOnMethods = { "setUpJalview", "setUpTree" })
   public void createTreeView()
   {
-    treeView = ArchaeopteryxInit.createInstance(inputTree);
+    treeView = ArchaeopteryxInit.createUnboundInstance(inputTree);
     aptx = (MainFrame) treeView; // pretty ugly
     treePanel = aptx.getMainPanel().getCurrentTreePanel();
     tree = treePanel.getPhylogeny();