-
Notifications
You must be signed in to change notification settings - Fork 13
/
flags.jl
251 lines (198 loc) · 7.18 KB
/
flags.jl
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# Flags
# =========
#
"""
flags(record::XAMRecord})::UInt16
Get the bitwise flags of `record`.
The returned value is a `UInt16` of each flag being OR'd together.
The possible flags are:
0x0001 template having multiple segments in sequencing
0x0002 each segment properly aligned according to the aligner
0x0004 segment unmapped
0x0008 next segment in the template unmapped
0x0010 SEQ being reverse complemented
0x0020 SEQ of the next segment in the template being reverse complemented
0x0040 the first segment in the template
0x0080 the last segment in the template
0x0100 secondary alignment
0x0200 not passing filters, such as platform/vendor quality controls
0x0400 PCR or optical duplicate
0x0800 supplementary alignment
"""
function flags end
# Bitwise flag (or FLAG).
for (name, bits, doc) in [
(:PAIRED, UInt16(0x001), "the segment is paired with other segments"),
(:PROPER_PAIR, UInt16(0x002), "the segment is in a template where all segments are properly aligned according to the aligner"),
(:UNMAPPED, UInt16(0x004), "the segment itself is unmapped; conflictive with FLAG_PROPER_PAIR"),
(:NEXT_UNMAPPED, UInt16(0x008), "the next segment in the template is unmapped"),
(:REVERSE, UInt16(0x010), "the *SEQ*uence is reverse complemented"),
(:NEXT_REVERSE, UInt16(0x020), "the *SEQ*uence of the next segment in the template is reverse complemented" ),
(:FIRST_SEGMENT, UInt16(0x040), "the segment is the first in the template"),
(:LAST_SEGMENT, UInt16(0x080), "the segment is last in the template"),
(:SECONDARY, UInt16(0x100), "not primary alignment"),
(:QCFAIL, UInt16(0x200), "QC failure"),
(:DUPLICATE, UInt16(0x400), "optical or PCR duplicate"),
(:SUPPLEMENTARY, UInt16(0x800), "supplementary alignment"),
]
@assert bits isa UInt16 "The bits must be of type UInt16."
sym = Symbol("FLAG_", name)
docstring = """ $sym
SAM/BAM flags: $doc
See also: [`flags`](@ref)
"""
@eval begin
@doc $(docstring) const $(sym) = $(bits)
end
end
"""
ispaired(record::XAMRecord)::Bool
Query whether the segment is in a template having multiple segments in sequencing.
"""
function ispaired(record::XAMRecord)::Bool
return flags(record) & FLAG_PAIRED == FLAG_PAIRED
end
"""
isproperpair(record::XAMRecord)::Bool
Query whether the segment is in a template where all segments are properly aligned according to the aligner.
"""
function isproperpair(record::XAMRecord)::Bool
return flags(record) & PROPER_PAIR == PROPER_PAIR
end
"""
isunmapped(record::XAMRecord)::Bool
Query whether the segment is unmapped.
"""
function isunmapped(record::XAMRecord)::Bool
return flags(record) & FLAG_UNMAPPED == FLAG_UNMAPPED
end
"""
ismapped(record::XAMRecord)::Bool
Query whether the segment is mapped.
"""
function ismapped(record::XAMRecord)::Bool
# return flags(record) & FLAG_UNMAPPED == 0
return isfilled(record) && (flags(record) & FLAG_UNMAPPED == 0)
end
"""
isnextunmapped(record::XAMRecord)::Bool
Query whether the next segment in the template is unmapped.
"""
function isnextunmapped(record::XAMRecord)::Bool
return flags(record) & FLAG_NEXT_UNMAPPED == FLAG_NEXT_UNMAPPED
end
"""
isnextmapped(record::XAMRecord)::Bool
Query whether the next segment in the template is mapped.
"""
function isnextmapped(record::XAMRecord)::Bool
return flags(record) & FLAG_NEXT_UNMAPPED == 0
end
"""
isreverse(record::XAMRecord)::Bool
Query whether the `record.SEQ`uence is reverse complemented.
"""
function isreversecomplemented(record::XAMRecord)::Bool
return flags(record) & FLAG_REVERSE == FLAG_REVERSE
end
"""
isforward(record::XAMRecord)::Bool
Query whether the `record.SEQ`uence is mapped to the forward strand.
"""
function isforwardstrand(record::XAMRecord)::Bool
# return flags(record) & FLAG_REVERSE == 0
return !isreversecomplemented(record) # Note: this is an interpretation of FLAG_REVERSE.
end
"""
ispositivestrand(record::XAMRecord)::Bool
Query whether the `record.SEQ`uence is aligned to the positive strand.
"""
function ispositivestrand(record::XAMRecord)::Bool
return isforwardstrand(record)
end
"""
isreversestrand(record::XAMRecord)::Bool
Query whether the `record.SEQ`uence is aligned to the reverse strand.
"""
function isreversestrand(record::XAMRecord)::Bool
return isreversecomplemented(record) # Note: this is an interpretation of FLAG_REVERSE.
end
"""
ispositivestrand(record::XAMRecord)::Bool
Query whether the `record.SEQ`uence is aligned to the negative strand.
"""
function isnegativestrand(record::XAMRecord)::Bool
return isreversestrand(record)
end
"""
isnextreversecomplemented(record::XAMRecord)::Bool
Query whether the next segment in the template is reverse complemented.
"""
function isnextreversecomplemented(record::XAMRecord)::Bool
return flags(record) & FLAG_NEXT_REVERSE == FLAG_NEXT_REVERSE
end
"""
isfirstsegment(record::XAMRecord)::Bool
Query whether the segemnt is first in the template.
"""
function isfirstsegment(record::XAMRecord)::Bool
return flags(record) & FLAG_FIRST_SEGMENT == FLAG_FIRST_SEGMENT
end
"""
isread1(record::XAMRecord)::Bool
From a paired-end sequencing point of view, query whether the read is read1.
"""
function isread1(record::XAMRecord)::Bool
return isfirstsegment(record)
end
"""
islastsegment(record::XAMRecord)::Bool
Query whether the segemnt is last in the template.
"""
function islastsegment(record::XAMRecord)::Bool
return flags(record) & FLAG_LAST_SEGMENT == FLAG_LAST_SEGMENT
end
"""
isread2(record::XAMRecord)::Bool
From a paired-end sequencing point of view, query whether the read is read2.
"""
function isread2(record::XAMRecord)::Bool
return islastsegment(record)
end
"""
issecondaryalignment(record::XAMRecord)::Bool
Query whether the read is considered to be the secondary alignment.
"""
function issecondaryalignment(record::XAMRecord)::Bool
return flags(record) & FLAG_SECONDARY == FLAG_SECONDARY
end
"""
isqcfail(record::XAMRecord)::Bool
Query whether the read failed filters, such as platform/vendor quality controls.
"""
function isqcfail(record::XAMRecord)::Bool
return flags(record) & FLAG_QCFAIL == FLAG_QCFAIL
end
"""
isduplicate(record::XAMRecord)::Bool
Query whether the read is a PCR or optical duplicate.
"""
function isduplicate(record::XAMRecord)::Bool
return flags(record) & FLAG_DUPLICATE == FLAG_DUPLICATE
end
"""
issupplementaryalignment(record::XAMRecord)::Bool
Query whether the read alignment is considered to be a supplementary alignment.
"""
function issupplementaryalignment(record::XAMRecord)::Bool
return flags(record) & FLAG_SUPPLEMENTARY == FLAG_SUPPLEMENTARY
end
"""
isprimaryalignment(record::XAMRecord)::Bool
Query whether the read alignment is considered to be the primary alignment.
This is primary line of the read and is equivalent to `flags(record) & 0x900 == 0`.
"""
function isprimaryalignment(record::XAMRecord)::Bool
# return !issecondaryalignment(record) && !issupplementaryalignment(record)
return flags(record) & 0x900 == 0
end