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

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