Skip to content

Commit 9a9f2c1

Browse files
committed
Fun with flags
Implements flag queries.
1 parent 0d1eec3 commit 9a9f2c1

File tree

7 files changed

+233
-122
lines changed

7 files changed

+233
-122
lines changed

src/XAM.jl

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,17 @@
11
module XAM
22

33
using BioGenerics
4+
import BioGenerics: isfilled #Note: used by `ismapped`.
45

56
export
67
SAM,
78
BAM
8-
9+
910
abstract type XAMRecord end
1011
abstract type XAMReader <: BioGenerics.IO.AbstractReader end
1112
abstract type XAMWriter <: BioGenerics.IO.AbstractWriter end
1213

13-
"""
14-
flag(record::Union{SAM.Record, BAM.Record})::UInt16
15-
16-
Get the bitwise flags of `record`. The returned value is a `UInt16` of each flag
17-
being OR'd together. The possible flags are:
18-
19-
0x0001 template having multiple segments in sequencing
20-
0x0002 each segment properly aligned according to the aligner
21-
0x0004 segment unmapped
22-
0x0008 next segment in the template unmapped
23-
0x0010 SEQ being reverse complemented
24-
0x0020 SEQ of the next segment in the template being reverse complemented
25-
0x0040 the first segment in the template
26-
0x0080 the last segment in the template
27-
0x0100 secondary alignment
28-
0x0200 not passing filters, such as platform/vendor quality controls
29-
0x0400 PCR or optical duplicate
30-
0x0800 supplementary alignment
31-
"""
32-
function flag end
14+
include("flags.jl")
3315

3416
include("sam/sam.jl")
3517
include("bam/bam.jl")

src/bam/bam.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ module BAM
66
using BioGenerics
77
using GenomicFeatures
88
using XAM.SAM
9-
import ..XAM: flag, XAMRecord, XAMReader, XAMWriter
9+
import ..XAM: flag, XAMRecord, XAMReader, XAMWriter,
10+
ismapped, isprimary, ispositivestrand, isnextmapped #TODO: Deprecate import of flag queries. These were imported to preseve existing API.
1011

1112
import BGZFStreams
1213
import BioAlignments

src/bam/record.jl

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -137,37 +137,6 @@ function hasflag(record::Record)
137137
return isfilled(record)
138138
end
139139

140-
"""
141-
ismapped(record::Record)::Bool
142-
143-
Test if `record` is mapped.
144-
"""
145-
function ismapped(record::Record)::Bool
146-
return flag(record) & SAM.FLAG_UNMAP == 0
147-
end
148-
149-
"""
150-
isprimary(record::Record)::Bool
151-
152-
Test if `record` is a primary line of the read.
153-
154-
This is equivalent to `flag(record) & 0x900 == 0`.
155-
"""
156-
function isprimary(record::Record)::Bool
157-
return flag(record) & 0x900 == 0
158-
end
159-
160-
"""
161-
ispositivestrand(record::Record)::Bool
162-
163-
Test if `record` is aligned to the positive strand.
164-
165-
This is equivalent to `flag(record) & 0x10 == 0`.
166-
"""
167-
function ispositivestrand(record::Record)::Bool
168-
flag(record) & 0x10 == 0
169-
end
170-
171140
"""
172141
refid(record::Record)::Int
173142
@@ -253,15 +222,6 @@ function hasrightposition(record::Record)
253222
return isfilled(record) && ismapped(record)
254223
end
255224

256-
"""
257-
isnextmapped(record::Record)::Bool
258-
259-
Test if the mate/next read of `record` is mapped.
260-
"""
261-
function isnextmapped(record::Record)::Bool
262-
return isfilled(record) && (flag(record) & SAM.FLAG_MUNMAP == 0)
263-
end
264-
265225
"""
266226
nextrefid(record::Record)::Int
267227

