JAL-3725 helper methods for computing mapped feature range overlap
[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 the overlap of the ranges in the 'from' map
341    * for given range in the 'to' map
342    */
343   @Test(groups = { "Functional" })
344   public void testGetOverlapsInFrom_withIntrons()
345   {
346     /*
347      * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
348      * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
349      */
350     int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
351     int[] protein = { 11, 14 };
352     MapList ml = new MapList(codons, protein, 3, 1);
353
354     assertEquals("[2, 3, 5, 5]",
355             Arrays.toString(ml.getOverlapsInFrom(11, 11)));
356     assertEquals("[2, 3, 5, 7, 9, 9]",
357             Arrays.toString(ml.getOverlapsInFrom(11, 12)));
358     // out of range 5' :
359     assertEquals("[2, 3, 5, 7, 9, 9]",
360             Arrays.toString(ml.getOverlapsInFrom(8, 12)));
361     // out of range 3' :
362     assertEquals("[10, 10, 12, 12, 14, 14, 16, 18]",
363             Arrays.toString(ml.getOverlapsInFrom(13, 16)));
364     // out of range both :
365     assertEquals("[2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18]",
366             Arrays.toString(ml.getOverlapsInFrom(1, 16)));
367     // no overlap:
368     assertNull(ml.getOverlapsInFrom(20, 25));
369   }
370
371   /**
372    * Tests for method that locates the overlap of the ranges in the 'to' map for
373    * given range in the 'from' map
374    */
375   @Test(groups = { "Functional" })
376   public void testGetOverlapsInTo_withIntrons()
377   {
378     /*
379      * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [17, 18, 19] i.e.
380      * 2-3, 5-7, 9-10, 12-12, 14-14, 17-19
381      */
382     int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 17, 19 };
383     /*
384      * Mapped proteins at positions 1, 3, 4, 6 in the sequence
385      */
386     int[] protein = { 1, 1, 3, 4, 6, 6 };
387     MapList ml = new MapList(codons, protein, 3, 1);
388
389     /*
390      * Can't map from an unmapped position
391      */
392     assertNull(ml.getOverlapsInTo(1, 1));
393     assertNull(ml.getOverlapsInTo(4, 4));
394     assertNull(ml.getOverlapsInTo(15, 16));
395
396     /*
397      * nor from a range that includes no mapped position (exon)
398      */
399     assertNull(ml.getOverlapsInTo(15, 16));
400
401     // end of codon 1 maps to first peptide
402     assertEquals("[1, 1]", Arrays.toString(ml.getOverlapsInTo(2, 2)));
403     // end of codon 1 and start of codon 2 maps to first 2 peptides
404     assertEquals("[1, 1, 3, 3]", Arrays.toString(ml.getOverlapsInTo(3, 7)));
405
406     // range overlaps 5' end of dna:
407     assertEquals("[1, 1, 3, 3]", Arrays.toString(ml.getOverlapsInTo(1, 6)));
408     assertEquals("[1, 1, 3, 3]", Arrays.toString(ml.getOverlapsInTo(1, 8)));
409
410     // range overlaps 3' end of dna:
411     assertEquals("[6, 6]", Arrays.toString(ml.getOverlapsInTo(17, 24)));
412     assertEquals("[6, 6]", Arrays.toString(ml.getOverlapsInTo(16, 24)));
413
414     // dna positions 8, 11 are intron but include end of exon 2 and start of
415     // exon 3
416     assertEquals("[3, 4]", Arrays.toString(ml.getOverlapsInTo(8, 11)));
417   }
418
419   /**
420    * Tests for method that locates ranges in the 'to' map for given range in the
421    * 'from' map.
422    */
423   @Test(groups = { "Functional" })
424   public void testLocateInTo_noIntrons()
425   {
426     /*
427      * Simple mapping with no introns
428      */
429     int[] codons = new int[] { 1, 12 };
430     int[] protein = new int[] { 1, 4 };
431     MapList ml = new MapList(codons, protein, 3, 1);
432     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 3)));
433     assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
434     assertEquals("[3, 3]", Arrays.toString(ml.locateInTo(7, 9)));
435     assertEquals("[4, 4]", Arrays.toString(ml.locateInTo(10, 12)));
436     assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 6)));
437     assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(1, 9)));
438     assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(1, 12)));
439     assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
440     assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(4, 12)));
441     // reverse range treated as if forwards:
442     assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(12, 4)));
443
444     /*
445      * A part codon is treated as if a whole one.
446      */
447     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
448     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 2)));
449     assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 4)));
450     assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(2, 8)));
451     assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
452     assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(5, 11)));
453
454     /*
455      * partial overlap
456      */
457     assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(1, 13)));
458     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(-1, 2)));
459
460     /*
461      * no overlap
462      */
463     assertNull(ml.locateInTo(0, 0));
464   }
465
466   /**
467    * Tests for method that locates ranges in the 'to' map for given range in the
468    * 'from' map.
469    */
470   @Test(groups = { "Functional" })
471   public void testLocateInTo_withIntrons()
472   {
473     /*
474      * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
475      * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
476      */
477     int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
478     /*
479      * Mapped proteins at positions 1, 3, 4, 6 in the sequence
480      */
481     int[] protein = { 1, 1, 3, 4, 6, 6 };
482     MapList ml = new MapList(codons, protein, 3, 1);
483
484     /*
485      * Valid range or subrange of codon1 maps to protein1
486      */
487     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 2)));
488     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 3)));
489     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 5)));
490     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 3)));
491     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 5)));
492
493     // codon position 6 starts the next protein:
494     assertEquals("[1, 1, 3, 3]", Arrays.toString(ml.locateInTo(3, 6)));
495
496     // codon positions 7 to 17 (part) cover proteins 2/3/4 at positions 3/4/6
497     assertEquals("[3, 4, 6, 6]", Arrays.toString(ml.locateInTo(7, 17)));
498
499     /*
500      * partial overlap
501      */
502     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 2)));
503     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 4)));
504     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 4)));
505
506     /*
507      * no overlap
508      */
509     assertNull(ml.locateInTo(4, 4));
510   }
511
512   /**
513    * Test equals method.
514    */
515   @Test(groups = { "Functional" })
516   public void testEquals()
517   {
518     int[] codons = new int[] { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
519     int[] protein = new int[] { 1, 4 };
520     MapList ml = new MapList(codons, protein, 3, 1);
521     MapList ml1 = new MapList(codons, protein, 3, 1); // same values
522     MapList ml2 = new MapList(codons, protein, 2, 1); // fromRatio differs
523     MapList ml3 = new MapList(codons, protein, 3, 2); // toRatio differs
524     codons[2] = 4;
525     MapList ml6 = new MapList(codons, protein, 3, 1); // fromShifts differ
526     protein[1] = 3;
527     MapList ml7 = new MapList(codons, protein, 3, 1); // toShifts differ
528
529     assertTrue(ml.equals(ml));
530     assertEquals(ml.hashCode(), ml.hashCode());
531     assertTrue(ml.equals(ml1));
532     assertEquals(ml.hashCode(), ml1.hashCode());
533     assertTrue(ml1.equals(ml));
534
535     assertFalse(ml.equals(null));
536     assertFalse(ml.equals("hello"));
537     assertFalse(ml.equals(ml2));
538     assertFalse(ml.equals(ml3));
539     assertFalse(ml.equals(ml6));
540     assertFalse(ml.equals(ml7));
541     assertFalse(ml6.equals(ml7));
542
543     try
544     {
545       MapList ml4 = new MapList(codons, null, 3, 1); // toShifts null
546       assertFalse(ml.equals(ml4));
547     } catch (NullPointerException e)
548     {
549       // actually thrown by constructor before equals can be called
550     }
551     try
552     {
553       MapList ml5 = new MapList(null, protein, 3, 1); // fromShifts null
554       assertFalse(ml.equals(ml5));
555     } catch (NullPointerException e)
556     {
557       // actually thrown by constructor before equals can be called
558     }
559   }
560
561   /**
562    * Test for the method that flattens a list of ranges into a single array.
563    */
564   @Test(groups = { "Functional" })
565   public void testGetRanges()
566   {
567     List<int[]> ranges = new ArrayList<>();
568     ranges.add(new int[] { 2, 3 });
569     ranges.add(new int[] { 5, 6 });
570     assertEquals("[2, 3, 5, 6]",
571             Arrays.toString(MapList.getRanges(ranges)));
572   }
573
574   /**
575    * Check state after construction
576    */
577   @Test(groups = { "Functional" })
578   public void testConstructor()
579   {
580     int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
581     int[] protein = { 1, 1, 3, 4, 6, 6 };
582     MapList ml = new MapList(codons, protein, 3, 1);
583     assertEquals(3, ml.getFromRatio());
584     assertEquals(2, ml.getFromLowest());
585     assertEquals(18, ml.getFromHighest());
586     assertEquals(1, ml.getToLowest());
587     assertEquals(6, ml.getToHighest());
588     assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
589             prettyPrint(ml.getFromRanges()));
590     assertEquals("{[1, 1], [3, 4], [6, 6]}", prettyPrint(ml.getToRanges()));
591
592     /*
593      * Also copy constructor
594      */
595     MapList ml2 = new MapList(ml);
596     assertEquals(3, ml2.getFromRatio());
597     assertEquals(2, ml2.getFromLowest());
598     assertEquals(18, ml2.getFromHighest());
599     assertEquals(1, ml2.getToLowest());
600     assertEquals(6, ml2.getToHighest());
601     assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
602             prettyPrint(ml2.getFromRanges()));
603     assertEquals("{[1, 1], [3, 4], [6, 6]}",
604             prettyPrint(ml2.getToRanges()));
605
606     /*
607      * reverse direction
608      */
609     codons = new int[] { 9, 6 };
610     protein = new int[] { 100, 91, 80, 79 };
611     ml = new MapList(codons, protein, 3, 1);
612     assertEquals(6, ml.getFromLowest());
613     assertEquals(9, ml.getFromHighest());
614     assertEquals(79, ml.getToLowest());
615     assertEquals(100, ml.getToHighest());
616   }
617
618   /**
619    * Test constructor used to merge consecutive ranges but now just leaves them
620    * as supplied (JAL-3751)
621    */
622   @Test(groups = { "Functional" })
623   public void testConstructor_mergeRanges()
624   {
625     int[] codons = { 2, 3, 3, 7, 9, 10, 12, 12, 13, 14, 16, 17 };
626     int[] protein = { 1, 1, 2, 3, 6, 6 };
627     MapList ml = new MapList(codons, protein, 3, 1);
628     assertEquals(3, ml.getFromRatio());
629     assertEquals(2, ml.getFromLowest());
630     assertEquals(17, ml.getFromHighest());
631     assertEquals(1, ml.getToLowest());
632     assertEquals(6, ml.getToHighest());
633     assertEquals("{[2, 3], [3, 7], [9, 10], [12, 12], [13, 14], [16, 17]}",
634             prettyPrint(ml.getFromRanges()));
635     assertEquals("{[1, 1], [2, 3], [6, 6]}", prettyPrint(ml.getToRanges()));
636   }
637
638   /**
639    * Convert a List of {[i, j], [k, l], ...} to "[[i, j], [k, l], ...]"
640    * 
641    * @param ranges
642    * @return
643    */
644   private String prettyPrint(List<int[]> ranges)
645   {
646     StringBuilder sb = new StringBuilder(ranges.size() * 5);
647     boolean first = true;
648     sb.append("{");
649     for (int[] range : ranges)
650     {
651       if (!first)
652       {
653         sb.append(", ");
654       }
655       sb.append(Arrays.toString(range));
656       first = false;
657     }
658     sb.append("}");
659     return sb.toString();
660   }
661
662   /**
663    * Test the method that creates an inverse mapping
664    */
665   @Test(groups = { "Functional" })
666   public void testGetInverse()
667   {
668     int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
669     int[] protein = { 1, 1, 3, 4, 6, 6 };
670
671     MapList ml = new MapList(codons, protein, 3, 1);
672     MapList ml2 = ml.getInverse();
673     assertEquals(ml.getFromRatio(), ml2.getToRatio());
674     assertEquals(ml.getFromRatio(), ml2.getToRatio());
675     assertEquals(ml.getToHighest(), ml2.getFromHighest());
676     assertEquals(ml.getFromHighest(), ml2.getToHighest());
677     assertEquals(prettyPrint(ml.getFromRanges()),
678             prettyPrint(ml2.getToRanges()));
679     assertEquals(prettyPrint(ml.getToRanges()),
680             prettyPrint(ml2.getFromRanges()));
681   }
682
683   @Test(groups = { "Functional" })
684   public void testToString()
685   {
686     MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 },
687             new int[]
688             { 51, 1 }, 1, 3);
689     String s = ml.toString();
690     assertEquals("[ [1, 5] [10, 15] [25, 20] ] 1:3 to [ [51, 1] ]", s);
691   }
692
693   @Test(groups = { "Functional" })
694   public void testAddMapList()
695   {
696     MapList ml = new MapList(new int[] { 11, 15, 20, 25, 35, 30 },
697             new int[]
698             { 72, 22 }, 1, 3);
699     assertEquals(11, ml.getFromLowest());
700     assertEquals(35, ml.getFromHighest());
701     assertEquals(22, ml.getToLowest());
702     assertEquals(72, ml.getToHighest());
703
704     MapList ml2 = new MapList(new int[] { 2, 4, 37, 40 },
705             new int[]
706             { 12, 17, 78, 83, 88, 96 }, 1, 3);
707     ml.addMapList(ml2);
708     assertEquals(2, ml.getFromLowest());
709     assertEquals(40, ml.getFromHighest());
710     assertEquals(12, ml.getToLowest());
711     assertEquals(96, ml.getToHighest());
712
713     String s = ml.toString();
714     assertEquals(
715             "[ [11, 15] [20, 25] [35, 30] [2, 4] [37, 40] ] 1:3 to [ [72, 22] [12, 17] [78, 83] [88, 96] ]",
716             s);
717   }
718
719   /**
720    * Test that confirms adding a map twice does nothing
721    */
722   @Test(groups = { "Functional" })
723   public void testAddMapList_sameMap()
724   {
725     MapList ml = new MapList(new int[] { 11, 15, 20, 25, 35, 30 },
726             new int[]
727             { 72, 22 }, 1, 3);
728     String before = ml.toString();
729     ml.addMapList(ml);
730     assertEquals(before, ml.toString());
731     ml.addMapList(new MapList(ml));
732     assertEquals(before, ml.toString());
733   }
734
735   @Test(groups = { "Functional" })
736   public void testAddMapList_contiguous()
737   {
738     MapList ml = new MapList(new int[] { 11, 15 }, new int[] { 72, 58 }, 1,
739             3);
740
741     MapList ml2 = new MapList(new int[] { 15, 16 }, new int[] { 58, 53 }, 1,
742             3);
743     ml.addMapList(ml2);
744     assertEquals("[ [11, 16] ] 1:3 to [ [72, 53] ]", ml.toString());
745   }
746
747   @Test(groups = "Functional")
748   public void testAddRange()
749   {
750     int[] range = { 1, 5 };
751     List<int[]> ranges = new ArrayList<>();
752
753     // add to empty list:
754     MapList.addRange(range, ranges);
755     assertEquals(1, ranges.size());
756     assertSame(range, ranges.get(0));
757
758     // extend contiguous (same position):
759     MapList.addRange(new int[] { 5, 10 }, ranges);
760     assertEquals(1, ranges.size());
761     assertEquals(1, ranges.get(0)[0]);
762     assertEquals(10, ranges.get(0)[1]);
763
764     // extend contiguous (next position):
765     MapList.addRange(new int[] { 11, 15 }, ranges);
766     assertEquals(1, ranges.size());
767     assertEquals(1, ranges.get(0)[0]);
768     assertEquals(15, ranges.get(0)[1]);
769
770     // change direction: range is not merged:
771     MapList.addRange(new int[] { 16, 10 }, ranges);
772     assertEquals(2, ranges.size());
773     assertEquals(16, ranges.get(1)[0]);
774     assertEquals(10, ranges.get(1)[1]);
775
776     // extend reverse contiguous (same position):
777     MapList.addRange(new int[] { 10, 8 }, ranges);
778     assertEquals(2, ranges.size());
779     assertEquals(16, ranges.get(1)[0]);
780     assertEquals(8, ranges.get(1)[1]);
781
782     // extend reverse contiguous (next position):
783     MapList.addRange(new int[] { 7, 6 }, ranges);
784     assertEquals(2, ranges.size());
785     assertEquals(16, ranges.get(1)[0]);
786     assertEquals(6, ranges.get(1)[1]);
787
788     // change direction: range is not merged:
789     MapList.addRange(new int[] { 6, 9 }, ranges);
790     assertEquals(3, ranges.size());
791     assertEquals(6, ranges.get(2)[0]);
792     assertEquals(9, ranges.get(2)[1]);
793
794     // not contiguous: not merged
795     MapList.addRange(new int[] { 11, 12 }, ranges);
796     assertEquals(4, ranges.size());
797     assertEquals(11, ranges.get(3)[0]);
798     assertEquals(12, ranges.get(3)[1]);
799   }
800
801   /**
802    * Check state after construction
803    */
804   @Test(groups = { "Functional" })
805   public void testConstructor_withLists()
806   {
807     /*
808      * reverse direction
809      */
810     int[][] codons = new int[][] { { 9, 6 } };
811     int[][] protein = new int[][] { { 100, 91 }, { 80, 79 } };
812     MapList ml = new MapList(Arrays.asList(codons), Arrays.asList(protein),
813             3, 1);
814     assertEquals(6, ml.getFromLowest());
815     assertEquals(9, ml.getFromHighest());
816     assertEquals(79, ml.getToLowest());
817     assertEquals(100, ml.getToHighest());
818   }
819
820   /**
821    * Test that method that inspects for the (first) forward or reverse from
822    * range. Single position ranges are ignored.
823    */
824   @Test(groups = { "Functional" })
825   public void testIsFromForwardStrand()
826   {
827     // [3-9] declares forward strand
828     MapList ml = new MapList(new int[] { 2, 2, 3, 9, 12, 11 },
829             new int[]
830             { 20, 11 }, 1, 1);
831     assertTrue(ml.isFromForwardStrand());
832
833     // [11-5] declares reverse strand ([13-14] is ignored)
834     ml = new MapList(new int[] { 2, 2, 11, 5, 13, 14 },
835             new int[]
836             { 20, 11 }, 1, 1);
837     assertFalse(ml.isFromForwardStrand());
838
839     // all single position ranges - defaults to forward strand
840     ml = new MapList(new int[] { 2, 2, 4, 4, 6, 6 }, new int[] { 3, 1 }, 1,
841             1);
842     assertTrue(ml.isFromForwardStrand());
843   }
844
845   /**
846    * Test the method that merges contiguous ranges
847    */
848   @Test(groups = { "Functional" })
849   public void testCoalesceRanges()
850   {
851     assertNull(MapList.coalesceRanges(null));
852     List<int[]> ranges = new ArrayList<>();
853     assertSame(ranges, MapList.coalesceRanges(ranges));
854     ranges.add(new int[] { 1, 3 });
855     assertSame(ranges, MapList.coalesceRanges(ranges));
856
857     // add non-contiguous range:
858     ranges.add(new int[] { 5, 6 });
859     assertSame(ranges, MapList.coalesceRanges(ranges));
860
861     // 'contiguous' range in opposite direction is not merged:
862     ranges.add(new int[] { 7, 6 });
863     assertSame(ranges, MapList.coalesceRanges(ranges));
864
865     // merging in forward direction:
866     ranges.clear();
867     ranges.add(new int[] { 1, 3 });
868     ranges.add(new int[] { 4, 5 }); // contiguous
869     ranges.add(new int[] { 5, 5 }); // overlap!
870     ranges.add(new int[] { 6, 7 }); // contiguous
871     List<int[]> merged = MapList.coalesceRanges(ranges);
872     assertEquals(2, merged.size());
873     assertArrayEquals(new int[] { 1, 5 }, merged.get(0));
874     assertArrayEquals(new int[] { 5, 7 }, merged.get(1));
875     // verify input list is unchanged
876     assertEquals(4, ranges.size());
877     assertArrayEquals(new int[] { 1, 3 }, ranges.get(0));
878     assertArrayEquals(new int[] { 4, 5 }, ranges.get(1));
879     assertArrayEquals(new int[] { 5, 5 }, ranges.get(2));
880     assertArrayEquals(new int[] { 6, 7 }, ranges.get(3));
881
882     // merging in reverse direction:
883     ranges.clear();
884     ranges.add(new int[] { 7, 5 });
885     ranges.add(new int[] { 5, 4 }); // overlap
886     ranges.add(new int[] { 4, 4 }); // overlap
887     ranges.add(new int[] { 3, 1 }); // contiguous
888     merged = MapList.coalesceRanges(ranges);
889     assertEquals(3, merged.size());
890     assertArrayEquals(new int[] { 7, 5 }, merged.get(0));
891     assertArrayEquals(new int[] { 5, 4 }, merged.get(1));
892     assertArrayEquals(new int[] { 4, 1 }, merged.get(2));
893
894     // merging with switches of direction:
895     ranges.clear();
896     ranges.add(new int[] { 1, 3 });
897     ranges.add(new int[] { 4, 5 }); // contiguous
898     ranges.add(new int[] { 5, 5 }); // overlap
899     ranges.add(new int[] { 6, 6 }); // contiguous
900     ranges.add(new int[] { 12, 10 });
901     ranges.add(new int[] { 9, 8 }); // contiguous
902     ranges.add(new int[] { 8, 8 }); // overlap
903     ranges.add(new int[] { 7, 7 }); // contiguous
904     merged = MapList.coalesceRanges(ranges);
905     assertEquals(4, merged.size());
906     assertArrayEquals(new int[] { 1, 5 }, merged.get(0));
907     assertArrayEquals(new int[] { 5, 6 }, merged.get(1));
908     assertArrayEquals(new int[] { 12, 8 }, merged.get(2));
909     assertArrayEquals(new int[] { 8, 7 }, merged.get(3));
910
911     // 'subsumed' ranges are preserved
912     ranges.clear();
913     ranges.add(new int[] { 10, 30 });
914     ranges.add(new int[] { 15, 25 });
915     merged = MapList.coalesceRanges(ranges);
916     assertEquals(2, merged.size());
917     assertArrayEquals(new int[] { 10, 30 }, merged.get(0));
918     assertArrayEquals(new int[] { 15, 25 }, merged.get(1));
919   }
920
921   /**
922    * Test the method that compounds ('traverses') two mappings
923    */
924   @Test(groups = "Functional")
925   public void testTraverse()
926   {
927     /*
928      * simple 1:1 plus 1:1 forwards
929      */
930     MapList ml1 = new MapList(new int[] { 3, 4, 8, 12 },
931             new int[]
932             { 5, 8, 11, 13 }, 1, 1);
933     assertEquals("{[3, 4], [8, 12]}", prettyPrint(ml1.getFromRanges()));
934     assertEquals("{[5, 8], [11, 13]}", prettyPrint(ml1.getToRanges()));
935
936     MapList ml2 = new MapList(new int[] { 1, 50 },
937             new int[]
938             { 40, 45, 70, 75, 90, 127 }, 1, 1);
939     assertEquals("{[1, 50]}", prettyPrint(ml2.getFromRanges()));
940     assertEquals("{[40, 45], [70, 75], [90, 127]}",
941             prettyPrint(ml2.getToRanges()));
942
943     MapList compound = ml1.traverse(ml2);
944
945     assertEquals(1, compound.getFromRatio());
946     assertEquals(1, compound.getToRatio());
947     List<int[]> fromRanges = compound.getFromRanges();
948     assertEquals(2, fromRanges.size());
949     assertArrayEquals(new int[] { 3, 4 }, fromRanges.get(0));
950     assertArrayEquals(new int[] { 8, 12 }, fromRanges.get(1));
951     List<int[]> toRanges = compound.getToRanges();
952     assertEquals(4, toRanges.size());
953     // 5-8 maps to 44-45,70-71
954     // 11-13 maps to 74-75,90
955     assertArrayEquals(new int[] { 44, 45 }, toRanges.get(0));
956     assertArrayEquals(new int[] { 70, 71 }, toRanges.get(1));
957     assertArrayEquals(new int[] { 74, 75 }, toRanges.get(2));
958     assertArrayEquals(new int[] { 90, 90 }, toRanges.get(3));
959
960     /*
961      * 1:1 over 1:1 backwards ('reverse strand')
962      */
963     ml1 = new MapList(new int[] { 1, 50 }, new int[] { 70, 119 }, 1, 1);
964     ml2 = new MapList(new int[] { 1, 500 },
965             new int[]
966             { 1000, 901, 600, 201 }, 1, 1);
967     compound = ml1.traverse(ml2);
968
969     assertEquals(1, compound.getFromRatio());
970     assertEquals(1, compound.getToRatio());
971     fromRanges = compound.getFromRanges();
972     assertEquals(1, fromRanges.size());
973     assertArrayEquals(new int[] { 1, 50 }, fromRanges.get(0));
974     toRanges = compound.getToRanges();
975     assertEquals(2, toRanges.size());
976     assertArrayEquals(new int[] { 931, 901 }, toRanges.get(0));
977     assertArrayEquals(new int[] { 600, 582 }, toRanges.get(1));
978
979     /*
980      * 1:1 plus 1:3 should result in 1:3
981      */
982     ml1 = new MapList(new int[] { 1, 30 }, new int[] { 11, 40 }, 1, 1);
983     ml2 = new MapList(new int[] { 1, 100 }, new int[] { 1, 50, 91, 340 }, 1,
984             3);
985     compound = ml1.traverse(ml2);
986
987     assertEquals(1, compound.getFromRatio());
988     assertEquals(3, compound.getToRatio());
989     fromRanges = compound.getFromRanges();
990     assertEquals(1, fromRanges.size());
991     assertArrayEquals(new int[] { 1, 30 }, fromRanges.get(0));
992     // 11-40 maps to 31-50,91-160
993     toRanges = compound.getToRanges();
994     assertEquals(2, toRanges.size());
995     assertArrayEquals(new int[] { 31, 50 }, toRanges.get(0));
996     assertArrayEquals(new int[] { 91, 160 }, toRanges.get(1));
997
998     /*
999      * 3:1 plus 1:1 should result in 3:1
1000      */
1001     ml1 = new MapList(new int[] { 1, 30 }, new int[] { 11, 20 }, 3, 1);
1002     ml2 = new MapList(new int[] { 1, 100 }, new int[] { 1, 15, 91, 175 }, 1,
1003             1);
1004     compound = ml1.traverse(ml2);
1005
1006     assertEquals(3, compound.getFromRatio());
1007     assertEquals(1, compound.getToRatio());
1008     fromRanges = compound.getFromRanges();
1009     assertEquals(1, fromRanges.size());
1010     assertArrayEquals(new int[] { 1, 30 }, fromRanges.get(0));
1011     // 11-20 maps to 11-15, 91-95
1012     toRanges = compound.getToRanges();
1013     assertEquals(2, toRanges.size());
1014     assertArrayEquals(new int[] { 11, 15 }, toRanges.get(0));
1015     assertArrayEquals(new int[] { 91, 95 }, toRanges.get(1));
1016
1017     /*
1018      * 1:3 plus 3:1 should result in 1:1
1019      */
1020     ml1 = new MapList(new int[] { 21, 40 }, new int[] { 13, 72 }, 1, 3);
1021     ml2 = new MapList(new int[] { 1, 300 }, new int[] { 51, 70, 121, 200 },
1022             3, 1);
1023     compound = ml1.traverse(ml2);
1024
1025     assertEquals(1, compound.getFromRatio());
1026     assertEquals(1, compound.getToRatio());
1027     fromRanges = compound.getFromRanges();
1028     assertEquals(1, fromRanges.size());
1029     assertArrayEquals(new int[] { 21, 40 }, fromRanges.get(0));
1030     // 13-72 maps 3:1 to 55-70, 121-124
1031     toRanges = compound.getToRanges();
1032     assertEquals(2, toRanges.size());
1033     assertArrayEquals(new int[] { 55, 70 }, toRanges.get(0));
1034     assertArrayEquals(new int[] { 121, 124 }, toRanges.get(1));
1035
1036     /*
1037      * 3:1 plus 1:3 should result in 1:1
1038      */
1039     ml1 = new MapList(new int[] { 31, 90 }, new int[] { 13, 32 }, 3, 1);
1040     ml2 = new MapList(new int[] { 11, 40 }, new int[] { 41, 50, 71, 150 },
1041             1, 3);
1042     compound = ml1.traverse(ml2);
1043
1044     assertEquals(1, compound.getFromRatio());
1045     assertEquals(1, compound.getToRatio());
1046     fromRanges = compound.getFromRanges();
1047     assertEquals(1, fromRanges.size());
1048     assertArrayEquals(new int[] { 31, 90 }, fromRanges.get(0));
1049     // 13-32 maps to 47-50,71-126
1050     toRanges = compound.getToRanges();
1051     assertEquals(2, toRanges.size());
1052     assertArrayEquals(new int[] { 47, 50 }, toRanges.get(0));
1053     assertArrayEquals(new int[] { 71, 126 }, toRanges.get(1));
1054
1055     /*
1056      * if not all regions are mapped through, returns what is
1057      */
1058     ml1 = new MapList(new int[] { 1, 50 }, new int[] { 101, 150 }, 1, 1);
1059     ml2 = new MapList(new int[] { 131, 180 }, new int[] { 201, 250 }, 1, 1);
1060     compound = ml1.traverse(ml2);
1061     assertNull(compound);
1062   }
1063
1064   /**
1065    * Test that method that inspects for the (first) forward or reverse 'to'
1066    * range. Single position ranges are ignored.
1067    */
1068   @Test(groups = { "Functional" })
1069   public void testIsToForwardsStrand()
1070   {
1071     // [3-9] declares forward strand
1072     MapList ml = new MapList(new int[] { 20, 11 },
1073             new int[]
1074             { 2, 2, 3, 9, 12, 11 }, 1, 1);
1075     assertTrue(ml.isToForwardStrand());
1076
1077     // [11-5] declares reverse strand ([13-14] is ignored)
1078     ml = new MapList(new int[] { 20, 11 },
1079             new int[]
1080             { 2, 2, 11, 5, 13, 14 }, 1, 1);
1081     assertFalse(ml.isToForwardStrand());
1082
1083     // all single position ranges - defaults to forward strand
1084     ml = new MapList(new int[] { 3, 1 }, new int[] { 2, 2, 4, 4, 6, 6 }, 1,
1085             1);
1086     assertTrue(ml.isToForwardStrand());
1087   }
1088
1089   /**
1090    * Test for mapping with overlapping ranges
1091    */
1092   @Test(groups = { "Functional" })
1093   public void testLocateInFrom_withOverlap()
1094   {
1095     /*
1096      * gene to protein...
1097      */
1098     int[] codons = new int[] { 1, 12, 12, 17 };
1099     int[] protein = new int[] { 1, 6 };
1100     MapList ml = new MapList(codons, protein, 3, 1);
1101     assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(1, 1)));
1102     assertEquals("[4, 6]", Arrays.toString(ml.locateInFrom(2, 2)));
1103     assertEquals("[7, 9]", Arrays.toString(ml.locateInFrom(3, 3)));
1104     assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
1105     assertEquals("[12, 14]", Arrays.toString(ml.locateInFrom(5, 5)));
1106     assertEquals("[15, 17]", Arrays.toString(ml.locateInFrom(6, 6)));
1107     assertEquals("[1, 6]", Arrays.toString(ml.locateInFrom(1, 2)));
1108     assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(1, 3)));
1109     assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 4)));
1110     assertEquals("[1, 12, 12, 14]", Arrays.toString(ml.locateInFrom(1, 5)));
1111     assertEquals("[1, 12, 12, 17]", Arrays.toString(ml.locateInFrom(1, 6)));
1112     assertEquals("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3)));
1113     assertEquals("[7, 12, 12, 17]", Arrays.toString(ml.locateInFrom(3, 6)));
1114
1115     /*
1116      * partial overlap of range
1117      */
1118     assertEquals("[4, 12, 12, 17]", Arrays.toString(ml.locateInFrom(2, 7)));
1119     assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(-1, 1)));
1120
1121     /*
1122      * no overlap in range
1123      */
1124     assertNull(ml.locateInFrom(0, 0));
1125
1126     /*
1127      * gene to CDS...from EMBL:MN908947
1128      */
1129     int[] gene = new int[] { 266, 13468, 13468, 21555 };
1130     int[] cds = new int[] { 1, 21291 };
1131     ml = new MapList(gene, cds, 1, 1);
1132     assertEquals("[13468, 13468]",
1133             Arrays.toString(ml.locateInFrom(13203, 13203)));
1134     assertEquals("[13468, 13468]",
1135             Arrays.toString(ml.locateInFrom(13204, 13204)));
1136     assertEquals("[13468, 13468, 13468, 13468]",
1137             Arrays.toString(ml.locateInFrom(13203, 13204)));
1138   }
1139
1140   /**
1141    * Test for mapping with overlapping ranges
1142    */
1143   @Test(groups = { "Functional" })
1144   public void testLocateInTo_withOverlap()
1145   {
1146     /*
1147      * gene to protein...
1148      */
1149     int[] codons = new int[] { 1, 12, 12, 17 };
1150     int[] protein = new int[] { 1, 6 };
1151     MapList ml = new MapList(codons, protein, 3, 1);
1152     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
1153     assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(3, 8)));
1154     assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(2, 11)));
1155     assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
1156
1157     // we want base 12 to map to both of the amino acids it codes for
1158     assertEquals("[4, 5]", Arrays.toString(ml.locateInTo(12, 12)));
1159     assertEquals("[4, 5]", Arrays.toString(ml.locateInTo(11, 12)));
1160     assertEquals("[4, 6]", Arrays.toString(ml.locateInTo(11, 15)));
1161     assertEquals("[6, 6]", Arrays.toString(ml.locateInTo(15, 17)));
1162
1163     /*
1164      * no overlap
1165      */
1166     assertNull(ml.locateInTo(0, 0));
1167
1168     /*
1169      * partial overlap
1170      */
1171     assertEquals("[1, 6]", Arrays.toString(ml.locateInTo(1, 18)));
1172     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(-1, 1)));
1173
1174     /*
1175      * gene to CDS...from EMBL:MN908947
1176      * the base at 13468 is used twice in transcription
1177      */
1178     int[] gene = new int[] { 266, 13468, 13468, 21555 };
1179     int[] cds = new int[] { 1, 21291 };
1180     ml = new MapList(gene, cds, 1, 1);
1181     assertEquals("[13203, 13204]",
1182             Arrays.toString(ml.locateInTo(13468, 13468)));
1183
1184     /*
1185      * gene to protein
1186      * the base at 13468 is in the codon for 4401N and also 4402R
1187      */
1188     gene = new int[] { 266, 13468, 13468, 21552 }; // stop codon excluded
1189     protein = new int[] { 1, 7096 };
1190     ml = new MapList(gene, protein, 3, 1);
1191     assertEquals("[4401, 4402]",
1192             Arrays.toString(ml.locateInTo(13468, 13468)));
1193   }
1194
1195   @Test(groups = { "Functional" })
1196   public void testTraverseToPosition()
1197   {
1198     List<int[]> ranges = new ArrayList<>();
1199     assertNull(MapList.traverseToPosition(ranges, 0));
1200
1201     ranges.add(new int[] { 3, 6 });
1202     assertNull(MapList.traverseToPosition(ranges, 0));
1203   }
1204
1205   @Test(groups = { "Functional" })
1206   public void testCountPositions()
1207   {
1208     try
1209     {
1210       MapList.countPositions(null, 1);
1211       fail("expected exception");
1212     } catch (NullPointerException e)
1213     {
1214       // expected
1215     }
1216
1217     List<int[]> intervals = new ArrayList<>();
1218     assertNull(MapList.countPositions(intervals, 1));
1219
1220     /*
1221      * forward strand
1222      */
1223     intervals.add(new int[] { 10, 20 });
1224     assertNull(MapList.countPositions(intervals, 9));
1225     assertNull(MapList.countPositions(intervals, 21));
1226     assertArrayEquals(new int[] { 1, 1 },
1227             MapList.countPositions(intervals, 10));
1228     assertArrayEquals(new int[] { 6, 1 },
1229             MapList.countPositions(intervals, 15));
1230     assertArrayEquals(new int[] { 11, 1 },
1231             MapList.countPositions(intervals, 20));
1232
1233     intervals.add(new int[] { 25, 25 });
1234     assertArrayEquals(new int[] { 12, 1 },
1235             MapList.countPositions(intervals, 25));
1236
1237     // next interval repeats position 25 - which should be counted twice if
1238     // traversed
1239     intervals.add(new int[] { 25, 26 });
1240     assertArrayEquals(new int[] { 12, 1 },
1241             MapList.countPositions(intervals, 25));
1242     assertArrayEquals(new int[] { 14, 1 },
1243             MapList.countPositions(intervals, 26));
1244
1245     /*
1246      * reverse strand
1247      */
1248     intervals.clear();
1249     intervals.add(new int[] { 5, -5 });
1250     assertNull(MapList.countPositions(intervals, 6));
1251     assertNull(MapList.countPositions(intervals, -6));
1252     assertArrayEquals(new int[] { 1, -1 },
1253             MapList.countPositions(intervals, 5));
1254     assertArrayEquals(new int[] { 7, -1 },
1255             MapList.countPositions(intervals, -1));
1256     assertArrayEquals(new int[] { 11, -1 },
1257             MapList.countPositions(intervals, -5));
1258
1259     /*
1260      * reverse then forward
1261      */
1262     intervals.add(new int[] { 5, 10 });
1263     assertArrayEquals(new int[] { 13, 1 },
1264             MapList.countPositions(intervals, 6));
1265
1266     /*
1267      * reverse then forward then reverse
1268      */
1269     intervals.add(new int[] { -10, -20 });
1270     assertArrayEquals(new int[] { 20, -1 },
1271             MapList.countPositions(intervals, -12));
1272
1273     /*
1274      * an interval [x, x] is treated as forward
1275      */
1276     intervals.add(new int[] { 30, 30 });
1277     assertArrayEquals(new int[] { 29, 1 },
1278             MapList.countPositions(intervals, 30));
1279
1280     /*
1281      * it is the first matched occurrence that is returned
1282      */
1283     intervals.clear();
1284     intervals.add(new int[] { 1, 2 });
1285     intervals.add(new int[] { 2, 3 });
1286     assertArrayEquals(new int[] { 2, 1 },
1287             MapList.countPositions(intervals, 2));
1288     intervals.add(new int[] { -1, -2 });
1289     intervals.add(new int[] { -2, -3 });
1290     assertArrayEquals(new int[] { 6, -1 },
1291             MapList.countPositions(intervals, -2));
1292   }
1293
1294   /**
1295    * Tests for helper method that adds any overlap (plus offset) to a set of
1296    * overlaps
1297    */
1298   @Test(groups = { "Functional" })
1299   public void testAddOffsetPositions()
1300   {
1301     List<int[]> mapped = new ArrayList<>();
1302     int[] range = new int[] { 10, 20 };
1303     BitSet offsets = new BitSet();
1304
1305     MapList.addOffsetPositions(mapped, 0, range, offsets);
1306     assertTrue(mapped.isEmpty()); // nothing marked for overlap
1307
1308     offsets.set(11);
1309     MapList.addOffsetPositions(mapped, 0, range, offsets);
1310     assertTrue(mapped.isEmpty()); // no offset 11 in range
1311
1312     offsets.set(4, 6); // this sets bits 4 and 5
1313     MapList.addOffsetPositions(mapped, 0, range, offsets);
1314     assertEquals(1, mapped.size());
1315     assertArrayEquals(new int[] { 14, 15 }, mapped.get(0));
1316
1317     mapped.clear();
1318     offsets.set(10);
1319     MapList.addOffsetPositions(mapped, 0, range, offsets);
1320     assertEquals(2, mapped.size());
1321     assertArrayEquals(new int[] { 14, 15 }, mapped.get(0));
1322     assertArrayEquals(new int[] { 20, 20 }, mapped.get(1));
1323
1324     /*
1325      * reverse range
1326      */
1327     range = new int[] { 20, 10 };
1328     mapped.clear();
1329     offsets.clear();
1330     MapList.addOffsetPositions(mapped, 0, range, offsets);
1331     assertTrue(mapped.isEmpty()); // nothing marked for overlap
1332     offsets.set(11);
1333     MapList.addOffsetPositions(mapped, 0, range, offsets);
1334     assertTrue(mapped.isEmpty()); // no offset 11 in range
1335     offsets.set(0);
1336     offsets.set(10);
1337     offsets.set(6, 8); // sets bits 6 and 7
1338     MapList.addOffsetPositions(mapped, 0, range, offsets);
1339     assertEquals(3, mapped.size());
1340     assertArrayEquals(new int[] { 20, 20 }, mapped.get(0));
1341     assertArrayEquals(new int[] { 14, 13 }, mapped.get(1));
1342     assertArrayEquals(new int[] { 10, 10 }, mapped.get(2));
1343   }
1344
1345   @Test(groups = { "Functional" })
1346   public void testGetPositionsForOffsets()
1347   {
1348     List<int[]> ranges = new ArrayList<>();
1349     BitSet offsets = new BitSet();
1350     List<int[]> mapped = MapList.getPositionsForOffsets(ranges, offsets);
1351     assertTrue(mapped.isEmpty()); // no ranges and no offsets!
1352
1353     offsets.set(5, 1000);
1354     mapped = MapList.getPositionsForOffsets(ranges, offsets);
1355     assertTrue(mapped.isEmpty()); // no ranges
1356
1357     /*
1358      * one range with overlap of offsets
1359      */
1360     ranges.add(new int[] { 15, 25 });
1361     mapped = MapList.getPositionsForOffsets(ranges, offsets);
1362     assertEquals(1, mapped.size());
1363     assertArrayEquals(new int[] { 20, 25 }, mapped.get(0));
1364
1365     /*
1366      * two ranges
1367      */
1368     ranges.add(new int[] { 300, 320 });
1369     mapped = MapList.getPositionsForOffsets(ranges, offsets);
1370     assertEquals(2, mapped.size());
1371     assertArrayEquals(new int[] { 20, 25 }, mapped.get(0));
1372     assertArrayEquals(new int[] { 300, 320 }, mapped.get(1));
1373
1374     /*
1375      * boundary case - right end of first range overlaps
1376      */
1377     offsets.clear();
1378     offsets.set(10);
1379     mapped = MapList.getPositionsForOffsets(ranges, offsets);
1380     assertEquals(1, mapped.size());
1381     assertArrayEquals(new int[] { 25, 25 }, mapped.get(0));
1382
1383     /*
1384      * boundary case - left end of second range overlaps
1385      */
1386     offsets.set(11);
1387     mapped = MapList.getPositionsForOffsets(ranges, offsets);
1388     assertEquals(2, mapped.size());
1389     assertArrayEquals(new int[] { 25, 25 }, mapped.get(0));
1390     assertArrayEquals(new int[] { 300, 300 }, mapped.get(1));
1391
1392     /*
1393      * offsets into a circular range are reported in
1394      * the order in which they are traversed
1395      */
1396     ranges.clear();
1397     ranges.add(new int[] { 100, 150 });
1398     ranges.add(new int[] { 60, 80 });
1399     offsets.clear();
1400     offsets.set(45, 55); // sets bits 45 to 54
1401     mapped = MapList.getPositionsForOffsets(ranges, offsets);
1402     assertEquals(2, mapped.size());
1403     assertArrayEquals(new int[] { 145, 150 }, mapped.get(0)); // offsets 45-50
1404     assertArrayEquals(new int[] { 60, 63 }, mapped.get(1)); // offsets 51-54
1405
1406     /*
1407      * reverse range overlap is reported with start < end
1408      */
1409     ranges.clear();
1410     ranges.add(new int[] { 4321, 4000 });
1411     offsets.clear();
1412     offsets.set(20, 22); // sets bits 20 and 21
1413     offsets.set(30);
1414     mapped = MapList.getPositionsForOffsets(ranges, offsets);
1415     assertEquals(2, mapped.size());
1416     assertArrayEquals(new int[] { 4301, 4300 }, mapped.get(0));
1417     assertArrayEquals(new int[] { 4291, 4291 }, mapped.get(1));
1418   }
1419
1420   @Test(groups = { "Functional" })
1421   public void testGetMappedOffsetsForPositions()
1422   {
1423     /*
1424      * start by verifying the examples in the method's Javadoc!
1425      */
1426     List<int[]> ranges = new ArrayList<>();
1427     ranges.add(new int[] { 10, 20 });
1428     ranges.add(new int[] { 31, 40 });
1429     BitSet overlaps = MapList.getMappedOffsetsForPositions(1, 9, ranges, 1,
1430             1);
1431     assertTrue(overlaps.isEmpty());
1432     overlaps = MapList.getMappedOffsetsForPositions(1, 11, ranges, 1, 1);
1433     assertEquals(2, overlaps.cardinality());
1434     assertTrue(overlaps.get(0));
1435     assertTrue(overlaps.get(1));
1436     overlaps = MapList.getMappedOffsetsForPositions(15, 35, ranges, 1, 1);
1437     assertEquals(11, overlaps.cardinality());
1438     for (int i = 5; i <= 11; i++)
1439     {
1440       assertTrue(overlaps.get(i));
1441     }
1442
1443     ranges.clear();
1444     ranges.add(new int[] { 1, 200 });
1445     overlaps = MapList.getMappedOffsetsForPositions(9, 9, ranges, 1, 3);
1446     assertEquals(3, overlaps.cardinality());
1447     assertTrue(overlaps.get(24));
1448     assertTrue(overlaps.get(25));
1449     assertTrue(overlaps.get(26));
1450
1451     ranges.clear();
1452     ranges.add(new int[] { 101, 150 });
1453     ranges.add(new int[] { 171, 180 });
1454     overlaps = MapList.getMappedOffsetsForPositions(101, 102, ranges, 3, 1);
1455     assertEquals(1, overlaps.cardinality());
1456     assertTrue(overlaps.get(0));
1457     overlaps = MapList.getMappedOffsetsForPositions(150, 171, ranges, 3, 1);
1458     assertEquals(1, overlaps.cardinality());
1459     assertTrue(overlaps.get(16));
1460
1461     ranges.clear();
1462     ranges.add(new int[] { 101, 150 });
1463     ranges.add(new int[] { 21, 30 });
1464     overlaps = MapList.getMappedOffsetsForPositions(24, 40, ranges, 3, 1);
1465     assertEquals(3, overlaps.cardinality());
1466     assertTrue(overlaps.get(17));
1467     assertTrue(overlaps.get(18));
1468     assertTrue(overlaps.get(19));
1469
1470     /*
1471      * reverse range 1:1 (e.g. reverse strand gene to transcript)
1472      */
1473     ranges.clear();
1474     ranges.add(new int[] { 20, 10 });
1475     overlaps = MapList.getMappedOffsetsForPositions(12, 13, ranges, 1, 1);
1476     assertEquals(2, overlaps.cardinality());
1477     assertTrue(overlaps.get(7));
1478     assertTrue(overlaps.get(8));
1479
1480     /*
1481      * reverse range 3:1 (e.g. reverse strand gene to peptide)
1482      * from EMBL:J03321 to P0CE20
1483      */
1484     ranges.clear();
1485     ranges.add(new int[] { 1480, 488 });
1486     overlaps = MapList.getMappedOffsetsForPositions(1460, 1460, ranges, 3,
1487             1);
1488     // 1460 is the end of the 7th codon
1489     assertEquals(1, overlaps.cardinality());
1490     assertTrue(overlaps.get(6));
1491     // add one base (part codon)
1492     overlaps = MapList.getMappedOffsetsForPositions(1459, 1460, ranges, 3,
1493             1);
1494     assertEquals(2, overlaps.cardinality());
1495     assertTrue(overlaps.get(6));
1496     assertTrue(overlaps.get(7));
1497     // add second base (part codon)
1498     overlaps = MapList.getMappedOffsetsForPositions(1458, 1460, ranges, 3,
1499             1);
1500     assertEquals(2, overlaps.cardinality());
1501     assertTrue(overlaps.get(6));
1502     assertTrue(overlaps.get(7));
1503     // add third base (whole codon)
1504     overlaps = MapList.getMappedOffsetsForPositions(1457, 1460, ranges, 3,
1505             1);
1506     assertEquals(2, overlaps.cardinality());
1507     assertTrue(overlaps.get(6));
1508     assertTrue(overlaps.get(7));
1509     // add one more base (part codon)
1510     overlaps = MapList.getMappedOffsetsForPositions(1456, 1460, ranges, 3,
1511             1);
1512     assertEquals(3, overlaps.cardinality());
1513     assertTrue(overlaps.get(6));
1514     assertTrue(overlaps.get(7));
1515     assertTrue(overlaps.get(8));
1516   }
1517 }