3 import java.text.ParseException;
4 import java.util.ArrayList;
5 import java.util.Collections;
12 * Parses an ENA/GenBank format location specifier and returns a list of
13 * [start, end] ranges. Throws an exception if not able to parse.
15 * Currently we do not parse "order()" specifiers, or indeterminate ranges of
16 * the format "<start..end" or "start..>end" or "start.end" or
21 * @throws ParseException
22 * if unable to parse the location (the exception message is the
23 * location specifier being parsed); we use ParseException in
24 * preference to the unchecked IllegalArgumentException
25 * @see http://www.insdc.org/files/feature_table.html#3.4
27 public static List<int[]> parseLocation(String location)
30 if (location.startsWith("join("))
32 return parseJoin(location);
34 else if (location.startsWith("complement("))
36 return parseComplement(location);
38 if (location.startsWith("order("))
40 throw new ParseException(location, 0);
44 * try to parse m..n (or simply m)
46 String[] range = location.split("\\.\\.");
47 if (range.length == 1 || range.length == 2)
51 int start = Integer.valueOf(range[0]);
52 int end = range.length == 1 ? start : Integer.valueOf(range[1]);
53 return Collections.singletonList(new int[] { start, end });
54 } catch (NumberFormatException e)
57 * could be a location like <1..888 or 1..>888
59 throw new ParseException(location, 0);
65 * could be a location like 102.110 or 123^124
67 throw new ParseException(location, 0);
72 * Parses a complement(locationSpec) into a list of start-end ranges
76 * @throws ParseException
78 static List<int[]> parseComplement(String location) throws ParseException
81 * take what is inside complement()
83 if (!location.endsWith(")"))
85 throw new ParseException(location, 0);
87 String toComplement = location.substring("complement(".length(),
88 location.length() - 1);
89 List<int[]> ranges = parseLocation(toComplement);
92 * reverse the order and direction of ranges
94 Collections.reverse(ranges);
95 for (int[] range : ranges)
105 * Parses a join(loc1,loc2,...,locn) into a list of start-end ranges
109 * @throws ParseException
111 static List<int[]> parseJoin(String location) throws ParseException
113 List<int[]> ranges = new ArrayList<int[]>();
116 * take what is inside join()
118 if (!location.endsWith(")"))
120 throw new ParseException(location, 0);
122 String joinedLocs = location.substring("join(".length(),
123 location.length() - 1);
124 String[] locations = joinedLocs.split(",");
125 for (String loc : locations)
127 List<int[]> range = parseLocation(loc);
128 ranges.addAll(range);