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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
mod builder;
pub mod feature;
mod flags;
mod next_mate_flags;
mod read_group_id;
pub mod resolve;
pub mod tag;
pub use self::{
builder::Builder, feature::Feature, flags::Flags, next_mate_flags::NextMateFlags,
read_group_id::ReadGroupId, tag::Tag,
};
use std::{fmt, str};
use noodles_bam as bam;
use noodles_sam as sam;
#[derive(Clone, PartialEq)]
pub struct Record {
pub(crate) id: i64,
pub(crate) bam_bit_flags: sam::record::Flags,
pub(crate) cram_bit_flags: Flags,
pub(crate) reference_sequence_id: Option<bam::record::ReferenceSequenceId>,
pub(crate) read_length: i32,
pub(crate) alignment_start: i32,
pub(crate) read_group: ReadGroupId,
pub(crate) read_name: Vec<u8>,
pub(crate) next_mate_bit_flags: NextMateFlags,
pub(crate) next_fragment_reference_sequence_id: Option<bam::record::ReferenceSequenceId>,
pub(crate) next_mate_alignment_start: i32,
pub(crate) template_size: i32,
pub(crate) distance_to_next_fragment: i32,
pub(crate) tags: Vec<Tag>,
pub(crate) bases: Vec<u8>,
pub(crate) features: Vec<Feature>,
pub(crate) mapping_quality: sam::record::MappingQuality,
pub(crate) quality_scores: Vec<u8>,
}
impl Record {
pub fn builder() -> Builder {
Builder::default()
}
pub(crate) fn id(&self) -> i64 {
self.id
}
pub fn bam_flags(&self) -> sam::record::Flags {
self.bam_bit_flags
}
pub fn flags(&self) -> Flags {
self.cram_bit_flags
}
pub fn reference_sequence_id(&self) -> Option<bam::record::ReferenceSequenceId> {
self.reference_sequence_id
}
pub fn read_length(&self) -> i32 {
self.read_length
}
pub fn alignment_start(&self) -> i32 {
self.alignment_start
}
pub fn alignment_end(&self) -> i32 {
let mut alignment_span = self.read_length();
for feature in self.features() {
match feature {
Feature::Insertion(_, bases) => {
alignment_span -= bases.len() as i32;
}
Feature::InsertBase(_, _) => {
alignment_span -= 1;
}
Feature::Deletion(_, len) => {
alignment_span += len;
}
Feature::ReferenceSkip(_, len) => {
alignment_span += len;
}
Feature::SoftClip(_, bases) => {
alignment_span -= bases.len() as i32;
}
_ => {}
}
}
self.alignment_start() + alignment_span - 1
}
pub fn read_group_id(&self) -> ReadGroupId {
self.read_group
}
pub fn read_name(&self) -> &[u8] {
&self.read_name
}
pub fn next_mate_flags(&self) -> NextMateFlags {
self.next_mate_bit_flags
}
pub fn next_fragment_reference_sequence_id(&self) -> Option<bam::record::ReferenceSequenceId> {
self.next_fragment_reference_sequence_id
}
pub fn next_mate_alignment_start(&self) -> i32 {
self.next_mate_alignment_start
}
pub fn template_size(&self) -> i32 {
self.template_size
}
pub fn distance_to_next_fragment(&self) -> i32 {
self.distance_to_next_fragment
}
pub fn tags(&self) -> &[Tag] {
&self.tags
}
pub(crate) fn add_tag(&mut self, tag: Tag) {
self.tags.push(tag);
}
pub fn bases(&self) -> &[u8] {
&self.bases
}
pub fn features(&self) -> &[Feature] {
&self.features
}
pub(crate) fn add_feature(&mut self, feature: Feature) {
self.features.push(feature);
}
pub fn mapping_quality(&self) -> sam::record::MappingQuality {
self.mapping_quality
}
pub fn quality_scores(&self) -> &[u8] {
&self.quality_scores
}
}
impl Default for Record {
fn default() -> Self {
Builder::default().build()
}
}
impl fmt::Debug for Record {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let read_name = str::from_utf8(self.read_name());
fmt.debug_struct("Record")
.field("id", &self.id)
.field("bam_bit_flags", &self.bam_flags())
.field("cram_bit_flags", &self.flags())
.field("reference_id", &self.reference_sequence_id)
.field("read_length", &self.read_length)
.field("alignment_start", &self.alignment_start)
.field("read_group", &self.read_group)
.field("read_name", &read_name)
.field("next_mate_bit_flags", &self.next_mate_flags())
.field(
"next_fragment_reference_sequence_id",
&self.next_fragment_reference_sequence_id,
)
.field("next_mate_alignment_start", &self.next_mate_alignment_start)
.field("template_size", &self.template_size)
.field("distance_to_next_fragment", &self.distance_to_next_fragment)
.field("tags", &self.tags)
.field("bases", &self.bases)
.field("features", &self.features)
.field("mapping_quality", &self.mapping_quality)
.field("quality_scores", &self.quality_scores)
.finish()
}
}