fromHighest = Integer.MIN_VALUE;
for (int[] range : fromRange)
{
+ if (range.length != 2)
+ {
+ // throw new IllegalArgumentException(range);
+ System.err.println(
+ "Invalid format for fromRange " + Arrays.toString(range)
+ + " may cause errors");
+ }
fromLowest = Math.min(fromLowest, Math.min(range[0], range[1]));
fromHighest = Math.max(fromHighest, Math.max(range[0], range[1]));
}
toHighest = Integer.MIN_VALUE;
for (int[] range : toRange)
{
+ if (range.length != 2)
+ {
+ // throw new IllegalArgumentException(range);
+ System.err.println("Invalid format for toRange "
+ + Arrays.toString(range)
+ + " may cause errors");
+ }
toLowest = Math.min(toLowest, Math.min(range[0], range[1]));
toHighest = Math.max(toHighest, Math.max(range[0], range[1]));
}
for (int[] range : map.getFromRanges())
{
- addRange(range, fromShifts);
+ MappingUtils.addRange(range, fromShifts);
}
for (int[] range : map.getToRanges())
{
- addRange(range, toShifts);
+ MappingUtils.addRange(range, toShifts);
}
}
/**
- * Adds the given range to a list of ranges. If the new range just extends
- * existing ranges, the current endpoint is updated instead.
- *
- * @param range
- * @param addTo
- */
- static void addRange(int[] range, List<int[]> addTo)
- {
- /*
- * list is empty - add to it!
- */
- if (addTo.size() == 0)
- {
- addTo.add(range);
- return;
- }
-
- int[] last = addTo.get(addTo.size() - 1);
- boolean lastForward = last[1] >= last[0];
- boolean newForward = range[1] >= range[0];
-
- /*
- * contiguous range in the same direction - just update endpoint
- */
- if (lastForward == newForward && last[1] == range[0])
- {
- last[1] = range[1];
- return;
- }
-
- /*
- * next range starts at +1 in forward sense - update endpoint
- */
- if (lastForward && newForward && range[0] == last[1] + 1)
- {
- last[1] = range[1];
- return;
- }
-
- /*
- * next range starts at -1 in reverse sense - update endpoint
- */
- if (!lastForward && !newForward && range[0] == last[1] - 1)
- {
- last[1] = range[1];
- return;
- }
-
- /*
- * just add the new range
- */
- addTo.add(range);
- }
-
- /**
* Returns true if mapping is from forward strand, false if from reverse
* strand. Result is just based on the first 'from' range that is not a single
* position. Default is true unless proven to be false. Behaviour is not well
for (int[] range : getToRanges())
{
int[] transferred = map.locateInTo(range[0], range[1]);
- if (transferred == null)
+ if (transferred == null || transferred.length % 2 != 0)
{
return null;
}
- toRanges.add(transferred);
+
+ /*
+ * convert [start1, end1, start2, end2, ...]
+ * to [[start1, end1], [start2, end2], ...]
+ */
+ for (int i = 0; i < transferred.length;)
+ {
+ toRanges.add(new int[] { transferred[i], transferred[i + 1] });
+ i += 2;
+ }
}
return new MapList(getFromRanges(), toRanges, outFromRatio, outToRatio);
assertEquals("[ [11, 16] ] 1:3 to [ [72, 53] ]", ml.toString());
}
- @Test(groups = "Functional")
- public void testAddRange()
- {
- int[] range = { 1, 5 };
- List<int[]> ranges = new ArrayList<>();
-
- // add to empty list:
- MapList.addRange(range, ranges);
- assertEquals(1, ranges.size());
- assertSame(range, ranges.get(0));
-
- // extend contiguous (same position):
- MapList.addRange(new int[] { 5, 10 }, ranges);
- assertEquals(1, ranges.size());
- assertEquals(1, ranges.get(0)[0]);
- assertEquals(10, ranges.get(0)[1]);
-
- // extend contiguous (next position):
- MapList.addRange(new int[] { 11, 15 }, ranges);
- assertEquals(1, ranges.size());
- assertEquals(1, ranges.get(0)[0]);
- assertEquals(15, ranges.get(0)[1]);
-
- // change direction: range is not merged:
- MapList.addRange(new int[] { 16, 10 }, ranges);
- assertEquals(2, ranges.size());
- assertEquals(16, ranges.get(1)[0]);
- assertEquals(10, ranges.get(1)[1]);
-
- // extend reverse contiguous (same position):
- MapList.addRange(new int[] { 10, 8 }, ranges);
- assertEquals(2, ranges.size());
- assertEquals(16, ranges.get(1)[0]);
- assertEquals(8, ranges.get(1)[1]);
-
- // extend reverse contiguous (next position):
- MapList.addRange(new int[] { 7, 6 }, ranges);
- assertEquals(2, ranges.size());
- assertEquals(16, ranges.get(1)[0]);
- assertEquals(6, ranges.get(1)[1]);
-
- // change direction: range is not merged:
- MapList.addRange(new int[] { 6, 9 }, ranges);
- assertEquals(3, ranges.size());
- assertEquals(6, ranges.get(2)[0]);
- assertEquals(9, ranges.get(2)[1]);
-
- // not contiguous: not merged
- MapList.addRange(new int[] { 11, 12 }, ranges);
- assertEquals(4, ranges.size());
- assertEquals(11, ranges.get(3)[0]);
- assertEquals(12, ranges.get(3)[1]);
- }
-
/**
* Check state after construction
*/
*/
MapList ml1 = new MapList(new int[] { 3, 4, 8, 12 }, new int[] { 5, 8,
11, 13 }, 1, 1);
+ assertEquals("{[3, 4], [8, 12]}", prettyPrint(ml1.getFromRanges()));
+ assertEquals("{[5, 8], [11, 13]}", prettyPrint(ml1.getToRanges()));
+
MapList ml2 = new MapList(new int[] { 1, 50 }, new int[] { 40, 45, 70,
75, 90, 127 }, 1, 1);
+ assertEquals("{[1, 50]}", prettyPrint(ml2.getFromRanges()));
+ assertEquals("{[40, 45], [70, 75], [90, 127]}",
+ prettyPrint(ml2.getToRanges()));
+
MapList compound = ml1.traverse(ml2);
- assertEquals(compound.getFromRatio(), 1);
- assertEquals(compound.getToRatio(), 1);
+ assertEquals(1, compound.getFromRatio());
+ assertEquals(1, compound.getToRatio());
List<int[]> fromRanges = compound.getFromRanges();
- assertEquals(fromRanges.size(), 2);
+ assertEquals(2, fromRanges.size());
assertArrayEquals(new int[] { 3, 4 }, fromRanges.get(0));
assertArrayEquals(new int[] { 8, 12 }, fromRanges.get(1));
List<int[]> toRanges = compound.getToRanges();
- assertEquals(toRanges.size(), 2);
+ assertEquals(4, toRanges.size());
// 5-8 maps to 44-45,70-71
// 11-13 maps to 74-75,90
- assertArrayEquals(new int[] { 44, 45, 70, 71 }, toRanges.get(0));
- assertArrayEquals(new int[] { 74, 75, 90, 90 }, toRanges.get(1));
+ assertArrayEquals(new int[] { 44, 45 }, toRanges.get(0));
+ assertArrayEquals(new int[] { 70, 71 }, toRanges.get(1));
+ assertArrayEquals(new int[] { 74, 75 }, toRanges.get(2));
+ assertArrayEquals(new int[] { 90, 90 }, toRanges.get(3));
/*
* 1:1 over 1:1 backwards ('reverse strand')
new int[] { 1000, 901, 600, 201 }, 1, 1);
compound = ml1.traverse(ml2);
- assertEquals(compound.getFromRatio(), 1);
- assertEquals(compound.getToRatio(), 1);
+ assertEquals(1, compound.getFromRatio());
+ assertEquals(1, compound.getToRatio());
fromRanges = compound.getFromRanges();
- assertEquals(fromRanges.size(), 1);
+ assertEquals(1, fromRanges.size());
assertArrayEquals(new int[] { 1, 50 }, fromRanges.get(0));
toRanges = compound.getToRanges();
- assertEquals(toRanges.size(), 1);
- assertArrayEquals(new int[] { 931, 901, 600, 582 }, toRanges.get(0));
+ assertEquals(2, toRanges.size());
+ assertArrayEquals(new int[] { 931, 901 }, toRanges.get(0));
+ assertArrayEquals(new int[] { 600, 582 }, toRanges.get(1));
/*
* 1:1 plus 1:3 should result in 1:3
1, 3);
compound = ml1.traverse(ml2);
- assertEquals(compound.getFromRatio(), 1);
- assertEquals(compound.getToRatio(), 3);
+ assertEquals(1, compound.getFromRatio());
+ assertEquals(3, compound.getToRatio());
fromRanges = compound.getFromRanges();
- assertEquals(fromRanges.size(), 1);
+ assertEquals(1, fromRanges.size());
assertArrayEquals(new int[] { 1, 30 }, fromRanges.get(0));
// 11-40 maps to 31-50,91-160
toRanges = compound.getToRanges();
- assertEquals(toRanges.size(), 1);
- assertArrayEquals(new int[] { 31, 50, 91, 160 }, toRanges.get(0));
+ assertEquals(2, toRanges.size());
+ assertArrayEquals(new int[] { 31, 50 }, toRanges.get(0));
+ assertArrayEquals(new int[] { 91, 160 }, toRanges.get(1));
/*
* 3:1 plus 1:1 should result in 3:1
1, 1);
compound = ml1.traverse(ml2);
- assertEquals(compound.getFromRatio(), 3);
- assertEquals(compound.getToRatio(), 1);
+ assertEquals(3, compound.getFromRatio());
+ assertEquals(1, compound.getToRatio());
fromRanges = compound.getFromRanges();
- assertEquals(fromRanges.size(), 1);
+ assertEquals(1, fromRanges.size());
assertArrayEquals(new int[] { 1, 30 }, fromRanges.get(0));
// 11-20 maps to 11-15, 91-95
toRanges = compound.getToRanges();
- assertEquals(toRanges.size(), 1);
- assertArrayEquals(new int[] { 11, 15, 91, 95 }, toRanges.get(0));
+ assertEquals(2, toRanges.size());
+ assertArrayEquals(new int[] { 11, 15 }, toRanges.get(0));
+ assertArrayEquals(new int[] { 91, 95 }, toRanges.get(1));
/*
* 1:3 plus 3:1 should result in 1:1
3, 1);
compound = ml1.traverse(ml2);
- assertEquals(compound.getFromRatio(), 1);
- assertEquals(compound.getToRatio(), 1);
+ assertEquals(1, compound.getFromRatio());
+ assertEquals(1, compound.getToRatio());
fromRanges = compound.getFromRanges();
- assertEquals(fromRanges.size(), 1);
+ assertEquals(1, fromRanges.size());
assertArrayEquals(new int[] { 21, 40 }, fromRanges.get(0));
// 13-72 maps 3:1 to 55-70, 121-124
toRanges = compound.getToRanges();
- assertEquals(toRanges.size(), 1);
- assertArrayEquals(new int[] { 55, 70, 121, 124 }, toRanges.get(0));
+ assertEquals(2, toRanges.size());
+ assertArrayEquals(new int[] { 55, 70 }, toRanges.get(0));
+ assertArrayEquals(new int[] { 121, 124 }, toRanges.get(1));
/*
* 3:1 plus 1:3 should result in 1:1
1, 3);
compound = ml1.traverse(ml2);
- assertEquals(compound.getFromRatio(), 1);
- assertEquals(compound.getToRatio(), 1);
+ assertEquals(1, compound.getFromRatio());
+ assertEquals(1, compound.getToRatio());
fromRanges = compound.getFromRanges();
- assertEquals(fromRanges.size(), 1);
+ assertEquals(1, fromRanges.size());
assertArrayEquals(new int[] { 31, 90 }, fromRanges.get(0));
// 13-32 maps to 47-50,71-126
toRanges = compound.getToRanges();
- assertEquals(toRanges.size(), 1);
- assertArrayEquals(new int[] { 47, 50, 71, 126 }, toRanges.get(0));
+ assertEquals(2, toRanges.size());
+ assertArrayEquals(new int[] { 47, 50 }, toRanges.get(0));
+ assertArrayEquals(new int[] { 71, 126 }, toRanges.get(1));
/*
* method returns null if not all regions are mapped through