1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.utgenome.format.fastq;
24
25 import java.io.BufferedReader;
26 import java.io.IOException;
27 import java.io.StringWriter;
28
29 import org.utgenome.UTGBErrorCode;
30 import org.utgenome.UTGBException;
31 import org.xerial.silk.SilkWriter;
32 import org.xerial.util.StringUtil;
33
34
35
36
37
38
39
40 public class FastqRead {
41 public String seqname;
42 public String seq;
43 public String qual;
44
45 public FastqRead() {
46 }
47
48 public FastqRead(String seqname, String seq, String qual) {
49 this.seqname = seqname;
50 this.seq = seq;
51 this.qual = qual;
52 }
53
54 public String toFASTQString() {
55 StringBuilder buf = new StringBuilder();
56 buf.append("@");
57 buf.append(seqname);
58 buf.append(StringUtil.NEW_LINE);
59 buf.append(seq);
60 buf.append(StringUtil.NEW_LINE);
61 buf.append("+");
62 buf.append(StringUtil.NEW_LINE);
63 buf.append(qual);
64 buf.append(StringUtil.NEW_LINE);
65 return buf.toString();
66 }
67
68 public static FastqRead parse(BufferedReader reader) throws UTGBException {
69
70 try {
71 String seqNameLine = reader.readLine();
72 String sequence = reader.readLine();
73 reader.readLine();
74 String qual = reader.readLine();
75
76 if (seqNameLine == null || sequence == null || qual == null) {
77 return null;
78 }
79
80 if (seqNameLine.length() < 2) {
81 throw new UTGBException(UTGBErrorCode.PARSE_ERROR, "invalid sequence name: " + seqNameLine);
82 }
83
84 return new FastqRead(seqNameLine.substring(1), sequence, qual);
85 }
86 catch (IOException e) {
87 throw new UTGBException(UTGBErrorCode.PARSE_ERROR, "invalid fastq block");
88 }
89 }
90
91 public void toSilk(SilkWriter silk) {
92 SilkWriter sub = silk.node("fastq");
93 sub.leaf("name", seqname);
94 sub.leaf("seq", seq);
95 sub.leaf("qual", qual);
96 }
97
98 public String toSilk() {
99 StringWriter w = new StringWriter();
100 SilkWriter sw = new SilkWriter(w);
101 toSilk(sw);
102 sw.flush();
103 return w.toString();
104 }
105
106 }