In sam and bam files, flag is used to indicate the alignment state of the read, each flag has 11 positions when expressed as binary numbers, and each position indicates a specific attribute, which is shown below, 0x1
represents the first position, 0x400
represents the 11th position. At each position, 1 indicates the read has this attribute, 0 indicates the read lacks this attribute. For example, a flag 4 is 00000000100, indicates that this read is unmapped.
Bit Description
0x1
template having multiple segments in sequencing0x2
each segment properly aligned according to the aligner0x4
segment unmapped0x8
next segment in the template unmapped0x10
SEQ being reverse complemented0x20
SEQ of the next segment in the template being reversed0x40
the first segment in the template0x80
the last segment in the template0x100
secondary alignment0x200
not passing quality controls0x400
PCR or optical duplicates
samtools can select reads according to their flags.
-f INT Only output alignments with all bits set in INT present in the FLAG field. INT can be specified in hex by beginning with `0x` (i.e. `/^0x[0-9A-F]+/`) or in octal by beginning with `0` (i.e. /`^0[0-7]+/`) [0].
-F INT Do not output alignments with any bits set in INT present in the FLAG field. INT can be specified in hex by beginning with `0x` (i.e. `/^0x[0-9A-F]+/`) or in octal by beginning with `0` (i.e. `/^0[0-7]+/`) [0].
-G INT Do not output alignments with all bits set in INT present in the FLAG field. This is the opposite of -f such that -f12 -G12 is the same as no filtering at all. INT can be specified in hex by beginning with `0x` (i.e. `/^0x[0-9A-F]+/`) or in octal by beginning with `0` (i.e. `/^0[0-7]+/`) [0]
When you want to select reads with a specific flag, none of this option only will get what you want. You can get what you want by combining -f
and -F
. For example, if you want a flag 67, which is 00001000011, the complement flag is 11110111100, which is 1980. Then samtools view -f 67 -F 1980
will get reads with flag exactly 67.
For completion...some useful links:
Explain SAM flags
SAM flag idioms
Understamding MAPQ scores in SAM flags