Clover icon

Coverage Report

  1. Project Clover database Tue Mar 10 2026 14:58:44 GMT
  2. Package jalview.util

File MapListTest.java

 

Code metrics

52
677
34
1
1,464
1,008
63
0.09
19.91
34
1.85

Classes

Class Line # Actions
MapListTest 42 677 63
0.00%
 

Contributing tests

No tests hitting this source file were found.

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.AssertJUnit.fail;
29    import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
30   
31    import java.util.ArrayList;
32    import java.util.Arrays;
33    import java.util.BitSet;
34    import java.util.List;
35   
36    import org.testng.annotations.BeforeClass;
37    import org.testng.annotations.Test;
38   
39    import jalview.bin.Console;
40    import jalview.gui.JvOptionPane;
41   
 
42    public class MapListTest
43    {
 
44  0 toggle @BeforeClass(alwaysRun = true)
45    public void setUp()
46    {
47  0 Console.initLogger();
48    }
49   
 
50  0 toggle @BeforeClass(alwaysRun = true)
51    public void setUpJvOptionPane()
52    {
53  0 JvOptionPane.setInteractiveMode(false);
54  0 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
55    }
56   
 
57  0 toggle @Test(groups = { "Functional" }, enabled = false)
58    public void testSomething()
59    {
60  0 MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 },
61    new int[]
62    { 51, 1 }, 1, 3);
63  0 MapList ml1 = new MapList(new int[] { 1, 3, 17, 4 },
64    new int[]
65    { 51, 1 }, 1, 3);
66  0 MapList ml2 = new MapList(new int[] { 1, 60 }, new int[] { 1, 20 }, 3,
67    1);
68    // test internal consistency
69  0 int to[] = new int[51];
70  0 testMap(ml, 1, 60);
71  0 MapList mldna = new MapList(new int[] { 2, 2, 6, 8, 12, 16 },
72    new int[]
73    { 1, 3 }, 3, 1);
74  0 int[] frm = mldna.locateInFrom(1, 1);
75  0 testLocateFrom(mldna, 1, 1, new int[] { 2, 2, 6, 7 });
76  0 testMap(mldna, 1, 3);
77    /*
78    * for (int from=1; from<=51; from++) { int[] too=ml.shiftTo(from); int[]
79    * toofrom=ml.shiftFrom(too[0]);
80    * System.out.println("ShiftFrom("+from+")=="+too[0]+" %
81    * "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]); }
82    */
83    }
84   
 
85  0 toggle private static void testLocateFrom(MapList mldna, int i, int j, int[] ks)
86    {
87  0 int[] frm = mldna.locateInFrom(i, j);
88  0 assertEquals("Failed test locate from " + i + " to " + j,
89    Arrays.toString(frm), Arrays.toString(ks));
90    }
91   
92    /**
93    * test routine. not incremental.
94    *
95    * @param ml
96    * @param fromS
97    * @param fromE
98    */
 
99  0 toggle private void testMap(MapList ml, int fromS, int fromE)
100    {
101    // todo convert to JUnit style tests
102  0 for (int from = 1; from <= 25; from++)
103    {
104  0 int[] too = ml.shiftFrom(from);
105  0 System.out.print("ShiftFrom(" + from + ")==");
106  0 if (too == null)
107    {
108  0 System.out.print("NaN\n");
109    }
110    else
111    {
112  0 System.out.print(too[0] + " % " + too[1] + " (" + too[2] + ")");
113  0 System.out.print("\t+--+\t");
114  0 int[] toofrom = ml.shiftTo(too[0]);
115  0 if (toofrom != null)
116    {
117  0 if (toofrom[0] != from)
118    {
119  0 System.err.println("Mapping not reflexive:" + from + " "
120    + too[0] + "->" + toofrom[0]);
121    }
122  0 System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0]
123    + " % " + toofrom[1] + " (" + toofrom[2] + ")");
124    }
125    else
126    {
127  0 System.out.println("ShiftTo(" + too[0] + ")=="
128    + "NaN! - not Bijective Mapping!");
129    }
130    }
131    }
132  0 int mmap[][] = ml.makeFromMap();
133  0 System.out.println("FromMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
134    + mmap[0][2] + " " + mmap[0][3] + " ");
135  0 for (int i = 1; i <= mmap[1].length; i++)
136    {
137  0 if (mmap[1][i - 1] == -1)
138    {
139  0 System.out.print(i + "=XXX");
140   
141    }
142    else
143    {
144  0 System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
145    }
146  0 if (i % 20 == 0)
147    {
148  0 System.out.print("\n");
149    }
150    else
151    {
152  0 System.out.print(",");
153    }
154    }
155    // test range function
156  0 System.out.print("\nTest locateInFrom\n");
157    {
158  0 int f = mmap[0][2], t = mmap[0][3];
159  0 while (f <= t)
160    {
161  0 System.out.println("Range " + f + " to " + t);
162  0 int rng[] = ml.locateInFrom(f, t);
163  0 if (rng != null)
164    {
165  0 for (int i = 0; i < rng.length; i++)
166    {
167  0 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
168    }
169    }
170    else
171    {
172  0 System.out.println("No range!");
173    }
174  0 System.out.print("\nReversed\n");
175  0 rng = ml.locateInFrom(t, f);
176  0 if (rng != null)
177    {
178  0 for (int i = 0; i < rng.length; i++)
179    {
180  0 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
181    }
182    }
183    else
184    {
185  0 System.out.println("No range!");
186    }
187  0 System.out.print("\n");
188  0 f++;
189  0 t--;
190    }
191    }
192  0 System.out.print("\n");
193  0 mmap = ml.makeToMap();
194  0 System.out.println("ToMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
195    + mmap[0][2] + " " + mmap[0][3] + " ");
196  0 for (int i = 1; i <= mmap[1].length; i++)
197    {
198  0 if (mmap[1][i - 1] == -1)
199    {
200  0 System.out.print(i + "=XXX");
201   
202    }
203    else
204    {
205  0 System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
206    }
207  0 if (i % 20 == 0)
208    {
209  0 System.out.print("\n");
210    }
211    else
212    {
213  0 System.out.print(",");
214    }
215    }
216  0 System.out.print("\n");
217    // test range function
218  0 System.out.print("\nTest locateInTo\n");
219    {
220  0 int f = mmap[0][2], t = mmap[0][3];
221  0 while (f <= t)
222    {
223  0 System.out.println("Range " + f + " to " + t);
224  0 int rng[] = ml.locateInTo(f, t);
225  0 if (rng != null)
226    {
227  0 for (int i = 0; i < rng.length; i++)
228    {
229  0 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
230    }
231    }
232    else
233    {
234  0 System.out.println("No range!");
235    }
236  0 System.out.print("\nReversed\n");
237  0 rng = ml.locateInTo(t, f);
238  0 if (rng != null)
239    {
240  0 for (int i = 0; i < rng.length; i++)
241    {
242  0 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
243    }
244    }
245    else
246    {
247  0 System.out.println("No range!");
248    }
249  0 f++;
250  0 t--;
251  0 System.out.print("\n");
252    }
253    }
254    }
255   
256    /**
257    * Tests for method that locates ranges in the 'from' map for given range in
258    * the 'to' map.
259    */
 
