2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 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 TODO:
28 * this class retains some global states concerning sort-order which should be
29 * made attributes for the caller's alignment visualization. TODO: refactor to
30 * allow a subset of selected sequences to be sorted within the context of a
31 * whole alignment. Sort method template is: SequenceI[] tobesorted, [ input
32 * data mapping to each tobesorted element to use ], Alignment context of
33 * tobesorted that are to be re-ordered, boolean sortinplace, [special data - ie
34 * seuqence to be sorted w.r.t.]) sortinplace implies that the sorted vector
35 * resulting from applying the operation to tobesorted should be mapped back to
36 * the original positions in alignment. Otherwise, normal behaviour is to re
37 * order alignment so that tobesorted is sorted and grouped together starting
38 * from the first tobesorted position in the alignment. e.g. (a,tb2,b,tb1,c,tb3
39 * becomes a,tb1,tb2,tb3,b,c)
41 public class AlignmentSorter
43 static boolean sortIdAscending = true;
45 static int lastGroupHash = 0;
47 static boolean sortGroupAscending = true;
49 static AlignmentOrder lastOrder = null;
51 static boolean sortOrderAscending = true;
53 static NJTree lastTree = null;
55 static boolean sortTreeAscending = true;
57 private static String lastSortByScore;
60 * Sort by Percentage Identity w.r.t. s
67 * sequences from align that are to be sorted.
69 public static void sortByPID(AlignmentI align, SequenceI s,
72 int nSeq = align.getHeight();
74 float[] scores = new float[nSeq];
75 SequenceI[] seqs = new SequenceI[nSeq];
77 for (int i = 0; i < nSeq; i++)
79 scores[i] = Comparison.PID(align.getSequenceAt(i)
80 .getSequenceAsString(), s.getSequenceAsString());
81 seqs[i] = align.getSequenceAt(i);
84 QuickSort.sort(scores, 0, scores.length - 1, seqs);
86 setReverseOrder(align, seqs);
90 * Reverse the order of the sort
97 private static void setReverseOrder(AlignmentI align, SequenceI[] seqs)
99 int nSeq = seqs.length;
109 len = (nSeq + 1) / 2;
112 // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work
113 for (int i = 0; i < len; i++)
115 // SequenceI tmp = seqs[i];
116 align.getSequences().setElementAt(seqs[nSeq - i - 1], i);
117 align.getSequences().setElementAt(seqs[i], nSeq - i - 1);
122 * Sets the Alignment object with the given sequences
125 * Alignment object to be updated
127 * sequences as a vector
129 private static void setOrder(AlignmentI align, Vector tmp)
131 setOrder(align, vectorSubsetToArray(tmp, align.getSequences()));
135 * Sets the Alignment object with the given sequences
140 * sequences as an array
142 public static void setOrder(AlignmentI align, SequenceI[] seqs)
144 // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work
145 Vector algn = align.getSequences();
146 Vector tmp = new Vector();
148 for (int i = 0; i < seqs.length; i++)
150 if (algn.contains(seqs[i]))
152 tmp.addElement(seqs[i]);
156 algn.removeAllElements();
157 // User may have hidden seqs, then clicked undo or redo
158 for (int i = 0; i < tmp.size(); i++)
160 algn.addElement(tmp.elementAt(i));
166 * Sorts by ID. Numbers are sorted before letters.
169 * The alignment object to sort
171 public static void sortByID(AlignmentI align)
173 int nSeq = align.getHeight();
175 String[] ids = new String[nSeq];
176 SequenceI[] seqs = new SequenceI[nSeq];
178 for (int i = 0; i < nSeq; i++)
180 ids[i] = align.getSequenceAt(i).getName();
181 seqs[i] = align.getSequenceAt(i);
184 QuickSort.sort(ids, seqs);
188 setReverseOrder(align, seqs);
192 setOrder(align, seqs);
195 sortIdAscending = !sortIdAscending;
199 * Sorts the alignment by size of group. <br>
200 * Maintains the order of sequences in each group by order in given alignment
204 * sorts the given alignment object by group
206 public static void sortByGroup(AlignmentI align)
208 // MAINTAINS ORIGNAL SEQUENCE ORDER,
209 // ORDERS BY GROUP SIZE
210 Vector groups = new Vector();
212 if (groups.hashCode() != lastGroupHash)
214 sortGroupAscending = true;
215 lastGroupHash = groups.hashCode();
219 sortGroupAscending = !sortGroupAscending;
222 // SORTS GROUPS BY SIZE
223 // ////////////////////
224 for (int i = 0; i < align.getGroups().size(); i++)
226 SequenceGroup sg = (SequenceGroup) align.getGroups().elementAt(i);
228 for (int j = 0; j < groups.size(); j++)
230 SequenceGroup sg2 = (SequenceGroup) groups.elementAt(j);
232 if (sg.getSize() > sg2.getSize())
234 groups.insertElementAt(sg, j);
240 if (!groups.contains(sg))
242 groups.addElement(sg);
246 // NOW ADD SEQUENCES MAINTAINING ALIGNMENT ORDER
247 // /////////////////////////////////////////////
248 Vector seqs = new Vector();
250 for (int i = 0; i < groups.size(); i++)
252 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
253 SequenceI[] orderedseqs = sg.getSequencesInOrder(align);
255 for (int j = 0; j < orderedseqs.length; j++)
257 seqs.addElement(orderedseqs[j]);
261 if (sortGroupAscending)
263 setOrder(align, seqs);
267 setReverseOrder(align,
268 vectorSubsetToArray(seqs, align.getSequences()));
273 * Converts Vector to array. java 1.18 does not have Vector.toArray()
276 * Vector of SequenceI objects
278 * @return array of Sequence[]
280 private static SequenceI[] vectorToArray(Vector tmp)
282 SequenceI[] seqs = new SequenceI[tmp.size()];
284 for (int i = 0; i < tmp.size(); i++)
286 seqs[i] = (SequenceI) tmp.elementAt(i);
300 * @return DOCUMENT ME!
302 private static SequenceI[] vectorSubsetToArray(Vector tmp, Vector mask)
304 Vector seqs = new Vector();
306 boolean[] tmask = new boolean[mask.size()];
308 for (i = 0; i < mask.size(); i++)
313 for (i = 0; i < tmp.size(); i++)
315 Object sq = tmp.elementAt(i);
317 if (mask.contains(sq) && tmask[mask.indexOf(sq)])
319 tmask[mask.indexOf(sq)] = false;
324 for (i = 0; i < tmask.length; i++)
328 seqs.addElement(mask.elementAt(i));
332 return vectorToArray(seqs);
336 * Sorts by a given AlignmentOrder object
341 * specified order for alignment
343 public static void sortBy(AlignmentI align, AlignmentOrder order)
345 // Get an ordered vector of sequences which may also be present in align
346 Vector tmp = order.getOrder();
348 if (lastOrder == order)
350 sortOrderAscending = !sortOrderAscending;
354 sortOrderAscending = true;
357 if (sortOrderAscending)
359 setOrder(align, tmp);
363 setReverseOrder(align, vectorSubsetToArray(tmp, align.getSequences()));
375 * @return DOCUMENT ME!
377 private static Vector getOrderByTree(AlignmentI align, NJTree tree)
379 int nSeq = align.getHeight();
381 Vector tmp = new Vector();
383 tmp = _sortByTree(tree.getTopNode(), tmp, align.getSequences());
385 if (tmp.size() != nSeq)
387 // TODO: JBPNote - decide if this is always an error
388 // (eg. not when a tree is associated to another alignment which has more
390 if (tmp.size() < nSeq)
392 addStrays(align, tmp);
395 if (tmp.size() != nSeq)
397 System.err.println("ERROR: tmp.size()=" + tmp.size() + " != nseq="
398 + nSeq + " in getOrderByTree");
406 * Sorts the alignment by a given tree
413 public static void sortByTree(AlignmentI align, NJTree tree)
415 Vector tmp = getOrderByTree(align, tree);
417 // tmp should properly permute align with tree.
418 if (lastTree != tree)
420 sortTreeAscending = true;
425 sortTreeAscending = !sortTreeAscending;
428 if (sortTreeAscending)
430 setOrder(align, tmp);
434 setReverseOrder(align, vectorSubsetToArray(tmp, align.getSequences()));
446 private static void addStrays(AlignmentI align, Vector seqs)
448 int nSeq = align.getHeight();
450 for (int i = 0; i < nSeq; i++)
452 if (!seqs.contains(align.getSequenceAt(i)))
454 seqs.addElement(align.getSequenceAt(i));
458 if (nSeq != seqs.size())
461 .println("ERROR: Size still not right even after addStrays");
475 * @return DOCUMENT ME!
477 private static Vector _sortByTree(SequenceNode node, Vector tmp,
485 SequenceNode left = (SequenceNode) node.left();
486 SequenceNode right = (SequenceNode) node.right();
488 if ((left == null) && (right == null))
490 if (!node.isPlaceholder() && (node.element() != null))
492 if (node.element() instanceof SequenceI)
494 if (!tmp.contains(node.element()))
496 tmp.addElement((SequenceI) node.element());
505 _sortByTree(left, tmp, seqset);
506 _sortByTree(right, tmp, seqset);
513 // Alignment.sortBy(OrderObj) - sequence of sequence pointer refs in
518 * recover the order of sequences given by the safe numbering scheme introducd
519 * SeqsetUtils.uniquify.
521 public static void recoverOrder(SequenceI[] alignment)
523 float[] ids = new float[alignment.length];
525 for (int i = 0; i < alignment.length; i++)
527 ids[i] = (new Float(alignment[i].getName().substring(8)))
531 jalview.util.QuickSort.sort(ids, alignment);
535 * Sort sequence in order of increasing score attribute for annotation with a
536 * particular scoreLabel. Or reverse if same label was used previously
539 * exact label for sequence associated AlignmentAnnotation
540 * scores to use for sorting.
542 * sequences to be sorted
544 public static void sortByAnnotationScore(String scoreLabel,
545 AlignmentI alignment)
547 SequenceI[] seqs = alignment.getSequencesArray();
548 boolean[] hasScore = new boolean[seqs.length]; // per sequence score
550 int hasScores = 0; // number of scores present on set
551 double[] scores = new double[seqs.length];
552 double min = 0, max = 0;
553 for (int i = 0; i < seqs.length; i++)
555 AlignmentAnnotation[] scoreAnn = seqs[i].getAnnotation(scoreLabel);
556 if (scoreAnn != null)
560 scores[i] = scoreAnn[0].getScore(); // take the first instance of this
564 max = min = scores[i];
585 return; // do nothing - no scores present to sort by.
587 if (hasScores < seqs.length)
589 for (int i = 0; i < seqs.length; i++)
593 scores[i] = (max + i);
598 jalview.util.QuickSort.sort(scores, seqs);
599 if (lastSortByScore != scoreLabel)
601 lastSortByScore = scoreLabel;
602 setOrder(alignment, seqs);
606 setReverseOrder(alignment, seqs);