SAM Flags Explained: How to Decode
By Dr. Zubair Khalid, DVM, MS, PhD ·

A SAM flag is a 16-bit integer in the second column of a SAM alignment record that encodes up to 12 independent yes-or-no facts about a read, its mate, and its alignment. Decoding a flag means translating that integer into the set of bits it contains, so you can answer concrete questions such as whether the read is paired, mapped, reverse-complemented, a duplicate, or a secondary alignment.
Every alignment record in a SAM or BAM file carries this field, and nearly every downstream step in a sequencing pipeline depends on reading it correctly. Variant callers, expression quantifiers, duplicate markers, and quality-control summaries all use flags to decide which records to keep, which to skip, and which to count separately. A filter written as -F 4 instead of -f 4 inverts the meaning of an entire analysis. A script that treats 99 as a quality score or a percentage produces nonsense. Tools that process SAM and BAM files must manipulate large amounts of data, and memory management is a central engineering challenge for that software [1]. Knowing exactly what each bit means is the difference between a reproducible pipeline and a silent bug.
What the SAM flag field actually is
The Sequence Alignment/Map (SAM) format is a tab-delimited text format for storing read alignments against a reference. Each alignment occupies one line and contains 11 mandatory fields followed by optional tags. The fields are query name, flag, reference name, 1-based position, mapping quality, CIGAR string, mate reference name, mate position, observed template length, sequence, and base qualities. BAM is the binary, compressed representation of the same information. CRAM is a further compressed format that stores reads relative to a reference. All three carry the same flag semantics.
The second field, the flag, is not a count, a score, or an identifier. It is a bit field. In BAM the value is stored as a 16-bit integer. In SAM text it is written as a decimal integer. Each bit position represents one property, and the properties are independent. A value of 4 means that the read is unmapped. A value of 16 means that the read aligns to the reverse strand. A value of 20 means both, because 4 + 16 = 20.
Because the bits are powers of two, any combination of properties has exactly one integer representation. That is the whole design. You can reconstruct the full set of true statements by subtracting the largest bit values that fit, or by testing each bit with a bitwise AND. The SAM specification defines 12 bits, numbered 0 through 11, with hexadecimal values from 0x1 to 0x800. The remaining four bits in the 16-bit field are reserved and should not be used by producers.
SAM and BAM files are the interchange format that aligners write and that downstream tools read, and processing them at scale is a memory-intensive job [1]. The flag field is small, but it is read billions of times in a large study. Getting its interpretation right at the start avoids expensive reruns later.
Why the flag is a bit field and not a list
A bit field packs many independent booleans into one integer. If the format stored each property as a separate text column, every SAM record would need 12 extra columns, and most would be empty or zero. The bit field keeps the record compact and makes filtering cheap. A tool can test one bit without parsing a list of labels.
The cost is that the integer is unreadable at a glance. A value of 353 is not obviously "paired, proper pair, reverse, read1, secondary" unless you do the arithmetic. That is why decoding tables and command-line filters exist. The format assumes that software will do the bit tests and that humans will use tools to inspect the results.
Binary representation
It helps to see a flag in binary. The bit positions from right to left correspond to 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, and 2048. The value 99 in binary is 01100011. Reading from the right, the set positions are 1, 2, 32, and 64, which sum to 99. The leading zero is the 128 bit, which is not set. The two leading zeros are the 256 and 512 bits, which are also not set.
The value 147 in binary is 10010011. The set positions are 1, 2, 16, and 128, which sum to 147. The value 4 is 00000100. The value 16 is 00010000. The value 2048 is 100000000000, which is bit 11.
Once you see flags as binary, the arithmetic becomes obvious. Adding a bit is the same as turning on a switch. Removing a bit is the same as turning it off. Combining two flags with a bitwise OR produces a record that has both sets of properties.
How to decode a SAM flag step by step
Decoding is mechanical once you know the bit table. Use the same procedure every time.
- Write down the flag value from column 2.
- Compare it with the bit table in descending order of value.
- If the flag is greater than or equal to a bit value, that property is present. Subtract the bit value.
- Repeat with the remainder until it reaches zero.
- Read the resulting list of properties as a sentence about the read.
- Check the combination against the record's other fields, especially RNAME, POS, RNEXT, and PNEXT.
The bitwise method is faster in code. For a flag f and a bit b, the expression f & b is nonzero when the bit is set. In Python, flag & 0x4 is true for unmapped reads. In Bash arithmetic, $((flag & 4)) gives 4 for unmapped reads and 0 for mapped reads. In awk with GNU awk, and(flag, 4) works the same way.
Worked example: flag 99
99 is 64 + 32 + 2 + 1. The bits are READ1 (64), MREVERSE (32), PROPER_PAIR (2), and PAIRED (1). The record describes the first read of a paired template, with a mate that aligns to the reverse strand, and the pair is properly aligned according to the aligner. The read itself can be on either strand, and in the common case of an Illumina pair mapped in the usual orientation, it is on the forward strand.
Worked example: flag 147
147 is 128 + 16 + 2 + 1. The bits are READ2 (128), REVERSE (16), PROPER_PAIR (2), and PAIRED (1). This is the mate of the read above. It is the second read in the template, it aligns to the reverse strand, and the pair is properly aligned. Flags 99 and 147 are the most frequently cited pair in paired-end Illumina data, but they are not the only valid pair.
Worked example: flag 83
83 is 64 + 16 + 2 + 1. The bits are READ1 (64), REVERSE (16), PROPER_PAIR (2), and PAIRED (1). This is the first read of a pair, aligned to the reverse strand, with a properly aligned mate. Its partner is often 163, which is 128 + 32 + 2 + 1 and describes READ2 with MREVERSE. The 83/163 pair appears when the fragment orientation is reversed relative to the 99/147 case.
Worked example: flag 0
0 has no bits set. The read is not paired, not proper-paired, not unmapped, not reverse, not a duplicate, and not secondary or supplementary. In a single-end experiment, flag 0 usually means a mapped forward-strand read with no mate information. A flag of 0 is valid, not an error.
Worked example: flag 4 and flag 16
4 is UNMAP alone. The read is unmapped. 16 is REVERSE alone. The read is mapped to the reverse strand and is not paired. A flag of 20 combines both, meaning an unmapped read that is also flagged as reverse. For unmapped reads, the reverse bit can be set because the original read orientation is still known, even though there is no alignment.
Worked example: flags 1024, 256, and 2048
1024 is DUP. The record is marked as a PCR or optical duplicate. 256 is SECONDARY. The record is an alternative alignment of a read that already has a primary alignment elsewhere. 2048 is SUPPLEMENTARY. The record is part of a chimeric or split alignment, where one read has multiple alignment segments that together cover the full read. These three flags are often confused with each other, but they answer different questions.
Worked example: flag 369
369 is 256 + 64 + 32 + 16 + 1. The bits are SECONDARY (256), READ1 (64), MREVERSE (32), REVERSE (16), and PAIRED (1). The record is a secondary alignment of the first read in a pair, the read is on the reverse strand, and the mate is also on the reverse strand. This is a plausible multimapping record in a paired-end experiment.
A compact decoding table
| Flag | Bits present | Meaning |
|---|---|---|
| 0 | none | mapped, unpaired, forward, primary |
| 4 | UNMAP | read is unmapped |
| 16 | REVERSE | mapped to reverse strand |
| 99 | PAIRED, PROPER_PAIR, MREVERSE, READ1 | first read of a proper pair, mate reverse |
| 147 | PAIRED, PROPER_PAIR, REVERSE, READ2 | second read of a proper pair, read reverse |
| 83 | PAIRED, PROPER_PAIR, REVERSE, READ1 | first read of a proper pair, read reverse |
| 163 | PAIRED, PROPER_PAIR, MREVERSE, READ2 | second read of a proper pair, mate reverse |
| 69 | PAIRED, UNMAP, READ1 | first read of a pair, unmapped |
| 137 | PAIRED, MUNMAP, READ2 | second read of a pair, mate unmapped |
| 73 | PAIRED, MUNMAP, READ1 | first read of a pair, mate unmapped |
| 133 | PAIRED, UNMAP, READ2 | second read of a pair, unmapped |
| 256 | SECONDARY | secondary alignment |
| 384 | SECONDARY, READ2 | secondary alignment of the second read |
| 1024 | DUP | duplicate |
| 2048 | SUPPLEMENTARY | supplementary alignment |
| 369 | PAIRED, REVERSE, MREVERSE, READ1, SECONDARY | secondary alignment of a paired first read |
| 4095 | all 12 defined bits | every defined property is true, usually a sign of a synthetic or malformed record |
The table is a reference, not a substitute for reading the bits. Any integer from 0 to 4095 can appear in a valid file, though many combinations are biologically unlikely.
SAM flags explained: the 12 defined bits in detail
Pairing bits: 0x1, 0x2, 0x40, 0x80
PAIRED (0x1, 1) means the read is part of a template with more than one segment. In practice this means paired-end or mate-pair sequencing. A record with PAIRED set should also have exactly one of READ1 or READ2 set. If both are set or neither is set, the file is malformed or the aligner wrote something unusual.
PROPER_PAIR (0x2, 2) means the aligner judged that the read and its mate align to the same reference in the expected orientation and within the expected insert size. The exact definition of proper is aligner-specific and parameter-dependent. BWA, Bowtie 2, and STAR can disagree about which pairs are proper because they use different insert-size models and orientation rules. The bit does not mean that both reads are mapped. It means the aligner considered the pair proper. A properly paired read is mapped by definition, because an unmapped read cannot satisfy the aligner's pairing model.
READ1 (0x40, 64) and READ2 (0x80, 128) identify the first and second segment of the template. They are set by the aligner based on the order of the reads in the input FASTQ files. If you swap the two FASTQ files in a paired-end run, READ1 and READ2 swap with them, and downstream strand-specific analysis can break.
Mate bits: 0x8 and 0x20
MUNMAP (0x8, 8) means the next segment in the template is unmapped. It describes the mate, not the current read. A read can be mapped while its mate is unmapped, in which case the read has MUNMAP set and the mate has UNMAP set. These are the singleton reads that many pipelines separate for special handling.
MREVERSE (0x20, 32) means the mate aligns to the reverse strand. Like MUNMAP, it describes the other read. If the current read is READ1, MREVERSE tells you the orientation of READ2.
The mate bits are only meaningful when PAIRED is set. In a single-end library, mate bits should be zero, and tools may ignore them if they are not.
Mapping and strand bits: 0x4 and 0x10
UNMAP (0x4, 4) means the read itself is unmapped. An unmapped record can still have a reference name and position. In SAM, if the mate is mapped, the unmapped read often carries the mate's reference name and position so that the pair stays together in coordinate-sorted files. The UNMAP bit is the authoritative statement about the read, not the presence or absence of RNAME and POS.
REVERSE (0x10, 16) means the read's sequence is the reverse complement of the original read. The SEQ field in SAM is stored in the orientation that aligns to the reference, so a reverse-strand read has SEQ already reverse-complemented relative to the instrument output. The QUAL field is reversed to match. This bit is essential for strand-specific assays, such as RNA-seq libraries that preserve strand information.
Alignment status bits: 0x100, 0x200, 0x400, 0x800
SECONDARY (0x100, 256) marks an alternative alignment of a read that has a primary alignment elsewhere. A read that maps equally well to two places may have one primary record and one or more secondary records. Secondary records are usually excluded from variant calling and quantification because counting them double-counts the read. They are useful for detecting multimapping and repeats.
QCFAIL (0x200, 512) means the read did not pass vendor or platform quality filters. It is not a statement about alignment quality or mapping confidence. Reads flagged QCFAIL are often dropped before alignment, but if they survive, the flag lets you exclude them later.
DUP (0x400, 1024) means the record is a PCR or optical duplicate. Duplicate marking is a separate step, usually performed with Picard MarkDuplicates, sambamba markdup, or a similar tool. The aligner does not set this bit. A duplicate read can be perfectly mapped and biologically real, so the flag is a warning about library preparation, not proof of an artifact.
SUPPLEMENTARY (0x800, 2048) marks a supplementary alignment, which is a segment of a chimeric or split read. When a read spans a structural variant, a fusion, or a large deletion, the aligner may split it into multiple records. One record is primary, and the others are supplementary. Supplementary records are not alternative locations for the same bases. They are different parts of the same read.
The difference between secondary and supplementary is one of the most common sources of confusion. Secondary means "this read could also align here." Supplementary means "this read aligns here and also there, as one continuous molecule." Both are non-primary, and both are excluded by -F 2304, but they answer different biological questions.
How aligners set flags
Most flags are written by the aligner at the moment it creates an alignment record. The aligner knows whether the input was paired, which file was read1, which was read2, where the read mapped, and whether the mate mapped. It sets PAIRED, READ1, READ2, UNMAP, MUNMAP, REVERSE, and MREVERSE directly from that information.
PROPER_PAIR is more subjective. The aligner compares the observed insert size and orientation with its model for the library. If the pair falls within the expected range and orientation, the aligner sets PROPER_PAIR. If the insert size is outside the expected range, the pair may still be mapped and paired, but it will not be proper. This is why a BAM can contain many mapped pairs with a low proper-pair rate. The reads are fine, but the library or the alignment parameters do not fit the model.
SECONDARY is set when the aligner reports more than one alignment for a read. Some aligners report only the best alignment by default, so a BAM with no secondary records may simply be a BAM where multimapping was suppressed. SUPPLEMENTARY is set when the aligner splits a read into multiple alignment segments. BWA-MEM, STAR, and minimap2 are common sources of supplementary records.
DUP is not set by the aligner. It is set by a duplicate-marking tool that scans the coordinate-sorted BAM and flags reads with identical start and end positions. QCFAIL is usually set by the instrument or the primary analysis software, not by the aligner.
Filtering alignments with flags
The most common use of flags is filtering. The samtools view command has two flag-related options. -f INT includes a record only if all bits in INT are set. -F INT excludes a record if any bit in INT is set. The two options are not opposites in a simple sense. -f 4 selects unmapped reads. -F 4 selects everything that is not unmapped. -f 1 selects paired reads. -F 1 selects unpaired reads.
| Goal | Command |
|---|---|
| Mapped reads only | samtools view -F 4 aligned.bam |
| Unmapped reads only | samtools view -f 4 aligned.bam |
| Properly paired reads only | samtools view -f 2 aligned.bam |
| Paired reads only | samtools view -f 1 aligned.bam |
| First read of each pair | samtools view -f 64 aligned.bam |
| Second read of each pair | samtools view -f 128 aligned.bam |
| Exclude duplicates | samtools view -F 1024 aligned.bam |
| Exclude secondary and supplementary | samtools view -F 2304 aligned.bam |
| Primary alignments, no duplicates | samtools view -F 3328 aligned.bam |
| Count mapped reads | samtools view -c -F 4 aligned.bam |
| Count unmapped reads | samtools view -c -f 4 aligned.bam |
| Count duplicates | samtools view -c -f 1024 aligned.bam |
The value 2304 is 256 + 2048. The value 3328 is 256 + 2048 + 1024. You can combine any bits this way as long as you use addition, not concatenation. -F 256 -F 2048 may not behave as expected in every tool version, so a single combined decimal value is safer.
For paired-end reads, filtering on one bit often leaves the mate behind. If you select READ1 with -f 64, you get only the first reads. If you want both reads of every pair where the first read is mapped, you need a two-step approach or a tool that understands templates. samtools view -f 64 -F 4 selects mapped first reads, but it does not automatically bring in their mates. For template-level filtering, use samtools view -f 2 to keep proper pairs, or use a tool such as samtools fastq with the appropriate options, or filter with a script that tracks query names.
elPrep is an example of a multithreaded framework built specifically to process SAM and BAM files efficiently in next-generation sequencing pipelines [1]. Whether you use elPrep, samtools, sambamba, or Picard, the flag semantics are the same because they come from the SAM specification.
Filter recipes for common workflows
For germline variant calling, a common starting point is to exclude unmapped reads, secondary alignments, supplementary alignments, duplicates, and reads that fail vendor quality checks. In flag terms, that is -F 3860, which is 4 + 256 + 2048 + 1024 + 512. Many pipelines also apply a mapping-quality threshold separately because MAPQ is not a flag.
For RNA-seq quantification, the primary concern is to count each read once at its best alignment. Excluding secondary and supplementary records with -F 2304 is standard. Duplicate handling depends on the protocol. Some RNA-seq pipelines mark duplicates and keep them, while others remove them. Read1 and read2 are often counted separately for stranded libraries.
For ChIP-seq peak calling, duplicates are usually removed, and unmapped or low-quality reads are excluded. Proper-pair filtering is optional because the signal comes from fragment coverage rather than from pair orientation. The exact recipe depends on the peak caller and the library.
For structural variant calling, supplementary alignments are often retained because they carry the split-read evidence. Excluding all non-primary records with -F 2304 would remove exactly the evidence the caller needs. This is a case where understanding the bits matters more than following a default filter.
Common flag combinations and what they tell you
A proper pair in the usual Illumina orientation is 99 and 147. The first read is forward, the second is reverse, and the pair is proper. A pair in the reverse orientation is 83 and 163. The first read is reverse, the second is forward, and the pair is proper. These four values account for a large fraction of reads in a typical paired-end BAM.
A singleton is a mapped read whose mate is unmapped. The mapped read carries MUNMAP, and the unmapped mate carries UNMAP. For a first read that is unmapped and a second read that is mapped, the pair might appear as 69 (first read unmapped) and 137 (second read mapped with mate unmapped). If the first read is mapped and the second is unmapped, the values are 73 and 133.
A secondary alignment of a paired first read can be 369, which is 256 + 64 + 32 + 16 + 1. It describes a secondary alignment of READ1, paired, reverse, with a mate on the reverse strand. A secondary alignment of READ2 can be 384, which is 256 + 128. These values appear in multimapping data and should be excluded from most counts.
A duplicate proper pair can be 99 + 1024 = 1123 and 147 + 1024 = 1171. The duplicate bit is added to the existing flag, so the underlying pairing and mapping information is preserved. This is why you should never test a flag for equality with a single value unless you are sure no other bits are set.
A supplementary alignment often has a high but not maximal flag value because it inherits the pairing and strand bits of the primary record. A supplementary record for a reverse-strand read might be 2048 + 16 = 2064, or 2048 + 16 + 1 + 64 = 2129 if it is paired and READ1. The supplementary bit is a modifier, not a replacement.
How SAM flags fit into bioinformatics file formats
Flags exist only in alignment formats. They do not exist in FASTA or FASTQ.
| Format | Content | Flag field |
|---|---|---|
| FASTA | sequence with a text header, no quality scores | no |
| FASTQ | sequence with a text header, plus per-base quality scores | no |
| SAM | tab-delimited alignments with 11 mandatory fields | yes, column 2 |
| BAM | binary compressed SAM | yes, column 2 in the binary record |
| CRAM | reference-based compressed alignments | yes, same flag semantics |
FASTA is the format for reference genomes, transcripts, and protein sequences. A FASTA record starts with a greater-than sign, followed by an identifier and optional description, then one or more lines of sequence. There is no quality score and no alignment. FASTQ is the format for raw sequencing reads. A FASTQ record has four lines: an at-sign header, the sequence, a plus-sign separator, and a quality string with one character per base. The quality characters encode Phred scores, typically with an ASCII offset of 33, so the character I represents a Phred score of 40.
A read begins as a FASTQ record. The sequencer writes the bases and the per-base quality scores. An aligner reads the FASTQ, compares each read with a reference FASTA, and writes a SAM or BAM record. The flag is created at that step. It summarizes what the aligner decided about pairing, strand, mapping status, and alignment type. A FASTQ file cannot tell you whether a read is a duplicate or a secondary alignment because those concepts require an alignment.
The FASTQ versus FASTA distinction is a common source of beginner confusion. FASTA is sequence only. FASTQ is sequence plus quality. SAM and BAM add alignment. The flag belongs to the alignment layer, not the raw read layer.
SAM/BAM tools in next-generation sequencing pipelines handle files large enough that memory management becomes a central design constraint [1]. A single human whole-genome BAM can contain hundreds of millions of records. Counting flags by loading the entire file into memory is impractical. Streaming tools read one record at a time and maintain only counters, which is why samtools view -c and samtools flagstat are preferred over custom parsers for routine work.
Reading flags in code
The bitwise operation is simple enough that you can decode flags in a few lines of Python. The key is to test each bit with a bitwise AND and print the names of the bits that are set.
flag = 99
bits = [
("PAIRED", 0x1),
("PROPER_PAIR", 0x2),
("UNMAP", 0x4),
("MUNMAP", 0x8),
("REVERSE", 0x10),
("MREVERSE", 0x20),
("READ1", 0x40),
("READ2", 0x80),
("SECONDARY", 0x100),
("QCFAIL", 0x200),
("DUP", 0x400),
("SUPPLEMENTARY", 0x800),
]
for name, bit in bits:
if flag & bit:
print(name)
For 99, the output is PAIRED, PROPER_PAIR, MREVERSE, and READ1. The order depends on the list, not on the flag value.
In awk, the same test uses the and() function in GNU awk. In Perl, the ampersand operator works on integers. In R, the bitwAnd() function does the same. The syntax changes, but the logic does not.
The same bitwise test is what samtools does internally when you pass -f or -F. The command-line options are a convenience wrapper around the integer comparison.
How flags are tested and observed in practice
The first check on any BAM is samtools flagstat. It prints counts for total records, primary records, secondary records, supplementary records, duplicates, mapped records, paired records, properly paired records, singletons, and records with mates on different chromosomes. The exact labels vary slightly by samtools version, but the categories are stable. A typical output starts with a total count and then breaks it down.
A healthy paired-end alignment often shows a high percentage of properly paired reads, a small percentage of singletons, and a duplicate rate that depends on library complexity and sequencing depth. A properly paired count that exceeds the paired count is impossible and indicates a malformed file or a misunderstanding of the labels. A mapped count that exceeds the total count is also impossible.
The second check is a count with an explicit flag filter. samtools view -c -F 4 aligned.bam counts mapped records. samtools view -c -f 4 aligned.bam counts unmapped records. The two counts should sum to the total number of records, assuming no other filters. If they do not, check whether the file contains records with both bits set, which would be unusual but possible in a malformed file.
The third check is to inspect individual records. samtools view aligned.bam | head prints the first few alignments. The second column is the flag. You can decode it by hand and compare it with the RNAME, POS, and CIGAR fields. For a properly paired read, the mate reference and mate position should be consistent with the pair. For an unmapped read, the CIGAR is usually *, and the sequence may still be present.
The fourth check is to verify that filtering does what you think. If you run samtools view -f 2 aligned.bam | wc -l and samtools view -c -f 2 aligned.bam, the two numbers should match. If they do not, you may be counting header lines or using a different filter. samtools view -c counts alignments, not header lines, so it is the safer choice.
Because SAM/BAM files are large, memory management is a central concern for any tool that counts or filters them [1]. Streaming tools are not just faster. They are the only practical option when the file is larger than the available RAM.
Validation and sanity checks
Picard ValidateSamFile checks a BAM for structural problems, including invalid flag combinations, missing read groups, and inconsistent mate fields. It is a useful first pass before downstream analysis. Some errors are warnings, and some are fatal, depending on the validation mode.
A simple sanity check is to look for reads with both READ1 and READ2 set. A well-formed paired-end file should not contain them. Another is to look for reads with UNMAP set but a non-star CIGAR. That combination can occur in some pipelines, but it deserves a look.
A third check is to compare the number of primary records with the number of query names. If a BAM has more primary records than distinct query names, something is wrong with the pairing or the primary flag. If it has fewer, some reads may be missing.
Common mistakes and limitations
The first mistake is confusing -f and -F. -f 4 selects unmapped reads. -F 4 selects mapped reads. Reversing them silently changes an analysis from "mapped reads only" to "unmapped reads only." The counts may still look plausible, which makes the error hard to catch.
The second mistake is treating PROPER_PAIR as a statement that both reads are mapped. The bit means the aligner considered the pair proper. A read with PROPER_PAIR is mapped, but the bit does not independently guarantee that the mate is mapped in every edge case. Check MUNMAP and the mate fields if you need that assurance.
The third mistake is treating DUP as proof that a read is bad. Duplicate marking flags reads that share the same start and end coordinates, which is evidence of PCR amplification or optical duplication. Some duplicates are real fragments that happen to share coordinates. The flag is a filter heuristic, not a biological verdict.
The fourth mistake is ignoring secondary and supplementary alignments when counting. A naive count of records in a BAM overcounts reads, because one read can produce a primary record, one or more secondary records, and multiple supplementary records. Use -F 2304 when you want one record per read, and use the primary-only counts in samtools flagstat when you want a summary.
The fifth mistake is assuming that an unmapped read has no coordinates. In a coordinate-sorted BAM, an unmapped read with a mapped mate is often placed at the mate's position so the pair stays together. The UNMAP bit is the authoritative statement, not the position.
The sixth mistake is confusing QCFAIL with a low mapping quality. QCFAIL refers to vendor or platform quality filters. MAPQ is a separate field that estimates alignment confidence. A read can have QCFAIL unset and MAPQ 0, or QCFAIL set and a high MAPQ.
The seventh mistake is hardcoding 99 and 147 as the only proper-pair flags. The 83 and 163 pair is equally valid, and so are many other combinations in different library types. Always decode the bits instead of matching a short list of known values.
The eighth mistake is parsing flags with string operations. A flag is an integer. flag & 4 is a bitwise test. "4" in str(flag) is a string search that gives wrong answers for values such as 14, 40, and 2048.
Large alignment files make ad hoc parsing risky, and dedicated SAM/BAM tools exist partly to manage that scale [1]. A custom parser that works on a small test file can fail on a production BAM because of memory, sorting order, or malformed records.
A limitation of flags is that they are per-record, not per-template. To reason about a pair, you need both records. A flag on one read can tell you about the mate, but the mate's own flag is the authoritative statement about the mate. Tools that filter at the template level must join the two records by query name or use the mate fields.
Another limitation is that flags are not self-validating. A file can contain contradictory flags, such as READ1 and READ2 both set, or UNMAP set with a valid CIGAR. Some tools tolerate these records, and others reject them. Validation tools such as Picard ValidateSamFile and samtools quickcheck can catch structural problems, but they do not resolve every semantic inconsistency.
A third limitation is that the meaning of PROPER_PAIR depends on the aligner and its parameters. Two pipelines can produce different proper-pair rates from the same FASTQ files because they use different insert-size models. The flag is not a universal truth about the library. It is a record of what one aligner decided.
Quick Review
- A SAM flag is a bit field in column 2, not a count or a score.
- The 12 defined bits are PAIRED, PROPER_PAIR, UNMAP, MUNMAP, REVERSE, MREVERSE, READ1, READ2, SECONDARY, QCFAIL, DUP, and SUPPLEMENTARY.
- Decode by subtracting bit values or by testing with bitwise AND.
samtools view -frequires all specified bits, and-Fexcludes records with any specified bit.- 99 and 147 are common proper-pair flags, but 83 and 163 are also common and valid.
- Secondary alignments are alternative locations. Supplementary alignments are split segments of one read.
- FASTA and FASTQ have no flag field. Flags appear only after alignment in SAM, BAM, or CRAM.
Frequently Asked Questions
What does a SAM flag of 4 mean?
A SAM flag of 4 means the read is unmapped. The UNMAP bit is set alone, so the record has no alignment to the reference even if it carries a reference name and position inherited from its mate.
What is the difference between flag 99 and flag 147?
Flag 99 marks the first read of a properly paired template with a reverse-strand mate. Flag 147 marks the second read of the same kind of template, and the read itself is on the reverse strand. They are mates in the common Illumina orientation.
Why do my counts change when I use -f instead of -F in samtools?
-f includes records that have all the specified bits set. -F excludes records that have any of the specified bits set. The two options select different sets, and swapping them can invert a filter.
Is a duplicate read always an artifact?
No. The DUP flag marks reads that share coordinates with another read, which is evidence of PCR or optical duplication. Some flagged reads are genuine fragments that happen to share coordinates, so duplicate filtering is a heuristic rather than a biological conclusion.
What is the difference between a secondary and a supplementary alignment?
A secondary alignment is an alternative location for the same read. A supplementary alignment is a different segment of a chimeric or split read. Both are non-primary, but they describe different alignment events.
Can a SAM flag be 0?
Yes. A flag of 0 means no defined bits are set. In a single-end experiment, it usually describes a mapped forward-strand read with no mate, no duplicate marking, and no secondary or supplementary status.