2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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;
21 import jalview.datamodel.*;
23 import jalview.util.*;
28 /** Data structure to hold and manipulate a multiple sequence alignment
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;
41 * Sort by Percentage Identity
43 * @param align AlignmentI
46 public static void sortByPID(AlignmentI align, SequenceI s)
48 int nSeq = align.getHeight();
50 float[] scores = new float[nSeq];
51 SequenceI[] seqs = new SequenceI[nSeq];
53 for (int i = 0; i < nSeq; i++)
55 scores[i] = Comparison.PID(align.getSequenceAt(i).getSequence(),
57 seqs[i] = align.getSequenceAt(i);
60 QuickSort.sort(scores, 0, scores.length - 1, seqs);
62 setReverseOrder(align, seqs);
66 * Reverse the order of the sort
68 * @param align DOCUMENT ME!
69 * @param seqs DOCUMENT ME!
71 private static void setReverseOrder(AlignmentI align, SequenceI[] seqs)
73 int nSeq = seqs.length;
86 // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work
87 for (int i = 0; i < len; i++)
89 //SequenceI tmp = seqs[i];
90 align.getSequences().setElementAt(seqs[nSeq - i - 1], i);
91 align.getSequences().setElementAt(seqs[i], nSeq - i - 1);
96 * Sets the Alignment object with the given sequences
98 * @param align Alignment object to be updated
99 * @param tmp sequences as a vector
101 private static void setOrder(AlignmentI align, Vector tmp)
103 setOrder(align, vectorSubsetToArray(tmp, align.getSequences()));
107 * Sets the Alignment object with the given sequences
109 * @param align DOCUMENT ME!
110 * @param seqs sequences as an array
112 private static void setOrder(AlignmentI align, SequenceI[] seqs)
114 // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work
115 Vector algn = align.getSequences();
117 for (int i = 0; i < seqs.length; i++)
119 algn.setElementAt(seqs[i], i);
124 * Sorts by ID. Numbers are sorted before letters.
126 * @param align The alignment object to sort
128 public static void sortByID(AlignmentI align)
130 int nSeq = align.getHeight();
132 String[] ids = new String[nSeq];
133 SequenceI[] seqs = new SequenceI[nSeq];
135 for (int i = 0; i < nSeq; i++)
137 ids[i] = align.getSequenceAt(i).getName();
138 seqs[i] = align.getSequenceAt(i);
141 QuickSort.sort(ids, seqs);
145 setReverseOrder(align, seqs);
149 setOrder(align, seqs);
152 sortIdAscending = !sortIdAscending;
156 * Sorts the alignment by size of group.
157 * <br>Maintains the order of sequences in each group
158 * by order in given alignment object.
160 * @param align sorts the given alignment object by group
162 public static void sortByGroup(AlignmentI align)
164 //MAINTAINS ORIGNAL SEQUENCE ORDER,
165 //ORDERS BY GROUP SIZE
166 Vector groups = new Vector();
168 if (groups.hashCode() != lastGroupHash)
170 sortGroupAscending = true;
171 lastGroupHash = groups.hashCode();
175 sortGroupAscending = !sortGroupAscending;
178 //SORTS GROUPS BY SIZE
179 //////////////////////
180 for (int i = 0; i < align.getGroups().size(); i++)
182 SequenceGroup sg = (SequenceGroup) align.getGroups().elementAt(i);
184 for (int j = 0; j < groups.size(); j++)
186 SequenceGroup sg2 = (SequenceGroup) groups.elementAt(j);
188 if (sg.getSize(false) > sg2.getSize(false))
190 groups.insertElementAt(sg, j);
196 if (!groups.contains(sg))
198 groups.addElement(sg);
202 //NOW ADD SEQUENCES MAINTAINING ALIGNMENT ORDER
203 ///////////////////////////////////////////////
204 Vector seqs = new Vector();
206 for (int i = 0; i < groups.size(); i++)
208 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
209 SequenceI[] orderedseqs = sg.getSequencesInOrder(align);
211 for (int j = 0; j < orderedseqs.length; j++)
213 seqs.addElement(orderedseqs[j]);
217 if (sortGroupAscending)
219 setOrder(align, seqs);
223 setReverseOrder(align,
224 vectorSubsetToArray(seqs, align.getSequences()));
229 * Converts Vector to array.
230 * java 1.18 does not have Vector.toArray()
232 * @param tmp Vector of SequenceI objects
234 * @return array of Sequence[]
236 private static SequenceI[] vectorToArray(Vector tmp)
238 SequenceI[] seqs = new SequenceI[tmp.size()];
240 for (int i = 0; i < tmp.size(); i++)
242 seqs[i] = (SequenceI) tmp.elementAt(i);
251 * @param tmp DOCUMENT ME!
252 * @param mask DOCUMENT ME!
254 * @return DOCUMENT ME!
256 private static SequenceI[] vectorSubsetToArray(Vector tmp, Vector mask)
258 Vector seqs = new Vector();
260 boolean[] tmask = new boolean[mask.size()];
262 for (i = 0; i < mask.size(); i++)
265 for (i = 0; i < tmp.size(); i++)
267 Object sq = tmp.elementAt(i);
269 if (mask.contains(sq) && tmask[mask.indexOf(sq)])
271 tmask[mask.indexOf(sq)] = false;
276 for (i = 0; i < tmask.length; i++)
279 seqs.addElement(mask.elementAt(i));
282 return vectorToArray(seqs);
286 * Sorts by a given AlignmentOrder object
288 * @param align Alignment to order
289 * @param order specified order for alignment
291 public static void sortBy(AlignmentI align, AlignmentOrder order)
293 // Get an ordered vector of sequences which may also be present in align
294 Vector tmp = order.getOrder();
296 if (lastOrder == order)
298 sortOrderAscending = !sortOrderAscending;
302 sortOrderAscending = true;
305 if (sortOrderAscending)
307 setOrder(align, tmp);
311 setReverseOrder(align,
312 vectorSubsetToArray(tmp, align.getSequences()));
319 * @param align alignment to order
320 * @param tree tree which has
322 * @return DOCUMENT ME!
324 private static Vector getOrderByTree(AlignmentI align, NJTree tree)
326 int nSeq = align.getHeight();
328 Vector tmp = new Vector();
330 tmp = _sortByTree(tree.getTopNode(), tmp, align.getSequences());
332 if (tmp.size() != nSeq)
334 // TODO: JBPNote - decide if this is always an error
335 // (eg. not when a tree is associated to another alignment which has more
337 if (tmp.size() < nSeq)
339 addStrays(align, tmp);
342 if (tmp.size() != nSeq)
344 System.err.println("ERROR: tmp.size()=" + tmp.size() +
345 " != nseq=" + nSeq + " in getOrderByTree");
353 * Sorts the alignment by a given tree
355 * @param align alignment to order
356 * @param tree tree which has
358 public static void sortByTree(AlignmentI align, NJTree tree)
360 Vector tmp = getOrderByTree(align, tree);
362 // tmp should properly permute align with tree.
363 if (lastTree != tree)
365 sortTreeAscending = true;
370 sortTreeAscending = !sortTreeAscending;
373 if (sortTreeAscending)
375 setOrder(align, tmp);
379 setReverseOrder(align,
380 vectorSubsetToArray(tmp, align.getSequences()));
387 * @param align DOCUMENT ME!
388 * @param seqs DOCUMENT ME!
390 private static void addStrays(AlignmentI align, Vector seqs)
392 int nSeq = align.getHeight();
394 for (int i = 0; i < nSeq; i++)
396 if (!seqs.contains(align.getSequenceAt(i)))
398 seqs.addElement(align.getSequenceAt(i));
402 if (nSeq != seqs.size())
405 "ERROR: Size still not right even after addStrays");
412 * @param node DOCUMENT ME!
413 * @param tmp DOCUMENT ME!
414 * @param seqset DOCUMENT ME!
416 * @return DOCUMENT ME!
418 private static Vector _sortByTree(SequenceNode node, Vector tmp,
426 SequenceNode left = (SequenceNode) node.left();
427 SequenceNode right = (SequenceNode) node.right();
429 if ((left == null) && (right == null))
431 if (!node.isPlaceholder() && (node.element() != null))
433 if (node.element() instanceof SequenceI)
435 if (!tmp.contains(node.element()))
437 tmp.addElement((SequenceI) node.element());
446 _sortByTree(left, tmp, seqset);
447 _sortByTree(right, tmp, seqset);
454 // Alignment.sortBy(OrderObj) - sequence of sequence pointer refs in appropriate order
458 * recover the order of sequences given by the safe numbering scheme introducd
459 * SeqsetUtils.uniquify.
461 public static void recoverOrder(SequenceI[] alignment)
463 float[] ids = new float[alignment.length];
465 for (int i = 0; i < alignment.length; i++)
466 ids[i] = (new Float(alignment[i].getName().substring(8))).floatValue();
468 jalview.util.QuickSort.sort(ids, alignment);