Samtools: BAM → FASTA

Accepted: .sam, .bam, .cram

BAM to FASTA Conversion (samtools fasta)

Function:

samtools fasta is a command-line utility used to extract nucleotide sequences from SAM, BAM, or CRAM alignment files and convert them into FASTA format. FASTA is one of the most widely used sequence formats in bioinformatics and is suitable for sequence analysis, alignment, assembly, and annotation workflows.

Input Format:

  • SAM File (.sam)
  • BAM File (.bam)
  • CRAM File (.cram)

Output Format:

  • FASTA File (.fasta or .fa)
  • Contains sequence identifiers and nucleotide sequences.

Example Usage:

# Convert BAM to FASTA
samtools fasta sample.bam > sample.fasta

# Convert SAM to FASTA
samtools fasta sample.sam > sample.fasta

# Convert CRAM to FASTA
samtools fasta sample.cram > sample.fasta

Applications:

  • Extract nucleotide sequences from aligned reads.
  • Prepare data for sequence alignment tools.
  • Generate FASTA files for assembly workflows.
  • Create datasets for phylogenetic analysis.
  • Perform sequence annotation and characterization.
  • Export reads for downstream bioinformatics analyses.

Output Example:

>Read_001
ATGCGTAGCTAGCTAGCTAGCTAGCTA

>Read_002
CGTACGTAGCTAGCTAGCGTAGCTAGC

Each FASTA record consists of a sequence identifier line beginning with >, followed by the nucleotide sequence.

Advantages:

  • Simple and human-readable sequence format.
  • Compatible with most bioinformatics tools.
  • Useful for sequence-based downstream analyses.
  • Supports extraction from SAM, BAM, and CRAM files.
  • Efficient conversion of aligned reads to raw sequence data.

Suggested Reading:

Official Samtools Documentation: Samtools FASTA Manual

Citation:

Li, H., Handsaker, B., Wysoker, A., Fennell, T., Ruan, J., Homer, N., et al. (2009). The Sequence Alignment/Map (SAM) format and SAMtools. Bioinformatics, 25(16), 2078–2079. DOI: 10.1093/bioinformatics/btp352