-
Notifications
You must be signed in to change notification settings - Fork 1
/
ancestral_analyse.R
259 lines (208 loc) · 7.75 KB
/
ancestral_analyse.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# The code below was written by S. Guindon
###################################################################################################
###################################################################################################
## Calculate expected posterior error for all ambiguity levels. Each level has a
## corresponding value of alpha
which.min.error <- function(alpha,p)
{
n = length(p);
sorted.p = sort(p,decreasing=TRUE);
cdf.sorted.p = cumsum(sorted.p);
pme = 1:(n-1);
for(i in 1:(n-1)) ## for the different levels of ambiguity
{
a = alpha[i];
b = (n-1)/(n-i) - a * i/(n-i);
pme[i] = a + (b-a)*(1-cdf.sorted.p[i]);
}
idx.min = which.min(pme); ## ambiguity level that minimizes posterior error
## binary encoding of state that minimises posterior error
ordered.p = order(p,decreasing=TRUE);
best.state.int = 0;
for(i in 1:idx.min)
{
best.state.int = best.state.int + 2^(ordered.p[i]-1);
}
return(best.state.int);
}
###################################################################################################
###################################################################################################
## Returns binary vector corresponding to integer i
int.to.bit <- function(i)
{
return(as.integer(intToBits(i)));
}
###################################################################################################
###################################################################################################
## Returns set of character states where bit > 0 (i.e. bit==1)
bit.to.state <- function(bit,n)
{
if(n == 4)
{
states = c("A","C","G","T");
}
else
{
states = c("A","R","N","D","C","Q","E","G","H","I","L","K","M","F","P","S","T","W","Y","V");
}
return(states[bit[1:n]>0]);
}
###################################################################################################
###################################################################################################
state.to.bit <- function(state,n)
{
# state is a string
if(n == 4)
{
return(as.numeric(lapply(c("A","C","G","T"),grepl,state)));
}
else
{
return(as.numeric(lapply(c("A","R","N","D","C","Q","E","G","H","I","L","K","M","F","P","S","T","W","Y","V"),grepl,state)));
}
}
###################################################################################################
###################################################################################################
mpee <- function(p,mesh.size)
{
n = length(p);
alpha = 1:n;
best.state = 1:mesh.size;
for(k in 0:mesh.size)
{
for(i in 1:n) { alpha[i] = (i-1)/i * (k/mesh.size); }
best.state[k+1] = which.min.error(alpha,p);
}
z=as.integer(names(sort(table(best.state),decreasing=TRUE)[1]));
return(paste(sort(bit.to.state(int.to.bit(z),n),decreasing=FALSE),collapse=""));
}
###################################################################################################
###################################################################################################
brier.score <- function(est.true.p,n)
{
# est and true are strings
est = as.character(est.true.p[1]);
true = as.character(est.true.p[2]);
p = as.numeric(as.vector(est.true.p[3:(3+n-1)]));
p.true = state.to.bit(true,n);
p.est = state.to.bit(est,n);
p.est = p.est * p;
p.est = p.est / sum(p.est);
diff = (p.est - p.true)^2;
## return(sqrt(sum(diff)));
return(sum(diff));
}
###################################################################################################
###################################################################################################
brier <- function(p)
{
n = length(p);
brier.score = 1:n;
sorted.p = sort(p,decreasing=TRUE);
for(k in 1:n) # calculate Brier score for each ambiguity level
{
brier.score[k] = 0.0;
for(i in 1:k)
{
brier.score[k] = brier.score[k] + (sorted.p[i] - 1./k)^2;
}
if(k < n)
{
for(i in (k+1):n)
{
brier.score[k] = brier.score[k] + sorted.p[i]^2;
}
}
}
idx.min = which.min(brier.score); ## ambiguity level that minimizes Brier score
ordered.p = order(p,decreasing=TRUE);
## binary encoding of state that minimises Brier score
best.state.int = 0;
for(i in 1:idx.min) { best.state.int = best.state.int + 2^(ordered.p[i]-1); }
return(paste(sort(bit.to.state(int.to.bit(best.state.int),n),decreasing=FALSE),collapse=""));
}
###################################################################################################
###################################################################################################
map <- function(p)
{
n = length(p);
return(idx.to.state(which.max(p),n));
}
###################################################################################################
###################################################################################################
# Diff criterion
map.plus <- function(p,eps)
{
n = length(p);
sort.p = sort(p,decreasing=TRUE);
ordered.p = order(p,decreasing=TRUE);
## binary encoding of state that minimises map.plus score
best.state.int = 2^(ordered.p[1]-1);
i = 2;
while(((sort.p[i-1] - sort.p[i]) < eps) && i <= n)
{
best.state.int = best.state.int + 2^(ordered.p[i]-1);
i = i+1;
}
return(paste(sort(bit.to.state(int.to.bit(best.state.int),n),decreasing=FALSE),collapse=""));
}
###################################################################################################
###################################################################################################
# CumProb criterion
cum.prob <- function(p,eps)
{
n = length(p);
sort.p = sort(p,decreasing=TRUE);
cum.sort.p = cumsum(sort.p);
ordered.p = order(p,decreasing=TRUE);
## binary encoding of state that minimises map.plus score
best.state.int = 0;
i = 1;
repeat
{
best.state.int = best.state.int + 2^(ordered.p[i]-1);
if(cum.sort.p[i] > eps || i >= n) { break; }
i = i+1;
}
return(paste(sort(bit.to.state(int.to.bit(best.state.int),n),decreasing=FALSE),collapse=""));
}
###################################################################################################
###################################################################################################
# Thresh criterion
prob.thresh <- function(p,thresh)
{
n = length(p);
sort.p = sort(p,decreasing=TRUE);
ordered.p = order(p,decreasing=TRUE);
best.state.int = 0;
i = 1;
while(sort.p[i] > thresh && i <= n)
{
best.state.int = best.state.int + 2^(ordered.p[i]-1);
i = i+1;
}
return(paste(sort(bit.to.state(int.to.bit(best.state.int),n),decreasing=FALSE),collapse=""));
}
###################################################################################################
###################################################################################################
## true is the vector of all true ancestral states and est is the corresponding vector of ancestral
## states estimated with a given method.
accuracy <- function(true,est)
{
z=apply(cbind(true,est),1,match.length);
return(z);
}
match.length <- function(true.est)
{
length(grep(true.est[1],true.est[2]));
}
precision <- function(est)
{
z=apply(as.array(est),1,nchar);
return(z);
}
brier.score.all <- function(true,est,p,n)
{
z = apply(matrix(c(est,true,p),ncol=2+n),1,brier.score,n);
return(z);
}