From 4d5c986641c7281b09e3e70c46fcf91d64f9afa7 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Fri, 20 May 2016 18:30:55 +0100 Subject: [PATCH] JAL-2114 accept "<123..178" or "123..>178" format --- src/jalview/util/DnaUtils.java | 21 +++++++++++++++++++-- test/jalview/util/DnaUtilsTest.java | 14 ++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/jalview/util/DnaUtils.java b/src/jalview/util/DnaUtils.java index 9ab4fda..f6514e5 100644 --- a/src/jalview/util/DnaUtils.java +++ b/src/jalview/util/DnaUtils.java @@ -35,14 +35,15 @@ public class DnaUtils /* * try to parse m..n (or simply m) + * also handles n (discarding < or >) */ String[] range = location.split("\\.\\."); if (range.length == 1 || range.length == 2) { try { - int start = Integer.valueOf(range[0]); - int end = range.length == 1 ? start : Integer.valueOf(range[1]); + int start = parseRangeEnd(range[0]); + int end = range.length == 1 ? start : parseRangeEnd(range[1]); return Collections.singletonList(new int[] { start, end }); } catch (NumberFormatException e) { @@ -64,6 +65,22 @@ public class DnaUtils } /** + * Returns the integer value of a locus, discarding any < or > prefix + * + * @throws NumberFormatException + * if value is not numeric + */ + static int parseRangeEnd(String loc) + { + + if (loc.startsWith("<") || loc.startsWith(">")) + { + loc = loc.substring(1); + } + return Integer.valueOf(loc); + } + + /** * Parses a complement(locationSpec) into a list of start-end ranges * * @param location diff --git a/test/jalview/util/DnaUtilsTest.java b/test/jalview/util/DnaUtilsTest.java index 9e978fe..bb3cc5b 100644 --- a/test/jalview/util/DnaUtilsTest.java +++ b/test/jalview/util/DnaUtilsTest.java @@ -89,10 +89,20 @@ public class DnaUtilsTest assertEquals(87064, ranges.get(1)[1]); /* + * beyond 5' or 3' locus + */ + ranges = DnaUtils.parseLocation("<34..126"); + assertEquals(1, ranges.size()); + assertEquals(34, ranges.get(0)[0]); + assertEquals(126, ranges.get(0)[1]); + ranges = DnaUtils.parseLocation("35..>127"); + assertEquals(1, ranges.size()); + assertEquals(35, ranges.get(0)[0]); + assertEquals(127, ranges.get(0)[1]); + + /* * valid things we don't yet handle */ - assertNull(DnaUtils.parseLocation("<34..126")); - assertNull(DnaUtils.parseLocation("34..>126")); assertNull(DnaUtils.parseLocation("34.126")); assertNull(DnaUtils.parseLocation("34^126")); assertNull(DnaUtils.parseLocation("order(34..126,130..180)")); -- 1.7.10.2