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