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  // FastqRename.java
20  // Since: 2010/10/06
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.shell;
26  
27  import java.io.BufferedReader;
28  import java.io.BufferedWriter;
29  import java.io.FileReader;
30  import java.io.FileWriter;
31  import java.io.InputStreamReader;
32  import java.io.OutputStreamWriter;
33  import java.io.Reader;
34  import java.io.Writer;
35  
36  import org.utgenome.format.fastq.FastqRead;
37  import org.utgenome.format.fastq.FastqReader;
38  import org.utgenome.util.StandardInputStream;
39  import org.utgenome.util.StandardOutputStream;
40  import org.xerial.util.opt.Argument;
41  import org.xerial.util.opt.Option;
42  
43  public class FastqRename extends UTGBShellCommand {
44  
45  	@Option(symbol = "g", longName = "readgroup", varName = "RG", description = "read group name (e.g., HG0001.PE001.L3)")
46  	private String readGroup;
47  
48  	@Argument(index = 0, name = "input")
49  	private String input = "-";
50  
51  	@Argument(index = 1, name = "input")
52  	private String output = "-";
53  
54  	@Override
55  	public String name() {
56  		return "rename-fastq";
57  	}
58  
59  	@Override
60  	public void execute(String[] args) throws Exception {
61  
62  		if (readGroup == null)
63  			throw new UTGBShellException("-g (read group prefix) must be specified");
64  
65  		Reader in = null;
66  		if ("-".equals(input))
67  			in = new BufferedReader(new InputStreamReader(new StandardInputStream()));
68  		else
69  			in = new BufferedReader(new FileReader(input));
70  
71  		Writer out = null;
72  		if ("-".equals(output))
73  			out = new BufferedWriter(new OutputStreamWriter(new StandardOutputStream()));
74  		else
75  			out = new BufferedWriter(new FileWriter(output));
76  
77  		int readCount = 0;
78  
79  		FastqReader fastq = new FastqReader(in);
80  		for (FastqRead read; (read = fastq.next()) != null; readCount++) {
81  			read.seqname = String.format("%s.%d", readGroup, readCount + 1);
82  			out.append(read.toFASTQString());
83  		}
84  
85  		out.flush();
86  		out.close();
87  
88  		in.close();
89  	}
90  
91  	@Override
92  	public String getOneLinerDescription() {
93  		return "rename fastq reads";
94  	}
95  
96  }