interpX <- seq(min(x),max(x),le=400)
interpY <- seq(min(y),max(y),le=400)
pc1 <- interp(x, y, rupica.spca1$ls[,1], xo=interpX, yo=interpY, duplicate="mean")
pc2 <- interp(x, y, rupica.spca1$ls[,2], xo=interpX, yo=interpY, duplicate="mean")

pc1
image(pc1)
image(pc2)
pc1$z
image(pc1)

image(pc1, color.pal=colorRampPalette(c("red","white")))
image(pc1, color.pal=colorRampPalette(c("red","white")))
?image
image(pc1, color=colorRamp(c("red","white"),20))
?colorRamp
colorRamp(c("red","white")
)
image(pc1, col=colorRampPalette(c("red","white"))(20))
image(pc1, col=transp(colorRampPalette(c("red","white"))(20)))
image(pc2, col=transp(colorRampPalette(c("blue","white"))(20)), add=TRUE)
points(other(rupica)$xy)
image(pc1, col=transp(colorRampPalette(c("red","white"))(20)))
image(pc1, col=transp(colorRampPalette(c("red","white"))(30)))
image(pc1, col=transp(colorRampPalette(c("red","white"))(100)))
image(pc2, col=transp(colorRampPalette(c("blue","white"))(100)), add=TRUE)
Sweave("/home/thibaut/dev/adegenet/pkg/inst/doc/adegenet-spca.Rnw")
temp <- loadingplot(myLoadings, threshold=quantile(myLoadings, 0.95),
                    xlab="Alleles",ylab="Weight of the alleles",
                    main="Contribution of alleles \n to the first sPCA axis",
                    fac=pop(obj), cex.fac=0.6)
temp
temp <- loadingplot(myLoadings, threshold=quantile(myLoadings, 0.95),
                    xlab="Alleles",ylab="Weight of the alleles",
                    main="Contribution of alleles \n to the first sPCA axis",
                    fac=obj$loc.fac, cex.fac=0.6)

Sweave("/home/thibaut/dev/adegenet/pkg/inst/doc/adegenet-spca.Rnw")
Sweave("/home/thibaut/dev/adegenet/pkg/inst/doc/adegenet-spca.Rnw")
q()
n
Sweave("/home/thibaut/dev/adegenet/pkg/inst/doc/adegenet-dapc.")
Sweave("/home/thibaut/dev/adegenet/pkg/inst/doc/adegenet-dapc.Rnw<")
Sweave("/home/thibaut/dev/adegenet/pkg/inst/doc/adegenet-dapc.Rnw")
?find.cluster
library(adegenet)
?find.cluster
?find.clusters
?read.genetix
getwd()
Sweave("/home/thibaut/dev/adegenet/pkg/inst/doc/adegenet-basics.Rnw")
q()
n
Sweave("/home/thibaut/dev/adegenet/pkg/inst/doc/adegenet-basics.Rnw")
Sweave("/home/thibaut/dev/adegenet/pkg/inst/doc/adegenet-dapc.Rnw")
traceback()
scatter(dapc1, ratio.pca=0.3, bg="white", pch=20,  cell=0, cstar=0, col=myCol, solid=.4,
        cex=3, clab=0, mstree=TRUE, scree.da=FALSE,
        posi.pca="bottomright", leg=TRUE, txt.leg=paste("Cluster",1:6))

par(xpd=TRUE)
points(dapc1$grp.coord[,1], dapc1$grp.coord[,2], pch=4, cex=3, lwd=8, col="black")
points(dapc1$grp.coord[,1], dapc1$grp.coord[,2], pch=4, cex=3, lwd=2, col=myCol)

myInset <- function(){
    temp <- dapc1$pca.eig
    temp <- 100* cumsum(temp)/sum(temp)
    plot(temp, col=rep(c("black","lightgrey"), c(dapc1$n.pca,1000)), ylim=c(0,100),
         xlab="PCA axis", ylab="Cumulated variance (%)", cex=1, pch=20, type="h", lwd=2)
}

add.scatter(myInset(), posi="bottomright", inset=c(-0.03,-0.01), ratio=.28, bg=transp("white"))
scatter(dapc1, ratio.pca=0.3, bg="white", pch=20,  cell=0, cstar=0, col=myCol, solid=.4,
        cex=3, clab=0, mstree=TRUE, scree.da=FALSE,
        posi.pca="bottomright", leg=TRUE, txt.leg=paste("Cluster",1:6))

dapc1
scatter(dapc1, ratio.pca=0.3, bg="white", pch=20,  cell=0, cstar=0, col=myCol, solid=.4,
        cex=3, clab=0, mstree=TRUE, scree.da=FALSE,
        posi.pca="bottomright", leg=TRUE, txt.leg=paste("Cluster",1:6))

scatter(dapc1, ratio.pca=0.3, bg="white", pch=20,  cell=0, cstar=0, col=myCol, solid=.4)
dev.off()
scatter(dapc1, ratio.pca=0.3, bg="white", pch=20,  cell=0, cstar=0, col=myCol, solid=.4)
scatter(dapc1, ratio.pca=0.3, bg="white", pch=20,  cell=0, cstar=0, col=myCol, solid=.4,
        cex=3, clab=0, mstree=TRUE, scree.da=FALSE,
        posi.pca="bottomright", leg=TRUE, txt.leg=paste("Cluster",1:6))

