From: gmungoc Date: Mon, 4 Jun 2018 19:40:07 +0000 (+0100) Subject: JAL-3019 correctly form toRange of 'traversed' MapList X-Git-Tag: Release_2_11_0~32 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=10dd3a771da0085ead2318b7385211c7b675d7f4;p=jalview.git JAL-3019 correctly form toRange of 'traversed' MapList --- diff --git a/src/jalview/util/MapList.java b/src/jalview/util/MapList.java index 7f1abc4..9f28ee1 100644 --- a/src/jalview/util/MapList.java +++ b/src/jalview/util/MapList.java @@ -327,6 +327,13 @@ public class MapList 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])); } @@ -335,6 +342,13 @@ public class MapList 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])); } @@ -1203,11 +1217,20 @@ public class MapList 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); diff --git a/test/jalview/analysis/AlignmentUtilsTests.java b/test/jalview/analysis/AlignmentUtilsTests.java index 92bf0ce..a7a7d34 100644 --- a/test/jalview/analysis/AlignmentUtilsTests.java +++ b/test/jalview/analysis/AlignmentUtilsTests.java @@ -2685,14 +2685,14 @@ public class AlignmentUtilsTests assertEquals(2, toMap.getFromRanges().get(0).length); assertEquals(1, toMap.getFromRanges().get(0)[0]); assertEquals(12, toMap.getFromRanges().get(0)[1]); - assertEquals(1, toMap.getToRanges().size()); - assertEquals(4, toMap.getToRanges().get(0).length); + assertEquals(2, toMap.getToRanges().size()); + assertEquals(2, toMap.getToRanges().get(0).length); assertEquals(158, toMap.getToRanges().get(0)[0]); assertEquals(164, toMap.getToRanges().get(0)[1]); - assertEquals(210, toMap.getToRanges().get(0)[2]); - assertEquals(214, toMap.getToRanges().get(0)[3]); + assertEquals(210, toMap.getToRanges().get(1)[0]); + assertEquals(214, toMap.getToRanges().get(1)[1]); // or summarised as (but toString might change in future): - assertEquals("[ [1, 12] ] 1:1 to [ [158, 164, 210, 214] ]", + assertEquals("[ [1, 12] ] 1:1 to [ [158, 164] [210, 214] ]", toMap.toString()); /* @@ -2704,7 +2704,7 @@ public class AlignmentUtilsTests assertEquals("GRCh38", toLoci.getAssemblyId()); assertEquals("7", toLoci.getChromosomeId()); toMap = toLoci.getMap(); - assertEquals("[ [1, 12] ] 1:1 to [ [158, 164, 210, 214] ]", + assertEquals("[ [1, 12] ] 1:1 to [ [158, 164] [210, 214] ]", toMap.toString()); } diff --git a/test/jalview/util/MapListTest.java b/test/jalview/util/MapListTest.java index 1acc9e1..86dcc39 100644 --- a/test/jalview/util/MapListTest.java +++ b/test/jalview/util/MapListTest.java @@ -828,22 +828,31 @@ public class MapListTest */ 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 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 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') @@ -853,14 +862,15 @@ public class MapListTest 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 @@ -870,15 +880,16 @@ public class MapListTest 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 @@ -888,15 +899,16 @@ public class MapListTest 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 @@ -906,15 +918,16 @@ public class MapListTest 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 @@ -924,15 +937,16 @@ public class MapListTest 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