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.internal.junit.ArrayAsserts.assertArrayEquals;
30 import jalview.gui.JvOptionPane;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.List;
36 import org.testng.annotations.BeforeClass;
37 import org.testng.annotations.Test;
39 public class MapListTest
42 @BeforeClass(alwaysRun = true)
43 public void setUpJvOptionPane()
45 JvOptionPane.setInteractiveMode(false);
46 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
49 @Test(groups = { "Functional" })
50 public void testSomething()
52 MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 }, new int[] {
54 MapList ml1 = new MapList(new int[] { 1, 3, 17, 4 },
55 new int[] { 51, 1 }, 1, 3);
56 MapList ml2 = new MapList(new int[] { 1, 60 }, new int[] { 1, 20 }, 3,
58 // test internal consistency
59 int to[] = new int[51];
61 MapList mldna = new MapList(new int[] { 2, 2, 6, 8, 12, 16 }, new int[]
63 int[] frm = mldna.locateInFrom(1, 1);
64 testLocateFrom(mldna, 1, 1, new int[] { 2, 2, 6, 7 });
67 * for (int from=1; from<=51; from++) { int[] too=ml.shiftTo(from); int[]
68 * toofrom=ml.shiftFrom(too[0]);
69 * System.out.println("ShiftFrom("+from+")=="+too[0]+" %
70 * "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]); }
74 private static void testLocateFrom(MapList mldna, int i, int j, int[] ks)
76 int[] frm = mldna.locateInFrom(i, j);
77 assertEquals("Failed test locate from " + i + " to " + j,
78 Arrays.toString(frm), Arrays.toString(ks));
82 * test routine. not incremental.
88 private void testMap(MapList ml, int fromS, int fromE)
90 // todo convert to JUnit style tests
91 for (int from = 1; from <= 25; from++)
93 int[] too = ml.shiftFrom(from);
94 System.out.print("ShiftFrom(" + from + ")==");
97 System.out.print("NaN\n");
101 System.out.print(too[0] + " % " + too[1] + " (" + too[2] + ")");
102 System.out.print("\t+--+\t");
103 int[] toofrom = ml.shiftTo(too[0]);
106 if (toofrom[0] != from)
108 System.err.println("Mapping not reflexive:" + from + " "
109 + too[0] + "->" + toofrom[0]);
111 System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0]
112 + " % " + toofrom[1] + " (" + toofrom[2] + ")");
116 System.out.println("ShiftTo(" + too[0] + ")=="
117 + "NaN! - not Bijective Mapping!");
121 int mmap[][] = ml.makeFromMap();
122 System.out.println("FromMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
123 + mmap[0][2] + " " + mmap[0][3] + " ");
124 for (int i = 1; i <= mmap[1].length; i++)
126 if (mmap[1][i - 1] == -1)
128 System.out.print(i + "=XXX");
133 System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
137 System.out.print("\n");
141 System.out.print(",");
144 // test range function
145 System.out.print("\nTest locateInFrom\n");
147 int f = mmap[0][2], t = mmap[0][3];
150 System.out.println("Range " + f + " to " + t);
151 int rng[] = ml.locateInFrom(f, t);
154 for (int i = 0; i < rng.length; i++)
156 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
161 System.out.println("No range!");
163 System.out.print("\nReversed\n");
164 rng = ml.locateInFrom(t, f);
167 for (int i = 0; i < rng.length; i++)
169 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
174 System.out.println("No range!");
176 System.out.print("\n");
181 System.out.print("\n");
182 mmap = ml.makeToMap();
183 System.out.println("ToMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
184 + mmap[0][2] + " " + mmap[0][3] + " ");
185 for (int i = 1; i <= mmap[1].length; i++)
187 if (mmap[1][i - 1] == -1)
189 System.out.print(i + "=XXX");
194 System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
198 System.out.print("\n");
202 System.out.print(",");
205 System.out.print("\n");
206 // test range function
207 System.out.print("\nTest locateInTo\n");
209 int f = mmap[0][2], t = mmap[0][3];
212 System.out.println("Range " + f + " to " + t);
213 int rng[] = ml.locateInTo(f, t);
216 for (int i = 0; i < rng.length; i++)
218 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
223 System.out.println("No range!");
225 System.out.print("\nReversed\n");
226 rng = ml.locateInTo(t, f);
229 for (int i = 0; i < rng.length; i++)
231 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
236 System.out.println("No range!");
240 System.out.print("\n");
246 * Tests for method that locates ranges in the 'from' map for given range in
249 @Test(groups = { "Functional" })
250 public void testLocateInFrom_noIntrons()
253 * Simple mapping with no introns
255 int[] codons = new int[] { 1, 12 };
256 int[] protein = new int[] { 1, 4 };
257 MapList ml = new MapList(codons, protein, 3, 1);
258 assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(1, 1)));
259 assertEquals("[4, 6]", Arrays.toString(ml.locateInFrom(2, 2)));
260 assertEquals("[7, 9]", Arrays.toString(ml.locateInFrom(3, 3)));
261 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
262 assertEquals("[1, 6]", Arrays.toString(ml.locateInFrom(1, 2)));
263 assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(1, 3)));
264 assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 4)));
265 assertEquals("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3)));
266 assertEquals("[4, 12]", Arrays.toString(ml.locateInFrom(2, 4)));
267 assertEquals("[7, 12]", Arrays.toString(ml.locateInFrom(3, 4)));
268 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
270 assertNull(ml.locateInFrom(0, 0));
271 assertNull(ml.locateInFrom(1, 5));
272 assertNull(ml.locateInFrom(-1, 1));
276 * Tests for method that locates ranges in the 'from' map for given range in
279 @Test(groups = { "Functional" })
280 public void testLocateInFrom_withIntrons()
283 * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
284 * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
286 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
287 int[] protein = { 1, 4 };
288 MapList ml = new MapList(codons, protein, 3, 1);
289 assertEquals("[2, 3, 5, 5]", Arrays.toString(ml.locateInFrom(1, 1)));
290 assertEquals("[6, 7, 9, 9]", Arrays.toString(ml.locateInFrom(2, 2)));
291 assertEquals("[10, 10, 12, 12, 14, 14]",
292 Arrays.toString(ml.locateInFrom(3, 3)));
293 assertEquals("[16, 18]", Arrays.toString(ml.locateInFrom(4, 4)));
297 * Tests for method that locates ranges in the 'to' map for given range in the
300 @Test(groups = { "Functional" })
301 public void testLocateInTo_noIntrons()
304 * Simple mapping with no introns
306 int[] codons = new int[] { 1, 12 };
307 int[] protein = new int[] { 1, 4 };
308 MapList ml = new MapList(codons, protein, 3, 1);
309 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 3)));
310 assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
311 assertEquals("[3, 3]", Arrays.toString(ml.locateInTo(7, 9)));
312 assertEquals("[4, 4]", Arrays.toString(ml.locateInTo(10, 12)));
313 assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 6)));
314 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(1, 9)));
315 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(1, 12)));
316 assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
317 assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(4, 12)));
320 * A part codon is treated as if a whole one.
322 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
323 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 2)));
324 assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 4)));
325 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(2, 8)));
326 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
327 assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(5, 11)));
329 assertNull(ml.locateInTo(0, 0));
330 assertNull(ml.locateInTo(1, 13));
331 assertNull(ml.locateInTo(-1, 1));
335 * Tests for method that locates ranges in the 'to' map for given range in the
338 @Test(groups = { "Functional" })
339 public void testLocateInTo_withIntrons()
342 * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
343 * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
345 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
347 * Mapped proteins at positions 1, 3, 4, 6 in the sequence
349 int[] protein = { 1, 1, 3, 4, 6, 6 };
350 MapList ml = new MapList(codons, protein, 3, 1);
353 * Can't map from an unmapped position
355 assertNull(ml.locateInTo(1, 2));
356 assertNull(ml.locateInTo(2, 4));
357 assertNull(ml.locateInTo(4, 4));
360 * Valid range or subrange of codon1 maps to protein1.
362 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 2)));
363 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 3)));
364 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 5)));
365 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 3)));
366 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 5)));
368 // codon position 6 starts the next protein:
369 assertEquals("[1, 1, 3, 3]", Arrays.toString(ml.locateInTo(3, 6)));
371 // codon positions 7 to 17 (part) cover proteins 2/3/4 at positions 3/4/6
372 assertEquals("[3, 4, 6, 6]", Arrays.toString(ml.locateInTo(7, 17)));
377 * Test equals method.
379 @Test(groups = { "Functional" })
380 public void testEquals()
382 int[] codons = new int[] { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
383 int[] protein = new int[] { 1, 4 };
384 MapList ml = new MapList(codons, protein, 3, 1);
385 MapList ml1 = new MapList(codons, protein, 3, 1); // same values
386 MapList ml2 = new MapList(codons, protein, 2, 1); // fromRatio differs
387 MapList ml3 = new MapList(codons, protein, 3, 2); // toRatio differs
389 MapList ml6 = new MapList(codons, protein, 3, 1); // fromShifts differ
391 MapList ml7 = new MapList(codons, protein, 3, 1); // toShifts differ
393 assertTrue(ml.equals(ml));
394 assertEquals(ml.hashCode(), ml.hashCode());
395 assertTrue(ml.equals(ml1));
396 assertEquals(ml.hashCode(), ml1.hashCode());
397 assertTrue(ml1.equals(ml));
399 assertFalse(ml.equals(null));
400 assertFalse(ml.equals("hello"));
401 assertFalse(ml.equals(ml2));
402 assertFalse(ml.equals(ml3));
403 assertFalse(ml.equals(ml6));
404 assertFalse(ml.equals(ml7));
405 assertFalse(ml6.equals(ml7));
409 MapList ml4 = new MapList(codons, null, 3, 1); // toShifts null
410 assertFalse(ml.equals(ml4));
411 } catch (NullPointerException e)
413 // actually thrown by constructor before equals can be called
417 MapList ml5 = new MapList(null, protein, 3, 1); // fromShifts null
418 assertFalse(ml.equals(ml5));
419 } catch (NullPointerException e)
421 // actually thrown by constructor before equals can be called
426 * Test for the method that flattens a list of ranges into a single array.
428 @Test(groups = { "Functional" })
429 public void testGetRanges()
431 List<int[]> ranges = new ArrayList<>();
432 ranges.add(new int[] { 2, 3 });
433 ranges.add(new int[] { 5, 6 });
434 assertEquals("[2, 3, 5, 6]", Arrays.toString(MapList.getRanges(ranges)));
438 * Check state after construction
440 @Test(groups = { "Functional" })
441 public void testConstructor()
443 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
444 int[] protein = { 1, 1, 3, 4, 6, 6 };
445 MapList ml = new MapList(codons, protein, 3, 1);
446 assertEquals(3, ml.getFromRatio());
447 assertEquals(2, ml.getFromLowest());
448 assertEquals(18, ml.getFromHighest());
449 assertEquals(1, ml.getToLowest());
450 assertEquals(6, ml.getToHighest());
451 assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
452 prettyPrint(ml.getFromRanges()));
453 assertEquals("{[1, 1], [3, 4], [6, 6]}", prettyPrint(ml.getToRanges()));
456 * Also copy constructor
458 MapList ml2 = new MapList(ml);
459 assertEquals(3, ml2.getFromRatio());
460 assertEquals(2, ml2.getFromLowest());
461 assertEquals(18, ml2.getFromHighest());
462 assertEquals(1, ml2.getToLowest());
463 assertEquals(6, ml2.getToHighest());
464 assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
465 prettyPrint(ml2.getFromRanges()));
466 assertEquals("{[1, 1], [3, 4], [6, 6]}", prettyPrint(ml2.getToRanges()));
471 codons = new int[] { 9, 6 };
472 protein = new int[] { 100, 91, 80, 79 };
473 ml = new MapList(codons, protein, 3, 1);
474 assertEquals(6, ml.getFromLowest());
475 assertEquals(9, ml.getFromHighest());
476 assertEquals(79, ml.getToLowest());
477 assertEquals(100, ml.getToHighest());
481 * Test constructor can merge consecutive ranges
483 @Test(groups = { "Functional" })
484 public void testConstructor_mergeRanges()
486 int[] codons = { 2, 3, 3, 7, 9, 10, 12, 12, 14, 14, 16, 17 };
487 int[] protein = { 1, 1, 1, 3, 6, 6 };
488 MapList ml = new MapList(codons, protein, 3, 1);
489 assertEquals(3, ml.getFromRatio());
490 assertEquals(2, ml.getFromLowest());
491 assertEquals(17, ml.getFromHighest());
492 assertEquals(1, ml.getToLowest());
493 assertEquals(6, ml.getToHighest());
494 assertEquals("{[2, 7], [9, 10], [12, 12], [14, 14], [16, 17]}",
495 prettyPrint(ml.getFromRanges()));
496 assertEquals("{[1, 3], [6, 6]}", prettyPrint(ml.getToRanges()));
500 * Convert a List of {[i, j], [k, l], ...} to "[[i, j], [k, l], ...]"
505 private String prettyPrint(List<int[]> ranges)
507 StringBuilder sb = new StringBuilder(ranges.size() * 5);
508 boolean first = true;
510 for (int[] range : ranges)
516 sb.append(Arrays.toString(range));
520 return sb.toString();
524 * Test the method that creates an inverse mapping
526 @Test(groups = { "Functional" })
527 public void testGetInverse()
529 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
530 int[] protein = { 1, 1, 3, 4, 6, 6 };
532 MapList ml = new MapList(codons, protein, 3, 1);
533 MapList ml2 = ml.getInverse();
534 assertEquals(ml.getFromRatio(), ml2.getToRatio());
535 assertEquals(ml.getFromRatio(), ml2.getToRatio());
536 assertEquals(ml.getToHighest(), ml2.getFromHighest());
537 assertEquals(ml.getFromHighest(), ml2.getToHighest());
538 assertEquals(prettyPrint(ml.getFromRanges()),
539 prettyPrint(ml2.getToRanges()));
540 assertEquals(prettyPrint(ml.getToRanges()),
541 prettyPrint(ml2.getFromRanges()));
544 @Test(groups = { "Functional" })
545 public void testToString()
547 MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 }, new int[] {
549 String s = ml.toString();
550 assertEquals("[ [1, 5] [10, 15] [25, 20] ] 1:3 to [ [51, 1] ]", s);
553 @Test(groups = { "Functional" })
554 public void testAddMapList()
556 MapList ml = new MapList(new int[] { 11, 15, 20, 25, 35, 30 },
557 new int[] { 72, 22 }, 1, 3);
558 assertEquals(11, ml.getFromLowest());
559 assertEquals(35, ml.getFromHighest());
560 assertEquals(22, ml.getToLowest());
561 assertEquals(72, ml.getToHighest());
563 MapList ml2 = new MapList(new int[] { 2, 4, 37, 40 }, new int[] { 12,
564 17, 78, 83, 88, 96 }, 1, 3);
566 assertEquals(2, ml.getFromLowest());
567 assertEquals(40, ml.getFromHighest());
568 assertEquals(12, ml.getToLowest());
569 assertEquals(96, ml.getToHighest());
571 String s = ml.toString();
573 "[ [11, 15] [20, 25] [35, 30] [2, 4] [37, 40] ] 1:3 to [ [72, 22] [12, 17] [78, 83] [88, 96] ]",
578 * Test that confirms adding a map twice does nothing
580 @Test(groups = { "Functional" })
581 public void testAddMapList_sameMap()
583 MapList ml = new MapList(new int[] { 11, 15, 20, 25, 35, 30 },
584 new int[] { 72, 22 }, 1, 3);
585 String before = ml.toString();
587 assertEquals(before, ml.toString());
588 ml.addMapList(new MapList(ml));
589 assertEquals(before, ml.toString());
592 @Test(groups = { "Functional" })
593 public void testAddMapList_contiguous()
595 MapList ml = new MapList(new int[] { 11, 15 }, new int[] { 72, 58 }, 1,
598 MapList ml2 = new MapList(new int[] { 15, 16 }, new int[] { 58, 53 },
601 assertEquals("[ [11, 16] ] 1:3 to [ [72, 53] ]", ml.toString());
605 * Check state after construction
607 @Test(groups = { "Functional" })
608 public void testConstructor_withLists()
613 int[][] codons = new int[][] { { 9, 6 } };
614 int[][] protein = new int[][] { { 100, 91 }, { 80, 79 } };
615 MapList ml = new MapList(Arrays.asList(codons), Arrays.asList(protein),
617 assertEquals(6, ml.getFromLowest());
618 assertEquals(9, ml.getFromHighest());
619 assertEquals(79, ml.getToLowest());
620 assertEquals(100, ml.getToHighest());
624 * Test that method that inspects for the (first) forward or reverse from
625 * range. Single position ranges are ignored.
627 @Test(groups = { "Functional" })
628 public void testIsFromForwardStrand()
630 // [3-9] declares forward strand
631 MapList ml = new MapList(new int[] { 2, 2, 3, 9, 12, 11 }, new int[] {
633 assertTrue(ml.isFromForwardStrand());
635 // [11-5] declares reverse strand ([13-14] is ignored)
636 ml = new MapList(new int[] { 2, 2, 11, 5, 13, 14 },
637 new int[] { 20, 11 }, 1, 1);
638 assertFalse(ml.isFromForwardStrand());
640 // all single position ranges - defaults to forward strand
641 ml = new MapList(new int[] { 2, 2, 4, 4, 6, 6 }, new int[] { 3, 1 }, 1,
643 assertTrue(ml.isFromForwardStrand());
647 * Test the method that merges a list of ranges where possible
649 @Test(groups = { "Functional" })
650 public void testCoalesceRanges()
652 assertNull(MapList.coalesceRanges(null));
653 List<int[]> ranges = new ArrayList<>();
654 assertSame(ranges, MapList.coalesceRanges(ranges));
655 ranges.add(new int[] { 1, 3 });
656 assertSame(ranges, MapList.coalesceRanges(ranges));
658 // add non-contiguous range:
659 ranges.add(new int[] { 5, 6 });
660 assertSame(ranges, MapList.coalesceRanges(ranges));
662 // 'contiguous' range in opposite direction is not merged:
663 ranges.add(new int[] { 7, 6 });
664 assertSame(ranges, MapList.coalesceRanges(ranges));
666 // merging in forward direction:
668 ranges.add(new int[] { 1, 3 });
669 ranges.add(new int[] { 4, 5 });
670 ranges.add(new int[] { 5, 5 });
671 ranges.add(new int[] { 5, 7 });
672 List<int[]> merged = MapList.coalesceRanges(ranges);
673 assertEquals(1, merged.size());
674 assertArrayEquals(new int[] { 1, 7 }, merged.get(0));
675 // verify input list is unchanged
676 assertEquals(4, ranges.size());
677 assertArrayEquals(new int[] { 1, 3 }, ranges.get(0));
678 assertArrayEquals(new int[] { 4, 5 }, ranges.get(1));
679 assertArrayEquals(new int[] { 5, 5 }, ranges.get(2));
680 assertArrayEquals(new int[] { 5, 7 }, ranges.get(3));
682 // merging in reverse direction:
684 ranges.add(new int[] { 7, 5 });
685 ranges.add(new int[] { 5, 4 });
686 ranges.add(new int[] { 4, 4 });
687 ranges.add(new int[] { 3, 1 });
688 merged = MapList.coalesceRanges(ranges);
689 assertEquals(1, merged.size());
690 assertArrayEquals(new int[] { 7, 1 }, merged.get(0));
692 // merging with switches of direction:
694 ranges.add(new int[] { 1, 3 });
695 ranges.add(new int[] { 4, 5 });
696 ranges.add(new int[] { 5, 5 });
697 ranges.add(new int[] { 6, 6 });
698 ranges.add(new int[] { 12, 10 });
699 ranges.add(new int[] { 9, 8 });
700 ranges.add(new int[] { 8, 8 });
701 ranges.add(new int[] { 7, 7 });
702 merged = MapList.coalesceRanges(ranges);
703 assertEquals(2, merged.size());
704 assertArrayEquals(new int[] { 1, 6 }, merged.get(0));
705 assertArrayEquals(new int[] { 12, 7 }, merged.get(1));
709 * Test the method that merges a list of ranges where possible
711 @Test(groups = { "Functional" })
712 public void testCoalesceRanges_withOverlap()
714 List<int[]> ranges = new ArrayList<>();
715 ranges.add(new int[] { 1, 3 });
716 ranges.add(new int[] { 2, 5 });
719 * [2, 5] should extend [1, 3]
721 List<int[]> merged = MapList.coalesceRanges(ranges);
722 assertEquals(1, merged.size());
723 assertArrayEquals(new int[] { 1, 5 }, merged.get(0));
726 * a subsumed interval should be dropped
729 ranges.add(new int[] { 1, 6 });
730 ranges.add(new int[] { 2, 4 });
731 merged = MapList.coalesceRanges(ranges);
732 assertEquals(1, merged.size());
733 assertArrayEquals(new int[] { 1, 6 }, merged.get(0));
736 ranges.add(new int[] { 1, 5 });
737 ranges.add(new int[] { 1, 6 });
738 merged = MapList.coalesceRanges(ranges);
739 assertEquals(1, merged.size());
740 assertArrayEquals(new int[] { 1, 6 }, merged.get(0));
743 * merge duplicate ranges
746 ranges.add(new int[] { 1, 3 });
747 ranges.add(new int[] { 1, 3 });
748 merged = MapList.coalesceRanges(ranges);
749 assertEquals(1, merged.size());
750 assertArrayEquals(new int[] { 1, 3 }, merged.get(0));
756 ranges.add(new int[] { 9, 5 });
757 ranges.add(new int[] { 9, 4 });
758 ranges.add(new int[] { 8, 3 });
759 ranges.add(new int[] { 3, 2 });
760 ranges.add(new int[] { 1, 0 });
761 merged = MapList.coalesceRanges(ranges);
762 assertEquals(1, merged.size());
763 assertArrayEquals(new int[] { 9, 0 }, merged.get(0));
767 * Test the method that compounds ('traverses') two mappings
769 @Test(groups = "Functional")
770 public void testTraverse()
773 * simple 1:1 plus 1:1 forwards
775 MapList ml1 = new MapList(new int[] { 3, 4, 8, 12 }, new int[] { 5, 8,
777 MapList ml2 = new MapList(new int[] { 1, 50 }, new int[] { 40, 45, 70,
778 75, 90, 127 }, 1, 1);
779 MapList compound = ml1.traverse(ml2);
781 assertEquals(compound.getFromRatio(), 1);
782 assertEquals(compound.getToRatio(), 1);
783 List<int[]> fromRanges = compound.getFromRanges();
784 assertEquals(fromRanges.size(), 2);
785 assertArrayEquals(new int[] { 3, 4 }, fromRanges.get(0));
786 assertArrayEquals(new int[] { 8, 12 }, fromRanges.get(1));
787 List<int[]> toRanges = compound.getToRanges();
788 assertEquals(toRanges.size(), 2);
789 // 5-8 maps to 44-45,70-71
790 // 11-13 maps to 74-75,90
791 assertArrayEquals(new int[] { 44, 45, 70, 71 }, toRanges.get(0));
792 assertArrayEquals(new int[] { 74, 75, 90, 90 }, toRanges.get(1));
795 * 1:1 over 1:1 backwards ('reverse strand')
797 ml1 = new MapList(new int[] { 1, 50 }, new int[] { 70, 119 }, 1, 1);
798 ml2 = new MapList(new int[] { 1, 500 },
799 new int[] { 1000, 901, 600, 201 }, 1, 1);
800 compound = ml1.traverse(ml2);
802 assertEquals(compound.getFromRatio(), 1);
803 assertEquals(compound.getToRatio(), 1);
804 fromRanges = compound.getFromRanges();
805 assertEquals(fromRanges.size(), 1);
806 assertArrayEquals(new int[] { 1, 50 }, fromRanges.get(0));
807 toRanges = compound.getToRanges();
808 assertEquals(toRanges.size(), 1);
809 assertArrayEquals(new int[] { 931, 901, 600, 582 }, toRanges.get(0));
812 * 1:1 plus 1:3 should result in 1:3
814 ml1 = new MapList(new int[] { 1, 30 }, new int[] { 11, 40 }, 1, 1);
815 ml2 = new MapList(new int[] { 1, 100 }, new int[] { 1, 50, 91, 340 },
817 compound = ml1.traverse(ml2);
819 assertEquals(compound.getFromRatio(), 1);
820 assertEquals(compound.getToRatio(), 3);
821 fromRanges = compound.getFromRanges();
822 assertEquals(fromRanges.size(), 1);
823 assertArrayEquals(new int[] { 1, 30 }, fromRanges.get(0));
824 // 11-40 maps to 31-50,91-160
825 toRanges = compound.getToRanges();
826 assertEquals(toRanges.size(), 1);
827 assertArrayEquals(new int[] { 31, 50, 91, 160 }, toRanges.get(0));
830 * 3:1 plus 1:1 should result in 3:1
832 ml1 = new MapList(new int[] { 1, 30 }, new int[] { 11, 20 }, 3, 1);
833 ml2 = new MapList(new int[] { 1, 100 }, new int[] { 1, 15, 91, 175 },
835 compound = ml1.traverse(ml2);
837 assertEquals(compound.getFromRatio(), 3);
838 assertEquals(compound.getToRatio(), 1);
839 fromRanges = compound.getFromRanges();
840 assertEquals(fromRanges.size(), 1);
841 assertArrayEquals(new int[] { 1, 30 }, fromRanges.get(0));
842 // 11-20 maps to 11-15, 91-95
843 toRanges = compound.getToRanges();
844 assertEquals(toRanges.size(), 1);
845 assertArrayEquals(new int[] { 11, 15, 91, 95 }, toRanges.get(0));
848 * 1:3 plus 3:1 should result in 1:1
850 ml1 = new MapList(new int[] { 21, 40 }, new int[] { 13, 72 }, 1, 3);
851 ml2 = new MapList(new int[] { 1, 300 }, new int[] { 51, 70, 121, 200 },
853 compound = ml1.traverse(ml2);
855 assertEquals(compound.getFromRatio(), 1);
856 assertEquals(compound.getToRatio(), 1);
857 fromRanges = compound.getFromRanges();
858 assertEquals(fromRanges.size(), 1);
859 assertArrayEquals(new int[] { 21, 40 }, fromRanges.get(0));
860 // 13-72 maps 3:1 to 55-70, 121-124
861 toRanges = compound.getToRanges();
862 assertEquals(toRanges.size(), 1);
863 assertArrayEquals(new int[] { 55, 70, 121, 124 }, toRanges.get(0));
866 * 3:1 plus 1:3 should result in 1:1
868 ml1 = new MapList(new int[] { 31, 90 }, new int[] { 13, 32 }, 3, 1);
869 ml2 = new MapList(new int[] { 11, 40 }, new int[] { 41, 50, 71, 150 },
871 compound = ml1.traverse(ml2);
873 assertEquals(compound.getFromRatio(), 1);
874 assertEquals(compound.getToRatio(), 1);
875 fromRanges = compound.getFromRanges();
876 assertEquals(fromRanges.size(), 1);
877 assertArrayEquals(new int[] { 31, 90 }, fromRanges.get(0));
878 // 13-32 maps to 47-50,71-126
879 toRanges = compound.getToRanges();
880 assertEquals(toRanges.size(), 1);
881 assertArrayEquals(new int[] { 47, 50, 71, 126 }, toRanges.get(0));
884 * method returns null if not all regions are mapped through
886 ml1 = new MapList(new int[] { 1, 50 }, new int[] { 101, 150 }, 1, 1);
887 ml2 = new MapList(new int[] { 131, 180 }, new int[] { 201, 250 }, 1, 3);
888 compound = ml1.traverse(ml2);
889 assertNull(compound);
893 * Test that method that inspects for the (first) forward or reverse 'to' range.
894 * Single position ranges are ignored.
896 @Test(groups = { "Functional" })
897 public void testIsToForwardsStrand()
899 // [3-9] declares forward strand
900 MapList ml = new MapList(new int[] { 20, 11 },
902 { 2, 2, 3, 9, 12, 11 }, 1, 1);
903 assertTrue(ml.isToForwardStrand());
905 // [11-5] declares reverse strand ([13-14] is ignored)
906 ml = new MapList(new int[] { 20, 11 },
908 { 2, 2, 11, 5, 13, 14 }, 1, 1);
909 assertFalse(ml.isToForwardStrand());
911 // all single position ranges - defaults to forward strand
912 ml = new MapList(new int[] { 3, 1 }, new int[] { 2, 2, 4, 4, 6, 6 }, 1,
914 assertTrue(ml.isToForwardStrand());