Skip to main content
v2026.11,986 entries · CC-BY 4.0

samtools faidx: Index a FASTA and Extract Regions

How to run samtools faidx: build a .fai index, extract regions, read the five index columns, and fix common errors, checked against the samtools-1.25 manual.

Written and maintained by CASRAI Editorial Board

Published · Last updated

On this page

samtools faidx does two jobs on a reference sequence file in FASTA format: run with only a file name, it indexes the file and writes a companion index named after it with .fai added (ref.fasta.fai); run with one or more regions after the file name, it reads that index and prints just those subsequences to standard output in FASTA format. The index is what lets analysis tools jump straight to a chromosome or interval without reading the whole file, which is why it is usually the first command run on a new reference genome.

What samtools faidx is used for in research

Reference genomes, transcriptomes and other sequence collections are stored as FASTA files that can run to gigabytes. Aligners, variant callers and genome browsers need to fetch an arbitrary stretch of one sequence quickly, and they do it through a small flat index rather than by scanning. samtools faidx builds that index and is also a handy way to pull a single gene, amplicon or probe target out of a reference by coordinates for primer design, manual inspection or a figure. The same command reads FASTQ files when asked, and its manual page notes that BGZF-compressed input and output are supported. If you work with alignments, the sequence names and lengths in the index are the same values that appear in a SAM or BAM header, covered in what a BAM file is and SAM vs BAM vs CRAM.

How to install or access it

samtools faidx is a subcommand of Samtools, which is released together with the HTSlib library by the project that publishes its documentation at htslib.org. Use the project’s own release and manual pages rather than third-party mirrors, because options differ between versions. The manual page for the command (htslib.org/doc/samtools-faidx.html) was checked for this guide against samtools-1.25, released on 27 October 2026. Several options below, such as –mark-strand and –write-index, appear in that manual, so an older installed version may not list them. The reliable way to see what your own installation supports is to run the built-in help:

samtools faidx --help

Step-by-step: index a FASTA file and extract regions

All forms below use the synopsis from the manual, samtools faidx ref.fasta [region1 […]], with ref.fasta standing in for your own file.

  1. Check the file layout first. The index format needs every sequence line of a record to be the same length except the last line, with consistent line terminators inside each record (see the next section). A file that violates this cannot be indexed as a regular FASTA.
  2. Build the index. Give only the file name:
    samtools faidx ref.fasta

    This creates ref.fasta.fai next to the FASTA file. Nothing is printed to the screen. If two records share a name, the manual says indexing warns and only the first sequence with that name can be retrieved, so keep names unique.

  3. Extract one region. Add a region in the chr:from-to form after the file name:
    samtools faidx ref.fasta chr1:10000-20000

    The subsequence is written to standard output as FASTA. Redirect it, or use -o, to keep it.

  4. Extract a whole sequence. Give just the sequence name, for example samtools faidx ref.fasta chr1, to print that record in full. Several regions can follow one another on the same command line.
  5. Extract many regions from a list. Put one chr:from-to region per line in a text file and pass it with -r:
    samtools faidx ref.fasta -r regions.txt
  6. Write to a file and re-wrap the lines. -o names the output file, and -n sets the line length of the FASTA output (0 means no wrapping; by default the line length of the input file is kept):
    samtools faidx ref.fasta chr1:10000-20000 -o target.fa -n 0
  7. Keep going when a region is missing. Add -c (–continue) to keep working if a requested region does not exist:
    samtools faidx ref.fasta -c -r regions.txt

The sequence name in a region has to be a name the index knows. Because the index NAME is the first word after the greater-than sign on the header line, a region written as 17:100-200 will not find a sequence named chr17, and the reverse is equally true. When a pipeline mixes references from different sources, check the names in the .fai first.

Options that change the output

