library(adegenet) library(hierfstat) rm(list = ls()) ## get number of individuals (n_ind) and number of loci (n_loc) ## ## skip the first line, since STRUCTURE wants only allele labels and other ## lines include inidividual number and pop ## wang <- read.table("wang-et-al.stru", header = FALSE, skip = 1) ## the STRUCTURE file has 2 lines per individuals, so the number of ## individuals is the number of rows divided by 2 ## n_ind = nrow(wang)/2 ## the STRUCTURE file has 1 column with the individual index and a 2nd ## column with the group number, so the number of loci is the number of ## columns minus 2 ## n_loc = ncol(wang) - 2 ## get group, area, and continent labels ## wang_lbl <- read.table("wang-et-al.pop", header = TRUE) ## read data and labels into genind object ## wang_fst <- read.structure(file = "wang-et-al.stru", n.ind = n_ind, n.loc = n_loc, onerowperind = FALSE, col.lab = 1, col.pop = 2, col.others = NULL, row.marknames = 1, NA.char = "-9", pop = NULL, sep = NULL, ask = FALSE, quiet = TRUE) other(wang_fst)$continent <- wang_lbl$continent other(wang_fst)$area <- wang_lbl$area other(wang_fst)$group <- wang_lbl$group ## wang_levels contains information for each individual indicating ## which continent, area, and group they belong to ## wang_levels <- data.frame(other(wang_fst)) ## wang_loci contains information about the genotype of each individual ## the strange 2nd line is necessary to make sure the alleles are ## treated as number rather than as characters ## wang_loci <- genind2df(wang_fst)[, -1] wang_loci <- data.frame(lapply(wang_loci, as.numeric))