3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertNull;
6 import static org.junit.Assert.assertTrue;
8 import java.util.ArrayList;
9 import java.util.Arrays;
10 import java.util.List;
12 import org.junit.Assert;
13 import org.junit.Test;
15 public class MapListTest
19 public void testSomething()
21 MapList ml = new MapList(new int[]
22 { 1, 5, 10, 15, 25, 20 }, new int[]
24 MapList ml1 = new MapList(new int[]
25 { 1, 3, 17, 4 }, new int[]
27 MapList ml2 = new MapList(new int[]
30 // test internal consistency
31 int to[] = new int[51];
33 MapList mldna = new MapList(new int[]
34 { 2, 2, 6, 8, 12, 16 }, new int[]
36 int[] frm = mldna.locateInFrom(1, 1);
37 testLocateFrom(mldna, 1, 1, new int[]
41 * for (int from=1; from<=51; from++) { int[] too=ml.shiftTo(from); int[]
42 * toofrom=ml.shiftFrom(too[0]);
43 * System.out.println("ShiftFrom("+from+")=="+too[0]+" %
44 * "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]); }
48 private static void testLocateFrom(MapList mldna, int i, int j, int[] ks)
50 int[] frm = mldna.locateInFrom(i, j);
51 Assert.assertEquals("Failed test locate from " + i + " to " + j,
52 Arrays.toString(frm), Arrays.toString(ks));
56 * test routine. not incremental.
62 private void testMap(MapList ml, int fromS, int fromE)
64 // todo convert to JUnit style tests
65 for (int from = 1; from <= 25; from++)
67 int[] too = ml.shiftFrom(from);
68 System.out.print("ShiftFrom(" + from + ")==");
71 System.out.print("NaN\n");
75 System.out.print(too[0] + " % " + too[1] + " (" + too[2] + ")");
76 System.out.print("\t+--+\t");
77 int[] toofrom = ml.shiftTo(too[0]);
80 if (toofrom[0] != from)
82 System.err.println("Mapping not reflexive:" + from + " "
83 + too[0] + "->" + toofrom[0]);
85 System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0]
86 + " % " + toofrom[1] + " (" + toofrom[2] + ")");
90 System.out.println("ShiftTo(" + too[0] + ")=="
91 + "NaN! - not Bijective Mapping!");
95 int mmap[][] = ml.makeFromMap();
96 System.out.println("FromMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
97 + mmap[0][2] + " " + mmap[0][3] + " ");
98 for (int i = 1; i <= mmap[1].length; i++)
100 if (mmap[1][i - 1] == -1)
102 System.out.print(i + "=XXX");
107 System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
111 System.out.print("\n");
115 System.out.print(",");
118 // test range function
119 System.out.print("\nTest locateInFrom\n");
121 int f = mmap[0][2], t = mmap[0][3];
124 System.out.println("Range " + f + " to " + t);
125 int rng[] = ml.locateInFrom(f, t);
128 for (int i = 0; i < rng.length; i++)
130 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
135 System.out.println("No range!");
137 System.out.print("\nReversed\n");
138 rng = ml.locateInFrom(t, f);
141 for (int i = 0; i < rng.length; i++)
143 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
148 System.out.println("No range!");
150 System.out.print("\n");
155 System.out.print("\n");
156 mmap = ml.makeToMap();
157 System.out.println("ToMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
158 + mmap[0][2] + " " + mmap[0][3] + " ");
159 for (int i = 1; i <= mmap[1].length; i++)
161 if (mmap[1][i - 1] == -1)
163 System.out.print(i + "=XXX");
168 System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
172 System.out.print("\n");
176 System.out.print(",");
179 System.out.print("\n");
180 // test range function
181 System.out.print("\nTest locateInTo\n");
183 int f = mmap[0][2], t = mmap[0][3];
186 System.out.println("Range " + f + " to " + t);
187 int rng[] = ml.locateInTo(f, t);
190 for (int i = 0; i < rng.length; i++)
192 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
197 System.out.println("No range!");
199 System.out.print("\nReversed\n");
200 rng = ml.locateInTo(t, f);
203 for (int i = 0; i < rng.length; i++)
205 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
210 System.out.println("No range!");
214 System.out.print("\n");
220 * Tests for method that locates ranges in the 'from' map for given range in
224 public void testLocateInFrom_noIntrons()
227 * Simple mapping with no introns
229 int[] codons = new int[]
231 int[] protein = new int[]
233 MapList ml = new MapList(codons, protein, 3, 1);
234 assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(1, 1)));
235 assertEquals("[4, 6]", Arrays.toString(ml.locateInFrom(2, 2)));
236 assertEquals("[7, 9]", Arrays.toString(ml.locateInFrom(3, 3)));
237 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
238 assertEquals("[1, 6]", Arrays.toString(ml.locateInFrom(1, 2)));
239 assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(1, 3)));
240 assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 4)));
241 assertEquals("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3)));
242 assertEquals("[4, 12]", Arrays.toString(ml.locateInFrom(2, 4)));
243 assertEquals("[7, 12]", Arrays.toString(ml.locateInFrom(3, 4)));
244 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
246 assertNull(ml.locateInFrom(0, 0));
247 assertNull(ml.locateInFrom(1, 5));
248 assertNull(ml.locateInFrom(-1, 1));
252 * Tests for method that locates ranges in the 'from' map for given range in
256 public void testLocateInFrom_withIntrons()
259 * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
260 * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
263 { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
266 MapList ml = new MapList(codons, protein, 3, 1);
267 assertEquals("[2, 3, 5, 5]", Arrays.toString(ml.locateInFrom(1, 1)));
268 assertEquals("[6, 7, 9, 9]", Arrays.toString(ml.locateInFrom(2, 2)));
269 assertEquals("[10, 10, 12, 12, 14, 14]",
270 Arrays.toString(ml.locateInFrom(3, 3)));
271 assertEquals("[16, 18]", Arrays.toString(ml.locateInFrom(4, 4)));
275 * Tests for method that locates ranges in the 'to' map for given range in the
279 public void testLocateInTo_noIntrons()
282 * Simple mapping with no introns
284 int[] codons = new int[]
286 int[] protein = new int[]
288 MapList ml = new MapList(codons, protein, 3, 1);
289 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 3)));
290 assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
291 assertEquals("[3, 3]", Arrays.toString(ml.locateInTo(7, 9)));
292 assertEquals("[4, 4]", Arrays.toString(ml.locateInTo(10, 12)));
293 assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 6)));
294 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(1, 9)));
295 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(1, 12)));
296 assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
297 assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(4, 12)));
300 * A part codon is treated as if a whole one.
302 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
303 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 2)));
304 assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 4)));
305 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(2, 8)));
306 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
307 assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(5, 11)));
309 assertNull(ml.locateInTo(0, 0));
310 assertNull(ml.locateInTo(1, 13));
311 assertNull(ml.locateInTo(-1, 1));
315 * Tests for method that locates ranges in the 'to' map for given range in the
319 public void testLocateInTo_withIntrons()
322 * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
323 * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
326 { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
328 * Mapped proteins at positions 1, 3, 4, 6 in the sequence
331 { 1, 1, 3, 4, 6, 6 };
332 MapList ml = new MapList(codons, protein, 3, 1);
335 * Can't map from an unmapped position
337 assertNull(ml.locateInTo(1, 2));
338 assertNull(ml.locateInTo(2, 4));
339 assertNull(ml.locateInTo(4, 4));
342 * Valid range or subrange of codon1 maps to protein1.
344 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 2)));
345 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 3)));
346 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 5)));
347 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 3)));
348 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 5)));
350 // codon position 6 starts the next protein:
351 assertEquals("[1, 1, 3, 3]", Arrays.toString(ml.locateInTo(3, 6)));
353 // codon positions 7 to 17 (part) cover proteins 2/3/4 at positions 3/4/6
354 assertEquals("[3, 4, 6, 6]", Arrays.toString(ml.locateInTo(7, 17)));
359 * Test equals method.
362 public void testEquals()
364 int[] codons = new int[]
365 { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
366 int[] protein = new int[]
368 MapList ml = new MapList(codons, protein, 3, 1);
369 MapList ml1 = new MapList(codons, protein, 3, 1); // same values
370 MapList ml2 = new MapList(codons, protein, 2, 1); // fromRatio differs
371 MapList ml3 = new MapList(codons, protein, 3, 2); // toRatio differs
373 MapList ml6 = new MapList(codons, protein, 3, 1); // fromShifts differ
375 MapList ml7 = new MapList(codons, protein, 3, 1); // toShifts differ
377 assertTrue(ml.equals(ml));
378 assertTrue(ml.equals(ml1));
379 assertTrue(ml1.equals(ml));
381 assertFalse(ml.equals(null));
382 assertFalse(ml.equals("hello"));
383 assertFalse(ml.equals(ml2));
384 assertFalse(ml.equals(ml3));
385 assertFalse(ml.equals(ml6));
386 assertFalse(ml.equals(ml7));
387 assertFalse(ml6.equals(ml7));
391 MapList ml4 = new MapList(codons, null, 3, 1); // toShifts null
392 assertFalse(ml.equals(ml4));
393 } catch (NullPointerException e)
395 // actually thrown by constructor before equals can be called
399 MapList ml5 = new MapList(null, protein, 3, 1); // fromShifts null
400 assertFalse(ml.equals(ml5));
401 } catch (NullPointerException e)
403 // actually thrown by constructor before equals can be called
408 * Test for the method that flattens a list of ranges into a single array.
411 public void testGetRanges()
413 List<int[]> ranges = new ArrayList<int[]>();
418 assertEquals("[2, 3, 5, 6]", Arrays.toString(MapList.getRanges(ranges)));
422 * Check state after construction
425 public void testConstructor()
428 { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
430 { 1, 1, 3, 4, 6, 6 };
431 MapList ml = new MapList(codons, protein, 3, 1);
432 assertEquals(3, ml.getFromRatio());
433 assertEquals(2, ml.getFromLowest());
434 assertEquals(18, ml.getFromHighest());
435 assertEquals(1, ml.getToLowest());
436 assertEquals(6, ml.getToHighest());
437 assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
438 prettyPrint(ml.getFromRanges()));
439 assertEquals("{[1, 1], [3, 4], [6, 6]}", prettyPrint(ml.getToRanges()));
442 * Also copy constructor
444 MapList ml2 = new MapList(ml);
445 assertEquals(3, ml2.getFromRatio());
446 assertEquals(2, ml2.getFromLowest());
447 assertEquals(18, ml2.getFromHighest());
448 assertEquals(1, ml2.getToLowest());
449 assertEquals(6, ml2.getToHighest());
450 assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
451 prettyPrint(ml2.getFromRanges()));
452 assertEquals("{[1, 1], [3, 4], [6, 6]}", prettyPrint(ml2.getToRanges()));
456 * Convert a List of {[i, j], [k, l], ...} to "[[i, j], [k, l], ...]"
461 private String prettyPrint(List<int[]> ranges)
463 StringBuilder sb = new StringBuilder(ranges.size() * 5);
464 boolean first = true;
466 for (int[] range : ranges)
472 sb.append(Arrays.toString(range));
476 return sb.toString();
480 * Test the method that creates an inverse mapping
483 public void testGetInverse()
486 { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
488 { 1, 1, 3, 4, 6, 6 };
490 MapList ml = new MapList(codons, protein, 3, 1);
491 MapList ml2 = ml.getInverse();
492 assertEquals(ml.getFromRatio(), ml2.getToRatio());
493 assertEquals(ml.getFromRatio(), ml2.getToRatio());
494 assertEquals(ml.getToHighest(), ml2.getFromHighest());
495 assertEquals(ml.getFromHighest(), ml2.getToHighest());
496 assertEquals(prettyPrint(ml.getFromRanges()),
497 prettyPrint(ml2.getToRanges()));
498 assertEquals(prettyPrint(ml.getToRanges()),
499 prettyPrint(ml2.getFromRanges()));
503 public void testToString()
505 MapList ml = new MapList(new int[]
506 { 1, 5, 10, 15, 25, 20 }, new int[]
508 String s = ml.toString();
509 assertEquals("From (1:3) [ [1, 5] [10, 15] [25, 20] ] To [ [51, 1] ]",