From dc6d9b88b36c704faa61bcd3ed29a9e2f510bc83 Mon Sep 17 00:00:00 2001 From: amwaterhouse Date: Thu, 16 Jun 2005 13:31:23 +0000 Subject: [PATCH] Sort Groups modified --- src/jalview/analysis/AlignmentSorter.java | 102 +++++++++++++---------------- src/jalview/appletgui/AlignFrame.java | 2 +- src/jalview/datamodel/SequenceGroup.java | 26 ++------ src/jalview/gui/AlignFrame.java | 3 +- 4 files changed, 54 insertions(+), 79 deletions(-) diff --git a/src/jalview/analysis/AlignmentSorter.java b/src/jalview/analysis/AlignmentSorter.java index 7c2a554..bdd6bce 100755 --- a/src/jalview/analysis/AlignmentSorter.java +++ b/src/jalview/analysis/AlignmentSorter.java @@ -39,31 +39,6 @@ public class AlignmentSorter { static NJTree lastTree = null; static boolean sortTreeAscending = true; - private AlignmentSorter() { - try { - jbInit(); - } catch (Exception ex) { - ex.printStackTrace(); - } - } - - public static void sortGroups(AlignmentI align) { - Vector groups = align.getGroups(); - int nGroup = groups.size(); - - float[] arr = new float[nGroup]; - Object[] s = new Object[nGroup]; - - for (int i = 0; i < nGroup; i++) { - arr[i] = ((SequenceGroup) groups.elementAt(i)).getSize(); - s[i] = groups.elementAt(i); - } - - QuickSort.sort(arr, s); - - // align..setGroups(newg); - } - /** * Sort by Percentage Identity * @@ -110,11 +85,13 @@ public class AlignmentSorter { } private static void setOrder(AlignmentI align, SequenceI[] seqs) { + // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work Vector algn = align.getSequences(); - - for (int i = 0, p = 0; i < seqs.length; i++) - algn.setElementAt(seqs[i], p++); + for (int i = 0; i < seqs.length; i++) + { + algn.setElementAt(seqs[i], i); + } } public static void sortByID(AlignmentI align) { @@ -140,7 +117,10 @@ public class AlignmentSorter { } public static void sortByGroup(AlignmentI align) { - Vector groups = align.getGroups(); + //MAINTAINS ORIGNAL SEQUENCE ORDER, + //ORDERS BY GROUP SIZE + + Vector groups = new Vector(); if (groups.hashCode() != lastGroupHash) { sortGroupAscending = true; @@ -149,26 +129,41 @@ public class AlignmentSorter { sortGroupAscending = !sortGroupAscending; } - Vector seqs = new Vector(); + //SORTS GROUPS BY SIZE + ////////////////////// + for(int i=0; i sg2.getSize()) + { + groups.insertElementAt(sg, j); + break; + } + } + + if (!groups.contains(sg)) + { + groups.addElement(sg); + } + } - for (int i = 0; i < groups.size(); i++) { + //NOW ADD SEQUENCES MAINTAINING ALIGNMENT ORDER + /////////////////////////////////////////////// + Vector seqs = new Vector(); + for (int i = 0; i < groups.size(); i++) + { SequenceGroup sg = (SequenceGroup) groups.elementAt(i); - - for (int j = 0; j < sg.getSize(); j++) { - seqs.addElement(sg.getSequenceAt(j)); + SequenceI [] orderedseqs = sg.getSequencesInOrder(align); + for (int j = 0; j < orderedseqs.length; j++) + { + seqs.addElement(orderedseqs[j]); } } - // Deletions can happen so this check may fail - - /* - if (seqs.size() != nSeq) { - System.err.println("ERROR: tmp.size() != nseq in sortByGroups"); - if (seqs.size() < nSeq) { - addStrays(align,seqs); - } - } - */ if (sortGroupAscending) { setOrder(align, seqs); } else { @@ -178,6 +173,7 @@ public class AlignmentSorter { } private static SequenceI[] vectorToArray(Vector tmp) { + SequenceI[] seqs = new SequenceI[tmp.size()]; for (int i = 0; i < tmp.size(); i++) { @@ -190,20 +186,19 @@ public class AlignmentSorter { private static SequenceI[] vectorSubsetToArray(Vector tmp, Vector mask) { Vector seqs = new Vector(); int i; - int m; - int p; - boolean[] tmask = new boolean[m = mask.size()]; + boolean[] tmask = new boolean[mask.size()]; - for (i = 0; i < m; i++) + for (i = 0; i < mask.size(); i++) tmask[i] = true; for (i = 0; i < tmp.size(); i++) { - Object sq; - if (mask.contains(sq = tmp.elementAt(i))) { + Object sq = tmp.elementAt(i); + + if (mask.contains(sq) && tmask[mask.indexOf(sq)]) + { tmask[mask.indexOf(sq)] = false; seqs.addElement(sq); - m--; } } @@ -219,8 +214,6 @@ public class AlignmentSorter { // Get an ordered vector of sequences which may also be present in align Vector tmp = order.getOrder(); - // if (tmp.size()