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.
30 public class AlignmentSorter
32 static boolean sortIdAscending = true;
33 static int lastGroupHash = 0;
34 static boolean sortGroupAscending = true;
35 static AlignmentOrder lastOrder = null;
36 static boolean sortOrderAscending = true;
37 static NJTree lastTree = null;
38 static boolean sortTreeAscending = true;
39 private static String lastSortByScore;
42 * Sort by Percentage Identity
44 * @param align AlignmentI
47 public static void sortByPID(AlignmentI align, SequenceI s)
49 int nSeq = align.getHeight();
51 float[] scores = new float[nSeq];
52 SequenceI[] seqs = new SequenceI[nSeq];
54 for (int i = 0; i < nSeq; i++)
56 scores[i] = Comparison.PID(align.getSequenceAt(i).getSequenceAsString(),
57 s.getSequenceAsString());
58 seqs[i] = align.getSequenceAt(i);
61 QuickSort.sort(scores, 0, scores.length - 1, seqs);
63 setReverseOrder(align, seqs);
67 * Reverse the order of the sort
69 * @param align DOCUMENT ME!
70 * @param seqs DOCUMENT ME!
72 private static void setReverseOrder(AlignmentI align, SequenceI[] seqs)
74 int nSeq = seqs.length;
87 // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work
88 for (int i = 0; i < len; i++)
90 //SequenceI tmp = seqs[i];
91 align.getSequences().setElementAt(seqs[nSeq - i - 1], i);
92 align.getSequences().setElementAt(seqs[i], nSeq - i - 1);
97 * Sets the Alignment object with the given sequences
99 * @param align Alignment object to be updated
100 * @param tmp sequences as a vector
102 private static void setOrder(AlignmentI align, Vector tmp)
104 setOrder(align, vectorSubsetToArray(tmp, align.getSequences()));
108 * Sets the Alignment object with the given sequences
110 * @param align DOCUMENT ME!
111 * @param seqs sequences as an array
113 public static void setOrder(AlignmentI align, SequenceI[] seqs)
115 // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work
116 Vector algn = align.getSequences();
117 Vector tmp = new Vector();
119 for (int i = 0; i < seqs.length; i++)
121 if (algn.contains(seqs[i]))
123 tmp.addElement(seqs[i]);
127 algn.removeAllElements();
128 //User may have hidden seqs, then clicked undo or redo
129 for (int i = 0; i < tmp.size(); i++)
131 algn.addElement(tmp.elementAt(i));
137 * Sorts by ID. Numbers are sorted before letters.
139 * @param align The alignment object to sort
141 public static void sortByID(AlignmentI align)
143 int nSeq = align.getHeight();
145 String[] ids = new String[nSeq];
146 SequenceI[] seqs = new SequenceI[nSeq];
148 for (int i = 0; i < nSeq; i++)
150 ids[i] = align.getSequenceAt(i).getName();
151 seqs[i] = align.getSequenceAt(i);
154 QuickSort.sort(ids, seqs);
158 setReverseOrder(align, seqs);
162 setOrder(align, seqs);
165 sortIdAscending = !sortIdAscending;
169 * Sorts the alignment by size of group.
170 * <br>Maintains the order of sequences in each group
171 * by order in given alignment object.
173 * @param align sorts the given alignment object by group
175 public static void sortByGroup(AlignmentI align)
177 //MAINTAINS ORIGNAL SEQUENCE ORDER,
178 //ORDERS BY GROUP SIZE
179 Vector groups = new Vector();
181 if (groups.hashCode() != lastGroupHash)
183 sortGroupAscending = true;
184 lastGroupHash = groups.hashCode();
188 sortGroupAscending = !sortGroupAscending;
191 //SORTS GROUPS BY SIZE
192 //////////////////////
193 for (int i = 0; i < align.getGroups().size(); i++)
195 SequenceGroup sg = (SequenceGroup) align.getGroups().elementAt(i);
197 for (int j = 0; j < groups.size(); j++)
199 SequenceGroup sg2 = (SequenceGroup) groups.elementAt(j);
201 if (sg.getSize() > sg2.getSize())
203 groups.insertElementAt(sg, j);
209 if (!groups.contains(sg))
211 groups.addElement(sg);
215 //NOW ADD SEQUENCES MAINTAINING ALIGNMENT ORDER
216 ///////////////////////////////////////////////
217 Vector seqs = new Vector();
219 for (int i = 0; i < groups.size(); i++)
221 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
222 SequenceI[] orderedseqs = sg.getSequencesInOrder(align);
224 for (int j = 0; j < orderedseqs.length; j++)
226 seqs.addElement(orderedseqs[j]);
230 if (sortGroupAscending)
232 setOrder(align, seqs);
236 setReverseOrder(align,
237 vectorSubsetToArray(seqs, align.getSequences()));
242 * Converts Vector to array.
243 * java 1.18 does not have Vector.toArray()
245 * @param tmp Vector of SequenceI objects
247 * @return array of Sequence[]
249 private static SequenceI[] vectorToArray(Vector tmp)
251 SequenceI[] seqs = new SequenceI[tmp.size()];
253 for (int i = 0; i < tmp.size(); i++)
255 seqs[i] = (SequenceI) tmp.elementAt(i);
264 * @param tmp DOCUMENT ME!
265 * @param mask DOCUMENT ME!
267 * @return DOCUMENT ME!
269 private static SequenceI[] vectorSubsetToArray(Vector tmp, Vector mask)
271 Vector seqs = new Vector();
273 boolean[] tmask = new boolean[mask.size()];
275 for (i = 0; i < mask.size(); i++)
280 for (i = 0; i < tmp.size(); i++)
282 Object sq = tmp.elementAt(i);
284 if (mask.contains(sq) && tmask[mask.indexOf(sq)])
286 tmask[mask.indexOf(sq)] = false;
291 for (i = 0; i < tmask.length; i++)
295 seqs.addElement(mask.elementAt(i));
299 return vectorToArray(seqs);
303 * Sorts by a given AlignmentOrder object
305 * @param align Alignment to order
306 * @param order specified order for alignment
308 public static void sortBy(AlignmentI align, AlignmentOrder order)
310 // Get an ordered vector of sequences which may also be present in align
311 Vector tmp = order.getOrder();
313 if (lastOrder == order)
315 sortOrderAscending = !sortOrderAscending;
319 sortOrderAscending = true;
322 if (sortOrderAscending)
324 setOrder(align, tmp);
328 setReverseOrder(align,
329 vectorSubsetToArray(tmp, align.getSequences()));
336 * @param align alignment to order
337 * @param tree tree which has
339 * @return DOCUMENT ME!
341 private static Vector getOrderByTree(AlignmentI align, NJTree tree)
343 int nSeq = align.getHeight();
345 Vector tmp = new Vector();
347 tmp = _sortByTree(tree.getTopNode(), tmp, align.getSequences());
349 if (tmp.size() != nSeq)
351 // TODO: JBPNote - decide if this is always an error
352 // (eg. not when a tree is associated to another alignment which has more
354 if (tmp.size() < nSeq)
356 addStrays(align, tmp);
359 if (tmp.size() != nSeq)
361 System.err.println("ERROR: tmp.size()=" + tmp.size() +
362 " != nseq=" + nSeq + " in getOrderByTree");
370 * Sorts the alignment by a given tree
372 * @param align alignment to order
373 * @param tree tree which has
375 public static void sortByTree(AlignmentI align, NJTree tree)
377 Vector tmp = getOrderByTree(align, tree);
379 // tmp should properly permute align with tree.
380 if (lastTree != tree)
382 sortTreeAscending = true;
387 sortTreeAscending = !sortTreeAscending;
390 if (sortTreeAscending)
392 setOrder(align, tmp);
396 setReverseOrder(align,
397 vectorSubsetToArray(tmp, align.getSequences()));
404 * @param align DOCUMENT ME!
405 * @param seqs DOCUMENT ME!
407 private static void addStrays(AlignmentI align, Vector seqs)
409 int nSeq = align.getHeight();
411 for (int i = 0; i < nSeq; i++)
413 if (!seqs.contains(align.getSequenceAt(i)))
415 seqs.addElement(align.getSequenceAt(i));
419 if (nSeq != seqs.size())
422 "ERROR: Size still not right even after addStrays");
429 * @param node DOCUMENT ME!
430 * @param tmp DOCUMENT ME!
431 * @param seqset DOCUMENT ME!
433 * @return DOCUMENT ME!
435 private static Vector _sortByTree(SequenceNode node, Vector tmp,
443 SequenceNode left = (SequenceNode) node.left();
444 SequenceNode right = (SequenceNode) node.right();
446 if ( (left == null) && (right == null))
448 if (!node.isPlaceholder() && (node.element() != null))
450 if (node.element() instanceof SequenceI)
452 if (!tmp.contains(node.element()))
454 tmp.addElement( (SequenceI) node.element());
463 _sortByTree(left, tmp, seqset);
464 _sortByTree(right, tmp, seqset);
471 // Alignment.sortBy(OrderObj) - sequence of sequence pointer refs in appropriate order
475 * recover the order of sequences given by the safe numbering scheme introducd
476 * SeqsetUtils.uniquify.
478 public static void recoverOrder(SequenceI[] alignment)
480 float[] ids = new float[alignment.length];
482 for (int i = 0; i < alignment.length; i++)
484 ids[i] = (new Float(alignment[i].getName().substring(8))).floatValue();
487 jalview.util.QuickSort.sort(ids, alignment);
490 * Sort sequence in order of increasing score attribute for annotation with a particular
491 * scoreLabel. Or reverse if same label was used previously
492 * @param scoreLabel exact label for sequence associated AlignmentAnnotation scores to use for sorting.
493 * @param alignment sequences to be sorted
495 public static void sortByAnnotationScore(String scoreLabel, AlignmentI alignment)
497 SequenceI[] seqs = alignment.getSequencesArray();
498 boolean[] hasScore = new boolean[seqs.length]; // per sequence score presence
499 int hasScores=0; // number of scores present on set
500 double[] scores = new double[seqs.length];
502 for (int i = 0; i < seqs.length; i++)
504 AlignmentAnnotation[] scoreAnn = seqs[i].getAnnotation(scoreLabel);
509 scores[i] = scoreAnn[0].getScore(); // take the first instance of this score.
512 max = min = scores[i];
532 return; // do nothing - no scores present to sort by.
534 if (hasScores<seqs.length)
536 for (int i=0; i<seqs.length;i++)
545 jalview.util.QuickSort.sort(scores, seqs);
546 if (lastSortByScore!=scoreLabel)
548 lastSortByScore = scoreLabel;
549 setOrder(alignment, seqs);
551 setReverseOrder(alignment, seqs);