bcftools常用命令总结

bcftools是一个用于检测突变和处理vcf(variant call format)、bcf(vcf对应的二进制形式)文件的工具。虽然bcftools的大部分命令都可以直接处理没有索引的bcf和vcf,但是个别命令需要建立索引。尤其当bcftools需要同时处理多个vcf时,任何命令都要求vcf文件为压缩形式并存在对应索引文件。

关于BCF1格式不能被当前版本bcftools处理的问题?
倘若由于samtools版本过低造成BCF1格式的input文件不能被当前bcftools处理,可以使用其samtools对应旧版本的bcftools工具的view命令将它转换为vcf格式,如此一来便可以被当前最新版本bcftools处理。

关于输入文件:

  • vcf和bcf
  • BGZF压缩的vcf和bcf
  • 通过管道符获得的标准输入文件

0. 公用参数

注意:-r 和 -R,两个参数不能同时使用;-t和-T,两个参数不能同时使用
-o, --output FILE #输出到指定文件
-O, --output-type b|u|z|v #指定输出文件类型(compressed BCF (b), uncompressed BCF (u), compressed VCF (z), uncompressed VCF (v))
-r, --regions chr|chr:pos|chr:beg-end|chr:beg-[,…] #限制命令的作用区域(不同的染色体区间用','分隔),但凡是与指定区间存在overlap的record都会被处理。(通过tbi/csi index锁定目标区域)
-R, --regions-file FILE #同样得作为一个限制作用区域的命令,可以接VCF、BED和命令行以及tab分隔的文件。如果是VCF/BCF文件需要建立索引。The columns of the tab-delimited file can contain either positions (two-column format) or intervals (three-column format): CHROM, POS, and, optionally, END, where positions are 1-based and inclusive. The columns of the tab-delimited BED file are also CHROM, POS and END (trailing columns are ignored), but coordinates are 0-based, half-open. To indicate that a file be treated as BED rather than the 1-based tab-delimited file, the file must have the ".bed" or ".bed.gz" suffix (case-insensitive). Uncompressed files are stored in memory, while bgzip-compressed and tabix-indexed region files are streamed. Note that sequence names must match exactly, "chr20" is not the same as "20". Also note that chromosome ordering in FILE will be respected, the VCF will be processed in the order in which chromosomes first appear in FILE. However, within chromosomes, the VCF will always be processed in ascending genomic coordinate order no matter what order they appear in FILE. Note that overlapping regions in FILE can result in duplicated out of order positions in the output. 
-s, --samples [^]LIST #用来限定执行操作的sample,排除使用"^" The sample order is updated to reflect that given on the command line. Note that in general tags such as INFO/AC, INFO/AN, etc are not updated to correspond to the subset samples.
-S, --samples-file FILE #与-s功能相似,只不过这里把list改成了file,要求一个样本一行,按顺序更新对应样本信息。在call 命令中使用时,file可以存储为两列:一列为sample name, 一列为指示信息
-t, --targets [^]chr|chr:pos|chr:from-to|chr:from-[,…] #同样是限制作用区域的命令,与-r不同的是它直接将非目标区域的内容舍弃,然后采用线性化的方式锁定每一步的小区域。只有与目标区域完全match的位置才会做对应处理。
-T, --targets-file [^]FILE #Same -t, --targets, but reads regions from a file. Note that -T cannot be used in combination with -t.With the call -C alleles command, third column of the targets file must be comma-separated list of alleles, starting with the reference allele. Note that the file must be compressed and index. 
--threads INT #指定线程数

1. annotate

主要功能:添加注释信息;删除注释信息

-a, --annotations file #注释用的数据库文件(Bgzip压缩;tabix索引)#数据库文件形式可以是VCF、BCF、BED或者由tab键分隔的二进制文件(前提:一定要包含CHROM和POSE信息,其中POSE可以被FROM和TO替换)
-c, --columns list #指定将数据库文件中的哪些信息输出到目标文件(不同tag用逗号(',')分隔)
-x, --remove list #去除vcf文件中的注释信息,可以是其中的某一列(这些信息所在的列并不会去除,而是用‘.’填充)

应用举例:
#使用database.vcf数据库注释raw.vcf,并将数据库的ID,QUAL信息输出到结果文件annotated_file.vcf
bcftools annotate -a database.vcf  -c ID,QUAL raw.vcf -o annotated_file.vcf
#将raw.vcf的两列:ID,INFO/DP填充为'.'
bcftools annotate -x ID,INFO/DP raw.vcf -o modified.vcf

2. call

call是bcftools查找变异的工具,它取代了bcftools 之前的view caller

-c, --consensus-caller #最早时的samtools/bcftools call变异用的算法
-C, --constrain alleles|trio #alleles:只call指定alleles的突变;trio:只call father-mother-child constraint的突变
-m, --multiallelic-caller #相比较刚刚提到的-c calling model,该算法更适合多等位基因变异和罕见变异的calling
-v, --variants-only #只输出变异位点的信息(如果没有发生突变,便不会输出)

3.concat

用于将多个VCF/BCF文件合并为1个。常常用来将多个染色体的vcf文件合为一个;或者将snp和indel两种不同突变类型的文件合并为1个。
注意:

  • source files如果包含多个样本的信息,样本的顺序必须一致
  • source files 根据chr和pose排过序
  • VCF文件必须是经过bgzip压缩的文件
