All studies
DNA·November 21, 2026·6 min read

What Is the .bai File Next to My BAM and How Do I Make One When It Is Missing

A .bai is the small index that lets a program jump to one region of a BAM. If it is missing, samtools index makes one, provided the BAM is coordinate-sorted.

A .bai file is the index of a BAM: a small companion file that records where each stretch of the genome sits inside the compressed BAM, so a program can jump straight to one region instead of reading the whole thing. If your sequencing company did not send it, samtools index sample.bam writes it, usually in minutes to under an hour, as long as the BAM is coordinate-sorted.

What the .bai file actually is

A whole-genome BAM holds every sequencing read, already aligned to the reference genome. At 30x coverage that is tens of gigabytes. The BAM is compressed in BGZF blocks, and the reads inside are ordered by where they land on the genome.

The .bai is a lookup table over that order. It divides each chromosome into nested bins and also keeps, for every 16 kb window, the file offset of the first read that overlaps it. Each offset names a compressed block plus a position inside it. As an example, ask for chr19:44,908,684 (a GRCh38 coordinate, the position of rs429358 in APOE) and the program reads the index, learns which few blocks cover that spot, and decompresses only those. The index is tiny next to the BAM.

≈60–100 GB
typical size of a 30x whole-genome BAM (typical, varies with coverage and read length)
≈5–10 MB
typical size of its .bai index (typical, varies with coverage and read length)

Why a tool that reads your BAM asks for it

Without the index, finding one position means decompressing the file from the start until you reach it. For a single SNP that is absurd, and for a few million SNPs it is worse. So most tools simply refuse. The usual messages are along the lines of "random alignment retrieval only works for indexed BAM or CRAM files" or a failure to load the index. Nothing is wrong with your reads. The tool just wants a map before it will go looking.

Get samtools first

samtools is free. Pick the route that fits your system:

  • Any system with conda: conda install -c bioconda samtools
  • macOS with Homebrew: brew install samtools
  • Debian or Ubuntu Linux: sudo apt install samtools
  • Windows: samtools has no simple native Windows install. Use WSL (the Windows Subsystem for Linux, then the apt command above inside it), or install conda and use the bioconda command. Your Windows drives show up inside WSL under /mnt/c/.

Create a bam.bai index when it is missing

First check your free disk space with df -h. Indexing needs almost nothing, but if you end up sorting, a 30x genome needs roughly 2 to 3 times the BAM size free (temporary files plus the sorted output).

Then the quickest test is to try it. Run:

  • samtools index sample.bam

That writes sample.bam.bai next to the BAM. For a big file, add threads with samtools index -@ 4 sample.bam.

samtools index checks the actual order of the reads, not the header label. A BAM with no SO field may already be coordinate-sorted, and a header can claim SO:coordinate wrongly. If indexing stops with a "not sorted" error, sort and then index:

  • samtools sort -@ 4 -m 2G -T /path/with/space/tmp/sample -o sample.sorted.bam sample.bam
  • samtools index -@ 4 sample.sorted.bam

Here -m 2G is the memory per thread and -T sets where the temporary files go, so point it at a drive with room. The -@ option sets additional threads on top of the main one, so the memory figure of about 8 GB (-m times threads) is approximate. If your machine has less than 16 GB of RAM, lower -m (for example -m 1G) rather than let it swap. For a 30x genome, plan for temporary files about the size of the BAM again, on top of the sorted output. Sorting is the slow step, often one to a few hours depending on threads and disk. Indexing afterwards is mostly a single read through the file, so disk speed sets the time.

What the BAM header suggests
Header saysNext step
Sorted by positionSO:coordinateIndex now
Sorted by read nameSO:querynameSort, then index
Not sortedSO:unsortedTry index; sort if it errors
Not statedno SO fieldTry index; sort if it errors

Scroll the table sideways for the rest →

Read from the @HD line of samtools view -H. The header is only a hint: samtools index checks the real read order.

That "not sorted" error is the tool protecting you: an index over unordered reads would point to the wrong places.

The BAM and the .bai have to be the same pair

This is where people go wrong. The index holds file offsets, and offsets only mean something for the exact file they were built from. The .bai carries no check that it belongs to your BAM, so a mismatched pair can fail with an error or hand back the wrong reads. The one safeguard is timestamps: htslib tools such as samtools warn when the index file is older than the BAM. If you see that warning, rebuild the .bai.

So keep them together and keep the names matched. Tools look for sample.bam.bai or sample.bai. If you rename the BAM, rename the index to match. If you sort, re-header or otherwise rewrite the BAM in any way, the old .bai is dead: build a new one. Never borrow an index from a friend's file, or from a different run of your own.

When the simple answer does not hold

  • CRAM instead of BAM. CRAM uses a .crai index. A .bai will not do, and this is a different file type. The BAM to VCF page takes a BAM with its .bai, so a CRAM has to become a BAM first: samtools view -b -T reference.fa -o sample.bam sample.cram, then samtools index sample.bam. The -T reference must be the exact one the CRAM was compressed against (same build and same contig names), or the conversion fails.
  • Very long chromosomes. The BAI format caps each reference sequence at 2^29 bases, about 537 million bases. Human chromosomes are well under that, so you will not hit it. Some plant and amphibian genomes do, and they need a .csi index, which you make with samtools index -c sample.bam.
  • A truncated BAM. samtools warns that the EOF marker is missing, and indexing usually fails with a truncated-file or missing-EOF error. Re-download the file and compare its size or checksum with what the provider gives.
  • No reads in a region. An index can be perfect and a position can still return nothing, because a BAM holds reads, not calls. A spot with no coverage has no genotype.

What the pair lets you do in your browser

Once you have a coordinate-sorted BAM and its matching .bai, you can drop both on the BAM to VCF page. It reads the .bam with its .bai in your browser and calls your genotype at about two million known SNPs, then writes a VCF on GRCh37 or GRCh38. Nothing is uploaded. If you want output in a consumer chip layout instead, use BAM to 23andMe.

This is not a whole-genome variant call. It will not give you rare variants, indels or structural variants. For those you run GATK or DeepVariant on your own machine.

The .bai is only a map of one specific BAM: same sort, same file, same name, or it points to the wrong places.

Once you have the VCF made from your BAM, the free report at healthosx.com/dna reads that whole-genome VCF and turns it into your results.

New to raw DNA files? Start with the full guide: 23andMe Raw Data Interpretation Free in Your Browser

Written by

HealthOSX Research

Keep reading