
# DAPC code for Dyer et al. 2018 Molecular Ecology

library(adegenet) 
 
#general methods for each dataset

dapc <- dapc (x, x$pop)
# estimate the proportion of individuals assigned to the right cluster
a.score(dapc)
# then repeat dapc, varying the number of PCAs to keep.  Kept DC as N pops-1
# want to maximize the mean of a.score(dapc) 

# optimize the a-score to know how many PCs to keep
temp <- optim.a.score(dapc)

# get the vector of eigenvalues 
>dapc$eig
#get the sum of eigenvalues
>sum(dapc$eig)
# then to get the proportion for the first two just take the first and second eigenvalues and divide each by the total.
# these were placed on the X and Y axis of each graph



#------------------Subquinaria and Recens(all samples)----------------

#all_all samples  --> all loci, both species
 x <- read.structure("all_all_dapc.str")  
# 826 indiv, 30 loci, 1,2,,1,y
dapc <- dapc (x, x$pop)
# kept 40 PCs and 49 discr functions

#color by region
myCol <- c("red",
"red",
"red",
"sienna",
"sienna",
"red",
"sienna",
"dark orange",
"sienna",
"sienna",
"sienna",
"dark orange",
"red",
"red",
"red",
"sienna",
"sienna",
"red",
"dark orange",
"dark orange",
"red",
"red",
"red",
"dark orange",
"sienna",
"dodger blue",
"dodger blue",
"purple",
"darkblue",
"purple",
"dodger blue",
"dodger blue",
"dodger blue",
"blue",
"blue",
"darkblue",
"blue",
"dodger blue",
"dodger blue",
"blue",
"darkblue",
"darkblue",
"purple",
"darkblue",
"darkblue",
"purple",
"purple",
"blue",
"purple",
"dodger blue")
scatter(dapc, scree.da=TRUE, bg="white", col = myCol, pch=20, cell=0, cstar=0, solid=.6,
cex=2,clab=0, leg=FALSE, posi.da="bottomleft")


#autosomal loci for all samples
x <- read.structure("all_A_dapc.str")  
# 826 genotypes, 21 loci, 1,2,,1,y
dapc <- dapc (x, x$pop)
# kept 25 PCs and 49 DC
#use same colors and scatter as for all_all
scatter(dapc, scree.da=TRUE, bg="white", col = myCol, pch=20, cell=0, cstar=0, solid=.6,
cex=2,clab=0, leg=FALSE, posi.da="bottomleft")

#X-linked loci for both species (all samples)
x <- read.structure("all_X_dapc.str")  
# 684 genotypes, 9 loci, 1,2,,1,y
dapc <- dapc (x, x$pop)
# kept 32 PCs and 45 discr functions

#color by region for all_X. Note that R_LL, R_NO, and S_NO pops removed because they had only one sample
myCol <- c("red",
"red",
"red",
"sienna",
"sienna",
"red",
"sienna",
"dark orange",
"sienna",
"sienna",
"sienna",
"red",
"red",
"red",
"sienna",
"red",
"dark orange",
"dark orange",
"red",
"red",
"red",
"dark orange",
"sienna",
"dodger blue",
"dodger blue",
"purple",
"darkblue",
"purple",
"dodger blue",
"dodger blue",
"dodger blue",
"blue",
"blue",
"darkblue",
"blue",
"dodger blue",
"blue",
"darkblue",
"darkblue",
"purple",
"darkblue",
"darkblue",
"purple",
"purple",
"blue",
"purple",
"dodger blue")
#plot 
scatter(dapc, scree.da=TRUE, bg="white", col = myCol, pch=20, cell=0, cstar=0, solid=.6,
cex=2,clab=0, leg=FALSE, posi.da="bottomleft")
 
 ##---------------- Subquinaria only -------------------  
 
#  Subquinaria all loci
x <- read.structure("subq_all_dapc.str")  
#472 genotypes, 30 loci, 1,2,,1,y
dapc <- dapc (x, x$pop)
# keep 20 PCs, 24 DCs