应用举例:
bcftools concat a.sorted.vcf.gz b.sorted.vcf.gz -o ab.sorted.vcf

4. merge

同样用于合并vcf文件,merge主要是将单个样本的VCF文件合并成一个多个样本的VCF文件。

注意:

  • 所有文件中存在的sample names可以互相区分
  • 输入文件必须是经过bgzip压缩的文件
应用举例:
bcftools merge a.b.vcf.gz c.vcf.gz -o a.b.c.vcf

5. isec

该命令用于多个vcf文件之间取交集、差集、并集等操作。经典的应用场景是对多种软件的SNP calling 结果进行venn 分析。

应用举例:
bcftools isec a.vcf.gz b.vcf.gz -p dir#默认参数就是取交集
额外补充一个参数:
-C, --complement A.vcf.gz B.vcf.gz […] #求补集:只在A.vcf.gz中出现,其他几个文件中不存在

6. stats

用于统计vcf文件的基本信息,例如,突变个数、突变类型的个数、转换和颠换的个数、测序深度、INDEL长度等。同时,还可以用plot-vcfstats进行可视化处理。
如果该命令后面接了2个vcf文件,则会分别计算两个vcf文件的交集和补集(默认以position比较,倘若vcf文件内包含多个sample的突变信息,则需要通过-s-S参数载入“样本名称”)。并判断两个vcf文件的一致性(突变基因对应频率的一致性、样本基因型的一致性、以及不一致的基因型)和关联性。

应用举例:
bcftools stats a.vcf > a.stats
bcftools plot-vcfstats a.vcf -p dir #指定输出结果的目录(其中生成pdf依赖latex)
额外补充:
-v, --verbose #当输入文件为2个vcf时,输出每个样本对应的不一致位点

7. index

对bgzip压缩的bcf、vcf文件建立索引,默认索引格式为csi(coordinate-sorted index),同时支持tbi索引

bcftools index a.vcf.gz #生成csi格式索引
bcftools index -t a.vcf.gz #生成tbi格式索引

8. view

可以处理vcf和bcf文件(bcf文件为vcf文件的二进制形式),用来将vcf文件和bcf文件互相转换。也可以通过使用过滤参数或限制位置区间提取文件信息。
下面介绍几个过滤参数:
(注意⚠️:取子集的参数过滤参数 尽可能不要出现在同一个命令行)

-g, --genotype [^][hom|het|miss] #筛选指定基因型:hom-纯合;het-杂合;miss-基因缺失,其中^表示逻辑反义。eg: ^het表示排除杂合型
-k, --known #筛选已知的突变位点(ID column is not ".")
-n, --novel #筛选未知的突变位点(ID column is ".")
-v, --types snps|indels|mnps|other #筛选指定的突变类型,多个类型可以用','分隔(通过比较vcf中REF和ALT判断 )
-V, --exclude-types snps|indels|mnps|ref|bnd|other # 排除指定的突变类型,多个类型可以用','分隔(通过比较vcf中REF和ALT判断 )
-i, --include EXPRESSION #输出符合 EXPRESSION模式的位点,关于EXPRESSION的使用可以参考软件的帮助文档

举例:用公用参数-s筛选样本信息
bcftools view a.vcf.gz -s sample1,sample2 -o sample1_2.vcf
-O参数指定输出文件的类型;
-o参数指定输出文件的名字;
u代表未经压缩的BCF文件;
b代表压缩后的BCF文件;
v代表未经压缩的VCF文件;
z代表压缩后的VCF文件;

举例:压缩的vcf文件,转换为非压缩的bcf文件
bcftools view sample.vcf.gz -O u -o sample.bcf

9. query

输出指定格式的vcf和bcf文件

-f, --format FORMAT #指定输出格式

Format:
%CHROM 染色体列(POS, ID, REF, ALT, QUAL, FILTER也是类似的写法)
%GT 基因型列(e.g. 0/1)
%INFO 全部INFO列
%TYPE 突变类型(REF, SNP, MNP, INDEL, BND, OTHER)
%SAMPLE 代表样本名称
[] 对于FORMAT字段的信息,必须要中括号括起来
\t 制表符分隔,\n 换行
应用举例:
bcftools query -f '%CHROM  %POS  %REF  %ALT{0}\n' file.vcf.gz
bcftools query -f '%CHROM\t%POS\t%REF\t%ALT[\t%SAMPLE=%GT]\n' file.vcf.gz

10. sort

按照染色体位置对vcf文件排序

应用举例:
bcftools sort raw.vcf.gz -o raw.sort.vcf

11.reheader

编辑VCF/BCF文件的头部或替换VCF文件中的样本名

-h, --header FILE #编辑VCF/BCF文件的头部,输入新的VCF头部
-s, --samples FILE #替换VCF文件中的样本名,输入替换文件。形式:"old_name new_name\n",中间用"空格"隔开(同时与vcf中sample出现的顺序保持一致)
应用举例:
#编辑VCF文件的头部
bcftools reheader -h header.file raw.vcf -o header.vcf
#替换文件中的样本名
bcftools reheader -s sample.file raw.vcf -o new.vcf

#sample.file内容举例:
sample1 S1
sample2 S2
sample3 S3

应用实例补充:

待更新ing....

纯属个人理解,如有错误希望批评指正~
参考链接:
http://samtools.github.io/bcftools/bcftools.html#common_options

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章