Option What it does (samtools-1.25 manual)
-o, –output FILE Writes to a file instead of standard output. A name ending in .gz, .bgz or .bgzf produces BGZF-compressed output.
-n, –length INT Sets the FASTA line length of the output; 0 means no wrapping; the default is the line length of the input.
-c, –continue Carries on if a requested region is not found.
-r, –region-file FILE Reads regions from a file, one chr:from-to per line.
-f, –fastq Reads FASTQ and writes FASTQ; the manual says it is the same as samtools fqidx. Without it, sequences extracted from a FASTQ are written as FASTA.
-i, –reverse-complement Outputs the reverse complement; by default “/rc” is added to the sequence name.
–mark-strand TYPE Controls that name suffix: rc (default), no, sign, or custom,POS,NEG. The sign form appends (+) or (-), matching the output of bedtools getfasta -s.
–fai-idx FILE, –gzi-idx FILE Name the index files to read or write; the .gzi index is used with BGZF-compressed (.gz) files.
–output-fmt-option OPT=VAL Sets output options, for example level=0 to level=9 compression. Compressed output defaults to level 4.
–write-index Creates the .fai and .gzi indexes for the output file. Valid only when the output goes to a file.
-@, –threads INT Number of extra threads for compressed-file operations.

Output and how to read the .fai index

The extraction output is ordinary FASTA: one header line per requested region followed by the sequence, wrapped at the input line length unless you set -n. The index is a tab-delimited text file with one line per sequence. According to the HTSlib faidx format page, a FASTA index has five columns and a FASTQ index has six:

Column Name Meaning
1 NAME Sequence name, the first word after the header marker. It is the value used as SN in a SAM @SQ header line.
2 LENGTH Number of bases in the sequence. It corresponds to LN in a SAM @SQ line.
3 OFFSET Byte offset in the file of the first base of the sequence, counted from 0.
4 LINEBASES Number of bases on each full line, not counting the line terminator.
5 LINEWIDTH Number of bytes in each full line, including the line terminator.
6 QUALOFFSET FASTQ only: byte offset of the first quality value.

With those columns, a tool locates any base by arithmetic: skip to OFFSET, then add one terminator for every LINEBASES bases passed. The data table below the guide reproduces the worked examples from the HTSlib format page. In the LF example the sequence named one has length 66, offset 5, 30 bases per line and a line width of 31 bytes, while two has length 28, offset 98, 14 bases per line and a width of 15. The same records saved with CRLF (Windows) line endings give a line width of 32 for one (30 bases plus two terminator bytes) and push the offsets to 6 and 103, because every terminator, including the one after the header, is now two bytes. This is why a FASTA file edited on another operating system can need re-indexing: the index records byte positions, not just base positions.

The format page lists the conditions a file must meet to be indexed: all sequence lines in a record are the same length except the last, the line length may differ from one record to the next, and the terminators within a record are consistent. For FASTQ the quality lines are wrapped the same way as the sequence lines, and the index belongs next to the file as file.fq.fai or file.fastq.fai. Because the index is tied to the exact bytes of the FASTA file, regenerate it whenever you modify, re-wrap or re-save the reference.

Indexing and extracting from FASTQ

The manual states that FASTQ files can be indexed too. Run samtools faidx reads.fq (or samtools fqidx reads.fq) to build the six-column index, then add -f to get FASTQ back out. Without -f the extracted sequences are FASTA records. Details of the read format are in FASTQ format explained.

Reverse-complementing a region

-i emits the reverse complement of each requested region and, by default, adds the suffix “/rc” to its name so a flipped sequence is not mistaken for the forward one. Use –mark-strand sign to get (+) and (-) suffixes, no to leave names unchanged, or custom,POS,NEG to choose your own text. The sign style is documented as matching bedtools getfasta -s, which makes outputs from the two tools easier to compare.

