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  // Keyword.java
20  // Since: May 20, 2010
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.shell;
26  
27  import java.io.BufferedReader;
28  import java.io.File;
29  import java.io.FileInputStream;
30  import java.io.FileNotFoundException;
31  import java.io.FileReader;
32  import java.io.InputStream;
33  import java.io.InputStreamReader;
34  import java.io.Reader;
35  
36  import org.utgenome.format.keyword.KeywordDB;
37  import org.utgenome.gwt.utgb.client.bio.KeywordSearchResult;
38  import org.utgenome.shell.Import.FileType;
39  import org.utgenome.util.StandardInputStream;
40  import org.xerial.lens.SilkLens;
41  import org.xerial.util.StopWatch;
42  import org.xerial.util.log.Logger;
43  import org.xerial.util.opt.Argument;
44  import org.xerial.util.opt.Option;
45  
46  /**
47   * keyword DB management tool
48   * 
49   * @author leo
50   * 
51   */
52  public class Keyword extends UTGBShellCommand {
53  
54  	private static Logger _logger = Logger.getLogger(Keyword.class);
55  
56  	@Option(symbol = "f", longName = "file", description = "keyword database file name. default=db/keyword.sqlite")
57  	private String dbFile = "db/keyword.sqlite";
58  
59  	public static enum SubCommand {
60  		IMPORT, ALIAS, SEARCH
61  	};
62  
63  	/**
64  	 * import (from BED, FASTA.index), alias
65  	 */
66  	@Argument(index = 0, name = "command")
67  	private SubCommand subCommand = null;
68  
69  	@Argument(index = 1, name = "input file")
70  	private String input = "-";
71  
72  	@Option(symbol = "r", longName = "ref", description = "reference sequence name for the keywords, e.g., hg19, ce6, etc.")
73  	private String ref;
74  
75  	@Option(symbol = "p", description = "keyword page (default = 1)")
76  	private int page = 1;
77  	@Option(symbol = "s", description = "page size (default = 10)")
78  	private int pageSize = 10;
79  
80  	@Option(symbol = "t", description = "input file type (AUTO, BED) for importing")
81  	private FileType inputFileType = FileType.AUTO;
82  
83  	@Override
84  	public void execute(String[] args) throws Exception {
85  
86  		if (subCommand == null)
87  			throw new UTGBShellException("specify one of the sub command: utgb keyword (import | alias | search)");
88  
89  		_logger.info("keyword database: " + dbFile);
90  		File dbPath = new File(getProjectRoot(), dbFile);
91  		if (!dbPath.getParentFile().exists())
92  			dbPath.getParentFile().mkdirs();
93  
94  		KeywordDB db = new KeywordDB(dbPath);
95  
96  		StopWatch timer = new StopWatch();
97  		try {
98  			switch (subCommand) {
99  			case IMPORT: {
100 				if (ref == null)
101 					throw new UTGBShellException("specify a reference sequence name with -r option");
102 
103 				if (inputFileType == FileType.AUTO)
104 					inputFileType = Import.detectFileType(input);
105 				switch (inputFileType) {
106 				case BED: {
107 					Reader r = getInputFileReader();
108 					try {
109 						db.importFromBED(ref, r);
110 						_logger.info(String.format("done. %s sec.", timer.getElapsedTime()));
111 					}
112 					finally {
113 						r.close();
114 					}
115 				}
116 					break;
117 				case SAM:
118 				case BAM: {
119 					InputStream in = getInputFileStream();
120 					try {
121 						db.importFromBAM(ref, in);
122 						_logger.info(String.format("done. %s sec.", timer.getElapsedTime()));
123 					}
124 					finally {
125 						in.close();
126 					}
127 				}
128 					break;
129 				case KTAB: {
130 					Reader r = getInputFileReader();
131 					try {
132 						db.importFromTAB(ref, r);
133 						_logger.info(String.format("done. %s sec.", timer.getElapsedTime()));
134 					}
135 					finally {
136 						r.close();
137 					}
138 				}
139 					break;
140 				default:
141 					throw new UTGBShellException(String.format("Unsupported (or unknown) file type. Use -t option to explicitely specify the file type."));
142 				}
143 				break;
144 			}
145 			case ALIAS:
146 				Reader r = getInputFileReader();
147 
148 				db.importKeywordAliasFile(r);
149 				_logger.info(String.format("done. %s sec.", timer.getElapsedTime()));
150 				break;
151 			case SEARCH:
152 				KeywordSearchResult query = db.query(ref, input, page, pageSize);
153 				System.out.println(SilkLens.toSilk(query));
154 				break;
155 			}
156 
157 		}
158 		finally {
159 			db.close();
160 		}
161 
162 	}
163 
164 	private BufferedReader getInputFileReader() throws FileNotFoundException {
165 		if (input != null && !input.equals("-")) {
166 			_logger.info("input file: " + input);
167 			return new BufferedReader(new FileReader(input));
168 		}
169 		else {
170 			_logger.info("use STDIN for input");
171 			return new BufferedReader(new InputStreamReader(new StandardInputStream()));
172 		}
173 	}
174 
175 	private InputStream getInputFileStream() throws FileNotFoundException {
176 		if (input != null && !input.equals("-")) {
177 			_logger.info("input file: " + input);
178 			return new FileInputStream(input);
179 		}
180 		else {
181 			_logger.info("use STDIN for input");
182 			return new StandardInputStream();
183 		}
184 	}
185 
186 	@Override
187 	public String getOneLinerDescription() {
188 		return "create a keyword database";
189 	}
190 
191 	@Override
192 	public String name() {
193 		return "keyword";
194 	}
195 
196 }