3 import java.util.ArrayList;
4 import java.util.Collections;
11 * Parses an ENA/GenBank format location specifier and returns a list of
12 * [start, end] ranges. Returns null if not able to parse.
16 * @see http://www.insdc.org/files/feature_table.html#3.4
18 public static List<int[]> parseLocation(String location)
20 if (location.startsWith("join("))
22 return parseJoin(location);
24 else if (location.startsWith("complement("))
26 return parseComplement(location);
28 String errorMessage = "Unable to process location specifier: "
30 if (location.startsWith("order("))
32 System.err.println(errorMessage);
37 * try to parse m..n (or simply m)
38 * also handles <m..n or m..>n (discarding < or >)
40 String[] range = location.split("\\.\\.");
41 if (range.length == 1 || range.length == 2)
45 int start = parseRangeEnd(range[0]);
46 int end = range.length == 1 ? start : parseRangeEnd(range[1]);
47 return Collections.singletonList(new int[] { start, end });
48 } catch (NumberFormatException e)
51 * could be a location like <1..888 or 1..>888
53 System.err.println(errorMessage);
60 * could be a location like 102.110 or 123^124
62 System.err.println(errorMessage);
68 * Returns the integer value of a locus, discarding any < or > prefix
70 * @throws NumberFormatException
71 * if value is not numeric
73 static int parseRangeEnd(String loc)
76 if (loc.startsWith("<") || loc.startsWith(">"))
78 loc = loc.substring(1);
80 return Integer.valueOf(loc);
84 * Parses a complement(locationSpec) into a list of start-end ranges
89 static List<int[]> parseComplement(String location)
92 * take what is inside complement()
94 if (!location.endsWith(")"))
98 String toComplement = location.substring("complement(".length(),
99 location.length() - 1);
100 List<int[]> ranges = parseLocation(toComplement);
104 * something bad in there
110 * reverse the order and direction of ranges
112 Collections.reverse(ranges);
113 for (int[] range : ranges)
123 * Parses a join(loc1,loc2,...,locn) into a list of start-end ranges
128 static List<int[]> parseJoin(String location)
130 List<int[]> ranges = new ArrayList<int[]>();
133 * take what is inside join()
135 if (!location.endsWith(")"))
139 String joinedLocs = location.substring("join(".length(),
140 location.length() - 1);
141 String[] locations = joinedLocs.split(",");
142 for (String loc : locations)
144 List<int[]> range = parseLocation(loc);
148 * something bad in there
154 ranges.addAll(range);