260  0 toggle @Test(groups = { "Functional" })
261    public void testLocateInFrom_noIntrons()
262    {
263    /*
264    * Simple mapping with no introns
265    */
266  0 int[] codons = new int[] { 1, 12 };
267  0 int[] protein = new int[] { 1, 4 };
268  0 MapList ml = new MapList(codons, protein, 3, 1);
269  0 assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(1, 1)));
270  0 assertEquals("[4, 6]", Arrays.toString(ml.locateInFrom(2, 2)));
271  0 assertEquals("[7, 9]", Arrays.toString(ml.locateInFrom(3, 3)));
272  0 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
273  0 assertEquals("[1, 6]", Arrays.toString(ml.locateInFrom(1, 2)));
274  0 assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(1, 3)));
275    // reversed range treated as if forwards:
276  0 assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(3, 1)));
277  0 assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 4)));
278  0 assertEquals("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3)));
279  0 assertEquals("[4, 12]", Arrays.toString(ml.locateInFrom(2, 4)));
280  0 assertEquals("[7, 12]", Arrays.toString(ml.locateInFrom(3, 4)));
281  0 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
282   
283    /*
284    * partial overlap
285    */
286  0 assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 5)));
287  0 assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(-1, 1)));
288   
289    /*
290    * no overlap
291    */
292  0 assertNull(ml.locateInFrom(0, 0));
293   
294    }
295   
296    /**
297    * Tests for method that locates ranges in the 'from' map for given range in
298    * the 'to' map.
299    */
 
300  0 toggle @Test(groups = { "Functional" })
301    public void testLocateInFrom_withIntrons()
302    {
303    /*
304    * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
305    * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
306    */
307  0 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
308  0 int[] protein = { 1, 4 };
309  0 MapList ml = new MapList(codons, protein, 3, 1);
310  0 assertEquals("[2, 3, 5, 5]", Arrays.toString(ml.locateInFrom(1, 1)));
311  0 assertEquals("[6, 7, 9, 9]", Arrays.toString(ml.locateInFrom(2, 2)));
312  0 assertEquals("[10, 10, 12, 12, 14, 14]",
313    Arrays.toString(ml.locateInFrom(3, 3)));
314  0 assertEquals("[16, 18]", Arrays.toString(ml.locateInFrom(4, 4)));
315   
316    /*
317    * codons at 11-16, 21-26, 31-36 mapped to peptide positions 1, 3-4, 6-8
318    */
319  0 ml = new MapList(new int[] { 11, 16, 21, 26, 31, 36 },
320    new int[]
321    { 1, 1, 3, 4, 6, 8 }, 3, 1);
322  0 assertArrayEquals(new int[] { 11, 13 }, ml.locateInFrom(1, 1));
323  0 assertArrayEquals(new int[] { 11, 16 }, ml.locateInFrom(1, 3));
324  0 assertArrayEquals(new int[] { 11, 16, 21, 23 }, ml.locateInFrom(1, 4));
325  0 assertArrayEquals(new int[] { 14, 16, 21, 23 }, ml.locateInFrom(3, 4));
326   
327    }
328   
 
329  0 toggle @Test(groups = { "Functional" })
330    public void testLocateInFrom_reverseStrand()
331    {
332  0 int[] codons = new int[] { 12, 1 };
333  0 int[] protein = new int[] { 1, 4 };
334  0 MapList ml = new MapList(codons, protein, 3, 1);
335  0 assertEquals("[12, 10]", Arrays.toString(ml.locateInFrom(1, 1)));
336  0 assertEquals("[9, 4]", Arrays.toString(ml.locateInFrom(2, 3)));
337    }
338   
339    /**
340    * Tests for method that locates the overlap of the ranges in the 'from' map
341    * for given range in the 'to' map
342    */
 
343  0 toggle @Test(groups = { "Functional" })
344    public void testGetOverlapsInFrom_withIntrons()
345    {
346    /*
347    * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
348    * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
349    */
350  0 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
351  0 int[] protein = { 11, 14 };
352  0 MapList ml = new MapList(codons, protein, 3, 1);
353   
354  0 assertEquals("[2, 3, 5, 5]",
355    Arrays.toString(ml.getOverlapsInFrom(11, 11)));
356  0 assertEquals("[2, 3, 5, 7, 9, 9]",
357    Arrays.toString(ml.getOverlapsInFrom(11, 12)));
358    // out of range 5' :
359  0 assertEquals("[2, 3, 5, 7, 9, 9]",
360    Arrays.toString(ml.getOverlapsInFrom(8, 12)));
361    // out of range 3' :
362  0 assertEquals("[10, 10, 12, 12, 14, 14, 16, 18]",
363    Arrays.toString(ml.getOverlapsInFrom(13, 16)));
364    // out of range both :
365  0 assertEquals("[2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18]",
366    Arrays.toString(ml.getOverlapsInFrom(1, 16)));
367    // no overlap:
368  0 assertNull(ml.getOverlapsInFrom(20, 25));
369    }
370   
371    /**
372    * Tests for method that locates the overlap of the ranges in the 'to' map for
373    * given range in the 'from' map
374    */
 
375  0 toggle @Test(groups = { "Functional" })
376    public void testGetOverlapsInTo_withIntrons()
377    {
378    /*
379    * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [17, 18, 19] i.e.
380    * 2-3, 5-7, 9-10, 12-12, 14-14, 17-19
381    */
382  0 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 17, 19 };
383    /*
384    * Mapped proteins at positions 1, 3, 4, 6 in the sequence
385    */
386  0 int[] protein = { 1, 1, 3, 4, 6, 6 };
387  0 MapList ml = new MapList(codons, protein, 3, 1);
388   
389    /*
390    * Can't map from an unmapped position
391    */
392  0 assertNull(ml.getOverlapsInTo(1, 1));
393  0 assertNull(ml.getOverlapsInTo(4, 4));
394  0 assertNull(ml.getOverlapsInTo(15, 16));
395   
396    /*
397    * nor from a range that includes no mapped position (exon)
398    */
399  0 assertNull(ml.getOverlapsInTo(15, 16));
400   
401    // end of codon 1 maps to first peptide
402  0 assertEquals("[1, 1]", Arrays.toString(ml.getOverlapsInTo(2, 2)));
403    // end of codon 1 and start of codon 2 maps to first 2 peptides
404  0 assertEquals("[1, 1, 3, 3]", Arrays.toString(ml.getOverlapsInTo(3, 7)));
405   
406    // range overlaps 5' end of dna:
407  0 assertEquals("[1, 1, 3, 3]", Arrays.toString(ml.getOverlapsInTo(1, 6)));
408  0 assertEquals("[1, 1, 3, 3]", Arrays.toString(ml.getOverlapsInTo(1, 8)));
409   
410    // range overlaps 3' end of dna:
411  0 assertEquals("[6, 6]", Arrays.toString(ml.getOverlapsInTo(17, 24)));
412  0 assertEquals("[6, 6]", Arrays.toString(ml.getOverlapsInTo(16, 24)));
413   
414    // dna positions 8, 11 are intron but include end of exon 2 and start of
415    // exon 3
416  0 assertEquals("[3, 4]", Arrays.toString(ml.getOverlapsInTo(8, 11)));
417    }
418   
419    /**
420    * Tests for method that locates ranges in the 'to' map for given range in the
421    * 'from' map.
422    */
 
423  0 toggle @Test(groups = { "Functional" })
424    public void testLocateInTo_noIntrons()
425    {
426    /*
427    * Simple mapping with no introns
428    */
429  0 int[] codons = new int[] { 1, 12 };
430  0 int[] protein = new int[] { 1, 4 };
431  0 MapList ml = new MapList(codons, protein, 3, 1);
432  0 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 3)));
433  0 assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
434  0 assertEquals("[3, 3]", Arrays.toString(ml.locateInTo(7, 9)));
435  0 assertEquals("[4, 4]", Arrays.toString(ml.locateInTo(10, 12)));
436  0 assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 6)));
437  0 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(1, 9)));
438  0 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(1, 12)));
439  0 assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
440  0 assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(4, 12)));
441    // reverse range treated as if forwards:
442  0 assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(12, 4)));
443   
444    /*
445    * A part codon is treated as if a whole one.
446    */
447  0 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
448  0 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 2)));
449  0 assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 4)));
450  0 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(2, 8)));
451  0 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
452  0 assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(5, 11)));
453   
454    /*
455    * partial overlap
456    */
457  0 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(1, 13)));
458  0 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(-1, 2)));
459   
460    /*
461    * no overlap
462    */
463  0 assertNull(ml.locateInTo(0, 0));
464    }
465   
466    /**
467    * Tests for method that locates ranges in the 'to' map for given range in the
468    * 'from' map.
469    */
 
