X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2FDnaUtils.java;h=9582e2ea1938c7bfcac3d25844573a8a986c542d;hb=f80f1dd5762b7c035ca2502f35f059e9f5fe5a49;hp=f6514e513f707f94a7fa5313bdb01cd2c168121f;hpb=4d5c986641c7281b09e3e70c46fcf91d64f9afa7;p=jalview.git diff --git a/src/jalview/util/DnaUtils.java b/src/jalview/util/DnaUtils.java index f6514e5..9582e2e 100644 --- a/src/jalview/util/DnaUtils.java +++ b/src/jalview/util/DnaUtils.java @@ -1,5 +1,6 @@ package jalview.util; +import java.text.ParseException; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -9,13 +10,22 @@ public class DnaUtils /** * Parses an ENA/GenBank format location specifier and returns a list of - * [start, end] ranges. Returns null if not able to parse. + * [start, end] ranges. Throws an exception if not able to parse. + *

+ * Currently we do not parse "order()" specifiers, or indeterminate ranges of + * the format "<start..end" or "start..>end" or "start.end" or + * "start^end" * * @param location * @return + * @throws ParseException + * if unable to parse the location (the exception message is the + * location specifier being parsed); we use ParseException in + * preference to the unchecked IllegalArgumentException * @see http://www.insdc.org/files/feature_table.html#3.4 */ public static List parseLocation(String location) + throws ParseException { if (location.startsWith("join(")) { @@ -25,33 +35,28 @@ public class DnaUtils { return parseComplement(location); } - String errorMessage = "Unable to process location specifier: " - + location; if (location.startsWith("order(")) { - System.err.println(errorMessage); - return null; + throw new ParseException(location, 0); } /* * 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 = parseRangeEnd(range[0]); - int end = range.length == 1 ? start : parseRangeEnd(range[1]); + int start = Integer.valueOf(range[0]); + int end = range.length == 1 ? start : Integer.valueOf(range[1]); return Collections.singletonList(new int[] { start, end }); } catch (NumberFormatException e) { /* * could be a location like <1..888 or 1..>888 */ - System.err.println(errorMessage); - return null; + throw new ParseException(location, 0); } } else @@ -59,52 +64,29 @@ public class DnaUtils /* * could be a location like 102.110 or 123^124 */ - System.err.println(errorMessage); - return null; + throw new ParseException(location, 0); } } /** - * 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 * @return + * @throws ParseException */ - static List parseComplement(String location) + static List parseComplement(String location) throws ParseException { /* * take what is inside complement() */ if (!location.endsWith(")")) { - return null; + throw new ParseException(location, 0); } String toComplement = location.substring("complement(".length(), location.length() - 1); List ranges = parseLocation(toComplement); - if (ranges == null) - { - /* - * something bad in there - */ - return null; - } /* * reverse the order and direction of ranges @@ -124,8 +106,9 @@ public class DnaUtils * * @param location * @return + * @throws ParseException */ - static List parseJoin(String location) + static List parseJoin(String location) throws ParseException { List ranges = new ArrayList(); @@ -134,7 +117,7 @@ public class DnaUtils */ if (!location.endsWith(")")) { - return null; + throw new ParseException(location, 0); } String joinedLocs = location.substring("join(".length(), location.length() - 1); @@ -142,17 +125,7 @@ public class DnaUtils for (String loc : locations) { List range = parseLocation(loc); - if (range == null) - { - /* - * something bad in there - */ - return null; - } - else - { - ranges.addAll(range); - } + ranges.addAll(range); } return ranges; }