JAL-3761 unit test for MapList.countPos()
[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" })
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     assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 4)));
275     assertEquals("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3)));
276     assertEquals("[4, 12]", Arrays.toString(ml.locateInFrom(2, 4)));
277     assertEquals("[7, 12]", Arrays.toString(ml.locateInFrom(3, 4)));
278     assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
279
280     assertNull(ml.locateInFrom(0, 0));
281     assertNull(ml.locateInFrom(1, 5));
282     assertNull(ml.locateInFrom(-1, 1));
283   }
284
285   /**
286    * Tests for method that locates ranges in the 'from' map for given range in
287    * the 'to' map.
288    */
289   @Test(groups = { "Functional" })
290   public void testLocateInFrom_withIntrons()
291   {
292     /*
293      * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
294      * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
295      */
296     int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
297     int[] protein = { 1, 4 };
298     MapList ml = new MapList(codons, protein, 3, 1);
299     assertEquals("[2, 3, 5, 5]", Arrays.toString(ml.locateInFrom(1, 1)));
300     assertEquals("[6, 7, 9, 9]", Arrays.toString(ml.locateInFrom(2, 2)));
301     assertEquals("[10, 10, 12, 12, 14, 14]",
302             Arrays.toString(ml.locateInFrom(3, 3)));
303     assertEquals("[16, 18]", Arrays.toString(ml.locateInFrom(4, 4)));
304   }
305
306   /**
307    * Tests for method that locates ranges in the 'to' map for given range in the
308    * 'from' map.
309    */
310   @Test(groups = { "Functional" })
311   public void testLocateInTo_noIntrons()
312   {
313     /*
314      * Simple mapping with no introns
315      */
316     int[] codons = new int[] { 1, 12 };
317     int[] protein = new int[] { 1, 4 };
318     MapList ml = new MapList(codons, protein, 3, 1);
319     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 3)));
320     assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
321     assertEquals("[3, 3]", Arrays.toString(ml.locateInTo(7, 9)));
322     assertEquals("[4, 4]", Arrays.toString(ml.locateInTo(10, 12)));
323     assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 6)));
324     assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(1, 9)));
325     assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(1, 12)));
326     assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
327     assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(4, 12)));
328
329     /*
330      * A part codon is treated as if a whole one.
331      */
332     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
333     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 2)));
334     assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 4)));
335     assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(2, 8)));
336     assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
337     assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(5, 11)));
338
339     assertNull(ml.locateInTo(0, 0));
340     assertNull(ml.locateInTo(1, 13));
341     assertNull(ml.locateInTo(-1, 1));
342   }
343
344   /**
345    * Tests for method that locates ranges in the 'to' map for given range in the
346    * 'from' map.
347    */
348   @Test(groups = { "Functional" })
349   public void testLocateInTo_withIntrons()
350   {
351     /*
352      * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
353      * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
354      */
355     int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
356     /*
357      * Mapped proteins at positions 1, 3, 4, 6 in the sequence
358      */
359     int[] protein = { 1, 1, 3, 4, 6, 6 };
360     MapList ml = new MapList(codons, protein, 3, 1);
361
362     /*
363      * Can't map from an unmapped position
364      */
365     assertNull(ml.locateInTo(1, 2));
366     assertNull(ml.locateInTo(2, 4));
367     assertNull(ml.locateInTo(4, 4));
368
369     /*
370      * Valid range or subrange of codon1 maps to protein1.
371      */
372     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 2)));
373     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 3)));
374     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 5)));
375     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 3)));
376     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 5)));
377
378     // codon position 6 starts the next protein:
379     assertEquals("[1, 1, 3, 3]", Arrays.toString(ml.locateInTo(3, 6)));
380
381     // codon positions 7 to 17 (part) cover proteins 2/3/4 at positions 3/4/6
382     assertEquals("[3, 4, 6, 6]", Arrays.toString(ml.locateInTo(7, 17)));
383
384   }
385
386   /**
387    * Test equals method.
388    */
389   @Test(groups = { "Functional" })
390   public void testEquals()
391   {
392     int[] codons = new int[] { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
393     int[] protein = new int[] { 1, 4 };
394     MapList ml = new MapList(codons, protein, 3, 1);
395     MapList ml1 = new MapList(codons, protein, 3, 1); // same values
396     MapList ml2 = new MapList(codons, protein, 2, 1); // fromRatio differs
397     MapList ml3 = new MapList(codons, protein, 3, 2); // toRatio differs
398     codons[2] = 4;
399     MapList ml6 = new MapList(codons, protein, 3, 1); // fromShifts differ
400     protein[1] = 3;
401     MapList ml7 = new MapList(codons, protein, 3, 1); // toShifts differ
402
403     assertTrue(ml.equals(ml));
404     assertEquals(ml.hashCode(), ml.hashCode());
405     assertTrue(ml.equals(ml1));
406     assertEquals(ml.hashCode(), ml1.hashCode());
407     assertTrue(ml1.equals(ml));
408
409     assertFalse(ml.equals(null));
410     assertFalse(ml.equals("hello"));
411     assertFalse(ml.equals(ml2));
412     assertFalse(ml.equals(ml3));
413     assertFalse(ml.equals(ml6));
414     assertFalse(ml.equals(ml7));
415     assertFalse(ml6.equals(ml7));
416
417     try
418     {
419       MapList ml4 = new MapList(codons, null, 3, 1); // toShifts null
420       assertFalse(ml.equals(ml4));
421     } catch (NullPointerException e)
422     {
423       // actually thrown by constructor before equals can be called
424     }
425     try
426     {
427       MapList ml5 = new MapList(null, protein, 3, 1); // fromShifts null
428       assertFalse(ml.equals(ml5));
429     } catch (NullPointerException e)
430     {
431       // actually thrown by constructor before equals can be called
432     }
433   }
434
435   /**
436    * Test for the method that flattens a list of ranges into a single array.
437    */
438   @Test(groups = { "Functional" })
439   public void testGetRanges()
440   {
441     List<int[]> ranges = new ArrayList<>();
442     ranges.add(new int[] { 2, 3 });
443     ranges.add(new int[] { 5, 6 });
444     assertEquals("[2, 3, 5, 6]",
445             Arrays.toString(MapList.getRanges(ranges)));
446   }
447
448   /**
449    * Check state after construction
450    */
451   @Test(groups = { "Functional" })
452   public void testConstructor()
453   {
454     int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
455     int[] protein = { 1, 1, 3, 4, 6, 6 };
456     MapList ml = new MapList(codons, protein, 3, 1);
457     assertEquals(3, ml.getFromRatio());
458     assertEquals(2, ml.getFromLowest());
459     assertEquals(18, ml.getFromHighest());
460     assertEquals(1, ml.getToLowest());
461     assertEquals(6, ml.getToHighest());
462     assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
463             prettyPrint(ml.getFromRanges()));
464     assertEquals("{[1, 1], [3, 4], [6, 6]}", prettyPrint(ml.getToRanges()));
465
466     /*
467      * Also copy constructor
468      */
469     MapList ml2 = new MapList(ml);
470     assertEquals(3, ml2.getFromRatio());
471     assertEquals(2, ml2.getFromLowest());
472     assertEquals(18, ml2.getFromHighest());
473     assertEquals(1, ml2.getToLowest());
474     assertEquals(6, ml2.getToHighest());
475     assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
476             prettyPrint(ml2.getFromRanges()));
477     assertEquals("{[1, 1], [3, 4], [6, 6]}",
478             prettyPrint(ml2.getToRanges()));
479
480     /*
481      * reverse direction
482      */
483     codons = new int[] { 9, 6 };
484     protein = new int[] { 100, 91, 80, 79 };
485     ml = new MapList(codons, protein, 3, 1);
486     assertEquals(6, ml.getFromLowest());
487     assertEquals(9, ml.getFromHighest());
488     assertEquals(79, ml.getToLowest());
489     assertEquals(100, ml.getToHighest());
490   }
491
492   /**
493    * Test constructor used to merge consecutive ranges but now just leaves them
494    * as supplied (JAL-3751)
495    */
496   @Test(groups = { "Functional" })
497   public void testConstructor_mergeRanges()
498   {
499     int[] codons = { 2, 3, 3, 7, 9, 10, 12, 12, 13, 14, 16, 17 };
500     int[] protein = { 1, 1, 2, 3, 6, 6 };
501     MapList ml = new MapList(codons, protein, 3, 1);
502     assertEquals(3, ml.getFromRatio());
503     assertEquals(2, ml.getFromLowest());
504     assertEquals(17, ml.getFromHighest());
505     assertEquals(1, ml.getToLowest());
506     assertEquals(6, ml.getToHighest());
507     assertEquals("{[2, 3], [3, 7], [9, 10], [12, 12], [13, 14], [16, 17]}",
508             prettyPrint(ml.getFromRanges()));
509     assertEquals("{[1, 1], [2, 3], [6, 6]}", prettyPrint(ml.getToRanges()));
510   }
511
512   /**
513    * Convert a List of {[i, j], [k, l], ...} to "[[i, j], [k, l], ...]"
514    * 
515    * @param ranges
516    * @return
517    */
518   private String prettyPrint(List<int[]> ranges)
519   {
520     StringBuilder sb = new StringBuilder(ranges.size() * 5);
521     boolean first = true;
522     sb.append("{");
523     for (int[] range : ranges)
524     {
525       if (!first)
526       {
527         sb.append(", ");
528       }
529       sb.append(Arrays.toString(range));
530       first = false;
531     }
532     sb.append("}");
533     return sb.toString();
534   }
535
536   /**
537    * Test the method that creates an inverse mapping
538    */
539   @Test(groups = { "Functional" })
540   public void testGetInverse()
541   {
542     int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
543     int[] protein = { 1, 1, 3, 4, 6, 6 };
544
545     MapList ml = new MapList(codons, protein, 3, 1);
546     MapList ml2 = ml.getInverse();
547     assertEquals(ml.getFromRatio(), ml2.getToRatio());
548     assertEquals(ml.getFromRatio(), ml2.getToRatio());
549     assertEquals(ml.getToHighest(), ml2.getFromHighest());
550     assertEquals(ml.getFromHighest(), ml2.getToHighest());
551     assertEquals(prettyPrint(ml.getFromRanges()),
552             prettyPrint(ml2.getToRanges()));
553     assertEquals(prettyPrint(ml.getToRanges()),
554             prettyPrint(ml2.getFromRanges()));
555   }
556
557   @Test(groups = { "Functional" })
558   public void testToString()
559   {
560     MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 },
561             new int[]
562             { 51, 1 }, 1, 3);
563     String s = ml.toString();
564     assertEquals("[ [1, 5] [10, 15] [25, 20] ] 1:3 to [ [51, 1] ]", s);
565   }
566
567   @Test(groups = { "Functional" })
568   public void testAddMapList()
569   {
570     MapList ml = new MapList(new int[] { 11, 15, 20, 25, 35, 30 },
571             new int[]
572             { 72, 22 }, 1, 3);
573     assertEquals(11, ml.getFromLowest());
574     assertEquals(35, ml.getFromHighest());
575     assertEquals(22, ml.getToLowest());
576     assertEquals(72, ml.getToHighest());
577
578     MapList ml2 = new MapList(new int[] { 2, 4, 37, 40 },
579             new int[]
580             { 12, 17, 78, 83, 88, 96 }, 1, 3);
581     ml.addMapList(ml2);
582     assertEquals(2, ml.getFromLowest());
583     assertEquals(40, ml.getFromHighest());
584     assertEquals(12, ml.getToLowest());
585     assertEquals(96, ml.getToHighest());
586
587     String s = ml.toString();
588     assertEquals(
589             "[ [11, 15] [20, 25] [35, 30] [2, 4] [37, 40] ] 1:3 to [ [72, 22] [12, 17] [78, 83] [88, 96] ]",
590             s);
591   }
592
593   /**
594    * Test that confirms adding a map twice does nothing
595    */
596   @Test(groups = { "Functional" })
597   public void testAddMapList_sameMap()
598   {
599     MapList ml = new MapList(new int[] { 11, 15, 20, 25, 35, 30 },
600             new int[]
601             { 72, 22 }, 1, 3);
602     String before = ml.toString();
603     ml.addMapList(ml);
604     assertEquals(before, ml.toString());
605     ml.addMapList(new MapList(ml));
606     assertEquals(before, ml.toString());
607   }
608
609   @Test(groups = { "Functional" })
610   public void testAddMapList_contiguous()
611   {
612     MapList ml = new MapList(new int[] { 11, 15 }, new int[] { 72, 58 }, 1,
613             3);
614
615     MapList ml2 = new MapList(new int[] { 15, 16 }, new int[] { 58, 53 }, 1,
616             3);
617     ml.addMapList(ml2);
618     assertEquals("[ [11, 16] ] 1:3 to [ [72, 53] ]", ml.toString());
619   }
620
621   @Test(groups = "Functional")
622   public void testAddRange()
623   {
624     int[] range = { 1, 5 };
625     List<int[]> ranges = new ArrayList<>();
626
627     // add to empty list:
628     MapList.addRange(range, ranges);
629     assertEquals(1, ranges.size());
630     assertSame(range, ranges.get(0));
631
632     // extend contiguous (same position):
633     MapList.addRange(new int[] { 5, 10 }, ranges);
634     assertEquals(1, ranges.size());
635     assertEquals(1, ranges.get(0)[0]);
636     assertEquals(10, ranges.get(0)[1]);
637
638     // extend contiguous (next position):
639     MapList.addRange(new int[] { 11, 15 }, ranges);
640     assertEquals(1, ranges.size());
641     assertEquals(1, ranges.get(0)[0]);
642     assertEquals(15, ranges.get(0)[1]);
643
644     // change direction: range is not merged:
645     MapList.addRange(new int[] { 16, 10 }, ranges);
646     assertEquals(2, ranges.size());
647     assertEquals(16, ranges.get(1)[0]);
648     assertEquals(10, ranges.get(1)[1]);
649
650     // extend reverse contiguous (same position):
651     MapList.addRange(new int[] { 10, 8 }, ranges);
652     assertEquals(2, ranges.size());
653     assertEquals(16, ranges.get(1)[0]);
654     assertEquals(8, ranges.get(1)[1]);
655
656     // extend reverse contiguous (next position):
657     MapList.addRange(new int[] { 7, 6 }, ranges);
658     assertEquals(2, ranges.size());
659     assertEquals(16, ranges.get(1)[0]);
660     assertEquals(6, ranges.get(1)[1]);
661
662     // change direction: range is not merged:
663     MapList.addRange(new int[] { 6, 9 }, ranges);
664     assertEquals(3, ranges.size());
665     assertEquals(6, ranges.get(2)[0]);
666     assertEquals(9, ranges.get(2)[1]);
667
668     // not contiguous: not merged
669     MapList.addRange(new int[] { 11, 12 }, ranges);
670     assertEquals(4, ranges.size());
671     assertEquals(11, ranges.get(3)[0]);
672     assertEquals(12, ranges.get(3)[1]);
673   }
674
675   /**
676    * Check state after construction
677    */
678   @Test(groups = { "Functional" })
679   public void testConstructor_withLists()
680   {
681     /*
682      * reverse direction
683      */
684     int[][] codons = new int[][] { { 9, 6 } };
685     int[][] protein = new int[][] { { 100, 91 }, { 80, 79 } };
686     MapList ml = new MapList(Arrays.asList(codons), Arrays.asList(protein),
687             3, 1);
688     assertEquals(6, ml.getFromLowest());
689     assertEquals(9, ml.getFromHighest());
690     assertEquals(79, ml.getToLowest());
691     assertEquals(100, ml.getToHighest());
692   }
693
694   /**
695    * Test that method that inspects for the (first) forward or reverse from
696    * range. Single position ranges are ignored.
697    */
698   @Test(groups = { "Functional" })
699   public void testIsFromForwardStrand()
700   {
701     // [3-9] declares forward strand
702     MapList ml = new MapList(new int[] { 2, 2, 3, 9, 12, 11 },
703             new int[]
704             { 20, 11 }, 1, 1);
705     assertTrue(ml.isFromForwardStrand());
706
707     // [11-5] declares reverse strand ([13-14] is ignored)
708     ml = new MapList(new int[] { 2, 2, 11, 5, 13, 14 },
709             new int[]
710             { 20, 11 }, 1, 1);
711     assertFalse(ml.isFromForwardStrand());
712
713     // all single position ranges - defaults to forward strand
714     ml = new MapList(new int[] { 2, 2, 4, 4, 6, 6 }, new int[] { 3, 1 }, 1,
715             1);
716     assertTrue(ml.isFromForwardStrand());
717   }
718
719   /**
720    * Test the method that merges contiguous ranges
721    */
722   @Test(groups = { "Functional" })
723   public void testCoalesceRanges()
724   {
725     assertNull(MapList.coalesceRanges(null));
726     List<int[]> ranges = new ArrayList<>();
727     assertSame(ranges, MapList.coalesceRanges(ranges));
728     ranges.add(new int[] { 1, 3 });
729     assertSame(ranges, MapList.coalesceRanges(ranges));
730
731     // add non-contiguous range:
732     ranges.add(new int[] { 5, 6 });
733     assertSame(ranges, MapList.coalesceRanges(ranges));
734
735     // 'contiguous' range in opposite direction is not merged:
736     ranges.add(new int[] { 7, 6 });
737     assertSame(ranges, MapList.coalesceRanges(ranges));
738
739     // merging in forward direction:
740     ranges.clear();
741     ranges.add(new int[] { 1, 3 });
742     ranges.add(new int[] { 4, 5 }); // contiguous
743     ranges.add(new int[] { 5, 5 }); // overlap!
744     ranges.add(new int[] { 6, 7 }); // contiguous
745     List<int[]> merged = MapList.coalesceRanges(ranges);
746     assertEquals(2, merged.size());
747     assertArrayEquals(new int[] { 1, 5 }, merged.get(0));
748     assertArrayEquals(new int[] { 5, 7 }, merged.get(1));
749     // verify input list is unchanged
750     assertEquals(4, ranges.size());
751     assertArrayEquals(new int[] { 1, 3 }, ranges.get(0));
752     assertArrayEquals(new int[] { 4, 5 }, ranges.get(1));
753     assertArrayEquals(new int[] { 5, 5 }, ranges.get(2));
754     assertArrayEquals(new int[] { 6, 7 }, ranges.get(3));
755
756     // merging in reverse direction:
757     ranges.clear();
758     ranges.add(new int[] { 7, 5 });
759     ranges.add(new int[] { 5, 4 }); // overlap
760     ranges.add(new int[] { 4, 4 }); // overlap
761     ranges.add(new int[] { 3, 1 }); // contiguous
762     merged = MapList.coalesceRanges(ranges);
763     assertEquals(3, merged.size());
764     assertArrayEquals(new int[] { 7, 5 }, merged.get(0));
765     assertArrayEquals(new int[] { 5, 4 }, merged.get(1));
766     assertArrayEquals(new int[] { 4, 1 }, merged.get(2));
767
768     // merging with switches of direction:
769     ranges.clear();
770     ranges.add(new int[] { 1, 3 });
771     ranges.add(new int[] { 4, 5 }); // contiguous
772     ranges.add(new int[] { 5, 5 }); // overlap
773     ranges.add(new int[] { 6, 6 }); // contiguous
774     ranges.add(new int[] { 12, 10 });
775     ranges.add(new int[] { 9, 8 }); // contiguous
776     ranges.add(new int[] { 8, 8 }); // overlap
777     ranges.add(new int[] { 7, 7 }); // contiguous
778     merged = MapList.coalesceRanges(ranges);
779     assertEquals(4, merged.size());
780     assertArrayEquals(new int[] { 1, 5 }, merged.get(0));
781     assertArrayEquals(new int[] { 5, 6 }, merged.get(1));
782     assertArrayEquals(new int[] { 12, 8 }, merged.get(2));
783     assertArrayEquals(new int[] { 8, 7 }, merged.get(3));
784
785     // 'subsumed' ranges are preserved
786     ranges.clear();
787     ranges.add(new int[] { 10, 30 });
788     ranges.add(new int[] { 15, 25 });
789     merged = MapList.coalesceRanges(ranges);
790     assertEquals(2, merged.size());
791     assertArrayEquals(new int[] { 10, 30 }, merged.get(0));
792     assertArrayEquals(new int[] { 15, 25 }, merged.get(1));
793   }
794
795   /**
796    * Test the method that compounds ('traverses') two mappings
797    */
798   @Test(groups = "Functional")
799   public void testTraverse()
800   {
801     /*
802      * simple 1:1 plus 1:1 forwards
803      */
804     MapList ml1 = new MapList(new int[] { 3, 4, 8, 12 },
805             new int[]
806             { 5, 8, 11, 13 }, 1, 1);
807     assertEquals("{[3, 4], [8, 12]}", prettyPrint(ml1.getFromRanges()));
808     assertEquals("{[5, 8], [11, 13]}", prettyPrint(ml1.getToRanges()));
809
810     MapList ml2 = new MapList(new int[] { 1, 50 },
811             new int[]
812             { 40, 45, 70, 75, 90, 127 }, 1, 1);
813     assertEquals("{[1, 50]}", prettyPrint(ml2.getFromRanges()));
814     assertEquals("{[40, 45], [70, 75], [90, 127]}",
815             prettyPrint(ml2.getToRanges()));
816
817     MapList compound = ml1.traverse(ml2);
818
819     assertEquals(1, compound.getFromRatio());
820     assertEquals(1, compound.getToRatio());
821     List<int[]> fromRanges = compound.getFromRanges();
822     assertEquals(2, fromRanges.size());
823     assertArrayEquals(new int[] { 3, 4 }, fromRanges.get(0));
824     assertArrayEquals(new int[] { 8, 12 }, fromRanges.get(1));
825     List<int[]> toRanges = compound.getToRanges();
826     assertEquals(4, toRanges.size());
827     // 5-8 maps to 44-45,70-71
828     // 11-13 maps to 74-75,90
829     assertArrayEquals(new int[] { 44, 45 }, toRanges.get(0));
830     assertArrayEquals(new int[] { 70, 71 }, toRanges.get(1));
831     assertArrayEquals(new int[] { 74, 75 }, toRanges.get(2));
832     assertArrayEquals(new int[] { 90, 90 }, toRanges.get(3));
833
834     /*
835      * 1:1 over 1:1 backwards ('reverse strand')
836      */
837     ml1 = new MapList(new int[] { 1, 50 }, new int[] { 70, 119 }, 1, 1);
838     ml2 = new MapList(new int[] { 1, 500 },
839             new int[]
840             { 1000, 901, 600, 201 }, 1, 1);
841     compound = ml1.traverse(ml2);
842
843     assertEquals(1, compound.getFromRatio());
844     assertEquals(1, compound.getToRatio());
845     fromRanges = compound.getFromRanges();
846     assertEquals(1, fromRanges.size());
847     assertArrayEquals(new int[] { 1, 50 }, fromRanges.get(0));
848     toRanges = compound.getToRanges();
849     assertEquals(2, toRanges.size());
850     assertArrayEquals(new int[] { 931, 901 }, toRanges.get(0));
851     assertArrayEquals(new int[] { 600, 582 }, toRanges.get(1));
852
853     /*
854      * 1:1 plus 1:3 should result in 1:3
855      */
856     ml1 = new MapList(new int[] { 1, 30 }, new int[] { 11, 40 }, 1, 1);
857     ml2 = new MapList(new int[] { 1, 100 }, new int[] { 1, 50, 91, 340 }, 1,
858             3);
859     compound = ml1.traverse(ml2);
860
861     assertEquals(1, compound.getFromRatio());
862     assertEquals(3, compound.getToRatio());
863     fromRanges = compound.getFromRanges();
864     assertEquals(1, fromRanges.size());
865     assertArrayEquals(new int[] { 1, 30 }, fromRanges.get(0));
866     // 11-40 maps to 31-50,91-160
867     toRanges = compound.getToRanges();
868     assertEquals(2, toRanges.size());
869     assertArrayEquals(new int[] { 31, 50 }, toRanges.get(0));
870     assertArrayEquals(new int[] { 91, 160 }, toRanges.get(1));
871
872     /*
873      * 3:1 plus 1:1 should result in 3:1
874      */
875     ml1 = new MapList(new int[] { 1, 30 }, new int[] { 11, 20 }, 3, 1);
876     ml2 = new MapList(new int[] { 1, 100 }, new int[] { 1, 15, 91, 175 }, 1,
877             1);
878     compound = ml1.traverse(ml2);
879
880     assertEquals(3, compound.getFromRatio());
881     assertEquals(1, compound.getToRatio());
882     fromRanges = compound.getFromRanges();
883     assertEquals(1, fromRanges.size());
884     assertArrayEquals(new int[] { 1, 30 }, fromRanges.get(0));
885     // 11-20 maps to 11-15, 91-95
886     toRanges = compound.getToRanges();
887     assertEquals(2, toRanges.size());
888     assertArrayEquals(new int[] { 11, 15 }, toRanges.get(0));
889     assertArrayEquals(new int[] { 91, 95 }, toRanges.get(1));
890
891     /*
892      * 1:3 plus 3:1 should result in 1:1
893      */
894     ml1 = new MapList(new int[] { 21, 40 }, new int[] { 13, 72 }, 1, 3);
895     ml2 = new MapList(new int[] { 1, 300 }, new int[] { 51, 70, 121, 200 },
896             3, 1);
897     compound = ml1.traverse(ml2);
898
899     assertEquals(1, compound.getFromRatio());
900     assertEquals(1, compound.getToRatio());
901     fromRanges = compound.getFromRanges();
902     assertEquals(1, fromRanges.size());
903     assertArrayEquals(new int[] { 21, 40 }, fromRanges.get(0));
904     // 13-72 maps 3:1 to 55-70, 121-124
905     toRanges = compound.getToRanges();
906     assertEquals(2, toRanges.size());
907     assertArrayEquals(new int[] { 55, 70 }, toRanges.get(0));
908     assertArrayEquals(new int[] { 121, 124 }, toRanges.get(1));
909
910     /*
911      * 3:1 plus 1:3 should result in 1:1
912      */
913     ml1 = new MapList(new int[] { 31, 90 }, new int[] { 13, 32 }, 3, 1);
914     ml2 = new MapList(new int[] { 11, 40 }, new int[] { 41, 50, 71, 150 },
915             1, 3);
916     compound = ml1.traverse(ml2);
917
918     assertEquals(1, compound.getFromRatio());
919     assertEquals(1, compound.getToRatio());
920     fromRanges = compound.getFromRanges();
921     assertEquals(1, fromRanges.size());
922     assertArrayEquals(new int[] { 31, 90 }, fromRanges.get(0));
923     // 13-32 maps to 47-50,71-126
924     toRanges = compound.getToRanges();
925     assertEquals(2, toRanges.size());
926     assertArrayEquals(new int[] { 47, 50 }, toRanges.get(0));
927     assertArrayEquals(new int[] { 71, 126 }, toRanges.get(1));
928
929     /*
930      * method returns null if not all regions are mapped through
931      */
932     ml1 = new MapList(new int[] { 1, 50 }, new int[] { 101, 150 }, 1, 1);
933     ml2 = new MapList(new int[] { 131, 180 }, new int[] { 201, 250 }, 1, 3);
934     compound = ml1.traverse(ml2);
935     assertNull(compound);
936   }
937
938   /**
939    * Test that method that inspects for the (first) forward or reverse 'to'
940    * range. Single position ranges are ignored.
941    */
942   @Test(groups = { "Functional" })
943   public void testIsToForwardsStrand()
944   {
945     // [3-9] declares forward strand
946     MapList ml = new MapList(new int[] { 20, 11 },
947             new int[]
948             { 2, 2, 3, 9, 12, 11 }, 1, 1);
949     assertTrue(ml.isToForwardStrand());
950
951     // [11-5] declares reverse strand ([13-14] is ignored)
952     ml = new MapList(new int[] { 20, 11 },
953             new int[]
954             { 2, 2, 11, 5, 13, 14 }, 1, 1);
955     assertFalse(ml.isToForwardStrand());
956
957     // all single position ranges - defaults to forward strand
958     ml = new MapList(new int[] { 3, 1 }, new int[] { 2, 2, 4, 4, 6, 6 }, 1,
959             1);
960     assertTrue(ml.isToForwardStrand());
961   }
962
963   /**
964    * Test for mapping with overlapping ranges
965    */
966   @Test(groups = { "Functional" })
967   public void testLocateInFrom_withOverlap()
968   {
969     int[] codons = new int[] { 1, 12, 12, 17 };
970     int[] protein = new int[] { 1, 6 };
971     MapList ml = new MapList(codons, protein, 3, 1);
972     assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(1, 1)));
973     assertEquals("[4, 6]", Arrays.toString(ml.locateInFrom(2, 2)));
974     assertEquals("[7, 9]", Arrays.toString(ml.locateInFrom(3, 3)));
975     assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
976     assertEquals("[12, 14]", Arrays.toString(ml.locateInFrom(5, 5)));
977     assertEquals("[15, 17]", Arrays.toString(ml.locateInFrom(6, 6)));
978     assertEquals("[1, 6]", Arrays.toString(ml.locateInFrom(1, 2)));
979     assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(1, 3)));
980     assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 4)));
981     assertEquals("[1, 12, 12, 14]", Arrays.toString(ml.locateInFrom(1, 5)));
982     assertEquals("[1, 12, 12, 17]", Arrays.toString(ml.locateInFrom(1, 6)));
983     assertEquals("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3)));
984     assertEquals("[7, 12, 12, 17]", Arrays.toString(ml.locateInFrom(3, 6)));
985
986     assertNull(ml.locateInFrom(0, 0));
987     assertNull(ml.locateInFrom(1, 7));
988     assertNull(ml.locateInFrom(-1, 1));
989   }
990
991   /**
992    * Test for mapping with overlapping ranges
993    */
994   @Test(groups = { "Functional" })
995   public void testLocateInTo_withOverlap()
996   {
997     int[] codons = new int[] { 1, 12, 12, 17 };
998     int[] protein = new int[] { 1, 6 };
999     MapList ml = new MapList(codons, protein, 3, 1);
1000     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
1001     assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(3, 8)));
1002     assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(2, 11)));
1003     assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
1004
1005     // we want base 12 to map to both of the amino acids it codes for
1006     assertEquals("[4, 5]", Arrays.toString(ml.locateInTo(12, 12)));
1007     assertEquals("[4, 5]", Arrays.toString(ml.locateInTo(11, 12)));
1008     assertEquals("[4, 6]", Arrays.toString(ml.locateInTo(11, 15)));
1009     assertEquals("[6, 6]", Arrays.toString(ml.locateInTo(15, 17)));
1010
1011     assertNull(ml.locateInTo(0, 0));
1012     assertNull(ml.locateInTo(1, 18));
1013     assertNull(ml.locateInTo(-1, 1));
1014   }
1015
1016   @Test(groups = { "Functional" })
1017   public void testTraverseToPosition()
1018   {
1019     List<int[]> ranges = new ArrayList<>();
1020     assertNull(MapList.traverseToPosition(ranges, 0));
1021
1022     ranges.add(new int[] { 3, 6 });
1023     assertNull(MapList.traverseToPosition(ranges, 0));
1024   }
1025
1026   @Test(groups = { "Functional" })
1027   public void testCountPos()
1028   {
1029     try
1030     {
1031       MapList.countPos(null, 1);
1032       fail("expected exception");
1033     } catch (NullPointerException e)
1034     {
1035       // expected
1036     }
1037
1038     List<int[]> intervals = new ArrayList<>();
1039     assertNull(MapList.countPos(intervals,  1));
1040     
1041     /*
1042      * forward strand
1043      */
1044     intervals.add(new int[] {10, 20});
1045     assertNull(MapList.countPos(intervals,  9));
1046     assertNull(MapList.countPos(intervals,  21));
1047     assertArrayEquals(new int[] {1, 1}, MapList.countPos(intervals,  10));
1048     assertArrayEquals(new int[] {6, 1}, MapList.countPos(intervals,  15));
1049     assertArrayEquals(new int[] {11, 1}, MapList.countPos(intervals,  20));
1050
1051     intervals.add(new int[] {25, 25});
1052     assertArrayEquals(new int[] {12, 1}, MapList.countPos(intervals,  25));
1053
1054     // next interval repeats position 25 - which should be counted twice if traversed
1055     intervals.add(new int[] {25, 26});
1056     assertArrayEquals(new int[] {12, 1}, MapList.countPos(intervals,  25));
1057     assertArrayEquals(new int[] {14, 1}, MapList.countPos(intervals,  26));
1058     
1059     /*
1060      * reverse strand
1061      */
1062     intervals.clear();
1063     intervals.add(new int[] {5, -5});
1064     assertNull(MapList.countPos(intervals,  6));
1065     assertNull(MapList.countPos(intervals,  -6));
1066     assertArrayEquals(new int[] {1, -1}, MapList.countPos(intervals,  5));
1067     assertArrayEquals(new int[] {7, -1}, MapList.countPos(intervals,  -1));
1068     assertArrayEquals(new int[] {11, -1}, MapList.countPos(intervals,  -5));
1069     
1070     /*
1071      * reverse then forward
1072      */
1073     intervals.add(new int[] {5, 10});
1074     assertArrayEquals(new int[] {13, 1}, MapList.countPos(intervals,  6));
1075     
1076     /*
1077      * reverse then forward then reverse
1078      */
1079     intervals.add(new int[] {-10, -20});
1080     assertArrayEquals(new int[] {20, -1}, MapList.countPos(intervals,  -12));
1081     
1082     /*
1083      * an interval [x, x] is treated as forward
1084      */
1085     intervals.add(new int[] {30, 30});
1086     assertArrayEquals(new int[] {29, 1}, MapList.countPos(intervals,  30));
1087   }
1088 }