#color by population
myCol <- c(
"slate blue",
"dark violet",
"tomato",
"rosy brown",
"orange",
"maroon",
"magenta",
"dark magenta",
"tan",
"navy",
"chocolate",
"dodger blue",
"violet",
"pale violet red",
"turquoise",
"dark goldenrod",
"dark red",
"goldenrod",
"red",
"orange",
"gold",
"sandy brown",
"blue",
"sienna",
"deep pink")
scatter(dapc, scree.da=TRUE, bg="white", col = myCol, pch=20, cell=0, cstar=0, solid=.8,
cex=2,clab=1, leg=TRUE, posi.da="bottomleft")


#  Subquinaria autosomes
x <- read.structure("subq_A_dapc.str")  
#472 genotypes, 21 loci,  1,2,,1,y
dapc <- dapc (x, x$pop)
# 20 PCs, 24 DCs

#Plot with the same colors as for all loci
scatter(dapc, scree.da=TRUE, bg="white", col = myCol, pch=20, cell=0, cstar=0, solid=.8,
cex=2,clab=1, leg=TRUE, posi.da="bottomleft")

#  Subquinaria X-linked
x <- read.structure("subq_X_dapc.str")  
#426 genotypes, 9 loci
dapc <- dapc (x, x$pop)
# 30 PCs, 23 DCs

# colors for subq X-loci; exclude pop NO because only one sample
myCol <- c(
"slate blue",
"dark violet",
"tomato",
"rosy brown",
"orange",
"maroon",
"magenta",
"dark magenta",
"tan",
"navy",
"chocolate",
"dodger blue",
"pale violet red",
"turquoise",
"dark goldenrod",
"dark red",
"goldenrod",
"red",
"orange",
"gold",
"sandy brown",
"blue",
"sienna",
"deep pink")

scatter(dapc, scree.da=TRUE, bg="white", col = myCol, pch=20, cell=0, cstar=0, solid=1,
cex=2,clab=1, leg=TRUE, posi.da="bottomleft")

#----------------Recens only --------------
# D. recens all loci
x <- read.structure("rec_all_dapc.str")  
#354 genotypes, 30 loci, 1,2,,1,y
dapc <- dapc (x, x$pop)
# kept 40 PCs, 24 DCs  

#color by population
myCol <- c(
"red",
"rosy brown",
"chocolate",
"blue",
"dodger blue",
"dark goldenrod",
"dark violet",
"maroon",
"deep sky blue",
"navy",
"slate blue",
"deep pink",
"gold",
"goldenrod",
"tan",
"light slate grey",
"dark slate blue",
"tomato",
"violet",
"magenta",
"sienna",
"orange",
"dark red",
"dark magenta",
"cyan"
)
scatter(dapc, scree.da=TRUE, bg="white", col = myCol, pch=20, cell=0, cstar=0, solid=1,
cex=2,clab=1, leg=TRUE, posi.da="bottomleft")

# D. recens autosomal loci
x <- read.structure("rec_A_dapc.str")  
#354 genotypes, 21 loci, 1,2,,1,y
dapc <- dapc (x, x$pop)
# kept 30 PCs, 24 DCs
# plot with same colors as for recens all loci
scatter(dapc, scree.da=TRUE, bg="white", col = myCol, pch=20, cell=0, cstar=0, solid=1,
cex=2,clab=1, leg=TRUE, posi.da="bottomleft")


# D. recens X-linked loci
x <- read.structure("rec_X_dapc.str")  
#258 genotypes, 9 loci, 1,2,,1,y
dapc <- dapc (x, x$pop)
# kept 15 PCs, 22 DCs

#color by population for recens X-loci (LL and NO excluded because only one sample)
myCol <- c(
"red",
"rosy brown",
"chocolate",
"blue",
"dodger blue",
"dark goldenrod",
"dark violet",
"maroon",
"deep sky blue",
"navy",
"slate blue",
"gold",
"goldenrod",
"tan",
"dark slate blue",
"tomato",
"violet",
"magenta",
"sienna",
"orange",
"dark red",
"dark magenta",
"cyan")
scatter(dapc, scree.da=TRUE, bg="white", col = myCol, pch=20, cell=0, cstar=0, solid=1,
cex=2,clab=1, leg=TRUE, posi.da="bottomleft")

