How to Convert BAM file to FASTQ

FASTQ Files are huge. The recommended option for long term storage, archival and sharing over internet is  BAM format.  BAM files are binary aligned compressed  files and uses considerably less space. However, often we have to go back to fastq format, because certain analysis works on fastq files only. You may have downloaded BAM files from Internet and want to convert to fastq format.

This article will explain how to Convert a BAM File to FASTQ format using SAMTOOLS and BEDTOOLS

Below are the steps to Convert BAM File to FASTQ format. This will work on Linux and will require SAMTOOLS and BEDTOOLS

  1. Download SAMTOOLS. It will be in samtools-version.tar.bz2 format. 
  2. Extract using Command tar xzvf samtools-version.tar.bz2
  3. It will create a folder by the name samtools-version
  4. Move to the directory cd samtools-version
  5. Type make   (This will compile and create executable)
  6. This is the directory where samtools will be installed. You can set path or provide the complete path when executing samtools
  7. SAMTOOLS is required to SORT the BAM Files. If your BAM files are already sorted you can miss the above steps
  8. Sort the BAM files. Type /complete_path/samtools sort filename.bam -0 filename.sorted.bam. You will be using filename.sort.bam to convert to fastq files.
  9. Download BedTools. Follow the same process of installation as for SAMTOOLS.
  10. Move to the bin directory.
  11. Execute /path_to_bedtools/bin/bamToFastq -i filename.sorted.bam -fq filename.sorted.fq
  12. For Paired end Bam /path_to_bedtools/bin/bamToFastq -i filename.sorted.bam -fq filename.sorted_1.fq -fq2 filename.sorted_2.fq

Your BAM file has been converted to FASTQ files and will be in the same directory

Please follow and like us:

Love Technology, Follow us