JAL-2738 unit test for mapping failure case
[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.internal.junit.ArrayAsserts.assertArrayEquals;
29
30 import jalview.gui.JvOptionPane;
31
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.List;
35
36 import org.testng.annotations.BeforeClass;
37 import org.testng.annotations.Test;
38
39 public class MapListTest
40 {
41
42   @BeforeClass(alwaysRun = true)
43   public void setUpJvOptionPane()
44   {
45     JvOptionPane.setInteractiveMode(false);
46     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
47   }
48
49   @Test(groups = { "Functional" })
50   public void testSomething()
51   {
52     MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 }, new int[] {
53         51, 1 }, 1, 3);
54     MapList ml1 = new MapList(new int[] { 1, 3, 17, 4 },
55             new int[] { 51, 1 }, 1, 3);
56     MapList ml2 = new MapList(new int[] { 1, 60 }, new int[] { 1, 20 }, 3,
57             1);
58     // test internal consistency
59     int to[] = new int[51];
60     testMap(ml, 1, 60);
61     MapList mldna = new MapList(new int[] { 2, 2, 6, 8, 12, 16 }, new int[]
62     { 1, 3 }, 3, 1);
63     int[] frm = mldna.locateInFrom(1, 1);
64     testLocateFrom(mldna, 1, 1, new int[] { 2, 2, 6, 7 });
65     testMap(mldna, 1, 3);
66     /*
67      * for (int from=1; from<=51; from++) { int[] too=ml.shiftTo(from); int[]
68      * toofrom=ml.shiftFrom(too[0]);
69      * System.out.println("ShiftFrom("+from+")=="+too[0]+" %
70      * "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]); }
71      */
72   }
73
74   private static void testLocateFrom(MapList mldna, int i, int j, int[] ks)
75   {
76     int[] frm = mldna.locateInFrom(i, j);
77     assertEquals("Failed test locate from " + i + " to " + j,
78             Arrays.toString(frm), Arrays.toString(ks));
79   }
80
81   /**
82    * test routine. not incremental.
83    * 
84    * @param ml
85    * @param fromS
86    * @param fromE
87    */
88   private void testMap(MapList ml, int fromS, int fromE)
89   {
90     // todo convert to JUnit style tests
91     for (int from = 1; from <= 25; from++)
92     {
93       int[] too = ml.shiftFrom(from);
94       System.out.print("ShiftFrom(" + from + ")==");
95       if (too == null)
96       {
97         System.out.print("NaN\n");
98       }
99       else
100       {
101         System.out.print(too[0] + " % " + too[1] + " (" + too[2] + ")");
102         System.out.print("\t+--+\t");
103         int[] toofrom = ml.shiftTo(too[0]);
104         if (toofrom != null)
105         {
106           if (toofrom[0] != from)
107           {
108             System.err.println("Mapping not reflexive:" + from + " "
109                     + too[0] + "->" + toofrom[0]);
110           }
111           System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0]
112                   + " % " + toofrom[1] + " (" + toofrom[2] + ")");
113         }
114         else
115         {
116           System.out.println("ShiftTo(" + too[0] + ")=="
117                   + "NaN! - not Bijective Mapping!");
118         }
119       }
120     }
121     int mmap[][] = ml.makeFromMap();
122     System.out.println("FromMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
123             + mmap[0][2] + " " + mmap[0][3] + " ");
124     for (int i = 1; i <= mmap[1].length; i++)
125     {
126       if (mmap[1][i - 1] == -1)
127       {
128         System.out.print(i + "=XXX");
129
130       }
131       else
132       {
133         System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
134       }
135       if (i % 20 == 0)
136       {
137         System.out.print("\n");
138       }
139       else
140       {
141         System.out.print(",");
142       }
143     }
144     // test range function
145     System.out.print("\nTest locateInFrom\n");
146     {
147       int f = mmap[0][2], t = mmap[0][3];
148       while (f <= t)
149       {
150         System.out.println("Range " + f + " to " + t);
151         int rng[] = ml.locateInFrom(f, t);
152         if (rng != null)
153         {
154           for (int i = 0; i < rng.length; i++)
155           {
156             System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
157           }
158         }
159         else
160         {
161           System.out.println("No range!");
162         }
163         System.out.print("\nReversed\n");
164         rng = ml.locateInFrom(t, f);
165         if (rng != null)
166         {
167           for (int i = 0; i < rng.length; i++)
168           {
169             System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
170           }
171         }
172         else
173         {
174           System.out.println("No range!");
175         }
176         System.out.print("\n");
177         f++;
178         t--;
179       }
180     }
181     System.out.print("\n");
182     mmap = ml.makeToMap();
183     System.out.println("ToMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
184             + mmap[0][2] + " " + mmap[0][3] + " ");
185     for (int i = 1; i <= mmap[1].length; i++)
186     {
187       if (mmap[1][i - 1] == -1)
188       {
189         System.out.print(i + "=XXX");
190
191       }
192       else
193       {
194         System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
195       }
196       if (i % 20 == 0)
197       {
198         System.out.print("\n");
199       }
200       else
201       {
202         System.out.print(",");
203       }
204     }
205     System.out.print("\n");
206     // test range function
207     System.out.print("\nTest locateInTo\n");
208     {
209       int f = mmap[0][2], t = mmap[0][3];
210       while (f <= t)
211       {
212         System.out.println("Range " + f + " to " + t);
213         int rng[] = ml.locateInTo(f, t);
214         if (rng != null)
215         {
216           for (int i = 0; i < rng.length; i++)
217           {
218             System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
219           }
220         }
221         else
222         {
223           System.out.println("No range!");
224         }
225         System.out.print("\nReversed\n");
226         rng = ml.locateInTo(t, f);
227         if (rng != null)
228         {
229           for (int i = 0; i < rng.length; i++)
230           {
231             System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
232           }
233         }
234         else
235         {
236           System.out.println("No range!");
237         }
238         f++;
239         t--;
240         System.out.print("\n");
241       }
242     }
243   }
244
245   /**
246    * Tests for method that locates ranges in the 'from' map for given range in
247    * the 'to' map.
248    */
249   @Test(groups = { "Functional" })
250   public void testLocateInFrom_noIntrons()
251   {
252     /*
253      * Simple mapping with no introns
254      */
255     int[] codons = new int[] { 1, 12 };
256     int[] protein = new int[] { 1, 4 };
257     MapList ml = new MapList(codons, protein, 3, 1);
258     assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(1, 1)));
259     assertEquals("[4, 6]", Arrays.toString(ml.locateInFrom(2, 2)));
260     assertEquals("[7, 9]", Arrays.toString(ml.locateInFrom(3, 3)));
261     assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
262     assertEquals("[1, 6]", Arrays.toString(ml.locateInFrom(1, 2)));
263     assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(1, 3)));
264     assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 4)));
265     assertEquals("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3)));
266     assertEquals("[4, 12]", Arrays.toString(ml.locateInFrom(2, 4)));
267     assertEquals("[7, 12]", Arrays.toString(ml.locateInFrom(3, 4)));
268     assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
269
270     assertNull(ml.locateInFrom(0, 0));
271     assertNull(ml.locateInFrom(1, 5));
272     assertNull(ml.locateInFrom(-1, 1));
273   }
274
275   /**
276    * Tests for method that locates ranges in the 'from' map for given range in
277    * the 'to' map.
278    */
279   @Test(groups = { "Functional" })
280   public void testLocateInFrom_withIntrons()
281   {
282     /*
283      * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
284      * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
285      */
286     int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
287     int[] protein = { 1, 4 };
288     MapList ml = new MapList(codons, protein, 3, 1);
289     assertEquals("[2, 3, 5, 5]", Arrays.toString(ml.locateInFrom(1, 1)));
290     assertEquals("[6, 7, 9, 9]", Arrays.toString(ml.locateInFrom(2, 2)));
291     assertEquals("[10, 10, 12, 12, 14, 14]",
292             Arrays.toString(ml.locateInFrom(3, 3)));
293     assertEquals("[16, 18]", Arrays.toString(ml.locateInFrom(4, 4)));
294   }
295
296   /**
297    * Tests for method that locates ranges in the 'to' map for given range in the
298    * 'from' map.
299    */
300   @Test(groups = { "Functional" })
301   public void testLocateInTo_noIntrons()
302   {
303     /*
304      * Simple mapping with no introns
305      */
306     int[] codons = new int[] { 1, 12 };
307     int[] protein = new int[] { 1, 4 };
308     MapList ml = new MapList(codons, protein, 3, 1);
309     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 3)));
310     assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
311     assertEquals("[3, 3]", Arrays.toString(ml.locateInTo(7, 9)));
312     assertEquals("[4, 4]", Arrays.toString(ml.locateInTo(10, 12)));
313     assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 6)));
314     assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(1, 9)));
315     assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(1, 12)));
316     assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
317     assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(4, 12)));
318
319     /*
320      * A part codon is treated as if a whole one.
321      */
322     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
323     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 2)));
324     assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 4)));
325     assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(2, 8)));
326     assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
327     assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(5, 11)));
328
329     assertNull(ml.locateInTo(0, 0));
330     assertNull(ml.locateInTo(1, 13));
331     assertNull(ml.locateInTo(-1, 1));
332   }
333
334   /**
335    * Tests for method that locates ranges in the 'to' map for given range in the
336    * 'from' map.
337    */
338   @Test(groups = { "Functional" })
339   public void testLocateInTo_withIntrons()
340   {
341     /*
342      * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
343      * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
344      */
345     int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
346     /*
347      * Mapped proteins at positions 1, 3, 4, 6 in the sequence
348      */
349     int[] protein = { 1, 1, 3, 4, 6, 6 };
350     MapList ml = new MapList(codons, protein, 3, 1);
351
352     /*
353      * Can't map from an unmapped position
354      */
355     assertNull(ml.locateInTo(1, 2));
356     assertNull(ml.locateInTo(2, 4));
357     assertNull(ml.locateInTo(4, 4));
358
359     /*
360      * Valid range or subrange of codon1 maps to protein1.
361      */
362     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 2)));
363     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 3)));
364     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 5)));
365     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 3)));
366     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 5)));
367
368     // codon position 6 starts the next protein:
369     assertEquals("[1, 1, 3, 3]", Arrays.toString(ml.locateInTo(3, 6)));
370
371     // codon positions 7 to 17 (part) cover proteins 2/3/4 at positions 3/4/6
372     assertEquals("[3, 4, 6, 6]", Arrays.toString(ml.locateInTo(7, 17)));
373
374   }
375
376   /**
377    * Test equals method.
378    */
379   @Test(groups = { "Functional" })
380   public void testEquals()
381   {
382     int[] codons = new int[] { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
383     int[] protein = new int[] { 1, 4 };
384     MapList ml = new MapList(codons, protein, 3, 1);
385     MapList ml1 = new MapList(codons, protein, 3, 1); // same values
386     MapList ml2 = new MapList(codons, protein, 2, 1); // fromRatio differs
387     MapList ml3 = new MapList(codons, protein, 3, 2); // toRatio differs
388     codons[2] = 4;
389     MapList ml6 = new MapList(codons, protein, 3, 1); // fromShifts differ
390     protein[1] = 3;
391     MapList ml7 = new MapList(codons, protein, 3, 1); // toShifts differ
392
393     assertTrue(ml.equals(ml));
394     assertTrue(ml.equals(ml1));
395     assertTrue(ml1.equals(ml));
396
397     assertFalse(ml.equals(null));
398     assertFalse(ml.equals("hello"));
399     assertFalse(ml.equals(ml2));
400     assertFalse(ml.equals(ml3));
401     assertFalse(ml.equals(ml6));
402     assertFalse(ml.equals(ml7));
403     assertFalse(ml6.equals(ml7));
404
405     try
406     {
407       MapList ml4 = new MapList(codons, null, 3, 1); // toShifts null
408       assertFalse(ml.equals(ml4));
409     } catch (NullPointerException e)
410     {
411       // actually thrown by constructor before equals can be called
412     }
413     try
414     {
415       MapList ml5 = new MapList(null, protein, 3, 1); // fromShifts null
416       assertFalse(ml.equals(ml5));
417     } catch (NullPointerException e)
418     {
419       // actually thrown by constructor before equals can be called
420     }
421   }
422
423   /**
424    * Test for the method that flattens a list of ranges into a single array.
425    */
426   @Test(groups = { "Functional" })
427   public void testGetRanges()
428   {
429     List<int[]> ranges = new ArrayList<int[]>();
430     ranges.add(new int[] { 2, 3 });
431     ranges.add(new int[] { 5, 6 });
432     assertEquals("[2, 3, 5, 6]", Arrays.toString(MapList.getRanges(ranges)));
433   }
434
435   /**
436    * Check state after construction
437    */
438   @Test(groups = { "Functional" })
439   public void testConstructor()
440   {
441     int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
442     int[] protein = { 1, 1, 3, 4, 6, 6 };
443     MapList ml = new MapList(codons, protein, 3, 1);
444     assertEquals(3, ml.getFromRatio());
445     assertEquals(2, ml.getFromLowest());
446     assertEquals(18, ml.getFromHighest());
447     assertEquals(1, ml.getToLowest());
448     assertEquals(6, ml.getToHighest());
449     assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
450             prettyPrint(ml.getFromRanges()));
451     assertEquals("{[1, 1], [3, 4], [6, 6]}", prettyPrint(ml.getToRanges()));
452
453     /*
454      * Also copy constructor
455      */
456     MapList ml2 = new MapList(ml);
457     assertEquals(3, ml2.getFromRatio());
458     assertEquals(2, ml2.getFromLowest());
459     assertEquals(18, ml2.getFromHighest());
460     assertEquals(1, ml2.getToLowest());
461     assertEquals(6, ml2.getToHighest());
462     assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
463             prettyPrint(ml2.getFromRanges()));
464     assertEquals("{[1, 1], [3, 4], [6, 6]}", prettyPrint(ml2.getToRanges()));
465
466     /*
467      * reverse direction
468      */
469     codons = new int[] { 9, 6 };
470     protein = new int[] { 100, 91, 80, 79 };
471     ml = new MapList(codons, protein, 3, 1);
472     assertEquals(6, ml.getFromLowest());
473     assertEquals(9, ml.getFromHighest());
474     assertEquals(79, ml.getToLowest());
475     assertEquals(100, ml.getToHighest());
476   }
477
478   /**
479    * Test constructor can merge consecutive ranges
480    */
481   @Test(groups = { "Functional" })
482   public void testConstructor_mergeRanges()
483   {
484     int[] codons = { 2, 3, 3, 7, 9, 10, 12, 12, 14, 14, 16, 17 };
485     int[] protein = { 1, 1, 1, 3, 6, 6 };
486     MapList ml = new MapList(codons, protein, 3, 1);
487     assertEquals(3, ml.getFromRatio());
488     assertEquals(2, ml.getFromLowest());
489     assertEquals(17, ml.getFromHighest());
490     assertEquals(1, ml.getToLowest());
491     assertEquals(6, ml.getToHighest());
492     assertEquals("{[2, 7], [9, 10], [12, 12], [14, 14], [16, 17]}",
493             prettyPrint(ml.getFromRanges()));
494     assertEquals("{[1, 3], [6, 6]}", prettyPrint(ml.getToRanges()));
495   }
496
497   /**
498    * Convert a List of {[i, j], [k, l], ...} to "[[i, j], [k, l], ...]"
499    * 
500    * @param ranges
501    * @return
502    */
503   private String prettyPrint(List<int[]> ranges)
504   {
505     StringBuilder sb = new StringBuilder(ranges.size() * 5);
506     boolean first = true;
507     sb.append("{");
508     for (int[] range : ranges)
509     {
510       if (!first)
511       {
512         sb.append(", ");
513       }
514       sb.append(Arrays.toString(range));
515       first = false;
516     }
517     sb.append("}");
518     return sb.toString();
519   }
520
521   /**
522    * Test the method that creates an inverse mapping
523    */
524   @Test(groups = { "Functional" })
525   public void testGetInverse()
526   {
527     int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
528     int[] protein = { 1, 1, 3, 4, 6, 6 };
529
530     MapList ml = new MapList(codons, protein, 3, 1);
531     MapList ml2 = ml.getInverse();
532     assertEquals(ml.getFromRatio(), ml2.getToRatio());
533     assertEquals(ml.getFromRatio(), ml2.getToRatio());
534     assertEquals(ml.getToHighest(), ml2.getFromHighest());
535     assertEquals(ml.getFromHighest(), ml2.getToHighest());
536     assertEquals(prettyPrint(ml.getFromRanges()),
537             prettyPrint(ml2.getToRanges()));
538     assertEquals(prettyPrint(ml.getToRanges()),
539             prettyPrint(ml2.getFromRanges()));
540   }
541
542   @Test(groups = { "Functional" })
543   public void testToString()
544   {
545     MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 }, new int[] {
546         51, 1 }, 1, 3);
547     String s = ml.toString();
548     assertEquals("[ [1, 5] [10, 15] [25, 20] ] 1:3 to [ [51, 1] ]", s);
549   }
550
551   @Test(groups = { "Functional" })
552   public void testAddMapList()
553   {
554     MapList ml = new MapList(new int[] { 11, 15, 20, 25, 35, 30 },
555             new int[] { 72, 22 }, 1, 3);
556     assertEquals(11, ml.getFromLowest());
557     assertEquals(35, ml.getFromHighest());
558     assertEquals(22, ml.getToLowest());
559     assertEquals(72, ml.getToHighest());
560
561     MapList ml2 = new MapList(new int[] { 2, 4, 37, 40 }, new int[] { 12,
562         17, 78, 83, 88, 96 }, 1, 3);
563     ml.addMapList(ml2);
564     assertEquals(2, ml.getFromLowest());
565     assertEquals(40, ml.getFromHighest());
566     assertEquals(12, ml.getToLowest());
567     assertEquals(96, ml.getToHighest());
568
569     String s = ml.toString();
570     assertEquals(
571             "[ [11, 15] [20, 25] [35, 30] [2, 4] [37, 40] ] 1:3 to [ [72, 22] [12, 17] [78, 83] [88, 96] ]",
572             s);
573   }
574
575   /**
576    * Test that confirms adding a map twice does nothing
577    */
578   @Test(groups = { "Functional" })
579   public void testAddMapList_sameMap()
580   {
581     MapList ml = new MapList(new int[] { 11, 15, 20, 25, 35, 30 },
582             new int[] { 72, 22 }, 1, 3);
583     String before = ml.toString();
584     ml.addMapList(ml);
585     assertEquals(before, ml.toString());
586     ml.addMapList(new MapList(ml));
587     assertEquals(before, ml.toString());
588   }
589
590   @Test(groups = { "Functional" })
591   public void testAddMapList_contiguous()
592   {
593     MapList ml = new MapList(new int[] { 11, 15 }, new int[] { 72, 58 }, 1,
594             3);
595
596     MapList ml2 = new MapList(new int[] { 15, 16 }, new int[] { 58, 53 },
597             1, 3);
598     ml.addMapList(ml2);
599     assertEquals("[ [11, 16] ] 1:3 to [ [72, 53] ]", ml.toString());
600   }
601
602   @Test(groups = "Functional")
603   public void testAddRange()
604   {
605     int[] range = { 1, 5 };
606     List<int[]> ranges = new ArrayList<int[]>();
607
608     // add to empty list:
609     MapList.addRange(range, ranges);
610     assertEquals(1, ranges.size());
611     assertSame(range, ranges.get(0));
612
613     // extend contiguous (same position):
614     MapList.addRange(new int[] { 5, 10 }, ranges);
615     assertEquals(1, ranges.size());
616     assertEquals(1, ranges.get(0)[0]);
617     assertEquals(10, ranges.get(0)[1]);
618
619     // extend contiguous (next position):
620     MapList.addRange(new int[] { 11, 15 }, ranges);
621     assertEquals(1, ranges.size());
622     assertEquals(1, ranges.get(0)[0]);
623     assertEquals(15, ranges.get(0)[1]);
624
625     // change direction: range is not merged:
626     MapList.addRange(new int[] { 16, 10 }, ranges);
627     assertEquals(2, ranges.size());
628     assertEquals(16, ranges.get(1)[0]);
629     assertEquals(10, ranges.get(1)[1]);
630
631     // extend reverse contiguous (same position):
632     MapList.addRange(new int[] { 10, 8 }, ranges);
633     assertEquals(2, ranges.size());
634     assertEquals(16, ranges.get(1)[0]);
635     assertEquals(8, ranges.get(1)[1]);
636
637     // extend reverse contiguous (next position):
638     MapList.addRange(new int[] { 7, 6 }, ranges);
639     assertEquals(2, ranges.size());
640     assertEquals(16, ranges.get(1)[0]);
641     assertEquals(6, ranges.get(1)[1]);
642
643     // change direction: range is not merged:
644     MapList.addRange(new int[] { 6, 9 }, ranges);
645     assertEquals(3, ranges.size());
646     assertEquals(6, ranges.get(2)[0]);
647     assertEquals(9, ranges.get(2)[1]);
648
649     // not contiguous: not merged
650     MapList.addRange(new int[] { 11, 12 }, ranges);
651     assertEquals(4, ranges.size());
652     assertEquals(11, ranges.get(3)[0]);
653     assertEquals(12, ranges.get(3)[1]);
654   }
655
656   /**
657    * Check state after construction
658    */
659   @Test(groups = { "Functional" })
660   public void testConstructor_withLists()
661   {
662     /*
663      * reverse direction
664      */
665     int[][] codons = new int[][] { { 9, 6 } };
666     int[][] protein = new int[][] { { 100, 91 }, { 80, 79 } };
667     MapList ml = new MapList(Arrays.asList(codons), Arrays.asList(protein),
668             3, 1);
669     assertEquals(6, ml.getFromLowest());
670     assertEquals(9, ml.getFromHighest());
671     assertEquals(79, ml.getToLowest());
672     assertEquals(100, ml.getToHighest());
673   }
674
675   /**
676    * Test that method that inspects for the (first) forward or reverse from
677    * range. Single position ranges are ignored.
678    */
679   @Test(groups = { "Functional" })
680   public void testIsFromForwardStrand()
681   {
682     // [3-9] declares forward strand
683     MapList ml = new MapList(new int[] { 2, 2, 3, 9, 12, 11 }, new int[] {
684         20, 11 }, 1, 1);
685     assertTrue(ml.isFromForwardStrand());
686
687     // [11-5] declares reverse strand ([13-14] is ignored)
688     ml = new MapList(new int[] { 2, 2, 11, 5, 13, 14 },
689             new int[] { 20, 11 }, 1, 1);
690     assertFalse(ml.isFromForwardStrand());
691
692     // all single position ranges - defaults to forward strand
693     ml = new MapList(new int[] { 2, 2, 4, 4, 6, 6 }, new int[] { 3, 1 }, 1,
694             1);
695     assertTrue(ml.isFromForwardStrand());
696   }
697
698   /**
699    * Test the method that merges a list of ranges where possible
700    */
701   @Test(groups = { "Functional" })
702   public void testCoalesceRanges()
703   {
704     assertNull(MapList.coalesceRanges(null));
705     List<int[]> ranges = new ArrayList<int[]>();
706     assertSame(ranges, MapList.coalesceRanges(ranges));
707     ranges.add(new int[] { 1, 3 });
708     assertSame(ranges, MapList.coalesceRanges(ranges));
709
710     // add non-contiguous range:
711     ranges.add(new int[] { 5, 6 });
712     assertSame(ranges, MapList.coalesceRanges(ranges));
713
714     // 'contiguous' range in opposite direction is not merged:
715     ranges.add(new int[] { 7, 6 });
716     assertSame(ranges, MapList.coalesceRanges(ranges));
717
718     // merging in forward direction:
719     ranges.clear();
720     ranges.add(new int[] { 1, 3 });
721     ranges.add(new int[] { 4, 5 });
722     ranges.add(new int[] { 5, 5 });
723     ranges.add(new int[] { 5, 7 });
724     List<int[]> merged = MapList.coalesceRanges(ranges);
725     assertEquals(1, merged.size());
726     assertArrayEquals(new int[] { 1, 7 }, merged.get(0));
727     // verify input list is unchanged
728     assertEquals(4, ranges.size());
729     assertArrayEquals(new int[] { 1, 3 }, ranges.get(0));
730     assertArrayEquals(new int[] { 4, 5 }, ranges.get(1));
731     assertArrayEquals(new int[] { 5, 5 }, ranges.get(2));
732     assertArrayEquals(new int[] { 5, 7 }, ranges.get(3));
733
734     // merging in reverse direction:
735     ranges.clear();
736     ranges.add(new int[] { 7, 5 });
737     ranges.add(new int[] { 5, 4 });
738     ranges.add(new int[] { 4, 4 });
739     ranges.add(new int[] { 3, 1 });
740     merged = MapList.coalesceRanges(ranges);
741     assertEquals(1, merged.size());
742     assertArrayEquals(new int[] { 7, 1 }, merged.get(0));
743
744     // merging with switches of direction:
745     ranges.clear();
746     ranges.add(new int[] { 1, 3 });
747     ranges.add(new int[] { 4, 5 });
748     ranges.add(new int[] { 5, 5 });
749     ranges.add(new int[] { 6, 6 });
750     ranges.add(new int[] { 12, 10 });
751     ranges.add(new int[] { 9, 8 });
752     ranges.add(new int[] { 8, 8 });
753     ranges.add(new int[] { 7, 7 });
754     merged = MapList.coalesceRanges(ranges);
755     assertEquals(2, merged.size());
756     assertArrayEquals(new int[] { 1, 6 }, merged.get(0));
757     assertArrayEquals(new int[] { 12, 7 }, merged.get(1));
758   }
759
760   /**
761    * Test the method that merges a list of ranges where possible
762    */
763   @Test(groups = { "Functional" })
764   public void testCoalesceRanges_withOverlap()
765   {
766     List<int[]> ranges = new ArrayList<int[]>();
767     ranges.add(new int[] { 1, 3 });
768     ranges.add(new int[] { 2, 5 });
769
770     /*
771      * [2, 5] should extend [1, 3]
772      */
773     List<int[]> merged = MapList.coalesceRanges(ranges);
774     assertEquals(1, merged.size());
775     assertArrayEquals(new int[] { 1, 5 }, merged.get(0));
776
777     /*
778      * a subsumed interval should be dropped
779      */
780     ranges.clear();
781     ranges.add(new int[] { 1, 6 });
782     ranges.add(new int[] { 2, 4 });
783     merged = MapList.coalesceRanges(ranges);
784     assertEquals(1, merged.size());
785     assertArrayEquals(new int[] { 1, 6 }, merged.get(0));
786
787     ranges.clear();
788     ranges.add(new int[] { 1, 5 });
789     ranges.add(new int[] { 1, 6 });
790     merged = MapList.coalesceRanges(ranges);
791     assertEquals(1, merged.size());
792     assertArrayEquals(new int[] { 1, 6 }, merged.get(0));
793
794     /*
795      * merge duplicate ranges
796      */
797     ranges.clear();
798     ranges.add(new int[] { 1, 3 });
799     ranges.add(new int[] { 1, 3 });
800     merged = MapList.coalesceRanges(ranges);
801     assertEquals(1, merged.size());
802     assertArrayEquals(new int[] { 1, 3 }, merged.get(0));
803
804     /*
805      * reverse direction
806      */
807     ranges.clear();
808     ranges.add(new int[] { 9, 5 });
809     ranges.add(new int[] { 9, 4 });
810     ranges.add(new int[] { 8, 3 });
811     ranges.add(new int[] { 3, 2 });
812     ranges.add(new int[] { 1, 0 });
813     merged = MapList.coalesceRanges(ranges);
814     assertEquals(1, merged.size());
815     assertArrayEquals(new int[] { 9, 0 }, merged.get(0));
816   }
817
818   /**
819    * Test the method that compounds ('traverses') two mappings
820    */
821   @Test
822   public void testTraverse()
823   {
824     /*
825      * simple 1:1 plus 1:1 forwards
826      */
827     MapList ml1 = new MapList(new int[] { 3, 4, 8, 12 }, new int[] { 5, 8,
828         11, 13 }, 1, 1);
829     MapList ml2 = new MapList(new int[] { 1, 50 }, new int[] { 40, 45, 70,
830         75, 90, 127 }, 1, 1);
831     MapList compound = ml1.traverse(ml2);
832
833     assertEquals(compound.getFromRatio(), 1);
834     assertEquals(compound.getToRatio(), 1);
835     List<int[]> fromRanges = compound.getFromRanges();
836     assertEquals(fromRanges.size(), 2);
837     assertArrayEquals(new int[] { 3, 4 }, fromRanges.get(0));
838     assertArrayEquals(new int[] { 8, 12 }, fromRanges.get(1));
839     List<int[]> toRanges = compound.getToRanges();
840     assertEquals(toRanges.size(), 2);
841     // 5-8 maps to 44-45,70-71
842     // 11-13 maps to 74-75,90
843     assertArrayEquals(new int[] { 44, 45, 70, 71 }, toRanges.get(0));
844     assertArrayEquals(new int[] { 74, 75, 90, 90 }, toRanges.get(1));
845
846     /*
847      * 1:1 over 1:1 backwards ('reverse strand')
848      */
849     ml1 = new MapList(new int[] { 1, 50 }, new int[] { 70, 119 }, 1, 1);
850     ml2 = new MapList(new int[] { 1, 500 },
851             new int[] { 1000, 901, 600, 201 }, 1, 1);
852     compound = ml1.traverse(ml2);
853
854     assertEquals(compound.getFromRatio(), 1);
855     assertEquals(compound.getToRatio(), 1);
856     fromRanges = compound.getFromRanges();
857     assertEquals(fromRanges.size(), 1);
858     assertArrayEquals(new int[] { 1, 50 }, fromRanges.get(0));
859     toRanges = compound.getToRanges();
860     assertEquals(toRanges.size(), 1);
861     assertArrayEquals(new int[] { 931, 901, 600, 582 }, toRanges.get(0));
862
863     /*
864      * 1:1 plus 1:3 should result in 1:3
865      */
866     ml1 = new MapList(new int[] { 1, 30 }, new int[] { 11, 40 }, 1, 1);
867     ml2 = new MapList(new int[] { 1, 100 }, new int[] { 1, 50, 91, 340 },
868             1, 3);
869     compound = ml1.traverse(ml2);
870
871     assertEquals(compound.getFromRatio(), 1);
872     assertEquals(compound.getToRatio(), 3);
873     fromRanges = compound.getFromRanges();
874     assertEquals(fromRanges.size(), 1);
875     assertArrayEquals(new int[] { 1, 30 }, fromRanges.get(0));
876     // 11-40 maps to 31-50,91-160
877     toRanges = compound.getToRanges();
878     assertEquals(toRanges.size(), 1);
879     assertArrayEquals(new int[] { 31, 50, 91, 160 }, toRanges.get(0));
880
881     /*
882      * 3:1 plus 1:1 should result in 3:1
883      */
884     ml1 = new MapList(new int[] { 1, 30 }, new int[] { 11, 20 }, 3, 1);
885     ml2 = new MapList(new int[] { 1, 100 }, new int[] { 1, 15, 91, 175 },
886             1, 1);
887     compound = ml1.traverse(ml2);
888
889     assertEquals(compound.getFromRatio(), 3);
890     assertEquals(compound.getToRatio(), 1);
891     fromRanges = compound.getFromRanges();
892     assertEquals(fromRanges.size(), 1);
893     assertArrayEquals(new int[] { 1, 30 }, fromRanges.get(0));
894     // 11-20 maps to 11-15, 91-95
895     toRanges = compound.getToRanges();
896     assertEquals(toRanges.size(), 1);
897     assertArrayEquals(new int[] { 11, 15, 91, 95 }, toRanges.get(0));
898
899     /*
900      * 1:3 plus 3:1 should result in 1:1
901      */
902     ml1 = new MapList(new int[] { 21, 40 }, new int[] { 13, 72 }, 1, 3);
903     ml2 = new MapList(new int[] { 1, 300 }, new int[] { 51, 70, 121, 200 },
904             3, 1);
905     compound = ml1.traverse(ml2);
906
907     assertEquals(compound.getFromRatio(), 1);
908     assertEquals(compound.getToRatio(), 1);
909     fromRanges = compound.getFromRanges();
910     assertEquals(fromRanges.size(), 1);
911     assertArrayEquals(new int[] { 21, 40 }, fromRanges.get(0));
912     // 13-72 maps 3:1 to 55-70, 121-124
913     toRanges = compound.getToRanges();
914     assertEquals(toRanges.size(), 1);
915     assertArrayEquals(new int[] { 55, 70, 121, 124 }, toRanges.get(0));
916
917     /*
918      * 3:1 plus 1:3 should result in 1:1
919      */
920     ml1 = new MapList(new int[] { 31, 90 }, new int[] { 13, 32 }, 3, 1);
921     ml2 = new MapList(new int[] { 11, 40 }, new int[] { 41, 50, 71, 150 },
922             1, 3);
923     compound = ml1.traverse(ml2);
924
925     assertEquals(compound.getFromRatio(), 1);
926     assertEquals(compound.getToRatio(), 1);
927     fromRanges = compound.getFromRanges();
928     assertEquals(fromRanges.size(), 1);
929     assertArrayEquals(new int[] { 31, 90 }, fromRanges.get(0));
930     // 13-32 maps to 47-50,71-126
931     toRanges = compound.getToRanges();
932     assertEquals(toRanges.size(), 1);
933     assertArrayEquals(new int[] { 47, 50, 71, 126 }, toRanges.get(0));
934
935     /*
936      * method returns null if not all regions are mapped through
937      */
938     ml1 = new MapList(new int[] { 1, 50 }, new int[] { 101, 150 }, 1, 1);
939     ml2 = new MapList(new int[] { 131, 180 }, new int[] { 201, 250 }, 1, 3);
940     compound = ml1.traverse(ml2);
941     assertNull(compound);
942   }
943 }