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