View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2011 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  // SAMToFastq.java
20  // Since: 2011/01/12
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.shell;
26  
27  import java.io.BufferedWriter;
28  import java.io.File;
29  import java.io.FileWriter;
30  import java.io.OutputStreamWriter;
31  
32  import net.sf.samtools.SAMFileReader;
33  import net.sf.samtools.SAMFileReader.ValidationStringency;
34  import net.sf.samtools.SAMRecord;
35  import net.sf.samtools.SAMRecordIterator;
36  
37  import org.utgenome.format.fastq.FastqRead;
38  import org.utgenome.util.StandardInputStream;
39  import org.xerial.util.io.StandardOutputStream;
40  import org.xerial.util.opt.Argument;
41  
42  public class SAMToFastq extends UTGBShellCommand {
43  
44  	@Override
45  	public String name() {
46  		return "sam2fastq";
47  	}
48  
49  	@Argument(index = 0)
50  	private String input = "-";
51  
52  	@Argument(index = 1)
53  	private String output = "-";
54  
55  	@Override
56  	public void execute(String[] args) throws Exception {
57  
58  		SAMFileReader sam;
59  		SAMFileReader.setDefaultValidationStringency(ValidationStringency.SILENT);
60  		if ("-".equals(input)) {
61  			sam = new SAMFileReader(new StandardInputStream(), false);
62  		}
63  		else {
64  			sam = new SAMFileReader(new File(input), false);
65  		}
66  
67  		BufferedWriter out;
68  		if ("-".equals(output)) {
69  			out = new BufferedWriter(new OutputStreamWriter(new StandardOutputStream()));
70  		}
71  		else {
72  			out = new BufferedWriter(new FileWriter(output));
73  		}
74  
75  		SAMRecordIterator it = sam.iterator();
76  		try {
77  			for (; it.hasNext();) {
78  				SAMRecord rec = it.next();
79  				FastqRead fastq = new FastqRead(rec.getReadName(), rec.getReadString(), rec.getBaseQualityString());
80  				out.append(fastq.toFASTQString());
81  			}
82  		}
83  		finally {
84  			out.flush();
85  			out.close();
86  			it.close();
87  			sam.close();
88  		}
89  	}
90  
91  	@Override
92  	public String getOneLinerDescription() {
93  		return "convert sam/bam file into fastq format";
94  	}
95  
96  }