To quickly convert a BAM file to a FASTQ file, you can use tools like samtools or bedtools. The most common and efficient method is with samtools fastq. Here’s how you can do it:
Using samtools
1. Install samtools (if not already installed):
conda install -c bioconda samtools
# or
sudo apt-get install samtools
2. Convert BAM to FASTQ:
samtools fastq input.bam -o output.fastq
Options:
-0 /dev/null to discard unpaired reads if you only want paired reads.
-s singles.fastq to save singleton reads.
Using bedtools
1. Install bedtools:
conda install -c bioconda bedtools
# or
sudo apt-get install bedtools
2. Convert BAM to FASTQ:
bedtools bamtofastq -i input.bam -fq output.fastq
Summary Table
| Tool | Command Example | Notes |
|---|
| samtools | samtools fastq input.bam -o output.fastq | Fast, widely used |
| bedtools | bedtools bamtofastq -i input.bam -fq output.fastq | Also fast, flexible |
Tip:
If your BAM file is coordinate-sorted, you may want to sort it by read name first for paired-end conversion:
samtools sort -n -o input_name_sorted.bam input.bam
samtools fastq -1 R1.fastq -2 R2.fastq input_name_sorted.bam
References:
Let me know if you need a command for a specific scenario!