2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertNull;
26 import static org.testng.AssertJUnit.assertTrue;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
32 import org.testng.annotations.Test;
34 public class MapListTest
37 @Test(groups = { "Functional" })
38 public void testSomething()
40 MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 }, new int[] {
42 MapList ml1 = new MapList(new int[] { 1, 3, 17, 4 },
43 new int[] { 51, 1 }, 1, 3);
44 MapList ml2 = new MapList(new int[] { 1, 60 }, new int[] { 1, 20 }, 3,
46 // test internal consistency
47 int to[] = new int[51];
49 MapList mldna = new MapList(new int[] { 2, 2, 6, 8, 12, 16 }, new int[]
51 int[] frm = mldna.locateInFrom(1, 1);
52 testLocateFrom(mldna, 1, 1, new int[] { 2, 2, 6, 7 });
55 * for (int from=1; from<=51; from++) { int[] too=ml.shiftTo(from); int[]
56 * toofrom=ml.shiftFrom(too[0]);
57 * System.out.println("ShiftFrom("+from+")=="+too[0]+" %
58 * "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]); }
62 private static void testLocateFrom(MapList mldna, int i, int j, int[] ks)
64 int[] frm = mldna.locateInFrom(i, j);
65 assertEquals("Failed test locate from " + i + " to " + j,
66 Arrays.toString(frm), Arrays.toString(ks));
70 * test routine. not incremental.
76 private void testMap(MapList ml, int fromS, int fromE)
78 // todo convert to JUnit style tests
79 for (int from = 1; from <= 25; from++)
81 int[] too = ml.shiftFrom(from);
82 System.out.print("ShiftFrom(" + from + ")==");
85 System.out.print("NaN\n");
89 System.out.print(too[0] + " % " + too[1] + " (" + too[2] + ")");
90 System.out.print("\t+--+\t");
91 int[] toofrom = ml.shiftTo(too[0]);
94 if (toofrom[0] != from)
96 System.err.println("Mapping not reflexive:" + from + " "
97 + too[0] + "->" + toofrom[0]);
99 System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0]
100 + " % " + toofrom[1] + " (" + toofrom[2] + ")");
104 System.out.println("ShiftTo(" + too[0] + ")=="
105 + "NaN! - not Bijective Mapping!");
109 int mmap[][] = ml.makeFromMap();
110 System.out.println("FromMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
111 + mmap[0][2] + " " + mmap[0][3] + " ");
112 for (int i = 1; i <= mmap[1].length; i++)
114 if (mmap[1][i - 1] == -1)
116 System.out.print(i + "=XXX");
121 System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
125 System.out.print("\n");
129 System.out.print(",");
132 // test range function
133 System.out.print("\nTest locateInFrom\n");
135 int f = mmap[0][2], t = mmap[0][3];
138 System.out.println("Range " + f + " to " + t);
139 int rng[] = ml.locateInFrom(f, t);
142 for (int i = 0; i < rng.length; i++)
144 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
149 System.out.println("No range!");
151 System.out.print("\nReversed\n");
152 rng = ml.locateInFrom(t, f);
155 for (int i = 0; i < rng.length; i++)
157 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
162 System.out.println("No range!");
164 System.out.print("\n");
169 System.out.print("\n");
170 mmap = ml.makeToMap();
171 System.out.println("ToMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
172 + mmap[0][2] + " " + mmap[0][3] + " ");
173 for (int i = 1; i <= mmap[1].length; i++)
175 if (mmap[1][i - 1] == -1)
177 System.out.print(i + "=XXX");
182 System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
186 System.out.print("\n");
190 System.out.print(",");
193 System.out.print("\n");
194 // test range function
195 System.out.print("\nTest locateInTo\n");
197 int f = mmap[0][2], t = mmap[0][3];
200 System.out.println("Range " + f + " to " + t);
201 int rng[] = ml.locateInTo(f, t);
204 for (int i = 0; i < rng.length; i++)
206 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
211 System.out.println("No range!");
213 System.out.print("\nReversed\n");
214 rng = ml.locateInTo(t, f);
217 for (int i = 0; i < rng.length; i++)
219 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
224 System.out.println("No range!");
228 System.out.print("\n");
234 * Tests for method that locates ranges in the 'from' map for given range in
237 @Test(groups = { "Functional" })
238 public void testLocateInFrom_noIntrons()
241 * Simple mapping with no introns
243 int[] codons = new int[] { 1, 12 };
244 int[] protein = new int[] { 1, 4 };
245 MapList ml = new MapList(codons, protein, 3, 1);
246 assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(1, 1)));
247 assertEquals("[4, 6]", Arrays.toString(ml.locateInFrom(2, 2)));
248 assertEquals("[7, 9]", Arrays.toString(ml.locateInFrom(3, 3)));
249 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
250 assertEquals("[1, 6]", Arrays.toString(ml.locateInFrom(1, 2)));
251 assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(1, 3)));
252 assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 4)));
253 assertEquals("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3)));
254 assertEquals("[4, 12]", Arrays.toString(ml.locateInFrom(2, 4)));
255 assertEquals("[7, 12]", Arrays.toString(ml.locateInFrom(3, 4)));
256 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
258 assertNull(ml.locateInFrom(0, 0));
259 assertNull(ml.locateInFrom(1, 5));
260 assertNull(ml.locateInFrom(-1, 1));
264 * Tests for method that locates ranges in the 'from' map for given range in
267 @Test(groups = { "Functional" })
268 public void testLocateInFrom_withIntrons()
271 * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
272 * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
274 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
275 int[] protein = { 1, 4 };
276 MapList ml = new MapList(codons, protein, 3, 1);
277 assertEquals("[2, 3, 5, 5]", Arrays.toString(ml.locateInFrom(1, 1)));
278 assertEquals("[6, 7, 9, 9]", Arrays.toString(ml.locateInFrom(2, 2)));
279 assertEquals("[10, 10, 12, 12, 14, 14]",
280 Arrays.toString(ml.locateInFrom(3, 3)));
281 assertEquals("[16, 18]", Arrays.toString(ml.locateInFrom(4, 4)));
285 * Tests for method that locates ranges in the 'to' map for given range in the
288 @Test(groups = { "Functional" })
289 public void testLocateInTo_noIntrons()
292 * Simple mapping with no introns
294 int[] codons = new int[] { 1, 12 };
295 int[] protein = new int[] { 1, 4 };
296 MapList ml = new MapList(codons, protein, 3, 1);
297 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 3)));
298 assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
299 assertEquals("[3, 3]", Arrays.toString(ml.locateInTo(7, 9)));
300 assertEquals("[4, 4]", Arrays.toString(ml.locateInTo(10, 12)));
301 assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 6)));
302 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(1, 9)));
303 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(1, 12)));
304 assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
305 assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(4, 12)));
308 * A part codon is treated as if a whole one.
310 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
311 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 2)));
312 assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 4)));
313 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(2, 8)));
314 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
315 assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(5, 11)));
317 assertNull(ml.locateInTo(0, 0));
318 assertNull(ml.locateInTo(1, 13));
319 assertNull(ml.locateInTo(-1, 1));
323 * Tests for method that locates ranges in the 'to' map for given range in the
326 @Test(groups = { "Functional" })
327 public void testLocateInTo_withIntrons()
330 * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
331 * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
333 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
335 * Mapped proteins at positions 1, 3, 4, 6 in the sequence
337 int[] protein = { 1, 1, 3, 4, 6, 6 };
338 MapList ml = new MapList(codons, protein, 3, 1);
341 * Can't map from an unmapped position
343 assertNull(ml.locateInTo(1, 2));
344 assertNull(ml.locateInTo(2, 4));
345 assertNull(ml.locateInTo(4, 4));
348 * Valid range or subrange of codon1 maps to protein1.
350 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 2)));
351 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 3)));
352 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 5)));
353 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 3)));
354 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 5)));
356 // codon position 6 starts the next protein:
357 assertEquals("[1, 1, 3, 3]", Arrays.toString(ml.locateInTo(3, 6)));
359 // codon positions 7 to 17 (part) cover proteins 2/3/4 at positions 3/4/6
360 assertEquals("[3, 4, 6, 6]", Arrays.toString(ml.locateInTo(7, 17)));
365 * Test equals method.
367 @Test(groups = { "Functional" })
368 public void testEquals()
370 int[] codons = new int[] { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
371 int[] protein = new int[] { 1, 4 };
372 MapList ml = new MapList(codons, protein, 3, 1);
373 MapList ml1 = new MapList(codons, protein, 3, 1); // same values
374 MapList ml2 = new MapList(codons, protein, 2, 1); // fromRatio differs
375 MapList ml3 = new MapList(codons, protein, 3, 2); // toRatio differs
377 MapList ml6 = new MapList(codons, protein, 3, 1); // fromShifts differ
379 MapList ml7 = new MapList(codons, protein, 3, 1); // toShifts differ
381 assertTrue(ml.equals(ml));
382 assertTrue(ml.equals(ml1));
383 assertTrue(ml1.equals(ml));
385 assertFalse(ml.equals(null));
386 assertFalse(ml.equals("hello"));
387 assertFalse(ml.equals(ml2));
388 assertFalse(ml.equals(ml3));
389 assertFalse(ml.equals(ml6));
390 assertFalse(ml.equals(ml7));
391 assertFalse(ml6.equals(ml7));
395 MapList ml4 = new MapList(codons, null, 3, 1); // toShifts null
396 assertFalse(ml.equals(ml4));
397 } catch (NullPointerException e)
399 // actually thrown by constructor before equals can be called
403 MapList ml5 = new MapList(null, protein, 3, 1); // fromShifts null
404 assertFalse(ml.equals(ml5));
405 } catch (NullPointerException e)
407 // actually thrown by constructor before equals can be called
412 * Test for the method that flattens a list of ranges into a single array.
414 @Test(groups = { "Functional" })
415 public void testGetRanges()
417 List<int[]> ranges = new ArrayList<int[]>();
418 ranges.add(new int[] { 2, 3 });
419 ranges.add(new int[] { 5, 6 });
420 assertEquals("[2, 3, 5, 6]", Arrays.toString(MapList.getRanges(ranges)));
424 * Check state after construction
426 @Test(groups = { "Functional" })
427 public void testConstructor()
429 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
430 int[] protein = { 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 * Test constructor can merge consecutive ranges
458 @Test(groups = { "Functional" })
459 public void testConstructor_mergeRanges()
461 int[] codons = { 2, 3, 3, 7, 9, 10, 12, 12, 14, 14, 16, 17 };
462 int[] protein = { 1, 1, 1, 3, 6, 6 };
463 MapList ml = new MapList(codons, protein, 3, 1);
464 assertEquals(3, ml.getFromRatio());
465 assertEquals(2, ml.getFromLowest());
466 assertEquals(17, ml.getFromHighest());
467 assertEquals(1, ml.getToLowest());
468 assertEquals(6, ml.getToHighest());
469 assertEquals("{[2, 7], [9, 10], [12, 12], [14, 14], [16, 17]}",
470 prettyPrint(ml.getFromRanges()));
471 assertEquals("{[1, 3], [6, 6]}", prettyPrint(ml.getToRanges()));
475 * Convert a List of {[i, j], [k, l], ...} to "[[i, j], [k, l], ...]"
480 private String prettyPrint(List<int[]> ranges)
482 StringBuilder sb = new StringBuilder(ranges.size() * 5);
483 boolean first = true;
485 for (int[] range : ranges)
491 sb.append(Arrays.toString(range));
495 return sb.toString();
499 * Test the method that creates an inverse mapping
501 @Test(groups = { "Functional" })
502 public void testGetInverse()
504 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
505 int[] protein = { 1, 1, 3, 4, 6, 6 };
507 MapList ml = new MapList(codons, protein, 3, 1);
508 MapList ml2 = ml.getInverse();
509 assertEquals(ml.getFromRatio(), ml2.getToRatio());
510 assertEquals(ml.getFromRatio(), ml2.getToRatio());
511 assertEquals(ml.getToHighest(), ml2.getFromHighest());
512 assertEquals(ml.getFromHighest(), ml2.getToHighest());
513 assertEquals(prettyPrint(ml.getFromRanges()),
514 prettyPrint(ml2.getToRanges()));
515 assertEquals(prettyPrint(ml.getToRanges()),
516 prettyPrint(ml2.getFromRanges()));
519 @Test(groups = { "Functional" })
520 public void testToString()
522 MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 }, new int[] {
524 String s = ml.toString();
525 assertEquals("From (1:3) [ [1, 5] [10, 15] [25, 20] ] To [ [51, 1] ]",