470  0 toggle @Test(groups = { "Functional" })
471    public void testLocateInTo_withIntrons()
472    {
473    /*
474    * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
475    * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
476    */
477  0 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
478    /*
479    * Mapped proteins at positions 1, 3, 4, 6 in the sequence
480    */
481  0 int[] protein = { 1, 1, 3, 4, 6, 6 };
482  0 MapList ml = new MapList(codons, protein, 3, 1);
483   
484    /*
485    * Valid range or subrange of codon1 maps to protein1
486    */
487  0 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 2)));
488  0 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 3)));
489  0 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 5)));
490  0 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 3)));
491  0 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 5)));
492   
493    // codon position 6 starts the next protein:
494  0 assertEquals("[1, 1, 3, 3]", Arrays.toString(ml.locateInTo(3, 6)));
495   
496    // codon positions 7 to 17 (part) cover proteins 2/3/4 at positions 3/4/6
497  0 assertEquals("[3, 4, 6, 6]", Arrays.toString(ml.locateInTo(7, 17)));
498   
499    /*
500    * partial overlap
501    */
502  0 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 2)));
503  0 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 4)));
504  0 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 4)));
505   
506    /*
507    * no overlap
508    */
509  0 assertNull(ml.locateInTo(4, 4));
510    }
511   
512    /**
513    * Test equals method.
514    */
 
515  0 toggle @Test(groups = { "Functional" })
516    public void testEquals()
517    {
518  0 int[] codons = new int[] { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
519  0 int[] protein = new int[] { 1, 4 };
520  0 MapList ml = new MapList(codons, protein, 3, 1);
521  0 MapList ml1 = new MapList(codons, protein, 3, 1); // same values
522  0 MapList ml2 = new MapList(codons, protein, 2, 1); // fromRatio differs
523  0 MapList ml3 = new MapList(codons, protein, 3, 2); // toRatio differs
524  0 codons[2] = 4;
525  0 MapList ml6 = new MapList(codons, protein, 3, 1); // fromShifts differ
526  0 protein[1] = 3;
527  0 MapList ml7 = new MapList(codons, protein, 3, 1); // toShifts differ
528   
529  0 assertTrue(ml.equals(ml));
530  0 assertEquals(ml.hashCode(), ml.hashCode());
531  0 assertTrue(ml.equals(ml1));
532  0 assertEquals(ml.hashCode(), ml1.hashCode());
533  0 assertTrue(ml1.equals(ml));
534   
535  0 assertFalse(ml.equals(null));
536  0 assertFalse(ml.equals("hello"));
537  0 assertFalse(ml.equals(ml2));
538  0 assertFalse(ml.equals(ml3));
539  0 assertFalse(ml.equals(ml6));
540  0 assertFalse(ml.equals(ml7));
541  0 assertFalse(ml6.equals(ml7));
542   
543  0 try
544    {
545  0 MapList ml4 = new MapList(codons, null, 3, 1); // toShifts null
546  0 assertFalse(ml.equals(ml4));
547    } catch (NullPointerException e)
548    {
549    // actually thrown by constructor before equals can be called
550    }
551  0 try
552    {
553  0 MapList ml5 = new MapList(null, protein, 3, 1); // fromShifts null
554  0 assertFalse(ml.equals(ml5));
555    } catch (NullPointerException e)
556    {
557    // actually thrown by constructor before equals can be called
558    }
559    }
560   
561    /**
562    * Test for the method that flattens a list of ranges into a single array.
563    */
 
564  0 toggle @Test(groups = { "Functional" })
565    public void testGetRanges()
566    {
567  0 List<int[]> ranges = new ArrayList<>();
568  0 ranges.add(new int[] { 2, 3 });
569  0 ranges.add(new int[] { 5, 6 });
570  0 assertEquals("[2, 3, 5, 6]",
571    Arrays.toString(MapList.getRanges(ranges)));
572    }
573   
574    /**
575    * Check state after construction
576    */
 
577  0 toggle @Test(groups = { "Functional" })
578    public void testConstructor()
579    {
580  0 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
581  0 int[] protein = { 1, 1, 3, 4, 6, 6 };
582  0 MapList ml = new MapList(codons, protein, 3, 1);
583  0 assertEquals(3, ml.getFromRatio());
584  0 assertEquals(2, ml.getFromLowest());
585  0 assertEquals(18, ml.getFromHighest());
586  0 assertEquals(1, ml.getToLowest());
587  0 assertEquals(6, ml.getToHighest());
588  0 assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
589    prettyPrint(ml.getFromRanges()));
590  0 assertEquals("{[1, 1], [3, 4], [6, 6]}", prettyPrint(ml.getToRanges()));
591   
592    /*
593    * Also copy constructor
594    */
595  0 MapList ml2 = new MapList(ml);
596  0 assertEquals(3, ml2.getFromRatio());
597  0 assertEquals(2, ml2.getFromLowest());
598  0 assertEquals(18, ml2.getFromHighest());
599  0 assertEquals(1, ml2.getToLowest());
600  0 assertEquals(6, ml2.getToHighest());
601  0 assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
602    prettyPrint(ml2.getFromRanges()));
603  0 assertEquals("{[1, 1], [3, 4], [6, 6]}",
604    prettyPrint(ml2.getToRanges()));
605   
606    /*
607    * reverse direction
608    */
609  0 codons = new int[] { 9, 6 };
610  0 protein = new int[] { 100, 91, 80, 79 };
611  0 ml = new MapList(codons, protein, 3, 1);
612  0 assertEquals(6, ml.getFromLowest());
613  0 assertEquals(9, ml.getFromHighest());
614  0 assertEquals(79, ml.getToLowest());
615  0 assertEquals(100, ml.getToHighest());
616    }
617   
618    /**
619    * Test constructor used to merge consecutive ranges but now just leaves them
620    * as supplied (JAL-3751)
621    */
 
622  0 toggle @Test(groups = { "Functional" })
623    public void testConstructor_mergeRanges()
624    {
625  0 int[] codons = { 2, 3, 3, 7, 9, 10, 12, 12, 13, 14, 16, 17 };
626  0 int[] protein = { 1, 1, 2, 3, 6, 6 };
627  0 MapList ml = new MapList(codons, protein, 3, 1);
628  0 assertEquals(3, ml.getFromRatio());
629  0 assertEquals(2, ml.getFromLowest());
630  0 assertEquals(17, ml.getFromHighest());
631  0 assertEquals(1, ml.getToLowest());
632  0 assertEquals(6, ml.getToHighest());
633  0 assertEquals("{[2, 3], [3, 7], [9, 10], [12, 12], [13, 14], [16, 17]}",
634    prettyPrint(ml.getFromRanges()));
635  0 assertEquals("{[1, 1], [2, 3], [6, 6]}", prettyPrint(ml.getToRanges()));
636    }
637   
638    /**
639    * Convert a List of {[i, j], [k, l], ...} to "[[i, j], [k, l], ...]"
640    *
641    * @param ranges
642    * @return
643    */
 
644  0 toggle private String prettyPrint(List<int[]> ranges)
645    {
646  0 StringBuilder sb = new StringBuilder(ranges.size() * 5);
647  0 boolean first = true;
648  0 sb.append("{");
649  0 for (int[] range : ranges)
650    {
651  0 if (!first)
652    {
653  0 sb.append(", ");
654    }
655  0 sb.append(Arrays.toString(range));
656  0 first = false;
657    }
658  0 sb.append("}");
659  0 return sb.toString();
660    }
661   
662    /**
663    * Test the method that creates an inverse mapping
664    */
 
