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)
39 String[] range = location.split("\\.\\.");
40 if (range.length == 1 || range.length == 2)
44 int start = Integer.valueOf(range[0]);
45 int end = range.length == 1 ? start : Integer.valueOf(range[1]);
46 return Collections.singletonList(new int[] { start, end });
47 } catch (NumberFormatException e)
50 * could be a location like <1..888 or 1..>888
52 System.err.println(errorMessage);
59 * could be a location like 102.110 or 123^124
61 System.err.println(errorMessage);
67 * Parses a complement(locationSpec) into a list of start-end ranges
72 static List<int[]> parseComplement(String location)
75 * take what is inside complement()
77 if (!location.endsWith(")"))
81 String toComplement = location.substring("complement(".length(),
82 location.length() - 1);
83 List<int[]> ranges = parseLocation(toComplement);
87 * something bad in there
93 * reverse the order and direction of ranges
95 Collections.reverse(ranges);
96 for (int[] range : ranges)
106 * Parses a join(loc1,loc2,...,locn) into a list of start-end ranges
111 static List<int[]> parseJoin(String location)
113 List<int[]> ranges = new ArrayList<int[]>();
116 * take what is inside join()
118 if (!location.endsWith(")"))
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);
131 * something bad in there
137 ranges.addAll(range);