Extract sequences from coordinates with bedtools getfasta
Turn BED intervals into FASTA sequences with bedtools getfasta, including strand-aware extraction that reverse-complements minus-strand features.
Prerequisites
- bedtools installed (`bedtools --version` >= 2.27)
- A genome FASTA file (genome.fasta)
- A BED file of 0-based, half-open intervals (regions.bed)
1Extract one sequence per BED interval
Point `-fi` at the genome and `-bed` at your intervals; each BED line becomes one FASTA record written to the `-fo` file. The command prints nothing to stdout, and getfasta auto-creates the `genome.fasta.fai` index on first run if it is missing.
bedtools getfasta -fi genome.fasta -bed regions.bed -fo out.fastaExpected output
$ head out.fasta
>chr1:1000-1500
GACTTTGCCATCGATTACAGGCATCCGGCCTTAAGG...ACGTACGTACGT
>chr1:2000-2200
CCTAATGTCCGTATGGCCGGAATTCCATGCTGAAAC...GGTAGCTAATGT2Extract strand-aware, named sequences
Add `-s` so minus-strand features are reverse-complemented and every record comes out on the sense strand, and `-name` to label each record with the BED name column instead of its coordinates. On recent bedtools the header keeps the interval and strand too, for example `>gene1::chr1:1000-1500(+)`.
bedtools getfasta -s -name -fi genome.fasta -bed regions.bed -fo out.fastaExpected output
$ head out.fasta
>gene1::chr1:1000-1500(+)
GACTTTGCCATCGATTACAGGCATCCGGCCTTAAGG...ACGTACGTACGT
>gene2::chr1:2000-2200(-)
CTTACGATTAGCTACCGTTTCAGCATGGAATTCCGG...GACATTAGGTACTroubleshooting
Feature (chr1:249000000-249000500) beyond the length of chr1 size (248956422 bp). Skipping.
The BED coordinates run past the end of the contig. Your BED is for a different assembly, or it is 1-based - BED must be 0-based, half-open. Convert coordinates or use the matching genome build.
Empty output, or 'WARNING. chromosome (1) was not found in the FASTA file. Skipping.'
Chromosome names in the BED and FASTA disagree (e.g. '1' vs 'chr1'). Rename one side so they match exactly, for example `sed 's/^/chr/' regions.bed` or strip the prefix from the FASTA headers.