View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2010 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  // FastqEntry.java
20  // Since: Jun 14, 2010
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   * An entry of FASTQ format
36   * 
37   * @author leo
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; // no more entry
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 }