2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.analysis;
\r
21 import jalview.datamodel.*;
\r
23 import jalview.util.*;
\r
28 /** Data structure to hold and manipulate a multiple sequence alignment
\r
30 public class AlignmentSorter
\r
32 static boolean sortIdAscending = true;
\r
33 static int lastGroupHash = 0;
\r
34 static boolean sortGroupAscending = true;
\r
35 static AlignmentOrder lastOrder = null;
\r
36 static boolean sortOrderAscending = true;
\r
37 static NJTree lastTree = null;
\r
38 static boolean sortTreeAscending = true;
\r
41 * Sort by Percentage Identity
\r
43 * @param align AlignmentI
\r
44 * @param s SequenceI
\r
46 public static void sortByPID(AlignmentI align, SequenceI s)
\r
48 int nSeq = align.getHeight();
\r
50 float[] scores = new float[nSeq];
\r
51 SequenceI[] seqs = new SequenceI[nSeq];
\r
53 for (int i = 0; i < nSeq; i++)
\r
55 scores[i] = Comparison.PID(align.getSequenceAt(i), s);
\r
56 seqs[i] = align.getSequenceAt(i);
\r
59 QuickSort.sort(scores, 0, scores.length - 1, seqs);
\r
61 setReverseOrder(align, seqs);
\r
65 * Reverse the order of the sort
\r
67 * @param align DOCUMENT ME!
\r
68 * @param seqs DOCUMENT ME!
\r
70 private static void setReverseOrder(AlignmentI align, SequenceI[] seqs)
\r
72 int nSeq = seqs.length;
\r
76 if ((nSeq % 2) == 0)
\r
82 len = (nSeq + 1) / 2;
\r
85 // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work
\r
86 for (int i = 0; i < len; i++)
\r
88 //SequenceI tmp = seqs[i];
\r
89 align.getSequences().setElementAt(seqs[nSeq - i - 1], i);
\r
90 align.getSequences().setElementAt(seqs[i], nSeq - i - 1);
\r
95 * Sets the Alignment object with the given sequences
\r
97 * @param align Alignment object to be updated
\r
98 * @param tmp sequences as a vector
\r
100 private static void setOrder(AlignmentI align, Vector tmp)
\r
102 setOrder(align, vectorSubsetToArray(tmp, align.getSequences()));
\r
106 * Sets the Alignment object with the given sequences
\r
108 * @param align DOCUMENT ME!
\r
109 * @param seqs sequences as an array
\r
111 private static void setOrder(AlignmentI align, SequenceI[] seqs)
\r
113 // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work
\r
114 Vector algn = align.getSequences();
\r
116 for (int i = 0; i < seqs.length; i++)
\r
118 algn.setElementAt(seqs[i], i);
\r
123 * Sorts by ID. Numbers are sorted before letters.
\r
125 * @param align The alignment object to sort
\r
127 public static void sortByID(AlignmentI align)
\r
129 int nSeq = align.getHeight();
\r
131 String[] ids = new String[nSeq];
\r
132 SequenceI[] seqs = new SequenceI[nSeq];
\r
134 for (int i = 0; i < nSeq; i++)
\r
136 ids[i] = align.getSequenceAt(i).getName();
\r
137 seqs[i] = align.getSequenceAt(i);
\r
140 QuickSort.sort(ids, seqs);
\r
142 if (sortIdAscending)
\r
144 setReverseOrder(align, seqs);
\r
148 setOrder(align, seqs);
\r
151 sortIdAscending = !sortIdAscending;
\r
155 * Sorts the alignment by size of group.
\r
156 * <br>Maintains the order of sequences in each group
\r
157 * by order in given alignment object.
\r
159 * @param align sorts the given alignment object by group
\r
161 public static void sortByGroup(AlignmentI align)
\r
163 //MAINTAINS ORIGNAL SEQUENCE ORDER,
\r
164 //ORDERS BY GROUP SIZE
\r
165 Vector groups = new Vector();
\r
167 if (groups.hashCode() != lastGroupHash)
\r
169 sortGroupAscending = true;
\r
170 lastGroupHash = groups.hashCode();
\r
174 sortGroupAscending = !sortGroupAscending;
\r
177 //SORTS GROUPS BY SIZE
\r
178 //////////////////////
\r
179 for (int i = 0; i < align.getGroups().size(); i++)
\r
181 SequenceGroup sg = (SequenceGroup) align.getGroups().elementAt(i);
\r
183 for (int j = 0; j < groups.size(); j++)
\r
185 SequenceGroup sg2 = (SequenceGroup) groups.elementAt(j);
\r
187 if (sg.getSize() > sg2.getSize())
\r
189 groups.insertElementAt(sg, j);
\r
195 if (!groups.contains(sg))
\r
197 groups.addElement(sg);
\r
201 //NOW ADD SEQUENCES MAINTAINING ALIGNMENT ORDER
\r
202 ///////////////////////////////////////////////
\r
203 Vector seqs = new Vector();
\r
205 for (int i = 0; i < groups.size(); i++)
\r
207 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
\r
208 SequenceI[] orderedseqs = sg.getSequencesInOrder(align);
\r
210 for (int j = 0; j < orderedseqs.length; j++)
\r
212 seqs.addElement(orderedseqs[j]);
\r
216 if (sortGroupAscending)
\r
218 setOrder(align, seqs);
\r
222 setReverseOrder(align,
\r
223 vectorSubsetToArray(seqs, align.getSequences()));
\r
228 * Converts Vector to array.
\r
229 * java 1.18 does not have Vector.toArray()
\r
231 * @param tmp Vector of SequenceI objects
\r
233 * @return array of Sequence[]
\r
235 private static SequenceI[] vectorToArray(Vector tmp)
\r
237 SequenceI[] seqs = new SequenceI[tmp.size()];
\r
239 for (int i = 0; i < tmp.size(); i++)
\r
241 seqs[i] = (SequenceI) tmp.elementAt(i);
\r
250 * @param tmp DOCUMENT ME!
\r
251 * @param mask DOCUMENT ME!
\r
253 * @return DOCUMENT ME!
\r
255 private static SequenceI[] vectorSubsetToArray(Vector tmp, Vector mask)
\r
257 Vector seqs = new Vector();
\r
259 boolean[] tmask = new boolean[mask.size()];
\r
261 for (i = 0; i < mask.size(); i++)
\r
264 for (i = 0; i < tmp.size(); i++)
\r
266 Object sq = tmp.elementAt(i);
\r
268 if (mask.contains(sq) && tmask[mask.indexOf(sq)])
\r
270 tmask[mask.indexOf(sq)] = false;
\r
271 seqs.addElement(sq);
\r
275 for (i = 0; i < tmask.length; i++)
\r
278 seqs.addElement(mask.elementAt(i));
\r
281 return vectorToArray(seqs);
\r
285 * Sorts by a given AlignmentOrder object
\r
287 * @param align Alignment to order
\r
288 * @param order specified order for alignment
\r
290 public static void sortBy(AlignmentI align, AlignmentOrder order)
\r
292 // Get an ordered vector of sequences which may also be present in align
\r
293 Vector tmp = order.getOrder();
\r
295 if (lastOrder == order)
\r
297 sortOrderAscending = !sortOrderAscending;
\r
301 sortOrderAscending = true;
\r
304 if (sortOrderAscending)
\r
306 setOrder(align, tmp);
\r
310 setReverseOrder(align,
\r
311 vectorSubsetToArray(tmp, align.getSequences()));
\r
318 * @param align alignment to order
\r
319 * @param tree tree which has
\r
321 * @return DOCUMENT ME!
\r
323 private static Vector getOrderByTree(AlignmentI align, NJTree tree)
\r
325 int nSeq = align.getHeight();
\r
327 Vector tmp = new Vector();
\r
329 tmp = _sortByTree(tree.getTopNode(), tmp, align.getSequences());
\r
331 if (tmp.size() != nSeq)
\r
333 // TODO: JBPNote - decide if this is always an error
\r
334 // (eg. not when a tree is associated to another alignment which has more
\r
336 if (tmp.size() < nSeq)
\r
338 addStrays(align, tmp);
\r
341 if (tmp.size() != nSeq)
\r
343 System.err.println("ERROR: tmp.size()=" + tmp.size() +
\r
344 " != nseq=" + nSeq + " in getOrderByTree");
\r
352 * Sorts the alignment by a given tree
\r
354 * @param align alignment to order
\r
355 * @param tree tree which has
\r
357 public static void sortByTree(AlignmentI align, NJTree tree)
\r
359 Vector tmp = getOrderByTree(align, tree);
\r
361 // tmp should properly permute align with tree.
\r
362 if (lastTree != tree)
\r
364 sortTreeAscending = true;
\r
369 sortTreeAscending = !sortTreeAscending;
\r
372 if (sortTreeAscending)
\r
374 setOrder(align, tmp);
\r
378 setReverseOrder(align,
\r
379 vectorSubsetToArray(tmp, align.getSequences()));
\r
386 * @param align DOCUMENT ME!
\r
387 * @param seqs DOCUMENT ME!
\r
389 private static void addStrays(AlignmentI align, Vector seqs)
\r
391 int nSeq = align.getHeight();
\r
393 for (int i = 0; i < nSeq; i++)
\r
395 if (!seqs.contains(align.getSequenceAt(i)))
\r
397 seqs.addElement(align.getSequenceAt(i));
\r
401 if (nSeq != seqs.size())
\r
403 System.err.println(
\r
404 "ERROR: Size still not right even after addStrays");
\r
411 * @param node DOCUMENT ME!
\r
412 * @param tmp DOCUMENT ME!
\r
413 * @param seqset DOCUMENT ME!
\r
415 * @return DOCUMENT ME!
\r
417 private static Vector _sortByTree(SequenceNode node, Vector tmp,
\r
425 SequenceNode left = (SequenceNode) node.left();
\r
426 SequenceNode right = (SequenceNode) node.right();
\r
428 if ((left == null) && (right == null))
\r
430 if (!node.isPlaceholder() && (node.element() != null))
\r
432 if (node.element() instanceof SequenceI)
\r
434 if (!tmp.contains(node.element()))
\r
436 tmp.addElement((SequenceI) node.element());
\r
445 _sortByTree(left, tmp, seqset);
\r
446 _sortByTree(right, tmp, seqset);
\r
452 // Ordering Objects
\r
453 // Alignment.sortBy(OrderObj) - sequence of sequence pointer refs in appropriate order
\r
457 * recover the order of sequences given by the safe numbering scheme introducd
\r
458 * SeqsetUtils.uniquify.
\r
460 public static void recoverOrder(SequenceI[] alignment)
\r
462 float[] ids = new float[alignment.length];
\r
464 for (int i = 0; i < alignment.length; i++)
\r
465 ids[i] = (new Float(alignment[i].getName().substring(8))).floatValue();
\r
467 jalview.util.QuickSort.sort(ids, alignment);
\r