

前言
有时候,分析已发表数据的时候,避免不了会遇到作者上传的数据是BAM格式,但是作者用的基因组又不是我想要的,所以我就需要将BAM转换为FASTQ,另外,有些特殊的测序,或者某些仪器的产出,或者某些测序公司的偏好,返还的数据是BAM格式(包含更全面的信息),加上许多分析工具都是需要以FASTQ文件为起始输入,所以需要将BAM转换为FASTQ。BAM转换为FASTQ是生信分析中的常见步骤,虽然现在还没遇到,提前整理,以备不时之需😑工具有很多,接下来就罗列几个工具
Samtools
Samtools 应该是圈子内最出名的几个工具之一了,应用十分广泛,专精于SAM、BAM格式文件的操作,被整合到各种分析流程之中,比如ATAC-seq、ChIP-seq等等。应用Samtools将BAM转换为Fastq要分为两步:
- 根据Read Name排序:
samtools sort -n SAMPLE.bam -o SAMPLE_sorted.bam
- 转换,两种方式:
- 直接转换
samtools fastq -@ 8 SAMPLE_sorted.bam \ -1 SAMPLE_R1.fastq.gz -2 SAMPLE_R2.fastq.gz \ -0 /dev/null -s /dev/null -n
- 先转换为fq,再拆分
转换:samtools fastq SAMPLE.bam > SAMPLE.fastq
拆分:对于双端测序数据,序列名字尾端会被加上 /1 或者 /2cat SAMPLE.fastq | grep '^@.*/1$' -A 3 --no-group-separator > SAMPLE_r1.fastq cat SAMPLE.fastq | grep '^@.*/2$' -A 3 --no-group-separator > SAMPLE_r2.fastq
- 直接转换
更多详见:http://www.htslib.org/doc/samtools.html
PS: samtools fastq
是新版的命令,而samtools bam2fq
,是旧版的命令(1.13之前?忘记了)
Bedtools
Bedtools,可用于各种分析流程,可以对例如BAM、BED、GFF/GTF、VCF等文件进行计数、合并、整合等各种操作。其提供了一个工具:bamtofastq,用于转换,但是对单端或者双端测序操作略有不同:
- 单端:
bedtools bamtofastq -i input.bam -fq output.fastq
- 双端:
- 按序列名称排序:
samtools sort -n input.bam -o input_sorted.bam
- 转换:
bedtools bamtofastq -i input_sorted.bam -fq output_r1.fastq -fq2 output_r2.fastq
- 按序列名称排序:
更多详见:http://bedtools.readthedocs.org/en/latest/content/tools/bamtofastq.html
Picard
Picard是一个基于Java开发的工具,用于处理测序数据,我接触的用途更多是去除PCR重复。它提供了一个工具:SamToFastq,可用于BAM到FASTQ的转换:java -Xmx2g -jar Picard/SamToFastq.jar I=SAMPLE.bam F=SAMPLE_r1.fastq F2=SAMPLE_r2.fastq
,双端就用F F2
参数,单端就是F
更多详见:http://broadinstitute.github.io/picard/command-line-overview.html#SamToFastq
10X Genomics 系列
10X的测序很火,加上数据很多且大,有些人发文章上传原始数据的时候就会选择二进制的BAM文件,方便数据的有效利用,所以10X搞了一个工具,专门针对其产品的BAM向Fastq转换,包括:
- cellranger (see exceptions below)
- cellranger-atac
- cellranger-arc
- cellranger-dna
- spaceranger
- longranger
以上分析流程产出的BAM文件,可以直接使用命令行:bamtofastq --nthreads=8 /path/to/mydata.bam /path/to/home/directory
,执行BAM向Fastq转换,该工具也被内置在上面提到的流程中,存在于安装位置的/lib/bin
目录中,比如cellranger-x.y.z/lib/bin/bamtofastq
,使用方法:[pipeline] bamtofastq
,比如: cellranger bamtofastq ...
、 spaceranger bamtofastq ...
其他工具
不展开讨论用法了,这里直接罗列,感兴趣的可以自行探索:
Bamtools:http://github.com/pezmaster31/bamtools
bam2fastx:http://manpages.ubuntu.com/manpages/quantal/man1/bam2fastx.1.html
参考
- http://www.htslib.org/doc/samtools.html
- http://bedtools.readthedocs.org/en/latest/content/tools/bamtofastq.html
- http://broadinstitute.github.io/picard/command-line-overview.html#SamToFastq
- https://support.10xgenomics.com/docs/bamtofastq