Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.util

File MapListTest.java

 

Code metrics

50
464
25
1
984
704
52
0.11
18.56
25
2.08

Classes

Class Line # Actions
MapListTest 39 464 52
0.985157798.5%
 

Contributing tests

This file is covered by 21 tests. .

Source view

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