JAL-2838 Sorting from the alignment view menu now functional
authorkjvdheide <kjvanderheide@dundee.ac.uk>
Tue, 5 Dec 2017 16:08:42 +0000 (16:08 +0000)
committerkjvdheide <kjvanderheide@dundee.ac.uk>
Tue, 5 Dec 2017 16:08:42 +0000 (16:08 +0000)
src/jalview/analysis/AlignmentSorter.java
src/jalview/ext/archaeopteryx/JalviewBinding.java

index b5cefe0..bb3dbcc 100755 (executable)
@@ -35,6 +35,11 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
+
+import org.forester.phylogeny.Phylogeny;
+import org.forester.phylogeny.PhylogenyNode;
+import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
 
 /**
  * Routines for manipulating the order of a multiple sequence alignment TODO:
@@ -69,6 +74,8 @@ public class AlignmentSorter
 
   static TreeModel lastTree = null;
 
+  static Phylogeny lastAptxTree = null;
+
   static boolean sortTreeAscending = true;
 
   /*
@@ -182,7 +189,7 @@ public class AlignmentSorter
     List<SequenceI> algn;
     synchronized (algn = align.getSequences())
     {
-      List<SequenceI> tmp = new ArrayList<SequenceI>();
+      List<SequenceI> tmp = new ArrayList<>();
 
       for (int i = 0; i < seqs.length; i++)
       {
@@ -279,7 +286,7 @@ public class AlignmentSorter
   {
     // MAINTAINS ORIGNAL SEQUENCE ORDER,
     // ORDERS BY GROUP SIZE
-    List<SequenceGroup> groups = new ArrayList<SequenceGroup>();
+    List<SequenceGroup> groups = new ArrayList<>();
 
     if (groups.hashCode() != lastGroupHash)
     {
@@ -315,7 +322,7 @@ public class AlignmentSorter
 
     // NOW ADD SEQUENCES MAINTAINING ALIGNMENT ORDER
     // /////////////////////////////////////////////
-    List<SequenceI> seqs = new ArrayList<SequenceI>();
+    List<SequenceI> seqs = new ArrayList<>();
 
     for (int i = 0; i < groups.size(); i++)
     {
@@ -357,7 +364,7 @@ public class AlignmentSorter
     // tmp2 = tmp.retainAll(mask);
     // return tmp2.addAll(mask.removeAll(tmp2))
 
-    ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
+    ArrayList<SequenceI> seqs = new ArrayList<>();
     int i, idx;
     boolean[] tmask = new boolean[mask.size()];
 
@@ -436,9 +443,9 @@ public class AlignmentSorter
   {
     int nSeq = align.getHeight();
 
-    List<SequenceI> tmp = new ArrayList<SequenceI>();
+    List<SequenceI> tmp = new ArrayList<>();
 
-    tmp = _sortByTree(tree.getTopNode(), tmp, align.getSequences());
+    tmp = _sortByTree(tree.getTopNode(), tmp);
 
     if (tmp.size() != nSeq)
     {
@@ -461,6 +468,27 @@ public class AlignmentSorter
     return tmp;
   }
 
+
+
+  private static List<SequenceI> getOrderByTree(Phylogeny aptxTree,
+          Map<PhylogenyNode, SequenceI> nodesWithBoundSeqs)
+  {
+    List<SequenceI> seqsByTreeOrder = new ArrayList<>();
+    if (!aptxTree.isEmpty())
+    {
+      for (final PhylogenyNodeIterator iter = aptxTree
+              .iteratorPreorder(); iter.hasNext();)
+      {
+        PhylogenyNode treeNode = iter.next();
+        seqsByTreeOrder.add(nodesWithBoundSeqs.get(treeNode));
+      }
+
+    }
+    return seqsByTreeOrder;
+
+
+  }
+
   /**
    * Sorts the alignment by a given tree
    * 
@@ -496,6 +524,48 @@ public class AlignmentSorter
   }
 
   /**
+   * Sorts the alignment by a given tree from Archaeopteryx
+   * 
+   * @param align
+   *          alignment to order
+   * @param tree
+   *          tree which has
+   */
+  public static void sortByTree(AlignmentI align,
+          Map<PhylogenyNode, SequenceI> aptxNodesWithSeqs,
+          Phylogeny aptxTree) throws IllegalArgumentException
+  {
+    List<SequenceI> tmp = getOrderByTree(aptxTree, aptxNodesWithSeqs);
+
+    if (!tmp.isEmpty())
+    {
+      if (lastAptxTree != aptxTree)
+      {
+        sortTreeAscending = true;
+        lastAptxTree = aptxTree;
+      }
+      else
+      {
+        sortTreeAscending = !sortTreeAscending;
+      }
+
+      if (sortTreeAscending)
+      {
+        setOrder(align, tmp);
+      }
+      else
+      {
+        setReverseOrder(align,
+                vectorSubsetToArray(tmp, align.getSequences()));
+      }
+    }
+    else
+    {
+      throw new IllegalArgumentException();
+    }
+  }
+
+  /**
    * DOCUMENT ME!
    * 
    * @param align
@@ -535,7 +605,7 @@ public class AlignmentSorter
    * @return DOCUMENT ME!
    */
   private static List<SequenceI> _sortByTree(SequenceNode node,
-          List<SequenceI> tmp, List<SequenceI> seqset)
+          List<SequenceI> tmp)
   {
     if (node == null)
     {
@@ -564,13 +634,15 @@ public class AlignmentSorter
     }
     else
     {
-      _sortByTree(left, tmp, seqset);
-      _sortByTree(right, tmp, seqset);
+      _sortByTree(left, tmp);
+      _sortByTree(right, tmp);
     }
 
     return tmp;
   }
 
+
+
   // Ordering Objects
   // Alignment.sortBy(OrderObj) - sequence of sequence pointer refs in
   // appropriate order
index 80b2e50..eca66ae 100644 (file)
@@ -640,15 +640,27 @@ public final class JalviewBinding
   public CommandI sortAlignmentIn(AlignmentPanel ap)
   {
     // TODO: move to alignment view controller
+
     AlignmentViewport viewport = ap.av;
     SequenceI[] oldOrder = viewport.getAlignment().getSequencesArray();
+    try
+    {
     AlignmentSorter.sortByTree(viewport.getAlignment(),
-            treeView.getPhylogeny());
-    CommandI undo;
-    undo = new OrderCommand("Tree Sort", oldOrder, viewport.getAlignment());
+            nodesBoundToSequences,
+              treeView.getPhylogeny());
+      CommandI undo;
+      undo = new OrderCommand("Tree Sort", oldOrder,
+              viewport.getAlignment());
+
+      ap.paintAlignment(true, false);
+      return undo;
+
+    } catch (Exception e)
+    {
+      System.err.println(e.getMessage());
+    }
+    return null;
 
-    ap.paintAlignment(true, false);
-    return undo;
   }