forked from z0on/2bRAD_denovo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_admixture_v5_function.R
58 lines (56 loc) · 1.65 KB
/
plot_admixture_v5_function.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
plotAdmixture=function(data,npops,colors=NA,ord=NULL,angle=0,space=0,grouping.method="distance",vshift=0,hshift=0,...) {
#data=tbl;npops=2;colors=NA;angle=0;ord=NULL;cluster.method="average"
tbl=data
require(colorspace)
require(RColorBrewer)
if (is.na(colors[1])){
colors=c("tomato", "lightblue", "wheat","olivedrab", "cyan3","hotpink","gold","orange")
# colors=diverge_hcl(n=npops,h = c(270, 0), c = 100, l = c(50, 90), power = 1, fixup = TRUE)
# colors=heat_hcl(n=npops,h = c(260, 0), c = 100, l = c(50, 90), power = 1, fixup = TRUE)
}
p=levels(data$pop)[1]
tsort=c()
if (is.null(ord)) {
for(p in levels(tbl$pop)){
s=subset(tbl,pop==p)
# dd=dist(s[,1:npops])
# dd[dd<0]=0
# dd=1-dd
if (nrow(s)>2) {
if (grouping.method=="distance") {
cl=hclust(dist(s[,1:npops]))
} else {
if (grouping.method=="correlation") {
dd=cor(t(s[,1:npops]))
dd[dd<0]=0
dd=1-dd
cl=hclust(as.dist(dd))
} else { stop("grouping.method not recognized\n") }
}
sorted=cl$labels[cl$order]
s=s[sorted,]
}
tsort=data.frame(rbind(tsort,s))
}
} else {
tsort=tbl[ord,]
}
midpts=barplot(t(as.matrix(tsort[1:npops])), col=colors,xlab="", space=space,ylab="Ancestry", border=NA, xaxt="n",mgp=c(2,1,0))
pops=unique(as.character(tsort$pop))
np=0;lp=0;lpoints=c()
abline(v=0)
abline(h=1,lwd=1)
abline(h=0,lwd=1)
for( p in pops) {
np0=table(tsort$pop==p)[2]
lp0=np0/2
lp=np+lp0
np=np+np0
lpoints=append(lpoints,lp)
abline(v=np)
}
text(as.numeric(lpoints)+3.5+hshift,par("usr")[3]-0.1-vshift,labels=pops,xpd=TRUE, srt=angle, pos=2,...)
row.names(tsort)=tsort$ind
# return(tsort)
return(row.names(tsort))
}