665  0 toggle @Test(groups = { "Functional" })
666    public void testGetInverse()
667    {
668  0 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
669  0 int[] protein = { 1, 1, 3, 4, 6, 6 };
670   
671  0 MapList ml = new MapList(codons, protein, 3, 1);
672  0 MapList ml2 = ml.getInverse();
673  0 assertEquals(ml.getFromRatio(), ml2.getToRatio());
674  0 assertEquals(ml.getFromRatio(), ml2.getToRatio());
675  0 assertEquals(ml.getToHighest(), ml2.getFromHighest());
676  0 assertEquals(ml.getFromHighest(), ml2.getToHighest());
677  0 assertEquals(prettyPrint(ml.getFromRanges()),
678    prettyPrint(ml2.getToRanges()));
679  0 assertEquals(prettyPrint(ml.getToRanges()),
680    prettyPrint(ml2.getFromRanges()));
681    }
682   
 
683  0 toggle @Test(groups = { "Functional" })
684    public void testToString()
685    {
686  0 MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 },
687    new int[]
688    { 51, 1 }, 1, 3);
689  0 String s = ml.toString();
690  0 assertEquals("[ [1, 5] [10, 15] [25, 20] ] 1:3 to [ [51, 1] ]", s);
691    }
692   
 
693  0 toggle @Test(groups = { "Functional" })
694    public void testAddMapList()
695    {
696  0 MapList ml = new MapList(new int[] { 11, 15, 20, 25, 35, 30 },
697    new int[]
698    { 72, 22 }, 1, 3);
699  0 assertEquals(11, ml.getFromLowest());
700  0 assertEquals(35, ml.getFromHighest());
701  0 assertEquals(22, ml.getToLowest());
702  0 assertEquals(72, ml.getToHighest());
703   
704  0 MapList ml2 = new MapList(new int[] { 2, 4, 37, 40 },
705    new int[]
706    { 12, 17, 78, 83, 88, 96 }, 1, 3);
707  0 ml.addMapList(ml2);
708  0 assertEquals(2, ml.getFromLowest());
709  0 assertEquals(40, ml.getFromHighest());
710  0 assertEquals(12, ml.getToLowest());
711  0 assertEquals(96, ml.getToHighest());
712   
713  0 String s = ml.toString();
714  0 assertEquals(
715    "[ [11, 15] [20, 25] [35, 30] [2, 4] [37, 40] ] 1:3 to [ [72, 22] [12, 17] [78, 83] [88, 96] ]",
716    s);
717    }
718   
719    /**
720    * Test that confirms adding a map twice does nothing
721    */
 
722  0 toggle @Test(groups = { "Functional" })
723    public void testAddMapList_sameMap()
724    {
725  0 MapList ml = new MapList(new int[] { 11, 15, 20, 25, 35, 30 },
726    new int[]
727    { 72, 22 }, 1, 3);
728  0 String before = ml.toString();
729  0 ml.addMapList(ml);
730  0 assertEquals(before, ml.toString());
731  0 ml.addMapList(new MapList(ml));
732  0 assertEquals(before, ml.toString());
733    }
734   
 
735  0 toggle @Test(groups = { "Functional" })
736    public void testAddMapList_contiguous()
737    {
738  0 MapList ml = new MapList(new int[] { 11, 15 }, new int[] { 72, 58 }, 1,
739    3);
740   
741  0 MapList ml2 = new MapList(new int[] { 15, 16 }, new int[] { 58, 53 }, 1,
742    3);
743  0 ml.addMapList(ml2);
744  0 assertEquals("[ [11, 16] ] 1:3 to [ [72, 53] ]", ml.toString());
745    }
746   
747    /**
748    * Check state after construction
749    */
 
750  0 toggle @Test(groups = { "Functional" })
751    public void testConstructor_withLists()
752    {
753    /*
754    * reverse direction
755    */
756  0 int[][] codons = new int[][] { { 9, 6 } };
757  0 int[][] protein = new int[][] { { 100, 91 }, { 80, 79 } };
758  0 MapList ml = new MapList(Arrays.asList(codons), Arrays.asList(protein),
759    3, 1);
760  0 assertEquals(6, ml.getFromLowest());
761  0 assertEquals(9, ml.getFromHighest());
762  0 assertEquals(79, ml.getToLowest());
763  0 assertEquals(100, ml.getToHighest());
764    }
765   
766    /**
767    * Test that method that inspects for the (first) forward or reverse from
768    * range. Single position ranges are ignored.
769    */
 
770  0 toggle @Test(groups = { "Functional" })
771    public void testIsFromForwardStrand()
772    {
773    // [3-9] declares forward strand
774  0 MapList ml = new MapList(new int[] { 2, 2, 3, 9, 12, 11 },
775    new int[]
776    { 20, 11 }, 1, 1);
777  0 assertTrue(ml.isFromForwardStrand());
778   
779    // [11-5] declares reverse strand ([13-14] is ignored)
780  0 ml = new MapList(new int[] { 2, 2, 11, 5, 13, 14 },
781    new int[]
782    { 20, 11 }, 1, 1);
783  0 assertFalse(ml.isFromForwardStrand());
784   
785    // all single position ranges - defaults to forward strand
786  0 ml = new MapList(new int[] { 2, 2, 4, 4, 6, 6 }, new int[] { 3, 1 }, 1,
787    1);
788  0 assertTrue(ml.isFromForwardStrand());
789    }
790   
791    /**
792    * Test the method that merges contiguous ranges
793    */
 
