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);
35 String[] range = location.split("\\.\\.");
36 if (range.length == 2)
40 int start = Integer.valueOf(range[0]);
41 int end = Integer.valueOf(range[1]);
42 return Collections.singletonList(new int[] { start, end });
43 } catch (NumberFormatException e)
46 * could be a location like <1..888 or 1..>888
48 System.err.println(errorMessage);
55 * could be a location like 102.110 or 123^124
57 System.err.println(errorMessage);
63 * Parses a complement(locationSpec) into a list of start-end ranges
68 static List<int[]> parseComplement(String location)
71 * take what is inside complement()
73 String toComplement = location.substring("complement(".length(),
74 location.length() - 1);
75 List<int[]> ranges = parseLocation(toComplement);
79 * something bad in there
85 * reverse the order and direction of ranges
87 Collections.reverse(ranges);
88 for (int[] range : ranges)
98 * Parses a join(loc1,loc2,...,locn) into a list of start-end ranges
103 static List<int[]> parseJoin(String location)
105 List<int[]> ranges = new ArrayList<int[]>();
108 * take what is inside join()
110 String joinedLocs = location.substring("join(".length(),
111 location.length() - 1);
112 String[] locations = joinedLocs.split(",");
113 for (String loc : locations)
115 List<int[]> range = parseLocation(loc);
119 * something bad in there
125 ranges.addAll(range);