1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.utgenome.format.egt;
26
27 import org.utgenome.UTGBException;
28 import org.xerial.util.Pair;
29 import org.xerial.util.StringUtil;
30 import org.xerial.xml.XMLGenerator;
31
32 public class EagyGeneLine {
33 private ParameterMap parameterMap;
34
35 public EagyGeneLine(String line) throws UTGBException {
36 parameterMap = parseLine(line);
37 }
38
39 public Parameter getValue(String key) {
40 return parameterMap.get(key);
41 }
42
43
44
45
46
47
48
49
50 public static ParameterMap parseLine(String line) throws UTGBException {
51 ParameterMap map = new ParameterMap();
52
53 String[] element = split(line);
54 if (element.length == 0)
55 return map;
56
57 ParameterMap childMap = new ParameterMap();
58 for (int i = 1; i < element.length; i++) {
59 try {
60 Pair<String, String> pair = parse(element[i]);
61
62 String key = pair.getFirst();
63 String value = pair.getSecond();
64 if (key.equals("range")) {
65 setRange(value, childMap);
66 }
67 else if (key.equals("exon")) {
68 ParameterMap rangeMap = new ParameterMap();
69 setRange(value, rangeMap);
70 childMap.put(key, rangeMap);
71 }
72 else {
73 childMap.put(key, new ValueParameter(value));
74 }
75 }
76 catch (UTGBException e) {
77
78 System.err.println(e);
79 }
80 }
81 map.put(element[0], childMap);
82
83 return map;
84 }
85
86 public static String[] split(String inputLine) {
87 return inputLine.split("[\t ]+");
88 }
89
90 public static Pair<String, String> parse(String parameter) throws UTGBException {
91 String[] keyAndValue = parameter.split("=");
92
93 if (keyAndValue.length == 2) {
94 return new Pair<String, String>(keyAndValue[0], StringUtil.unquote(keyAndValue[1]));
95 }
96 else {
97 throw new UTGBException("invalid format: " + parameter);
98 }
99
100 }
101
102 public static void setRange(String value, ParameterMap map) throws UTGBException {
103 String[] range = splitByComma(value);
104 if (range.length != 2)
105 throw new UTGBException("invalid range: " + value);
106 map.put("start", new ValueParameter(range[0]));
107 map.put("end", new ValueParameter(range[1]));
108 }
109
110 public static String[] splitByComma(String value) {
111 return value.split(",");
112 }
113
114 public void toXML(XMLGenerator xout) {
115 parameterMap.toXML(xout);
116 }
117
118 }