JAL-2805 undid accidental double (buggy) binding, more interface support
authorkjvdheide <kjvanderheide@dundee.ac.uk>
Mon, 13 Nov 2017 19:02:16 +0000 (19:02 +0000)
committerkjvdheide <kjvanderheide@dundee.ac.uk>
Mon, 13 Nov 2017 19:02:16 +0000 (19:02 +0000)
src/jalview/ext/archaeopteryx/ArchaeopteryxInit.java
src/jalview/ext/archaeopteryx/JalviewAptxBinding.java
src/jalview/ext/archaeopteryx/JalviewToAptxAssociation.java [new file with mode: 0644]
src/jalview/ext/archaeopteryx/JalviewToSequenceAssociationI.java [new file with mode: 0644]
src/jalview/ext/archaeopteryx/JalviewTreeViewerBindingI.java
src/jalview/ext/forester/io/SupportedTreeFileFilter.java
src/jalview/ext/forester/io/TreeParser.java
src/jalview/viewmodel/AlignmentViewport.java

index dc887fb..ce0917d 100644 (file)
@@ -32,7 +32,7 @@ public final class ArchaeopteryxInit
   public static MainFrame createUnboundInstance(final Phylogeny aptxTree)
   {
     Phylogeny[] aptxTrees = { aptxTree };
-    return createAptxFrameInJalview(aptxTrees);
+    return createAptxFrame(aptxTrees);
   }
 
   // public static MainFrame createInstance(final Phylogeny[] aptxTrees,
@@ -61,7 +61,7 @@ public final class ArchaeopteryxInit
     Phylogeny[] aptxTrees = { aptxTree }; // future possibility to load in
                                           // several trees simultaneously
 
