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
}
}
}
+
+ /**
+ * 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
+ */
+ public 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);
+ }
}
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
*/
assertEquals(1, ranges.size());
assertEquals(9, ranges.get(0)[1]);
}
+
+ @Test(groups = "Functional")
+ public void testAddRange()
+ {
+ int[] range = { 1, 5 };
+ List<int[]> ranges = new ArrayList<>();
+
+ // add to empty list:
+ MappingUtils.addRange(range, ranges);
+ assertEquals(1, ranges.size());
+ assertSame(range, ranges.get(0));
+
+ // extend contiguous (same position):
+ MappingUtils.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):
+ MappingUtils.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:
+ MappingUtils.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):
+ MappingUtils.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):
+ MappingUtils.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:
+ MappingUtils.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
+ MappingUtils.addRange(new int[] { 11, 12 }, ranges);
+ assertEquals(4, ranges.size());
+ assertEquals(11, ranges.get(3)[0]);
+ assertEquals(12, ranges.get(3)[1]);
+ }
}