-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.r
216 lines (191 loc) · 6.78 KB
/
tools.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# Throughout, we assume bi-allelic data
bootstrap.sample <- function(test.sample, training.sample, training.samLoc, lim="MN") {
locM = 1:ncol(training.sample)
locN = 1:nrow(training.sample)
if(lim=="M" || lim=="MN") locM = sample(ncol(training.sample), ncol(training.sample), replace = TRUE)
if(lim=="N" || lim=="MN") locN = sample(nrow(training.sample), nrow(training.sample), replace = TRUE)
list(test.sample=test.sample[locM], training.sample = training.sample[locN,locM], training.samLoc = training.samLoc[locN])
}
# compute coordinates in the triangle plot
coords<-function(vec) {
x = vec[1]
y = vec[2]
z = vec[3]
x*c(0,sqrt(3)-1/2) + y*c(-1,-1/2) + z*c(1,-1/2)
}
# compute frequencies from sample along samLoc
getfreqs = function(sample, samLoc){
t(simplify2array(by(sample,samLoc,colMeans,na.rm=TRUE))) / 2
}
# sample has nss columns and ninds rows; entries are 0, 1, 2
# freqs has npops rows and nss columns
# countsAncestral is a vector of length npops
# value is a matrix admixture.proportions with nsam.mixed rows and npops columns;
# admixture.proportion[i,k] gives the proportion of the genome of individual i coming from population k
# Here, x is a vector
get.admixtureproportions<-function(x, p, tol=1e-6, verbose = FALSE) {
p = p[,!is.na(x)]
x = x[!is.na(x)]
K = nrow(p)
M = ncol(p)
# initialize admixture proportions
res = as.vector(rdirichlet(1,K))
# res = rep(1/K, K)
names(res) = rownames(p)
j=1
err = 1
while(err>tol) {
j = j+1
loc = fun2(res, p, x)
err = sum(abs(res - loc))
res = loc
}
if(verbose) cat("\t Iterations: ", j, "\n")
res
}
fun2<-function(q, p, loc.x) {
K = length(q)
M = length(loc.x)
E = matrix(0, nrow=K, ncol = M)
# admixture.proportions = admixture.proportions / sum(admixture.proportions)
loc = q %*% p # has M cols
loc[loc==0] = 1e-16
loc[loc==1] = 1-1e-16
for(k in 1:K) {
E[k,] = (loc.x * p[k,] / loc + (2 - loc.x) * (1-p[k,])/(1 - loc))
}
res = rowSums(E)/M * q / 2
res/(sum(res))
}
get.loglik.admixture<-function(loc.x, p, q, sum=TRUE) {
loc = t(p) %*% q
if(sum) res = sum(log(choose(2, loc.x) * loc^loc.x * (1-loc)^(2-loc.x)))
else res = log(choose(2, loc.x) * loc^loc.x * (1-loc)^(2-loc.x))
res
}
rdirichlet<-function(n, classes){
res = matrix(rexp(classes*n), ncol=classes, nrow=n)
res / rowSums(res)
}
bound<-function(x, lower=0, upper=1) {
res = x
for(i in 1:length(x)) {
res[i] = min(max(x[i],lower), upper)
}
res
}
# var is a matrix containing the eigenvalues to be drawn
plotq<-function(x, y, ia, height, width, cols, var=NULL) {
npops = length(ia)
rect(x, y, x+width*ia[1], y+height, col = cols[1], border = NA)
for(i in 2:npops) {
rect(x+width*cumsum(ia)[i-1], y, x+width*cumsum(ia)[i], y+height, col = cols[i], border = NA)
}
if(length(var)) {
dy = height / (nrow(var)+1)
for(i in 1:nrow(var)) {
if(var[i,1]>0) var[i,]=-var[i,]
loc1 = ia + var[i,]
loc1 = loc1/sum(loc1)
loc2 = ia - var[i,]
loc2 = loc2/sum(loc2)
eps = rnorm(1, mean=0, sd = 0.1*dy)
if(abs(loc1[1]-loc2[1])>0.0001) {
lines(bound(c(x+width*ia[1], x+width*loc2[1]), lower=x, upper=x+width), c(y+height-i*dy+eps, y+height-i*dy+eps), lwd=2, col="darkgrey")
lines(bound(c(x+width*ia[1], x+width*loc1[1]), lower=x, upper=x+width), c(y+height-i*dy+eps, y+height-i*dy+eps), lwd=2, col="black")
lines(bound(c(x+width*loc1[1], x+width*loc1[1]), lower=x, upper=x+width), c(y+height-i*dy+eps-.2*dy, y+height-i*dy+eps+.2*dy), lwd=2, col="black")
lines(bound(c(x+width*loc2[1], x+width*loc2[1]), lower=x, upper=x+width), c(y+height-i*dy+eps-.2*dy, y+height-i*dy+eps+.2*dy), lwd=2, col="darkgrey")
}
for(j in 2:(npops-1)) {
eps = rnorm(1, mean=0, sd = 0.1*dy)
if(abs(cumsum(loc1)[j]-cumsum(loc2)[j])>0.0001) {
lines(bound(c(x+width*cumsum(ia)[j], x+width*cumsum(loc2)[j]), lower=x, upper=x+width), c(y+height-i*dy+eps, y+height-i*dy+eps), lwd=2, col="darkgrey")
lines(bound(c(x+width*cumsum(ia)[j], x+width*cumsum(loc1)[j]), lower=x, upper=x+width), c(y+height-i*dy+eps, y+height-i*dy+eps), lwd=2, col="black")
lines(bound(c(x+width*cumsum(loc1)[j], x+width*cumsum(loc1)[j]), lower=x, upper=x+width), c(y+height-i*dy+eps-.2*dy, y+height-i*dy+eps+.2*dy), lwd=2, col="black")
lines(bound(c(x+width*cumsum(loc2)[j], x+width*cumsum(loc2)[j]), lower=x, upper=x+width), c(y+height-i*dy+eps-.2*dy, y+height-i*dy+eps+.2*dy), lwd=2, col="darkgrey")
}
}
}
}
rect(x, y, x+width, y+height)
}
# Now we come to all matrices from the manuscript
DeltaTilde<-function(p,q,x=NULL) { # p and q are vectors of length K, x \in {0,1,2}
K = length(q)
loc = (q %*% p)[1,]
res = 0
if(!is.null(x)) {
if(x > 0) res = res + x * (diag(rep(1,K))/loc - (p %*% t(q)) / loc^2)
if(x < 2) res = res - (2-x) * (diag(rep(1,K))/(1-loc) - ((1-p) %*% t(q)) / (1-loc)^2)
} else {
res = 2 * (((1-p) %*% t(q)) / (1-loc) - (p %*% t(q)) / loc)
}
res
}
Pi<-function(p,r) { # p and r are vectors of length K
diag(p*(1-p)/r)
}
Gamma<-function(p,q,r,x=NULL){ # here, p is a matrix, and x is a vector
K = length(q)
res = matrix(0, nrow=K, ncol=K)
if(!is.null(x)) {
p = p[,!is.na(x)]
x = x[!is.na(x)]
M = ncol(p)
for(i in 1:M) {
loc = DeltaTilde(p[,i], q, x[i]) %*% Pi(p[,i], r) %*% t(DeltaTilde(p[,i], q, x[i]))
if(is.nan(loc[1,1])) cat("NaN produced for ", colnames(p)[i], "\n")
res = res + loc
}
} else {
M = ncol(p)
for(i in 1:M) {
loc = DeltaTilde(p[,i], q) %*% Pi(p[,i], r) %*% t(DeltaTilde(p[,i], q))
if(is.nan(loc[1,1])) cat("NaN produced for ", colnames(p)[i], "\n")
res = res + loc
}
}
res/M/2
}
# if x==NULL, return Sigma from the manuscript, otherwise, return Sigma_x
Sigma<-function(p, q, x=NULL) {
K = length(q)
res = matrix(0, nrow=K, ncol=K)
if(!is.null(x)) {
p = p[,!is.na(x)]
x = x[!is.na(x)]
M = ncol(p)
for(i in 1:M) {
loc = (q %*% p[,i])[1,1]
if(x[i] > 0) res = res + x[i] * p[,i]%*%t(p[,i]) / loc^2
if(x[i] < 2) res = res + (2-x[i]) * (1-p[,i])%*%t(1-p[,i]) / (1-loc)^2
}
} else {
M = ncol(p)
for(i in 1:M) {
loc = (q %*% p[,i])[1,1]
if(loc>0) res = res + 2 * p[,i]%*%t(p[,i]) / loc
if(loc<1) res = res + 2 * (1-p[,i])%*%t(1-p[,i]) / (1-loc)
}
}
solve(res/M/2)
}
# N is a vector of length K for the size of the reference database
varq<-function(p,q,N,x=NULL,lim="MN"){
if(lim!="M" && lim!="N" && lim!="MN") {
cat("lim must be M, N or MN.\n")
exit()
}
r = N/sum(N)
N = sum(N)
K = nrow(p)
M = ncol(p)
res = matrix(0, ncol=K, nrow=K)
if(lim == "M" || lim == "MN") {
res = res + (Sigma(p, q, x) - q %*% t(q))/2/M
}
if(lim == "N" || lim == "MN") {
res = res + Sigma(p, q, x) %*% Gamma(p,q,r,x) %*% Sigma(p, q, x)/4/N/M
}
res
}