2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.analysis;
23 import jalview.datamodel.*;
24 import jalview.util.*;
27 * Routines for manipulating the order of a multiple sequence alignment
28 * TODO: this class retains some global states concerning sort-order which should be made attributes for the caller's alignment visualization.
29 * TODO: refactor to allow a subset of selected sequences to be sorted within the context of a whole alignment.
30 * Sort method template is: SequenceI[] tobesorted, [ input data mapping to each tobesorted element to use ], Alignment context of tobesorted that are to be re-ordered, boolean sortinplace, [special data - ie seuqence to be sorted w.r.t.])
31 * sortinplace implies that the sorted vector resulting from applying the operation to tobesorted should be mapped back to the original positions in alignment.
32 * Otherwise, normal behaviour is to re order alignment so that tobesorted is sorted and grouped together starting from the first tobesorted position in the alignment.
33 * e.g. (a,tb2,b,tb1,c,tb3 becomes a,tb1,tb2,tb3,b,c)
35 public class AlignmentSorter
37 static boolean sortIdAscending = true;
38 static int lastGroupHash = 0;
39 static boolean sortGroupAscending = true;
40 static AlignmentOrder lastOrder = null;
41 static boolean sortOrderAscending = true;
42 static NJTree lastTree = null;
43 static boolean sortTreeAscending = true;
44 private static String lastSortByScore;
47 * Sort by Percentage Identity w.r.t. s
49 * @param align AlignmentI
51 * @param tosort sequences from align that are to be sorted.
53 public static void sortByPID(AlignmentI align, SequenceI s, SequenceI[] tosort)
55 int nSeq = align.getHeight();
57 float[] scores = new float[nSeq];
58 SequenceI[] seqs = new SequenceI[nSeq];
60 for (int i = 0; i < nSeq; i++)
62 scores[i] = Comparison.PID(align.getSequenceAt(i).getSequenceAsString(),
63 s.getSequenceAsString());
64 seqs[i] = align.getSequenceAt(i);
67 QuickSort.sort(scores, 0, scores.length - 1, seqs);
69 setReverseOrder(align, seqs);
73 * Reverse the order of the sort
75 * @param align DOCUMENT ME!
76 * @param seqs DOCUMENT ME!
78 private static void setReverseOrder(AlignmentI align, SequenceI[] seqs)
80 int nSeq = seqs.length;
93 // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work
94 for (int i = 0; i < len; i++)
96 //SequenceI tmp = seqs[i];
97 align.getSequences().setElementAt(seqs[nSeq - i - 1], i);
98 align.getSequences().setElementAt(seqs[i], nSeq - i - 1);
103 * Sets the Alignment object with the given sequences
105 * @param align Alignment object to be updated
106 * @param tmp sequences as a vector
108 private static void setOrder(AlignmentI align, Vector tmp)
110 setOrder(align, vectorSubsetToArray(tmp, align.getSequences()));
114 * Sets the Alignment object with the given sequences
116 * @param align DOCUMENT ME!
117 * @param seqs sequences as an array
119 public static void setOrder(AlignmentI align, SequenceI[] seqs)
121 // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work
122 Vector algn = align.getSequences();
123 Vector tmp = new Vector();
125 for (int i = 0; i < seqs.length; i++)
127 if (algn.contains(seqs[i]))
129 tmp.addElement(seqs[i]);
133 algn.removeAllElements();
134 //User may have hidden seqs, then clicked undo or redo
135 for (int i = 0; i < tmp.size(); i++)
137 algn.addElement(tmp.elementAt(i));
143 * Sorts by ID. Numbers are sorted before letters.
145 * @param align The alignment object to sort
147 public static void sortByID(AlignmentI align)
149 int nSeq = align.getHeight();
151 String[] ids = new String[nSeq];
152 SequenceI[] seqs = new SequenceI[nSeq];
154 for (int i = 0; i < nSeq; i++)
156 ids[i] = align.getSequenceAt(i).getName();
157 seqs[i] = align.getSequenceAt(i);
160 QuickSort.sort(ids, seqs);
164 setReverseOrder(align, seqs);
168 setOrder(align, seqs);
171 sortIdAscending = !sortIdAscending;
175 * Sorts the alignment by size of group.
176 * <br>Maintains the order of sequences in each group
177 * by order in given alignment object.
179 * @param align sorts the given alignment object by group
181 public static void sortByGroup(AlignmentI align)
183 //MAINTAINS ORIGNAL SEQUENCE ORDER,
184 //ORDERS BY GROUP SIZE
185 Vector groups = new Vector();
187 if (groups.hashCode() != lastGroupHash)
189 sortGroupAscending = true;
190 lastGroupHash = groups.hashCode();
194 sortGroupAscending = !sortGroupAscending;
197 //SORTS GROUPS BY SIZE
198 //////////////////////
199 for (int i = 0; i < align.getGroups().size(); i++)
201 SequenceGroup sg = (SequenceGroup) align.getGroups().elementAt(i);
203 for (int j = 0; j < groups.size(); j++)
205 SequenceGroup sg2 = (SequenceGroup) groups.elementAt(j);
207 if (sg.getSize() > sg2.getSize())
209 groups.insertElementAt(sg, j);
215 if (!groups.contains(sg))
217 groups.addElement(sg);
221 //NOW ADD SEQUENCES MAINTAINING ALIGNMENT ORDER
222 ///////////////////////////////////////////////
223 Vector seqs = new Vector();
225 for (int i = 0; i < groups.size(); i++)
227 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
228 SequenceI[] orderedseqs = sg.getSequencesInOrder(align);
230 for (int j = 0; j < orderedseqs.length; j++)
232 seqs.addElement(orderedseqs[j]);
236 if (sortGroupAscending)
238 setOrder(align, seqs);
242 setReverseOrder(align,
243 vectorSubsetToArray(seqs, align.getSequences()));
248 * Converts Vector to array.
249 * java 1.18 does not have Vector.toArray()
251 * @param tmp Vector of SequenceI objects
253 * @return array of Sequence[]
255 private static SequenceI[] vectorToArray(Vector tmp)
257 SequenceI[] seqs = new SequenceI[tmp.size()];
259 for (int i = 0; i < tmp.size(); i++)
261 seqs[i] = (SequenceI) tmp.elementAt(i);
270 * @param tmp DOCUMENT ME!
271 * @param mask DOCUMENT ME!
273 * @return DOCUMENT ME!
275 private static SequenceI[] vectorSubsetToArray(Vector tmp, Vector mask)
277 Vector seqs = new Vector();
279 boolean[] tmask = new boolean[mask.size()];
281 for (i = 0; i < mask.size(); i++)
286 for (i = 0; i < tmp.size(); i++)
288 Object sq = tmp.elementAt(i);
290 if (mask.contains(sq) && tmask[mask.indexOf(sq)])
292 tmask[mask.indexOf(sq)] = false;
297 for (i = 0; i < tmask.length; i++)
301 seqs.addElement(mask.elementAt(i));
305 return vectorToArray(seqs);
309 * Sorts by a given AlignmentOrder object
311 * @param align Alignment to order
312 * @param order specified order for alignment
314 public static void sortBy(AlignmentI align, AlignmentOrder order)
316 // Get an ordered vector of sequences which may also be present in align
317 Vector tmp = order.getOrder();
319 if (lastOrder == order)
321 sortOrderAscending = !sortOrderAscending;
325 sortOrderAscending = true;
328 if (sortOrderAscending)
330 setOrder(align, tmp);
334 setReverseOrder(align,
335 vectorSubsetToArray(tmp, align.getSequences()));
342 * @param align alignment to order
343 * @param tree tree which has
345 * @return DOCUMENT ME!
347 private static Vector getOrderByTree(AlignmentI align, NJTree tree)
349 int nSeq = align.getHeight();
351 Vector tmp = new Vector();
353 tmp = _sortByTree(tree.getTopNode(), tmp, align.getSequences());
355 if (tmp.size() != nSeq)
357 // TODO: JBPNote - decide if this is always an error
358 // (eg. not when a tree is associated to another alignment which has more
360 if (tmp.size() < nSeq)
362 addStrays(align, tmp);
365 if (tmp.size() != nSeq)
367 System.err.println("ERROR: tmp.size()=" + tmp.size() +
368 " != nseq=" + nSeq + " in getOrderByTree");
376 * Sorts the alignment by a given tree
378 * @param align alignment to order
379 * @param tree tree which has
381 public static void sortByTree(AlignmentI align, NJTree tree)
383 Vector tmp = getOrderByTree(align, tree);
385 // tmp should properly permute align with tree.
386 if (lastTree != tree)
388 sortTreeAscending = true;
393 sortTreeAscending = !sortTreeAscending;
396 if (sortTreeAscending)
398 setOrder(align, tmp);
402 setReverseOrder(align,
403 vectorSubsetToArray(tmp, align.getSequences()));
410 * @param align DOCUMENT ME!
411 * @param seqs DOCUMENT ME!
413 private static void addStrays(AlignmentI align, Vector seqs)
415 int nSeq = align.getHeight();
417 for (int i = 0; i < nSeq; i++)
419 if (!seqs.contains(align.getSequenceAt(i)))
421 seqs.addElement(align.getSequenceAt(i));
425 if (nSeq != seqs.size())
428 "ERROR: Size still not right even after addStrays");
435 * @param node DOCUMENT ME!
436 * @param tmp DOCUMENT ME!
437 * @param seqset DOCUMENT ME!
439 * @return DOCUMENT ME!
441 private static Vector _sortByTree(SequenceNode node, Vector tmp,
449 SequenceNode left = (SequenceNode) node.left();
450 SequenceNode right = (SequenceNode) node.right();
452 if ( (left == null) && (right == null))
454 if (!node.isPlaceholder() && (node.element() != null))
456 if (node.element() instanceof SequenceI)
458 if (!tmp.contains(node.element()))
460 tmp.addElement( (SequenceI) node.element());
469 _sortByTree(left, tmp, seqset);
470 _sortByTree(right, tmp, seqset);
477 // Alignment.sortBy(OrderObj) - sequence of sequence pointer refs in appropriate order
481 * recover the order of sequences given by the safe numbering scheme introducd
482 * SeqsetUtils.uniquify.
484 public static void recoverOrder(SequenceI[] alignment)
486 float[] ids = new float[alignment.length];
488 for (int i = 0; i < alignment.length; i++)
490 ids[i] = (new Float(alignment[i].getName().substring(8))).floatValue();
493 jalview.util.QuickSort.sort(ids, alignment);
496 * Sort sequence in order of increasing score attribute for annotation with a particular
497 * scoreLabel. Or reverse if same label was used previously
498 * @param scoreLabel exact label for sequence associated AlignmentAnnotation scores to use for sorting.
499 * @param alignment sequences to be sorted
501 public static void sortByAnnotationScore(String scoreLabel, AlignmentI alignment)
503 SequenceI[] seqs = alignment.getSequencesArray();
504 boolean[] hasScore = new boolean[seqs.length]; // per sequence score presence
505 int hasScores=0; // number of scores present on set
506 double[] scores = new double[seqs.length];
508 for (int i = 0; i < seqs.length; i++)
510 AlignmentAnnotation[] scoreAnn = seqs[i].getAnnotation(scoreLabel);
515 scores[i] = scoreAnn[0].getScore(); // take the first instance of this score.
518 max = min = scores[i];
538 return; // do nothing - no scores present to sort by.
540 if (hasScores<seqs.length)
542 for (int i=0; i<seqs.length;i++)
551 jalview.util.QuickSort.sort(scores, seqs);
552 if (lastSortByScore!=scoreLabel)
554 lastSortByScore = scoreLabel;
555 setOrder(alignment, seqs);
557 setReverseOrder(alignment, seqs);