SNPRelate:对给定区域snp做PCA分析

目标:

如题

数据:

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。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容