1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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 }