View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2008 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  // Sequence.java
20  // Since: Mar 15, 2010
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.shell;
26  
27  import org.utgenome.format.fasta.CompactFASTA;
28  import org.utgenome.format.fasta.GenomeSequence;
29  import org.utgenome.gwt.utgb.client.bio.ChrLoc;
30  import org.xerial.util.log.Logger;
31  import org.xerial.util.opt.Argument;
32  
33  /**
34   * Retrieve the genome sequence from the pack file
35   * 
36   * @author leo
37   * 
38   */
39  public class Sequence extends UTGBShellCommand {
40  
41  	private static Logger _logger = Logger.getLogger(Sequence.class);
42  
43  	@Argument(index = 0, name = "(ref pac)", required = true)
44  	private String packFilePrefix;
45  
46  	@Argument(index = 1, name = "(chr:start-end)")
47  	private String query;
48  
49  	@Override
50  	public void execute(String[] args) throws Exception {
51  
52  		if (query == null)
53  			throw new UTGBShellException("no query is given");
54  
55  		CompactFASTA f = new CompactFASTA(packFilePrefix);
56  
57  		ChrLoc loc = RegionQueryExpr.parse(query);
58  		GenomeSequence seq = f.getSequence(loc.chr, loc.start - 1, loc.end);
59  		if (seq == null) {
60  			_logger.warn("no entry found: " + loc);
61  		}
62  
63  		int cursor = 0;
64  		final int step = 50;
65  		while (cursor < seq.length()) {
66  			for (int i = 0; i < step && cursor < seq.length(); ++i, ++cursor) {
67  				System.out.print(seq.charAt(cursor));
68  			}
69  			System.out.println();
70  		}
71  
72  	}
73  
74  	@Override
75  	public String getOneLinerDescription() {
76  		return "retrieve a genome sequence from a pack file";
77  	}
78  
79  	@Override
80  	public String name() {
81  		return "seq";
82  	}
83  
84  }