From: gmungoc Date: Thu, 19 May 2016 16:14:09 +0000 (+0100) Subject: JAL-2114 added single-locus case and more error tests X-Git-Tag: Release_2_10_0~218^2~5 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=67538ac6c16cf7a413023a1faf0593a20b1634e3;p=jalview.git JAL-2114 added single-locus case and more error tests --- diff --git a/src/jalview/util/DnaUtils.java b/src/jalview/util/DnaUtils.java index 639eb8e..9ab4fda 100644 --- a/src/jalview/util/DnaUtils.java +++ b/src/jalview/util/DnaUtils.java @@ -32,13 +32,17 @@ public class DnaUtils System.err.println(errorMessage); return null; } + + /* + * try to parse m..n (or simply m) + */ String[] range = location.split("\\.\\."); - if (range.length == 2) + if (range.length == 1 || range.length == 2) { try { int start = Integer.valueOf(range[0]); - int end = Integer.valueOf(range[1]); + int end = range.length == 1 ? start : Integer.valueOf(range[1]); return Collections.singletonList(new int[] { start, end }); } catch (NumberFormatException e) { @@ -70,6 +74,10 @@ public class DnaUtils /* * take what is inside complement() */ + if (!location.endsWith(")")) + { + return null; + } String toComplement = location.substring("complement(".length(), location.length() - 1); List ranges = parseLocation(toComplement); @@ -107,6 +115,10 @@ public class DnaUtils /* * take what is inside join() */ + if (!location.endsWith(")")) + { + return null; + } String joinedLocs = location.substring("join(".length(), location.length() - 1); String[] locations = joinedLocs.split(","); diff --git a/test/jalview/util/DnaUtilsTest.java b/test/jalview/util/DnaUtilsTest.java index af76885..9e978fe 100644 --- a/test/jalview/util/DnaUtilsTest.java +++ b/test/jalview/util/DnaUtilsTest.java @@ -19,9 +19,17 @@ public class DnaUtilsTest public void testParseLocation() { /* + * single locus + */ + List ranges = DnaUtils.parseLocation("467"); + assertEquals(1, ranges.size()); + assertEquals(467, ranges.get(0)[0]); + assertEquals(467, ranges.get(0)[1]); + + /* * simple range */ - List ranges = DnaUtils.parseLocation("12..78"); + ranges = DnaUtils.parseLocation("12..78"); assertEquals(1, ranges.size()); assertEquals(12, ranges.get(0)[0]); assertEquals(78, ranges.get(0)[1]); @@ -87,6 +95,7 @@ public class DnaUtilsTest assertNull(DnaUtils.parseLocation("34..>126")); assertNull(DnaUtils.parseLocation("34.126")); assertNull(DnaUtils.parseLocation("34^126")); + assertNull(DnaUtils.parseLocation("order(34..126,130..180)")); /* * invalid things @@ -94,6 +103,9 @@ public class DnaUtilsTest assertNull(DnaUtils.parseLocation("")); assertNull(DnaUtils.parseLocation("JOIN(1..2)")); assertNull(DnaUtils.parseLocation("join(1..2")); + assertNull(DnaUtils.parseLocation("join(1..2(")); + assertNull(DnaUtils.parseLocation("complement(1..2")); + assertNull(DnaUtils.parseLocation("complement(1..2(")); try { assertNull(DnaUtils.parseLocation(null));