JAL-3761 debugged and working (but not tidied) locateInFrom2/To2
[jalview.git] / test / jalview / util / MapListTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.util;
22
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;
30
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34
35 import org.testng.annotations.BeforeClass;
36 import org.testng.annotations.Test;
37
38 import jalview.bin.Cache;
39 import jalview.gui.JvOptionPane;
40
41 public class MapListTest
42 {
43   @BeforeClass(alwaysRun = true)
44   public void setUp()
45   {
46     Cache.initLogger();
47   }
48
49   @BeforeClass(alwaysRun = true)
50   public void setUpJvOptionPane()
51   {
52     JvOptionPane.setInteractiveMode(false);
53     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
54   }
55
56   @Test(groups = { "Functional" }, enabled = false)
57   public void testSomething()
58   {
59     MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 },
60             new int[]
61             { 51, 1 }, 1, 3);
62     MapList ml1 = new MapList(new int[] { 1, 3, 17, 4 },
63             new int[]
64             { 51, 1 }, 1, 3);
65     MapList ml2 = new MapList(new int[] { 1, 60 }, new int[] { 1, 20 }, 3,
66             1);
67     // test internal consistency
68     int to[] = new int[51];
69     testMap(ml, 1, 60);
70     MapList mldna = new MapList(new int[] { 2, 2, 6, 8, 12, 16 },
71             new int[]
72             { 1, 3 }, 3, 1);
73     int[] frm = mldna.locateInFrom(1, 1);
74     testLocateFrom(mldna, 1, 1, new int[] { 2, 2, 6, 7 });
75     testMap(mldna, 1, 3);
76     /*
77      * for (int from=1; from<=51; from++) { int[] too=ml.shiftTo(from); int[]
78      * toofrom=ml.shiftFrom(too[0]);
79      * System.out.println("ShiftFrom("+from+")=="+too[0]+" %
80      * "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]); }
81      */
82   }
83
84   private static void testLocateFrom(MapList mldna, int i, int j, int[] ks)
85   {
86     int[] frm = mldna.locateInFrom(i, j);
87     assertEquals("Failed test locate from " + i + " to " + j,
88             Arrays.toString(frm), Arrays.toString(ks));
89   }
90
91   /**
92    * test routine. not incremental.
93    * 
94    * @param ml
95    * @param fromS
96    * @param fromE
97    */
98   private void testMap(MapList ml, int fromS, int fromE)
99   {
100     // todo convert to JUnit style tests
101     for (int from = 1; from <= 25; from++)
102     {
103       int[] too = ml.shiftFrom(from);
104       System.out.print("ShiftFrom(" + from + ")==");
105       if (too == null)
106       {
107         System.out.print("NaN\n");
108       }
109       else
110       {
111         System.out.print(too[0] + " % " + too[1] + " (" + too[2] + ")");
112         System.out.print("\t+--+\t");
113         int[] toofrom = ml.shiftTo(too[0]);
114         if (toofrom != null)
115         {
116           if (toofrom[0] != from)
117           {
118             System.err.println("Mapping not reflexive:" + from + " "
119                     + too[0] + "->" + toofrom[0]);
120           }
121           System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0]
122                   + " % " + toofrom[1] + " (" + toofrom[2] + ")");
123         }
124         else
125         {
126           System.out.println("ShiftTo(" + too[0] + ")=="
127                   + "NaN! - not Bijective Mapping!");
128         }
129       }
130     }
131     int mmap[][] = ml.makeFromMap();
132     System.out.println("FromMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
133             + mmap[0][2] + " " + mmap[0][3] + " ");
134     for (int i = 1; i <= mmap[1].length; i++)
135     {
136       if (mmap[1][i - 1] == -1)
137       {
138         System.out.print(i + "=XXX");
139
140       }
141       else
142       {
143         System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
144       }
145       if (i % 20 == 0)
146       {
147         System.out.print("\n");
148       }
149       else
150       {
151         System.out.print(",");
152       }
153     }
154     // test range function
155     System.out.print("\nTest locateInFrom\n");
156     {
157       int f = mmap[0][2], t = mmap[0][3];
158       while (f <= t)
159       {
160         System.out.println("Range " + f + " to " + t);
161         int rng[] = ml.locateInFrom(f, t);
162         if (rng != null)
163         {
164           for (int i = 0; i < rng.length; i++)
165           {
166             System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
167           }
168         }
169         else
170         {
171           System.out.println("No range!");
172         }
173         System.out.print("\nReversed\n");
174         rng = ml.locateInFrom(t, f);
175         if (rng != null)
176         {
177           for (int i = 0; i < rng.length; i++)
178           {
179             System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
180           }
181         }
182         else
183         {
184           System.out.println("No range!");
185         }
186         System.out.print("\n");
187         f++;
188         t--;
189       }
190     }
191     System.out.print("\n");
192     mmap = ml.makeToMap();
193     System.out.println("ToMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
194             + mmap[0][2] + " " + mmap[0][3] + " ");
195     for (int i = 1; i <= mmap[1].length; i++)
196     {
197       if (mmap[1][i - 1] == -1)
198       {
199         System.out.print(i + "=XXX");
200
201       }
202       else
203       {
204         System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
205       }
206       if (i % 20 == 0)
207       {
208         System.out.print("\n");
209       }
210       else
211       {
212         System.out.print(",");
213       }
214     }
215     System.out.print("\n");
216     // test range function
217     System.out.print("\nTest locateInTo\n");
218     {
219       int f = mmap[0][2], t = mmap[0][3];
220       while (f <= t)
221       {
222         System.out.println("Range " + f + " to " + t);
223         int rng[] = ml.locateInTo(f, t);
224         if (rng != null)
225         {
226           for (int i = 0; i < rng.length; i++)
227           {
228             System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
229           }
230         }
231         else
232         {
233           System.out.println("No range!");
234         }
235         System.out.print("\nReversed\n");
236         rng = ml.locateInTo(t, f);
237         if (rng != null)
238         {
239           for (int i = 0; i < rng.length; i++)
240           {
241             System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
242           }
243         }
244         else
245         {
246           System.out.println("No range!");
247         }
248         f++;
249         t--;
250         System.out.print("\n");
251       }
252     }
253   }
254
255   /**
256    * Tests for method that locates ranges in the 'from' map for given range in
257    * the 'to' map.
258    */
259   @Test(groups = { "Functional" })
260   public void testLocateInFrom_noIntrons()
261   {
262     /*
263      * Simple mapping with no introns
264      */
265     int[] codons = new int[] { 1, 12 };
266     int[] protein = new int[] { 1, 4 };
267     MapList ml = new MapList(codons, protein, 3, 1);
268     assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(1, 1)));
269     assertEquals("[4, 6]", Arrays.toString(ml.locateInFrom(2, 2)));
270     assertEquals("[7, 9]", Arrays.toString(ml.locateInFrom(3, 3)));
271     assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
272     assertEquals("[1, 6]", Arrays.toString(ml.locateInFrom(1, 2)));
273     assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(1, 3)));
274     // reversed range treated as if forwards:
275     assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(3, 1)));
276     assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 4)));
277     assertEquals("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3)));
278     assertEquals("[4, 12]", Arrays.toString(ml.locateInFrom(2, 4)));
279     assertEquals("[7, 12]", Arrays.toString(ml.locateInFrom(3, 4)));
280     assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
281
282     /*
283      * partial overlap
284      */
285     assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 5)));
286     assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(-1, 1)));
287
288     /*
289      * no overlap
290      */
291     assertNull(ml.locateInFrom(0, 0));
292   }
293
294   /**
295    * Tests for method that locates ranges in the 'from' map for given range in
296    * the 'to' map.
297    */
298   @Test(groups = { "Functional" })
299   public void testLocateInFrom_withIntrons()
300   {
301     /*
302      * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
303      * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
304      */
305     int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
306     int[] protein = { 1, 4 };
307     MapList ml = new MapList(codons, protein, 3, 1);
308     assertEquals("[2, 3, 5, 5]", Arrays.toString(ml.locateInFrom(1, 1)));
309     assertEquals("[6, 7, 9, 9]", Arrays.toString(ml.locateInFrom(2, 2)));
310     assertEquals("[10, 10, 12, 12, 14, 14]",
311             Arrays.toString(ml.locateInFrom(3, 3)));
312     assertEquals("[16, 18]", Arrays.toString(ml.locateInFrom(4, 4)));
313   }
314
315   @Test(groups = { "Functional" })
316   public void testLocateInFrom_reverseStrand()
317   {
318     int[] codons = new int[] { 12, 1 };
319     int[] protein = new int[] { 1, 4 };
320     MapList ml = new MapList(codons, protein, 3, 1);
321     assertEquals("[12, 10]", Arrays.toString(ml.locateInFrom(1, 1)));
322     assertEquals("[9, 4]", Arrays.toString(ml.locateInFrom(2, 3)));
323   }
324
325   /**
326    * Tests for method that locates ranges in the 'to' map for given range in the
327    * 'from' map.
328    */
329   @Test(groups = { "Functional" })
330   public void testLocateInTo_noIntrons()
331   {
332     /*
333      * Simple mapping with no introns
334      */
335     int[] codons = new int[] { 1, 12 };
336     int[] protein = new int[] { 1, 4 };
337     MapList ml = new MapList(codons, protein, 3, 1);
338     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 3)));
339     assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
340     assertEquals("[3, 3]", Arrays.toString(ml.locateInTo(7, 9)));
341     assertEquals("[4, 4]", Arrays.toString(ml.locateInTo(10, 12)));
342     assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 6)));
343     assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(1, 9)));
344     assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(1, 12)));
345     assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
346     assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(4, 12)));
347     // reverse range treated as if forwards:
348     assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(12, 4)));
349
350     /*
351      * A part codon is treated as if a whole one.
352      */
353     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
354     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 2)));
355     assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 4)));
356     assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(2, 8)));
357     assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
358     assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(5, 11)));
359
360     /*
361      * partial overlap
362      */
363     assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(1, 13)));
364     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(-1, 2)));
365     
366     /*
367      * no overlap
368      */
369     assertNull(ml.locateInTo(0, 0));
370   }
371
372   /**
373    * Tests for method that locates ranges in the 'to' map for given range in the
374    * 'from' map.
375    */
376   @Test(groups = { "Functional" })
377   public void testLocateInTo_withIntrons()
378   {
379     /*
380      * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
381      * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
382      */
383     int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
384     /*
385      * Mapped proteins at positions 1, 3, 4, 6 in the sequence
386      */
387     int[] protein = { 1, 1, 3, 4, 6, 6 };
388     MapList ml = new MapList(codons, protein, 3, 1);
389
390     /*
391      * Valid range or subrange of codon1 maps to protein1
392      */
393     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 2)));
394     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 3)));
395     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 5)));
396     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 3)));
397     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 5)));
398
399     // codon position 6 starts the next protein:
400     assertEquals("[1, 1, 3, 3]", Arrays.toString(ml.locateInTo(3, 6)));
401
402     // codon positions 7 to 17 (part) cover proteins 2/3/4 at positions 3/4/6
403     assertEquals("[3, 4, 6, 6]", Arrays.toString(ml.locateInTo(7, 17)));
404
405     /*
406      * partial overlap
407      */
408     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 2)));
409     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 4)));
410     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 4)));
411     
412     /*
413      * no overlap
414      */
415     assertNull(ml.locateInTo(4, 4));
416   }
417
418   /**
419    * Test equals method.
420    */
421   @Test(groups = { "Functional" })
422   public void testEquals()
423   {
424     int[] codons = new int[] { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
425     int[] protein = new int[] { 1, 4 };
426     MapList ml = new MapList(codons, protein, 3, 1);
427     MapList ml1 = new MapList(codons, protein, 3, 1); // same values
428     MapList ml2 = new MapList(codons, protein, 2, 1); // fromRatio differs
429     MapList ml3 = new MapList(codons, protein, 3, 2); // toRatio differs
430     codons[2] = 4;
431     MapList ml6 = new MapList(codons, protein, 3, 1); // fromShifts differ
432     protein[1] = 3;
433     MapList ml7 = new MapList(codons, protein, 3, 1); // toShifts differ
434
435     assertTrue(ml.equals(ml));
436     assertEquals(ml.hashCode(), ml.hashCode());
437     assertTrue(ml.equals(ml1));
438     assertEquals(ml.hashCode(), ml1.hashCode());
439     assertTrue(ml1.equals(ml));
440
441     assertFalse(ml.equals(null));
442     assertFalse(ml.equals("hello"));
443     assertFalse(ml.equals(ml2));
444     assertFalse(ml.equals(ml3));
445     assertFalse(ml.equals(ml6));
446     assertFalse(ml.equals(ml7));
447     assertFalse(ml6.equals(ml7));
448
449     try
450     {
451       MapList ml4 = new MapList(codons, null, 3, 1); // toShifts null
452       assertFalse(ml.equals(ml4));
453     } catch (NullPointerException e)
454     {
455       // actually thrown by constructor before equals can be called
456     }
457     try
458     {
459       MapList ml5 = new MapList(null, protein, 3, 1); // fromShifts null
460       assertFalse(ml.equals(ml5));
461     } catch (NullPointerException e)
462     {
463       // actually thrown by constructor before equals can be called
464     }
465   }
466
467   /**
468    * Test for the method that flattens a list of ranges into a single array.
469    */
470   @Test(groups = { "Functional" })
471   public void testGetRanges()
472   {
473     List<int[]> ranges = new ArrayList<>();
474     ranges.add(new int[] { 2, 3 });
475     ranges.add(new int[] { 5, 6 });
476     assertEquals("[2, 3, 5, 6]",
477             Arrays.toString(MapList.getRanges(ranges)));
478   }
479
480   /**
481    * Check state after construction
482    */
483   @Test(groups = { "Functional" })
484   public void testConstructor()
485   {
486     int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
487     int[] protein = { 1, 1, 3, 4, 6, 6 };
488     MapList ml = new MapList(codons, protein, 3, 1);
489     assertEquals(3, ml.getFromRatio());
490     assertEquals(2, ml.getFromLowest());
491     assertEquals(18, ml.getFromHighest());
492     assertEquals(1, ml.getToLowest());
493     assertEquals(6, ml.getToHighest());
494     assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
495             prettyPrint(ml.getFromRanges()));
496     assertEquals("{[1, 1], [3, 4], [6, 6]}", prettyPrint(ml.getToRanges()));
497
498     /*
499      * Also copy constructor
500      */
501     MapList ml2 = new MapList(ml);
502     assertEquals(3, ml2.getFromRatio());
503     assertEquals(2, ml2.getFromLowest());
504     assertEquals(18, ml2.getFromHighest());
505     assertEquals(1, ml2.getToLowest());
506     assertEquals(6, ml2.getToHighest());
507     assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
508             prettyPrint(ml2.getFromRanges()));
509     assertEquals("{[1, 1], [3, 4], [6, 6]}",
510             prettyPrint(ml2.getToRanges()));
511
512     /*
513      * reverse direction
514      */
515     codons = new int[] { 9, 6 };
516     protein = new int[] { 100, 91, 80, 79 };
517     ml = new MapList(codons, protein, 3, 1);
518     assertEquals(6, ml.getFromLowest());
519     assertEquals(9, ml.getFromHighest());
520     assertEquals(79, ml.getToLowest());
521     assertEquals(100, ml.getToHighest());
522   }
523
524   /**
525    * Test constructor used to merge consecutive ranges but now just leaves them
526    * as supplied (JAL-3751)
527    */
528   @Test(groups = { "Functional" })
529   public void testConstructor_mergeRanges()
530   {
531     int[] codons = { 2, 3, 3, 7, 9, 10, 12, 12, 13, 14, 16, 17 };
532     int[] protein = { 1, 1, 2, 3, 6, 6 };
533     MapList ml = new MapList(codons, protein, 3, 1);
534     assertEquals(3, ml.getFromRatio());
535     assertEquals(2, ml.getFromLowest());
536     assertEquals(17, ml.getFromHighest());
537     assertEquals(1, ml.getToLowest());
538     assertEquals(6, ml.getToHighest());
539     assertEquals("{[2, 3], [3, 7], [9, 10], [12, 12], [13, 14], [16, 17]}",
540             prettyPrint(ml.getFromRanges()));
541     assertEquals("{[1, 1], [2, 3], [6, 6]}", prettyPrint(ml.getToRanges()));
542   }
543
544   /**
545    * Convert a List of {[i, j], [k, l], ...} to "[[i, j], [k, l], ...]"
546    * 
547    * @param ranges
548    * @return
549    */
550   private String prettyPrint(List<int[]> ranges)
551   {
552     StringBuilder sb = new StringBuilder(ranges.size() * 5);
553     boolean first = true;
554     sb.append("{");
555     for (int[] range : ranges)
556     {
557       if (!first)
558       {
559         sb.append(", ");
560       }
561       sb.append(Arrays.toString(range));
562       first = false;
563     }
564     sb.append("}");
565     return sb.toString();
566   }
567
568   /**
569    * Test the method that creates an inverse mapping
570    */
571   @Test(groups = { "Functional" })
572   public void testGetInverse()
573   {
574     int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
575     int[] protein = { 1, 1, 3, 4, 6, 6 };
576
577     MapList ml = new MapList(codons, protein, 3, 1);
578     MapList ml2 = ml.getInverse();
579     assertEquals(ml.getFromRatio(), ml2.getToRatio());
580     assertEquals(ml.getFromRatio(), ml2.getToRatio());
581     assertEquals(ml.getToHighest(), ml2.getFromHighest());
582     assertEquals(ml.getFromHighest(), ml2.getToHighest());
583     assertEquals(prettyPrint(ml.getFromRanges()),
584             prettyPrint(ml2.getToRanges()));
585     assertEquals(prettyPrint(ml.getToRanges()),
586             prettyPrint(ml2.getFromRanges()));
587   }
588
589   @Test(groups = { "Functional" })
590   public void testToString()
591   {
592     MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 },
593             new int[]
594             { 51, 1 }, 1, 3);
595     String s = ml.toString();
596     assertEquals("[ [1, 5] [10, 15] [25, 20] ] 1:3 to [ [51, 1] ]", s);
597   }
598
599   @Test(groups = { "Functional" })
600   public void testAddMapList()
601   {
602     MapList ml = new MapList(new int[] { 11, 15, 20, 25, 35, 30 },
603             new int[]
604             { 72, 22 }, 1, 3);
605     assertEquals(11, ml.getFromLowest());
606     assertEquals(35, ml.getFromHighest());
607     assertEquals(22, ml.getToLowest());
608     assertEquals(72, ml.getToHighest());
609
610     MapList ml2 = new MapList(new int[] { 2, 4, 37, 40 },
611             new int[]
612             { 12, 17, 78, 83, 88, 96 }, 1, 3);
613     ml.addMapList(ml2);
614     assertEquals(2, ml.getFromLowest());
615     assertEquals(40, ml.getFromHighest());
616     assertEquals(12, ml.getToLowest());
617     assertEquals(96, ml.getToHighest());
618
619     String s = ml.toString();
620     assertEquals(
621             "[ [11, 15] [20, 25] [35, 30] [2, 4] [37, 40] ] 1:3 to [ [72, 22] [12, 17] [78, 83] [88, 96] ]",
622             s);
623   }
624
625   /**
626    * Test that confirms adding a map twice does nothing
627    */
628   @Test(groups = { "Functional" })
629   public void testAddMapList_sameMap()
630   {
631     MapList ml = new MapList(new int[] { 11, 15, 20, 25, 35, 30 },
632             new int[]
633             { 72, 22 }, 1, 3);
634     String before = ml.toString();
635     ml.addMapList(ml);
636     assertEquals(before, ml.toString());
637     ml.addMapList(new MapList(ml));
638     assertEquals(before, ml.toString());
639   }
640
641   @Test(groups = { "Functional" })
642   public void testAddMapList_contiguous()
643   {
644     MapList ml = new MapList(new int[] { 11, 15 }, new int[] { 72, 58 }, 1,
645             3);
646
647     MapList ml2 = new MapList(new int[] { 15, 16 }, new int[] { 58, 53 }, 1,
648             3);
649     ml.addMapList(ml2);
650     assertEquals("[ [11, 16] ] 1:3 to [ [72, 53] ]", ml.toString());
651   }
652
653   @Test(groups = "Functional")
654   public void testAddRange()
655   {
656     int[] range = { 1, 5 };
657     List<int[]> ranges = new ArrayList<>();
658
659     // add to empty list:
660     MapList.addRange(range, ranges);
661     assertEquals(1, ranges.size());
662     assertSame(range, ranges.get(0));
663
664     // extend contiguous (same position):
665     MapList.addRange(new int[] { 5, 10 }, ranges);
666     assertEquals(1, ranges.size());
667     assertEquals(1, ranges.get(0)[0]);
668     assertEquals(10, ranges.get(0)[1]);
669
670     // extend contiguous (next position):
671     MapList.addRange(new int[] { 11, 15 }, ranges);
672     assertEquals(1, ranges.size());
673     assertEquals(1, ranges.get(0)[0]);
674     assertEquals(15, ranges.get(0)[1]);
675
676     // change direction: range is not merged:
677     MapList.addRange(new int[] { 16, 10 }, ranges);
678     assertEquals(2, ranges.size());
679     assertEquals(16, ranges.get(1)[0]);
680     assertEquals(10, ranges.get(1)[1]);
681
682     // extend reverse contiguous (same position):
683     MapList.addRange(new int[] { 10, 8 }, ranges);
684     assertEquals(2, ranges.size());
685     assertEquals(16, ranges.get(1)[0]);
686     assertEquals(8, ranges.get(1)[1]);
687
688     // extend reverse contiguous (next position):
689     MapList.addRange(new int[] { 7, 6 }, ranges);
690     assertEquals(2, ranges.size());
691     assertEquals(16, ranges.get(1)[0]);
692     assertEquals(6, ranges.get(1)[1]);
693
694     // change direction: range is not merged:
695     MapList.addRange(new int[] { 6, 9 }, ranges);
696     assertEquals(3, ranges.size());
697     assertEquals(6, ranges.get(2)[0]);
698     assertEquals(9, ranges.get(2)[1]);
699
700     // not contiguous: not merged
701     MapList.addRange(new int[] { 11, 12 }, ranges);
702     assertEquals(4, ranges.size());
703     assertEquals(11, ranges.get(3)[0]);
704     assertEquals(12, ranges.get(3)[1]);
705   }
706
707   /**
708    * Check state after construction
709    */
710   @Test(groups = { "Functional" })
711   public void testConstructor_withLists()
712   {
713     /*
714      * reverse direction
715      */
716     int[][] codons = new int[][] { { 9, 6 } };
717     int[][] protein = new int[][] { { 100, 91 }, { 80, 79 } };
718     MapList ml = new MapList(Arrays.asList(codons), Arrays.asList(protein),
719             3, 1);
720     assertEquals(6, ml.getFromLowest());
721     assertEquals(9, ml.getFromHighest());
722     assertEquals(79, ml.getToLowest());
723     assertEquals(100, ml.getToHighest());
724   }
725
726   /**
727    * Test that method that inspects for the (first) forward or reverse from
728    * range. Single position ranges are ignored.
729    */
730   @Test(groups = { "Functional" })
731   public void testIsFromForwardStrand()
732   {
733     // [3-9] declares forward strand
734     MapList ml = new MapList(new int[] { 2, 2, 3, 9, 12, 11 },
735             new int[]
736             { 20, 11 }, 1, 1);
737     assertTrue(ml.isFromForwardStrand());
738
739     // [11-5] declares reverse strand ([13-14] is ignored)
740     ml = new MapList(new int[] { 2, 2, 11, 5, 13, 14 },
741             new int[]
742             { 20, 11 }, 1, 1);
743     assertFalse(ml.isFromForwardStrand());
744
745     // all single position ranges - defaults to forward strand
746     ml = new MapList(new int[] { 2, 2, 4, 4, 6, 6 }, new int[] { 3, 1 }, 1,
747             1);
748     assertTrue(ml.isFromForwardStrand());
749   }
750
751   /**
752    * Test the method that merges contiguous ranges
753    */
754   @Test(groups = { "Functional" })
755   public void testCoalesceRanges()
756   {
757     assertNull(MapList.coalesceRanges(null));
758     List<int[]> ranges = new ArrayList<>();
759     assertSame(ranges, MapList.coalesceRanges(ranges));
760     ranges.add(new int[] { 1, 3 });
761     assertSame(ranges, MapList.coalesceRanges(ranges));
762
763     // add non-contiguous range:
764     ranges.add(new int[] { 5, 6 });
765     assertSame(ranges, MapList.coalesceRanges(ranges));
766
767     // 'contiguous' range in opposite direction is not merged:
768     ranges.add(new int[] { 7, 6 });
769     assertSame(ranges, MapList.coalesceRanges(ranges));
770
771     // merging in forward direction:
772     ranges.clear();
773     ranges.add(new int[] { 1, 3 });
774     ranges.add(new int[] { 4, 5 }); // contiguous
775     ranges.add(new int[] { 5, 5 }); // overlap!
776     ranges.add(new int[] { 6, 7 }); // contiguous
777     List<int[]> merged = MapList.coalesceRanges(ranges);
778     assertEquals(2, merged.size());
779     assertArrayEquals(new int[] { 1, 5 }, merged.get(0));
780     assertArrayEquals(new int[] { 5, 7 }, merged.get(1));
781     // verify input list is unchanged
782     assertEquals(4, ranges.size());
783     assertArrayEquals(new int[] { 1, 3 }, ranges.get(0));
784     assertArrayEquals(new int[] { 4, 5 }, ranges.get(1));
785     assertArrayEquals(new int[] { 5, 5 }, ranges.get(2));
786     assertArrayEquals(new int[] { 6, 7 }, ranges.get(3));
787
788     // merging in reverse direction:
789     ranges.clear();
790     ranges.add(new int[] { 7, 5 });
791     ranges.add(new int[] { 5, 4 }); // overlap
792     ranges.add(new int[] { 4, 4 }); // overlap
793     ranges.add(new int[] { 3, 1 }); // contiguous
794     merged = MapList.coalesceRanges(ranges);
795     assertEquals(3, merged.size());
796     assertArrayEquals(new int[] { 7, 5 }, merged.get(0));
797     assertArrayEquals(new int[] { 5, 4 }, merged.get(1));
798     assertArrayEquals(new int[] { 4, 1 }, merged.get(2));
799
800     // merging with switches of direction:
801     ranges.clear();
802     ranges.add(new int[] { 1, 3 });
803     ranges.add(new int[] { 4, 5 }); // contiguous
804     ranges.add(new int[] { 5, 5 }); // overlap
805     ranges.add(new int[] { 6, 6 }); // contiguous
806     ranges.add(new int[] { 12, 10 });
807     ranges.add(new int[] { 9, 8 }); // contiguous
808     ranges.add(new int[] { 8, 8 }); // overlap
809     ranges.add(new int[] { 7, 7 }); // contiguous
810     merged = MapList.coalesceRanges(ranges);
811     assertEquals(4, merged.size());
812     assertArrayEquals(new int[] { 1, 5 }, merged.get(0));
813     assertArrayEquals(new int[] { 5, 6 }, merged.get(1));
814     assertArrayEquals(new int[] { 12, 8 }, merged.get(2));
815     assertArrayEquals(new int[] { 8, 7 }, merged.get(3));
816
817     // 'subsumed' ranges are preserved
818     ranges.clear();
819     ranges.add(new int[] { 10, 30 });
820     ranges.add(new int[] { 15, 25 });
821     merged = MapList.coalesceRanges(ranges);
822     assertEquals(2, merged.size());
823     assertArrayEquals(new int[] { 10, 30 }, merged.get(0));
824     assertArrayEquals(new int[] { 15, 25 }, merged.get(1));
825   }
826
827   /**
828    * Test the method that compounds ('traverses') two mappings
829    */
830   @Test(groups = "Functional")
831   public void testTraverse()
832   {
833     /*
834      * simple 1:1 plus 1:1 forwards
835      */
836     MapList ml1 = new MapList(new int[] { 3, 4, 8, 12 },
837             new int[]
838             { 5, 8, 11, 13 }, 1, 1);
839     assertEquals("{[3, 4], [8, 12]}", prettyPrint(ml1.getFromRanges()));
840     assertEquals("{[5, 8], [11, 13]}", prettyPrint(ml1.getToRanges()));
841
842     MapList ml2 = new MapList(new int[] { 1, 50 },
843             new int[]
844             { 40, 45, 70, 75, 90, 127 }, 1, 1);
845     assertEquals("{[1, 50]}", prettyPrint(ml2.getFromRanges()));
846     assertEquals("{[40, 45], [70, 75], [90, 127]}",
847             prettyPrint(ml2.getToRanges()));
848
849     MapList compound = ml1.traverse(ml2);
850
851     assertEquals(1, compound.getFromRatio());
852     assertEquals(1, compound.getToRatio());
853     List<int[]> fromRanges = compound.getFromRanges();
854     assertEquals(2, fromRanges.size());
855     assertArrayEquals(new int[] { 3, 4 }, fromRanges.get(0));
856     assertArrayEquals(new int[] { 8, 12 }, fromRanges.get(1));
857     List<int[]> toRanges = compound.getToRanges();
858     assertEquals(4, toRanges.size());
859     // 5-8 maps to 44-45,70-71
860     // 11-13 maps to 74-75,90
861     assertArrayEquals(new int[] { 44, 45 }, toRanges.get(0));
862     assertArrayEquals(new int[] { 70, 71 }, toRanges.get(1));
863     assertArrayEquals(new int[] { 74, 75 }, toRanges.get(2));
864     assertArrayEquals(new int[] { 90, 90 }, toRanges.get(3));
865
866     /*
867      * 1:1 over 1:1 backwards ('reverse strand')
868      */
869     ml1 = new MapList(new int[] { 1, 50 }, new int[] { 70, 119 }, 1, 1);
870     ml2 = new MapList(new int[] { 1, 500 },
871             new int[]
872             { 1000, 901, 600, 201 }, 1, 1);
873     compound = ml1.traverse(ml2);
874
875     assertEquals(1, compound.getFromRatio());
876     assertEquals(1, compound.getToRatio());
877     fromRanges = compound.getFromRanges();
878     assertEquals(1, fromRanges.size());
879     assertArrayEquals(new int[] { 1, 50 }, fromRanges.get(0));
880     toRanges = compound.getToRanges();
881     assertEquals(2, toRanges.size());
882     assertArrayEquals(new int[] { 931, 901 }, toRanges.get(0));
883     assertArrayEquals(new int[] { 600, 582 }, toRanges.get(1));
884
885     /*
886      * 1:1 plus 1:3 should result in 1:3
887      */
888     ml1 = new MapList(new int[] { 1, 30 }, new int[] { 11, 40 }, 1, 1);
889     ml2 = new MapList(new int[] { 1, 100 }, new int[] { 1, 50, 91, 340 }, 1,
890             3);
891     compound = ml1.traverse(ml2);
892
893     assertEquals(1, compound.getFromRatio());
894     assertEquals(3, compound.getToRatio());
895     fromRanges = compound.getFromRanges();
896     assertEquals(1, fromRanges.size());
897     assertArrayEquals(new int[] { 1, 30 }, fromRanges.get(0));
898     // 11-40 maps to 31-50,91-160
899     toRanges = compound.getToRanges();
900     assertEquals(2, toRanges.size());
901     assertArrayEquals(new int[] { 31, 50 }, toRanges.get(0));
902     assertArrayEquals(new int[] { 91, 160 }, toRanges.get(1));
903
904     /*
905      * 3:1 plus 1:1 should result in 3:1
906      */
907     ml1 = new MapList(new int[] { 1, 30 }, new int[] { 11, 20 }, 3, 1);
908     ml2 = new MapList(new int[] { 1, 100 }, new int[] { 1, 15, 91, 175 }, 1,
909             1);
910     compound = ml1.traverse(ml2);
911
912     assertEquals(3, compound.getFromRatio());
913     assertEquals(1, compound.getToRatio());
914     fromRanges = compound.getFromRanges();
915     assertEquals(1, fromRanges.size());
916     assertArrayEquals(new int[] { 1, 30 }, fromRanges.get(0));
917     // 11-20 maps to 11-15, 91-95
918     toRanges = compound.getToRanges();
919     assertEquals(2, toRanges.size());
920     assertArrayEquals(new int[] { 11, 15 }, toRanges.get(0));
921     assertArrayEquals(new int[] { 91, 95 }, toRanges.get(1));
922
923     /*
924      * 1:3 plus 3:1 should result in 1:1
925      */
926     ml1 = new MapList(new int[] { 21, 40 }, new int[] { 13, 72 }, 1, 3);
927     ml2 = new MapList(new int[] { 1, 300 }, new int[] { 51, 70, 121, 200 },
928             3, 1);
929     compound = ml1.traverse(ml2);
930
931     assertEquals(1, compound.getFromRatio());
932     assertEquals(1, compound.getToRatio());
933     fromRanges = compound.getFromRanges();
934     assertEquals(1, fromRanges.size());
935     assertArrayEquals(new int[] { 21, 40 }, fromRanges.get(0));
936     // 13-72 maps 3:1 to 55-70, 121-124
937     toRanges = compound.getToRanges();
938     assertEquals(2, toRanges.size());
939     assertArrayEquals(new int[] { 55, 70 }, toRanges.get(0));
940     assertArrayEquals(new int[] { 121, 124 }, toRanges.get(1));
941
942     /*
943      * 3:1 plus 1:3 should result in 1:1
944      */
945     ml1 = new MapList(new int[] { 31, 90 }, new int[] { 13, 32 }, 3, 1);
946     ml2 = new MapList(new int[] { 11, 40 }, new int[] { 41, 50, 71, 150 },
947             1, 3);
948     compound = ml1.traverse(ml2);
949
950     assertEquals(1, compound.getFromRatio());
951     assertEquals(1, compound.getToRatio());
952     fromRanges = compound.getFromRanges();
953     assertEquals(1, fromRanges.size());
954     assertArrayEquals(new int[] { 31, 90 }, fromRanges.get(0));
955     // 13-32 maps to 47-50,71-126
956     toRanges = compound.getToRanges();
957     assertEquals(2, toRanges.size());
958     assertArrayEquals(new int[] { 47, 50 }, toRanges.get(0));
959     assertArrayEquals(new int[] { 71, 126 }, toRanges.get(1));
960
961     /*
962      * if not all regions are mapped through, returns what is
963      */
964     ml1 = new MapList(new int[] { 1, 50 }, new int[] { 101, 150 }, 1, 1);
965     ml2 = new MapList(new int[] { 131, 180 }, new int[] { 201, 250 }, 1, 1);
966     compound = ml1.traverse(ml2);
967     assertNull(compound);
968   }
969
970   /**
971    * Test that method that inspects for the (first) forward or reverse 'to'
972    * range. Single position ranges are ignored.
973    */
974   @Test(groups = { "Functional" })
975   public void testIsToForwardsStrand()
976   {
977     // [3-9] declares forward strand
978     MapList ml = new MapList(new int[] { 20, 11 },
979             new int[]
980             { 2, 2, 3, 9, 12, 11 }, 1, 1);
981     assertTrue(ml.isToForwardStrand());
982
983     // [11-5] declares reverse strand ([13-14] is ignored)
984     ml = new MapList(new int[] { 20, 11 },
985             new int[]
986             { 2, 2, 11, 5, 13, 14 }, 1, 1);
987     assertFalse(ml.isToForwardStrand());
988
989     // all single position ranges - defaults to forward strand
990     ml = new MapList(new int[] { 3, 1 }, new int[] { 2, 2, 4, 4, 6, 6 }, 1,
991             1);
992     assertTrue(ml.isToForwardStrand());
993   }
994
995   /**
996    * Test for mapping with overlapping ranges
997    */
998   @Test(groups = { "Functional" })
999   public void testLocateInFrom_withOverlap()
1000   {
1001     /*
1002      * gene to protein...
1003      */
1004     int[] codons = new int[] { 1, 12, 12, 17 };
1005     int[] protein = new int[] { 1, 6 };
1006     MapList ml = new MapList(codons, protein, 3, 1);
1007     assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(1, 1)));
1008     assertEquals("[4, 6]", Arrays.toString(ml.locateInFrom(2, 2)));
1009     assertEquals("[7, 9]", Arrays.toString(ml.locateInFrom(3, 3)));
1010     assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
1011     assertEquals("[12, 14]", Arrays.toString(ml.locateInFrom(5, 5)));
1012     assertEquals("[15, 17]", Arrays.toString(ml.locateInFrom(6, 6)));
1013     assertEquals("[1, 6]", Arrays.toString(ml.locateInFrom(1, 2)));
1014     assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(1, 3)));
1015     assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 4)));
1016     assertEquals("[1, 12, 12, 14]", Arrays.toString(ml.locateInFrom(1, 5)));
1017     assertEquals("[1, 12, 12, 17]", Arrays.toString(ml.locateInFrom(1, 6)));
1018     assertEquals("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3)));
1019     assertEquals("[7, 12, 12, 17]", Arrays.toString(ml.locateInFrom(3, 6)));
1020
1021     /*
1022      * partial overlap of range
1023      */
1024     assertEquals("[4, 12, 12, 17]", Arrays.toString(ml.locateInFrom(2, 7)));
1025     assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(-1, 1)));
1026
1027     /*
1028      * no overlap in range
1029      */
1030     assertNull(ml.locateInFrom(0, 0));
1031
1032     /*
1033      * gene to CDS...from EMBL:MN908947
1034      */
1035     int[] gene = new int[] { 266, 13468, 13468, 21555 };
1036     int[] cds = new int[] { 1, 21291 };
1037     ml = new MapList(gene, cds, 1, 1);
1038     assertEquals("[13468, 13468]",
1039             Arrays.toString(ml.locateInFrom(13203, 13203)));
1040     assertEquals("[13468, 13468]",
1041             Arrays.toString(ml.locateInFrom(13204, 13204)));
1042     assertEquals("[13468, 13468, 13468, 13468]",
1043             Arrays.toString(ml.locateInFrom(13203, 13204)));
1044   }
1045
1046   /**
1047    * Test for mapping with overlapping ranges
1048    */
1049   @Test(groups = { "Functional" })
1050   public void testLocateInTo_withOverlap()
1051   {
1052     /*
1053      * gene to protein...
1054      */
1055     int[] codons = new int[] { 1, 12, 12, 17 };
1056     int[] protein = new int[] { 1, 6 };
1057     MapList ml = new MapList(codons, protein, 3, 1);
1058     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
1059     assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(3, 8)));
1060     assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(2, 11)));
1061     assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
1062
1063     // we want base 12 to map to both of the amino acids it codes for
1064     assertEquals("[4, 5]", Arrays.toString(ml.locateInTo(12, 12)));
1065     assertEquals("[4, 5]", Arrays.toString(ml.locateInTo(11, 12)));
1066     assertEquals("[4, 6]", Arrays.toString(ml.locateInTo(11, 15)));
1067     assertEquals("[6, 6]", Arrays.toString(ml.locateInTo(15, 17)));
1068
1069     /*
1070      * no overlap
1071      */
1072     assertNull(ml.locateInTo(0, 0));
1073     
1074     /*
1075      * partial overlap
1076      */
1077     assertEquals("[1, 6]", Arrays.toString(ml.locateInTo(1, 18)));
1078     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(-1, 1)));
1079
1080     /*
1081      * gene to CDS...from EMBL:MN908947
1082      * the base at 13468 is used twice in transcription
1083      */
1084     int[] gene = new int[] { 266, 13468, 13468, 21555 };
1085     int[] cds = new int[] { 1, 21291 };
1086     ml = new MapList(gene, cds, 1, 1);
1087     assertEquals("[13203, 13204]",
1088             Arrays.toString(ml.locateInTo(13468, 13468)));
1089     
1090     /*
1091      * gene to protein
1092      * the base at 13468 is in the codon for 4401N and also 4402R
1093      */
1094     gene = new int[] { 266, 13468, 13468, 21552 }; // stop codon excluded
1095     protein = new int[] { 1, 7096 };
1096     ml = new MapList(gene, protein, 3, 1);
1097     assertEquals("[4401, 4402]",
1098             Arrays.toString(ml.locateInTo(13468, 13468)));
1099   }
1100
1101   @Test(groups = { "Functional" })
1102   public void testTraverseToPosition()
1103   {
1104     List<int[]> ranges = new ArrayList<>();
1105     assertNull(MapList.traverseToPosition(ranges, 0));
1106
1107     ranges.add(new int[] { 3, 6 });
1108     assertNull(MapList.traverseToPosition(ranges, 0));
1109   }
1110
1111   @Test(groups = { "Functional" })
1112   public void testCountPositions()
1113   {
1114     try
1115     {
1116       MapList.countPositions(null, 1);
1117       fail("expected exception");
1118     } catch (NullPointerException e)
1119     {
1120       // expected
1121     }
1122
1123     List<int[]> intervals = new ArrayList<>();
1124     assertNull(MapList.countPositions(intervals, 1));
1125
1126     /*
1127      * forward strand
1128      */
1129     intervals.add(new int[] { 10, 20 });
1130     assertNull(MapList.countPositions(intervals, 9));
1131     assertNull(MapList.countPositions(intervals, 21));
1132     assertArrayEquals(new int[] { 1, 1 },
1133             MapList.countPositions(intervals, 10));
1134     assertArrayEquals(new int[] { 6, 1 },
1135             MapList.countPositions(intervals, 15));
1136     assertArrayEquals(new int[] { 11, 1 },
1137             MapList.countPositions(intervals, 20));
1138
1139     intervals.add(new int[] { 25, 25 });
1140     assertArrayEquals(new int[] { 12, 1 },
1141             MapList.countPositions(intervals, 25));
1142
1143     // next interval repeats position 25 - which should be counted twice if
1144     // traversed
1145     intervals.add(new int[] { 25, 26 });
1146     assertArrayEquals(new int[] { 12, 1 },
1147             MapList.countPositions(intervals, 25));
1148     assertArrayEquals(new int[] { 14, 1 },
1149             MapList.countPositions(intervals, 26));
1150
1151     /*
1152      * reverse strand
1153      */
1154     intervals.clear();
1155     intervals.add(new int[] { 5, -5 });
1156     assertNull(MapList.countPositions(intervals, 6));
1157     assertNull(MapList.countPositions(intervals, -6));
1158     assertArrayEquals(new int[] { 1, -1 },
1159             MapList.countPositions(intervals, 5));
1160     assertArrayEquals(new int[] { 7, -1 },
1161             MapList.countPositions(intervals, -1));
1162     assertArrayEquals(new int[] { 11, -1 },
1163             MapList.countPositions(intervals, -5));
1164
1165     /*
1166      * reverse then forward
1167      */
1168     intervals.add(new int[] { 5, 10 });
1169     assertArrayEquals(new int[] { 13, 1 },
1170             MapList.countPositions(intervals, 6));
1171
1172     /*
1173      * reverse then forward then reverse
1174      */
1175     intervals.add(new int[] { -10, -20 });
1176     assertArrayEquals(new int[] { 20, -1 },
1177             MapList.countPositions(intervals, -12));
1178
1179     /*
1180      * an interval [x, x] is treated as forward
1181      */
1182     intervals.add(new int[] { 30, 30 });
1183     assertArrayEquals(new int[] { 29, 1 },
1184             MapList.countPositions(intervals, 30));
1185
1186     /*
1187      * it is the first matched occurrence that is returned
1188      */
1189     intervals.clear();
1190     intervals.add(new int[] { 1, 2 });
1191     intervals.add(new int[] { 2, 3 });
1192     assertArrayEquals(new int[] { 2, 1 },
1193             MapList.countPositions(intervals, 2));
1194     intervals.add(new int[] { -1, -2 });
1195     intervals.add(new int[] { -2, -3 });
1196     assertArrayEquals(new int[] { 6, -1 },
1197             MapList.countPositions(intervals, -2));
1198   }
1199
1200   /**
1201    * Tests for helper method that adds any overlap (plus offset) to a list of
1202    * overlaps
1203    */
1204   @Test(groups = { "Functional" })
1205   public void testAddOverlap()
1206   {
1207     List<int[]> overlaps = new ArrayList<>();
1208     int[] candidate = new int[] { 10, 19 };
1209     MapList.addOverlap(overlaps, 5, candidate, 20, 30); // doesn't overlap
1210     assertTrue(overlaps.isEmpty());
1211     MapList.addOverlap(overlaps, 5, candidate, 31, 40); // doesn't overlap
1212     assertTrue(overlaps.isEmpty());
1213
1214     /*
1215      * 10-19 overlaps 15-25 at 15-19, which is offset 5-9 in 10-19
1216      * + 5 initial offset
1217      */
1218     MapList.addOverlap(overlaps, 5, candidate, 15, 25);
1219     assertEquals(1, overlaps.size());
1220     assertArrayEquals(new int[] { 10, 14 }, overlaps.get(0));
1221
1222     /*
1223      * reverse range overlap:
1224      * 300-20 overlaps 15-25 at 25-20, which is offset 275-280 in 300-20
1225      * + 8 initial offset
1226      */
1227     overlaps.clear();
1228     candidate = new int[] { 300, 20 };
1229     MapList.addOverlap(overlaps, 8, candidate, 15, 25);
1230     assertEquals(1, overlaps.size());
1231     assertArrayEquals(new int[] { 283, 288 }, overlaps.get(0));
1232   }
1233
1234   @Test(groups = { "Functional" })
1235   public void testFindOverlapPositions()
1236   {
1237     List<int[]> ranges = new ArrayList<>();
1238     List<int[]> overlaps = MapList.findOverlapPositions(ranges, 20, 30);
1239     assertTrue(overlaps.isEmpty()); // nothing to overlap
1240
1241     ranges.add(new int[] { 15, 25 });
1242     overlaps = MapList.findOverlapPositions(ranges, 5, 10);
1243     assertTrue(overlaps.isEmpty()); // no overlap
1244
1245     overlaps = MapList.findOverlapPositions(ranges, 20, 20);
1246     assertEquals(1, overlaps.size());
1247     assertArrayEquals(new int[] { 5, 5 }, overlaps.get(0));
1248
1249     overlaps = MapList.findOverlapPositions(ranges, 5, 19);
1250     assertEquals(1, overlaps.size());
1251     assertArrayEquals(new int[] { 0, 4 }, overlaps.get(0));
1252
1253     ranges.add(new int[] { 35, 45 });
1254     overlaps = MapList.findOverlapPositions(ranges, 26, 34);
1255     assertTrue(overlaps.isEmpty());
1256
1257     /*
1258      * 24-37 overlaps the end of 15-25 and the start of 35-45
1259      * - offset positions are contiguous in the map so merged
1260      */
1261     overlaps = MapList.findOverlapPositions(ranges, 24, 37);
1262     assertEquals(1, overlaps.size());
1263     assertArrayEquals(new int[] { 9, 13 }, overlaps.get(0));
1264
1265     /*
1266      * EMBL:MN908947  https://www.ebi.ac.uk/ena/browser/api/embl/MN908947 
1267      * (Covid-SARS-2) CDS mapping with 'slippage'
1268      * (base 13468 is used twice in transcription)
1269      */
1270     ranges.clear();
1271     ranges.add(new int[] { 266, 13468 });
1272     ranges.add(new int[] { 13468, 21555 });
1273
1274     // 13468 occupies two offsets in the range list
1275     overlaps = MapList.findOverlapPositions(ranges, 13468, 13468);
1276     assertEquals(1, overlaps.size());
1277     assertArrayEquals(new int[] { 13202, 13203 }, overlaps.get(0));
1278     overlaps = MapList.findOverlapPositions(ranges, 13469, 13470);
1279     assertEquals(1, overlaps.size());
1280     assertArrayEquals(new int[] { 13204, 13205 }, overlaps.get(0));
1281
1282     /*
1283      * EMBL:J03321 https://www.ebi.ac.uk/ena/browser/api/embl/J03321
1284      * circular dna: CDS at [7022-7502, 1-437] 
1285      * = 481 + 437 = 918 bases = 305 aa's + stop codon
1286      */
1287     ranges.clear();
1288     ranges.add(new int[] { 7022, 7502 });
1289     ranges.add(new int[] { 1, 437 });
1290
1291     overlaps = MapList.findOverlapPositions(ranges, 438, 7021);
1292     assertTrue(overlaps.isEmpty());
1293
1294     // overlap first exon only:
1295     overlaps = MapList.findOverlapPositions(ranges, 7000, 7100);
1296     assertEquals(1, overlaps.size());
1297     assertArrayEquals(new int[] { 0, 78 }, overlaps.get(0));
1298
1299     // overlap second exon only: offset to mapping includes first exon
1300     overlaps = MapList.findOverlapPositions(ranges, 400, 500);
1301     assertEquals(1, overlaps.size());
1302     assertArrayEquals(new int[] { 880, 917 }, overlaps.get(0));
1303
1304     // overlap both exons: first exon overlap precedes second exon overlap
1305     // offsets of overlaps are not contiguous
1306     overlaps = MapList.findOverlapPositions(ranges, 200, 7500);
1307     assertEquals(2, overlaps.size());
1308     // first overlap is offsets of 7022-7500 in exon 1 (7022-7502):
1309     assertArrayEquals(new int[] { 0, 478 }, overlaps.get(0));
1310     // second overlap offsets is (first exon length = 481) + (200-437)
1311     assertArrayEquals(new int[] { 680, 917 }, overlaps.get(1));
1312   }
1313
1314   @Test(groups = { "Functional" })
1315   public void testMapWords()
1316   {
1317     List<int[]> ranges = new ArrayList<>();
1318
1319     /*
1320      * 1:1 (trivial) case
1321      */
1322     ranges.add(new int[] { 2, 4 });
1323     ranges.add(new int[] { 6, 9 });
1324     MapList.mapWords(ranges, 1, 1);
1325     assertEquals(ranges.size(), 2);
1326     assertArrayEquals(new int[] { 2, 4 }, ranges.get(0));
1327     assertArrayEquals(new int[] { 6, 9 }, ranges.get(1));
1328
1329     /*
1330      * 1:3 case (peptide to codon ranges)
1331      */
1332     MapList.mapWords(ranges, 1, 3);
1333     assertEquals(ranges.size(), 2);
1334     assertArrayEquals(new int[] { 6, 14 }, ranges.get(0));
1335     assertArrayEquals(new int[] { 18, 29 }, ranges.get(1));
1336
1337     /*
1338      * 3:1 case (codon or part codon to peptide)
1339      */
1340     ranges.clear();
1341     ranges.add(new int[] { 0, 5 }); // 2 whole codons
1342     ranges.add(new int[] { 7, 11 }); // part + whole codon
1343     ranges.add(new int[] { 15, 19 }); // whole + part codon
1344     ranges.add(new int[] { 23, 27 }); // part + part codon
1345     ranges.add(new int[] { 30, 30 }); // first base of codon
1346     ranges.add(new int[] { 31, 31 }); // second base of codon
1347     ranges.add(new int[] { 32, 32 }); // third base of codon
1348     MapList.mapWords(ranges, 3, 1);
1349     assertEquals(ranges.size(), 7);
1350     assertArrayEquals(new int[] { 0, 1 }, ranges.get(0));
1351     assertArrayEquals(new int[] { 2, 3 }, ranges.get(1));
1352     assertArrayEquals(new int[] { 5, 6 }, ranges.get(2));
1353     assertArrayEquals(new int[] { 7, 9 }, ranges.get(3));
1354     assertArrayEquals(new int[] { 10, 10 }, ranges.get(4));
1355     assertArrayEquals(new int[] { 10, 10 }, ranges.get(5));
1356     assertArrayEquals(new int[] { 10, 10 }, ranges.get(6));
1357   }
1358
1359   @Test(groups = { "Functional" })
1360   public void testLocateInFrom2()
1361   {
1362     /*
1363      * codons at 11-16, 21-26, 31-36 mapped to peptide positions 1, 3-4, 6-8
1364      */
1365     MapList ml = new MapList(new int[] { 11, 16, 21, 26, 31, 36 },
1366             new int[]
1367             { 1, 1, 3, 4, 6, 8 }, 3, 1);
1368     assertArrayEquals(new int[] { 11, 13 }, ml.locateInFrom2(1, 1));
1369     assertArrayEquals(new int[] { 11, 16 }, ml.locateInFrom2(1, 3));
1370     assertArrayEquals(new int[] { 11, 16, 21, 23 }, ml.locateInFrom2(1, 4));
1371     assertArrayEquals(new int[] { 14, 16, 21, 23 }, ml.locateInFrom2(3, 4));
1372   }
1373 }