-    MainFrame aptxApp = createAptxFrameInJalview(aptxTrees);
+    MainFrame aptxApp = createAptxFrame(aptxTrees);
             
     bindNodesToJalviewSequences(aptxApp, calculatedTree.getAvport(),
             aptxTreeBuilder.getAlignmentBoundNodes(),
@@ -73,21 +73,21 @@ public final class ArchaeopteryxInit
 
 
 
-  public static MainFrame createAptxFrameInJalview(
+  public static MainFrame createAptxFrame(
           final Phylogeny[] aptxTrees)
   {
     MainFrame aptxApp = Archaeopteryx.createApplication(aptxTrees,
             "_aptx_jalview_configuration_file", null);
-    bindFrameToJalview(aptxApp);
     return aptxApp;
   }
 
-  public static void bindNodesToJalviewSequences(final MainFrame aptxApp,
+  public static JalviewTreeViewerBindingI bindNodesToJalviewSequences(
+          final MainFrame aptxApp,
           final AlignmentViewport jalviewAlignViewport,
           final Map<SequenceI, PhylogenyNode> alignMappedToNodes,
           final Map<PhylogenyNode, SequenceI> nodesMappedToAlign)
   {
-    new JalviewAptxBinding(aptxApp, jalviewAlignViewport,
+    return new JalviewAptxBinding(aptxApp, jalviewAlignViewport,
             alignMappedToNodes, nodesMappedToAlign);
   }
 
index e0a143c..f3305fd 100644 (file)
@@ -201,6 +201,7 @@ public final class JalviewAptxBinding implements JalviewTreeViewerBindingI
    * @param sequence
    *          of the node selected in the tree viewer.
    */
+  @Override
   public void treeSelectionChanged(final SequenceI sequence)
   {
     SequenceGroup selected = parentAvport.getSelectionGroup();
diff --git a/src/jalview/ext/archaeopteryx/JalviewToAptxAssociation.java b/src/jalview/ext/archaeopteryx/JalviewToAptxAssociation.java
new file mode 100644 (file)
index 0000000..4d34a96
--- /dev/null
@@ -0,0 +1,102 @@
+package jalview.ext.archaeopteryx;
+
+import jalview.analysis.SequenceIdMatcher;
+import jalview.datamodel.SequenceI;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.forester.phylogeny.Phylogeny;
+import org.forester.phylogeny.PhylogenyNode;
+
+public class JalviewToAptxAssociation
+        implements JalviewToSequenceAssociationI<Phylogeny>
+{
+  SequenceI[] alignSequences;
+
+  Phylogeny tree;
+
+  public JalviewToAptxAssociation(SequenceI[] alignmentSequences,
+          Phylogeny aptxTree)
+  {
+    alignSequences = alignmentSequences;
+    tree = aptxTree;
+
+  }
+
+
+
+  /**
+   * Tries to match sequences from Jalview with tree nodes in Archaeopteryx and
+   * fills in the tree node with sequence data if a match is found.
+   * 
+   * Partially refactored from the old Jalview TreeModel
+   * associateLeavesToSequences method.
+   *
+   * @param seqs
+   * @param aptxTree
+   */
+  @Override
+  public void associateLeavesToSequences(SequenceI[] seqs,
+          Phylogeny aptxTree)
+  {
+    SequenceIdMatcher algnIds = new SequenceIdMatcher(seqs);
+
+    List<PhylogenyNode> leaves = aptxTree.getExternalNodes();
+
+    int namesleft = seqs.length;
+    SequenceI nodeSequence;
+    String nodeSequenceName;
+    List<SequenceI> one2many = new ArrayList<>();
+    // int countOne2Many = 0;
+
+    for (PhylogenyNode treeNode : leaves)
+    {
+      nodeSequenceName = treeNode.getName();
+      nodeSequence = null;
+
+      if (namesleft > -1)
+      {
+        nodeSequence = algnIds.findIdMatch(nodeSequenceName);
+      }
+
+      if (nodeSequence != null)
+      {
+
+        //treeNode.setElement(nodeSequence);
+        if (one2many.contains(nodeSequence))
+        {
+          // countOne2Many++;
+          if (jalview.bin.Cache.log.isDebugEnabled())
+          {
+            jalview.bin.Cache.log.debug("One 2 many relationship for"
+             +nodeSequence.getName());
+          }
+        }
+        else
+        {
+          one2many.add(nodeSequence);
+          namesleft--;
+        }
+      }
+      else
+      {
+       // treeNode.setElement( new Sequence(nodeSequenceName, "THISISAPLACEHOLDER"));
+        // treeNode.setPlaceholder(true);
+      }
+    }
+    // if (jalview.bin.Cache.log.isDebugEnabled() && countOne2Many>0) {
+    // jalview.bin.Cache.log.debug("There were "+countOne2Many+" alignment
+    // sequence ids (out of "+one2many.size()+" unique ids) linked to two or
+    // more leaves.");
+    // }
+    // one2many.clear();
+
+  }
+}
+
+
+
+
+
+
diff --git a/src/jalview/ext/archaeopteryx/JalviewToSequenceAssociationI.java b/src/jalview/ext/archaeopteryx/JalviewToSequenceAssociationI.java
new file mode 100644 (file)
index 0000000..9f116a7
--- /dev/null
@@ -0,0 +1,8 @@
+package jalview.ext.archaeopteryx;
+
+import jalview.datamodel.SequenceI;
+
+interface JalviewToSequenceAssociationI<T>
+{
+  public void associateLeavesToSequences(SequenceI[] seqs, T tree);
+}
index e9d6f81..f506024 100644 (file)
@@ -20,6 +20,7 @@
  */
 package jalview.ext.archaeopteryx;
 
+import jalview.datamodel.SequenceI;
 import jalview.structure.SelectionListener;
 import jalview.structure.SelectionSource;
 
@@ -29,7 +30,7 @@ import java.awt.event.MouseListener;
 
 /**
  * Interface for binding a tree viewer to Jalview alignments. Assumes a tree
- * viewer will both want to receive and send selection events
+ * viewer will both want to receive and send selection events.
  * 
  * @author kjvanderheide
  *
@@ -40,5 +41,7 @@ interface JalviewTreeViewerBindingI
 {
   public void showNodeSelectionOnAlign(MouseEvent e);
 
+  public void treeSelectionChanged(final SequenceI sequence);
+
 
 }
index 214dd1d..b13c92a 100644 (file)
@@ -14,7 +14,7 @@ public enum SupportedTreeFileFilter
 
   private final FileFilter treeFilter;
 
-  SupportedTreeFileFilter(FileFilter treeFilter)
+  SupportedTreeFileFilter(final FileFilter treeFilter)
   {
     this.treeFilter = treeFilter;
 
index b5a29bf..48561dc 100644 (file)
@@ -18,7 +18,7 @@ public class TreeParser
 
   private final File file;
 
-  public TreeParser(String treeFilePath)
+  public TreeParser(final String treeFilePath)
   {
     final String possibleError = ForesterUtil.isReadableFile(treeFilePath);
     if (possibleError != null)
@@ -34,7 +34,7 @@ public class TreeParser
 
   }
 
-  public TreeParser(File treeFile) throws IOException
+  public TreeParser(final File treeFile) throws IOException
   {
     final String possibleError = ForesterUtil.isReadableFile(treeFile);
     if (possibleError != null)
@@ -48,13 +48,13 @@ public class TreeParser
     filePath = file.getCanonicalPath();
   }
 
-
-  public void loadTree()
+  public MainFrame loadTree()
   {
     String[] AptxArgs = new String[] { "-c",
         "_aptx_jalview_configuration_file", filePath };
     MainFrame aptx = Archaeopteryx.main(AptxArgs);
     ArchaeopteryxInit.bindFrameToJalview(aptx);
+    return aptx;
 
 
   }
index a0cbff4..d40ce8d 100644 (file)
@@ -2939,7 +2939,7 @@ public abstract class AlignmentViewport
   }
 
   @Override
-  public void setCurrentTree(TreeModel tree)
+  public void setCurrentTree(TreeModel tree) // adapt to Aptx
   {
     currentTree = tree;
   }