794  0 toggle @Test(groups = { "Functional" })
795    public void testCoalesceRanges()
796    {
797  0 assertNull(MapList.coalesceRanges(null));
798  0 List<int[]> ranges = new ArrayList<>();
799  0 assertSame(ranges, MapList.coalesceRanges(ranges));
800  0 ranges.add(new int[] { 1, 3 });
801  0 assertSame(ranges, MapList.coalesceRanges(ranges));
802   
803    // add non-contiguous range:
804  0 ranges.add(new int[] { 5, 6 });
805  0 assertSame(ranges, MapList.coalesceRanges(ranges));
806   
807    // 'contiguous' range in opposite direction is not merged:
808  0 ranges.add(new int[] { 7, 6 });
809  0 assertSame(ranges, MapList.coalesceRanges(ranges));
810   
811    // merging in forward direction:
812  0 ranges.clear();
813  0 ranges.add(new int[] { 1, 3 });
814  0 ranges.add(new int[] { 4, 5 }); // contiguous
815  0 ranges.add(new int[] { 5, 5 }); // overlap!
816  0 ranges.add(new int[] { 6, 7 }); // contiguous
817  0 List<int[]> merged = MapList.coalesceRanges(ranges);
818  0 assertEquals(2, merged.size());
819  0 assertArrayEquals(new int[] { 1, 5 }, merged.get(0));
820  0 assertArrayEquals(new int[] { 5, 7 }, merged.get(1));
821    // verify input list is unchanged
822  0 assertEquals(4, ranges.size());
823  0 assertArrayEquals(new int[] { 1, 3 }, ranges.get(0));
824  0 assertArrayEquals(new int[] { 4, 5 }, ranges.get(1));
825  0 assertArrayEquals(new int[] { 5, 5 }, ranges.get(2));
826  0 assertArrayEquals(new int[] { 6, 7 }, ranges.get(3));
827   
828    // merging in reverse direction:
829  0 ranges.clear();
830  0 ranges.add(new int[] { 7, 5 });
831  0 ranges.add(new int[] { 5, 4 }); // overlap
832  0 ranges.add(new int[] { 4, 4 }); // overlap
833  0 ranges.add(new int[] { 3, 1 }); // contiguous
834  0 merged = MapList.coalesceRanges(ranges);
835  0 assertEquals(3, merged.size());
836  0 assertArrayEquals(new int[] { 7, 5 }, merged.get(0));
837  0 assertArrayEquals(new int[] { 5, 4 }, merged.get(1));
838  0 assertArrayEquals(new int[] { 4, 1 }, merged.get(2));
839   
840    // merging with switches of direction:
841  0 ranges.clear();
842  0 ranges.add(new int[] { 1, 3 });
843  0 ranges.add(new int[] { 4, 5 }); // contiguous
844  0 ranges.add(new int[] { 5, 5 }); // overlap
845  0 ranges.add(new int[] { 6, 6 }); // contiguous
846  0 ranges.add(new int[] { 12, 10 });
847  0 ranges.add(new int[] { 9, 8 }); // contiguous
848  0 ranges.add(new int[] { 8, 8 }); // overlap
849  0 ranges.add(new int[] { 7, 7 }); // contiguous
850  0 merged = MapList.coalesceRanges(ranges);
851  0 assertEquals(4, merged.size());
852  0 assertArrayEquals(new int[] { 1, 5 }, merged.get(0));
853  0 assertArrayEquals(new int[] { 5, 6 }, merged.get(1));
854  0 assertArrayEquals(new int[] { 12, 8 }, merged.get(2));
855  0 assertArrayEquals(new int[] { 8, 7 }, merged.get(3));
856   
857    // 'subsumed' ranges are preserved
858  0 ranges.clear();
859  0 ranges.add(new int[] { 10, 30 });
860  0 ranges.add(new int[] { 15, 25 });
861   
862  0 merged = MapList.coalesceRanges(ranges);
863  0 assertEquals(2, merged.size());
864  0 assertArrayEquals(new int[] { 10, 30 }, merged.get(0));
865  0 assertArrayEquals(new int[] { 15, 25 }, merged.get(1));
866    }
867   
868    /**
869    * Test the method that compounds ('traverses') two mappings
870    */
 
871  0 toggle @Test(groups = "Functional")
872    public void testTraverse()
873    {
874    /*
875    * simple 1:1 plus 1:1 forwards
876    */
877  0 MapList ml1 = new MapList(new int[] { 3, 4, 8, 12 },
878    new int[]
879    { 5, 8, 11, 13 }, 1, 1);
880  0 assertEquals("{[3, 4], [8, 12]}", prettyPrint(ml1.getFromRanges()));
881  0 assertEquals("{[5, 8], [11, 13]}", prettyPrint(ml1.getToRanges()));
882   
883  0 MapList ml2 = new MapList(new int[] { 1, 50 },
884    new int[]
885    { 40, 45, 70, 75, 90, 127 }, 1, 1);
886  0 assertEquals("{[1, 50]}", prettyPrint(ml2.getFromRanges()));
887  0 assertEquals("{[40, 45], [70, 75], [90, 127]}",
888    prettyPrint(ml2.getToRanges()));
889   
890  0 MapList compound = ml1.traverse(ml2);
891   
892  0 assertEquals(1, compound.getFromRatio());
893  0 assertEquals(1, compound.getToRatio());
894  0 List<int[]> fromRanges = compound.getFromRanges();
895  0 assertEquals(2, fromRanges.size());
896  0 assertArrayEquals(new int[] { 3, 4 }, fromRanges.get(0));
897  0 assertArrayEquals(new int[] { 8, 12 }, fromRanges.get(1));
898  0 List<int[]> toRanges = compound.getToRanges();
899  0 assertEquals(4, toRanges.size());
900    // 5-8 maps to 44-45,70-71
901    // 11-13 maps to 74-75,90
902  0 assertArrayEquals(new int[] { 44, 45 }, toRanges.get(0));
903  0 assertArrayEquals(new int[] { 70, 71 }, toRanges.get(1));
904  0 assertArrayEquals(new int[] { 74, 75 }, toRanges.get(2));
905  0 assertArrayEquals(new int[] { 90, 90 }, toRanges.get(3));
906   
907    /*
908    * 1:1 over 1:1 backwards ('reverse strand')
909    */
910  0 ml1 = new MapList(new int[] { 1, 50 }, new int[] { 70, 119 }, 1, 1);
911  0 ml2 = new MapList(new int[] { 1, 500 },
912    new int[]
913    { 1000, 901, 600, 201 }, 1, 1);
914  0 compound = ml1.traverse(ml2);
915   
916  0 assertEquals(1, compound.getFromRatio());
917  0 assertEquals(1, compound.getToRatio());
918  0 fromRanges = compound.getFromRanges();
919  0 assertEquals(1, fromRanges.size());
920  0 assertArrayEquals(new int[] { 1, 50 }, fromRanges.get(0));
921  0 toRanges = compound.getToRanges();
922  0 assertEquals(2, toRanges.size());
923  0 assertArrayEquals(new int[] { 931, 901 }, toRanges.get(0));
924  0 assertArrayEquals(new int[] { 600, 582 }, toRanges.get(1));
925   
926    /*
927    * 1:1 plus 1:3 should result in 1:3
928    */
929  0 ml1 = new MapList(new int[] { 1, 30 }, new int[] { 11, 40 }, 1, 1);
930  0 ml2 = new MapList(new int[] { 1, 100 }, new int[] { 1, 50, 91, 340 }, 1,
931    3);
932  0 compound = ml1.traverse(ml2);
933   
934  0 assertEquals(1, compound.getFromRatio());
935  0 assertEquals(3, compound.getToRatio());
936  0 fromRanges = compound.getFromRanges();
937  0 assertEquals(1, fromRanges.size());
938  0 assertArrayEquals(new int[] { 1, 30 }, fromRanges.get(0));
939    // 11-40 maps to 31-50,91-160
940  0 toRanges = compound.getToRanges();
941  0 assertEquals(2, toRanges.size());
942  0 assertArrayEquals(new int[] { 31, 50 }, toRanges.get(0));
943  0 assertArrayEquals(new int[] { 91, 160 }, toRanges.get(1));
944   
945    /*
946    * 3:1 plus 1:1 should result in 3:1
947    */
948  0 ml1 = new MapList(new int[] { 1, 30 }, new int[] { 11, 20 }, 3, 1);
949  0 ml2 = new MapList(new int[] { 1, 100 }, new int[] { 1, 15, 91, 175 }, 1,
950    1);
951  0 compound = ml1.traverse(ml2);
952   
953  0 assertEquals(3, compound.getFromRatio());
954  0 assertEquals(1, compound.getToRatio());
955  0 fromRanges = compound.getFromRanges();
956  0 assertEquals(1, fromRanges.size());
957  0 assertArrayEquals(new int[] { 1, 30 }, fromRanges.get(0));
958    // 11-20 maps to 11-15, 91-95
959  0 toRanges = compound.getToRanges();
960  0 assertEquals(2, toRanges.size());
961  0 assertArrayEquals(new int[] { 11, 15 }, toRanges.get(0));
962  0 assertArrayEquals(new int[] { 91, 95 }, toRanges.get(1));
963   
964    /*
965    * 1:3 plus 3:1 should result in 1:1
966    */
967  0 ml1 = new MapList(new int[] { 21, 40 }, new int[] { 13, 72 }, 1, 3);
968  0 ml2 = new MapList(new int[] { 1, 300 }, new int[] { 51, 70, 121, 200 },
969    3, 1);
970  0 compound = ml1.traverse(ml2);
971   
972  0 assertEquals(1, compound.getFromRatio());
973  0 assertEquals(1, compound.getToRatio());
974  0 fromRanges = compound.getFromRanges();
975  0 assertEquals(1, fromRanges.size());
976  0 assertArrayEquals(new int[] { 21, 40 }, fromRanges.get(0));
977    // 13-72 maps 3:1 to 55-70, 121-124
978  0 toRanges = compound.getToRanges();
979  0 assertEquals(2, toRanges.size());
980  0 assertArrayEquals(new int[] { 55, 70 }, toRanges.get(0));
981  0 assertArrayEquals(new int[] { 121, 124 }, toRanges.get(1));
982   
983    /*
984    * 3:1 plus 1:3 should result in 1:1
985    */
986  0 ml1 = new MapList(new int[] { 31, 90 }, new int[] { 13, 32 }, 3, 1);
987  0 ml2 = new MapList(new int[] { 11, 40 }, new int[] { 41, 50, 71, 150 },
988    1, 3);
989  0 compound = ml1.traverse(ml2);
990   
991  0 assertEquals(1, compound.getFromRatio());
992  0 assertEquals(1, compound.getToRatio());
993  0 fromRanges = compound.getFromRanges();
994  0 assertEquals(1, fromRanges.size());
995  0 assertArrayEquals(new int[] { 31, 90 }, fromRanges.get(0));
996    // 13-32 maps to 47-50,71-126
997  0 toRanges = compound.getToRanges();
998  0 assertEquals(2, toRanges.size());
999  0 assertArrayEquals(new int[] { 47, 50 }, toRanges.get(0));
1000  0 assertArrayEquals(new int[] { 71, 126 }, toRanges.get(1));
1001   
1002    /*
1003    * if not all regions are mapped through, returns what is
1004    */
1005  0 ml1 = new MapList(new int[] { 1, 50 }, new int[] { 101, 150 }, 1, 1);
1006  0 ml2 = new MapList(new int[] { 131, 180 }, new int[] { 201, 250 }, 1, 1);
1007  0 compound = ml1.traverse(ml2);
1008  0 assertNull(compound);
1009    }
1010   
1011    /**
1012    * Test that method that inspects for the (first) forward or reverse 'to'
1013    * range. Single position ranges are ignored.
1014    */
 
