2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 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).getSequence(),
\r
57 seqs[i] = align.getSequenceAt(i);
\r
60 QuickSort.sort(scores, 0, scores.length - 1, seqs);
\r
62 setReverseOrder(align, seqs);
\r
66 * Reverse the order of the sort
\r
68 * @param align DOCUMENT ME!
\r
69 * @param seqs DOCUMENT ME!
\r
71 private static void setReverseOrder(AlignmentI align, SequenceI[] seqs)
\r
73 int nSeq = seqs.length;
\r
77 if ((nSeq % 2) == 0)
\r
83 len = (nSeq + 1) / 2;
\r
86 // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work
\r
87 for (int i = 0; i < len; i++)
\r
89 //SequenceI tmp = seqs[i];
\r
90 align.getSequences().setElementAt(seqs[nSeq - i - 1], i);
\r
91 align.getSequences().setElementAt(seqs[i], nSeq - i - 1);
\r
96 * Sets the Alignment object with the given sequences
\r
98 * @param align Alignment object to be updated
\r
99 * @param tmp sequences as a vector
\r
101 private static void setOrder(AlignmentI align, Vector tmp)
\r
103 setOrder(align, vectorSubsetToArray(tmp, align.getSequences()));
\r
107 * Sets the Alignment object with the given sequences
\r
109 * @param align DOCUMENT ME!
\r
110 * @param seqs sequences as an array
\r
112 public static void setOrder(AlignmentI align, SequenceI[] seqs)
\r
114 // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work
\r
115 Vector algn = align.getSequences();
\r
118 for (int i = 0; i < seqs.length; i++)
\r
120 if(algn.contains(seqs[i]))
\r
121 algn.setElementAt(seqs[i], index++);
\r
126 * Sorts by ID. Numbers are sorted before letters.
\r
128 * @param align The alignment object to sort
\r
130 public static void sortByID(AlignmentI align)
\r
132 int nSeq = align.getHeight();
\r
134 String[] ids = new String[nSeq];
\r
135 SequenceI[] seqs = new SequenceI[nSeq];
\r
137 for (int i = 0; i < nSeq; i++)
\r
139 ids[i] = align.getSequenceAt(i).getName();
\r
140 seqs[i] = align.getSequenceAt(i);
\r
143 QuickSort.sort(ids, seqs);
\r
145 if (sortIdAscending)
\r
147 setReverseOrder(align, seqs);
\r
151 setOrder(align, seqs);
\r
154 sortIdAscending = !sortIdAscending;
\r
158 * Sorts the alignment by size of group.
\r
159 * <br>Maintains the order of sequences in each group
\r
160 * by order in given alignment object.
\r
162 * @param align sorts the given alignment object by group
\r
164 public static void sortByGroup(AlignmentI align)
\r
166 //MAINTAINS ORIGNAL SEQUENCE ORDER,
\r
167 //ORDERS BY GROUP SIZE
\r
168 Vector groups = new Vector();
\r
170 if (groups.hashCode() != lastGroupHash)
\r
172 sortGroupAscending = true;
\r
173 lastGroupHash = groups.hashCode();
\r
177 sortGroupAscending = !sortGroupAscending;
\r
180 //SORTS GROUPS BY SIZE
\r
181 //////////////////////
\r
182 for (int i = 0; i < align.getGroups().size(); i++)
\r
184 SequenceGroup sg = (SequenceGroup) align.getGroups().elementAt(i);
\r
186 for (int j = 0; j < groups.size(); j++)
\r
188 SequenceGroup sg2 = (SequenceGroup) groups.elementAt(j);
\r
190 if (sg.getSize(false) > sg2.getSize(false))
\r
192 groups.insertElementAt(sg, j);
\r
198 if (!groups.contains(sg))
\r
200 groups.addElement(sg);
\r
204 //NOW ADD SEQUENCES MAINTAINING ALIGNMENT ORDER
\r
205 ///////////////////////////////////////////////
\r
206 Vector seqs = new Vector();
\r
208 for (int i = 0; i < groups.size(); i++)
\r
210 SequenceGroup sg = (SequenceGroup) groups.elementAt(i);
\r
211 SequenceI[] orderedseqs = sg.getSequencesInOrder(align);
\r
213 for (int j = 0; j < orderedseqs.length; j++)
\r
215 seqs.addElement(orderedseqs[j]);
\r
219 if (sortGroupAscending)
\r
221 setOrder(align, seqs);
\r
225 setReverseOrder(align,
\r
226 vectorSubsetToArray(seqs, align.getSequences()));
\r
231 * Converts Vector to array.
\r
232 * java 1.18 does not have Vector.toArray()
\r
234 * @param tmp Vector of SequenceI objects
\r
236 * @return array of Sequence[]
\r
238 private static SequenceI[] vectorToArray(Vector tmp)
\r
240 SequenceI[] seqs = new SequenceI[tmp.size()];
\r
242 for (int i = 0; i < tmp.size(); i++)
\r
244 seqs[i] = (SequenceI) tmp.elementAt(i);
\r
253 * @param tmp DOCUMENT ME!
\r
254 * @param mask DOCUMENT ME!
\r
256 * @return DOCUMENT ME!
\r
258 private static SequenceI[] vectorSubsetToArray(Vector tmp, Vector mask)
\r
260 Vector seqs = new Vector();
\r
262 boolean[] tmask = new boolean[mask.size()];
\r
264 for (i = 0; i < mask.size(); i++)
\r
267 for (i = 0; i < tmp.size(); i++)
\r
269 Object sq = tmp.elementAt(i);
\r
271 if (mask.contains(sq) && tmask[mask.indexOf(sq)])
\r
273 tmask[mask.indexOf(sq)] = false;
\r
274 seqs.addElement(sq);
\r
278 for (i = 0; i < tmask.length; i++)
\r
281 seqs.addElement(mask.elementAt(i));
\r
284 return vectorToArray(seqs);
\r
288 * Sorts by a given AlignmentOrder object
\r
290 * @param align Alignment to order
\r
291 * @param order specified order for alignment
\r
293 public static void sortBy(AlignmentI align, AlignmentOrder order)
\r
295 // Get an ordered vector of sequences which may also be present in align
\r
296 Vector tmp = order.getOrder();
\r
298 if (lastOrder == order)
\r
300 sortOrderAscending = !sortOrderAscending;
\r
304 sortOrderAscending = true;
\r
307 if (sortOrderAscending)
\r
309 setOrder(align, tmp);
\r
313 setReverseOrder(align,
\r
314 vectorSubsetToArray(tmp, align.getSequences()));
\r
321 * @param align alignment to order
\r
322 * @param tree tree which has
\r
324 * @return DOCUMENT ME!
\r
326 private static Vector getOrderByTree(AlignmentI align, NJTree tree)
\r
328 int nSeq = align.getHeight();
\r
330 Vector tmp = new Vector();
\r
332 tmp = _sortByTree(tree.getTopNode(), tmp, align.getSequences());
\r
334 if (tmp.size() != nSeq)
\r
336 // TODO: JBPNote - decide if this is always an error
\r
337 // (eg. not when a tree is associated to another alignment which has more
\r
339 if (tmp.size() < nSeq)
\r
341 addStrays(align, tmp);
\r
344 if (tmp.size() != nSeq)
\r
346 System.err.println("ERROR: tmp.size()=" + tmp.size() +
\r
347 " != nseq=" + nSeq + " in getOrderByTree");
\r
355 * Sorts the alignment by a given tree
\r
357 * @param align alignment to order
\r
358 * @param tree tree which has
\r
360 public static void sortByTree(AlignmentI align, NJTree tree)
\r
362 Vector tmp = getOrderByTree(align, tree);
\r
364 // tmp should properly permute align with tree.
\r
365 if (lastTree != tree)
\r
367 sortTreeAscending = true;
\r
372 sortTreeAscending = !sortTreeAscending;
\r
375 if (sortTreeAscending)
\r
377 setOrder(align, tmp);
\r
381 setReverseOrder(align,
\r
382 vectorSubsetToArray(tmp, align.getSequences()));
\r
389 * @param align DOCUMENT ME!
\r
390 * @param seqs DOCUMENT ME!
\r
392 private static void addStrays(AlignmentI align, Vector seqs)
\r
394 int nSeq = align.getHeight();
\r
396 for (int i = 0; i < nSeq; i++)
\r
398 if (!seqs.contains(align.getSequenceAt(i)))
\r
400 seqs.addElement(align.getSequenceAt(i));
\r
404 if (nSeq != seqs.size())
\r
406 System.err.println(
\r
407 "ERROR: Size still not right even after addStrays");
\r
414 * @param node DOCUMENT ME!
\r
415 * @param tmp DOCUMENT ME!
\r
416 * @param seqset DOCUMENT ME!
\r
418 * @return DOCUMENT ME!
\r
420 private static Vector _sortByTree(SequenceNode node, Vector tmp,
\r
428 SequenceNode left = (SequenceNode) node.left();
\r
429 SequenceNode right = (SequenceNode) node.right();
\r
431 if ((left == null) && (right == null))
\r
433 if (!node.isPlaceholder() && (node.element() != null))
\r
435 if (node.element() instanceof SequenceI)
\r
437 if (!tmp.contains(node.element()))
\r
439 tmp.addElement((SequenceI) node.element());
\r
448 _sortByTree(left, tmp, seqset);
\r
449 _sortByTree(right, tmp, seqset);
\r
455 // Ordering Objects
\r
456 // Alignment.sortBy(OrderObj) - sequence of sequence pointer refs in appropriate order
\r
460 * recover the order of sequences given by the safe numbering scheme introducd
\r
461 * SeqsetUtils.uniquify.
\r
463 public static void recoverOrder(SequenceI[] alignment)
\r
465 float[] ids = new float[alignment.length];
\r
467 for (int i = 0; i < alignment.length; i++)
\r
468 ids[i] = (new Float(alignment[i].getName().substring(8))).floatValue();
\r
470 jalview.util.QuickSort.sort(ids, alignment);
\r