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:
static TreeModel lastTree = null;
+ static Phylogeny lastAptxTree = null;
+
static boolean sortTreeAscending = true;
/*
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++)
{
{
// MAINTAINS ORIGNAL SEQUENCE ORDER,
// ORDERS BY GROUP SIZE
- List<SequenceGroup> groups = new ArrayList<SequenceGroup>();
+ List<SequenceGroup> groups = new ArrayList<>();
if (groups.hashCode() != lastGroupHash)
{
// 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++)
{
// 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()];
{
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)
{
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
*
}
/**
+ * 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
* @return DOCUMENT ME!
*/
private static List<SequenceI> _sortByTree(SequenceNode node,
- List<SequenceI> tmp, List<SequenceI> seqset)
+ List<SequenceI> tmp)
{
if (node == null)
{
}
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
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;
}