traceback()
scatter(dapc1, ratio.pca=0.3, bg="white", pch=20,  cell=0, cstar=0, col=myCol, solid=.4,
        cex=3, clab=0, mstree=FALSE, scree.da=FALSE,
        posi.pca="bottomright", leg=TRUE, txt.leg=paste("Cluster",1:6))

scatter(dapc1, ratio.pca=0.3, bg="white", pch=20,  cell=0, cstar=0, col=myCol, solid=.4,
        cex=3, clab=0, mstree=TRUE, scree.da=FALSE,
        posi.pca="bottomright", leg=TRUE, txt.leg=paste("Cluster",1:6))

debug(scatter.dapc)
scatter(dapc1, ratio.pca=0.3, bg="white", pch=20,  cell=0, cstar=0, col=myCol, solid=.4,
        cex=3, clab=0, mstree=TRUE, scree.da=FALSE,
        posi.pca="bottomright", leg=TRUE, txt.leg=paste("Cluster",1:6))

mstree
require(ade4)
D
scatter(dapc1, ratio.pca=0.3, bg="white", pch=20,  cell=0, cstar=0, col=myCol, solid=.4,
        cex=3, clab=0, mstree=TRUE, scree.da=FALSE,
        posi.pca="bottomright", leg=TRUE, txt.leg=paste("Cluster",1:6))

mtree(D)
mstree(D)
ade4::mstree(D)
mstree
ape::mstree
tre <- ade4::mstree(D)
Q()
q()
n
library(tools)
resaveRdaFiles("/home/thibaut/dev/adegenet/pkg/inst/doc/Robjects/dapcGL.RData")
?resaveRdaFiles
checkRdaFiles(dir("/home/thibaut/dev/adegenet/pkg/inst/doc/Robjects/"))
dir("/home/thibaut/dev/adegenet/pkg/inst/doc/Robjects/")
dir("/home/thibaut/dev/adegenet/pkg/inst/doc/Robjects/", full=TRUE)
checkRdaFiles(dir("/home/thibaut/dev/adegenet/pkg/inst/doc/Robjects/", full=TRUE))
q()
n
library(adegenet)
x <- glSim(100, 5e4, 50)

dapc1 <- dapc(x, n.pca=20, n.da=1)

example(dapc)
data(H3N2)
pop(H3N2) <- factor(H3N2$other$epid)
dapc1 <- dapc(H3N2, var.contrib=FALSE, scale=FALSE, n.pca=150, n.da=5)

scatter(dapc1, cell=0, pch=18:23, cstar=0, mstree=TRUE, lwd=2, lty=2)

scatter(dapc1, cell=2, pch="", cstar=0, posi.da="top",
lab=paste("year\n",2001:2006), axesel=FALSE, col=terrain.colors(10))


x <- glSim(50,4e3-50, 50, ploidy=2)
x
plot(x)

dapc1 <- dapc(x, n.pca=10, n.da=1)
dapc1

scatter(dapc1, da.scree=FALSE)

scatter(dapc1, scree.da=FALSE)
loadingplot(dapc1$var.contr)
loadingplot(tail(dapc1$var.contr, 100), main="Loading plot - last 100 SNPs")


data(sim2pop)

## we make a dataset of:
## 30 individuals from pop A
## 30 individuals from pop B
## 30 hybrids

## separate populations and make F1
temp <- seppop(sim2pop)
temp <- lapply(temp, function(e) hybridize(e,e,n=30)) # force equal popsizes

## make hybrids
hyb <- hybridize(temp[[1]], temp[[2]], n=30)

## repool data - needed to ensure allele matching
newdat <- repool(temp[[1]], temp[[2]], hyb)
pop(newdat) <- rep(c("pop A", "popB", "hyb AB"), c(30,30,30))

## perform the DAPC on the first 2 pop (60 first indiv)
dapc1 <- dapc(newdat[1:60],n.pca=5,n.da=1)

scatter(dapc1)

scatter(dapc1, scree.da=FALSE)

args(inbreeding
)
data(microbov)

## isolate Lagunaire breed
lagun <- seppop(microbov)$Lagunaire

## estimate inbreeding - return sample of F values
Fsamp <- inbreeding(lagun, N=30)


Fsamp <- inbreeding(lagun, N=200)
invisible(sapply(Fsamp[1:10], function(e) plot(density(e), xlab="F", xlim=c(0,1), main="Density of the sampled F values")))

data(H3N2)
dapc1 <- dapc(H3N2, pop=H3N2$other$epid, n.pca=30,n.da=6)

## defautl plot ##
scatter(dapc1)

## showing different scatter options ##
## remove internal segments and ellipses, different pch, add MStree
scatter(dapc1, pch=18:23, cstar=0, mstree=TRUE, lwd=2, lty=2, posi.da="topleft")

## only ellipse, custom labels, use insets
scatter(dapc1, cell=2, pch="", cstar=0, posi.pca="topleft", posi.da="topleft", scree.pca=TRUE,
inset.pca=c(.01,.3), lab=paste("year\n",2001:2006), axesel=FALSE, col=terrain.colors(10))

## without ellipses, use legend for groups
scatter(dapc1, cell=0, cstar=0, scree.da=FALSE, clab=0, cex=3, solid=.4, bg="white", leg=TRUE, posi.leg="topleft")

## only one axis
scatter(dapc1,1,1,scree.da=FALSE, legend=TRUE, solid=.4,bg="white")

q()
n
