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[] { 1, 5, 10, 15, 25, 20 }, new int[] {
22 MapList ml1 = new MapList(new int[] { 1, 3, 17, 4 },
23 new int[] { 51, 1 }, 1, 3);
24 MapList ml2 = new MapList(new int[] { 1, 60 }, new int[] { 1, 20 }, 3,
26 // test internal consistency
27 int to[] = new int[51];
29 MapList mldna = new MapList(new int[] { 2, 2, 6, 8, 12, 16 }, new int[]
31 int[] frm = mldna.locateInFrom(1, 1);
32 testLocateFrom(mldna, 1, 1, new int[] { 2, 2, 6, 7 });
35 * for (int from=1; from<=51; from++) { int[] too=ml.shiftTo(from); int[]
36 * toofrom=ml.shiftFrom(too[0]);
37 * System.out.println("ShiftFrom("+from+")=="+too[0]+" %
38 * "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]); }
42 private static void testLocateFrom(MapList mldna, int i, int j, int[] ks)
44 int[] frm = mldna.locateInFrom(i, j);
45 assertEquals("Failed test locate from " + i + " to " + j,
46 Arrays.toString(frm), Arrays.toString(ks));
50 * test routine. not incremental.
56 private void testMap(MapList ml, int fromS, int fromE)
58 // todo convert to JUnit style tests
59 for (int from = 1; from <= 25; from++)
61 int[] too = ml.shiftFrom(from);
62 System.out.print("ShiftFrom(" + from + ")==");
65 System.out.print("NaN\n");
69 System.out.print(too[0] + " % " + too[1] + " (" + too[2] + ")");
70 System.out.print("\t+--+\t");
71 int[] toofrom = ml.shiftTo(too[0]);
74 if (toofrom[0] != from)
76 System.err.println("Mapping not reflexive:" + from + " "
77 + too[0] + "->" + toofrom[0]);
79 System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0]
80 + " % " + toofrom[1] + " (" + toofrom[2] + ")");
84 System.out.println("ShiftTo(" + too[0] + ")=="
85 + "NaN! - not Bijective Mapping!");
89 int mmap[][] = ml.makeFromMap();
90 System.out.println("FromMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
91 + mmap[0][2] + " " + mmap[0][3] + " ");
92 for (int i = 1; i <= mmap[1].length; i++)
94 if (mmap[1][i - 1] == -1)
96 System.out.print(i + "=XXX");
101 System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
105 System.out.print("\n");
109 System.out.print(",");
112 // test range function
113 System.out.print("\nTest locateInFrom\n");
115 int f = mmap[0][2], t = mmap[0][3];
118 System.out.println("Range " + f + " to " + t);
119 int rng[] = ml.locateInFrom(f, t);
122 for (int i = 0; i < rng.length; i++)
124 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
129 System.out.println("No range!");
131 System.out.print("\nReversed\n");
132 rng = ml.locateInFrom(t, f);
135 for (int i = 0; i < rng.length; i++)
137 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
142 System.out.println("No range!");
144 System.out.print("\n");
149 System.out.print("\n");
150 mmap = ml.makeToMap();
151 System.out.println("ToMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
152 + mmap[0][2] + " " + mmap[0][3] + " ");
153 for (int i = 1; i <= mmap[1].length; i++)
155 if (mmap[1][i - 1] == -1)
157 System.out.print(i + "=XXX");
162 System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
166 System.out.print("\n");
170 System.out.print(",");
173 System.out.print("\n");
174 // test range function
175 System.out.print("\nTest locateInTo\n");
177 int f = mmap[0][2], t = mmap[0][3];
180 System.out.println("Range " + f + " to " + t);
181 int rng[] = ml.locateInTo(f, t);
184 for (int i = 0; i < rng.length; i++)
186 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
191 System.out.println("No range!");
193 System.out.print("\nReversed\n");
194 rng = ml.locateInTo(t, f);
197 for (int i = 0; i < rng.length; i++)
199 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
204 System.out.println("No range!");
208 System.out.print("\n");
214 * Tests for method that locates ranges in the 'from' map for given range in
217 @Test(groups = { "Functional" })
218 public void testLocateInFrom_noIntrons()
221 * Simple mapping with no introns
223 int[] codons = new int[] { 1, 12 };
224 int[] protein = new int[] { 1, 4 };
225 MapList ml = new MapList(codons, protein, 3, 1);
226 assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(1, 1)));
227 assertEquals("[4, 6]", Arrays.toString(ml.locateInFrom(2, 2)));
228 assertEquals("[7, 9]", Arrays.toString(ml.locateInFrom(3, 3)));
229 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
230 assertEquals("[1, 6]", Arrays.toString(ml.locateInFrom(1, 2)));
231 assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(1, 3)));
232 assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 4)));
233 assertEquals("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3)));
234 assertEquals("[4, 12]", Arrays.toString(ml.locateInFrom(2, 4)));
235 assertEquals("[7, 12]", Arrays.toString(ml.locateInFrom(3, 4)));
236 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
238 assertNull(ml.locateInFrom(0, 0));
239 assertNull(ml.locateInFrom(1, 5));
240 assertNull(ml.locateInFrom(-1, 1));
244 * Tests for method that locates ranges in the 'from' map for given range in
247 @Test(groups = { "Functional" })
248 public void testLocateInFrom_withIntrons()
251 * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
252 * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
254 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
255 int[] protein = { 1, 4 };
256 MapList ml = new MapList(codons, protein, 3, 1);
257 assertEquals("[2, 3, 5, 5]", Arrays.toString(ml.locateInFrom(1, 1)));
258 assertEquals("[6, 7, 9, 9]", Arrays.toString(ml.locateInFrom(2, 2)));
259 assertEquals("[10, 10, 12, 12, 14, 14]",
260 Arrays.toString(ml.locateInFrom(3, 3)));
261 assertEquals("[16, 18]", Arrays.toString(ml.locateInFrom(4, 4)));
265 * Tests for method that locates ranges in the 'to' map for given range in the
268 @Test(groups = { "Functional" })
269 public void testLocateInTo_noIntrons()
272 * Simple mapping with no introns
274 int[] codons = new int[] { 1, 12 };
275 int[] protein = new int[] { 1, 4 };
276 MapList ml = new MapList(codons, protein, 3, 1);
277 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 3)));
278 assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
279 assertEquals("[3, 3]", Arrays.toString(ml.locateInTo(7, 9)));
280 assertEquals("[4, 4]", Arrays.toString(ml.locateInTo(10, 12)));
281 assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 6)));
282 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(1, 9)));
283 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(1, 12)));
284 assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
285 assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(4, 12)));
288 * A part codon is treated as if a whole one.
290 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
291 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 2)));
292 assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 4)));
293 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(2, 8)));
294 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
295 assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(5, 11)));
297 assertNull(ml.locateInTo(0, 0));
298 assertNull(ml.locateInTo(1, 13));
299 assertNull(ml.locateInTo(-1, 1));
303 * Tests for method that locates ranges in the 'to' map for given range in the
306 @Test(groups = { "Functional" })
307 public void testLocateInTo_withIntrons()
310 * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
311 * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
313 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
315 * Mapped proteins at positions 1, 3, 4, 6 in the sequence
317 int[] protein = { 1, 1, 3, 4, 6, 6 };
318 MapList ml = new MapList(codons, protein, 3, 1);
321 * Can't map from an unmapped position
323 assertNull(ml.locateInTo(1, 2));
324 assertNull(ml.locateInTo(2, 4));
325 assertNull(ml.locateInTo(4, 4));
328 * Valid range or subrange of codon1 maps to protein1.
330 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 2)));
331 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 3)));
332 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 5)));
333 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 3)));
334 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 5)));
336 // codon position 6 starts the next protein:
337 assertEquals("[1, 1, 3, 3]", Arrays.toString(ml.locateInTo(3, 6)));
339 // codon positions 7 to 17 (part) cover proteins 2/3/4 at positions 3/4/6
340 assertEquals("[3, 4, 6, 6]", Arrays.toString(ml.locateInTo(7, 17)));
345 * Test equals method.
347 @Test(groups = { "Functional" })
348 public void testEquals()
350 int[] codons = new int[] { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
351 int[] protein = new int[] { 1, 4 };
352 MapList ml = new MapList(codons, protein, 3, 1);
353 MapList ml1 = new MapList(codons, protein, 3, 1); // same values
354 MapList ml2 = new MapList(codons, protein, 2, 1); // fromRatio differs
355 MapList ml3 = new MapList(codons, protein, 3, 2); // toRatio differs
357 MapList ml6 = new MapList(codons, protein, 3, 1); // fromShifts differ
359 MapList ml7 = new MapList(codons, protein, 3, 1); // toShifts differ
361 assertTrue(ml.equals(ml));
362 assertTrue(ml.equals(ml1));
363 assertTrue(ml1.equals(ml));
365 assertFalse(ml.equals(null));
366 assertFalse(ml.equals("hello"));
367 assertFalse(ml.equals(ml2));
368 assertFalse(ml.equals(ml3));
369 assertFalse(ml.equals(ml6));
370 assertFalse(ml.equals(ml7));
371 assertFalse(ml6.equals(ml7));
375 MapList ml4 = new MapList(codons, null, 3, 1); // toShifts null
376 assertFalse(ml.equals(ml4));
377 } catch (NullPointerException e)
379 // actually thrown by constructor before equals can be called
383 MapList ml5 = new MapList(null, protein, 3, 1); // fromShifts null
384 assertFalse(ml.equals(ml5));
385 } catch (NullPointerException e)
387 // actually thrown by constructor before equals can be called
392 * Test for the method that flattens a list of ranges into a single array.
394 @Test(groups = { "Functional" })
395 public void testGetRanges()
397 List<int[]> ranges = new ArrayList<int[]>();
398 ranges.add(new int[] { 2, 3 });
399 ranges.add(new int[] { 5, 6 });
400 assertEquals("[2, 3, 5, 6]", Arrays.toString(MapList.getRanges(ranges)));
404 * Check state after construction
406 @Test(groups = { "Functional" })
407 public void testConstructor()
409 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
410 int[] protein = { 1, 1, 3, 4, 6, 6 };
411 MapList ml = new MapList(codons, protein, 3, 1);
412 assertEquals(3, ml.getFromRatio());
413 assertEquals(2, ml.getFromLowest());
414 assertEquals(18, ml.getFromHighest());
415 assertEquals(1, ml.getToLowest());
416 assertEquals(6, ml.getToHighest());
417 assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
418 prettyPrint(ml.getFromRanges()));
419 assertEquals("{[1, 1], [3, 4], [6, 6]}", prettyPrint(ml.getToRanges()));
422 * Also copy constructor
424 MapList ml2 = new MapList(ml);
425 assertEquals(3, ml2.getFromRatio());
426 assertEquals(2, ml2.getFromLowest());
427 assertEquals(18, ml2.getFromHighest());
428 assertEquals(1, ml2.getToLowest());
429 assertEquals(6, ml2.getToHighest());
430 assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
431 prettyPrint(ml2.getFromRanges()));
432 assertEquals("{[1, 1], [3, 4], [6, 6]}", prettyPrint(ml2.getToRanges()));
436 * Convert a List of {[i, j], [k, l], ...} to "[[i, j], [k, l], ...]"
441 private String prettyPrint(List<int[]> ranges)
443 StringBuilder sb = new StringBuilder(ranges.size() * 5);
444 boolean first = true;
446 for (int[] range : ranges)
452 sb.append(Arrays.toString(range));
456 return sb.toString();
460 * Test the method that creates an inverse mapping
462 @Test(groups = { "Functional" })
463 public void testGetInverse()
465 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
466 int[] protein = { 1, 1, 3, 4, 6, 6 };
468 MapList ml = new MapList(codons, protein, 3, 1);
469 MapList ml2 = ml.getInverse();
470 assertEquals(ml.getFromRatio(), ml2.getToRatio());
471 assertEquals(ml.getFromRatio(), ml2.getToRatio());
472 assertEquals(ml.getToHighest(), ml2.getFromHighest());
473 assertEquals(ml.getFromHighest(), ml2.getToHighest());
474 assertEquals(prettyPrint(ml.getFromRanges()),
475 prettyPrint(ml2.getToRanges()));
476 assertEquals(prettyPrint(ml.getToRanges()),
477 prettyPrint(ml2.getFromRanges()));
480 @Test(groups = { "Functional" })
481 public void testToString()
483 MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 }, new int[] {
485 String s = ml.toString();
486 assertEquals("From (1:3) [ [1, 5] [10, 15] [25, 20] ] To [ [51, 1] ]",