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.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 }