src/flags.jl

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
# Flags
2+
# =========
3+
#
4+
5+
"""
6+
flag(record::XAMRecord})::UInt16
7+
8+
Get the bitwise flags of `record`.
9+
The returned value is a `UInt16` of each flag being OR'd together.
10+
The possible flags are:
11+
12+
0x0001 template having multiple segments in sequencing
13+
0x0002 each segment properly aligned according to the aligner
14+
0x0004 segment unmapped
15+
0x0008 next segment in the template unmapped
16+
0x0010 SEQ being reverse complemented
17+
0x0020 SEQ of the next segment in the template being reverse complemented
18+
0x0040 the first segment in the template
19+
0x0080 the last segment in the template
20+
0x0100 secondary alignment
21+
0x0200 not passing filters, such as platform/vendor quality controls
22+
0x0400 PCR or optical duplicate
23+
0x0800 supplementary alignment
24+
"""
25+
26+
function flag end
27+
28+
# Bitwise flags (or FLAG).
29+
for (name, bits, doc) in [
30+
(:PAIRED, UInt16(0x001), "the read is paired in sequencing, no matter whether it is mapped in a pair"),
31+
(:PROPER_PAIR, UInt16(0x002), "the read is mapped in a proper pair" ),
32+
(:UNMAP, UInt16(0x004), "the read itself is unmapped; conflictive with FLAG_PROPER_PAIR" ),
33+
(:MUNMAP, UInt16(0x008), "the mate is unmapped" ),
34+
(:REVERSE, UInt16(0x010), "the read is mapped to the reverse strand" ),
35+
(:MREVERSE, UInt16(0x020), "the mate is mapped to the reverse strand" ),
36+
(:READ1, UInt16(0x040), "this is read1" ),
37+
(:READ2, UInt16(0x080), "this is read2" ),
38+
(:SECONDARY, UInt16(0x100), "not primary alignment" ),
39+
(:QCFAIL, UInt16(0x200), "QC failure" ),
40+
(:DUP, UInt16(0x400), "optical or PCR duplicate" ),
41+
(:SUPPLEMENTARY, UInt16(0x800), "supplementary alignment" ),]
42+
@assert bits isa UInt16 "The bits must be of type UInt16."
43+
sym = Symbol("FLAG_", name)
44+
docstring = """ $sym
45+
SAM/BAM flag: $doc
46+
47+
See also: [`flag`](@ref)
48+
"""
49+
@eval begin
50+
@doc $(docstring) const $(sym) = $(bits)
51+
end
52+
end
53+
54+
"""
55+
ispaired(record::XAMRecord)::Bool
56+
57+
Query whether the `record`'s template has multiple segments in sequencing.
58+
"""
59+
function ispaired(record::XAMRecord)::Bool
60+
return flag(record) & FLAG_PAIRED == FLAG_PAIRED
61+
end
62+
63+
"""
64+
isproperpair(record::XAMRecord)::Bool
65+
66+
Query whether each segment of the `record`'s template properly aligned according to the aligner.
67+
"""
68+
function isproperpair(record::XAMRecord)::Bool
69+
return flag(record) & PROPER_PAIR == PROPER_PAIR
70+
end
71+
72+
"""
73+
isunmapped(record::XAMRecord)::Bool
74+
75+
Query whether the `record` is unmapped.
76+
"""
77+
function isunmapped(record::XAMRecord)::Bool
78+
return flag(record) & FLAG_UNMAP == FLAG_UNMAP
79+
end
80+
81+
"""
82+
ismapped(record::XAMRecord)::Bool
83+
84+
Query whether the `record` is mapped.
85+
"""
86+
function ismapped(record::XAMRecord)::Bool
87+
# return flag(record) & FLAG_UNMAP == 0
88+
return isfilled(record) && (flag(record) & FLAG_UNMAP == 0)
89+
end
90+
91+
"""
92+
ismateunmapped(record::XAMRecord)::Bool
93+
94+
Query whether the `record`'s mate is unmapped.
95+
"""
96+
function ismateunmapped(record::XAMRecord)::Bool
97+
return flag(record) & FLAG_MUNMAP == FLAG_MUNMAP
98+
end
99+
100+
"""
101+
isnextmapped(record::XAMRecord)::Bool
102+
103+
Test if the mate/next read of `record` is mapped.
104+
"""
105+
function isnextmapped(record::XAMRecord)::Bool
106+
return flag(record) & FLAG_MUNMAP == 0
107+
end
108+
109+
"""
110+
isreverse(record::XAMRecord)::Bool
111+
112+
Query whether the `record` is mapped to the reverse strand.
113+
"""
114+
function isreverse(record::XAMRecord)::Bool
115+
return flag(record) & FLAG_REVERSE == FLAG_REVERSE
116+
end
117+
118+
"""
119+
isforward(record::XAMRecord)::Bool
120+
121+
Query whether the `record` is mapped to the forward strand.
122+
"""
123+
function isforward(record::XAMRecord)::Bool
124+
return flag(record) & FLAG_REVERSE == 0
125+
end
126+
127+
"""
128+
ispositivestrand(record::XAMRecord)::Bool
129+
130+
Query whether `record` is aligned to the positive strand.
131+
"""
132+
function ispositivestrand(record::XAMRecord)::Bool
133+
return isforward(record)
134+
end
135+
136+
"""
137+
ispositivestrand(record::XAMRecord)::Bool
138+
139+
Query whether `record` is aligned to the negative strand.
140+
"""
141+
function isnegativestrand(record::XAMRecord)::Bool
142+
return isreverse(record)
143+
end
144+
145+
"""
146+
ismatereverse(record::XAMRecord)::Bool
147+
148+
Query whether the `record`'s mate is mapped to the reverse strand.
149+
"""
150+
function ismatereverse(record::XAMRecord)::Bool
151+
return flag(record) & FLAG_MREVERSE == FLAG_MREVERSE
152+
end
153+
154+
"""
155+
isread1(record::XAMRecord)::Bool
156+
157+
Query whether the `record` is read1.
158+
"""
159+
function isread1(record::XAMRecord)::Bool
160+
return flag(record) & FLAG_READ1 == FLAG_READ1
161+
end
162+
163+
"""
164+
isread2(record::XAMRecord)::Bool
165+
166+
Query whether the `record` is read2.
167+
"""
168+
function isread2(record::XAMRecord)::Bool
169+
return flag(record) & FLAG_READ2 == FLAG_READ2
170+
end
171+
172+
"""
173+
issecondaryalignment(record::XAMRecord)::Bool
174+
175+
Query whether the `record` is a secondary alignment.
176+
"""
177+
function issecondaryalignment(record::XAMRecord)::Bool
178+
return flag(record) & FLAG_SECONDARY == FLAG_SECONDARY
179+
end
180+
181+
"""
182+
isprimaryalignment(record::XAMRecord)::Bool
183+
184+
Query whether the `record` is the primary alignment.
185+
"""
186+
function isprimaryalignment(record::XAMRecord)::Bool
187+
return flag(record) & FLAG_SECONDARY == 0
188+
end
189+
190+
"""
191+
isqcfail(record::XAMRecord)::Bool
192+
193+
Query whether the `record` did not pass filters, such as platform/vendor quality controls.
194+
"""
195+
function isqcfail(record::XAMRecord)::Bool
196+
return flag(record) & FLAG_QCFAIL == FLAG_QCFAIL
197+
end
198+
199+
"""
200+
isduplicate(record::XAMRecord)::Bool
201+
202+
Query whether the `record` is a PCR or optical duplicate.
203+
"""
204+
function isduplicate(record::XAMRecord)::Bool
205+
return flag(record) & FLAG_DUP == FLAG_DUP
206+
end
207+
208+
"""
209+
issupplementaryalignment(record::XAMRecord)::Bool
210+
211+
Query whether the `record` is a supplementary alignment.
212+
"""
213+
function issupplementaryalignment(record::XAMRecord)::Bool
214+
return flag(record) & FLAG_SUPPLEMENTARY == FLAG_SUPPLEMENTARY
215+
end
216+
217+
"""
218+
isprimary(record::XAMRecord)::Bool
219+
220+
Query whether `record` is a primary line of the read.
221+
222+
This is equivalent to `flag(record) & 0x900 == 0`.
223+
"""
224+
function isprimary(record::XAMRecord)::Bool
225+
return flag(record) & 0x900 == 0
226+
end

src/sam/flags.jl

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)