View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2011 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-shell Project
18  //
19  // RegionQueryExpr.java
20  // Since: 2011/01/06
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.shell;
26  
27  import java.util.regex.Matcher;
28  import java.util.regex.Pattern;
29  
30  import org.utgenome.gwt.utgb.client.bio.ChrLoc;
31  
32  public class RegionQueryExpr {
33  
34  	private static Pattern p = Pattern.compile("([^:]+)(:([0-9]+)-([0-9]+))?");
35  
36  	public static ChrLoc parse(String expr) throws UTGBShellException {
37  		Matcher m = p.matcher(expr.replaceAll(",", "")); // remove comma
38  		if (!m.matches())
39  			throw new UTGBShellException("invalid query format:" + expr);
40  		String chr = m.group(1);
41  		String sStart = m.group(3);
42  		String sEnd = m.group(4);
43  
44  		int start = 0;
45  		if (sStart != null)
46  			start = Integer.parseInt(sStart);
47  		int end = Integer.MAX_VALUE;
48  		if (sEnd != null)
49  			end = Integer.parseInt(sEnd);
50  
51  		return new ChrLoc(chr, start, end);
52  	}
53  }