需求
客戶的一個簡單需求:
我有一批功能基因位點,想從重測序的群體材料中找到這些位點,如何批量快速獲得?
示例文件
gene.txt
test.vcf
代碼實現
run.sh
cat $1 |while read gene chr from to
do
#echo $chr $from $to
if echo $2 |grep -q '.*.vcf.gz$';then
vcftools --gzvcf $2 --chr $chr --from-bp $from --to-bp $to --recode --recode-INFO-all --out $gene.$chr.$from-$to
elif echo $2 |grep -q '.*.vcf$';then
vcftools --vcf $2 --chr $chr --from-bp $from --to-bp $to --recode --recode-INFO-all --out $gene.$chr.$from-$to
fi
done
運行sh run.sh gene.txt test.vcf
,或sh run.sh gene.txt test.vcf.gz
生成結果:
補充說明
以上代碼中利用了vcftools工具,以及shell中讀取每行文件的每個字段進行賦值。
vcftools還能提取某個具體位置的SNP:
vcftools --gzvcf test.vcf.gz --positions specific_position.txt --recode --out specific_position.vcf
specific_position.txt文件格式如下:
1 842013
1 891021
1 903426
1 949654
1 1018704
除了vcftools,bcftools和plink等工具也能實現類似的功能。
bcftools filter test.vcf.gz --regions 9:4700000-4800000 > out.vcf
但bcftools要求vcf必須是gz格式,如不是,則需要進行轉化(直接用gzip不行):
bcftools view test.vcf -Oz -o test.vcf.gz
bcftools index test.vcf.gz
需要格外注意的是,vcf中的染色體名稱要和提取文件中的染色體名保持一致,如Chr1或chr1或1。
或者:
bcftools view -S keep.list test.vcf >sub_indv.vcf
keep.list可以是“染色體+具體位置”兩列,也可以是“染色體+起始+終止”三列:
chr1 27639
chr1 60383
chr2 60469
chr3 60516
chr4 60534
#或者
chr1 1 1000
chr1 2000 4500
在plink中,可以指定特定的樣本(keep)或SNP(extract)。
指定樣本提取:
plink --bfile file --noweb --keep sampleID.txt --recode --make-bed --out sample
sampleID.txt第一列為提取的樣本Family ID,第二列為Within-family ID(IID)。
指定位點提取:
plink --bfile file --extract snp.txt --make-bed --out snp
snp.txt文件中一個SNP名稱一行。
Ref:https://www.cnblogs.com/chenwenyan/p/9151672.html
https://blog.csdn.net/weixin_34387468/article/details/94519445
https://www.cnblogs.com/mmtinfo/p/11945592.html
https://www.cnblogs.com/chenwenyan/p/8991417.html