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