|
| 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 |
0 commit comments