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.BitSet;
34 import java.util.List;
36 import org.testng.annotations.BeforeClass;
37 import org.testng.annotations.Test;
39 import jalview.bin.Cache;
40 import jalview.gui.JvOptionPane;
42 public class MapListTest
44 @BeforeClass(alwaysRun = true)
50 @BeforeClass(alwaysRun = true)
51 public void setUpJvOptionPane()
53 JvOptionPane.setInteractiveMode(false);
54 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
57 @Test(groups = { "Functional" }, enabled = false)
58 public void testSomething()
60 MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 },
63 MapList ml1 = new MapList(new int[] { 1, 3, 17, 4 },
66 MapList ml2 = new MapList(new int[] { 1, 60 }, new int[] { 1, 20 }, 3,
68 // test internal consistency
69 int to[] = new int[51];
71 MapList mldna = new MapList(new int[] { 2, 2, 6, 8, 12, 16 },
74 int[] frm = mldna.locateInFrom(1, 1);
75 testLocateFrom(mldna, 1, 1, new int[] { 2, 2, 6, 7 });
78 * for (int from=1; from<=51; from++) { int[] too=ml.shiftTo(from); int[]
79 * toofrom=ml.shiftFrom(too[0]);
80 * System.out.println("ShiftFrom("+from+")=="+too[0]+" %
81 * "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]); }
85 private static void testLocateFrom(MapList mldna, int i, int j, int[] ks)
87 int[] frm = mldna.locateInFrom(i, j);
88 assertEquals("Failed test locate from " + i + " to " + j,
89 Arrays.toString(frm), Arrays.toString(ks));
93 * test routine. not incremental.
99 private void testMap(MapList ml, int fromS, int fromE)
101 // todo convert to JUnit style tests
102 for (int from = 1; from <= 25; from++)
104 int[] too = ml.shiftFrom(from);
105 System.out.print("ShiftFrom(" + from + ")==");
108 System.out.print("NaN\n");
112 System.out.print(too[0] + " % " + too[1] + " (" + too[2] + ")");
113 System.out.print("\t+--+\t");
114 int[] toofrom = ml.shiftTo(too[0]);
117 if (toofrom[0] != from)
119 System.err.println("Mapping not reflexive:" + from + " "
120 + too[0] + "->" + toofrom[0]);
122 System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0]
123 + " % " + toofrom[1] + " (" + toofrom[2] + ")");
127 System.out.println("ShiftTo(" + too[0] + ")=="
128 + "NaN! - not Bijective Mapping!");
132 int mmap[][] = ml.makeFromMap();
133 System.out.println("FromMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
134 + mmap[0][2] + " " + mmap[0][3] + " ");
135 for (int i = 1; i <= mmap[1].length; i++)
137 if (mmap[1][i - 1] == -1)
139 System.out.print(i + "=XXX");
144 System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
148 System.out.print("\n");
152 System.out.print(",");
155 // test range function
156 System.out.print("\nTest locateInFrom\n");
158 int f = mmap[0][2], t = mmap[0][3];
161 System.out.println("Range " + f + " to " + t);
162 int rng[] = ml.locateInFrom(f, t);
165 for (int i = 0; i < rng.length; i++)
167 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
172 System.out.println("No range!");
174 System.out.print("\nReversed\n");
175 rng = ml.locateInFrom(t, f);
178 for (int i = 0; i < rng.length; i++)
180 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
185 System.out.println("No range!");
187 System.out.print("\n");
192 System.out.print("\n");
193 mmap = ml.makeToMap();
194 System.out.println("ToMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
195 + mmap[0][2] + " " + mmap[0][3] + " ");
196 for (int i = 1; i <= mmap[1].length; i++)
198 if (mmap[1][i - 1] == -1)
200 System.out.print(i + "=XXX");
205 System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
209 System.out.print("\n");
213 System.out.print(",");
216 System.out.print("\n");
217 // test range function
218 System.out.print("\nTest locateInTo\n");
220 int f = mmap[0][2], t = mmap[0][3];
223 System.out.println("Range " + f + " to " + t);
224 int rng[] = ml.locateInTo(f, t);
227 for (int i = 0; i < rng.length; i++)
229 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
234 System.out.println("No range!");
236 System.out.print("\nReversed\n");
237 rng = ml.locateInTo(t, f);
240 for (int i = 0; i < rng.length; i++)
242 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
247 System.out.println("No range!");
251 System.out.print("\n");
257 * Tests for method that locates ranges in the 'from' map for given range in
260 @Test(groups = { "Functional" })
261 public void testLocateInFrom_noIntrons()
264 * Simple mapping with no introns
266 int[] codons = new int[] { 1, 12 };
267 int[] protein = new int[] { 1, 4 };
268 MapList ml = new MapList(codons, protein, 3, 1);
269 assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(1, 1)));
270 assertEquals("[4, 6]", Arrays.toString(ml.locateInFrom(2, 2)));
271 assertEquals("[7, 9]", Arrays.toString(ml.locateInFrom(3, 3)));
272 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
273 assertEquals("[1, 6]", Arrays.toString(ml.locateInFrom(1, 2)));
274 assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(1, 3)));
275 // reversed range treated as if forwards:
276 assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(3, 1)));
277 assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 4)));
278 assertEquals("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3)));
279 assertEquals("[4, 12]", Arrays.toString(ml.locateInFrom(2, 4)));
280 assertEquals("[7, 12]", Arrays.toString(ml.locateInFrom(3, 4)));
281 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
286 assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 5)));
287 assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(-1, 1)));
292 assertNull(ml.locateInFrom(0, 0));
297 * Tests for method that locates ranges in the 'from' map for given range in
300 @Test(groups = { "Functional" })
301 public void testLocateInFrom_withIntrons()
304 * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
305 * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
307 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
308 int[] protein = { 1, 4 };
309 MapList ml = new MapList(codons, protein, 3, 1);
310 assertEquals("[2, 3, 5, 5]", Arrays.toString(ml.locateInFrom(1, 1)));
311 assertEquals("[6, 7, 9, 9]", Arrays.toString(ml.locateInFrom(2, 2)));
312 assertEquals("[10, 10, 12, 12, 14, 14]",
313 Arrays.toString(ml.locateInFrom(3, 3)));
314 assertEquals("[16, 18]", Arrays.toString(ml.locateInFrom(4, 4)));
317 * codons at 11-16, 21-26, 31-36 mapped to peptide positions 1, 3-4, 6-8
319 ml = new MapList(new int[] { 11, 16, 21, 26, 31, 36 },
321 { 1, 1, 3, 4, 6, 8 }, 3, 1);
322 assertArrayEquals(new int[] { 11, 13 }, ml.locateInFrom(1, 1));
323 assertArrayEquals(new int[] { 11, 16 }, ml.locateInFrom(1, 3));
324 assertArrayEquals(new int[] { 11, 16, 21, 23 }, ml.locateInFrom(1, 4));
325 assertArrayEquals(new int[] { 14, 16, 21, 23 }, ml.locateInFrom(3, 4));
329 @Test(groups = { "Functional" })
330 public void testLocateInFrom_reverseStrand()
332 int[] codons = new int[] { 12, 1 };
333 int[] protein = new int[] { 1, 4 };
334 MapList ml = new MapList(codons, protein, 3, 1);
335 assertEquals("[12, 10]", Arrays.toString(ml.locateInFrom(1, 1)));
336 assertEquals("[9, 4]", Arrays.toString(ml.locateInFrom(2, 3)));
340 * Tests for method that locates ranges in the 'to' map for given range in the
343 @Test(groups = { "Functional" })
344 public void testLocateInTo_noIntrons()
347 * Simple mapping with no introns
349 int[] codons = new int[] { 1, 12 };
350 int[] protein = new int[] { 1, 4 };
351 MapList ml = new MapList(codons, protein, 3, 1);
352 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 3)));
353 assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
354 assertEquals("[3, 3]", Arrays.toString(ml.locateInTo(7, 9)));
355 assertEquals("[4, 4]", Arrays.toString(ml.locateInTo(10, 12)));
356 assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 6)));
357 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(1, 9)));
358 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(1, 12)));
359 assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
360 assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(4, 12)));
361 // reverse range treated as if forwards:
362 assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(12, 4)));
365 * A part codon is treated as if a whole one.
367 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
368 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 2)));
369 assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 4)));
370 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(2, 8)));
371 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
372 assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(5, 11)));
377 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(1, 13)));
378 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(-1, 2)));
383 assertNull(ml.locateInTo(0, 0));
387 * Tests for method that locates ranges in the 'to' map for given range in the
390 @Test(groups = { "Functional" })
391 public void testLocateInTo_withIntrons()
394 * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
395 * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
397 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
399 * Mapped proteins at positions 1, 3, 4, 6 in the sequence
401 int[] protein = { 1, 1, 3, 4, 6, 6 };
402 MapList ml = new MapList(codons, protein, 3, 1);
405 * Valid range or subrange of codon1 maps to protein1
407 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 2)));
408 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 3)));
409 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 5)));
410 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 3)));
411 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 5)));
413 // codon position 6 starts the next protein:
414 assertEquals("[1, 1, 3, 3]", Arrays.toString(ml.locateInTo(3, 6)));
416 // codon positions 7 to 17 (part) cover proteins 2/3/4 at positions 3/4/6
417 assertEquals("[3, 4, 6, 6]", Arrays.toString(ml.locateInTo(7, 17)));
422 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 2)));
423 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 4)));
424 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 4)));
429 assertNull(ml.locateInTo(4, 4));
433 * Test equals method.
435 @Test(groups = { "Functional" })
436 public void testEquals()
438 int[] codons = new int[] { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
439 int[] protein = new int[] { 1, 4 };
440 MapList ml = new MapList(codons, protein, 3, 1);
441 MapList ml1 = new MapList(codons, protein, 3, 1); // same values
442 MapList ml2 = new MapList(codons, protein, 2, 1); // fromRatio differs
443 MapList ml3 = new MapList(codons, protein, 3, 2); // toRatio differs
445 MapList ml6 = new MapList(codons, protein, 3, 1); // fromShifts differ
447 MapList ml7 = new MapList(codons, protein, 3, 1); // toShifts differ
449 assertTrue(ml.equals(ml));
450 assertEquals(ml.hashCode(), ml.hashCode());
451 assertTrue(ml.equals(ml1));
452 assertEquals(ml.hashCode(), ml1.hashCode());
453 assertTrue(ml1.equals(ml));
455 assertFalse(ml.equals(null));
456 assertFalse(ml.equals("hello"));
457 assertFalse(ml.equals(ml2));
458 assertFalse(ml.equals(ml3));
459 assertFalse(ml.equals(ml6));
460 assertFalse(ml.equals(ml7));
461 assertFalse(ml6.equals(ml7));
465 MapList ml4 = new MapList(codons, null, 3, 1); // toShifts null
466 assertFalse(ml.equals(ml4));
467 } catch (NullPointerException e)
469 // actually thrown by constructor before equals can be called
473 MapList ml5 = new MapList(null, protein, 3, 1); // fromShifts null
474 assertFalse(ml.equals(ml5));
475 } catch (NullPointerException e)
477 // actually thrown by constructor before equals can be called
482 * Test for the method that flattens a list of ranges into a single array.
484 @Test(groups = { "Functional" })
485 public void testGetRanges()
487 List<int[]> ranges = new ArrayList<>();
488 ranges.add(new int[] { 2, 3 });
489 ranges.add(new int[] { 5, 6 });
490 assertEquals("[2, 3, 5, 6]",
491 Arrays.toString(MapList.getRanges(ranges)));
495 * Check state after construction
497 @Test(groups = { "Functional" })
498 public void testConstructor()
500 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
501 int[] protein = { 1, 1, 3, 4, 6, 6 };
502 MapList ml = new MapList(codons, protein, 3, 1);
503 assertEquals(3, ml.getFromRatio());
504 assertEquals(2, ml.getFromLowest());
505 assertEquals(18, ml.getFromHighest());
506 assertEquals(1, ml.getToLowest());
507 assertEquals(6, ml.getToHighest());
508 assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
509 prettyPrint(ml.getFromRanges()));
510 assertEquals("{[1, 1], [3, 4], [6, 6]}", prettyPrint(ml.getToRanges()));
513 * Also copy constructor
515 MapList ml2 = new MapList(ml);
516 assertEquals(3, ml2.getFromRatio());
517 assertEquals(2, ml2.getFromLowest());
518 assertEquals(18, ml2.getFromHighest());
519 assertEquals(1, ml2.getToLowest());
520 assertEquals(6, ml2.getToHighest());
521 assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
522 prettyPrint(ml2.getFromRanges()));
523 assertEquals("{[1, 1], [3, 4], [6, 6]}",
524 prettyPrint(ml2.getToRanges()));
529 codons = new int[] { 9, 6 };
530 protein = new int[] { 100, 91, 80, 79 };
531 ml = new MapList(codons, protein, 3, 1);
532 assertEquals(6, ml.getFromLowest());
533 assertEquals(9, ml.getFromHighest());
534 assertEquals(79, ml.getToLowest());
535 assertEquals(100, ml.getToHighest());
539 * Test constructor used to merge consecutive ranges but now just leaves them
540 * as supplied (JAL-3751)
542 @Test(groups = { "Functional" })
543 public void testConstructor_mergeRanges()
545 int[] codons = { 2, 3, 3, 7, 9, 10, 12, 12, 13, 14, 16, 17 };
546 int[] protein = { 1, 1, 2, 3, 6, 6 };
547 MapList ml = new MapList(codons, protein, 3, 1);
548 assertEquals(3, ml.getFromRatio());
549 assertEquals(2, ml.getFromLowest());
550 assertEquals(17, ml.getFromHighest());
551 assertEquals(1, ml.getToLowest());
552 assertEquals(6, ml.getToHighest());
553 assertEquals("{[2, 3], [3, 7], [9, 10], [12, 12], [13, 14], [16, 17]}",
554 prettyPrint(ml.getFromRanges()));
555 assertEquals("{[1, 1], [2, 3], [6, 6]}", prettyPrint(ml.getToRanges()));
559 * Convert a List of {[i, j], [k, l], ...} to "[[i, j], [k, l], ...]"
564 private String prettyPrint(List<int[]> ranges)
566 StringBuilder sb = new StringBuilder(ranges.size() * 5);
567 boolean first = true;
569 for (int[] range : ranges)
575 sb.append(Arrays.toString(range));
579 return sb.toString();
583 * Test the method that creates an inverse mapping
585 @Test(groups = { "Functional" })
586 public void testGetInverse()
588 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
589 int[] protein = { 1, 1, 3, 4, 6, 6 };
591 MapList ml = new MapList(codons, protein, 3, 1);
592 MapList ml2 = ml.getInverse();
593 assertEquals(ml.getFromRatio(), ml2.getToRatio());
594 assertEquals(ml.getFromRatio(), ml2.getToRatio());
595 assertEquals(ml.getToHighest(), ml2.getFromHighest());
596 assertEquals(ml.getFromHighest(), ml2.getToHighest());
597 assertEquals(prettyPrint(ml.getFromRanges()),
598 prettyPrint(ml2.getToRanges()));
599 assertEquals(prettyPrint(ml.getToRanges()),
600 prettyPrint(ml2.getFromRanges()));
603 @Test(groups = { "Functional" })
604 public void testToString()
606 MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 },
609 String s = ml.toString();
610 assertEquals("[ [1, 5] [10, 15] [25, 20] ] 1:3 to [ [51, 1] ]", s);
613 @Test(groups = { "Functional" })
614 public void testAddMapList()
616 MapList ml = new MapList(new int[] { 11, 15, 20, 25, 35, 30 },
619 assertEquals(11, ml.getFromLowest());
620 assertEquals(35, ml.getFromHighest());
621 assertEquals(22, ml.getToLowest());
622 assertEquals(72, ml.getToHighest());
624 MapList ml2 = new MapList(new int[] { 2, 4, 37, 40 },
626 { 12, 17, 78, 83, 88, 96 }, 1, 3);
628 assertEquals(2, ml.getFromLowest());
629 assertEquals(40, ml.getFromHighest());
630 assertEquals(12, ml.getToLowest());
631 assertEquals(96, ml.getToHighest());
633 String s = ml.toString();
635 "[ [11, 15] [20, 25] [35, 30] [2, 4] [37, 40] ] 1:3 to [ [72, 22] [12, 17] [78, 83] [88, 96] ]",
640 * Test that confirms adding a map twice does nothing
642 @Test(groups = { "Functional" })
643 public void testAddMapList_sameMap()
645 MapList ml = new MapList(new int[] { 11, 15, 20, 25, 35, 30 },
648 String before = ml.toString();
650 assertEquals(before, ml.toString());
651 ml.addMapList(new MapList(ml));
652 assertEquals(before, ml.toString());
655 @Test(groups = { "Functional" })
656 public void testAddMapList_contiguous()
658 MapList ml = new MapList(new int[] { 11, 15 }, new int[] { 72, 58 }, 1,
661 MapList ml2 = new MapList(new int[] { 15, 16 }, new int[] { 58, 53 }, 1,
664 assertEquals("[ [11, 16] ] 1:3 to [ [72, 53] ]", ml.toString());
667 @Test(groups = "Functional")
668 public void testAddRange()
670 int[] range = { 1, 5 };
671 List<int[]> ranges = new ArrayList<>();
673 // add to empty list:
674 MapList.addRange(range, ranges);
675 assertEquals(1, ranges.size());
676 assertSame(range, ranges.get(0));
678 // extend contiguous (same position):
679 MapList.addRange(new int[] { 5, 10 }, ranges);
680 assertEquals(1, ranges.size());
681 assertEquals(1, ranges.get(0)[0]);
682 assertEquals(10, ranges.get(0)[1]);
684 // extend contiguous (next position):
685 MapList.addRange(new int[] { 11, 15 }, ranges);
686 assertEquals(1, ranges.size());
687 assertEquals(1, ranges.get(0)[0]);
688 assertEquals(15, ranges.get(0)[1]);
690 // change direction: range is not merged:
691 MapList.addRange(new int[] { 16, 10 }, ranges);
692 assertEquals(2, ranges.size());
693 assertEquals(16, ranges.get(1)[0]);
694 assertEquals(10, ranges.get(1)[1]);
696 // extend reverse contiguous (same position):
697 MapList.addRange(new int[] { 10, 8 }, ranges);
698 assertEquals(2, ranges.size());
699 assertEquals(16, ranges.get(1)[0]);
700 assertEquals(8, ranges.get(1)[1]);
702 // extend reverse contiguous (next position):
703 MapList.addRange(new int[] { 7, 6 }, ranges);
704 assertEquals(2, ranges.size());
705 assertEquals(16, ranges.get(1)[0]);
706 assertEquals(6, ranges.get(1)[1]);
708 // change direction: range is not merged:
709 MapList.addRange(new int[] { 6, 9 }, ranges);
710 assertEquals(3, ranges.size());
711 assertEquals(6, ranges.get(2)[0]);
712 assertEquals(9, ranges.get(2)[1]);
714 // not contiguous: not merged
715 MapList.addRange(new int[] { 11, 12 }, ranges);
716 assertEquals(4, ranges.size());
717 assertEquals(11, ranges.get(3)[0]);
718 assertEquals(12, ranges.get(3)[1]);
722 * Check state after construction
724 @Test(groups = { "Functional" })
725 public void testConstructor_withLists()
730 int[][] codons = new int[][] { { 9, 6 } };
731 int[][] protein = new int[][] { { 100, 91 }, { 80, 79 } };
732 MapList ml = new MapList(Arrays.asList(codons), Arrays.asList(protein),
734 assertEquals(6, ml.getFromLowest());
735 assertEquals(9, ml.getFromHighest());
736 assertEquals(79, ml.getToLowest());
737 assertEquals(100, ml.getToHighest());
741 * Test that method that inspects for the (first) forward or reverse from
742 * range. Single position ranges are ignored.
744 @Test(groups = { "Functional" })
745 public void testIsFromForwardStrand()
747 // [3-9] declares forward strand
748 MapList ml = new MapList(new int[] { 2, 2, 3, 9, 12, 11 },
751 assertTrue(ml.isFromForwardStrand());
753 // [11-5] declares reverse strand ([13-14] is ignored)
754 ml = new MapList(new int[] { 2, 2, 11, 5, 13, 14 },
757 assertFalse(ml.isFromForwardStrand());
759 // all single position ranges - defaults to forward strand
760 ml = new MapList(new int[] { 2, 2, 4, 4, 6, 6 }, new int[] { 3, 1 }, 1,
762 assertTrue(ml.isFromForwardStrand());
766 * Test the method that merges contiguous ranges
768 @Test(groups = { "Functional" })
769 public void testCoalesceRanges()
771 assertNull(MapList.coalesceRanges(null));
772 List<int[]> ranges = new ArrayList<>();
773 assertSame(ranges, MapList.coalesceRanges(ranges));
774 ranges.add(new int[] { 1, 3 });
775 assertSame(ranges, MapList.coalesceRanges(ranges));
777 // add non-contiguous range:
778 ranges.add(new int[] { 5, 6 });
779 assertSame(ranges, MapList.coalesceRanges(ranges));
781 // 'contiguous' range in opposite direction is not merged:
782 ranges.add(new int[] { 7, 6 });
783 assertSame(ranges, MapList.coalesceRanges(ranges));
785 // merging in forward direction:
787 ranges.add(new int[] { 1, 3 });
788 ranges.add(new int[] { 4, 5 }); // contiguous
789 ranges.add(new int[] { 5, 5 }); // overlap!
790 ranges.add(new int[] { 6, 7 }); // contiguous
791 List<int[]> merged = MapList.coalesceRanges(ranges);
792 assertEquals(2, merged.size());
793 assertArrayEquals(new int[] { 1, 5 }, merged.get(0));
794 assertArrayEquals(new int[] { 5, 7 }, merged.get(1));
795 // verify input list is unchanged
796 assertEquals(4, ranges.size());
797 assertArrayEquals(new int[] { 1, 3 }, ranges.get(0));
798 assertArrayEquals(new int[] { 4, 5 }, ranges.get(1));
799 assertArrayEquals(new int[] { 5, 5 }, ranges.get(2));
800 assertArrayEquals(new int[] { 6, 7 }, ranges.get(3));
802 // merging in reverse direction:
804 ranges.add(new int[] { 7, 5 });
805 ranges.add(new int[] { 5, 4 }); // overlap
806 ranges.add(new int[] { 4, 4 }); // overlap
807 ranges.add(new int[] { 3, 1 }); // contiguous
808 merged = MapList.coalesceRanges(ranges);
809 assertEquals(3, merged.size());
810 assertArrayEquals(new int[] { 7, 5 }, merged.get(0));
811 assertArrayEquals(new int[] { 5, 4 }, merged.get(1));
812 assertArrayEquals(new int[] { 4, 1 }, merged.get(2));
814 // merging with switches of direction:
816 ranges.add(new int[] { 1, 3 });
817 ranges.add(new int[] { 4, 5 }); // contiguous
818 ranges.add(new int[] { 5, 5 }); // overlap
819 ranges.add(new int[] { 6, 6 }); // contiguous
820 ranges.add(new int[] { 12, 10 });
821 ranges.add(new int[] { 9, 8 }); // contiguous
822 ranges.add(new int[] { 8, 8 }); // overlap
823 ranges.add(new int[] { 7, 7 }); // contiguous
824 merged = MapList.coalesceRanges(ranges);
825 assertEquals(4, merged.size());
826 assertArrayEquals(new int[] { 1, 5 }, merged.get(0));
827 assertArrayEquals(new int[] { 5, 6 }, merged.get(1));
828 assertArrayEquals(new int[] { 12, 8 }, merged.get(2));
829 assertArrayEquals(new int[] { 8, 7 }, merged.get(3));
831 // 'subsumed' ranges are preserved
833 ranges.add(new int[] { 10, 30 });
834 ranges.add(new int[] { 15, 25 });
835 merged = MapList.coalesceRanges(ranges);
836 assertEquals(2, merged.size());
837 assertArrayEquals(new int[] { 10, 30 }, merged.get(0));
838 assertArrayEquals(new int[] { 15, 25 }, merged.get(1));
842 * Test the method that compounds ('traverses') two mappings
844 @Test(groups = "Functional")
845 public void testTraverse()
848 * simple 1:1 plus 1:1 forwards
850 MapList ml1 = new MapList(new int[] { 3, 4, 8, 12 },
852 { 5, 8, 11, 13 }, 1, 1);
853 assertEquals("{[3, 4], [8, 12]}", prettyPrint(ml1.getFromRanges()));
854 assertEquals("{[5, 8], [11, 13]}", prettyPrint(ml1.getToRanges()));
856 MapList ml2 = new MapList(new int[] { 1, 50 },
858 { 40, 45, 70, 75, 90, 127 }, 1, 1);
859 assertEquals("{[1, 50]}", prettyPrint(ml2.getFromRanges()));
860 assertEquals("{[40, 45], [70, 75], [90, 127]}",
861 prettyPrint(ml2.getToRanges()));
863 MapList compound = ml1.traverse(ml2);
865 assertEquals(1, compound.getFromRatio());
866 assertEquals(1, compound.getToRatio());
867 List<int[]> fromRanges = compound.getFromRanges();
868 assertEquals(2, fromRanges.size());
869 assertArrayEquals(new int[] { 3, 4 }, fromRanges.get(0));
870 assertArrayEquals(new int[] { 8, 12 }, fromRanges.get(1));
871 List<int[]> toRanges = compound.getToRanges();
872 assertEquals(4, toRanges.size());
873 // 5-8 maps to 44-45,70-71
874 // 11-13 maps to 74-75,90
875 assertArrayEquals(new int[] { 44, 45 }, toRanges.get(0));
876 assertArrayEquals(new int[] { 70, 71 }, toRanges.get(1));
877 assertArrayEquals(new int[] { 74, 75 }, toRanges.get(2));
878 assertArrayEquals(new int[] { 90, 90 }, toRanges.get(3));
881 * 1:1 over 1:1 backwards ('reverse strand')
883 ml1 = new MapList(new int[] { 1, 50 }, new int[] { 70, 119 }, 1, 1);
884 ml2 = new MapList(new int[] { 1, 500 },
886 { 1000, 901, 600, 201 }, 1, 1);
887 compound = ml1.traverse(ml2);
889 assertEquals(1, compound.getFromRatio());
890 assertEquals(1, compound.getToRatio());
891 fromRanges = compound.getFromRanges();
892 assertEquals(1, fromRanges.size());
893 assertArrayEquals(new int[] { 1, 50 }, fromRanges.get(0));
894 toRanges = compound.getToRanges();
895 assertEquals(2, toRanges.size());
896 assertArrayEquals(new int[] { 931, 901 }, toRanges.get(0));
897 assertArrayEquals(new int[] { 600, 582}, toRanges.get(1));
900 * 1:1 plus 1:3 should result in 1:3
902 ml1 = new MapList(new int[] { 1, 30 }, new int[] { 11, 40 }, 1, 1);
903 ml2 = new MapList(new int[] { 1, 100 }, new int[] { 1, 50, 91, 340 }, 1,
905 compound = ml1.traverse(ml2);
907 assertEquals(1, compound.getFromRatio());
908 assertEquals(3, compound.getToRatio());
909 fromRanges = compound.getFromRanges();
910 assertEquals(1, fromRanges.size());
911 assertArrayEquals(new int[] { 1, 30 }, fromRanges.get(0));
912 // 11-40 maps to 31-50,91-160
913 toRanges = compound.getToRanges();
914 assertEquals(2, toRanges.size());
915 assertArrayEquals(new int[] { 31, 50 }, toRanges.get(0));
916 assertArrayEquals(new int[] { 91, 160 }, toRanges.get(1));
919 * 3:1 plus 1:1 should result in 3:1
921 ml1 = new MapList(new int[] { 1, 30 }, new int[] { 11, 20 }, 3, 1);
922 ml2 = new MapList(new int[] { 1, 100 }, new int[] { 1, 15, 91, 175 }, 1,
924 compound = ml1.traverse(ml2);
926 assertEquals(3, compound.getFromRatio());
927 assertEquals(1, compound.getToRatio());
928 fromRanges = compound.getFromRanges();
929 assertEquals(1, fromRanges.size());
930 assertArrayEquals(new int[] { 1, 30 }, fromRanges.get(0));
931 // 11-20 maps to 11-15, 91-95
932 toRanges = compound.getToRanges();
933 assertEquals(2, toRanges.size());
934 assertArrayEquals(new int[] { 11, 15 }, toRanges.get(0));
935 assertArrayEquals(new int[] { 91, 95 }, toRanges.get(1));
938 * 1:3 plus 3:1 should result in 1:1
940 ml1 = new MapList(new int[] { 21, 40 }, new int[] { 13, 72 }, 1, 3);
941 ml2 = new MapList(new int[] { 1, 300 }, new int[] { 51, 70, 121, 200 },
943 compound = ml1.traverse(ml2);
945 assertEquals(1, compound.getFromRatio());
946 assertEquals(1, compound.getToRatio());
947 fromRanges = compound.getFromRanges();
948 assertEquals(1, fromRanges.size());
949 assertArrayEquals(new int[] { 21, 40 }, fromRanges.get(0));
950 // 13-72 maps 3:1 to 55-70, 121-124
951 toRanges = compound.getToRanges();
952 assertEquals(2, toRanges.size());
953 assertArrayEquals(new int[] { 55, 70 }, toRanges.get(0));
954 assertArrayEquals(new int[] { 121, 124 }, toRanges.get(1));
957 * 3:1 plus 1:3 should result in 1:1
959 ml1 = new MapList(new int[] { 31, 90 }, new int[] { 13, 32 }, 3, 1);
960 ml2 = new MapList(new int[] { 11, 40 }, new int[] { 41, 50, 71, 150 },
962 compound = ml1.traverse(ml2);
964 assertEquals(1, compound.getFromRatio());
965 assertEquals(1, compound.getToRatio());
966 fromRanges = compound.getFromRanges();
967 assertEquals(1, fromRanges.size());
968 assertArrayEquals(new int[] { 31, 90 }, fromRanges.get(0));
969 // 13-32 maps to 47-50,71-126
970 toRanges = compound.getToRanges();
971 assertEquals(2, toRanges.size());
972 assertArrayEquals(new int[] { 47, 50 }, toRanges.get(0));
973 assertArrayEquals(new int[] { 71, 126 }, toRanges.get(1));
976 * if not all regions are mapped through, returns what is
978 ml1 = new MapList(new int[] { 1, 50 }, new int[] { 101, 150 }, 1, 1);
979 ml2 = new MapList(new int[] { 131, 180 }, new int[] { 201, 250 }, 1, 1);
980 compound = ml1.traverse(ml2);
981 assertNull(compound);
985 * Test that method that inspects for the (first) forward or reverse 'to'
986 * range. Single position ranges are ignored.
988 @Test(groups = { "Functional" })
989 public void testIsToForwardsStrand()
991 // [3-9] declares forward strand
992 MapList ml = new MapList(new int[] { 20, 11 },
994 { 2, 2, 3, 9, 12, 11 }, 1, 1);
995 assertTrue(ml.isToForwardStrand());
997 // [11-5] declares reverse strand ([13-14] is ignored)
998 ml = new MapList(new int[] { 20, 11 },
1000 { 2, 2, 11, 5, 13, 14 }, 1, 1);
1001 assertFalse(ml.isToForwardStrand());
1003 // all single position ranges - defaults to forward strand
1004 ml = new MapList(new int[] { 3, 1 }, new int[] { 2, 2, 4, 4, 6, 6 }, 1,
1006 assertTrue(ml.isToForwardStrand());
1010 * Test for mapping with overlapping ranges
1012 @Test(groups = { "Functional" })
1013 public void testLocateInFrom_withOverlap()
1016 * gene to protein...
1018 int[] codons = new int[] { 1, 12, 12, 17 };
1019 int[] protein = new int[] { 1, 6 };
1020 MapList ml = new MapList(codons, protein, 3, 1);
1021 assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(1, 1)));
1022 assertEquals("[4, 6]", Arrays.toString(ml.locateInFrom(2, 2)));
1023 assertEquals("[7, 9]", Arrays.toString(ml.locateInFrom(3, 3)));
1024 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
1025 assertEquals("[12, 14]", Arrays.toString(ml.locateInFrom(5, 5)));
1026 assertEquals("[15, 17]", Arrays.toString(ml.locateInFrom(6, 6)));
1027 assertEquals("[1, 6]", Arrays.toString(ml.locateInFrom(1, 2)));
1028 assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(1, 3)));
1029 assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 4)));
1030 assertEquals("[1, 12, 12, 14]", Arrays.toString(ml.locateInFrom(1, 5)));
1031 assertEquals("[1, 12, 12, 17]", Arrays.toString(ml.locateInFrom(1, 6)));
1032 assertEquals("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3)));
1033 assertEquals("[7, 12, 12, 17]", Arrays.toString(ml.locateInFrom(3, 6)));
1036 * partial overlap of range
1038 assertEquals("[4, 12, 12, 17]", Arrays.toString(ml.locateInFrom(2, 7)));
1039 assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(-1, 1)));
1042 * no overlap in range
1044 assertNull(ml.locateInFrom(0, 0));
1047 * gene to CDS...from EMBL:MN908947
1049 int[] gene = new int[] { 266, 13468, 13468, 21555 };
1050 int[] cds = new int[] { 1, 21291 };
1051 ml = new MapList(gene, cds, 1, 1);
1052 assertEquals("[13468, 13468]",
1053 Arrays.toString(ml.locateInFrom(13203, 13203)));
1054 assertEquals("[13468, 13468]",
1055 Arrays.toString(ml.locateInFrom(13204, 13204)));
1056 assertEquals("[13468, 13468, 13468, 13468]",
1057 Arrays.toString(ml.locateInFrom(13203, 13204)));
1061 * Test for mapping with overlapping ranges
1063 @Test(groups = { "Functional" })
1064 public void testLocateInTo_withOverlap()
1067 * gene to protein...
1069 int[] codons = new int[] { 1, 12, 12, 17 };
1070 int[] protein = new int[] { 1, 6 };
1071 MapList ml = new MapList(codons, protein, 3, 1);
1072 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
1073 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(3, 8)));
1074 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(2, 11)));
1075 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
1077 // we want base 12 to map to both of the amino acids it codes for
1078 assertEquals("[4, 5]", Arrays.toString(ml.locateInTo(12, 12)));
1079 assertEquals("[4, 5]", Arrays.toString(ml.locateInTo(11, 12)));
1080 assertEquals("[4, 6]", Arrays.toString(ml.locateInTo(11, 15)));
1081 assertEquals("[6, 6]", Arrays.toString(ml.locateInTo(15, 17)));
1086 assertNull(ml.locateInTo(0, 0));
1091 assertEquals("[1, 6]", Arrays.toString(ml.locateInTo(1, 18)));
1092 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(-1, 1)));
1095 * gene to CDS...from EMBL:MN908947
1096 * the base at 13468 is used twice in transcription
1098 int[] gene = new int[] { 266, 13468, 13468, 21555 };
1099 int[] cds = new int[] { 1, 21291 };
1100 ml = new MapList(gene, cds, 1, 1);
1101 assertEquals("[13203, 13204]",
1102 Arrays.toString(ml.locateInTo(13468, 13468)));
1106 * the base at 13468 is in the codon for 4401N and also 4402R
1108 gene = new int[] { 266, 13468, 13468, 21552 }; // stop codon excluded
1109 protein = new int[] { 1, 7096 };
1110 ml = new MapList(gene, protein, 3, 1);
1111 assertEquals("[4401, 4402]",
1112 Arrays.toString(ml.locateInTo(13468, 13468)));
1115 @Test(groups = { "Functional" })
1116 public void testTraverseToPosition()
1118 List<int[]> ranges = new ArrayList<>();
1119 assertNull(MapList.traverseToPosition(ranges, 0));
1121 ranges.add(new int[] { 3, 6 });
1122 assertNull(MapList.traverseToPosition(ranges, 0));
1125 @Test(groups = { "Functional" })
1126 public void testCountPositions()
1130 MapList.countPositions(null, 1);
1131 fail("expected exception");
1132 } catch (NullPointerException e)
1137 List<int[]> intervals = new ArrayList<>();
1138 assertNull(MapList.countPositions(intervals, 1));
1143 intervals.add(new int[] { 10, 20 });
1144 assertNull(MapList.countPositions(intervals, 9));
1145 assertNull(MapList.countPositions(intervals, 21));
1146 assertArrayEquals(new int[] { 1, 1 },
1147 MapList.countPositions(intervals, 10));
1148 assertArrayEquals(new int[] { 6, 1 },
1149 MapList.countPositions(intervals, 15));
1150 assertArrayEquals(new int[] { 11, 1 },
1151 MapList.countPositions(intervals, 20));
1153 intervals.add(new int[] { 25, 25 });
1154 assertArrayEquals(new int[] { 12, 1 },
1155 MapList.countPositions(intervals, 25));
1157 // next interval repeats position 25 - which should be counted twice if
1159 intervals.add(new int[] { 25, 26 });
1160 assertArrayEquals(new int[] { 12, 1 },
1161 MapList.countPositions(intervals, 25));
1162 assertArrayEquals(new int[] { 14, 1 },
1163 MapList.countPositions(intervals, 26));
1169 intervals.add(new int[] { 5, -5 });
1170 assertNull(MapList.countPositions(intervals, 6));
1171 assertNull(MapList.countPositions(intervals, -6));
1172 assertArrayEquals(new int[] { 1, -1 },
1173 MapList.countPositions(intervals, 5));
1174 assertArrayEquals(new int[] { 7, -1 },
1175 MapList.countPositions(intervals, -1));
1176 assertArrayEquals(new int[] { 11, -1 },
1177 MapList.countPositions(intervals, -5));
1180 * reverse then forward
1182 intervals.add(new int[] { 5, 10 });
1183 assertArrayEquals(new int[] { 13, 1 },
1184 MapList.countPositions(intervals, 6));
1187 * reverse then forward then reverse
1189 intervals.add(new int[] { -10, -20 });
1190 assertArrayEquals(new int[] { 20, -1 },
1191 MapList.countPositions(intervals, -12));
1194 * an interval [x, x] is treated as forward
1196 intervals.add(new int[] { 30, 30 });
1197 assertArrayEquals(new int[] { 29, 1 },
1198 MapList.countPositions(intervals, 30));
1201 * it is the first matched occurrence that is returned
1204 intervals.add(new int[] { 1, 2 });
1205 intervals.add(new int[] { 2, 3 });
1206 assertArrayEquals(new int[] { 2, 1 },
1207 MapList.countPositions(intervals, 2));
1208 intervals.add(new int[] { -1, -2 });
1209 intervals.add(new int[] { -2, -3 });
1210 assertArrayEquals(new int[] { 6, -1 },
1211 MapList.countPositions(intervals, -2));
1215 * Tests for helper method that adds any overlap (plus offset) to a set of
1218 @Test(groups = { "Functional" })
1219 public void testAddOffsetPositions()
1221 List<int[]> mapped = new ArrayList<>();
1222 int[] range = new int[] {10, 20};
1223 BitSet offsets = new BitSet();
1225 MapList.addOffsetPositions(mapped, 0, range, offsets);
1226 assertTrue(mapped.isEmpty()); // nothing marked for overlap
1229 MapList.addOffsetPositions(mapped, 0, range, offsets);
1230 assertTrue(mapped.isEmpty()); // no offset 11 in range
1232 offsets.set(4, 6); // this sets bits 4 and 5
1233 MapList.addOffsetPositions(mapped, 0, range, offsets);
1234 assertEquals(1, mapped.size());
1235 assertArrayEquals(new int[] { 14, 15 }, mapped.get(0));
1239 MapList.addOffsetPositions(mapped, 0, range, offsets);
1240 assertEquals(2, mapped.size());
1241 assertArrayEquals(new int[] { 14, 15 }, mapped.get(0));
1242 assertArrayEquals(new int[] { 20, 20 }, mapped.get(1));
1247 range = new int[] { 20, 10 };
1250 MapList.addOffsetPositions(mapped, 0, range, offsets);
1251 assertTrue(mapped.isEmpty()); // nothing marked for overlap
1253 MapList.addOffsetPositions(mapped, 0, range, offsets);
1254 assertTrue(mapped.isEmpty()); // no offset 11 in range
1257 offsets.set(6, 8); // sets bits 6 and 7
1258 MapList.addOffsetPositions(mapped, 0, range, offsets);
1259 assertEquals(3, mapped.size());
1260 assertArrayEquals(new int[] { 20, 20 }, mapped.get(0));
1261 assertArrayEquals(new int[] { 14, 13 }, mapped.get(1));
1262 assertArrayEquals(new int[] { 10, 10 }, mapped.get(2));
1265 @Test(groups = { "Functional" })
1266 public void testGetPositionsForOffsets()
1268 List<int[]> ranges = new ArrayList<>();
1269 BitSet offsets = new BitSet();
1270 List<int[]> mapped = MapList.getPositionsForOffsets(ranges, offsets);
1271 assertTrue(mapped.isEmpty()); // no ranges and no offsets!
1273 offsets.set(5, 1000);
1274 mapped = MapList.getPositionsForOffsets(ranges, offsets);
1275 assertTrue(mapped.isEmpty()); // no ranges
1278 * one range with overlap of offsets
1280 ranges.add(new int[] {15, 25});
1281 mapped = MapList.getPositionsForOffsets(ranges, offsets);
1282 assertEquals(1, mapped.size());
1283 assertArrayEquals(new int[] {20, 25}, mapped.get(0));
1288 ranges.add(new int[] {300, 320});
1289 mapped = MapList.getPositionsForOffsets(ranges, offsets);
1290 assertEquals(2, mapped.size());
1291 assertArrayEquals(new int[] {20, 25}, mapped.get(0));
1292 assertArrayEquals(new int[] {300, 320}, mapped.get(1));
1295 * boundary case - right end of first range overlaps
1299 mapped = MapList.getPositionsForOffsets(ranges, offsets);
1300 assertEquals(1, mapped.size());
1301 assertArrayEquals(new int[] {25, 25}, mapped.get(0));
1304 * boundary case - left end of second range overlaps
1307 mapped = MapList.getPositionsForOffsets(ranges, offsets);
1308 assertEquals(2, mapped.size());
1309 assertArrayEquals(new int[] {25, 25}, mapped.get(0));
1310 assertArrayEquals(new int[] {300, 300}, mapped.get(1));
1313 * offsets into a circular range are reported in
1314 * the order in which they are traversed
1317 ranges.add(new int[] {100, 150});
1318 ranges.add(new int[] {60, 80});
1320 offsets.set(45, 55); // sets bits 45 to 54
1321 mapped = MapList.getPositionsForOffsets(ranges, offsets);
1322 assertEquals(2, mapped.size());
1323 assertArrayEquals(new int[] {145, 150}, mapped.get(0)); // offsets 45-50
1324 assertArrayEquals(new int[] {60, 63}, mapped.get(1)); // offsets 51-54
1327 * reverse range overlap is reported with start < end
1330 ranges.add(new int[] {4321, 4000});
1332 offsets.set(20, 22); // sets bits 20 and 21
1334 mapped = MapList.getPositionsForOffsets(ranges, offsets);
1335 assertEquals(2, mapped.size());
1336 assertArrayEquals(new int[] {4301, 4300}, mapped.get(0));
1337 assertArrayEquals(new int[] {4291, 4291}, mapped.get(1));
1340 @Test(groups = { "Functional" })
1341 public void testGetMappedOffsetsForPositions()
1344 * start by verifying the examples in the method's Javadoc!
1346 List<int[]> ranges = new ArrayList<>();
1347 ranges.add(new int[] {10, 20});
1348 ranges.add(new int[] {31, 40});
1349 BitSet overlaps = MapList.getMappedOffsetsForPositions(1, 9, ranges, 1, 1);
1350 assertTrue(overlaps.isEmpty());
1351 overlaps = MapList.getMappedOffsetsForPositions(1, 11, ranges, 1, 1);
1352 assertEquals(2, overlaps.cardinality());
1353 assertTrue(overlaps.get(0));
1354 assertTrue(overlaps.get(1));
1355 overlaps = MapList.getMappedOffsetsForPositions(15, 35, ranges, 1, 1);
1356 assertEquals(11, overlaps.cardinality());
1357 for (int i = 5 ; i <= 11 ; i++)
1359 assertTrue(overlaps.get(i));
1363 ranges.add(new int[] {1, 200});
1364 overlaps = MapList.getMappedOffsetsForPositions(9, 9, ranges, 1, 3);
1365 assertEquals(3, overlaps.cardinality());
1366 assertTrue(overlaps.get(24));
1367 assertTrue(overlaps.get(25));
1368 assertTrue(overlaps.get(26));
1371 ranges.add(new int[] {101, 150});
1372 ranges.add(new int[] {171, 180});
1373 overlaps = MapList.getMappedOffsetsForPositions(101, 102, ranges, 3, 1);
1374 assertEquals(1, overlaps.cardinality());
1375 assertTrue(overlaps.get(0));
1376 overlaps = MapList.getMappedOffsetsForPositions(150, 171, ranges, 3, 1);
1377 assertEquals(1, overlaps.cardinality());
1378 assertTrue(overlaps.get(16));
1381 ranges.add(new int[] {101, 150});
1382 ranges.add(new int[] {21, 30});
1383 overlaps = MapList.getMappedOffsetsForPositions(24, 40, ranges, 3, 1);
1384 assertEquals(3, overlaps.cardinality());
1385 assertTrue(overlaps.get(17));
1386 assertTrue(overlaps.get(18));
1387 assertTrue(overlaps.get(19));
1390 * reverse range 1:1 (e.g. reverse strand gene to transcript)
1393 ranges.add(new int[] {20, 10});
1394 overlaps = MapList.getMappedOffsetsForPositions(12, 13, ranges, 1, 1);
1395 assertEquals(2, overlaps.cardinality());
1396 assertTrue(overlaps.get(7));
1397 assertTrue(overlaps.get(8));
1400 * reverse range 3:1 (e.g. reverse strand gene to peptide)
1401 * from EMBL:J03321 to P0CE20
1404 ranges.add(new int[] {1480, 488});
1405 overlaps = MapList.getMappedOffsetsForPositions(1460, 1460, ranges, 3, 1);
1406 // 1460 is the end of the 7th codon
1407 assertEquals(1, overlaps.cardinality());
1408 assertTrue(overlaps.get(6));
1409 // add one base (part codon)
1410 overlaps = MapList.getMappedOffsetsForPositions(1459, 1460, ranges, 3, 1);
1411 assertEquals(2, overlaps.cardinality());
1412 assertTrue(overlaps.get(6));
1413 assertTrue(overlaps.get(7));
1414 // add second base (part codon)
1415 overlaps = MapList.getMappedOffsetsForPositions(1458, 1460, ranges, 3, 1);
1416 assertEquals(2, overlaps.cardinality());
1417 assertTrue(overlaps.get(6));
1418 assertTrue(overlaps.get(7));
1419 // add third base (whole codon)
1420 overlaps = MapList.getMappedOffsetsForPositions(1457, 1460, ranges, 3, 1);
1421 assertEquals(2, overlaps.cardinality());
1422 assertTrue(overlaps.get(6));
1423 assertTrue(overlaps.get(7));
1424 // add one more base (part codon)
1425 overlaps = MapList.getMappedOffsetsForPositions(1456, 1460, ranges, 3, 1);
1426 assertEquals(3, overlaps.cardinality());
1427 assertTrue(overlaps.get(6));
1428 assertTrue(overlaps.get(7));
1429 assertTrue(overlaps.get(8));