eduKateSG · Why Science?
Store and retrieve millions of alignment records efficiently—while preserving the header, sort order, index and scientific meaning
Choose the closest route, then return to the full index whenever you need the wider evidence chain.
Science learning becomes powerful when students can tell a measured pattern from a model, a statistical decision from a mechanism, and a useful lead from a finished conclusion. BAM is the binary equivalent of SAM. It preserves the SAM header and alignment information in a compact binary representation, normally stored in BGZF blocks. The official specification says BGZF is block compression built on gzip, designed to combine useful compression with efficient random access. Coordinate-sorted BAM can be indexed with BAI or CSI so software retrieves alignments overlapping a region without scanning the whole file. Faster access does not prove that records are correctly aligned, the right reference was used or the file is complete. This guide supports Primary Science, PSLE Science, Secondary Science, O-Level Science and STEM education as a longform evidence-reading route; it is not medical advice, a laboratory protocol, an admission promise or a career guarantee.
Related eduKate reading: CRAM and reference-based genomic compression; htsget and secure genomic data streaming; GA4GH Data Repository Service and portable data access; Science Learning Hub; Education Hub; STEM Education; Careers by Subject Capability.
Primary and official sources checked on 12 October 2026: Official SAM/BAM Format Specification Working Group specification, version 1.6, 12 August 2025; Official SAM/BAM and related specifications repository; Official CSI index specification; 2026 Singapore–Cambridge O-Level Biology syllabus; MOE G2/G3 Lower Secondary Science teaching and learning syllabus; 2026 MOE G2 Computing syllabus. Standards, binary encodings and recommendations can change, so preserve the dated documentation, reference and software versions used.
A deeper reading route
BAM solves an engineering problem created by scale. Human-readable SAM is excellent for inspection, yet millions of tab-delimited records are expensive to store and repeatedly parse. BAM encodes the same logical alignment model in binary fields. Tools can decode it quickly, while researchers can convert a small region back to SAM for inspection. Compression changes representation, not the biological claim carried by each record.
BGZF provides the bridge between compression and random access. It is a series of individually bounded gzip-compatible blocks. A virtual offset combines the compressed-file offset of a block with an offset inside the decompressed block. An index can therefore point software near relevant records without expanding the whole archive. The design is clever because it treats file position as structured information rather than a simple byte number.
Regional retrieval requires ordering. The specification states that BAM must be sorted by reference identifier and leftmost coordinate before indexing. BAI uses binning and a linear index; CSI is a later, more general index format. An index is meaningful only for the exact BAM bytes and ordering from which it was built. Moving a stale .bai beside a rewritten BAM creates a plausible-looking but unsafe pair.
The binary record packs reference IDs, positions, names, CIGAR operations, sequence, qualities and optional data into typed fields. Reference IDs are offsets into the header dictionary rather than standalone chromosome meanings. Corrupt or mismatched headers therefore affect every record. A validator should inspect both structural integrity and the semantic link between numeric IDs and reference metadata.
BGZF files conventionally end with a defined empty marker that helps detect unintended truncation. The specification says its absence should produce a warning or error where random access is available. The marker is useful but not a cryptographic guarantee. Checksums, object sizes, transfer verification and record-level tests remain necessary when BAM travels between systems.
For students, BAM illustrates a joyful idea in computing: a careful representation can make a giant scientific library searchable without changing the story in each record. The lesson also carries a warning. Efficiency, integrity and scientific validity are different layers, and a trustworthy workflow tests all three.
A practical BAM workflow also distinguishes file-level summaries from region-level queries. Whole-file statistics can reveal totals, reference coverage and unmapped records, while an indexed query retrieves only alignments overlapping a requested interval. Comparing the two views catches mistakes that a fast regional result can hide, especially missing references, stale indexes and unexpectedly excluded unmapped data.
| Stage | Question | Evidence | Responsible output |
|---|---|---|---|
| Encode | How are SAM fields represented efficiently? | Binary header and typed alignment records | Compact alignment storage |
| Compress | How can storage remain seekable? | BGZF blocks and virtual offsets | Block-addressable compressed bytes |
| Index | How can one genomic region be found? | Coordinate order plus BAI or CSI | A regional retrieval map |
| Validate | Is the file complete and meaningful? | Header, EOF, checksums, index and known fixtures | A trustworthy storage object |
Inside this guide
1–12 · Foundations and design
- 1. Start with the storage question
- 2. Use the current specification
- 3. Treat BAM as SAM-equivalent
- 4. Keep the BAM header
- 5. Understand little-endian encoding
- 6. Read the magic and header length
- 7. Map numeric reference IDs
- 8. Understand packed alignment records
- 9. Decode packed sequence
- 10. Decode CIGAR operations
- 11. Decode qualities correctly
- 12. Decode optional tags by type
13–24 · Models and estimation
- 13. Understand BGZF
- 14. Use virtual offsets
- 15. Check block size boundaries
- 16. Understand the EOF marker
- 17. Do not confuse EOF with authenticity
- 18. Sort before indexing
- 19. Understand BAI
- 20. Understand CSI
- 21. Query overlaps, then filter
- 22. Reject stale indexes
- 23. Preserve unmapped tails
- 24. Stream and seek deliberately
25–36 · Diagnostics and interpretation
- 25. Compare BAM with CRAM
- 26. Protect sample information
- 27. Validate conversion fidelity
- 28. Connect to Primary Science
- 29. Connect to Secondary Science
- 30. Connect to mathematics
- 31. Connect to computing
- 32. Connect to pathways
- 33. Did you know?
- 34. Write a reader-ready storage report
- 35. Build a validation ladder
- 36. Finish with a reproducible bundle
Section 1 of 36
1. Start with the storage question
BAM is chosen when alignment data need compact storage, efficient processing or regional retrieval. The scientific owner remains the alignment model and reference context.
Working checkpoint — BAM: Write why binary storage is needed and which record semantics must remain unchanged. Keep BAM and index hashes, header, reference, sorting method, BGZF validation and software versions together.
For a classroom model, index fictional cards in small compressed packets.
Section 2 of 36
2. Use the current specification
The official SAM/BAM document identifies format version 1.6 and covers SAM, BAM, BGZF and BAI. CSI is documented separately.
Working checkpoint — binary alignment: Pin the specifications and tool versions used. Read retrieval speed beside record counts, reference identity, region boundaries and known control results.
For future study, connect compression to genomics, mathematics and systems engineering while checking official pathways separately.
Section 3 of 36
3. Treat BAM as SAM-equivalent
BAM is the binary equivalent of SAM, not a different experiment or stronger form of evidence. Conversion should preserve representable fields and header meaning.
Working checkpoint — BGZF: Compare decoded records before and after a round trip. Preserve unmapped tails, rejected records and failed regions; an indexed query is not the whole file.
For scientific writing, report file and index identity, reference, sort order and retrieval method.
Section 4 of 36
4. Keep the BAM header
The BAM header contains textual SAM header data and a binary reference list. Both must agree because numeric reference IDs in records depend on that dictionary.
Working checkpoint — virtual offset: Validate names, lengths and dictionary order. Separate compact storage and successful random access from alignment correctness and biological validity.
For students, ask what the codebook and index let a computer find without changing record meaning.
Section 5 of 36
5. Understand little-endian encoding
BAM stores multibyte integer values in little-endian form. A custom reader that assumes host order can silently misread positions and lengths.
Working checkpoint — BAI index: Use mature libraries or explicit byte-order tests. Treat file bytes, virtual offsets and reference IDs as linked scientific inputs: a stale companion can misroute access.
For parents, ask whether efficient storage also has integrity, privacy and provenance checks.
Section 6 of 36
6. Read the magic and header length
The binary prefix and length fields let a parser identify BAM and delimit the header. A matching extension alone is not evidence of file type.
Working checkpoint — coordinate sorting: Inspect the magic bytes and bounded lengths before allocating memory. Keep BAM and index hashes, header, reference, sorting method, BGZF validation and software versions together.
For a classroom model, index fictional cards in small compressed packets.
Section 7 of 36
7. Map numeric reference IDs
Alignment records use integer reference IDs that point into the header’s reference sequence list. The number has no meaning outside that file context.
Working checkpoint — BAM: Resolve every ID through the same validated header. Read retrieval speed beside record counts, reference identity, region boundaries and known control results.
For future study, connect compression to genomics, mathematics and systems engineering while checking official pathways separately.
Section 8 of 36
8. Understand packed alignment records
Each record carries fixed core fields plus variable-length query name, CIGAR, packed sequence, qualities and optional data. Length fields drive safe parsing.
Working checkpoint — binary alignment: Reject records whose declared spans exceed the block. Preserve unmapped tails, rejected records and failed regions; an indexed query is not the whole file.
For scientific writing, report file and index identity, reference, sort order and retrieval method.
Section 9 of 36
9. Decode packed sequence
BAM packs bases into four-bit values, two symbols per byte. Decoding must follow the defined symbol table rather than assuming only A, C, G and T.
Working checkpoint — BGZF: Test ambiguous bases and odd sequence lengths. Separate compact storage and successful random access from alignment correctness and biological validity.
For students, ask what the codebook and index let a computer find without changing record meaning.
Section 10 of 36
10. Decode CIGAR operations
BAM packs each CIGAR operation as a length and operation code. The decoded semantics should match the SAM representation.
Working checkpoint — virtual offset: Compare query and reference spans after decoding. Treat file bytes, virtual offsets and reference IDs as linked scientific inputs: a stale companion can misroute access.
For parents, ask whether efficient storage also has integrity, privacy and provenance checks.
Section 11 of 36
11. Decode qualities correctly
Base qualities are stored as numeric values, with a special all-255 representation for unavailable quality. Textual SAM uses an ASCII offset when qualities are present.
Working checkpoint — BAI index: Test missing and present qualities separately. Keep BAM and index hashes, header, reference, sorting method, BGZF validation and software versions together.
For a classroom model, index fictional cards in small compressed packets.
Section 12 of 36
12. Decode optional tags by type
Optional fields retain tag and type information in binary form. Unknown types or malformed lengths can corrupt parsing of the remainder of a record.
Working checkpoint — coordinate sorting: Use type-aware validation and preserve unsupported tags. Read retrieval speed beside record counts, reference identity, region boundaries and known control results.
For future study, connect compression to genomics, mathematics and systems engineering while checking official pathways separately.
Section 13 of 36
13. Understand BGZF
BGZF is block compression implemented on standard gzip. Its concatenated blocks are individually bounded and designed for efficient random access.
Working checkpoint — BAM: Record BGZF structure separately from BAM record validation. Preserve unmapped tails, rejected records and failed regions; an indexed query is not the whole file.
For scientific writing, report file and index identity, reference, sort order and retrieval method.
Section 14 of 36
14. Use virtual offsets
A virtual offset combines the compressed offset of a BGZF block with an uncompressed offset within that block. It is comparable but not an ordinary linear byte position.
Working checkpoint — binary alignment: Never add arbitrary byte counts to a virtual offset. Separate compact storage and successful random access from alignment correctness and biological validity.
For students, ask what the codebook and index let a computer find without changing record meaning.
Section 15 of 36
15. Check block size boundaries
BGZF blocks are limited to 64 KiB before and after compression under the specification’s rules. Corrupt sizes can make a parser seek outside valid data.
Working checkpoint — BGZF: Validate headers, compressed payloads and checksums for every block. Treat file bytes, virtual offsets and reference IDs as linked scientific inputs: a stale companion can misroute access.
For parents, ask whether efficient storage also has integrity, privacy and provenance checks.
Section 16 of 36
16. Understand the EOF marker
A defined empty BGZF marker should appear at the end so unintended truncation can be detected. Its absence warrants warning or error in appropriate access modes.
Working checkpoint — virtual offset: Check the marker after every transfer or write. Keep BAM and index hashes, header, reference, sorting method, BGZF validation and software versions together.
For a classroom model, index fictional cards in small compressed packets.
Section 17 of 36
17. Do not confuse EOF with authenticity
The marker shows an intended physical ending pattern, not who created the file or whether all expected records are present.
Working checkpoint — BAI index: Pair it with hashes, sizes and expected-count checks. Read retrieval speed beside record counts, reference identity, region boundaries and known control results.
For future study, connect compression to genomics, mathematics and systems engineering while checking official pathways separately.
Section 18 of 36
18. Sort before indexing
The specification requires BAM to be sorted by reference ID and leftmost coordinate before indexing. Header declarations alone do not rearrange data.
Working checkpoint — coordinate sorting: Verify order and regenerate @HD sorting metadata. Preserve unmapped tails, rejected records and failed regions; an indexed query is not the whole file.
For scientific writing, report file and index identity, reference, sort order and retrieval method.
Section 19 of 36
19. Understand BAI
BAI uses binning and linear indexes to identify chunks likely to overlap a requested region. It points into one particular BGZF byte stream.
Working checkpoint — BAM: Keep the BAM and BAI as an integrity-linked pair. Separate compact storage and successful random access from alignment correctness and biological validity.
For students, ask what the codebook and index let a computer find without changing record meaning.
Section 20 of 36
20. Understand CSI
CSI is a successor to BAI with a related configurable binning approach and can index BAM and other coordinate-sorted formats.
Working checkpoint — binary alignment: Choose an index format that supports the reference coordinate range. Treat file bytes, virtual offsets and reference IDs as linked scientific inputs: a stale companion can misroute access.
For parents, ask whether efficient storage also has integrity, privacy and provenance checks.
Section 21 of 36
21. Query overlaps, then filter
An index retrieves candidate chunks, and the reader still evaluates decoded records for actual overlap. An index is not a list of biological features.
Working checkpoint — BGZF: Validate boundary cases at interval starts and ends. Keep BAM and index hashes, header, reference, sorting method, BGZF validation and software versions together.
For a classroom model, index fictional cards in small compressed packets.
Section 22 of 36
22. Reject stale indexes
Changing sorting, filtering, headers or compression offsets invalidates an existing index even when the filename stays the same.
Working checkpoint — virtual offset: Regenerate and timestamp the index after every rewrite. Read retrieval speed beside record counts, reference identity, region boundaries and known control results.
For future study, connect compression to genomics, mathematics and systems engineering while checking official pathways separately.
Section 23 of 36
23. Preserve unmapped tails
Coordinate-sorted BAM commonly places records without a reference coordinate after mapped records. Regional queries may not enumerate every unmapped record.
Working checkpoint — BAI index: Use whole-file statistics for unmapped totals. Preserve unmapped tails, rejected records and failed regions; an indexed query is not the whole file.
For scientific writing, report file and index identity, reference, sort order and retrieval method.
Section 24 of 36
24. Stream and seek deliberately
Sequential streaming and indexed random access answer different operational needs. Remote range retrieval adds transport assumptions and caching.
Working checkpoint — coordinate sorting: Test full scans and selected regions independently. Separate compact storage and successful random access from alignment correctness and biological validity.
For students, ask what the codebook and index let a computer find without changing record meaning.
Section 25 of 36
25. Compare BAM with CRAM
BAM compresses alignment records in BGZF without CRAM’s reference-based encoding model. CRAM can reduce storage further but adds explicit reference and codec considerations.
Working checkpoint — BAM: Choose by access, preservation and interoperability requirements. Treat file bytes, virtual offsets and reference IDs as linked scientific inputs: a stale companion can misroute access.
For parents, ask whether efficient storage also has integrity, privacy and provenance checks.
Section 26 of 36
26. Protect sample information
Headers and tags can contain sample, library, platform and processing metadata. Compression does not make sensitive genomic data anonymous.
Working checkpoint — binary alignment: Apply access control, encryption and data minimisation. Keep BAM and index hashes, header, reference, sorting method, BGZF validation and software versions together.
For a classroom model, index fictional cards in small compressed packets.
Section 27 of 36
27. Validate conversion fidelity
SAM-to-BAM and BAM-to-SAM tools may normalise formatting while preserving logical values. A validation should compare records semantically, not only byte for byte.
Working checkpoint — BGZF: Use canonical fixtures and field-level comparisons. Read retrieval speed beside record counts, reference identity, region boundaries and known control results.
For future study, connect compression to genomics, mathematics and systems engineering while checking official pathways separately.
Section 28 of 36
28. Connect to Primary Science
Compressing a labelled collection shows how a codebook can save space while still allowing every item to be reconstructed.
Working checkpoint — virtual offset: Use fictional cards and keep the codebook visible. Preserve unmapped tails, rejected records and failed regions; an indexed query is not the whole file.
For scientific writing, report file and index identity, reference, sort order and retrieval method.
Section 29 of 36
29. Connect to Secondary Science
A storage format preserves measurements only when metadata, units and provenance travel with them. Fast retrieval does not make the measurement more accurate.
Working checkpoint — BAI index: Separate data logistics from evidence quality. Separate compact storage and successful random access from alignment correctness and biological validity.
For students, ask what the codebook and index let a computer find without changing record meaning.
Section 30 of 36
30. Connect to mathematics
Binary numbers, bit packing, powers of two, intervals and hierarchical bins make BAM a concrete application of discrete mathematics.
Working checkpoint — coordinate sorting: Convert one packed value and trace one interval to a bin. Treat file bytes, virtual offsets and reference IDs as linked scientific inputs: a stale companion can misroute access.
For parents, ask whether efficient storage also has integrity, privacy and provenance checks.
Section 31 of 36
31. Connect to computing
Compression, random access, checksums, indexing and safe binary parsing show how systems design supports genomic science.
Working checkpoint — BAM: Fuzz malformed lengths and truncated blocks safely. Keep BAM and index hashes, header, reference, sorting method, BGZF validation and software versions together.
For a classroom model, index fictional cards in small compressed packets.
Section 32 of 36
32. Connect to pathways
BAM work connects bioinformatics, cloud engineering, cybersecurity and research data stewardship and can illuminate STEM pathways.
Working checkpoint — binary alignment: Check current official qualifications separately. Read retrieval speed beside record counts, reference identity, region boundaries and known control results.
For future study, connect compression to genomics, mathematics and systems engineering while checking official pathways separately.
Section 33 of 36
33. Did you know?
A BAM index does not point to an ordinary uncompressed byte position; it uses a virtual offset that identifies a BGZF block and a location inside its decoded data.
Working checkpoint — BGZF: Explain both halves before interpreting an index entry. Preserve unmapped tails, rejected records and failed regions; an indexed query is not the whole file.
For scientific writing, report file and index identity, reference, sort order and retrieval method.
Section 34 of 36
34. Write a reader-ready storage report
Name BAM and index specifications, reference, header, sort method, conversion tools, file and index hashes, record counts and validation results.
Working checkpoint — virtual offset: State whether access was sequential, local indexed or remote ranged. Separate compact storage and successful random access from alignment correctness and biological validity.
For students, ask what the codebook and index let a computer find without changing record meaning.
Section 35 of 36
35. Build a validation ladder
Check magic, header, reference dictionary, record lengths, BGZF blocks, EOF, sort order, index pairing and known-region retrieval.
Working checkpoint — BAI index: Stop before biological analysis if any storage layer fails. Treat file bytes, virtual offsets and reference IDs as linked scientific inputs: a stale companion can misroute access.
For parents, ask whether efficient storage also has integrity, privacy and provenance checks.
Section 36 of 36
36. Finish with a reproducible bundle
Deliver BAM and index hashes, reference identifiers, decoded header, software environment, commands, transfer receipts, validation logs and bounded downstream outputs.
Working checkpoint — coordinate sorting: Make every retrieved record traceable to exact stored bytes. Keep BAM and index hashes, header, reference, sorting method, BGZF validation and software versions together.
For a classroom model, index fictional cards in small compressed packets.
Contents · Previous section · Continue to the Science Learning Hub
