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.assertSame;
27 import static org.testng.AssertJUnit.assertTrue;
28 import static org.testng.AssertJUnit.fail;
29 import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
35 import org.testng.annotations.BeforeClass;
36 import org.testng.annotations.Test;
38 import jalview.bin.Cache;
39 import jalview.gui.JvOptionPane;
41 public class MapListTest
43 @BeforeClass(alwaysRun = true)
49 @BeforeClass(alwaysRun = true)
50 public void setUpJvOptionPane()
52 JvOptionPane.setInteractiveMode(false);
53 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
56 @Test(groups = { "Functional" })
57 public void testSomething()
59 MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 },
62 MapList ml1 = new MapList(new int[] { 1, 3, 17, 4 },
65 MapList ml2 = new MapList(new int[] { 1, 60 }, new int[] { 1, 20 }, 3,
67 // test internal consistency
68 int to[] = new int[51];
70 MapList mldna = new MapList(new int[] { 2, 2, 6, 8, 12, 16 },
73 int[] frm = mldna.locateInFrom(1, 1);
74 testLocateFrom(mldna, 1, 1, new int[] { 2, 2, 6, 7 });
77 * for (int from=1; from<=51; from++) { int[] too=ml.shiftTo(from); int[]
78 * toofrom=ml.shiftFrom(too[0]);
79 * System.out.println("ShiftFrom("+from+")=="+too[0]+" %
80 * "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]); }
84 private static void testLocateFrom(MapList mldna, int i, int j, int[] ks)
86 int[] frm = mldna.locateInFrom(i, j);
87 assertEquals("Failed test locate from " + i + " to " + j,
88 Arrays.toString(frm), Arrays.toString(ks));
92 * test routine. not incremental.
98 private void testMap(MapList ml, int fromS, int fromE)
100 // todo convert to JUnit style tests
101 for (int from = 1; from <= 25; from++)
103 int[] too = ml.shiftFrom(from);
104 System.out.print("ShiftFrom(" + from + ")==");
107 System.out.print("NaN\n");
111 System.out.print(too[0] + " % " + too[1] + " (" + too[2] + ")");
112 System.out.print("\t+--+\t");
113 int[] toofrom = ml.shiftTo(too[0]);
116 if (toofrom[0] != from)
118 System.err.println("Mapping not reflexive:" + from + " "
119 + too[0] + "->" + toofrom[0]);
121 System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0]
122 + " % " + toofrom[1] + " (" + toofrom[2] + ")");
126 System.out.println("ShiftTo(" + too[0] + ")=="
127 + "NaN! - not Bijective Mapping!");
131 int mmap[][] = ml.makeFromMap();
132 System.out.println("FromMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
133 + mmap[0][2] + " " + mmap[0][3] + " ");
134 for (int i = 1; i <= mmap[1].length; i++)
136 if (mmap[1][i - 1] == -1)
138 System.out.print(i + "=XXX");
143 System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
147 System.out.print("\n");
151 System.out.print(",");
154 // test range function
155 System.out.print("\nTest locateInFrom\n");
157 int f = mmap[0][2], t = mmap[0][3];
160 System.out.println("Range " + f + " to " + t);
161 int rng[] = ml.locateInFrom(f, t);
164 for (int i = 0; i < rng.length; i++)
166 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
171 System.out.println("No range!");
173 System.out.print("\nReversed\n");
174 rng = ml.locateInFrom(t, f);
177 for (int i = 0; i < rng.length; i++)
179 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
184 System.out.println("No range!");
186 System.out.print("\n");
191 System.out.print("\n");
192 mmap = ml.makeToMap();
193 System.out.println("ToMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
194 + mmap[0][2] + " " + mmap[0][3] + " ");
195 for (int i = 1; i <= mmap[1].length; i++)
197 if (mmap[1][i - 1] == -1)
199 System.out.print(i + "=XXX");
204 System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
208 System.out.print("\n");
212 System.out.print(",");
215 System.out.print("\n");
216 // test range function
217 System.out.print("\nTest locateInTo\n");
219 int f = mmap[0][2], t = mmap[0][3];
222 System.out.println("Range " + f + " to " + t);
223 int rng[] = ml.locateInTo(f, t);
226 for (int i = 0; i < rng.length; i++)
228 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
233 System.out.println("No range!");
235 System.out.print("\nReversed\n");
236 rng = ml.locateInTo(t, f);
239 for (int i = 0; i < rng.length; i++)
241 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
246 System.out.println("No range!");
250 System.out.print("\n");
256 * Tests for method that locates ranges in the 'from' map for given range in
259 @Test(groups = { "Functional" })
260 public void testLocateInFrom_noIntrons()
263 * Simple mapping with no introns
265 int[] codons = new int[] { 1, 12 };
266 int[] protein = new int[] { 1, 4 };
267 MapList ml = new MapList(codons, protein, 3, 1);
268 assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(1, 1)));
269 assertEquals("[4, 6]", Arrays.toString(ml.locateInFrom(2, 2)));
270 assertEquals("[7, 9]", Arrays.toString(ml.locateInFrom(3, 3)));
271 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
272 assertEquals("[1, 6]", Arrays.toString(ml.locateInFrom(1, 2)));
273 assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(1, 3)));
274 assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 4)));
275 assertEquals("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3)));
276 assertEquals("[4, 12]", Arrays.toString(ml.locateInFrom(2, 4)));
277 assertEquals("[7, 12]", Arrays.toString(ml.locateInFrom(3, 4)));
278 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
280 assertNull(ml.locateInFrom(0, 0));
281 assertNull(ml.locateInFrom(1, 5));
282 assertNull(ml.locateInFrom(-1, 1));
286 * Tests for method that locates ranges in the 'from' map for given range in
289 @Test(groups = { "Functional" })
290 public void testLocateInFrom_withIntrons()
293 * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
294 * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
296 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
297 int[] protein = { 1, 4 };
298 MapList ml = new MapList(codons, protein, 3, 1);
299 assertEquals("[2, 3, 5, 5]", Arrays.toString(ml.locateInFrom(1, 1)));
300 assertEquals("[6, 7, 9, 9]", Arrays.toString(ml.locateInFrom(2, 2)));
301 assertEquals("[10, 10, 12, 12, 14, 14]",
302 Arrays.toString(ml.locateInFrom(3, 3)));
303 assertEquals("[16, 18]", Arrays.toString(ml.locateInFrom(4, 4)));
307 * Tests for method that locates ranges in the 'to' map for given range in the
310 @Test(groups = { "Functional" })
311 public void testLocateInTo_noIntrons()
314 * Simple mapping with no introns
316 int[] codons = new int[] { 1, 12 };
317 int[] protein = new int[] { 1, 4 };
318 MapList ml = new MapList(codons, protein, 3, 1);
319 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 3)));
320 assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
321 assertEquals("[3, 3]", Arrays.toString(ml.locateInTo(7, 9)));
322 assertEquals("[4, 4]", Arrays.toString(ml.locateInTo(10, 12)));
323 assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 6)));
324 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(1, 9)));
325 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(1, 12)));
326 assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
327 assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(4, 12)));
330 * A part codon is treated as if a whole one.
332 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
333 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 2)));
334 assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 4)));
335 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(2, 8)));
336 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
337 assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(5, 11)));
339 assertNull(ml.locateInTo(0, 0));
340 assertNull(ml.locateInTo(1, 13));
341 assertNull(ml.locateInTo(-1, 1));
345 * Tests for method that locates ranges in the 'to' map for given range in the
348 @Test(groups = { "Functional" })
349 public void testLocateInTo_withIntrons()
352 * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
353 * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
355 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
357 * Mapped proteins at positions 1, 3, 4, 6 in the sequence
359 int[] protein = { 1, 1, 3, 4, 6, 6 };
360 MapList ml = new MapList(codons, protein, 3, 1);
363 * Can't map from an unmapped position
365 assertNull(ml.locateInTo(1, 2));
366 assertNull(ml.locateInTo(2, 4));
367 assertNull(ml.locateInTo(4, 4));
370 * Valid range or subrange of codon1 maps to protein1.
372 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 2)));
373 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 3)));
374 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 5)));
375 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 3)));
376 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 5)));
378 // codon position 6 starts the next protein:
379 assertEquals("[1, 1, 3, 3]", Arrays.toString(ml.locateInTo(3, 6)));
381 // codon positions 7 to 17 (part) cover proteins 2/3/4 at positions 3/4/6
382 assertEquals("[3, 4, 6, 6]", Arrays.toString(ml.locateInTo(7, 17)));
387 * Test equals method.
389 @Test(groups = { "Functional" })
390 public void testEquals()
392 int[] codons = new int[] { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
393 int[] protein = new int[] { 1, 4 };
394 MapList ml = new MapList(codons, protein, 3, 1);
395 MapList ml1 = new MapList(codons, protein, 3, 1); // same values
396 MapList ml2 = new MapList(codons, protein, 2, 1); // fromRatio differs
397 MapList ml3 = new MapList(codons, protein, 3, 2); // toRatio differs
399 MapList ml6 = new MapList(codons, protein, 3, 1); // fromShifts differ
401 MapList ml7 = new MapList(codons, protein, 3, 1); // toShifts differ
403 assertTrue(ml.equals(ml));
404 assertEquals(ml.hashCode(), ml.hashCode());
405 assertTrue(ml.equals(ml1));
406 assertEquals(ml.hashCode(), ml1.hashCode());
407 assertTrue(ml1.equals(ml));
409 assertFalse(ml.equals(null));
410 assertFalse(ml.equals("hello"));
411 assertFalse(ml.equals(ml2));
412 assertFalse(ml.equals(ml3));
413 assertFalse(ml.equals(ml6));
414 assertFalse(ml.equals(ml7));
415 assertFalse(ml6.equals(ml7));
419 MapList ml4 = new MapList(codons, null, 3, 1); // toShifts null
420 assertFalse(ml.equals(ml4));
421 } catch (NullPointerException e)
423 // actually thrown by constructor before equals can be called
427 MapList ml5 = new MapList(null, protein, 3, 1); // fromShifts null
428 assertFalse(ml.equals(ml5));
429 } catch (NullPointerException e)
431 // actually thrown by constructor before equals can be called
436 * Test for the method that flattens a list of ranges into a single array.
438 @Test(groups = { "Functional" })
439 public void testGetRanges()
441 List<int[]> ranges = new ArrayList<>();
442 ranges.add(new int[] { 2, 3 });
443 ranges.add(new int[] { 5, 6 });
444 assertEquals("[2, 3, 5, 6]",
445 Arrays.toString(MapList.getRanges(ranges)));
449 * Check state after construction
451 @Test(groups = { "Functional" })
452 public void testConstructor()
454 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
455 int[] protein = { 1, 1, 3, 4, 6, 6 };
456 MapList ml = new MapList(codons, protein, 3, 1);
457 assertEquals(3, ml.getFromRatio());
458 assertEquals(2, ml.getFromLowest());
459 assertEquals(18, ml.getFromHighest());
460 assertEquals(1, ml.getToLowest());
461 assertEquals(6, ml.getToHighest());
462 assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
463 prettyPrint(ml.getFromRanges()));
464 assertEquals("{[1, 1], [3, 4], [6, 6]}", prettyPrint(ml.getToRanges()));
467 * Also copy constructor
469 MapList ml2 = new MapList(ml);
470 assertEquals(3, ml2.getFromRatio());
471 assertEquals(2, ml2.getFromLowest());
472 assertEquals(18, ml2.getFromHighest());
473 assertEquals(1, ml2.getToLowest());
474 assertEquals(6, ml2.getToHighest());
475 assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
476 prettyPrint(ml2.getFromRanges()));
477 assertEquals("{[1, 1], [3, 4], [6, 6]}",
478 prettyPrint(ml2.getToRanges()));
483 codons = new int[] { 9, 6 };
484 protein = new int[] { 100, 91, 80, 79 };
485 ml = new MapList(codons, protein, 3, 1);
486 assertEquals(6, ml.getFromLowest());
487 assertEquals(9, ml.getFromHighest());
488 assertEquals(79, ml.getToLowest());
489 assertEquals(100, ml.getToHighest());
493 * Test constructor used to merge consecutive ranges but now just leaves them
494 * as supplied (JAL-3751)
496 @Test(groups = { "Functional" })
497 public void testConstructor_mergeRanges()
499 int[] codons = { 2, 3, 3, 7, 9, 10, 12, 12, 13, 14, 16, 17 };
500 int[] protein = { 1, 1, 2, 3, 6, 6 };
501 MapList ml = new MapList(codons, protein, 3, 1);
502 assertEquals(3, ml.getFromRatio());
503 assertEquals(2, ml.getFromLowest());
504 assertEquals(17, ml.getFromHighest());
505 assertEquals(1, ml.getToLowest());
506 assertEquals(6, ml.getToHighest());
507 assertEquals("{[2, 3], [3, 7], [9, 10], [12, 12], [13, 14], [16, 17]}",
508 prettyPrint(ml.getFromRanges()));
509 assertEquals("{[1, 1], [2, 3], [6, 6]}", prettyPrint(ml.getToRanges()));
513 * Convert a List of {[i, j], [k, l], ...} to "[[i, j], [k, l], ...]"
518 private String prettyPrint(List<int[]> ranges)
520 StringBuilder sb = new StringBuilder(ranges.size() * 5);
521 boolean first = true;
523 for (int[] range : ranges)
529 sb.append(Arrays.toString(range));
533 return sb.toString();
537 * Test the method that creates an inverse mapping
539 @Test(groups = { "Functional" })
540 public void testGetInverse()
542 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
543 int[] protein = { 1, 1, 3, 4, 6, 6 };
545 MapList ml = new MapList(codons, protein, 3, 1);
546 MapList ml2 = ml.getInverse();
547 assertEquals(ml.getFromRatio(), ml2.getToRatio());
548 assertEquals(ml.getFromRatio(), ml2.getToRatio());
549 assertEquals(ml.getToHighest(), ml2.getFromHighest());
550 assertEquals(ml.getFromHighest(), ml2.getToHighest());
551 assertEquals(prettyPrint(ml.getFromRanges()),
552 prettyPrint(ml2.getToRanges()));
553 assertEquals(prettyPrint(ml.getToRanges()),
554 prettyPrint(ml2.getFromRanges()));
557 @Test(groups = { "Functional" })
558 public void testToString()
560 MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 },
563 String s = ml.toString();
564 assertEquals("[ [1, 5] [10, 15] [25, 20] ] 1:3 to [ [51, 1] ]", s);
567 @Test(groups = { "Functional" })
568 public void testAddMapList()
570 MapList ml = new MapList(new int[] { 11, 15, 20, 25, 35, 30 },
573 assertEquals(11, ml.getFromLowest());
574 assertEquals(35, ml.getFromHighest());
575 assertEquals(22, ml.getToLowest());
576 assertEquals(72, ml.getToHighest());
578 MapList ml2 = new MapList(new int[] { 2, 4, 37, 40 },
580 { 12, 17, 78, 83, 88, 96 }, 1, 3);
582 assertEquals(2, ml.getFromLowest());
583 assertEquals(40, ml.getFromHighest());
584 assertEquals(12, ml.getToLowest());
585 assertEquals(96, ml.getToHighest());
587 String s = ml.toString();
589 "[ [11, 15] [20, 25] [35, 30] [2, 4] [37, 40] ] 1:3 to [ [72, 22] [12, 17] [78, 83] [88, 96] ]",
594 * Test that confirms adding a map twice does nothing
596 @Test(groups = { "Functional" })
597 public void testAddMapList_sameMap()
599 MapList ml = new MapList(new int[] { 11, 15, 20, 25, 35, 30 },
602 String before = ml.toString();
604 assertEquals(before, ml.toString());
605 ml.addMapList(new MapList(ml));
606 assertEquals(before, ml.toString());
609 @Test(groups = { "Functional" })
610 public void testAddMapList_contiguous()
612 MapList ml = new MapList(new int[] { 11, 15 }, new int[] { 72, 58 }, 1,
615 MapList ml2 = new MapList(new int[] { 15, 16 }, new int[] { 58, 53 }, 1,
618 assertEquals("[ [11, 16] ] 1:3 to [ [72, 53] ]", ml.toString());
621 @Test(groups = "Functional")
622 public void testAddRange()
624 int[] range = { 1, 5 };
625 List<int[]> ranges = new ArrayList<>();
627 // add to empty list:
628 MapList.addRange(range, ranges);
629 assertEquals(1, ranges.size());
630 assertSame(range, ranges.get(0));
632 // extend contiguous (same position):
633 MapList.addRange(new int[] { 5, 10 }, ranges);
634 assertEquals(1, ranges.size());
635 assertEquals(1, ranges.get(0)[0]);
636 assertEquals(10, ranges.get(0)[1]);
638 // extend contiguous (next position):
639 MapList.addRange(new int[] { 11, 15 }, ranges);
640 assertEquals(1, ranges.size());
641 assertEquals(1, ranges.get(0)[0]);
642 assertEquals(15, ranges.get(0)[1]);
644 // change direction: range is not merged:
645 MapList.addRange(new int[] { 16, 10 }, ranges);
646 assertEquals(2, ranges.size());
647 assertEquals(16, ranges.get(1)[0]);
648 assertEquals(10, ranges.get(1)[1]);
650 // extend reverse contiguous (same position):
651 MapList.addRange(new int[] { 10, 8 }, ranges);
652 assertEquals(2, ranges.size());
653 assertEquals(16, ranges.get(1)[0]);
654 assertEquals(8, ranges.get(1)[1]);
656 // extend reverse contiguous (next position):
657 MapList.addRange(new int[] { 7, 6 }, ranges);
658 assertEquals(2, ranges.size());
659 assertEquals(16, ranges.get(1)[0]);
660 assertEquals(6, ranges.get(1)[1]);
662 // change direction: range is not merged:
663 MapList.addRange(new int[] { 6, 9 }, ranges);
664 assertEquals(3, ranges.size());
665 assertEquals(6, ranges.get(2)[0]);
666 assertEquals(9, ranges.get(2)[1]);
668 // not contiguous: not merged
669 MapList.addRange(new int[] { 11, 12 }, ranges);
670 assertEquals(4, ranges.size());
671 assertEquals(11, ranges.get(3)[0]);
672 assertEquals(12, ranges.get(3)[1]);
676 * Check state after construction
678 @Test(groups = { "Functional" })
679 public void testConstructor_withLists()
684 int[][] codons = new int[][] { { 9, 6 } };
685 int[][] protein = new int[][] { { 100, 91 }, { 80, 79 } };
686 MapList ml = new MapList(Arrays.asList(codons), Arrays.asList(protein),
688 assertEquals(6, ml.getFromLowest());
689 assertEquals(9, ml.getFromHighest());
690 assertEquals(79, ml.getToLowest());
691 assertEquals(100, ml.getToHighest());
695 * Test that method that inspects for the (first) forward or reverse from
696 * range. Single position ranges are ignored.
698 @Test(groups = { "Functional" })
699 public void testIsFromForwardStrand()
701 // [3-9] declares forward strand
702 MapList ml = new MapList(new int[] { 2, 2, 3, 9, 12, 11 },
705 assertTrue(ml.isFromForwardStrand());
707 // [11-5] declares reverse strand ([13-14] is ignored)
708 ml = new MapList(new int[] { 2, 2, 11, 5, 13, 14 },
711 assertFalse(ml.isFromForwardStrand());
713 // all single position ranges - defaults to forward strand
714 ml = new MapList(new int[] { 2, 2, 4, 4, 6, 6 }, new int[] { 3, 1 }, 1,
716 assertTrue(ml.isFromForwardStrand());
720 * Test the method that merges contiguous ranges
722 @Test(groups = { "Functional" })
723 public void testCoalesceRanges()
725 assertNull(MapList.coalesceRanges(null));
726 List<int[]> ranges = new ArrayList<>();
727 assertSame(ranges, MapList.coalesceRanges(ranges));
728 ranges.add(new int[] { 1, 3 });
729 assertSame(ranges, MapList.coalesceRanges(ranges));
731 // add non-contiguous range:
732 ranges.add(new int[] { 5, 6 });
733 assertSame(ranges, MapList.coalesceRanges(ranges));
735 // 'contiguous' range in opposite direction is not merged:
736 ranges.add(new int[] { 7, 6 });
737 assertSame(ranges, MapList.coalesceRanges(ranges));
739 // merging in forward direction:
741 ranges.add(new int[] { 1, 3 });
742 ranges.add(new int[] { 4, 5 }); // contiguous
743 ranges.add(new int[] { 5, 5 }); // overlap!
744 ranges.add(new int[] { 6, 7 }); // contiguous
745 List<int[]> merged = MapList.coalesceRanges(ranges);
746 assertEquals(2, merged.size());
747 assertArrayEquals(new int[] { 1, 5 }, merged.get(0));
748 assertArrayEquals(new int[] { 5, 7 }, merged.get(1));
749 // verify input list is unchanged
750 assertEquals(4, ranges.size());
751 assertArrayEquals(new int[] { 1, 3 }, ranges.get(0));
752 assertArrayEquals(new int[] { 4, 5 }, ranges.get(1));
753 assertArrayEquals(new int[] { 5, 5 }, ranges.get(2));
754 assertArrayEquals(new int[] { 6, 7 }, ranges.get(3));
756 // merging in reverse direction:
758 ranges.add(new int[] { 7, 5 });
759 ranges.add(new int[] { 5, 4 }); // overlap
760 ranges.add(new int[] { 4, 4 }); // overlap
761 ranges.add(new int[] { 3, 1 }); // contiguous
762 merged = MapList.coalesceRanges(ranges);
763 assertEquals(3, merged.size());
764 assertArrayEquals(new int[] { 7, 5 }, merged.get(0));
765 assertArrayEquals(new int[] { 5, 4 }, merged.get(1));
766 assertArrayEquals(new int[] { 4, 1 }, merged.get(2));
768 // merging with switches of direction:
770 ranges.add(new int[] { 1, 3 });
771 ranges.add(new int[] { 4, 5 }); // contiguous
772 ranges.add(new int[] { 5, 5 }); // overlap
773 ranges.add(new int[] { 6, 6 }); // contiguous
774 ranges.add(new int[] { 12, 10 });
775 ranges.add(new int[] { 9, 8 }); // contiguous
776 ranges.add(new int[] { 8, 8 }); // overlap
777 ranges.add(new int[] { 7, 7 }); // contiguous
778 merged = MapList.coalesceRanges(ranges);
779 assertEquals(4, merged.size());
780 assertArrayEquals(new int[] { 1, 5 }, merged.get(0));
781 assertArrayEquals(new int[] { 5, 6 }, merged.get(1));
782 assertArrayEquals(new int[] { 12, 8 }, merged.get(2));
783 assertArrayEquals(new int[] { 8, 7 }, merged.get(3));
785 // 'subsumed' ranges are preserved
787 ranges.add(new int[] { 10, 30 });
788 ranges.add(new int[] { 15, 25 });
789 merged = MapList.coalesceRanges(ranges);
790 assertEquals(2, merged.size());
791 assertArrayEquals(new int[] { 10, 30 }, merged.get(0));
792 assertArrayEquals(new int[] { 15, 25 }, merged.get(1));
796 * Test the method that compounds ('traverses') two mappings
798 @Test(groups = "Functional")
799 public void testTraverse()
802 * simple 1:1 plus 1:1 forwards
804 MapList ml1 = new MapList(new int[] { 3, 4, 8, 12 },
806 { 5, 8, 11, 13 }, 1, 1);
807 assertEquals("{[3, 4], [8, 12]}", prettyPrint(ml1.getFromRanges()));
808 assertEquals("{[5, 8], [11, 13]}", prettyPrint(ml1.getToRanges()));
810 MapList ml2 = new MapList(new int[] { 1, 50 },
812 { 40, 45, 70, 75, 90, 127 }, 1, 1);
813 assertEquals("{[1, 50]}", prettyPrint(ml2.getFromRanges()));
814 assertEquals("{[40, 45], [70, 75], [90, 127]}",
815 prettyPrint(ml2.getToRanges()));
817 MapList compound = ml1.traverse(ml2);
819 assertEquals(1, compound.getFromRatio());
820 assertEquals(1, compound.getToRatio());
821 List<int[]> fromRanges = compound.getFromRanges();
822 assertEquals(2, fromRanges.size());
823 assertArrayEquals(new int[] { 3, 4 }, fromRanges.get(0));
824 assertArrayEquals(new int[] { 8, 12 }, fromRanges.get(1));
825 List<int[]> toRanges = compound.getToRanges();
826 assertEquals(4, toRanges.size());
827 // 5-8 maps to 44-45,70-71
828 // 11-13 maps to 74-75,90
829 assertArrayEquals(new int[] { 44, 45 }, toRanges.get(0));
830 assertArrayEquals(new int[] { 70, 71 }, toRanges.get(1));
831 assertArrayEquals(new int[] { 74, 75 }, toRanges.get(2));
832 assertArrayEquals(new int[] { 90, 90 }, toRanges.get(3));
835 * 1:1 over 1:1 backwards ('reverse strand')
837 ml1 = new MapList(new int[] { 1, 50 }, new int[] { 70, 119 }, 1, 1);
838 ml2 = new MapList(new int[] { 1, 500 },
840 { 1000, 901, 600, 201 }, 1, 1);
841 compound = ml1.traverse(ml2);
843 assertEquals(1, compound.getFromRatio());
844 assertEquals(1, compound.getToRatio());
845 fromRanges = compound.getFromRanges();
846 assertEquals(1, fromRanges.size());
847 assertArrayEquals(new int[] { 1, 50 }, fromRanges.get(0));
848 toRanges = compound.getToRanges();
849 assertEquals(2, toRanges.size());
850 assertArrayEquals(new int[] { 931, 901 }, toRanges.get(0));
851 assertArrayEquals(new int[] { 600, 582 }, toRanges.get(1));
854 * 1:1 plus 1:3 should result in 1:3
856 ml1 = new MapList(new int[] { 1, 30 }, new int[] { 11, 40 }, 1, 1);
857 ml2 = new MapList(new int[] { 1, 100 }, new int[] { 1, 50, 91, 340 }, 1,
859 compound = ml1.traverse(ml2);
861 assertEquals(1, compound.getFromRatio());
862 assertEquals(3, compound.getToRatio());
863 fromRanges = compound.getFromRanges();
864 assertEquals(1, fromRanges.size());
865 assertArrayEquals(new int[] { 1, 30 }, fromRanges.get(0));
866 // 11-40 maps to 31-50,91-160
867 toRanges = compound.getToRanges();
868 assertEquals(2, toRanges.size());
869 assertArrayEquals(new int[] { 31, 50 }, toRanges.get(0));
870 assertArrayEquals(new int[] { 91, 160 }, toRanges.get(1));
873 * 3:1 plus 1:1 should result in 3:1
875 ml1 = new MapList(new int[] { 1, 30 }, new int[] { 11, 20 }, 3, 1);
876 ml2 = new MapList(new int[] { 1, 100 }, new int[] { 1, 15, 91, 175 }, 1,
878 compound = ml1.traverse(ml2);
880 assertEquals(3, compound.getFromRatio());
881 assertEquals(1, compound.getToRatio());
882 fromRanges = compound.getFromRanges();
883 assertEquals(1, fromRanges.size());
884 assertArrayEquals(new int[] { 1, 30 }, fromRanges.get(0));
885 // 11-20 maps to 11-15, 91-95
886 toRanges = compound.getToRanges();
887 assertEquals(2, toRanges.size());
888 assertArrayEquals(new int[] { 11, 15 }, toRanges.get(0));
889 assertArrayEquals(new int[] { 91, 95 }, toRanges.get(1));
892 * 1:3 plus 3:1 should result in 1:1
894 ml1 = new MapList(new int[] { 21, 40 }, new int[] { 13, 72 }, 1, 3);
895 ml2 = new MapList(new int[] { 1, 300 }, new int[] { 51, 70, 121, 200 },
897 compound = ml1.traverse(ml2);
899 assertEquals(1, compound.getFromRatio());
900 assertEquals(1, compound.getToRatio());
901 fromRanges = compound.getFromRanges();
902 assertEquals(1, fromRanges.size());
903 assertArrayEquals(new int[] { 21, 40 }, fromRanges.get(0));
904 // 13-72 maps 3:1 to 55-70, 121-124
905 toRanges = compound.getToRanges();
906 assertEquals(2, toRanges.size());
907 assertArrayEquals(new int[] { 55, 70 }, toRanges.get(0));
908 assertArrayEquals(new int[] { 121, 124 }, toRanges.get(1));
911 * 3:1 plus 1:3 should result in 1:1
913 ml1 = new MapList(new int[] { 31, 90 }, new int[] { 13, 32 }, 3, 1);
914 ml2 = new MapList(new int[] { 11, 40 }, new int[] { 41, 50, 71, 150 },
916 compound = ml1.traverse(ml2);
918 assertEquals(1, compound.getFromRatio());
919 assertEquals(1, compound.getToRatio());
920 fromRanges = compound.getFromRanges();
921 assertEquals(1, fromRanges.size());
922 assertArrayEquals(new int[] { 31, 90 }, fromRanges.get(0));
923 // 13-32 maps to 47-50,71-126
924 toRanges = compound.getToRanges();
925 assertEquals(2, toRanges.size());
926 assertArrayEquals(new int[] { 47, 50 }, toRanges.get(0));
927 assertArrayEquals(new int[] { 71, 126 }, toRanges.get(1));
930 * method returns null if not all regions are mapped through
932 ml1 = new MapList(new int[] { 1, 50 }, new int[] { 101, 150 }, 1, 1);
933 ml2 = new MapList(new int[] { 131, 180 }, new int[] { 201, 250 }, 1, 3);
934 compound = ml1.traverse(ml2);
935 assertNull(compound);
939 * Test that method that inspects for the (first) forward or reverse 'to'
940 * range. Single position ranges are ignored.
942 @Test(groups = { "Functional" })
943 public void testIsToForwardsStrand()
945 // [3-9] declares forward strand
946 MapList ml = new MapList(new int[] { 20, 11 },
948 { 2, 2, 3, 9, 12, 11 }, 1, 1);
949 assertTrue(ml.isToForwardStrand());
951 // [11-5] declares reverse strand ([13-14] is ignored)
952 ml = new MapList(new int[] { 20, 11 },
954 { 2, 2, 11, 5, 13, 14 }, 1, 1);
955 assertFalse(ml.isToForwardStrand());
957 // all single position ranges - defaults to forward strand
958 ml = new MapList(new int[] { 3, 1 }, new int[] { 2, 2, 4, 4, 6, 6 }, 1,
960 assertTrue(ml.isToForwardStrand());
964 * Test for mapping with overlapping ranges
966 @Test(groups = { "Functional" })
967 public void testLocateInFrom_withOverlap()
969 int[] codons = new int[] { 1, 12, 12, 17 };
970 int[] protein = new int[] { 1, 6 };
971 MapList ml = new MapList(codons, protein, 3, 1);
972 assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(1, 1)));
973 assertEquals("[4, 6]", Arrays.toString(ml.locateInFrom(2, 2)));
974 assertEquals("[7, 9]", Arrays.toString(ml.locateInFrom(3, 3)));
975 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
976 assertEquals("[12, 14]", Arrays.toString(ml.locateInFrom(5, 5)));
977 assertEquals("[15, 17]", Arrays.toString(ml.locateInFrom(6, 6)));
978 assertEquals("[1, 6]", Arrays.toString(ml.locateInFrom(1, 2)));
979 assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(1, 3)));
980 assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 4)));
981 assertEquals("[1, 12, 12, 14]", Arrays.toString(ml.locateInFrom(1, 5)));
982 assertEquals("[1, 12, 12, 17]", Arrays.toString(ml.locateInFrom(1, 6)));
983 assertEquals("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3)));
984 assertEquals("[7, 12, 12, 17]", Arrays.toString(ml.locateInFrom(3, 6)));
986 assertNull(ml.locateInFrom(0, 0));
987 assertNull(ml.locateInFrom(1, 7));
988 assertNull(ml.locateInFrom(-1, 1));
992 * Test for mapping with overlapping ranges
994 @Test(groups = { "Functional" })
995 public void testLocateInTo_withOverlap()
997 int[] codons = new int[] { 1, 12, 12, 17 };
998 int[] protein = new int[] { 1, 6 };
999 MapList ml = new MapList(codons, protein, 3, 1);
1000 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
1001 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(3, 8)));
1002 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(2, 11)));
1003 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
1005 // we want base 12 to map to both of the amino acids it codes for
1006 assertEquals("[4, 5]", Arrays.toString(ml.locateInTo(12, 12)));
1007 assertEquals("[4, 5]", Arrays.toString(ml.locateInTo(11, 12)));
1008 assertEquals("[4, 6]", Arrays.toString(ml.locateInTo(11, 15)));
1009 assertEquals("[6, 6]", Arrays.toString(ml.locateInTo(15, 17)));
1011 assertNull(ml.locateInTo(0, 0));
1012 assertNull(ml.locateInTo(1, 18));
1013 assertNull(ml.locateInTo(-1, 1));
1016 @Test(groups = { "Functional" })
1017 public void testTraverseToPosition()
1019 List<int[]> ranges = new ArrayList<>();
1020 assertNull(MapList.traverseToPosition(ranges, 0));
1022 ranges.add(new int[] { 3, 6 });
1023 assertNull(MapList.traverseToPosition(ranges, 0));
1026 @Test(groups = { "Functional" })
1027 public void testCountPos()
1031 MapList.countPos(null, 1);
1032 fail("expected exception");
1033 } catch (NullPointerException e)
1038 List<int[]> intervals = new ArrayList<>();
1039 assertNull(MapList.countPos(intervals, 1));
1044 intervals.add(new int[] {10, 20});
1045 assertNull(MapList.countPos(intervals, 9));
1046 assertNull(MapList.countPos(intervals, 21));
1047 assertArrayEquals(new int[] {1, 1}, MapList.countPos(intervals, 10));
1048 assertArrayEquals(new int[] {6, 1}, MapList.countPos(intervals, 15));
1049 assertArrayEquals(new int[] {11, 1}, MapList.countPos(intervals, 20));
1051 intervals.add(new int[] {25, 25});
1052 assertArrayEquals(new int[] {12, 1}, MapList.countPos(intervals, 25));
1054 // next interval repeats position 25 - which should be counted twice if traversed
1055 intervals.add(new int[] {25, 26});
1056 assertArrayEquals(new int[] {12, 1}, MapList.countPos(intervals, 25));
1057 assertArrayEquals(new int[] {14, 1}, MapList.countPos(intervals, 26));
1063 intervals.add(new int[] {5, -5});
1064 assertNull(MapList.countPos(intervals, 6));
1065 assertNull(MapList.countPos(intervals, -6));
1066 assertArrayEquals(new int[] {1, -1}, MapList.countPos(intervals, 5));
1067 assertArrayEquals(new int[] {7, -1}, MapList.countPos(intervals, -1));
1068 assertArrayEquals(new int[] {11, -1}, MapList.countPos(intervals, -5));
1071 * reverse then forward
1073 intervals.add(new int[] {5, 10});
1074 assertArrayEquals(new int[] {13, 1}, MapList.countPos(intervals, 6));
1077 * reverse then forward then reverse
1079 intervals.add(new int[] {-10, -20});
1080 assertArrayEquals(new int[] {20, -1}, MapList.countPos(intervals, -12));
1083 * an interval [x, x] is treated as forward
1085 intervals.add(new int[] {30, 30});
1086 assertArrayEquals(new int[] {29, 1}, MapList.countPos(intervals, 30));