JAL-1645 source formatting and organise imports
[jalview.git] / test / jalview / util / MapListTest.java
1 package jalview.util;
2
3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertFalse;
5 import static org.testng.AssertJUnit.assertNull;
6 import static org.testng.AssertJUnit.assertTrue;
7
8 import java.util.ArrayList;
9 import java.util.Arrays;
10 import java.util.List;
11
12 import org.testng.annotations.Test;
13
14 public class MapListTest
15 {
16
17   @Test(groups = { "Functional" })
18   public void testSomething()
19   {
20     MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 }, new int[] {
21         51, 1 }, 1, 3);
22     MapList ml1 = new MapList(new int[] { 1, 3, 17, 4 },
23             new int[] { 51, 1 }, 1, 3);
24     MapList ml2 = new MapList(new int[] { 1, 60 }, new int[] { 1, 20 }, 3,
25             1);
26     // test internal consistency
27     int to[] = new int[51];
28     testMap(ml, 1, 60);
29     MapList mldna = new MapList(new int[] { 2, 2, 6, 8, 12, 16 }, new int[]
30     { 1, 3 }, 3, 1);
31     int[] frm = mldna.locateInFrom(1, 1);
32     testLocateFrom(mldna, 1, 1, new int[] { 2, 2, 6, 7 });
33     testMap(mldna, 1, 3);
34     /*
35      * for (int from=1; from<=51; from++) { int[] too=ml.shiftTo(from); int[]
36      * toofrom=ml.shiftFrom(too[0]);
37      * System.out.println("ShiftFrom("+from+")=="+too[0]+" %
38      * "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]); }
39      */
40   }
41
42   private static void testLocateFrom(MapList mldna, int i, int j, int[] ks)
43   {
44     int[] frm = mldna.locateInFrom(i, j);
45     assertEquals("Failed test locate from " + i + " to " + j,
46             Arrays.toString(frm), Arrays.toString(ks));
47   }
48
49   /**
50    * test routine. not incremental.
51    * 
52    * @param ml
53    * @param fromS
54    * @param fromE
55    */
56   private void testMap(MapList ml, int fromS, int fromE)
57   {
58     // todo convert to JUnit style tests
59     for (int from = 1; from <= 25; from++)
60     {
61       int[] too = ml.shiftFrom(from);
62       System.out.print("ShiftFrom(" + from + ")==");
63       if (too == null)
64       {
65         System.out.print("NaN\n");
66       }
67       else
68       {
69         System.out.print(too[0] + " % " + too[1] + " (" + too[2] + ")");
70         System.out.print("\t+--+\t");
71         int[] toofrom = ml.shiftTo(too[0]);
72         if (toofrom != null)
73         {
74           if (toofrom[0] != from)
75           {
76             System.err.println("Mapping not reflexive:" + from + " "
77                     + too[0] + "->" + toofrom[0]);
78           }
79           System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0]
80                   + " % " + toofrom[1] + " (" + toofrom[2] + ")");
81         }
82         else
83         {
84           System.out.println("ShiftTo(" + too[0] + ")=="
85                   + "NaN! - not Bijective Mapping!");
86         }
87       }
88     }
89     int mmap[][] = ml.makeFromMap();
90     System.out.println("FromMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
91             + mmap[0][2] + " " + mmap[0][3] + " ");
92     for (int i = 1; i <= mmap[1].length; i++)
93     {
94       if (mmap[1][i - 1] == -1)
95       {
96         System.out.print(i + "=XXX");
97
98       }
99       else
100       {
101         System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
102       }
103       if (i % 20 == 0)
104       {
105         System.out.print("\n");
106       }
107       else
108       {
109         System.out.print(",");
110       }
111     }
112     // test range function
113     System.out.print("\nTest locateInFrom\n");
114     {
115       int f = mmap[0][2], t = mmap[0][3];
116       while (f <= t)
117       {
118         System.out.println("Range " + f + " to " + t);
119         int rng[] = ml.locateInFrom(f, t);
120         if (rng != null)
121         {
122           for (int i = 0; i < rng.length; i++)
123           {
124             System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
125           }
126         }
127         else
128         {
129           System.out.println("No range!");
130         }
131         System.out.print("\nReversed\n");
132         rng = ml.locateInFrom(t, f);
133         if (rng != null)
134         {
135           for (int i = 0; i < rng.length; i++)
136           {
137             System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
138           }
139         }
140         else
141         {
142           System.out.println("No range!");
143         }
144         System.out.print("\n");
145         f++;
146         t--;
147       }
148     }
149     System.out.print("\n");
150     mmap = ml.makeToMap();
151     System.out.println("ToMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
152             + mmap[0][2] + " " + mmap[0][3] + " ");
153     for (int i = 1; i <= mmap[1].length; i++)
154     {
155       if (mmap[1][i - 1] == -1)
156       {
157         System.out.print(i + "=XXX");
158
159       }
160       else
161       {
162         System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
163       }
164       if (i % 20 == 0)
165       {
166         System.out.print("\n");
167       }
168       else
169       {
170         System.out.print(",");
171       }
172     }
173     System.out.print("\n");
174     // test range function
175     System.out.print("\nTest locateInTo\n");
176     {
177       int f = mmap[0][2], t = mmap[0][3];
178       while (f <= t)
179       {
180         System.out.println("Range " + f + " to " + t);
181         int rng[] = ml.locateInTo(f, t);
182         if (rng != null)
183         {
184           for (int i = 0; i < rng.length; i++)
185           {
186             System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
187           }
188         }
189         else
190         {
191           System.out.println("No range!");
192         }
193         System.out.print("\nReversed\n");
194         rng = ml.locateInTo(t, f);
195         if (rng != null)
196         {
197           for (int i = 0; i < rng.length; i++)
198           {
199             System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
200           }
201         }
202         else
203         {
204           System.out.println("No range!");
205         }
206         f++;
207         t--;
208         System.out.print("\n");
209       }
210     }
211   }
212
213   /**
214    * Tests for method that locates ranges in the 'from' map for given range in
215    * the 'to' map.
216    */
217   @Test(groups = { "Functional" })
218   public void testLocateInFrom_noIntrons()
219   {
220     /*
221      * Simple mapping with no introns
222      */
223     int[] codons = new int[] { 1, 12 };
224     int[] protein = new int[] { 1, 4 };
225     MapList ml = new MapList(codons, protein, 3, 1);
226     assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(1, 1)));
227     assertEquals("[4, 6]", Arrays.toString(ml.locateInFrom(2, 2)));
228     assertEquals("[7, 9]", Arrays.toString(ml.locateInFrom(3, 3)));
229     assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
230     assertEquals("[1, 6]", Arrays.toString(ml.locateInFrom(1, 2)));
231     assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(1, 3)));
232     assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 4)));
233     assertEquals("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3)));
234     assertEquals("[4, 12]", Arrays.toString(ml.locateInFrom(2, 4)));
235     assertEquals("[7, 12]", Arrays.toString(ml.locateInFrom(3, 4)));
236     assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
237
238     assertNull(ml.locateInFrom(0, 0));
239     assertNull(ml.locateInFrom(1, 5));
240     assertNull(ml.locateInFrom(-1, 1));
241   }
242
243   /**
244    * Tests for method that locates ranges in the 'from' map for given range in
245    * the 'to' map.
246    */
247   @Test(groups = { "Functional" })
248   public void testLocateInFrom_withIntrons()
249   {
250     /*
251      * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
252      * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
253      */
254     int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
255     int[] protein = { 1, 4 };
256     MapList ml = new MapList(codons, protein, 3, 1);
257     assertEquals("[2, 3, 5, 5]", Arrays.toString(ml.locateInFrom(1, 1)));
258     assertEquals("[6, 7, 9, 9]", Arrays.toString(ml.locateInFrom(2, 2)));
259     assertEquals("[10, 10, 12, 12, 14, 14]",
260             Arrays.toString(ml.locateInFrom(3, 3)));
261     assertEquals("[16, 18]", Arrays.toString(ml.locateInFrom(4, 4)));
262   }
263
264   /**
265    * Tests for method that locates ranges in the 'to' map for given range in the
266    * 'from' map.
267    */
268   @Test(groups = { "Functional" })
269   public void testLocateInTo_noIntrons()
270   {
271     /*
272      * Simple mapping with no introns
273      */
274     int[] codons = new int[] { 1, 12 };
275     int[] protein = new int[] { 1, 4 };
276     MapList ml = new MapList(codons, protein, 3, 1);
277     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 3)));
278     assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
279     assertEquals("[3, 3]", Arrays.toString(ml.locateInTo(7, 9)));
280     assertEquals("[4, 4]", Arrays.toString(ml.locateInTo(10, 12)));
281     assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 6)));
282     assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(1, 9)));
283     assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(1, 12)));
284     assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
285     assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(4, 12)));
286
287     /*
288      * A part codon is treated as if a whole one.
289      */
290     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
291     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 2)));
292     assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 4)));
293     assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(2, 8)));
294     assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
295     assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(5, 11)));
296
297     assertNull(ml.locateInTo(0, 0));
298     assertNull(ml.locateInTo(1, 13));
299     assertNull(ml.locateInTo(-1, 1));
300   }
301
302   /**
303    * Tests for method that locates ranges in the 'to' map for given range in the
304    * 'from' map.
305    */
306   @Test(groups = { "Functional" })
307   public void testLocateInTo_withIntrons()
308   {
309     /*
310      * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
311      * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
312      */
313     int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
314     /*
315      * Mapped proteins at positions 1, 3, 4, 6 in the sequence
316      */
317     int[] protein = { 1, 1, 3, 4, 6, 6 };
318     MapList ml = new MapList(codons, protein, 3, 1);
319
320     /*
321      * Can't map from an unmapped position
322      */
323     assertNull(ml.locateInTo(1, 2));
324     assertNull(ml.locateInTo(2, 4));
325     assertNull(ml.locateInTo(4, 4));
326
327     /*
328      * Valid range or subrange of codon1 maps to protein1.
329      */
330     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 2)));
331     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 3)));
332     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 5)));
333     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 3)));
334     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 5)));
335
336     // codon position 6 starts the next protein:
337     assertEquals("[1, 1, 3, 3]", Arrays.toString(ml.locateInTo(3, 6)));
338
339     // codon positions 7 to 17 (part) cover proteins 2/3/4 at positions 3/4/6
340     assertEquals("[3, 4, 6, 6]", Arrays.toString(ml.locateInTo(7, 17)));
341
342   }
343
344   /**
345    * Test equals method.
346    */
347   @Test(groups = { "Functional" })
348   public void testEquals()
349   {
350     int[] codons = new int[] { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
351     int[] protein = new int[] { 1, 4 };
352     MapList ml = new MapList(codons, protein, 3, 1);
353     MapList ml1 = new MapList(codons, protein, 3, 1); // same values
354     MapList ml2 = new MapList(codons, protein, 2, 1); // fromRatio differs
355     MapList ml3 = new MapList(codons, protein, 3, 2); // toRatio differs
356     codons[2] = 4;
357     MapList ml6 = new MapList(codons, protein, 3, 1); // fromShifts differ
358     protein[1] = 3;
359     MapList ml7 = new MapList(codons, protein, 3, 1); // toShifts differ
360
361     assertTrue(ml.equals(ml));
362     assertTrue(ml.equals(ml1));
363     assertTrue(ml1.equals(ml));
364
365     assertFalse(ml.equals(null));
366     assertFalse(ml.equals("hello"));
367     assertFalse(ml.equals(ml2));
368     assertFalse(ml.equals(ml3));
369     assertFalse(ml.equals(ml6));
370     assertFalse(ml.equals(ml7));
371     assertFalse(ml6.equals(ml7));
372
373     try
374     {
375       MapList ml4 = new MapList(codons, null, 3, 1); // toShifts null
376       assertFalse(ml.equals(ml4));
377     } catch (NullPointerException e)
378     {
379       // actually thrown by constructor before equals can be called
380     }
381     try
382     {
383       MapList ml5 = new MapList(null, protein, 3, 1); // fromShifts null
384       assertFalse(ml.equals(ml5));
385     } catch (NullPointerException e)
386     {
387       // actually thrown by constructor before equals can be called
388     }
389   }
390
391   /**
392    * Test for the method that flattens a list of ranges into a single array.
393    */
394   @Test(groups = { "Functional" })
395   public void testGetRanges()
396   {
397     List<int[]> ranges = new ArrayList<int[]>();
398     ranges.add(new int[] { 2, 3 });
399     ranges.add(new int[] { 5, 6 });
400     assertEquals("[2, 3, 5, 6]", Arrays.toString(MapList.getRanges(ranges)));
401   }
402
403   /**
404    * Check state after construction
405    */
406   @Test(groups = { "Functional" })
407   public void testConstructor()
408   {
409     int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
410     int[] protein = { 1, 1, 3, 4, 6, 6 };
411     MapList ml = new MapList(codons, protein, 3, 1);
412     assertEquals(3, ml.getFromRatio());
413     assertEquals(2, ml.getFromLowest());
414     assertEquals(18, ml.getFromHighest());
415     assertEquals(1, ml.getToLowest());
416     assertEquals(6, ml.getToHighest());
417     assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
418             prettyPrint(ml.getFromRanges()));
419     assertEquals("{[1, 1], [3, 4], [6, 6]}", prettyPrint(ml.getToRanges()));
420
421     /*
422      * Also copy constructor
423      */
424     MapList ml2 = new MapList(ml);
425     assertEquals(3, ml2.getFromRatio());
426     assertEquals(2, ml2.getFromLowest());
427     assertEquals(18, ml2.getFromHighest());
428     assertEquals(1, ml2.getToLowest());
429     assertEquals(6, ml2.getToHighest());
430     assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
431             prettyPrint(ml2.getFromRanges()));
432     assertEquals("{[1, 1], [3, 4], [6, 6]}", prettyPrint(ml2.getToRanges()));
433   }
434
435   /**
436    * Convert a List of {[i, j], [k, l], ...} to "[[i, j], [k, l], ...]"
437    * 
438    * @param ranges
439    * @return
440    */
441   private String prettyPrint(List<int[]> ranges)
442   {
443     StringBuilder sb = new StringBuilder(ranges.size() * 5);
444     boolean first = true;
445     sb.append("{");
446     for (int[] range : ranges)
447     {
448       if (!first)
449       {
450         sb.append(", ");
451       }
452       sb.append(Arrays.toString(range));
453       first = false;
454     }
455     sb.append("}");
456     return sb.toString();
457   }
458
459   /**
460    * Test the method that creates an inverse mapping
461    */
462   @Test(groups = { "Functional" })
463   public void testGetInverse()
464   {
465     int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
466     int[] protein = { 1, 1, 3, 4, 6, 6 };
467
468     MapList ml = new MapList(codons, protein, 3, 1);
469     MapList ml2 = ml.getInverse();
470     assertEquals(ml.getFromRatio(), ml2.getToRatio());
471     assertEquals(ml.getFromRatio(), ml2.getToRatio());
472     assertEquals(ml.getToHighest(), ml2.getFromHighest());
473     assertEquals(ml.getFromHighest(), ml2.getToHighest());
474     assertEquals(prettyPrint(ml.getFromRanges()),
475             prettyPrint(ml2.getToRanges()));
476     assertEquals(prettyPrint(ml.getToRanges()),
477             prettyPrint(ml2.getFromRanges()));
478   }
479
480   @Test(groups = { "Functional" })
481   public void testToString()
482   {
483     MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 }, new int[] {
484         51, 1 }, 1, 3);
485     String s = ml.toString();
486     assertEquals("From (1:3) [ [1, 5] [10, 15] [25, 20] ] To [ [51, 1] ]",
487             s);
488   }
489 }