今天推文的主要内容参考 链接 https://wurmlab.github.io/genomicscourse/2016-SIB/practicals/population_genetics/popgen,示例vcf格式数据下载自https://github.com/wurmlab/genomicscourse/tree/master/2016-SIB/data/popgen/vcf, 大家可以自己到链接下载示例数据,也可以给这篇推文点赞留言获取数据
bcftools view snp.vcf.gz scaffold_1 > popgenome-vcf/scaffold_1
bcftools view snp.vcf.gz scaffold_2 > popgenome-vcf/scaffold_2
如果当前目录下只有vcf格式文件,会遇到报错Failed to open .vcf.gz: could not load index
,可以参考 https://www.cnblogs.com/chenwenyan/p/11945445.html
tabix -p vcf snp.vcf.gz
如果当前目录下没有popgenome-vcf
这个目录,还需要新建目录
mkdir popgenome-vcf
今天参考的文章里写道 In theory, the r PopGenome can read VCF files directly, using the readVCF function. However, because our samples are haploid, we need to use a different function, r readData, which requires a folder with a separate VCF for each scaffold. 这个是为什么呢?
#install.packages("PopGenome")
library(PopGenome)
getwd()
setwd("VCF/")
snp<-readData("popgenome-vcf",format = "VCF")
get.sum.data(snp)
image.png
这里可以直接统计 转换和颠换的比例
pops<-get.individuals(snp)[[1]]
pop1<-pops[grep("B\\.bam",pops)]
pop2<-pops[grep("b\\.bam",pops)]
pop1
pop2
snp<-set.populations(snp,list(pop1,pop2))
snp@populations
snp<-F_ST.stats(snp)
get.F_ST(snp)
image.png
这里的指标都是什么意思呢?
get.diversity(snp)[[1]]
这里的指标也看不懂是什么意思呀
win_snp<-sliding.window.transform(snp,
width = 10000,
jump = 2000,type = 2)
win_snp<-F_ST.stats(win_snp)
win_snp@nucleotide.F_ST
win_snp@nuc.diversity.within
library(ggplot2)
win_fst <- data.frame(x=1:dim(win_snp@nucleotide.F_ST)[1],
y=win_snp@nucleotide.F_ST[,1])
head(win_fst)
p1<-ggplot(win_fst,aes(x=x,y=y))+
geom_point()+
geom_line()+
theme_bw()+
theme(panel.grid = element_blank())+
scale_x_continuous(breaks = win_fst$x,
labels = win_fst$x)+
labs(x=NULL,y=NULL,title = "FST")
ggsave("FST.pdf",p1,width = 15,height = 4)
image.png
bb_div <- win_snp@nuc.diversity.within[,1] # diversity among B (bb = "big B")
lb_div <- win_snp@nuc.diversity.within[,2] # diversity among B (lb = "little b")
bb_div
df1<-data.frame(x=1:length(bb_div),y=bb_div)
df2<-data.frame(x=1:length(lb_div),y=lb_div)
p2<-ggplot()+
geom_line(data=df1,aes(x=x,y=y),color="red")+
geom_point(data=df1,aes(x=x,y=y),size=2,color="red")+
geom_line(data=df2,aes(x=x,y=y),color="blue")+
geom_point(data=df2,aes(x=x,y=y),size=2,color="blue")+
theme_bw()+labs(x=NULL,y=NULL)
ggsave("diversity.pdf",p2,width = 15,height = 4)
image.png