目标:
如题
数据:
pombe_65_2dxm_strains.passed_snps_select1.vcf(GATK 分析产生的vcf文件)
工具:
R package: gdsfmt, SNPRelate, ggplot2,ggrepel
数据预处理:
从vcf文件中选择特定区域的snp sites:
cat pombe_65_2dxm_strains.passed_snps_select1.vcf |awk '{if(NR==52|| (NR>=1726 && NR<=2000)) print $0;}' >test.vcf
####NR==52: 保留 '#CHROM' 这行,这行包含sampleid
用SNPRelate进行PCA分析:
library(gdsfmt)
library(SNPRelate)
vcf.fn <- "test.vcf"
snpgdsVCF2GDS(vcf.fn, "test.gds", method="biallelic.only")
####这一步将vcf格式转换为gds格式,以符合SNPRelate对数据格式的要求;
genofile <- snpgdsOpen("test.gds")
####打开文件
pca <- snpgdsPCA(genofile, autosome.only=FALSE,num.thread=2)
###用SNPRelate中的snpgdsPCA函数进行PCA分析。autosome.only=FALSE,用于处理染色体非常规编号,例如本文件中染色体编号为“III”,不加此参数时,所有snp位点均被过滤;
ggplot作图:
library(ggplot2)
library(ggrepel)
pcadf<-data.frame(pca$eigenvect[,1],pca$eigenvect[,2],sampleid=pca$sample.id)
####存储为data.frame
ggplot(pcadf, aes(x = pca.eigenvect...1., y = pca.eigenvect...2.)) +
+ geom_point(size=3)+geom_text_repel(aes(label = sampleid), size = 2)
####"geom_text_repel"用于避免数据标签的重叠;
最终结果:

NOTE:如需要标注PC1,PC2对差异解释的比例,打印pca$varprop。