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