3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertFalse;
5 import static org.testng.AssertJUnit.assertNull;
6 import static org.testng.AssertJUnit.assertTrue;
8 import java.util.ArrayList;
9 import java.util.Arrays;
10 import java.util.List;
12 import org.testng.annotations.Test;
14 public class MapListTest
17 @Test(groups ={ "Functional" })
18 public void testSomething()
20 MapList ml = new MapList(new int[]
21 { 1, 5, 10, 15, 25, 20 }, new int[]
23 MapList ml1 = new MapList(new int[]
24 { 1, 3, 17, 4 }, new int[]
26 MapList ml2 = new MapList(new int[]
29 // test internal consistency
30 int to[] = new int[51];
32 MapList mldna = new MapList(new int[]
33 { 2, 2, 6, 8, 12, 16 }, new int[]
35 int[] frm = mldna.locateInFrom(1, 1);
36 testLocateFrom(mldna, 1, 1, new int[]
40 * for (int from=1; from<=51; from++) { int[] too=ml.shiftTo(from); int[]
41 * toofrom=ml.shiftFrom(too[0]);
42 * System.out.println("ShiftFrom("+from+")=="+too[0]+" %
43 * "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]); }
47 private static void testLocateFrom(MapList mldna, int i, int j, int[] ks)
49 int[] frm = mldna.locateInFrom(i, j);
50 assertEquals("Failed test locate from " + i + " to " + j,
51 Arrays.toString(frm), Arrays.toString(ks));
55 * test routine. not incremental.
61 private void testMap(MapList ml, int fromS, int fromE)
63 // todo convert to JUnit style tests
64 for (int from = 1; from <= 25; from++)
66 int[] too = ml.shiftFrom(from);
67 System.out.print("ShiftFrom(" + from + ")==");
70 System.out.print("NaN\n");
74 System.out.print(too[0] + " % " + too[1] + " (" + too[2] + ")");
75 System.out.print("\t+--+\t");
76 int[] toofrom = ml.shiftTo(too[0]);
79 if (toofrom[0] != from)
81 System.err.println("Mapping not reflexive:" + from + " "
82 + too[0] + "->" + toofrom[0]);
84 System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0]
85 + " % " + toofrom[1] + " (" + toofrom[2] + ")");
89 System.out.println("ShiftTo(" + too[0] + ")=="
90 + "NaN! - not Bijective Mapping!");
94 int mmap[][] = ml.makeFromMap();
95 System.out.println("FromMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
96 + mmap[0][2] + " " + mmap[0][3] + " ");
97 for (int i = 1; i <= mmap[1].length; i++)
99 if (mmap[1][i - 1] == -1)
101 System.out.print(i + "=XXX");
106 System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
110 System.out.print("\n");
114 System.out.print(",");
117 // test range function
118 System.out.print("\nTest locateInFrom\n");
120 int f = mmap[0][2], t = mmap[0][3];
123 System.out.println("Range " + f + " to " + t);
124 int rng[] = ml.locateInFrom(f, t);
127 for (int i = 0; i < rng.length; i++)
129 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
134 System.out.println("No range!");
136 System.out.print("\nReversed\n");
137 rng = ml.locateInFrom(t, f);
140 for (int i = 0; i < rng.length; i++)
142 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
147 System.out.println("No range!");
149 System.out.print("\n");
154 System.out.print("\n");
155 mmap = ml.makeToMap();
156 System.out.println("ToMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
157 + mmap[0][2] + " " + mmap[0][3] + " ");
158 for (int i = 1; i <= mmap[1].length; i++)
160 if (mmap[1][i - 1] == -1)
162 System.out.print(i + "=XXX");
167 System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
171 System.out.print("\n");
175 System.out.print(",");
178 System.out.print("\n");
179 // test range function
180 System.out.print("\nTest locateInTo\n");
182 int f = mmap[0][2], t = mmap[0][3];
185 System.out.println("Range " + f + " to " + t);
186 int rng[] = ml.locateInTo(f, t);
189 for (int i = 0; i < rng.length; i++)
191 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
196 System.out.println("No range!");
198 System.out.print("\nReversed\n");
199 rng = ml.locateInTo(t, f);
202 for (int i = 0; i < rng.length; i++)
204 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
209 System.out.println("No range!");
213 System.out.print("\n");
219 * Tests for method that locates ranges in the 'from' map for given range in
222 @Test(groups ={ "Functional" })
223 public void testLocateInFrom_noIntrons()
226 * Simple mapping with no introns
228 int[] codons = new int[]
230 int[] protein = new int[]
232 MapList ml = new MapList(codons, protein, 3, 1);
233 assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(1, 1)));
234 assertEquals("[4, 6]", Arrays.toString(ml.locateInFrom(2, 2)));
235 assertEquals("[7, 9]", Arrays.toString(ml.locateInFrom(3, 3)));
236 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
237 assertEquals("[1, 6]", Arrays.toString(ml.locateInFrom(1, 2)));
238 assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(1, 3)));
239 assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 4)));
240 assertEquals("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3)));
241 assertEquals("[4, 12]", Arrays.toString(ml.locateInFrom(2, 4)));
242 assertEquals("[7, 12]", Arrays.toString(ml.locateInFrom(3, 4)));
243 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
245 assertNull(ml.locateInFrom(0, 0));
246 assertNull(ml.locateInFrom(1, 5));
247 assertNull(ml.locateInFrom(-1, 1));
251 * Tests for method that locates ranges in the 'from' map for given range in
254 @Test(groups ={ "Functional" })
255 public void testLocateInFrom_withIntrons()
258 * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
259 * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
262 { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
265 MapList ml = new MapList(codons, protein, 3, 1);
266 assertEquals("[2, 3, 5, 5]", Arrays.toString(ml.locateInFrom(1, 1)));
267 assertEquals("[6, 7, 9, 9]", Arrays.toString(ml.locateInFrom(2, 2)));
268 assertEquals("[10, 10, 12, 12, 14, 14]",
269 Arrays.toString(ml.locateInFrom(3, 3)));
270 assertEquals("[16, 18]", Arrays.toString(ml.locateInFrom(4, 4)));
274 * Tests for method that locates ranges in the 'to' map for given range in the
277 @Test(groups ={ "Functional" })
278 public void testLocateInTo_noIntrons()
281 * Simple mapping with no introns
283 int[] codons = new int[]
285 int[] protein = new int[]
287 MapList ml = new MapList(codons, protein, 3, 1);
288 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 3)));
289 assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
290 assertEquals("[3, 3]", Arrays.toString(ml.locateInTo(7, 9)));
291 assertEquals("[4, 4]", Arrays.toString(ml.locateInTo(10, 12)));
292 assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 6)));
293 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(1, 9)));
294 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(1, 12)));
295 assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
296 assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(4, 12)));
299 * A part codon is treated as if a whole one.
301 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
302 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 2)));
303 assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 4)));
304 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(2, 8)));
305 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
306 assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(5, 11)));
308 assertNull(ml.locateInTo(0, 0));
309 assertNull(ml.locateInTo(1, 13));
310 assertNull(ml.locateInTo(-1, 1));
314 * Tests for method that locates ranges in the 'to' map for given range in the
317 @Test(groups ={ "Functional" })
318 public void testLocateInTo_withIntrons()
321 * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
322 * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
325 { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
327 * Mapped proteins at positions 1, 3, 4, 6 in the sequence
330 { 1, 1, 3, 4, 6, 6 };
331 MapList ml = new MapList(codons, protein, 3, 1);
334 * Can't map from an unmapped position
336 assertNull(ml.locateInTo(1, 2));
337 assertNull(ml.locateInTo(2, 4));
338 assertNull(ml.locateInTo(4, 4));
341 * Valid range or subrange of codon1 maps to protein1.
343 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 2)));
344 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 3)));
345 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 5)));
346 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 3)));
347 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 5)));
349 // codon position 6 starts the next protein:
350 assertEquals("[1, 1, 3, 3]", Arrays.toString(ml.locateInTo(3, 6)));
352 // codon positions 7 to 17 (part) cover proteins 2/3/4 at positions 3/4/6
353 assertEquals("[3, 4, 6, 6]", Arrays.toString(ml.locateInTo(7, 17)));
358 * Test equals method.
360 @Test(groups ={ "Functional" })
361 public void testEquals()
363 int[] codons = new int[]
364 { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
365 int[] protein = new int[]
367 MapList ml = new MapList(codons, protein, 3, 1);
368 MapList ml1 = new MapList(codons, protein, 3, 1); // same values
369 MapList ml2 = new MapList(codons, protein, 2, 1); // fromRatio differs
370 MapList ml3 = new MapList(codons, protein, 3, 2); // toRatio differs
372 MapList ml6 = new MapList(codons, protein, 3, 1); // fromShifts differ
374 MapList ml7 = new MapList(codons, protein, 3, 1); // toShifts differ
376 assertTrue(ml.equals(ml));
377 assertTrue(ml.equals(ml1));
378 assertTrue(ml1.equals(ml));
380 assertFalse(ml.equals(null));
381 assertFalse(ml.equals("hello"));
382 assertFalse(ml.equals(ml2));
383 assertFalse(ml.equals(ml3));
384 assertFalse(ml.equals(ml6));
385 assertFalse(ml.equals(ml7));
386 assertFalse(ml6.equals(ml7));
390 MapList ml4 = new MapList(codons, null, 3, 1); // toShifts null
391 assertFalse(ml.equals(ml4));
392 } catch (NullPointerException e)
394 // actually thrown by constructor before equals can be called
398 MapList ml5 = new MapList(null, protein, 3, 1); // fromShifts null
399 assertFalse(ml.equals(ml5));
400 } catch (NullPointerException e)
402 // actually thrown by constructor before equals can be called
407 * Test for the method that flattens a list of ranges into a single array.
409 @Test(groups ={ "Functional" })
410 public void testGetRanges()
412 List<int[]> ranges = new ArrayList<int[]>();
417 assertEquals("[2, 3, 5, 6]", Arrays.toString(MapList.getRanges(ranges)));
421 * Check state after construction
423 @Test(groups ={ "Functional" })
424 public void testConstructor()
427 { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
429 { 1, 1, 3, 4, 6, 6 };
430 MapList ml = new MapList(codons, protein, 3, 1);
431 assertEquals(3, ml.getFromRatio());
432 assertEquals(2, ml.getFromLowest());
433 assertEquals(18, ml.getFromHighest());
434 assertEquals(1, ml.getToLowest());
435 assertEquals(6, ml.getToHighest());
436 assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
437 prettyPrint(ml.getFromRanges()));
438 assertEquals("{[1, 1], [3, 4], [6, 6]}", prettyPrint(ml.getToRanges()));
441 * Also copy constructor
443 MapList ml2 = new MapList(ml);
444 assertEquals(3, ml2.getFromRatio());
445 assertEquals(2, ml2.getFromLowest());
446 assertEquals(18, ml2.getFromHighest());
447 assertEquals(1, ml2.getToLowest());
448 assertEquals(6, ml2.getToHighest());
449 assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
450 prettyPrint(ml2.getFromRanges()));
451 assertEquals("{[1, 1], [3, 4], [6, 6]}", prettyPrint(ml2.getToRanges()));
455 * Convert a List of {[i, j], [k, l], ...} to "[[i, j], [k, l], ...]"
460 private String prettyPrint(List<int[]> ranges)
462 StringBuilder sb = new StringBuilder(ranges.size() * 5);
463 boolean first = true;
465 for (int[] range : ranges)
471 sb.append(Arrays.toString(range));
475 return sb.toString();
479 * Test the method that creates an inverse mapping
481 @Test(groups ={ "Functional" })
482 public void testGetInverse()
485 { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
487 { 1, 1, 3, 4, 6, 6 };
489 MapList ml = new MapList(codons, protein, 3, 1);
490 MapList ml2 = ml.getInverse();
491 assertEquals(ml.getFromRatio(), ml2.getToRatio());
492 assertEquals(ml.getFromRatio(), ml2.getToRatio());
493 assertEquals(ml.getToHighest(), ml2.getFromHighest());
494 assertEquals(ml.getFromHighest(), ml2.getToHighest());
495 assertEquals(prettyPrint(ml.getFromRanges()),
496 prettyPrint(ml2.getToRanges()));
497 assertEquals(prettyPrint(ml.getToRanges()),
498 prettyPrint(ml2.getFromRanges()));
501 @Test(groups ={ "Functional" })
502 public void testToString()
504 MapList ml = new MapList(new int[]
505 { 1, 5, 10, 15, 25, 20 }, new int[]
507 String s = ml.toString();
508 assertEquals("From (1:3) [ [1, 5] [10, 15] [25, 20] ] To [ [51, 1] ]",