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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use std::io::{self, Write};
use crate::{
results::{
AlignmentOperation, LabeledQueryAlignment, QueryAlignment
}, Reference
};
/// A formatter that writes SAM records.
#[derive(Clone)]
pub struct SamFormatter {
itoa_buffer: itoa::Buffer,
}
impl SamFormatter {
pub fn new() -> Self {
Self {
itoa_buffer: itoa::Buffer::new(),
}
}
pub fn write_hd_header(&self, writer: &mut impl Write) -> Result<(), io::Error> {
writer.write_all(b"@HD\tVN:1.6\tSO:unsorted\n")?;
Ok(())
}
pub fn write_sq_header(
&self,
writer: &mut impl Write,
sn: &str,
ln: &u32,
) -> Result<(), io::Error> {
writer.write_all(format!("@SQ\tSN:{}\tLN:{}\n", sn, ln).as_bytes())?;
Ok(())
}
pub fn write_query_alignment(
&mut self,
writer: &mut impl Write,
query_alignment: &QueryAlignment,
qname: &str,
is_forward: bool,
reference: &Reference, // To parse the target label
) -> Result<(), io::Error> {
for target_alignment in query_alignment.0.iter() {
let target_label = reference.get_label_str(target_alignment.index).unwrap_or_default();
for alignment in target_alignment.alignments.iter() {
// (1) QNAME
writer.write(qname.as_bytes())?;
// (2) FLAG
writer.write(if is_forward { b"\t0\t" } else { b"\t16\t" })?;
// (3) RNAME
writer.write(target_label.as_bytes())?;
writer.write(b"\t")?;
// (4) POS
// SAM is 1-based, so add 1 to the 0-based position.
writer.write(
self.itoa_buffer.format(alignment.position.target.0 + 1).as_bytes()
)?;
// (5) MAPQ: 255 to indicate the score is not assigned
writer.write(b"\t255\t")?;
// (6) CIGAR
for op in alignment.operations.iter() {
writer.write(
self.itoa_buffer.format(op.count).as_bytes()
)?;
writer.write(
match op.operation {
AlignmentOperation::Match => b"=",
AlignmentOperation::Subst => b"X",
AlignmentOperation::Insertion => b"I",
AlignmentOperation::Deletion => b"D",
}
)?;
}
// (7) RNEXT
// (8) PNEXT
// (9) TLEN
// (10) SEQ
// (11) QUAL
// For a minimal single-end record (no mate information, no sequence/qual data)
writer.write(b"\t*\t0\t0\t*\t*\n")?;
}
}
Ok(())
}
// TODO: Remove duplicated code
pub fn write_labeled_query_alignment(
&mut self,
writer: &mut impl Write,
labeled_query_alignment: &LabeledQueryAlignment,
qname: &str,
is_forward: bool,
) -> Result<(), io::Error> {
for labeled_target_alignment in labeled_query_alignment.0.iter() {
for alignment in labeled_target_alignment.alignments.iter() {
// (1) QNAME
writer.write(qname.as_bytes())?;
// (2) FLAG
writer.write(if is_forward { b"\t0\t" } else { b"\t16\t" })?;
// (3) RNAME
writer.write(labeled_target_alignment.label.as_bytes())?;
writer.write(b"\t")?;
// (4) POS
// SAM is 1-based, so add 1 to the 0-based position.
writer.write(
self.itoa_buffer.format(alignment.position.target.0 + 1).as_bytes()
)?;
// (5) MAPQ: 255 to indicate the score is not assigned
writer.write(b"\t255\t")?;
// (6) CIGAR
for op in alignment.operations.iter() {
writer.write(
self.itoa_buffer.format(op.count).as_bytes()
)?;
writer.write(
match op.operation {
AlignmentOperation::Match => b"=",
AlignmentOperation::Subst => b"X",
AlignmentOperation::Insertion => b"I",
AlignmentOperation::Deletion => b"D",
}
)?;
}
// (7) RNEXT
// (8) PNEXT
// (9) TLEN
// (10) SEQ
// (11) QUAL
// For a minimal single-end record (no mate information, no sequence/qual data)
writer.write(b"\t*\t0\t0\t*\t*\n")?;
}
}
Ok(())
}
// TODO: Remove duplicated code
pub fn write_labeled_query_alignment_with_hclip(
&mut self,
writer: &mut impl Write,
labeled_query_alignment: &LabeledQueryAlignment,
qname: &str,
is_forward: bool,
query_length: u32,
) -> Result<(), io::Error> {
for labeled_target_alignment in labeled_query_alignment.0.iter() {
for alignment in labeled_target_alignment.alignments.iter() {
// (1) QNAME
writer.write(qname.as_bytes())?;
// (2) FLAG
writer.write(if is_forward { b"\t0\t" } else { b"\t16\t" })?;
// (3) RNAME
writer.write(labeled_target_alignment.label.as_bytes())?;
writer.write(b"\t")?;
// (4) POS
// SAM is 1-based, so add 1 to the 0-based position.
writer.write(
self.itoa_buffer.format(alignment.position.target.0 + 1).as_bytes()
)?;
// (5) MAPQ: 255 to indicate the score is not assigned
writer.write(b"\t255\t")?;
// (6) CIGAR
let lclip_size = alignment.position.query.0;
if lclip_size != 0 {
writer.write(
self.itoa_buffer.format(lclip_size).as_bytes()
)?;
writer.write(b"H")?;
}
for op in alignment.operations.iter() {
writer.write(
self.itoa_buffer.format(op.count).as_bytes()
)?;
writer.write(
match op.operation {
AlignmentOperation::Match => b"=",
AlignmentOperation::Subst => b"X",
AlignmentOperation::Insertion => b"I",
AlignmentOperation::Deletion => b"D",
}
)?;
}
let rclip_size = query_length - alignment.position.query.1;
if rclip_size != 0 {
writer.write(
self.itoa_buffer.format(rclip_size).as_bytes()
)?;
writer.write(b"H")?;
}
// (7) RNEXT
// (8) PNEXT
// (9) TLEN
// (10) SEQ
// (11) QUAL
// For a minimal single-end record (no mate information, no sequence/qual data)
writer.write(b"\t*\t0\t0\t*\t*\n")?;
}
}
Ok(())
}
}