bam2fq

bam2fq

原文日期: 2017-04-06
来源: https://github.com/wlz0726/wlz0726.github.io


BAM 转 FASTQ

使用 samtools

单端数据

1
samtools fastq input.bam > output.fastq

双端数据

1
2
# 输出两个文件
samtools fastq -1 read1.fastq -2 read2.fastq -0 /dev/null -s /dev/null -n input.bam

参数说明:

  • -1: 第一个 read 的输出文件

  • -2: 第二个 read 的输出文件

  • -0: 只有一个 read 比对的输出(通常丢弃)

  • -s: 只有一个 read 比对的输出(通常丢弃)

  • -n: 保留原始 read 名

按比对状态过滤

1
2
3
4
5
6
7
8
# 仅比对上的 reads
samtools fastq -F 4 input.bam > mapped.fastq

# 仅未比对的 reads
samtools fastq -f 4 input.bam > unmapped.fastq

# 正确配对的 reads
samtools fastq -f 2 input.bam > paired.fastq

使用 bedtools

1
2
3
bedtools bamtofastq \
-i input.bam \
-fq output.fastq

使用 Picard

1
2
3
4
5
picard SamToFastq \
I=input.bam \
F=output_R1.fastq \
F2=output_R2.fastq \
UNPAIRED_FASTQ=unpaired.fastq

注意事项

  1. 质量值格式: 确保与下游工具兼容

  2. 双端配对: 保持 read1 和 read2 的顺序一致

  3. 文件名: 使用有意义的命名

  4. 压缩: 输出 gzip 压缩格式节省空间

1
samtools fastq input.bam | gzip > output.fastq.gz

此文档为 GitHub 博客自动归档