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 java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.List;
34 import org.testng.annotations.BeforeClass;
35 import org.testng.annotations.Test;
37 import jalview.bin.Cache;
38 import jalview.gui.JvOptionPane;
40 public class MapListTest
42 @BeforeClass(alwaysRun = true)
48 @BeforeClass(alwaysRun = true)
49 public void setUpJvOptionPane()
51 JvOptionPane.setInteractiveMode(false);
52 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
55 @Test(groups = { "Functional" })
56 public void testSomething()
58 MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 },
61 MapList ml1 = new MapList(new int[] { 1, 3, 17, 4 },
64 MapList ml2 = new MapList(new int[] { 1, 60 }, new int[] { 1, 20 }, 3,
66 // test internal consistency
67 int to[] = new int[51];
69 MapList mldna = new MapList(new int[] { 2, 2, 6, 8, 12, 16 },
72 int[] frm = mldna.locateInFrom(1, 1);
73 testLocateFrom(mldna, 1, 1, new int[] { 2, 2, 6, 7 });
76 * for (int from=1; from<=51; from++) { int[] too=ml.shiftTo(from); int[]
77 * toofrom=ml.shiftFrom(too[0]);
78 * System.out.println("ShiftFrom("+from+")=="+too[0]+" %
79 * "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]); }
83 private static void testLocateFrom(MapList mldna, int i, int j, int[] ks)
85 int[] frm = mldna.locateInFrom(i, j);
86 assertEquals("Failed test locate from " + i + " to " + j,
87 Arrays.toString(frm), Arrays.toString(ks));
91 * test routine. not incremental.
97 private void testMap(MapList ml, int fromS, int fromE)
99 // todo convert to JUnit style tests
100 for (int from = 1; from <= 25; from++)
102 int[] too = ml.shiftFrom(from);
103 System.out.print("ShiftFrom(" + from + ")==");
106 System.out.print("NaN\n");
110 System.out.print(too[0] + " % " + too[1] + " (" + too[2] + ")");
111 System.out.print("\t+--+\t");
112 int[] toofrom = ml.shiftTo(too[0]);
115 if (toofrom[0] != from)
117 System.err.println("Mapping not reflexive:" + from + " "
118 + too[0] + "->" + toofrom[0]);
120 System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0]
121 + " % " + toofrom[1] + " (" + toofrom[2] + ")");
125 System.out.println("ShiftTo(" + too[0] + ")=="
126 + "NaN! - not Bijective Mapping!");
130 int mmap[][] = ml.makeFromMap();
131 System.out.println("FromMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
132 + mmap[0][2] + " " + mmap[0][3] + " ");
133 for (int i = 1; i <= mmap[1].length; i++)
135 if (mmap[1][i - 1] == -1)
137 System.out.print(i + "=XXX");
142 System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
146 System.out.print("\n");
150 System.out.print(",");
153 // test range function
154 System.out.print("\nTest locateInFrom\n");
156 int f = mmap[0][2], t = mmap[0][3];
159 System.out.println("Range " + f + " to " + t);
160 int rng[] = ml.locateInFrom(f, t);
163 for (int i = 0; i < rng.length; i++)
165 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
170 System.out.println("No range!");
172 System.out.print("\nReversed\n");
173 rng = ml.locateInFrom(t, f);
176 for (int i = 0; i < rng.length; i++)
178 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
183 System.out.println("No range!");
185 System.out.print("\n");
190 System.out.print("\n");
191 mmap = ml.makeToMap();
192 System.out.println("ToMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
193 + mmap[0][2] + " " + mmap[0][3] + " ");
194 for (int i = 1; i <= mmap[1].length; i++)
196 if (mmap[1][i - 1] == -1)
198 System.out.print(i + "=XXX");
203 System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
207 System.out.print("\n");
211 System.out.print(",");
214 System.out.print("\n");
215 // test range function
216 System.out.print("\nTest locateInTo\n");
218 int f = mmap[0][2], t = mmap[0][3];
221 System.out.println("Range " + f + " to " + t);
222 int rng[] = ml.locateInTo(f, t);
225 for (int i = 0; i < rng.length; i++)
227 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
232 System.out.println("No range!");
234 System.out.print("\nReversed\n");
235 rng = ml.locateInTo(t, f);
238 for (int i = 0; i < rng.length; i++)
240 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
245 System.out.println("No range!");
249 System.out.print("\n");
255 * Tests for method that locates ranges in the 'from' map for given range in
258 @Test(groups = { "Functional" })
259 public void testLocateInFrom_noIntrons()
262 * Simple mapping with no introns
264 int[] codons = new int[] { 1, 12 };
265 int[] protein = new int[] { 1, 4 };
266 MapList ml = new MapList(codons, protein, 3, 1);
267 assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(1, 1)));
268 assertEquals("[4, 6]", Arrays.toString(ml.locateInFrom(2, 2)));
269 assertEquals("[7, 9]", Arrays.toString(ml.locateInFrom(3, 3)));
270 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
271 assertEquals("[1, 6]", Arrays.toString(ml.locateInFrom(1, 2)));
272 assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(1, 3)));
273 assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 4)));
274 assertEquals("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3)));
275 assertEquals("[4, 12]", Arrays.toString(ml.locateInFrom(2, 4)));
276 assertEquals("[7, 12]", Arrays.toString(ml.locateInFrom(3, 4)));
277 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
279 assertNull(ml.locateInFrom(0, 0));
280 assertNull(ml.locateInFrom(1, 5));
281 assertNull(ml.locateInFrom(-1, 1));
285 * Tests for method that locates ranges in the 'from' map for given range in
288 @Test(groups = { "Functional" })
289 public void testLocateInFrom_withIntrons()
292 * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
293 * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
295 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
296 int[] protein = { 1, 4 };
297 MapList ml = new MapList(codons, protein, 3, 1);
298 assertEquals("[2, 3, 5, 5]", Arrays.toString(ml.locateInFrom(1, 1)));
299 assertEquals("[6, 7, 9, 9]", Arrays.toString(ml.locateInFrom(2, 2)));
300 assertEquals("[10, 10, 12, 12, 14, 14]",
301 Arrays.toString(ml.locateInFrom(3, 3)));
302 assertEquals("[16, 18]", Arrays.toString(ml.locateInFrom(4, 4)));
306 * Tests for method that locates ranges in the 'to' map for given range in the
309 @Test(groups = { "Functional" })
310 public void testLocateInTo_noIntrons()
313 * Simple mapping with no introns
315 int[] codons = new int[] { 1, 12 };
316 int[] protein = new int[] { 1, 4 };
317 MapList ml = new MapList(codons, protein, 3, 1);
318 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 3)));
319 assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
320 assertEquals("[3, 3]", Arrays.toString(ml.locateInTo(7, 9)));
321 assertEquals("[4, 4]", Arrays.toString(ml.locateInTo(10, 12)));
322 assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 6)));
323 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(1, 9)));
324 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(1, 12)));
325 assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
326 assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(4, 12)));
329 * A part codon is treated as if a whole one.
331 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
332 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 2)));
333 assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 4)));
334 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(2, 8)));
335 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
336 assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(5, 11)));
338 assertNull(ml.locateInTo(0, 0));
339 assertNull(ml.locateInTo(1, 13));
340 assertNull(ml.locateInTo(-1, 1));
344 * Tests for method that locates ranges in the 'to' map for given range in the
347 @Test(groups = { "Functional" })
348 public void testLocateInTo_withIntrons()
351 * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
352 * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
354 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
356 * Mapped proteins at positions 1, 3, 4, 6 in the sequence
358 int[] protein = { 1, 1, 3, 4, 6, 6 };
359 MapList ml = new MapList(codons, protein, 3, 1);
362 * Can't map from an unmapped position
364 assertNull(ml.locateInTo(1, 2));
365 assertNull(ml.locateInTo(2, 4));
366 assertNull(ml.locateInTo(4, 4));
369 * Valid range or subrange of codon1 maps to protein1.
371 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 2)));
372 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 3)));
373 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 5)));
374 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 3)));
375 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 5)));
377 // codon position 6 starts the next protein:
378 assertEquals("[1, 1, 3, 3]", Arrays.toString(ml.locateInTo(3, 6)));
380 // codon positions 7 to 17 (part) cover proteins 2/3/4 at positions 3/4/6
381 assertEquals("[3, 4, 6, 6]", Arrays.toString(ml.locateInTo(7, 17)));
386 * Test equals method.
388 @Test(groups = { "Functional" })
389 public void testEquals()
391 int[] codons = new int[] { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
392 int[] protein = new int[] { 1, 4 };
393 MapList ml = new MapList(codons, protein, 3, 1);
394 MapList ml1 = new MapList(codons, protein, 3, 1); // same values
395 MapList ml2 = new MapList(codons, protein, 2, 1); // fromRatio differs
396 MapList ml3 = new MapList(codons, protein, 3, 2); // toRatio differs
398 MapList ml6 = new MapList(codons, protein, 3, 1); // fromShifts differ
400 MapList ml7 = new MapList(codons, protein, 3, 1); // toShifts differ
402 assertTrue(ml.equals(ml));
403 assertEquals(ml.hashCode(), ml.hashCode());
404 assertTrue(ml.equals(ml1));
405 assertEquals(ml.hashCode(), ml1.hashCode());
406 assertTrue(ml1.equals(ml));
408 assertFalse(ml.equals(null));
409 assertFalse(ml.equals("hello"));
410 assertFalse(ml.equals(ml2));
411 assertFalse(ml.equals(ml3));
412 assertFalse(ml.equals(ml6));
413 assertFalse(ml.equals(ml7));
414 assertFalse(ml6.equals(ml7));
418 MapList ml4 = new MapList(codons, null, 3, 1); // toShifts null
419 assertFalse(ml.equals(ml4));
420 } catch (NullPointerException e)
422 // actually thrown by constructor before equals can be called
426 MapList ml5 = new MapList(null, protein, 3, 1); // fromShifts null
427 assertFalse(ml.equals(ml5));
428 } catch (NullPointerException e)
430 // actually thrown by constructor before equals can be called
435 * Test for the method that flattens a list of ranges into a single array.
437 @Test(groups = { "Functional" })
438 public void testGetRanges()
440 List<int[]> ranges = new ArrayList<>();
441 ranges.add(new int[] { 2, 3 });
442 ranges.add(new int[] { 5, 6 });
443 assertEquals("[2, 3, 5, 6]",
444 Arrays.toString(MapList.getRanges(ranges)));
448 * Check state after construction
450 @Test(groups = { "Functional" })
451 public void testConstructor()
453 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
454 int[] protein = { 1, 1, 3, 4, 6, 6 };
455 MapList ml = new MapList(codons, protein, 3, 1);
456 assertEquals(3, ml.getFromRatio());
457 assertEquals(2, ml.getFromLowest());
458 assertEquals(18, ml.getFromHighest());
459 assertEquals(1, ml.getToLowest());
460 assertEquals(6, ml.getToHighest());
461 assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
462 prettyPrint(ml.getFromRanges()));
463 assertEquals("{[1, 1], [3, 4], [6, 6]}", prettyPrint(ml.getToRanges()));
466 * Also copy constructor
468 MapList ml2 = new MapList(ml);
469 assertEquals(3, ml2.getFromRatio());
470 assertEquals(2, ml2.getFromLowest());
471 assertEquals(18, ml2.getFromHighest());
472 assertEquals(1, ml2.getToLowest());
473 assertEquals(6, ml2.getToHighest());
474 assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
475 prettyPrint(ml2.getFromRanges()));
476 assertEquals("{[1, 1], [3, 4], [6, 6]}",
477 prettyPrint(ml2.getToRanges()));
482 codons = new int[] { 9, 6 };
483 protein = new int[] { 100, 91, 80, 79 };
484 ml = new MapList(codons, protein, 3, 1);
485 assertEquals(6, ml.getFromLowest());
486 assertEquals(9, ml.getFromHighest());
487 assertEquals(79, ml.getToLowest());
488 assertEquals(100, ml.getToHighest());
492 * Test constructor used to merge consecutive ranges but now just leaves them
493 * as supplied (JAL-3751)
495 @Test(groups = { "Functional" })
496 public void testConstructor_mergeRanges()
498 int[] codons = { 2, 3, 3, 7, 9, 10, 12, 12, 13, 14, 16, 17 };
499 int[] protein = { 1, 1, 2, 3, 6, 6 };
500 MapList ml = new MapList(codons, protein, 3, 1);
501 assertEquals(3, ml.getFromRatio());
502 assertEquals(2, ml.getFromLowest());
503 assertEquals(17, ml.getFromHighest());
504 assertEquals(1, ml.getToLowest());
505 assertEquals(6, ml.getToHighest());
506 assertEquals("{[2, 3], [3, 7], [9, 10], [12, 12], [13, 14], [16, 17]}",
507 prettyPrint(ml.getFromRanges()));
508 assertEquals("{[1, 1], [2, 3], [6, 6]}", prettyPrint(ml.getToRanges()));
512 * Convert a List of {[i, j], [k, l], ...} to "[[i, j], [k, l], ...]"
517 private String prettyPrint(List<int[]> ranges)
519 StringBuilder sb = new StringBuilder(ranges.size() * 5);
520 boolean first = true;
522 for (int[] range : ranges)
528 sb.append(Arrays.toString(range));
532 return sb.toString();
536 * Test the method that creates an inverse mapping
538 @Test(groups = { "Functional" })
539 public void testGetInverse()
541 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
542 int[] protein = { 1, 1, 3, 4, 6, 6 };
544 MapList ml = new MapList(codons, protein, 3, 1);
545 MapList ml2 = ml.getInverse();
546 assertEquals(ml.getFromRatio(), ml2.getToRatio());
547 assertEquals(ml.getFromRatio(), ml2.getToRatio());
548 assertEquals(ml.getToHighest(), ml2.getFromHighest());
549 assertEquals(ml.getFromHighest(), ml2.getToHighest());
550 assertEquals(prettyPrint(ml.getFromRanges()),
551 prettyPrint(ml2.getToRanges()));
552 assertEquals(prettyPrint(ml.getToRanges()),
553 prettyPrint(ml2.getFromRanges()));
556 @Test(groups = { "Functional" })
557 public void testToString()
559 MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 },
562 String s = ml.toString();
563 assertEquals("[ [1, 5] [10, 15] [25, 20] ] 1:3 to [ [51, 1] ]", s);
566 @Test(groups = { "Functional" })
567 public void testAddMapList()
569 MapList ml = new MapList(new int[] { 11, 15, 20, 25, 35, 30 },
572 assertEquals(11, ml.getFromLowest());
573 assertEquals(35, ml.getFromHighest());
574 assertEquals(22, ml.getToLowest());
575 assertEquals(72, ml.getToHighest());
577 MapList ml2 = new MapList(new int[] { 2, 4, 37, 40 },
579 { 12, 17, 78, 83, 88, 96 }, 1, 3);
581 assertEquals(2, ml.getFromLowest());
582 assertEquals(40, ml.getFromHighest());
583 assertEquals(12, ml.getToLowest());
584 assertEquals(96, ml.getToHighest());
586 String s = ml.toString();
588 "[ [11, 15] [20, 25] [35, 30] [2, 4] [37, 40] ] 1:3 to [ [72, 22] [12, 17] [78, 83] [88, 96] ]",
593 * Test that confirms adding a map twice does nothing
595 @Test(groups = { "Functional" })
596 public void testAddMapList_sameMap()
598 MapList ml = new MapList(new int[] { 11, 15, 20, 25, 35, 30 },
601 String before = ml.toString();
603 assertEquals(before, ml.toString());
604 ml.addMapList(new MapList(ml));
605 assertEquals(before, ml.toString());
608 @Test(groups = { "Functional" })
609 public void testAddMapList_contiguous()
611 MapList ml = new MapList(new int[] { 11, 15 }, new int[] { 72, 58 }, 1,
614 MapList ml2 = new MapList(new int[] { 15, 16 }, new int[] { 58, 53 }, 1,
617 assertEquals("[ [11, 16] ] 1:3 to [ [72, 53] ]", ml.toString());
620 @Test(groups = "Functional")
621 public void testAddRange()
623 int[] range = { 1, 5 };
624 List<int[]> ranges = new ArrayList<>();
626 // add to empty list:
627 MapList.addRange(range, ranges);
628 assertEquals(1, ranges.size());
629 assertSame(range, ranges.get(0));
631 // extend contiguous (same position):
632 MapList.addRange(new int[] { 5, 10 }, ranges);
633 assertEquals(1, ranges.size());
634 assertEquals(1, ranges.get(0)[0]);
635 assertEquals(10, ranges.get(0)[1]);
637 // extend contiguous (next position):
638 MapList.addRange(new int[] { 11, 15 }, ranges);
639 assertEquals(1, ranges.size());
640 assertEquals(1, ranges.get(0)[0]);
641 assertEquals(15, ranges.get(0)[1]);
643 // change direction: range is not merged:
644 MapList.addRange(new int[] { 16, 10 }, ranges);
645 assertEquals(2, ranges.size());
646 assertEquals(16, ranges.get(1)[0]);
647 assertEquals(10, ranges.get(1)[1]);
649 // extend reverse contiguous (same position):
650 MapList.addRange(new int[] { 10, 8 }, ranges);
651 assertEquals(2, ranges.size());
652 assertEquals(16, ranges.get(1)[0]);
653 assertEquals(8, ranges.get(1)[1]);
655 // extend reverse contiguous (next position):
656 MapList.addRange(new int[] { 7, 6 }, ranges);
657 assertEquals(2, ranges.size());
658 assertEquals(16, ranges.get(1)[0]);
659 assertEquals(6, ranges.get(1)[1]);
661 // change direction: range is not merged:
662 MapList.addRange(new int[] { 6, 9 }, ranges);
663 assertEquals(3, ranges.size());
664 assertEquals(6, ranges.get(2)[0]);
665 assertEquals(9, ranges.get(2)[1]);
667 // not contiguous: not merged
668 MapList.addRange(new int[] { 11, 12 }, ranges);
669 assertEquals(4, ranges.size());
670 assertEquals(11, ranges.get(3)[0]);
671 assertEquals(12, ranges.get(3)[1]);
675 * Check state after construction
677 @Test(groups = { "Functional" })
678 public void testConstructor_withLists()
683 int[][] codons = new int[][] { { 9, 6 } };
684 int[][] protein = new int[][] { { 100, 91 }, { 80, 79 } };
685 MapList ml = new MapList(Arrays.asList(codons), Arrays.asList(protein),
687 assertEquals(6, ml.getFromLowest());
688 assertEquals(9, ml.getFromHighest());
689 assertEquals(79, ml.getToLowest());
690 assertEquals(100, ml.getToHighest());
694 * Test that method that inspects for the (first) forward or reverse from
695 * range. Single position ranges are ignored.
697 @Test(groups = { "Functional" })
698 public void testIsFromForwardStrand()
700 // [3-9] declares forward strand
701 MapList ml = new MapList(new int[] { 2, 2, 3, 9, 12, 11 },
704 assertTrue(ml.isFromForwardStrand());
706 // [11-5] declares reverse strand ([13-14] is ignored)
707 ml = new MapList(new int[] { 2, 2, 11, 5, 13, 14 },
710 assertFalse(ml.isFromForwardStrand());
712 // all single position ranges - defaults to forward strand
713 ml = new MapList(new int[] { 2, 2, 4, 4, 6, 6 }, new int[] { 3, 1 }, 1,
715 assertTrue(ml.isFromForwardStrand());
719 * Test the method that merges contiguous ranges
721 @Test(groups = { "Functional" })
722 public void testCoalesceRanges()
724 assertNull(MapList.coalesceRanges(null));
725 List<int[]> ranges = new ArrayList<>();
726 assertSame(ranges, MapList.coalesceRanges(ranges));
727 ranges.add(new int[] { 1, 3 });
728 assertSame(ranges, MapList.coalesceRanges(ranges));
730 // add non-contiguous range:
731 ranges.add(new int[] { 5, 6 });
732 assertSame(ranges, MapList.coalesceRanges(ranges));
734 // 'contiguous' range in opposite direction is not merged:
735 ranges.add(new int[] { 7, 6 });
736 assertSame(ranges, MapList.coalesceRanges(ranges));
738 // merging in forward direction:
740 ranges.add(new int[] { 1, 3 });
741 ranges.add(new int[] { 4, 5 }); // contiguous
742 ranges.add(new int[] { 5, 5 }); // overlap!
743 ranges.add(new int[] { 6, 7 }); // contiguous
744 List<int[]> merged = MapList.coalesceRanges(ranges);
745 assertEquals(2, merged.size());
746 assertArrayEquals(new int[] { 1, 5 }, merged.get(0));
747 assertArrayEquals(new int[] { 5, 7 }, merged.get(1));
748 // verify input list is unchanged
749 assertEquals(4, ranges.size());
750 assertArrayEquals(new int[] { 1, 3 }, ranges.get(0));
751 assertArrayEquals(new int[] { 4, 5 }, ranges.get(1));
752 assertArrayEquals(new int[] { 5, 5 }, ranges.get(2));
753 assertArrayEquals(new int[] { 6, 7 }, ranges.get(3));
755 // merging in reverse direction:
757 ranges.add(new int[] { 7, 5 });
758 ranges.add(new int[] { 5, 4 }); // overlap
759 ranges.add(new int[] { 4, 4 }); // overlap
760 ranges.add(new int[] { 3, 1 }); // contiguous
761 merged = MapList.coalesceRanges(ranges);
762 assertEquals(3, merged.size());
763 assertArrayEquals(new int[] { 7, 5 }, merged.get(0));
764 assertArrayEquals(new int[] { 5, 4 }, merged.get(1));
765 assertArrayEquals(new int[] { 4, 1 }, merged.get(2));
767 // merging with switches of direction:
769 ranges.add(new int[] { 1, 3 });
770 ranges.add(new int[] { 4, 5 }); // contiguous
771 ranges.add(new int[] { 5, 5 }); // overlap
772 ranges.add(new int[] { 6, 6 }); // contiguous
773 ranges.add(new int[] { 12, 10 });
774 ranges.add(new int[] { 9, 8 }); // contiguous
775 ranges.add(new int[] { 8, 8 }); // overlap
776 ranges.add(new int[] { 7, 7 }); // contiguous
777 merged = MapList.coalesceRanges(ranges);
778 assertEquals(4, merged.size());
779 assertArrayEquals(new int[] { 1, 5 }, merged.get(0));
780 assertArrayEquals(new int[] { 5, 6 }, merged.get(1));
781 assertArrayEquals(new int[] { 12, 8 }, merged.get(2));
782 assertArrayEquals(new int[] { 8, 7 }, merged.get(3));
784 // 'subsumed' ranges are preserved
786 ranges.add(new int[] { 10, 30 });
787 ranges.add(new int[] { 15, 25 });
788 merged = MapList.coalesceRanges(ranges);
789 assertEquals(2, merged.size());
790 assertArrayEquals(new int[] { 10, 30 }, merged.get(0));
791 assertArrayEquals(new int[] { 15, 25 }, merged.get(1));
795 * Test the method that compounds ('traverses') two mappings
797 @Test(groups = "Functional")
798 public void testTraverse()
801 * simple 1:1 plus 1:1 forwards
803 MapList ml1 = new MapList(new int[] { 3, 4, 8, 12 },
805 { 5, 8, 11, 13 }, 1, 1);
806 assertEquals("{[3, 4], [8, 12]}", prettyPrint(ml1.getFromRanges()));
807 assertEquals("{[5, 8], [11, 13]}", prettyPrint(ml1.getToRanges()));
809 MapList ml2 = new MapList(new int[] { 1, 50 },
811 { 40, 45, 70, 75, 90, 127 }, 1, 1);
812 assertEquals("{[1, 50]}", prettyPrint(ml2.getFromRanges()));
813 assertEquals("{[40, 45], [70, 75], [90, 127]}",
814 prettyPrint(ml2.getToRanges()));
816 MapList compound = ml1.traverse(ml2);
818 assertEquals(1, compound.getFromRatio());
819 assertEquals(1, compound.getToRatio());
820 List<int[]> fromRanges = compound.getFromRanges();
821 assertEquals(2, fromRanges.size());
822 assertArrayEquals(new int[] { 3, 4 }, fromRanges.get(0));
823 assertArrayEquals(new int[] { 8, 12 }, fromRanges.get(1));
824 List<int[]> toRanges = compound.getToRanges();
825 assertEquals(4, toRanges.size());
826 // 5-8 maps to 44-45,70-71
827 // 11-13 maps to 74-75,90
828 assertArrayEquals(new int[] { 44, 45 }, toRanges.get(0));
829 assertArrayEquals(new int[] { 70, 71 }, toRanges.get(1));
830 assertArrayEquals(new int[] { 74, 75 }, toRanges.get(2));
831 assertArrayEquals(new int[] { 90, 90 }, toRanges.get(3));
834 * 1:1 over 1:1 backwards ('reverse strand')
836 ml1 = new MapList(new int[] { 1, 50 }, new int[] { 70, 119 }, 1, 1);
837 ml2 = new MapList(new int[] { 1, 500 },
839 { 1000, 901, 600, 201 }, 1, 1);
840 compound = ml1.traverse(ml2);
842 assertEquals(1, compound.getFromRatio());
843 assertEquals(1, compound.getToRatio());
844 fromRanges = compound.getFromRanges();
845 assertEquals(1, fromRanges.size());
846 assertArrayEquals(new int[] { 1, 50 }, fromRanges.get(0));
847 toRanges = compound.getToRanges();
848 assertEquals(2, toRanges.size());
849 assertArrayEquals(new int[] { 931, 901 }, toRanges.get(0));
850 assertArrayEquals(new int[] { 600, 582 }, toRanges.get(1));
853 * 1:1 plus 1:3 should result in 1:3
855 ml1 = new MapList(new int[] { 1, 30 }, new int[] { 11, 40 }, 1, 1);
856 ml2 = new MapList(new int[] { 1, 100 }, new int[] { 1, 50, 91, 340 }, 1,
858 compound = ml1.traverse(ml2);
860 assertEquals(1, compound.getFromRatio());
861 assertEquals(3, compound.getToRatio());
862 fromRanges = compound.getFromRanges();
863 assertEquals(1, fromRanges.size());
864 assertArrayEquals(new int[] { 1, 30 }, fromRanges.get(0));
865 // 11-40 maps to 31-50,91-160
866 toRanges = compound.getToRanges();
867 assertEquals(2, toRanges.size());
868 assertArrayEquals(new int[] { 31, 50 }, toRanges.get(0));
869 assertArrayEquals(new int[] { 91, 160 }, toRanges.get(1));
872 * 3:1 plus 1:1 should result in 3:1
874 ml1 = new MapList(new int[] { 1, 30 }, new int[] { 11, 20 }, 3, 1);
875 ml2 = new MapList(new int[] { 1, 100 }, new int[] { 1, 15, 91, 175 }, 1,
877 compound = ml1.traverse(ml2);
879 assertEquals(3, compound.getFromRatio());
880 assertEquals(1, compound.getToRatio());
881 fromRanges = compound.getFromRanges();
882 assertEquals(1, fromRanges.size());
883 assertArrayEquals(new int[] { 1, 30 }, fromRanges.get(0));
884 // 11-20 maps to 11-15, 91-95
885 toRanges = compound.getToRanges();
886 assertEquals(2, toRanges.size());
887 assertArrayEquals(new int[] { 11, 15 }, toRanges.get(0));
888 assertArrayEquals(new int[] { 91, 95 }, toRanges.get(1));
891 * 1:3 plus 3:1 should result in 1:1
893 ml1 = new MapList(new int[] { 21, 40 }, new int[] { 13, 72 }, 1, 3);
894 ml2 = new MapList(new int[] { 1, 300 }, new int[] { 51, 70, 121, 200 },
896 compound = ml1.traverse(ml2);
898 assertEquals(1, compound.getFromRatio());
899 assertEquals(1, compound.getToRatio());
900 fromRanges = compound.getFromRanges();
901 assertEquals(1, fromRanges.size());
902 assertArrayEquals(new int[] { 21, 40 }, fromRanges.get(0));
903 // 13-72 maps 3:1 to 55-70, 121-124
904 toRanges = compound.getToRanges();
905 assertEquals(2, toRanges.size());
906 assertArrayEquals(new int[] { 55, 70 }, toRanges.get(0));
907 assertArrayEquals(new int[] { 121, 124 }, toRanges.get(1));
910 * 3:1 plus 1:3 should result in 1:1
912 ml1 = new MapList(new int[] { 31, 90 }, new int[] { 13, 32 }, 3, 1);
913 ml2 = new MapList(new int[] { 11, 40 }, new int[] { 41, 50, 71, 150 },
915 compound = ml1.traverse(ml2);
917 assertEquals(1, compound.getFromRatio());
918 assertEquals(1, compound.getToRatio());
919 fromRanges = compound.getFromRanges();
920 assertEquals(1, fromRanges.size());
921 assertArrayEquals(new int[] { 31, 90 }, fromRanges.get(0));
922 // 13-32 maps to 47-50,71-126
923 toRanges = compound.getToRanges();
924 assertEquals(2, toRanges.size());
925 assertArrayEquals(new int[] { 47, 50 }, toRanges.get(0));
926 assertArrayEquals(new int[] { 71, 126 }, toRanges.get(1));
929 * method returns null if not all regions are mapped through
931 ml1 = new MapList(new int[] { 1, 50 }, new int[] { 101, 150 }, 1, 1);
932 ml2 = new MapList(new int[] { 131, 180 }, new int[] { 201, 250 }, 1, 3);
933 compound = ml1.traverse(ml2);
934 assertNull(compound);
938 * Test that method that inspects for the (first) forward or reverse 'to'
939 * range. Single position ranges are ignored.
941 @Test(groups = { "Functional" })
942 public void testIsToForwardsStrand()
944 // [3-9] declares forward strand
945 MapList ml = new MapList(new int[] { 20, 11 },
947 { 2, 2, 3, 9, 12, 11 }, 1, 1);
948 assertTrue(ml.isToForwardStrand());
950 // [11-5] declares reverse strand ([13-14] is ignored)
951 ml = new MapList(new int[] { 20, 11 },
953 { 2, 2, 11, 5, 13, 14 }, 1, 1);
954 assertFalse(ml.isToForwardStrand());
956 // all single position ranges - defaults to forward strand
957 ml = new MapList(new int[] { 3, 1 }, new int[] { 2, 2, 4, 4, 6, 6 }, 1,
959 assertTrue(ml.isToForwardStrand());
963 * Test for mapping with overlapping ranges
965 @Test(groups = { "Functional" })
966 public void testLocateInFrom_withOverlap()
968 int[] codons = new int[] { 1, 12, 12, 17 };
969 int[] protein = new int[] { 1, 6 };
970 MapList ml = new MapList(codons, protein, 3, 1);
971 assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(1, 1)));
972 assertEquals("[4, 6]", Arrays.toString(ml.locateInFrom(2, 2)));
973 assertEquals("[7, 9]", Arrays.toString(ml.locateInFrom(3, 3)));
974 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
975 assertEquals("[12, 14]", Arrays.toString(ml.locateInFrom(5, 5)));
976 assertEquals("[15, 17]", Arrays.toString(ml.locateInFrom(6, 6)));
977 assertEquals("[1, 6]", Arrays.toString(ml.locateInFrom(1, 2)));
978 assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(1, 3)));
979 assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 4)));
980 assertEquals("[1, 12, 12, 14]", Arrays.toString(ml.locateInFrom(1, 5)));
981 assertEquals("[1, 12, 12, 17]", Arrays.toString(ml.locateInFrom(1, 6)));
982 assertEquals("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3)));
983 assertEquals("[7, 12, 12, 17]", Arrays.toString(ml.locateInFrom(3, 6)));
985 assertNull(ml.locateInFrom(0, 0));
986 assertNull(ml.locateInFrom(1, 7));
987 assertNull(ml.locateInFrom(-1, 1));
991 * Test for mapping with overlapping ranges
993 @Test(groups = { "Functional" })
994 public void testLocateInTo_withOverlap()
996 int[] codons = new int[] { 1, 12, 12, 17 };
997 int[] protein = new int[] { 1, 6 };
998 MapList ml = new MapList(codons, protein, 3, 1);
999 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
1000 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(3, 8)));
1001 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(2, 11)));
1002 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
1004 // we want base 12 to map to both of the amino acids it codes for
1005 assertEquals("[4, 5]", Arrays.toString(ml.locateInTo(12, 12)));
1006 assertEquals("[4, 5]", Arrays.toString(ml.locateInTo(11, 12)));
1007 assertEquals("[4, 6]", Arrays.toString(ml.locateInTo(11, 15)));
1008 assertEquals("[6, 6]", Arrays.toString(ml.locateInTo(15, 17)));
1010 assertNull(ml.locateInTo(0, 0));
1011 assertNull(ml.locateInTo(1, 18));
1012 assertNull(ml.locateInTo(-1, 1));
1015 @Test(groups = { "Functional" })
1016 public void testTraverseToPosition()
1018 List<int[]> ranges = new ArrayList<>();
1019 assertNull(MapList.traverseToPosition(ranges, 0));
1021 ranges.add(new int[] { 3, 6 });
1022 assertNull(MapList.traverseToPosition(ranges, 0));