View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2007 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // utgb-core Project
18  //
19  // EagyGeneLine.java
20  // Since: Dec 10, 2007
21  //
22  // $URL$ 
23  // $Author$
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  	 * parsing each line then convert its contents into a ParameterMap
45  	 * 
46  	 * @param line
47  	 * @return
48  	 * @throws UTGBException
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  				// ignore the invalid parameter
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 }