Troubleshooting

  • A region returns nothing. The sequence name in the region does not match any NAME in the .fai. Compare against the first column of the index (chr17 and 17 are different names). Add -c (–continue) to keep working if a requested region does not exist.
  • Only one of two same-named sequences comes back. The manual says duplicate names raise a warning at indexing and only the first sequence with that name is retrieved. Rename the duplicates in the FASTA file and re-index.
  • Indexing fails or the index seems wrong. Check the layout rules from the format page: equal line length inside each record (except the last line), consistent terminators, and a FASTQ whose quality lines wrap like its sequence lines. Re-wrapping the file with a consistent line length, then re-indexing, is the straightforward fix.
  • The index is out of date after editing the FASTA. The offsets are byte positions in that particular file, so rebuild the .fai after any change.
  • Output file is not compressed. Compression follows the output name: use .gz, .bgz or .bgzf with -o. To index a compressed result at the same time, add –write-index, which is accepted only with file output.
  • An option is not recognised. Options such as –mark-strand and –write-index are in the samtools-1.25 manual; an older build may not have them. Check samtools faidx --help on the machine you run it on and cite the version in your methods text.

Free alternatives and related tools

Tool Relation to samtools faidx Source statement
samtools fqidx The FASTQ counterpart; identical to samtools faidx -f. samtools faidx manual
pyfaidx (Python) Pure-Python indexing and random access to FASTA, working with a samtools-compatible .fai; it also installs a faidx command-line script; BSD-3-Clause licence. pyfaidx page on PyPI
bedtools getfasta Extracts FASTA sequences for intervals listed in a BED, GFF or VCF file; the FASTA headers must exactly match the chromosome column of the interval file; -s makes it strand-aware. bedtools documentation

In pyfaidx a region is fetched by slicing, for example from pyfaidx import Fasta; genes = Fasta('genes.fasta'); genes['NM_001282543.1'][200:230]. Its documentation notes that slice coordinates are 0-based like Python, so check the coordinate convention each time you move intervals between tools. For command-line analysis of downstream files see bcftools and the BAM index (.bai) file, the alignment-side counterpart of the .fai index.

Frequently asked questions

What does samtools faidx do?

With no region it indexes a FASTA file and writes ref.fasta.fai; with regions it prints those subsequences to standard output in FASTA format. This follows the command’s manual page.

What is a .fai file?

A tab-delimited index with one line per sequence: name, length, byte offset of the first base, bases per line and bytes per line (and a sixth column, quality offset, for FASTQ). It lets software read a region without scanning the whole file.

Can samtools faidx index a compressed FASTA?

The manual says BGZF input and output are supported, and a .gzi index is used with compressed files. Plain gzip files are not the same as BGZF; the manual refers specifically to BGZF.

How do I extract many regions at once?

List them in a text file, one chr:from-to per line, and pass the file with -r; add -c to continue past regions that are not found.

How do I get the reverse complement?

Add -i. The output name gets the suffix “/rc” unless you change it with –mark-strand.

Sources

Data: Worked .fai index examples for LF, CRLF and FASTQ files (HTSlib faidx format page)

Index values published as worked examples on the HTSlib faidx format page, arranged by line-terminator style and file type so that the effect of CRLF terminators and FASTQ quality offsets on each column can be compared side by side.

Worked .fai index examples for LF, CRLF and FASTQ files (HTSlib faidx format page)
ExampleNAMELENGTH (bases)OFFSET (bytes)LINEBASES (bases)LINEWIDTH (bytes)QUALOFFSET (bytes)
LF example, record oneone6653031none
LF example, record twotwo28981415none
CRLF example, record oneone6663032none
CRLF example, record twotwo281031416none
FASTQ example, record onefastq1668303179
FASTQ example, record twofastq2281561415188

Sources

Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · full access

Ask about samtools faidx: Index a FASTA and Extract Regions

Full access: large monthly usage, up to 10 source passages behind each answer, and MCP and API access for Claude Code, Codex and other CLIs. Cancel any time.

An AI assistant for research: funder rules, compliance, methods and publishing. It links the sources behind its answers and labels answers that come from a web search.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays open to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →