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