1015  0 toggle @Test(groups = { "Functional" })
1016    public void testIsToForwardsStrand()
1017    {
1018    // [3-9] declares forward strand
1019  0 MapList ml = new MapList(new int[] { 20, 11 },
1020    new int[]
1021    { 2, 2, 3, 9, 12, 11 }, 1, 1);
1022  0 assertTrue(ml.isToForwardStrand());
1023   
1024    // [11-5] declares reverse strand ([13-14] is ignored)
1025  0 ml = new MapList(new int[] { 20, 11 },
1026    new int[]
1027    { 2, 2, 11, 5, 13, 14 }, 1, 1);
1028  0 assertFalse(ml.isToForwardStrand());
1029   
1030    // all single position ranges - defaults to forward strand
1031  0 ml = new MapList(new int[] { 3, 1 }, new int[] { 2, 2, 4, 4, 6, 6 }, 1,
1032    1);
1033  0 assertTrue(ml.isToForwardStrand());
1034    }
1035   
1036    /**
1037    * Test for mapping with overlapping ranges
1038    */
 
1039  0 toggle @Test(groups = { "Functional" })
1040    public void testLocateInFrom_withOverlap()
1041    {
1042    /*
1043    * gene to protein...
1044    */
1045  0 int[] codons = new int[] { 1, 12, 12, 17 };
1046  0 int[] protein = new int[] { 1, 6 };
1047  0 MapList ml = new MapList(codons, protein, 3, 1);
1048  0 assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(1, 1)));
1049  0 assertEquals("[4, 6]", Arrays.toString(ml.locateInFrom(2, 2)));
1050  0 assertEquals("[7, 9]", Arrays.toString(ml.locateInFrom(3, 3)));
1051  0 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
1052  0 assertEquals("[12, 14]", Arrays.toString(ml.locateInFrom(5, 5)));
1053  0 assertEquals("[15, 17]", Arrays.toString(ml.locateInFrom(6, 6)));
1054  0 assertEquals("[1, 6]", Arrays.toString(ml.locateInFrom(1, 2)));
1055  0 assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(1, 3)));
1056  0 assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 4)));
1057  0 assertEquals("[1, 12, 12, 14]", Arrays.toString(ml.locateInFrom(1, 5)));
1058  0 assertEquals("[1, 12, 12, 17]", Arrays.toString(ml.locateInFrom(1, 6)));
1059  0 assertEquals("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3)));
1060  0 assertEquals("[7, 12, 12, 17]", Arrays.toString(ml.locateInFrom(3, 6)));
1061   
1062    /*
1063    * partial overlap of range
1064    */
1065  0 assertEquals("[4, 12, 12, 17]", Arrays.toString(ml.locateInFrom(2, 7)));
1066  0 assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(-1, 1)));
1067   
1068    /*
1069    * no overlap in range
1070    */
1071  0 assertNull(ml.locateInFrom(0, 0));
1072   
1073    /*
1074    * gene to CDS...from EMBL:MN908947
1075    */
1076  0 int[] gene = new int[] { 266, 13468, 13468, 21555 };
1077  0 int[] cds = new int[] { 1, 21291 };
1078  0 ml = new MapList(gene, cds, 1, 1);
1079  0 assertEquals("[13468, 13468]",
1080    Arrays.toString(ml.locateInFrom(13203, 13203)));
1081  0 assertEquals("[13468, 13468]",
1082    Arrays.toString(ml.locateInFrom(13204, 13204)));
1083  0 assertEquals("[13468, 13468, 13468, 13468]",
1084    Arrays.toString(ml.locateInFrom(13203, 13204)));
1085    }
1086   
1087    /**
1088    * Test for mapping with overlapping ranges
1089    */
 
1090  0 toggle @Test(groups = { "Functional" })
1091    public void testLocateInTo_withOverlap()
1092    {
1093    /*
1094    * gene to protein...
1095    */
1096  0 int[] codons = new int[] { 1, 12, 12, 17 };
1097  0 int[] protein = new int[] { 1, 6 };
1098  0 MapList ml = new MapList(codons, protein, 3, 1);
1099  0 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
1100  0 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(3, 8)));
1101  0 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(2, 11)));
1102  0 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
1103   
1104    // we want base 12 to map to both of the amino acids it codes for
1105  0 assertEquals("[4, 5]", Arrays.toString(ml.locateInTo(12, 12)));
1106  0 assertEquals("[4, 5]", Arrays.toString(ml.locateInTo(11, 12)));
1107  0 assertEquals("[4, 6]", Arrays.toString(ml.locateInTo(11, 15)));
1108  0 assertEquals("[6, 6]", Arrays.toString(ml.locateInTo(15, 17)));
1109   
1110    /*
1111    * no overlap
1112    */
1113  0 assertNull(ml.locateInTo(0, 0));
1114   
1115    /*
1116    * partial overlap
1117    */
1118  0 assertEquals("[1, 6]", Arrays.toString(ml.locateInTo(1, 18)));
1119  0 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(-1, 1)));
1120   
1121    /*
1122    * gene to CDS...from EMBL:MN908947
1123    * the base at 13468 is used twice in transcription
1124    */
1125  0 int[] gene = new int[] { 266, 13468, 13468, 21555 };
1126  0 int[] cds = new int[] { 1, 21291 };
1127  0 ml = new MapList(gene, cds, 1, 1);
1128  0 assertEquals("[13203, 13204]",
1129    Arrays.toString(ml.locateInTo(13468, 13468)));
1130   
1131    /*
1132    * gene to protein
1133    * the base at 13468 is in the codon for 4401N and also 4402R
1134    */
1135  0 gene = new int[] { 266, 13468, 13468, 21552 }; // stop codon excluded
1136  0 protein = new int[] { 1, 7096 };
1137  0 ml = new MapList(gene, protein, 3, 1);
1138  0 assertEquals("[4401, 4402]",
1139    Arrays.toString(ml.locateInTo(13468, 13468)));
1140    }
1141   
 
