Sort VCF by Chr and Pos根据染色体位置对VCF进行排序

对VCF文件中的突变按照染色体和位置进行排序,下面是本人的总结,其中利用bash命令的方法不依赖其他的工具或包。htslib前文中也提到过。

1, Use bash

bash raw.vcf

1
2
3
4
5
6
7
8
9
chr_order="chrMnchr1nchr2nchr3nchr4nchr5nchr6nchr7nchr8nchr9nchr10nchr11nchr12nchr13nchr14nchr15nchr16nchr17nchr18nchr19nchr20nchr21nchr22nchrXnchrY"

cat "$1" ' grep "^#" > .header.vcf
cat "$1" ' grep -v "^#" ' sort -k1,1 -k2,2n > .pre.sorted.vcf
echo -e $chr_order ' while read line
do
    cat .pre.sorted.vcf ' grep "^$line"$'t' >> .header.vcf
done
mv .header.vcf  sorted.vcf && rm .header.vcf .pre.sorted.vcf

2,Use awk and sed

(awk ‘/^#/{print}!/^#/{exit}’ raw.vcf;sed ‘/^#/d’ raw.vcf’awk -F"\t" ‘($1~/^[0-9]+$/){sub("^chr","",$0);print $0}‘‘sort -k1,1n -k2,2n’awk ‘{print “chr”$0}’ ;sed ‘/^#/d’ raw.vcf’ awk -F"\t" ‘($1!~/^[0-9]+$/){sub("^chr","",$0);print $0}‘‘sort -k1,1d -k2,2n’awk ‘{print “chr”$0}') > sort.vcf

3, Use Picard

Sorts one or more VCF files. This tool sorts the records in VCF files according to the order of the contigs in the header/sequence dictionary and then by coordinate. It can accept an external sequence dictionary.

java -jar picard.jar SortVcf I=unsort.vcf O=sorted.vcf

4,Use vcf-sort (in vcftools)

cat file.vcf ' vcf-sort > sorted.vcf

合并VCF文件 Merge VCF file

VCF为variant call file格式,现为标准的SNV突变存储格式。通常情况下,一个VCF文件对应一个样本的突变。但VCF格式同样支持同时在一个文件中表示多个样本的突变,在每一行的最后几列,每一列代表一个样本在特定位点的突变情况,若样本在该位点没有突变,在代表样本的那一列在该位点的记录为.点号。 此外,在生成VCF的时候,可以生成包含多个样本突变的VCF。但是要在序列比对生成SAM文件时要加入SM tag用来指定哪些reads属于哪个样本。

多个样本的突变情况用一个VCF文件存储的好处在于,对于发生突变的特定位点,可以迅速了解不同样本在该位点的突变情况。比如下列,最后三列表示三个样本在染色体chrT的525位点的突变情况,

1
2
#对应GT:PL:GQ:AD:DP。其中第一个样本在该位点没有发生突变,第二个样本的基因型为C/C,第三个样本的基因型为G/T。
chrT    515    .    G    C,T    1230.23    PASS    AC=4;AF=1.00;AN=4;DP=76;FS=0.000;MLEAC=2;MLEAF=1.00;MQ0=0;MQ=60.24;QD=33.32;RPA=5,4;RU=CA;SF=1,2;SOR=0.793;STR;set=variant23    GT:PL:GQ:AD:DP    .    1/1:1570,120,0:99:0,40:46    0/2:965,69,0:69:0,23:30

一,vcftools

VCFtools is a program package designed for working with VCF files, such as those generated by the 1000 Genomes Project. The aim of VCFtools is to provide easily accessible methods for working with complex genetic variation data in the form of VCF files.

VCFtools的功能和SAMtools类似,用于处理VCF格式的文件,可用于合并VCF。

VCFtools的输入文件格式为gz格式,需要用bgzip压缩,并用tabix生成index文件。 tabix和bgzip在samtools安装包的htslib-*文件夹中,需要单独安装。首先下载samtools(也可以专门下载htslib包,或者你已经有samtools,看是否有htslib文件夹),进入htslib文件夹中

1
2
3
4
5
6
wget https://github.com/samtools/htslib/releases/download/1.3/htslib-1.3.tar.bz2
tar -jxvf htslib-1.3.tar.bz2
cd htslib-1.3
./configure 
make
make prefix=/path/to/install install

download vcftools,install vcftools

1
2
3
4
5
6
git clone https://github.com/vcftools/vcftools.git
cd vcftools/ 
./autogen.sh 
./configure 
make 
make install

compress, index and merge VCF

1
2
3
4
5
6
7
bgzip -c "x.vcf" > "x.vcf.gz"
bgzip -c "y.vcf > "y.vcf.gz"
bgzip -c "z.vcf" > "z.vcf.gz"
tabix -f "x.vcf.gz"
tabix -f "y.vcf.gz"
tabix -f "z.vcf.gz"
vcf_merge   x.vcf.gz   y.vcf.gz   z.vcf.gz  > merged.vcf

Get the reference allele based on genomic position

This post will show how to get the reference base of chr1 from 49999 to 500001 (Version: hg19). Please note: different tools has different coordinate (0 start or 1 start).

1, SAMtools

Index reference sequence in the FASTA format or extract subsequence from indexed reference sequence. If no region is specified, faidx will index the file and create .fai on the disk. If regions are specified, the subsequences will be retrieved and printed to stdout in the FASTA format.

1
2
3
$samtools faidx ucsc.hg19.fasta chr1:49999-50001
>chr1:49999-50001
ATA

2, twoBitToFa

1
2
3
4
5
6
7
$wget http://hgdownload.soe.ucsc.edu/admin/exe/linux.x86_64/faToTwoBit
$wget http://hgdownload.soe.ucsc.edu/admin/exe/linux.x86_64/twoBitToFa
$chmod 755 twoBitToFa faToTwoBit 
$faToTwoBit ucsc.hg19.fasta ucsc.hg19.2bit
$twoBitToFa ucsc.hg19.2bit -seq=chr1 -start=49998 -end=50001 temp.out && cat temp.out && rm temp.out
>chr1:49998-50001
ATA

3, UCSC DAS server

1
2
$wget -qO- http://genome.ucsc.edu/cgi-bin/das/hg19/dna?segment=chr1:49999,50001 | grep -v '<'
ata

利用biomaRt包下载HGMD公开版的突变位点

前面文章介绍了Ensembl的biomart,相信你对biomart应该有所了解了,在此再介绍一种方法,即通过R语言包biomaRt下载HGMD的数据。

HGMD的最新数据是需要购买授权才行,公开版信息不仅滞后,而且不能下载,不能得到基因组位置,在biostart上看到有人说Ensembl整合了HGMD的公开版,心想能获得公开版的数据也不错,于是采用biomaRt包下载。

各位不要高兴,最终的结果是,只得到了所有突变的基因组位置,未能下到具体的突变类型,以及与表型的关系。不过能下载基因组的位置,也算不错,结合对这些位置的注释,能获取不少信息。如果您对这些位置的利用有更多或者更好的想法,欢迎与我讨论。

1,安装biomaRt包

1
2
3
4
source("http://bioconductor.org/biocLite.R")
chooseCRANmirror()
chooseBioCmirror()
biocLite("biomaRt")

2,显示ensembl的biomart

1
2
3
4
library(biomaRt)
listEnsembl()
#如果要显示特定版本的,添加version参数
#listEnsembl(version=78)

安装BioMart Perl及利用BioMart Perl API下载数据

上篇介绍了如何利用ensembl的biomart服务下载ensembl gene id与NCBI entrez gene id的对应关系时,最后一步是保存result。biomart也提供通过biomart-perl,在本地通过perl脚本下载,并通过标准输出到终端上。biomart提供生成好的perl脚本,只需在选择好相关attribute和filter之后,点击中间上方的perl即可。

biomart-perl-api