1 package jalview.analysis;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5 import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder;
6 import jalview.datamodel.Alignment;
7 import jalview.datamodel.AlignmentAnnotation;
8 import jalview.datamodel.Sequence;
9 import jalview.datamodel.SequenceI;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Random;
15 import org.junit.Before;
16 import org.junit.Test;
18 public class AnnotationSorterTest
20 private static final int NUM_SEQS = 6;
22 private static final int NUM_ANNS = 7;
24 private static final String SS = "secondary structure";
26 AlignmentAnnotation[] anns = new AlignmentAnnotation[0];
31 * Set up 6 sequences and 7 annotations.
36 al = buildAlignment(NUM_SEQS);
37 anns = buildAnnotations(NUM_ANNS);
41 * Construct an array of numAnns annotations
47 protected AlignmentAnnotation[] buildAnnotations(int numAnns)
49 List<AlignmentAnnotation> annlist = new ArrayList<AlignmentAnnotation>();
50 for (int i = 0; i < numAnns; i++)
52 AlignmentAnnotation ann = new AlignmentAnnotation(SS + i, "", 0);
55 return annlist.toArray(anns);
59 * Make an alignment with numSeqs sequences in it.
65 private Alignment buildAlignment(int numSeqs)
67 SequenceI[] seqs = new Sequence[numSeqs];
68 for (int i = 0; i < numSeqs; i++)
70 seqs[i] = new Sequence("Sequence" + i, "axrdkfp");
72 return new Alignment(seqs);
76 * Test sorting by annotation type (label) within sequence order, including
78 * <li>annotations with no sequence reference - sort to end keeping mutual
80 * <li>annotations with sequence ref = sort in sequence order</li>
81 * <li>multiple annotations for same sequence ref - sort by label
82 * non-case-specific</li>
83 * <li>annotations with reference to sequence not in alignment - treat like no
88 public void testSortBySequenceAndType_autocalcLast()
91 anns[0].sequenceRef = al.getSequenceAt(1); anns[0].label = "label0";
92 anns[1].sequenceRef = al.getSequenceAt(3); anns[1].label = "structure";
93 anns[2].sequenceRef = al.getSequenceAt(3); anns[2].label = "iron";
94 anns[3].sequenceRef = null; anns[3].label = "Quality";
95 anns[4].sequenceRef = null; anns[4].label = "Consensus";
96 anns[5].sequenceRef = al.getSequenceAt(0); anns[5].label = "label5";
97 anns[6].sequenceRef = al.getSequenceAt(3); anns[6].label = "IRP";
100 AnnotationSorter testee = new AnnotationSorter(al, false);
101 testee.sort(anns, SequenceAnnotationOrder.SEQUENCE_AND_LABEL);
102 assertEquals("label5", anns[0].label); // for sequence 0
103 assertEquals("label0", anns[1].label); // for sequence 1
104 assertEquals("iron", anns[2].label); // sequence 3 /iron
105 assertEquals("IRP", anns[3].label); // sequence 3/IRP
106 assertEquals("structure", anns[4].label); // sequence 3/structure
107 assertEquals("Quality", anns[5].label); // non-sequence annotations
108 assertEquals("Consensus", anns[6].label); // retain ordering
112 * Variant with autocalculated annotations sorting to front
115 public void testSortBySequenceAndType_autocalcFirst()
118 anns[0].sequenceRef = al.getSequenceAt(1); anns[0].label = "label0";
119 anns[1].sequenceRef = al.getSequenceAt(3); anns[1].label = "structure";
120 anns[2].sequenceRef = al.getSequenceAt(3); anns[2].label = "iron";
121 anns[3].sequenceRef = null; anns[3].label = "Quality";
122 anns[4].sequenceRef = null; anns[4].label = "Consensus";
123 anns[5].sequenceRef = al.getSequenceAt(0); anns[5].label = "label5";
124 anns[6].sequenceRef = al.getSequenceAt(3); anns[6].label = "IRP";
127 AnnotationSorter testee = new AnnotationSorter(al, true);
128 testee.sort(anns, SequenceAnnotationOrder.SEQUENCE_AND_LABEL);
129 assertEquals("Quality", anns[0].label); // non-sequence annotations
130 assertEquals("Consensus", anns[1].label); // retain ordering
131 assertEquals("label5", anns[2].label); // for sequence 0
132 assertEquals("label0", anns[3].label); // for sequence 1
133 assertEquals("iron", anns[4].label); // sequence 3 /iron
134 assertEquals("IRP", anns[5].label); // sequence 3/IRP
135 assertEquals("structure", anns[6].label); // sequence 3/structure
139 * Test sorting by annotation type (label) within sequence order, including
141 * <li>annotations with no sequence reference - sort to end keeping mutual
143 * <li>annotations with sequence ref = sort in sequence order</li>
144 * <li>multiple annotations for same sequence ref - sort by label
145 * non-case-specific</li>
146 * <li>annotations with reference to sequence not in alignment - treat like no
151 public void testSortByTypeAndSequence_autocalcLast()
154 anns[0].sequenceRef = al.getSequenceAt(1); anns[0].label = "label0";
155 anns[1].sequenceRef = al.getSequenceAt(3); anns[1].label = "structure";
156 anns[2].sequenceRef = al.getSequenceAt(3); anns[2].label = "iron";
157 anns[3].sequenceRef = null; anns[3].label = "Quality";
158 anns[4].sequenceRef = null; anns[4].label = "Consensus";
159 anns[5].sequenceRef = al.getSequenceAt(0); anns[5].label = "IRON";
160 anns[6].sequenceRef = al.getSequenceAt(2); anns[6].label = "Structure";
163 AnnotationSorter testee = new AnnotationSorter(al, false);
164 testee.sort(anns, SequenceAnnotationOrder.LABEL_AND_SEQUENCE);
165 assertEquals("IRON", anns[0].label); // IRON / sequence 0
166 assertEquals("iron", anns[1].label); // iron / sequence 3
167 assertEquals("label0", anns[2].label); // label0 / sequence 1
168 assertEquals("Structure", anns[3].label); // Structure / sequence 2
169 assertEquals("structure", anns[4].label); // structure / sequence 3
170 assertEquals("Quality", anns[5].label); // non-sequence annotations
171 assertEquals("Consensus", anns[6].label); // retain ordering
175 * Variant of test with autocalculated annotations sorted to front
178 public void testSortByTypeAndSequence_autocalcFirst()
181 anns[0].sequenceRef = al.getSequenceAt(1); anns[0].label = "label0";
182 anns[1].sequenceRef = al.getSequenceAt(3); anns[1].label = "structure";
183 anns[2].sequenceRef = al.getSequenceAt(3); anns[2].label = "iron";
184 anns[3].sequenceRef = null; anns[3].label = "Quality";
185 anns[4].sequenceRef = null; anns[4].label = "Consensus";
186 anns[5].sequenceRef = al.getSequenceAt(0); anns[5].label = "IRON";
187 anns[6].sequenceRef = al.getSequenceAt(2); anns[6].label = "Structure";
190 AnnotationSorter testee = new AnnotationSorter(al, true);
191 testee.sort(anns, SequenceAnnotationOrder.LABEL_AND_SEQUENCE);
192 assertEquals("Quality", anns[0].label); // non-sequence annotations
193 assertEquals("Consensus", anns[1].label); // retain ordering
194 assertEquals("IRON", anns[2].label); // IRON / sequence 0
195 assertEquals("iron", anns[3].label); // iron / sequence 3
196 assertEquals("label0", anns[4].label); // label0 / sequence 1
197 assertEquals("Structure", anns[5].label); // Structure / sequence 2
198 assertEquals("structure", anns[6].label); // structure / sequence 3
202 * Variant of test with autocalculated annotations sorted to front but
203 * otherwise no change.
206 public void testNoSort_autocalcFirst()
209 anns[0].sequenceRef = al.getSequenceAt(1); anns[0].label = "label0";
210 anns[1].sequenceRef = al.getSequenceAt(3); anns[1].label = "structure";
211 anns[2].sequenceRef = al.getSequenceAt(3); anns[2].label = "iron";
212 anns[3].sequenceRef = null; anns[3].label = "Quality";
213 anns[4].sequenceRef = null; anns[4].label = "Consensus";
214 anns[5].sequenceRef = al.getSequenceAt(0); anns[5].label = "IRON";
215 anns[6].sequenceRef = al.getSequenceAt(2); anns[6].label = "Structure";
218 AnnotationSorter testee = new AnnotationSorter(al, true);
219 testee.sort(anns, SequenceAnnotationOrder.NONE);
220 assertEquals("Quality", anns[0].label); // non-sequence annotations
221 assertEquals("Consensus", anns[1].label); // retain ordering
222 assertEquals("label0", anns[2].label);
223 assertEquals("structure", anns[3].label);
224 assertEquals("iron", anns[4].label);
225 assertEquals("IRON", anns[5].label);
226 assertEquals("Structure", anns[6].label);
230 public void testSort_timingPresorted()
232 final long targetTime = 100; // ms
233 final int numSeqs = 10000;
234 final int numAnns = 20000;
235 al = buildAlignment(numSeqs);
236 anns = buildAnnotations(numAnns);
239 * Set the annotations presorted by label
241 Random r = new Random();
242 final SequenceI[] sequences = al.getSequencesArray();
243 for (int i = 0; i < anns.length; i++)
245 SequenceI randomSequenceRef = sequences[r.nextInt(sequences.length)];
246 anns[i].sequenceRef = randomSequenceRef;
247 anns[i].label = "label" + i;
249 long startTime = System.currentTimeMillis();
250 AnnotationSorter testee = new AnnotationSorter(al, false);
251 testee.sort(anns, SequenceAnnotationOrder.LABEL_AND_SEQUENCE);
252 long endTime = System.currentTimeMillis();
253 final long elapsed = endTime - startTime;
254 System.out.println("Timing test for presorted " + numSeqs
256 + numAnns + " annotations took " + elapsed + "ms");
257 assertTrue("Sort took more than " + targetTime + "ms",
258 elapsed <= targetTime);
262 * Timing test for sorting randomly sorted annotations
265 public void testSort_timingUnsorted()
267 final int numSeqs = 2000;
268 final int numAnns = 4000;
269 al = buildAlignment(numSeqs);
270 anns = buildAnnotations(numAnns);
273 * Set the annotations in random order with respect to the sequences
275 Random r = new Random();
276 final SequenceI[] sequences = al.getSequencesArray();
277 for (int i = 0; i < anns.length; i++)
279 SequenceI randomSequenceRef = sequences[r.nextInt(sequences.length)];
280 anns[i].sequenceRef = randomSequenceRef;
281 anns[i].label = "label" + i;
283 long startTime = System.currentTimeMillis();
284 AnnotationSorter testee = new AnnotationSorter(al, false);
285 testee.sort(anns, SequenceAnnotationOrder.SEQUENCE_AND_LABEL);
286 long endTime = System.currentTimeMillis();
287 final long elapsed = endTime - startTime;
288 System.out.println("Timing test for unsorted " + numSeqs
290 + numAnns + " annotations took " + elapsed + "ms");
294 * Timing test for sorting annotations with a limited range of types (labels).
297 public void testSort_timingSemisorted()
299 final int numSeqs = 2000;
300 final int numAnns = 4000;
301 al = buildAlignment(numSeqs);
302 anns = buildAnnotations(numAnns);
304 String[] labels = new String[]
305 { "label1", "label2", "label3", "label4", "label5", "label6" };
308 * Set the annotations in sequence order with randomly assigned labels.
310 Random r = new Random();
311 final SequenceI[] sequences = al.getSequencesArray();
312 for (int i = 0; i < anns.length; i++)
314 SequenceI sequenceRef = sequences[i % sequences.length];
315 anns[i].sequenceRef = sequenceRef;
316 anns[i].label = labels[r.nextInt(labels.length)];
318 long startTime = System.currentTimeMillis();
319 AnnotationSorter testee = new AnnotationSorter(al, false);
320 testee.sort(anns, SequenceAnnotationOrder.LABEL_AND_SEQUENCE);
321 long endTime = System.currentTimeMillis();
322 long elapsed = endTime - startTime;
323 System.out.println("Sort by type for semisorted " + numSeqs
325 + numAnns + " annotations took " + elapsed + "ms");
327 // now resort by sequence
328 startTime = System.currentTimeMillis();
329 testee.sort(anns, SequenceAnnotationOrder.SEQUENCE_AND_LABEL);
330 endTime = System.currentTimeMillis();
331 elapsed = endTime - startTime;
332 System.out.println("Resort by sequence for semisorted " + numSeqs
333 + " sequences and " + numAnns + " annotations took " + elapsed
336 // now resort by type
337 startTime = System.currentTimeMillis();
338 testee.sort(anns, SequenceAnnotationOrder.LABEL_AND_SEQUENCE);
339 endTime = System.currentTimeMillis();
340 elapsed = endTime - startTime;
341 System.out.println("Resort by type for semisorted " + numSeqs
342 + " sequences and " + numAnns + " annotations took " + elapsed