1142  0 toggle @Test(groups = { "Functional" })
1143    public void testTraverseToPosition()
1144    {
1145  0 List<int[]> ranges = new ArrayList<>();
1146  0 assertNull(MapList.traverseToPosition(ranges, 0));
1147   
1148  0 ranges.add(new int[] { 3, 6 });
1149  0 assertNull(MapList.traverseToPosition(ranges, 0));
1150    }
1151   
 
1152  0 toggle @Test(groups = { "Functional" })
1153    public void testCountPositions()
1154    {
1155  0 try
1156    {
1157  0 MapList.countPositions(null, 1);
1158  0 fail("expected exception");
1159    } catch (NullPointerException e)
1160    {
1161    // expected
1162    }
1163   
1164  0 List<int[]> intervals = new ArrayList<>();
1165  0 assertNull(MapList.countPositions(intervals, 1));
1166   
1167    /*
1168    * forward strand
1169    */
1170  0 intervals.add(new int[] { 10, 20 });
1171  0 assertNull(MapList.countPositions(intervals, 9));
1172  0 assertNull(MapList.countPositions(intervals, 21));
1173  0 assertArrayEquals(new int[] { 1, 1 },
1174    MapList.countPositions(intervals, 10));
1175  0 assertArrayEquals(new int[] { 6, 1 },
1176    MapList.countPositions(intervals, 15));
1177  0 assertArrayEquals(new int[] { 11, 1 },
1178    MapList.countPositions(intervals, 20));
1179   
1180  0 intervals.add(new int[] { 25, 25 });
1181  0 assertArrayEquals(new int[] { 12, 1 },
1182    MapList.countPositions(intervals, 25));
1183   
1184    // next interval repeats position 25 - which should be counted twice if
1185    // traversed
1186  0 intervals.add(new int[] { 25, 26 });
1187  0 assertArrayEquals(new int[] { 12, 1 },
1188    MapList.countPositions(intervals, 25));
1189  0 assertArrayEquals(new int[] { 14, 1 },
1190    MapList.countPositions(intervals, 26));
1191   
1192    /*
1193    * reverse strand
1194    */
1195  0 intervals.clear();
1196  0 intervals.add(new int[] { 5, -5 });
1197  0 assertNull(MapList.countPositions(intervals, 6));
1198  0 assertNull(MapList.countPositions(intervals, -6));
1199  0 assertArrayEquals(new int[] { 1, -1 },
1200    MapList.countPositions(intervals, 5));
1201  0 assertArrayEquals(new int[] { 7, -1 },
1202    MapList.countPositions(intervals, -1));
1203  0 assertArrayEquals(new int[] { 11, -1 },
1204    MapList.countPositions(intervals, -5));
1205   
1206    /*
1207    * reverse then forward
1208    */
1209  0 intervals.add(new int[] { 5, 10 });
1210  0 assertArrayEquals(new int[] { 13, 1 },
1211    MapList.countPositions(intervals, 6));
1212   
1213    /*
1214    * reverse then forward then reverse
1215    */
1216  0 intervals.add(new int[] { -10, -20 });
1217  0 assertArrayEquals(new int[] { 20, -1 },
1218    MapList.countPositions(intervals, -12));
1219   
1220    /*
1221    * an interval [x, x] is treated as forward
1222    */
1223  0 intervals.add(new int[] { 30, 30 });
1224  0 assertArrayEquals(new int[] { 29, 1 },
1225    MapList.countPositions(intervals, 30));
1226   
1227    /*
1228    * it is the first matched occurrence that is returned
1229    */
1230  0 intervals.clear();
1231  0 intervals.add(new int[] { 1, 2 });
1232  0 intervals.add(new int[] { 2, 3 });
1233  0 assertArrayEquals(new int[] { 2, 1 },
1234    MapList.countPositions(intervals, 2));
1235  0 intervals.add(new int[] { -1, -2 });
1236  0 intervals.add(new int[] { -2, -3 });
1237  0 assertArrayEquals(new int[] { 6, -1 },
1238    MapList.countPositions(intervals, -2));
1239    }
1240   
1241    /**
1242    * Tests for helper method that adds any overlap (plus offset) to a set of
1243    * overlaps
1244    */
 
1245  0 toggle @Test(groups = { "Functional" })
1246    public void testAddOffsetPositions()
1247    {
1248  0 List<int[]> mapped = new ArrayList<>();
1249  0 int[] range = new int[] { 10, 20 };
1250  0 BitSet offsets = new BitSet();
1251   
1252  0 MapList.addOffsetPositions(mapped, 0, range, offsets);
1253  0 assertTrue(mapped.isEmpty()); // nothing marked for overlap
1254   
1255  0 offsets.set(11);
1256  0 MapList.addOffsetPositions(mapped, 0, range, offsets);
1257  0 assertTrue(mapped.isEmpty()); // no offset 11 in range
1258   
1259  0 offsets.set(4, 6); // this sets bits 4 and 5
1260  0 MapList.addOffsetPositions(mapped, 0, range, offsets);
1261  0 assertEquals(1, mapped.size());
1262  0 assertArrayEquals(new int[] { 14, 15 }, mapped.get(0));
1263   
1264  0 mapped.clear();
1265  0 offsets.set(10);
1266  0 MapList.addOffsetPositions(mapped, 0, range, offsets);
1267  0 assertEquals(2, mapped.size());
1268  0 assertArrayEquals(new int[] { 14, 15 }, mapped.get(0));
1269  0 assertArrayEquals(new int[] { 20, 20 }, mapped.get(1));
1270   
1271    /*
1272    * reverse range
1273    */
1274  0 range = new int[] { 20, 10 };
1275  0 mapped.clear();
1276  0 offsets.clear();
1277  0 MapList.addOffsetPositions(mapped, 0, range, offsets);
1278  0 assertTrue(mapped.isEmpty()); // nothing marked for overlap
1279  0 offsets.set(11);
1280  0 MapList.addOffsetPositions(mapped, 0, range, offsets);
1281  0 assertTrue(mapped.isEmpty()); // no offset 11 in range
1282  0 offsets.set(0);
1283  0 offsets.set(10);
1284  0 offsets.set(6, 8); // sets bits 6 and 7
1285  0 MapList.addOffsetPositions(mapped, 0, range, offsets);
1286  0 assertEquals(3, mapped.size());
1287  0 assertArrayEquals(new int[] { 20, 20 }, mapped.get(0));
1288  0 assertArrayEquals(new int[] { 14, 13 }, mapped.get(1));
1289  0 assertArrayEquals(new int[] { 10, 10 }, mapped.get(2));
1290    }
1291   
 
