From: kjvdheide Date: Tue, 5 Dec 2017 16:08:42 +0000 (+0000) Subject: JAL-2838 Sorting from the alignment view menu now functional X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=b56c8f4155ed8cdf730ad0d11cfe95fb4f6af2ba;p=jalview.git JAL-2838 Sorting from the alignment view menu now functional --- diff --git a/src/jalview/analysis/AlignmentSorter.java b/src/jalview/analysis/AlignmentSorter.java index b5cefe0..bb3dbcc 100755 --- a/src/jalview/analysis/AlignmentSorter.java +++ b/src/jalview/analysis/AlignmentSorter.java @@ -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 algn; synchronized (algn = align.getSequences()) { - List tmp = new ArrayList(); + List 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 groups = new ArrayList(); + List groups = new ArrayList<>(); if (groups.hashCode() != lastGroupHash) { @@ -315,7 +322,7 @@ public class AlignmentSorter // NOW ADD SEQUENCES MAINTAINING ALIGNMENT ORDER // ///////////////////////////////////////////// - List seqs = new ArrayList(); + List 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 seqs = new ArrayList(); + ArrayList seqs = new ArrayList<>(); int i, idx; boolean[] tmask = new boolean[mask.size()]; @@ -436,9 +443,9 @@ public class AlignmentSorter { int nSeq = align.getHeight(); - List tmp = new ArrayList(); + List 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 getOrderByTree(Phylogeny aptxTree, + Map nodesWithBoundSeqs) + { + List 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 aptxNodesWithSeqs, + Phylogeny aptxTree) throws IllegalArgumentException + { + List 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 _sortByTree(SequenceNode node, - List tmp, List seqset) + List 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 diff --git a/src/jalview/ext/archaeopteryx/JalviewBinding.java b/src/jalview/ext/archaeopteryx/JalviewBinding.java index 80b2e50..eca66ae 100644 --- a/src/jalview/ext/archaeopteryx/JalviewBinding.java +++ b/src/jalview/ext/archaeopteryx/JalviewBinding.java @@ -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; }