## DAPC by R
## format of data is for "structure"


library(adegenet)	# install package

all <- read.structure("Mozalb_GMdata.str", n.ind=977, n.loc=13, row.marknames=1, col.pop=2)       # read the data sheet

		# Which column contains labels for genotypes ('0' if absent)? 
		# 1
		#
		#  Which other optional columns should be read (press 'return' when done)? 
		# 1: 
		#
		# Are genotypes coded by a single row (y/n)? 
		# n
		#
		# Converting data from a STRUCTURE .stru file to a genind object... 


all		# confirm the data structure

clust1 <- find.clusters(all, max.n.clust=56)  # PCA and set clusers
# Choose the number PCs to retain (>= 1): 
#   100
# Choose the number of clusters (>=2): 
#   12

dapc1 <- dapc(all, clust1$grp)   # DAPC 
# Choose the number PCs to retain (>=1): 
#   100
# Choose the number discriminant functions to retain (>=1): 
#   3

dapc1    # the results of DAPC

scatter(dapc1)   # show the graph of the results with chosen number of clusters (12 at this time)


dapc2 <- dapc(all, n.pca=100, n.da=3)  # DAPC without setting the number of clusters, which will show the geographical grouping
dapc2
scatter(dapc2, scree.pca=TRUE, posi.pca="bottomleft", cstar=0)
 