View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2009 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  // KeywordAliasReader.java
20  // Since: May 20, 2010
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.format.keyword;
26  
27  import java.io.BufferedReader;
28  import java.io.FileNotFoundException;
29  import java.io.FileReader;
30  import java.io.IOException;
31  import java.io.Reader;
32  import java.util.ArrayList;
33  
34  import org.utgenome.format.keyword.GenomeKeywordEntry.KeywordAlias;
35  import org.xerial.util.StringUtil;
36  import org.xerial.util.log.Logger;
37  
38  /**
39   * keyword alias file is a tab-delimited file, each line of which has a keyword and its aliases.
40   * 
41   * <pre>
42   * # comment line
43   * (keyword1)	(aliases... )
44   * (keyword2)	(aliases... )
45   * ...
46   * </pre>
47   * 
48   * @author leo
49   * 
50   */
51  public class KeywordAliasReader {
52  
53  	private static Logger _logger = Logger.getLogger(KeywordAliasReader.class);
54  
55  	private BufferedReader reader;
56  	private int lineCount = 0;
57  
58  	public KeywordAliasReader(String file) throws FileNotFoundException {
59  		this(new FileReader(file));
60  	}
61  
62  	public KeywordAliasReader(Reader in) {
63  		this.reader = new BufferedReader(in);
64  	}
65  
66  	/**
67  	 * read a next keyword and its aliases
68  	 * 
69  	 * @return KeywordAlias or null if no further entry exists
70  	 * @throws IOException
71  	 */
72  	public KeywordAlias next() throws IOException {
73  		String line = reader.readLine();
74  		lineCount++;
75  		if (line == null)
76  			return null;
77  
78  		if (line.startsWith("#"))
79  			return next();
80  
81  		String[] split = line.split("[\t ]+");
82  		if (split.length < 2) {
83  			_logger.warn(String.format("line %d has no alias: %s", lineCount, line));
84  			return next();
85  		}
86  		else {
87  			ArrayList<String> keywords = new ArrayList<String>();
88  			for (int i = 1; i < split.length; ++i) {
89  				keywords.add(split[i]);
90  			}
91  			return new KeywordAlias(split[0], StringUtil.join(keywords, " "));
92  		}
93  
94  	}
95  
96  }