library(vcfR)library(ggplot2)library(SNPfiltR)library(StAMPP)library(adegenet)library(dplyr)v<-read.vcfR("~/Downloads/yhpa_final_filtered.vcf.gz")samps<-read.csv("~/Downloads/Table1.for.manuscript.csv")#make new columnsamps$pop<-samps$Subspecies#subset sampling file and fix pop IDssamps<-samps[samps$sample_ID %in%colnames(v@gt),]popmap<-samps
Code
#labeled pca#convert each to genlightgena<-vcfR2genlight(v)#perform PCAdi.pca<-glPca(gena, nf=4)#isolate PCA scores as a dataframedi.pca.scores<-as.data.frame(di.pca$scores)#check if sample order matches in vcf versus sampling tablerownames(di.pca.scores) == popmap$sample_ID
#reorder sampling file to match alphabetical orderpopmap<-popmap %>%slice(order(factor(sample_ID, levels =rownames(di.pca.scores))))#check that they all matchrownames(di.pca.scores) == popmap$sample_ID