1 package jalview.analysis;
\r
3 import jalview.datamodel.*;
\r
4 import jalview.util.*;
\r
9 /** Data structure to hold and manipulate a multiple sequence alignment
\r
11 public class AlignmentSorter {
\r
13 private AlignmentSorter() {
\r
16 public static void sortGroups(AlignmentI align) {
\r
17 Vector groups = align.getGroups();
\r
18 int nGroup = groups.size();
\r
20 float[] arr = new float [nGroup];
\r
21 Object[] s = new Object[nGroup];
\r
23 for (int i=0; i < nGroup; i++) {
\r
24 arr[i] = ((SequenceGroup)groups.elementAt(i)).getSize();
\r
25 s[i] = groups.elementAt(i);
\r
28 QuickSort.sort(arr,s);
\r
30 Vector newg = new Vector(nGroup);
\r
32 for (int i=nGroup-1; i >= 0; i--) {
\r
33 newg.addElement(s[i]);
\r
36 // align.setGroups(newg);
\r
40 public static void sortByPID(AlignmentI align, SequenceI s) {
\r
41 int nSeq = align.getHeight();
\r
43 float scores[] = new float[nSeq];
\r
44 SequenceI seqs[] = new SequenceI[nSeq];
\r
46 for (int i = 0; i < nSeq; i++) {
\r
47 scores[i] = Comparison.compare(align.getSequenceAt(i),s);
\r
48 seqs[i] = align.getSequenceAt(i);
\r
51 QuickSort.sort(scores,0,scores.length-1,seqs);
\r
53 setReverseOrder(align,seqs);
\r
56 private static void setReverseOrder(AlignmentI align, SequenceI [] seqs) {
\r
57 int nSeq = seqs.length;
\r
66 // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work
\r
67 for (int i = 0; i < len; i++) {
\r
68 //SequenceI tmp = seqs[i];
\r
69 align.getSequences().setElementAt(seqs[nSeq-i-1],i);
\r
70 align.getSequences().setElementAt(seqs[i],nSeq-i-1);
\r
74 private static void setOrder(AlignmentI align, Vector tmp) {
\r
75 setOrder(align,vectorToArray(tmp));
\r
78 private static void setOrder(AlignmentI align, SequenceI [] seqs) {
\r
79 // NOTE: DO NOT USE align.setSequenceAt() here - it will NOT work
\r
80 for (int i = 0; i < seqs.length; i++) {
\r
81 align.getSequences().setElementAt(seqs[i],i);
\r
86 static boolean sortIdAscending = true;
\r
87 public static void sortByID(AlignmentI align) {
\r
88 int nSeq = align.getHeight();
\r
90 String ids[] = new String[nSeq];
\r
91 SequenceI seqs[] = new SequenceI[nSeq];
\r
93 for (int i = 0; i < nSeq; i++) {
\r
94 ids[i] = align.getSequenceAt(i).getName();
\r
95 seqs[i] = align.getSequenceAt(i);
\r
98 QuickSort.sort(ids,seqs);
\r
100 if(sortIdAscending)
\r
101 setReverseOrder(align,seqs);
\r
103 setOrder(align, seqs);
\r
105 sortIdAscending = !sortIdAscending;
\r
108 static boolean sortGroupAscending = true;
\r
109 public static void sortByGroup(AlignmentI align) {
\r
110 int nSeq = align.getHeight();
\r
111 Vector groups = align.getGroups();
\r
113 Vector seqs = new Vector();
\r
115 for (int i=0; i < groups.size(); i++) {
\r
116 SequenceGroup sg = (SequenceGroup)groups.elementAt(i);
\r
118 for (int j = 0; j < sg.getSize(); j++) {
\r
119 seqs.addElement(sg.getSequenceAt(j));
\r
123 if (seqs.size() != nSeq) {
\r
124 System.err.println("ERROR: tmp.size() != nseq in sortByGroups");
\r
125 if (seqs.size() < nSeq) {
\r
126 addStrays(align,seqs);
\r
130 if(sortGroupAscending)
\r
131 setOrder(align,seqs);
\r
133 setReverseOrder( align, vectorToArray(seqs));
\r
135 sortGroupAscending = ! sortGroupAscending;
\r
138 private static SequenceI [] vectorToArray(Vector tmp) {
\r
139 SequenceI[] seqs = new SequenceI[tmp.size()];
\r
141 for (int i=0; i < tmp.size(); i++) {
\r
142 seqs[i] = (SequenceI)tmp.elementAt(i);
\r
147 static boolean sortTreeAscending = true;
\r
148 public static void sortByTree(AlignmentI align, NJTree tree) {
\r
149 int nSeq = align.getHeight();
\r
151 Vector tmp = new Vector();
\r
153 tmp = _sortByTree(tree.getTopNode(),tmp);
\r
155 if (tmp.size() != nSeq) {
\r
156 System.err.println("ERROR: tmp.size() != nseq in sortByTree");
\r
157 if (tmp.size() < nSeq) {
\r
158 addStrays(align,tmp);
\r
162 if(sortTreeAscending)
\r
163 setOrder(align,tmp);
\r
165 setReverseOrder(align, vectorToArray(tmp));
\r
167 sortTreeAscending = !sortTreeAscending;
\r
170 private static void addStrays(AlignmentI align, Vector seqs) {
\r
171 int nSeq = align.getHeight();
\r
172 for (int i=0;i<nSeq;i++) {
\r
173 if (!seqs.contains(align.getSequenceAt(i))) {
\r
174 seqs.addElement(align.getSequenceAt(i));
\r
177 if (nSeq != seqs.size()) {
\r
178 System.err.println("ERROR: Size still not right even after addStrays");
\r
182 public static Vector _sortByTree(SequenceNode node, Vector tmp) {
\r
183 if (node == null) {return tmp;}
\r
185 SequenceNode left = (SequenceNode)node.left();
\r
186 SequenceNode right = (SequenceNode)node.right();
\r
188 if (left == null && right == null) {
\r
189 if (node.element() instanceof SequenceI) {
\r
190 tmp.addElement((SequenceI)node.element());
\r
194 _sortByTree(left,tmp);
\r
195 _sortByTree(right,tmp);
\r