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.format.sam;
26
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.Reader;
30 import java.io.StringWriter;
31 import java.io.Writer;
32
33 import net.sf.samtools.SAMFileReader;
34 import net.sf.samtools.SAMRecord;
35 import net.sf.samtools.SAMRecord.SAMTagAndValue;
36 import net.sf.samtools.util.CloseableIterator;
37
38 import org.apache.tools.ant.util.ReaderInputStream;
39 import org.utgenome.format.FormatConversionReader;
40 import org.utgenome.gwt.utgb.client.bio.SAMRead;
41 import org.utgenome.gwt.utgb.client.bio.SAMReadLight;
42 import org.utgenome.gwt.utgb.client.util.Properties;
43 import org.xerial.silk.SilkWriter;
44
45
46
47
48
49
50
51 public class SAM2SilkReader extends FormatConversionReader {
52
53 public SAM2SilkReader(InputStream input) throws IOException {
54 super(input, new Converter());
55 }
56
57 public SAM2SilkReader(Reader input) throws IOException {
58 this(new ReaderInputStream(input));
59 }
60
61 private static class Converter extends FormatConversionReader.PipeConsumer {
62
63 @Override
64 public void consume(InputStream in, Writer out) throws Exception {
65 if (out == null)
66 return;
67
68 SAMFileReader samReader = new SAMFileReader(in);
69
70 SilkWriter w = new SilkWriter(out);
71 w.preamble();
72 w.preamble("schema record(qname, flag, rname, start, end, mapq, cigar, mrnm, mpos, isize, seq, qual, tag, vtype, tag*)");
73 for (CloseableIterator<SAMRecord> it = samReader.iterator(); it.hasNext();) {
74 SAMRecord rec = it.next();
75 toSilk(rec, w);
76 }
77
78 }
79
80 }
81
82
83
84
85
86
87
88 public static SAMRead convertToSAMRead(SAMRecord record) {
89 SAMRead read = new SAMRead(record.getAlignmentStart(), record.getAlignmentEnd() + 1);
90 if (record != null) {
91 read.qname = record.getReadName();
92 read.flag = record.getFlags();
93 read.rname = record.getReferenceName();
94 read.mapq = record.getMappingQuality();
95 read.cigar = record.getCigarString();
96 read.mrnm = record.getMateReferenceName();
97 read.mStart = record.getMateAlignmentStart();
98 read.iSize = record.getInferredInsertSize();
99 read.seq = record.getReadString();
100 read.qual = record.getBaseQualityString();
101 read.unclippedStart = record.getUnclippedStart();
102 read.unclippedEnd = record.getUnclippedEnd() + 1;
103 read.tag = new Properties();
104 for (SAMTagAndValue tag : record.getAttributes()) {
105 read.tag.add(tag.tag, String.valueOf(tag.value));
106 }
107 }
108
109 return read;
110 }
111
112
113
114
115
116
117
118 public static SAMReadLight convertToSAMReadLight(SAMRecord record) {
119 SAMReadLight read = new SAMReadLight(record.getAlignmentStart(), record.getAlignmentEnd() + 1);
120 if (record != null) {
121 read.qname = record.getReadName();
122 read.flag = record.getFlags();
123 read.cigar = record.getCigarString();
124 read.unclippedStart = record.getUnclippedStart();
125 read.unclippedEnd = record.getUnclippedEnd() + 1;
126 }
127 return read;
128 }
129
130
131
132
133
134
135
136 public static void toSilk(SAMRecord rec, SilkWriter w) {
137 StringWriter buf = new StringWriter();
138 SilkWriter rw = w.node("record");
139 rw.leaf("qname", rec.getReadName());
140 rw.leaf("flag", rec.getFlags());
141 rw.leaf("rname", rec.getReferenceName());
142 rw.leaf("start", rec.getAlignmentStart());
143 rw.leaf("end", rec.getAlignmentEnd() + 1);
144 rw.leaf("unclipped start", rec.getUnclippedStart());
145 rw.leaf("unclipped end", rec.getUnclippedEnd() + 1);
146 rw.leaf("mapq", rec.getMappingQuality());
147 rw.leaf("cigar", rec.getCigarString());
148 rw.leaf("mrname", rec.getMateReferenceName());
149 rw.leaf("mstart", rec.getMateAlignmentStart());
150 rw.leaf("isize", rec.getInferredInsertSize());
151 rw.leaf("seq", rec.getReadString());
152 rw.leaf("qual", String.format("\"%s\"", rec.getBaseQualityString()));
153 SilkWriter tw = rw.node("tag");
154 for (SAMTagAndValue each : rec.getAttributes()) {
155 tw.leaf(each.tag, each.value);
156 }
157 }
158
159
160
161
162
163
164
165 public static String toSilk(SAMRecord rec) {
166 StringWriter buf = new StringWriter();
167 SilkWriter w = new SilkWriter(buf);
168 toSilk(rec, w);
169 w.flush();
170 return buf.toString();
171 }
172
173 }