1292  0 toggle @Test(groups = { "Functional" })
1293    public void testGetPositionsForOffsets()
1294    {
1295  0 List<int[]> ranges = new ArrayList<>();
1296  0 BitSet offsets = new BitSet();
1297  0 List<int[]> mapped = MapList.getPositionsForOffsets(ranges, offsets);
1298  0 assertTrue(mapped.isEmpty()); // no ranges and no offsets!
1299   
1300  0 offsets.set(5, 1000);
1301  0 mapped = MapList.getPositionsForOffsets(ranges, offsets);
1302  0 assertTrue(mapped.isEmpty()); // no ranges
1303   
1304    /*
1305    * one range with overlap of offsets
1306    */
1307  0 ranges.add(new int[] { 15, 25 });
1308  0 mapped = MapList.getPositionsForOffsets(ranges, offsets);
1309  0 assertEquals(1, mapped.size());
1310  0 assertArrayEquals(new int[] { 20, 25 }, mapped.get(0));
1311   
1312    /*
1313    * two ranges
1314    */
1315  0 ranges.add(new int[] { 300, 320 });
1316  0 mapped = MapList.getPositionsForOffsets(ranges, offsets);
1317  0 assertEquals(2, mapped.size());
1318  0 assertArrayEquals(new int[] { 20, 25 }, mapped.get(0));
1319  0 assertArrayEquals(new int[] { 300, 320 }, mapped.get(1));
1320   
1321    /*
1322    * boundary case - right end of first range overlaps
1323    */
1324  0 offsets.clear();
1325  0 offsets.set(10);
1326  0 mapped = MapList.getPositionsForOffsets(ranges, offsets);
1327  0 assertEquals(1, mapped.size());
1328  0 assertArrayEquals(new int[] { 25, 25 }, mapped.get(0));
1329   
1330    /*
1331    * boundary case - left end of second range overlaps
1332    */
1333  0 offsets.set(11);
1334  0 mapped = MapList.getPositionsForOffsets(ranges, offsets);
1335  0 assertEquals(2, mapped.size());
1336  0 assertArrayEquals(new int[] { 25, 25 }, mapped.get(0));
1337  0 assertArrayEquals(new int[] { 300, 300 }, mapped.get(1));
1338   
1339    /*
1340    * offsets into a circular range are reported in
1341    * the order in which they are traversed
1342    */
1343  0 ranges.clear();
1344  0 ranges.add(new int[] { 100, 150 });
1345  0 ranges.add(new int[] { 60, 80 });
1346  0 offsets.clear();
1347  0 offsets.set(45, 55); // sets bits 45 to 54
1348  0 mapped = MapList.getPositionsForOffsets(ranges, offsets);
1349  0 assertEquals(2, mapped.size());
1350  0 assertArrayEquals(new int[] { 145, 150 }, mapped.get(0)); // offsets 45-50
1351  0 assertArrayEquals(new int[] { 60, 63 }, mapped.get(1)); // offsets 51-54
1352   
1353    /*
1354    * reverse range overlap is reported with start < end
1355    */
1356  0 ranges.clear();
1357  0 ranges.add(new int[] { 4321, 4000 });
1358  0 offsets.clear();
1359  0 offsets.set(20, 22); // sets bits 20 and 21
1360  0 offsets.set(30);
1361  0 mapped = MapList.getPositionsForOffsets(ranges, offsets);
1362  0 assertEquals(2, mapped.size());
1363  0 assertArrayEquals(new int[] { 4301, 4300 }, mapped.get(0));
1364  0 assertArrayEquals(new int[] { 4291, 4291 }, mapped.get(1));
1365    }
1366   
 
1367  0 toggle @Test(groups = { "Functional" })
1368    public void testGetMappedOffsetsForPositions()
1369    {
1370    /*
1371    * start by verifying the examples in the method's Javadoc!
1372    */
1373  0 List<int[]> ranges = new ArrayList<>();
1374  0 ranges.add(new int[] { 10, 20 });
1375  0 ranges.add(new int[] { 31, 40 });
1376  0 BitSet overlaps = MapList.getMappedOffsetsForPositions(1, 9, ranges, 1,
1377    1);
1378  0 assertTrue(overlaps.isEmpty());
1379  0 overlaps = MapList.getMappedOffsetsForPositions(1, 11, ranges, 1, 1);
1380  0 assertEquals(2, overlaps.cardinality());
1381  0 assertTrue(overlaps.get(0));
1382  0 assertTrue(overlaps.get(1));
1383  0 overlaps = MapList.getMappedOffsetsForPositions(15, 35, ranges, 1, 1);
1384  0 assertEquals(11, overlaps.cardinality());
1385  0 for (int i = 5; i <= 11; i++)
1386    {
1387  0 assertTrue(overlaps.get(i));
1388    }
1389   
1390  0 ranges.clear();
1391  0 ranges.add(new int[] { 1, 200 });
1392  0 overlaps = MapList.getMappedOffsetsForPositions(9, 9, ranges, 1, 3);
1393  0 assertEquals(3, overlaps.cardinality());
1394  0 assertTrue(overlaps.get(24));
1395  0 assertTrue(overlaps.get(25));
1396  0 assertTrue(overlaps.get(26));
1397   
1398  0 ranges.clear();
1399  0 ranges.add(new int[] { 101, 150 });
1400  0 ranges.add(new int[] { 171, 180 });
1401  0 overlaps = MapList.getMappedOffsetsForPositions(101, 102, ranges, 3, 1);
1402  0 assertEquals(1, overlaps.cardinality());
1403  0 assertTrue(overlaps.get(0));
1404  0 overlaps = MapList.getMappedOffsetsForPositions(150, 171, ranges, 3, 1);
1405  0 assertEquals(1, overlaps.cardinality());
1406  0 assertTrue(overlaps.get(16));
1407   
1408  0 ranges.clear();
1409  0 ranges.add(new int[] { 101, 150 });
1410  0 ranges.add(new int[] { 21, 30 });
1411  0 overlaps = MapList.getMappedOffsetsForPositions(24, 40, ranges, 3, 1);
1412  0 assertEquals(3, overlaps.cardinality());
1413  0 assertTrue(overlaps.get(17));
1414  0 assertTrue(overlaps.get(18));
1415  0 assertTrue(overlaps.get(19));
1416   
1417    /*
1418    * reverse range 1:1 (e.g. reverse strand gene to transcript)
1419    */
1420  0 ranges.clear();
1421  0 ranges.add(new int[] { 20, 10 });
1422  0 overlaps = MapList.getMappedOffsetsForPositions(12, 13, ranges, 1, 1);
1423  0 assertEquals(2, overlaps.cardinality());
1424  0 assertTrue(overlaps.get(7));
1425  0 assertTrue(overlaps.get(8));
1426   
1427    /*
1428    * reverse range 3:1 (e.g. reverse strand gene to peptide)
1429    * from EMBL:J03321 to P0CE20
1430    */
1431  0 ranges.clear();
1432  0 ranges.add(new int[] { 1480, 488 });
1433  0 overlaps = MapList.getMappedOffsetsForPositions(1460, 1460, ranges, 3,
1434    1);
1435    // 1460 is the end of the 7th codon
1436  0 assertEquals(1, overlaps.cardinality());
1437  0 assertTrue(overlaps.get(6));
1438    // add one base (part codon)
1439  0 overlaps = MapList.getMappedOffsetsForPositions(1459, 1460, ranges, 3,
1440    1);
1441  0 assertEquals(2, overlaps.cardinality());
1442  0 assertTrue(overlaps.get(6));
1443  0 assertTrue(overlaps.get(7));
1444    // add second base (part codon)
1445  0 overlaps = MapList.getMappedOffsetsForPositions(1458, 1460, ranges, 3,
1446    1);
1447  0 assertEquals(2, overlaps.cardinality());
1448  0 assertTrue(overlaps.get(6));
1449  0 assertTrue(overlaps.get(7));
1450    // add third base (whole codon)
1451  0 overlaps = MapList.getMappedOffsetsForPositions(1457, 1460, ranges, 3,
1452    1);
1453  0 assertEquals(2, overlaps.cardinality());
1454  0 assertTrue(overlaps.get(6));
1455  0 assertTrue(overlaps.get(7));
1456    // add one more base (part codon)
1457  0 overlaps = MapList.getMappedOffsetsForPositions(1456, 1460, ranges, 3,
1458    1);
1459  0 assertEquals(3, overlaps.cardinality());
1460  0 assertTrue(overlaps.get(6));
1461  0 assertTrue(overlaps.get(7));
1462  0 assertTrue(overlaps.get(8));
1463    }
1464    }