X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2FDnaUtils.java;h=284ec107f818b5b039ef993f615041b2a95f35e1;hb=37de9310bec3501cbc6381e0c3dcb282fcaad812;hp=639eb8e69a837672f95d82fa1331b614a8d5bba4;hpb=a4507b66add69be7e5097dcc8fbd9bd08b0626cb;p=jalview.git diff --git a/src/jalview/util/DnaUtils.java b/src/jalview/util/DnaUtils.java index 639eb8e..284ec10 100644 --- a/src/jalview/util/DnaUtils.java +++ b/src/jalview/util/DnaUtils.java @@ -1,5 +1,26 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ package jalview.util; +import java.text.ParseException; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -9,13 +30,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,28 +55,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) + */ 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) { /* * could be a location like <1..888 or 1..>888 */ - System.err.println(errorMessage); - return null; + throw new ParseException(location, 0); } } else @@ -54,8 +84,7 @@ public class DnaUtils /* * could be a location like 102.110 or 123^124 */ - System.err.println(errorMessage); - return null; + throw new ParseException(location, 0); } } @@ -64,22 +93,20 @@ public class DnaUtils * * @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(")")) + { + 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 @@ -99,31 +126,26 @@ public class DnaUtils * * @param location * @return + * @throws ParseException */ - static List parseJoin(String location) + static List parseJoin(String location) throws ParseException { List ranges = new ArrayList(); /* * take what is inside join() */ + if (!location.endsWith(")")) + { + throw new ParseException(location, 0); + } String joinedLocs = location.substring("join(".length(), location.length() - 1); String[] locations = joinedLocs.split(","); 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; }