1 | /* |
---|
2 | |
---|
3 | PhyML: a program that computes maximum likelihood phyLOGenies from |
---|
4 | DNA or AA homoLOGous sequences. |
---|
5 | |
---|
6 | Copyright (C) Stephane Guindon. Oct 2003 onward. |
---|
7 | |
---|
8 | All parts of the source except where indicated are distributed under |
---|
9 | the GNU public licence. See http://www.opensource.org for details. |
---|
10 | |
---|
11 | */ |
---|
12 | |
---|
13 | /* Routines for molecular clock trees and molecular dating */ |
---|
14 | |
---|
15 | |
---|
16 | #include "rates.h" |
---|
17 | |
---|
18 | #ifdef RWRAPPER |
---|
19 | #include <R.h> |
---|
20 | #endif |
---|
21 | |
---|
22 | |
---|
23 | |
---|
24 | ////////////////////////////////////////////////////////////// |
---|
25 | ////////////////////////////////////////////////////////////// |
---|
26 | |
---|
27 | |
---|
28 | phydbl RATES_Lk_Rates(t_tree *tree) |
---|
29 | { |
---|
30 | |
---|
31 | tree->rates->c_lnL_rates = .0; |
---|
32 | |
---|
33 | RATES_Lk_Rates_Pre(tree->n_root,tree->n_root->v[2],NULL,tree); |
---|
34 | RATES_Lk_Rates_Pre(tree->n_root,tree->n_root->v[1],NULL,tree); |
---|
35 | |
---|
36 | if(isnan(tree->rates->c_lnL_rates) || isinf(tree->rates->c_lnL_rates)) |
---|
37 | { |
---|
38 | PhyML_Printf("\n== Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
39 | Exit("\n"); |
---|
40 | } |
---|
41 | |
---|
42 | return tree->rates->c_lnL_rates; |
---|
43 | } |
---|
44 | |
---|
45 | ////////////////////////////////////////////////////////////// |
---|
46 | ////////////////////////////////////////////////////////////// |
---|
47 | |
---|
48 | |
---|
49 | void RATES_Lk_Rates_Pre(t_node *a, t_node *d, t_edge *b, t_tree *tree) |
---|
50 | { |
---|
51 | int i; |
---|
52 | phydbl log_dens,mu_a,mu_d,r_a,r_d,dt_a,dt_d; |
---|
53 | int n_a,n_d; |
---|
54 | |
---|
55 | log_dens = -1.; |
---|
56 | |
---|
57 | if(d->anc != a) |
---|
58 | { |
---|
59 | PhyML_Printf("\n. d=%d d->anc=%d a=%d root=%d",d->num,d->anc->num,a->num,tree->n_root->num); |
---|
60 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
61 | Warn_And_Exit(""); |
---|
62 | } |
---|
63 | |
---|
64 | dt_a = -1.; |
---|
65 | if(a != tree->n_root) dt_a = tree->rates->nd_t[a->num] - tree->rates->nd_t[a->anc->num]; |
---|
66 | |
---|
67 | mu_a = tree->rates->br_r[a->num]; |
---|
68 | r_a = tree->rates->nd_r[a->num]; |
---|
69 | n_a = tree->rates->n_jps[a->num]; |
---|
70 | |
---|
71 | dt_d = FABS(tree->rates->nd_t[d->num] - tree->rates->nd_t[a->num]); |
---|
72 | mu_d = tree->rates->br_r[d->num]; |
---|
73 | r_d = tree->rates->nd_r[d->num]; |
---|
74 | n_d = tree->rates->n_jps[d->num]; |
---|
75 | |
---|
76 | log_dens = RATES_Lk_Rates_Core(mu_a,mu_d,r_a,r_d,n_a,n_d,dt_a,dt_d,tree); |
---|
77 | tree->rates->c_lnL_rates += log_dens; |
---|
78 | |
---|
79 | /* printf("\n. a=%3d d=%3d r(a)=%10f r(d)=%10f %f",a->num,d->num,mu_a,mu_d,log_dens); */ |
---|
80 | |
---|
81 | if(isnan(tree->rates->c_lnL_rates)) |
---|
82 | { |
---|
83 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
84 | MCMC_Print_Param(tree->mcmc,tree); |
---|
85 | Exit("\n"); |
---|
86 | } |
---|
87 | |
---|
88 | tree->rates->triplet[a->num] += log_dens; |
---|
89 | |
---|
90 | |
---|
91 | if(d->tax) return; |
---|
92 | else |
---|
93 | { |
---|
94 | For(i,3) |
---|
95 | { |
---|
96 | if((d->v[i] != a) && (d->b[i] != tree->e_root)) |
---|
97 | { |
---|
98 | RATES_Lk_Rates_Pre(d,d->v[i],d->b[i],tree); |
---|
99 | } |
---|
100 | } |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | ////////////////////////////////////////////////////////////// |
---|
105 | ////////////////////////////////////////////////////////////// |
---|
106 | |
---|
107 | |
---|
108 | phydbl RATES_Lk_Change_One_Rate(t_node *d, phydbl new_rate, t_tree *tree) |
---|
109 | { |
---|
110 | tree->rates->br_r[d->num] = new_rate; |
---|
111 | RATES_Update_Triplet(d,tree); |
---|
112 | RATES_Update_Triplet(d->anc,tree); |
---|
113 | return(tree->rates->c_lnL_rates); |
---|
114 | } |
---|
115 | |
---|
116 | ////////////////////////////////////////////////////////////// |
---|
117 | ////////////////////////////////////////////////////////////// |
---|
118 | |
---|
119 | |
---|
120 | phydbl RATES_Lk_Change_One_Time(t_node *n, phydbl new_t, t_tree *tree) |
---|
121 | { |
---|
122 | if(n == tree->n_root) |
---|
123 | { |
---|
124 | PhyML_Printf("\n. Moving the time of the root t_node is not permitted."); |
---|
125 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
126 | Warn_And_Exit(""); |
---|
127 | } |
---|
128 | else |
---|
129 | { |
---|
130 | int i; |
---|
131 | |
---|
132 | tree->rates->nd_t[n->num] = new_t; |
---|
133 | |
---|
134 | RATES_Update_Triplet(n,tree); |
---|
135 | |
---|
136 | For(i,3) |
---|
137 | { |
---|
138 | if(n->b[i] != tree->e_root) RATES_Update_Triplet(n->v[i],tree); |
---|
139 | else RATES_Update_Triplet(tree->n_root,tree); |
---|
140 | } |
---|
141 | } |
---|
142 | return(tree->rates->c_lnL_rates); |
---|
143 | } |
---|
144 | |
---|
145 | ////////////////////////////////////////////////////////////// |
---|
146 | ////////////////////////////////////////////////////////////// |
---|
147 | |
---|
148 | |
---|
149 | void RATES_Update_Triplet(t_node *n, t_tree *tree) |
---|
150 | { |
---|
151 | phydbl curr_triplet,new_triplet; |
---|
152 | phydbl dt0,dt1,dt2; |
---|
153 | phydbl mu1_mu0,mu2_mu0; |
---|
154 | phydbl mu0,mu1,mu2; |
---|
155 | phydbl r0,r1,r2; |
---|
156 | int n0,n1,n2; |
---|
157 | int i; |
---|
158 | t_node *v1,*v2; |
---|
159 | |
---|
160 | if(n->tax) return; |
---|
161 | |
---|
162 | curr_triplet = tree->rates->triplet[n->num]; |
---|
163 | |
---|
164 | dt0 = dt1 = dt2 = -100.0; |
---|
165 | r0 = r1 = r2 = 0.0; |
---|
166 | |
---|
167 | if(n == tree->n_root) |
---|
168 | { |
---|
169 | phydbl log_dens; |
---|
170 | |
---|
171 | log_dens = 0.0; |
---|
172 | |
---|
173 | dt0 = tree->rates->nd_t[tree->n_root->v[2]->num] - tree->rates->nd_t[tree->n_root->num]; |
---|
174 | dt1 = tree->rates->nd_t[tree->n_root->v[1]->num] - tree->rates->nd_t[tree->n_root->num]; |
---|
175 | |
---|
176 | mu0 = tree->rates->br_r[tree->n_root->v[2]->num]; |
---|
177 | mu1 = tree->rates->br_r[tree->n_root->v[1]->num]; |
---|
178 | |
---|
179 | r0 = tree->rates->nd_r[tree->n_root->v[2]->num]; |
---|
180 | r1 = tree->rates->nd_r[tree->n_root->v[1]->num]; |
---|
181 | |
---|
182 | n0 = tree->rates->n_jps[tree->n_root->v[2]->num]; |
---|
183 | n1 = tree->rates->n_jps[tree->n_root->v[1]->num]; |
---|
184 | |
---|
185 | switch(tree->rates->model) |
---|
186 | { |
---|
187 | case COMPOUND_COR : case COMPOUND_NOCOR : |
---|
188 | { |
---|
189 | log_dens = RATES_Dmu(mu0,n0,dt0,tree->rates->nu,1./tree->rates->nu,tree->rates->lexp,0,1); |
---|
190 | log_dens *= RATES_Dmu(mu1,n1,dt1,tree->rates->nu,1./tree->rates->nu,tree->rates->lexp,0,1); |
---|
191 | log_dens = LOG(log_dens); |
---|
192 | break; |
---|
193 | } |
---|
194 | case EXPONENTIAL : |
---|
195 | { |
---|
196 | log_dens = Dexp(mu0,tree->rates->lexp) * Dexp(mu1,tree->rates->lexp); |
---|
197 | log_dens = LOG(log_dens); |
---|
198 | break; |
---|
199 | } |
---|
200 | case GAMMA : |
---|
201 | { |
---|
202 | log_dens = Dgamma(mu0,tree->rates->nu,1./tree->rates->nu) * Dgamma(mu1,tree->rates->nu,1./tree->rates->nu); |
---|
203 | log_dens = LOG(log_dens); |
---|
204 | break; |
---|
205 | } |
---|
206 | case THORNE : |
---|
207 | { |
---|
208 | int err; |
---|
209 | phydbl mean0,sd0; |
---|
210 | phydbl mean1,sd1; |
---|
211 | |
---|
212 | |
---|
213 | sd0 = SQRT(tree->rates->nu*dt0); |
---|
214 | mean0 = 1.0; |
---|
215 | |
---|
216 | sd1 = SQRT(tree->rates->nu*dt1); |
---|
217 | mean1 = 1.0; |
---|
218 | |
---|
219 | log_dens = |
---|
220 | Log_Dnorm_Trunc(mu0,mean0,sd0,tree->rates->min_rate,tree->rates->max_rate,&err) + |
---|
221 | Log_Dnorm_Trunc(mu1,mean1,sd1,tree->rates->min_rate,tree->rates->max_rate,&err); |
---|
222 | |
---|
223 | break; |
---|
224 | } |
---|
225 | case GUINDON : |
---|
226 | { |
---|
227 | Exit("\n. Not implemented yet.\n"); |
---|
228 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
229 | break; |
---|
230 | } |
---|
231 | default : |
---|
232 | { |
---|
233 | Exit("\n. Model not implemented yet.\n"); |
---|
234 | break; |
---|
235 | } |
---|
236 | } |
---|
237 | new_triplet = log_dens; |
---|
238 | |
---|
239 | if(isnan(log_dens) || isinf(FABS(log_dens))) |
---|
240 | { |
---|
241 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
242 | MCMC_Print_Param(tree->mcmc,tree); |
---|
243 | Exit("\n"); |
---|
244 | } |
---|
245 | } |
---|
246 | else |
---|
247 | { |
---|
248 | mu0 = mu1 = mu2 = -1.; |
---|
249 | n0 = n1 = n2 = -1; |
---|
250 | |
---|
251 | mu0 = tree->rates->br_r[n->num]; |
---|
252 | dt0 = FABS(tree->rates->nd_t[n->num] - tree->rates->nd_t[n->anc->num]); |
---|
253 | n0 = tree->rates->n_jps[n->num]; |
---|
254 | r0 = tree->rates->nd_r[n->num]; |
---|
255 | |
---|
256 | v1 = v2 = NULL; |
---|
257 | For(i,3) |
---|
258 | { |
---|
259 | if((n->v[i] != n->anc) && (n->b[i] != tree->e_root)) |
---|
260 | { |
---|
261 | if(!v1) |
---|
262 | { |
---|
263 | v1 = n->v[i]; |
---|
264 | mu1 = tree->rates->br_r[v1->num]; |
---|
265 | dt1 = FABS(tree->rates->nd_t[v1->num] - tree->rates->nd_t[n->num]); |
---|
266 | n1 = tree->rates->n_jps[v1->num]; |
---|
267 | r1 = tree->rates->nd_r[v1->num]; |
---|
268 | } |
---|
269 | else |
---|
270 | { |
---|
271 | v2 = n->v[i]; |
---|
272 | mu2 = tree->rates->br_r[v2->num]; |
---|
273 | dt2 = FABS(tree->rates->nd_t[v2->num] - tree->rates->nd_t[n->num]); |
---|
274 | n2 = tree->rates->n_jps[v2->num]; |
---|
275 | r2 = tree->rates->nd_r[v2->num]; |
---|
276 | } |
---|
277 | } |
---|
278 | } |
---|
279 | |
---|
280 | mu1_mu0 = RATES_Lk_Rates_Core(mu0,mu1,r0,r1,n0,n1,dt0,dt1,tree); |
---|
281 | mu2_mu0 = RATES_Lk_Rates_Core(mu0,mu2,r0,r1,n0,n2,dt0,dt2,tree); |
---|
282 | |
---|
283 | new_triplet = mu1_mu0 + mu2_mu0; |
---|
284 | } |
---|
285 | |
---|
286 | tree->rates->c_lnL_rates = tree->rates->c_lnL_rates + new_triplet - curr_triplet; |
---|
287 | tree->rates->triplet[n->num] = new_triplet; |
---|
288 | } |
---|
289 | |
---|
290 | ////////////////////////////////////////////////////////////// |
---|
291 | ////////////////////////////////////////////////////////////// |
---|
292 | |
---|
293 | /* Returns LOG(f(br_r_rght;br_r_left)) */ |
---|
294 | phydbl RATES_Lk_Rates_Core(phydbl br_r_a, phydbl br_r_d, phydbl nd_r_a, phydbl nd_r_d, int n_a, int n_d, phydbl dt_a, phydbl dt_d, t_tree *tree) |
---|
295 | { |
---|
296 | phydbl log_dens; |
---|
297 | phydbl mean,sd; |
---|
298 | phydbl min_r, max_r; |
---|
299 | phydbl cr,logcr; |
---|
300 | |
---|
301 | cr = tree->rates->clock_r; |
---|
302 | logcr = LOG(cr); |
---|
303 | log_dens = UNLIKELY; |
---|
304 | mean = sd = -1.; |
---|
305 | min_r = tree->rates->min_rate; |
---|
306 | max_r = tree->rates->max_rate; |
---|
307 | |
---|
308 | dt_d = MAX(0.5,dt_d); // We give only one decimal when printing out node heights. It is therefore a fair approximation |
---|
309 | |
---|
310 | switch(tree->rates->model) |
---|
311 | { |
---|
312 | case THORNE : |
---|
313 | { |
---|
314 | int err; |
---|
315 | |
---|
316 | if(tree->rates->model_log_rates == YES) |
---|
317 | { |
---|
318 | |
---|
319 | br_r_d += logcr; |
---|
320 | br_r_a += logcr; |
---|
321 | min_r += logcr; |
---|
322 | max_r += logcr; |
---|
323 | |
---|
324 | sd = SQRT(tree->rates->nu*dt_d); |
---|
325 | mean = br_r_a - .5*sd*sd; |
---|
326 | |
---|
327 | log_dens = Log_Dnorm_Trunc(br_r_d,mean,sd,min_r,max_r,&err); |
---|
328 | |
---|
329 | if(err) |
---|
330 | { |
---|
331 | PhyML_Printf("\n. Run: %d",tree->mcmc->run); |
---|
332 | PhyML_Printf("\n. br_r_d = %f br_r_a = %f dt_d = %f logcr = %f",br_r_d,br_r_a,dt_d,logcr); |
---|
333 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
334 | Exit("\n"); |
---|
335 | } |
---|
336 | } |
---|
337 | else |
---|
338 | { |
---|
339 | PhyML_Printf("\n. Not implemented yet."); |
---|
340 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
341 | Exit("\n"); |
---|
342 | } |
---|
343 | |
---|
344 | /* int err; */ |
---|
345 | /* phydbl cr; */ |
---|
346 | /* phydbl min_r, max_r; */ |
---|
347 | |
---|
348 | /* cr = tree->rates->clock_r; */ |
---|
349 | /* min_r = tree->rates->min_rate * cr; */ |
---|
350 | /* max_r = tree->rates->max_rate * cr; */ |
---|
351 | |
---|
352 | /* /\* !!!!!!!!!!!!1 *\/ */ |
---|
353 | /* br_r_d *= cr; */ |
---|
354 | /* br_r_a *= cr; */ |
---|
355 | |
---|
356 | /* err = NO; */ |
---|
357 | |
---|
358 | /* /\* if(dt_d < 5.0) dt_d = 5.0; *\/ */ |
---|
359 | |
---|
360 | /* sd = SQRT(dt_d*tree->rates->nu); */ |
---|
361 | |
---|
362 | /* /\* sd = SQRT(dt_d*EXP(tree->rates->nu)); *\/ */ |
---|
363 | /* /\* mean = LOG(br_r_a) - .5*sd*sd; *\/ */ |
---|
364 | |
---|
365 | /* if(tree->rates->model_log_rates == YES) */ |
---|
366 | /* { */ |
---|
367 | /* mean = br_r_a - .5*sd*sd; */ |
---|
368 | /* } */ |
---|
369 | /* else */ |
---|
370 | /* { */ |
---|
371 | /* mean = br_r_a; */ |
---|
372 | /* } */ |
---|
373 | |
---|
374 | /* log_dens = Log_Dnorm_Trunc(br_r_d,mean,sd,min_r,max_r,&err); */ |
---|
375 | |
---|
376 | /* if(err || isnan(log_dens) || isinf(log_dens)) */ |
---|
377 | /* { */ |
---|
378 | /* PhyML_Printf("\n. br_r_a=%f br_r_d=%f dt_d=%f nu=%G",br_r_a,br_r_d,dt_d,tree->rates->nu); */ |
---|
379 | /* PhyML_Printf("\n. Run: %d",tree->mcmc->run); */ |
---|
380 | /* PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); */ |
---|
381 | /* Exit("\n"); */ |
---|
382 | /* } */ |
---|
383 | |
---|
384 | break; |
---|
385 | |
---|
386 | } |
---|
387 | case GUINDON : |
---|
388 | { |
---|
389 | int err; |
---|
390 | |
---|
391 | min_r = tree->rates->min_rate; |
---|
392 | max_r = tree->rates->max_rate; |
---|
393 | |
---|
394 | br_r_d += logcr; |
---|
395 | br_r_a += logcr; |
---|
396 | min_r += logcr; |
---|
397 | max_r += logcr; |
---|
398 | |
---|
399 | sd = SQRT(tree->rates->nu*dt_d); |
---|
400 | mean = br_r_a - .5*sd*sd; |
---|
401 | |
---|
402 | log_dens = Log_Dnorm_Trunc(br_r_d,mean,sd,min_r,max_r,&err); |
---|
403 | |
---|
404 | if(err) |
---|
405 | { |
---|
406 | PhyML_Printf("\n. Run: %d",tree->mcmc->run); |
---|
407 | PhyML_Printf("\n. br_r_d=%f mean=%f sd=%f min_r=%f max_r=%f dt_d=%f",br_r_d,mean,sd,min_r,max_r,dt_d); |
---|
408 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
409 | Exit("\n"); |
---|
410 | } |
---|
411 | break; |
---|
412 | } |
---|
413 | case GAMMA : |
---|
414 | { |
---|
415 | if(tree->rates->model_log_rates == YES) |
---|
416 | log_dens = Dgamma(EXP(br_r_d),1./tree->rates->nu,tree->rates->nu); |
---|
417 | else |
---|
418 | log_dens = Dgamma(br_r_d,1./tree->rates->nu,tree->rates->nu); |
---|
419 | |
---|
420 | log_dens = LOG(log_dens); |
---|
421 | break; |
---|
422 | } |
---|
423 | case STRICTCLOCK : |
---|
424 | { |
---|
425 | log_dens = 0.0; |
---|
426 | break; |
---|
427 | } |
---|
428 | |
---|
429 | default : |
---|
430 | { |
---|
431 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
432 | Warn_And_Exit(""); |
---|
433 | } |
---|
434 | } |
---|
435 | |
---|
436 | if(isnan(log_dens) || isinf(FABS(log_dens))) |
---|
437 | { |
---|
438 | PhyML_Printf("\n. Run=%4d br_r_d=%f br_r_a=%f dt_d=%f dt_a=%f nu=%f log_dens=%G sd=%f mean=%f\n", |
---|
439 | tree->mcmc->run, |
---|
440 | br_r_d,br_r_a,dt_d,dt_a,tree->rates->nu,log_dens, |
---|
441 | sd,mean); |
---|
442 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
443 | Exit("\n"); |
---|
444 | } |
---|
445 | |
---|
446 | return log_dens; |
---|
447 | } |
---|
448 | |
---|
449 | ////////////////////////////////////////////////////////////// |
---|
450 | ////////////////////////////////////////////////////////////// |
---|
451 | |
---|
452 | |
---|
453 | phydbl RATES_Compound_Core(phydbl mu1, phydbl mu2, int n1, int n2, phydbl dt1, phydbl dt2, phydbl alpha, phydbl beta, phydbl lexp, phydbl eps, int approx) |
---|
454 | { |
---|
455 | if((n1 > -1) && (n2 > -1)) |
---|
456 | { |
---|
457 | return RATES_Compound_Core_Joint(mu1,mu2,n1,n2,dt1,dt2,alpha,beta,lexp,eps,approx); |
---|
458 | } |
---|
459 | else |
---|
460 | { |
---|
461 | return RATES_Compound_Core_Marginal(mu1,mu2,dt1,dt2,alpha,beta,lexp,eps,approx); |
---|
462 | } |
---|
463 | } |
---|
464 | |
---|
465 | ////////////////////////////////////////////////////////////// |
---|
466 | ////////////////////////////////////////////////////////////// |
---|
467 | |
---|
468 | |
---|
469 | phydbl RATES_Compound_Core_Marginal(phydbl mu1, phydbl mu2, phydbl dt1, phydbl dt2, phydbl alpha, phydbl beta, phydbl lexp, phydbl eps, int approx) |
---|
470 | { |
---|
471 | phydbl p0,p1,v0,v1,v2; |
---|
472 | phydbl dmu1; |
---|
473 | int equ; |
---|
474 | phydbl dens; |
---|
475 | |
---|
476 | v0 = v1 = v2 = 0.0; |
---|
477 | |
---|
478 | /* Probability of 0 and 1 jumps */ |
---|
479 | p0 = Dpois(0,lexp*(dt2+dt1)); |
---|
480 | p1 = Dpois(1,lexp*(dt2+dt1)); |
---|
481 | |
---|
482 | dmu1 = RATES_Dmu(mu1,-1,dt1,alpha,beta,lexp,0,0); |
---|
483 | |
---|
484 | /* Are the two rates equal ? */ |
---|
485 | equ = 0; |
---|
486 | if(FABS(mu1-mu2) < eps) equ = 1; |
---|
487 | |
---|
488 | /* No jump */ |
---|
489 | if(equ) |
---|
490 | { |
---|
491 | v0 = 1.0*Dgamma(mu1,alpha,beta)/dmu1; |
---|
492 | /* Rprintf("\n. mu1=%f mu2=%f",mu1,mu2); */ |
---|
493 | } |
---|
494 | else |
---|
495 | { |
---|
496 | v0 = 1.E-100; |
---|
497 | } |
---|
498 | |
---|
499 | /* One jump */ |
---|
500 | v1 = RATES_Dmu1_And_Mu2_One_Jump_Two_Intervals(mu1,mu2,dt1,dt2,alpha,beta); |
---|
501 | v1 /= dmu1; |
---|
502 | |
---|
503 | /* Two jumps and more (approximation) */ |
---|
504 | if(approx == 1) |
---|
505 | { |
---|
506 | v2 = |
---|
507 | RATES_Dmu(mu1,-1,dt1,alpha,beta,lexp,0,0)*RATES_Dmu(mu2,-1,dt2,alpha,beta,lexp,0,0) - |
---|
508 | Dpois(0,lexp*dt1) * Dpois(0,lexp*dt2) * |
---|
509 | Dgamma(mu1,alpha,beta) * Dgamma(mu2,alpha,beta); |
---|
510 | } |
---|
511 | else |
---|
512 | { |
---|
513 | v2 = |
---|
514 | RATES_Dmu_One(mu1,dt1,alpha,beta,lexp) * |
---|
515 | RATES_Dmu_One(mu2,dt2,alpha,beta,lexp); |
---|
516 | |
---|
517 | v2 += Dpois(0,lexp*dt1)*Dgamma(mu1,alpha,beta)*RATES_Dmu(mu2,-1,dt2,alpha,beta,lexp,1,0); |
---|
518 | v2 += Dpois(0,lexp*dt2)*Dgamma(mu2,alpha,beta)*RATES_Dmu(mu1,-1,dt1,alpha,beta,lexp,1,0); |
---|
519 | |
---|
520 | } |
---|
521 | /* PhyML_Printf("\n. %f %f %f %f %f ",mu1,mu2,dt1,dt2,v2); */ |
---|
522 | v2 /= dmu1; |
---|
523 | |
---|
524 | dens = p0*v0 + p1*v1 + v2; |
---|
525 | /* dens = p1*v1 + v2; */ |
---|
526 | /* dens = p1*v1 + v2; */ |
---|
527 | /* dens = v0; */ |
---|
528 | /* dens *= dmu1; */ |
---|
529 | |
---|
530 | if(dens < SMALL) |
---|
531 | { |
---|
532 | PhyML_Printf("\n. dens=%12G mu1=%12G mu2=%12G dt1=%12G dt2=%12G lexp=%12G alpha=%f v0=%f v1=%f v2=%f p0=%f p1=%f p2=%f", |
---|
533 | dens, |
---|
534 | mu1,mu2,dt1,dt2, |
---|
535 | lexp, |
---|
536 | alpha, |
---|
537 | v0,v1,v2, |
---|
538 | p0,p1,1.-p0-p1); |
---|
539 | } |
---|
540 | |
---|
541 | return dens; |
---|
542 | |
---|
543 | } |
---|
544 | |
---|
545 | /**********************************************************/ |
---|
546 | |
---|
547 | phydbl RATES_Compound_Core_Joint(phydbl mu1, phydbl mu2, int n1, int n2, phydbl dt1, phydbl dt2, |
---|
548 | phydbl alpha, phydbl beta, phydbl lexp, phydbl eps, int approx) |
---|
549 | { |
---|
550 | phydbl density; |
---|
551 | phydbl dmu1; |
---|
552 | |
---|
553 | |
---|
554 | if(n1 < 0 || n2 < 0) |
---|
555 | { |
---|
556 | PhyML_Printf("\n. n1=%d n2=%d",n1,n2); |
---|
557 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
558 | Warn_And_Exit(""); |
---|
559 | } |
---|
560 | |
---|
561 | dmu1 = RATES_Dmu(mu1,n1,dt1,alpha,beta,lexp,0,0); |
---|
562 | |
---|
563 | if((n1 < 0) || (n2 < 0)) |
---|
564 | { |
---|
565 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
566 | Warn_And_Exit(""); |
---|
567 | } |
---|
568 | |
---|
569 | if((n1 == 0) && (n2 == 0)) |
---|
570 | { |
---|
571 | if(FABS(mu1-mu2) < eps) { density = Dgamma(mu1,alpha,beta); } |
---|
572 | else { density = 1.E-70; } |
---|
573 | } |
---|
574 | else if((n1 == 0) && (n2 == 1)) |
---|
575 | { |
---|
576 | density = |
---|
577 | Dgamma(mu1,alpha,beta) * |
---|
578 | RATES_Dmu1_Given_V_And_N(mu2,mu1,1,dt2,alpha,beta); |
---|
579 | } |
---|
580 | else if((n1 == 1) && (n2 == 0)) |
---|
581 | { |
---|
582 | density = |
---|
583 | Dgamma(mu2,alpha,beta) * |
---|
584 | RATES_Dmu1_Given_V_And_N(mu1,mu2,1,dt1,alpha,beta); |
---|
585 | } |
---|
586 | else /* independent */ |
---|
587 | { |
---|
588 | density = |
---|
589 | RATES_Dmu(mu1,n1,dt1,alpha,beta,lexp,0,0) * |
---|
590 | RATES_Dmu(mu2,n2,dt2,alpha,beta,lexp,0,0); |
---|
591 | } |
---|
592 | |
---|
593 | density /= dmu1; |
---|
594 | |
---|
595 | density *= Dpois(n2,dt2*lexp); |
---|
596 | |
---|
597 | if(density < 1.E-70) density = 1.E-70; |
---|
598 | |
---|
599 | /* PhyML_Printf("\n. density = %15G mu1=%3.4f mu2=%3.4f dt1=%3.4f dt2=%3.4f n1=%2d n2=%2d",density,mu1,mu2,dt1,dt2,n1,n2); */ |
---|
600 | return density; |
---|
601 | } |
---|
602 | |
---|
603 | /**********************************************************/ |
---|
604 | |
---|
605 | void RATES_Print_Triplets(t_tree *tree) |
---|
606 | { |
---|
607 | int i; |
---|
608 | For(i,2*tree->n_otu-1) PhyML_Printf("\n. Node %3d t=%f",i,tree->rates->triplet[i]); |
---|
609 | } |
---|
610 | |
---|
611 | |
---|
612 | /**********************************************************/ |
---|
613 | |
---|
614 | void RATES_Print_Rates(t_tree *tree) |
---|
615 | { |
---|
616 | RATES_Print_Rates_Pre(tree->n_root,tree->n_root->v[2],NULL,tree); |
---|
617 | RATES_Print_Rates_Pre(tree->n_root,tree->n_root->v[1],NULL,tree); |
---|
618 | } |
---|
619 | |
---|
620 | ////////////////////////////////////////////////////////////// |
---|
621 | ////////////////////////////////////////////////////////////// |
---|
622 | |
---|
623 | |
---|
624 | void RATES_Print_Rates_Pre(t_node *a, t_node *d, t_edge *b, t_tree *tree) |
---|
625 | { |
---|
626 | if((d == tree->n_root->v[2] && d->tax) || (d == tree->n_root->v[1] && d->tax)) |
---|
627 | PhyML_Printf("\n. a=%3d ++d=%3d rate=%12f t_left=%12f t_rght=%12f ml=%12f l=%12f %12f", |
---|
628 | a->num,d->num, |
---|
629 | tree->rates->br_r[d->num], |
---|
630 | tree->rates->nd_t[a->num],tree->rates->nd_t[d->num], |
---|
631 | tree->rates->ml_l[d->num], |
---|
632 | tree->rates->cur_l[d->num], |
---|
633 | (tree->rates->nd_t[d->num]-tree->rates->nd_t[a->num])*tree->rates->clock_r*tree->rates->br_r[d->num]); |
---|
634 | |
---|
635 | else if((d == tree->n_root->v[2]) || (d == tree->n_root->v[1])) |
---|
636 | PhyML_Printf("\n. a=%3d __d=%3d rate=%12f t_left=%12f t_rght=%12f ml=%12f l=%12f %12f", |
---|
637 | a->num,d->num, |
---|
638 | tree->rates->br_r[d->num], |
---|
639 | tree->rates->nd_t[a->num],tree->rates->nd_t[d->num], |
---|
640 | tree->rates->ml_l[d->num], |
---|
641 | tree->rates->cur_l[d->num], |
---|
642 | (tree->rates->nd_t[d->num]-tree->rates->nd_t[a->num])*tree->rates->clock_r*tree->rates->br_r[d->num]); |
---|
643 | else |
---|
644 | PhyML_Printf("\n. a=%3d d=%3d rate=%12f t_left=%12f t_rght=%12f ml=%12f l=%12f %12f", |
---|
645 | a->num,d->num, |
---|
646 | tree->rates->br_r[d->num], |
---|
647 | tree->rates->nd_t[a->num],tree->rates->nd_t[d->num], |
---|
648 | tree->rates->ml_l[d->num], |
---|
649 | tree->rates->cur_l[d->num], |
---|
650 | (tree->rates->nd_t[d->num]-tree->rates->nd_t[a->num])*tree->rates->clock_r*tree->rates->br_r[d->num]); |
---|
651 | |
---|
652 | if(d->tax) return; |
---|
653 | else |
---|
654 | { |
---|
655 | int i; |
---|
656 | |
---|
657 | For(i,3) |
---|
658 | { |
---|
659 | if((d->v[i] != a) && (d->b[i] != tree->e_root)) |
---|
660 | { |
---|
661 | RATES_Print_Rates_Pre(d,d->v[i],d->b[i],tree); |
---|
662 | } |
---|
663 | } |
---|
664 | } |
---|
665 | } |
---|
666 | |
---|
667 | ////////////////////////////////////////////////////////////// |
---|
668 | ////////////////////////////////////////////////////////////// |
---|
669 | |
---|
670 | |
---|
671 | phydbl RATES_Average_Rate(t_tree *tree) |
---|
672 | { |
---|
673 | int i; |
---|
674 | phydbl sum; |
---|
675 | sum = 0.0; |
---|
676 | For(i,2*tree->n_otu-2) sum += tree->rates->br_r[i]; |
---|
677 | return sum/(2*tree->n_otu-2); |
---|
678 | } |
---|
679 | ////////////////////////////////////////////////////////////// |
---|
680 | ////////////////////////////////////////////////////////////// |
---|
681 | |
---|
682 | phydbl RATES_Average_Substitution_Rate(t_tree *tree) |
---|
683 | { |
---|
684 | phydbl sum_r,sum_dt; |
---|
685 | phydbl u,t,t_anc; |
---|
686 | int i; |
---|
687 | |
---|
688 | u = 0.0; |
---|
689 | sum_r = 0.0; |
---|
690 | sum_dt = 0.0; |
---|
691 | |
---|
692 | /* For(i,2*tree->n_otu-3) */ |
---|
693 | /* { */ |
---|
694 | /* t = tree->rates->nd_t[i]; */ |
---|
695 | /* t_anc = tree->rates->nd_t[tree->a_nodes[i]->anc->num]; */ |
---|
696 | /* u = tree->a_edges[i]->l->v; */ |
---|
697 | /* if(tree->rates->model == GUINDON) u = tree->a_edges[i]->gamma_prior_mean; */ |
---|
698 | /* sum_r += u; */ |
---|
699 | /* sum_dt += FABS(t-t_anc); */ |
---|
700 | /* } */ |
---|
701 | |
---|
702 | For(i,2*tree->n_otu-3) |
---|
703 | { |
---|
704 | if(tree->a_edges[i] != tree->e_root) |
---|
705 | { |
---|
706 | t = tree->rates->nd_t[tree->a_edges[i]->left->num]; |
---|
707 | t_anc = tree->rates->nd_t[tree->a_edges[i]->rght->num]; |
---|
708 | u = tree->a_edges[i]->l->v; |
---|
709 | sum_r += u; |
---|
710 | sum_dt += FABS(t-t_anc); |
---|
711 | } |
---|
712 | |
---|
713 | sum_dt += FABS(tree->rates->nd_t[tree->n_root->v[2]->num] - tree->rates->nd_t[tree->n_root->num]); |
---|
714 | sum_dt += FABS(tree->rates->nd_t[tree->n_root->v[1]->num] - tree->rates->nd_t[tree->n_root->num]); |
---|
715 | |
---|
716 | u = tree->e_root->l->v; |
---|
717 | sum_r += u; |
---|
718 | } |
---|
719 | return(sum_r / sum_dt); |
---|
720 | } |
---|
721 | |
---|
722 | ////////////////////////////////////////////////////////////// |
---|
723 | ////////////////////////////////////////////////////////////// |
---|
724 | |
---|
725 | |
---|
726 | phydbl RATES_Check_Mean_Rates_True(t_tree *tree) |
---|
727 | { |
---|
728 | phydbl sum; |
---|
729 | int i; |
---|
730 | |
---|
731 | sum = 0.0; |
---|
732 | For(i,2*tree->n_otu-2) sum += tree->rates->true_r[i]; |
---|
733 | return(sum/(phydbl)(2*tree->n_otu-2)); |
---|
734 | } |
---|
735 | |
---|
736 | ////////////////////////////////////////////////////////////// |
---|
737 | ////////////////////////////////////////////////////////////// |
---|
738 | |
---|
739 | |
---|
740 | int RATES_Check_Node_Times(t_tree *tree) |
---|
741 | { |
---|
742 | int err; |
---|
743 | err = NO; |
---|
744 | RATES_Check_Node_Times_Pre(tree->n_root,tree->n_root->v[2],&err,tree); |
---|
745 | RATES_Check_Node_Times_Pre(tree->n_root,tree->n_root->v[1],&err,tree); |
---|
746 | return err; |
---|
747 | } |
---|
748 | |
---|
749 | ////////////////////////////////////////////////////////////// |
---|
750 | ////////////////////////////////////////////////////////////// |
---|
751 | |
---|
752 | |
---|
753 | void RATES_Check_Node_Times_Pre(t_node *a, t_node *d, int *err, t_tree *tree) |
---|
754 | { |
---|
755 | if((tree->rates->nd_t[d->num] < tree->rates->nd_t[a->num]) || (FABS(tree->rates->nd_t[d->num] - tree->rates->nd_t[a->num]) < 1.E-20)) |
---|
756 | { |
---|
757 | PhyML_Printf("\n== a->t=%f d->t=%f",tree->rates->nd_t[a->num],tree->rates->nd_t[d->num]); |
---|
758 | PhyML_Printf("\n== a->t_prior_min=%f a->t_prior_max=%f",tree->rates->t_prior_min[a->num],tree->rates->t_prior_max[a->num]); |
---|
759 | PhyML_Printf("\n== d->t_prior_min=%f d->t_prior_max=%f",tree->rates->t_prior_min[d->num],tree->rates->t_prior_max[d->num]); |
---|
760 | *err = YES; |
---|
761 | } |
---|
762 | if(d->tax) return; |
---|
763 | else |
---|
764 | { |
---|
765 | int i; |
---|
766 | |
---|
767 | For(i,3) |
---|
768 | if((d->v[i] != a) && (d->b[i] != tree->e_root)) |
---|
769 | RATES_Check_Node_Times_Pre(d,d->v[i],err,tree); |
---|
770 | } |
---|
771 | } |
---|
772 | ////////////////////////////////////////////////////////////// |
---|
773 | ////////////////////////////////////////////////////////////// |
---|
774 | |
---|
775 | |
---|
776 | |
---|
777 | |
---|
778 | |
---|
779 | |
---|
780 | |
---|
781 | void RATES_Bracket_N_Jumps(int *up, int *down, phydbl param) |
---|
782 | { |
---|
783 | phydbl cdf,eps,a,b,c; |
---|
784 | int step; |
---|
785 | |
---|
786 | step = 10; |
---|
787 | eps = 1.E-10; |
---|
788 | cdf = 0.0; |
---|
789 | c = 1; |
---|
790 | |
---|
791 | while(cdf < 1.-eps) |
---|
792 | { |
---|
793 | c = (int)FLOOR(c * step); |
---|
794 | cdf = Ppois(c,param); |
---|
795 | } |
---|
796 | |
---|
797 | a = 0.0; |
---|
798 | b = (c-a)/2.; |
---|
799 | step = 0; |
---|
800 | do |
---|
801 | { |
---|
802 | step++; |
---|
803 | cdf = Ppois(b,param); |
---|
804 | if(cdf < eps) a = b; |
---|
805 | else |
---|
806 | { |
---|
807 | break; |
---|
808 | } |
---|
809 | b = (c-a)/2.; |
---|
810 | } |
---|
811 | while(step < 1000); |
---|
812 | |
---|
813 | if(step == 1000) |
---|
814 | { |
---|
815 | PhyML_Printf("\n. a=%f b=%f c=%f param=%f",a,b,c,param); |
---|
816 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
817 | Warn_And_Exit(""); |
---|
818 | } |
---|
819 | *up = c; |
---|
820 | *down = a; |
---|
821 | } |
---|
822 | |
---|
823 | ////////////////////////////////////////////////////////////// |
---|
824 | ////////////////////////////////////////////////////////////// |
---|
825 | |
---|
826 | /* |
---|
827 | mu : average rate of the time period dt |
---|
828 | dt : time period to be considered |
---|
829 | a : rate at a given time point is gamma distributed. a is the shape parameter |
---|
830 | b : rate at a given time point is gamma distributed. b is the scale parameter |
---|
831 | lexp : the number of rate switches is Poisson distributed with parameter lexp * dt |
---|
832 | */ |
---|
833 | /* compute f(mu;dt,a,b,lexp), the probability density of mu. We need to integrate over the |
---|
834 | possible number of jumps (n) during the time interval dt */ |
---|
835 | phydbl RATES_Dmu(phydbl mu, int n_jumps, phydbl dt, phydbl a, phydbl b, phydbl lexp, int min_n, int jps_dens) |
---|
836 | { |
---|
837 | if(n_jumps < 0) /* Marginal, i.e., the number of jumps is not fixed */ |
---|
838 | { |
---|
839 | phydbl var,cumpoissprob,dens,mean,poissprob,ab2,gammadens,lexpdt; |
---|
840 | int n,up,down; |
---|
841 | |
---|
842 | var = 0.0; |
---|
843 | cumpoissprob = 0.0; |
---|
844 | dens = 0.0; |
---|
845 | n = 0; |
---|
846 | mean = a*b; |
---|
847 | ab2 = a*b*b; |
---|
848 | lexpdt = lexp*dt; |
---|
849 | |
---|
850 | RATES_Bracket_N_Jumps(&up,&down,lexpdt); |
---|
851 | For(n,MAX(down,min_n)-1) cumpoissprob += Dpois(n,lexpdt); |
---|
852 | |
---|
853 | for(n=MAX(down,min_n);n<up+1;n++) |
---|
854 | { |
---|
855 | poissprob = Dpois(n,lexpdt); /* probability of having n jumps */ |
---|
856 | var = (2./(n+2.))*ab2; /* var(mu|n) = var(mu|n=0) * 2 / (n+2) */ |
---|
857 | gammadens = Dgamma_Moments(mu,mean,var); |
---|
858 | dens += poissprob * gammadens; |
---|
859 | cumpoissprob += poissprob; |
---|
860 | if(cumpoissprob > 1.-1.E-04) break; |
---|
861 | } |
---|
862 | |
---|
863 | if(dens < 1.E-70) dens = 1.E-70; |
---|
864 | |
---|
865 | return(dens); |
---|
866 | } |
---|
867 | else /* Joint, i.e., return P(mu | dt, n_jumps) */ |
---|
868 | { |
---|
869 | phydbl mean, var, density; |
---|
870 | |
---|
871 | |
---|
872 | mean = 1.0; |
---|
873 | var = (2./(n_jumps+2.))*a*b*b; |
---|
874 | |
---|
875 | if(jps_dens) |
---|
876 | density = Dgamma_Moments(mu,mean,var) * Dpois(n_jumps,dt*lexp); |
---|
877 | else |
---|
878 | density = Dgamma_Moments(mu,mean,var); |
---|
879 | |
---|
880 | if(density < 1.E-70) density = 1.E-70; |
---|
881 | |
---|
882 | return density; |
---|
883 | } |
---|
884 | } |
---|
885 | |
---|
886 | ////////////////////////////////////////////////////////////// |
---|
887 | ////////////////////////////////////////////////////////////// |
---|
888 | |
---|
889 | |
---|
890 | phydbl RATES_Dmu_One(phydbl mu, phydbl dt, phydbl a, phydbl b, phydbl lexp) |
---|
891 | { |
---|
892 | phydbl var,cumpoissprob,dens,mean,poissprob,ab2,gammadens,lexpdt; |
---|
893 | int n,up,down; |
---|
894 | |
---|
895 | var = 0.0; |
---|
896 | cumpoissprob = 0.0; |
---|
897 | dens = 0.0; |
---|
898 | n = 0; |
---|
899 | mean = a*b; |
---|
900 | ab2 = a*b*b; |
---|
901 | lexpdt = lexp*dt; |
---|
902 | |
---|
903 | if(dt < 0.0) |
---|
904 | { |
---|
905 | PhyML_Printf("\n. dt=%f",dt); |
---|
906 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
907 | Warn_And_Exit(""); |
---|
908 | } |
---|
909 | |
---|
910 | if(lexpdt < SMALL) |
---|
911 | { |
---|
912 | PhyML_Printf("\n. lexpdt=%G lexp=%G dt=%G",lexpdt,lexp,dt); |
---|
913 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
914 | Warn_And_Exit(""); |
---|
915 | } |
---|
916 | |
---|
917 | if(mu < 1.E-10) |
---|
918 | { |
---|
919 | PhyML_Printf("\n. mu=%G",mu); |
---|
920 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
921 | Warn_And_Exit(""); |
---|
922 | } |
---|
923 | |
---|
924 | RATES_Bracket_N_Jumps(&up,&down,lexpdt); |
---|
925 | |
---|
926 | For(n,MAX(1,down)-1) cumpoissprob += Dpois(n,lexpdt); |
---|
927 | |
---|
928 | for(n=MAX(1,down);n<up+1;n++) /* WARNING: we are considering that at least one jump occurs in the interval */ |
---|
929 | { |
---|
930 | poissprob = Dpois(n,lexpdt); /* probability of having n jumps */ |
---|
931 | var = (n/((n+1)*(n+1)*(n+2)))*(POW(1-a*b,2) + 2/(n+1)*ab2) + 2*n*n*ab2/POW(n+1,3); |
---|
932 | gammadens = Dgamma_Moments(mu,mean,var); |
---|
933 | dens += poissprob * gammadens; |
---|
934 | cumpoissprob += poissprob; |
---|
935 | if(cumpoissprob > 1.-1.E-06) break; |
---|
936 | } |
---|
937 | |
---|
938 | return(dens); |
---|
939 | } |
---|
940 | |
---|
941 | ////////////////////////////////////////////////////////////// |
---|
942 | ////////////////////////////////////////////////////////////// |
---|
943 | |
---|
944 | /* Given the times of nodes a (ta) and d (td), the shape of the gamma distribution of instantaneous |
---|
945 | rates, the parameter of the exponential distribution of waiting times between rate jumps and the |
---|
946 | instantaneous rate at t_node a, this function works out an expected number of (amino-acids or |
---|
947 | nucleotide) substitutions per site. |
---|
948 | */ |
---|
949 | void RATES_Expect_Number_Subst(phydbl t_beg, phydbl t_end, phydbl r_beg, int *n_jumps, phydbl *mean_r, phydbl *r_end, t_rate *rates, t_tree *tree) |
---|
950 | { |
---|
951 | phydbl curr_r, curr_t, next_t; |
---|
952 | |
---|
953 | switch(rates->model) |
---|
954 | { |
---|
955 | case COMPOUND_COR:case COMPOUND_NOCOR: |
---|
956 | { |
---|
957 | /* Compound Poisson */ |
---|
958 | if(rates->model == COMPOUND_COR) |
---|
959 | { |
---|
960 | curr_r = r_beg; |
---|
961 | *mean_r = r_beg; |
---|
962 | } |
---|
963 | else |
---|
964 | { |
---|
965 | curr_r = Rgamma(rates->nu,1./rates->nu);; |
---|
966 | *mean_r = curr_r; |
---|
967 | } |
---|
968 | |
---|
969 | curr_t = t_beg + Rexp(rates->lexp); /* Exponentially distributed waiting times */ |
---|
970 | next_t = curr_t; |
---|
971 | |
---|
972 | *n_jumps = 0; |
---|
973 | while(curr_t < t_end) |
---|
974 | { |
---|
975 | curr_r = Rgamma(rates->nu,1./rates->nu); /* Gamma distributed random instantaneous rate */ |
---|
976 | |
---|
977 | (*n_jumps)++; |
---|
978 | |
---|
979 | next_t = curr_t + Rexp(rates->lexp); |
---|
980 | |
---|
981 | if(next_t < t_end) |
---|
982 | { |
---|
983 | *mean_r = (1./(next_t - t_beg)) * (*mean_r * (curr_t - t_beg) + curr_r * (next_t - curr_t)); |
---|
984 | } |
---|
985 | else |
---|
986 | { |
---|
987 | *mean_r = (1./(t_end - t_beg)) * (*mean_r * (curr_t - t_beg) + curr_r * (t_end - curr_t)); |
---|
988 | } |
---|
989 | curr_t = next_t; |
---|
990 | } |
---|
991 | |
---|
992 | /* PhyML_Printf("\n. [%3d %f %f]",*n_jumps,*mean_r,r_beg); */ |
---|
993 | |
---|
994 | if(*mean_r < rates->min_rate) *mean_r = rates->min_rate; |
---|
995 | if(*mean_r > rates->max_rate) *mean_r = rates->max_rate; |
---|
996 | |
---|
997 | *r_end = curr_r; |
---|
998 | break; |
---|
999 | } |
---|
1000 | case EXPONENTIAL: |
---|
1001 | { |
---|
1002 | *mean_r = Rexp(rates->nu); |
---|
1003 | |
---|
1004 | if(*mean_r < rates->min_rate) *mean_r = rates->min_rate; |
---|
1005 | if(*mean_r > rates->max_rate) *mean_r = rates->max_rate; |
---|
1006 | |
---|
1007 | *r_end = *mean_r; |
---|
1008 | break; |
---|
1009 | } |
---|
1010 | case GAMMA: |
---|
1011 | { |
---|
1012 | *mean_r = Rgamma(rates->nu,1./rates->nu); |
---|
1013 | |
---|
1014 | if(*mean_r < rates->min_rate) *mean_r = rates->min_rate; |
---|
1015 | if(*mean_r > rates->max_rate) *mean_r = rates->max_rate; |
---|
1016 | |
---|
1017 | *r_end = *mean_r; |
---|
1018 | break; |
---|
1019 | } |
---|
1020 | case THORNE: |
---|
1021 | { |
---|
1022 | phydbl sd,mean; |
---|
1023 | int err; |
---|
1024 | |
---|
1025 | sd = SQRT(rates->nu*FABS(t_beg-t_end)); |
---|
1026 | mean = r_beg; |
---|
1027 | |
---|
1028 | *mean_r = Rnorm_Trunc(mean,sd,rates->min_rate,rates->max_rate,&err); |
---|
1029 | |
---|
1030 | if(err) PhyML_Printf("\n. %s %d %d",__FILE__,__LINE__,tree->mcmc->run); |
---|
1031 | *r_end = *mean_r; |
---|
1032 | break; |
---|
1033 | } |
---|
1034 | default: |
---|
1035 | { |
---|
1036 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
1037 | Exit("\n. Model not implemented yet.\n"); |
---|
1038 | break; |
---|
1039 | } |
---|
1040 | } |
---|
1041 | } |
---|
1042 | |
---|
1043 | ////////////////////////////////////////////////////////////// |
---|
1044 | ////////////////////////////////////////////////////////////// |
---|
1045 | |
---|
1046 | |
---|
1047 | void RATES_Get_Mean_Rates_Pre(t_node *a, t_node *d, t_edge *b, phydbl r_a, t_tree *tree) |
---|
1048 | { |
---|
1049 | phydbl a_t,d_t; |
---|
1050 | phydbl mean_r; |
---|
1051 | int n_jumps; |
---|
1052 | phydbl r_d; |
---|
1053 | |
---|
1054 | a_t = tree->rates->nd_t[a->num]; |
---|
1055 | d_t = tree->rates->nd_t[d->num]; |
---|
1056 | |
---|
1057 | RATES_Expect_Number_Subst(a_t,d_t,r_a,&n_jumps,&mean_r,&r_d,tree->rates,tree); |
---|
1058 | |
---|
1059 | tree->rates->br_r[d->num] = mean_r; |
---|
1060 | tree->rates->true_r[d->num] = mean_r; |
---|
1061 | tree->rates->t_jps[d->num] = n_jumps; |
---|
1062 | |
---|
1063 | |
---|
1064 | /* Move to the next branches */ |
---|
1065 | if(d->tax) return; |
---|
1066 | else |
---|
1067 | { |
---|
1068 | int i; |
---|
1069 | |
---|
1070 | For(i,3) |
---|
1071 | { |
---|
1072 | if((d->v[i] != a) && (d->b[i] != tree->e_root)) |
---|
1073 | { |
---|
1074 | RATES_Get_Mean_Rates_Pre(d,d->v[i],d->b[i],r_d,tree); |
---|
1075 | } |
---|
1076 | } |
---|
1077 | } |
---|
1078 | |
---|
1079 | } |
---|
1080 | |
---|
1081 | ////////////////////////////////////////////////////////////// |
---|
1082 | ////////////////////////////////////////////////////////////// |
---|
1083 | |
---|
1084 | |
---|
1085 | void RATES_Random_Branch_Lengths(t_tree *tree) |
---|
1086 | { |
---|
1087 | phydbl r0; |
---|
1088 | |
---|
1089 | r0 = 1.0; |
---|
1090 | |
---|
1091 | tree->rates->br_r[tree->n_root->num] = r0; |
---|
1092 | |
---|
1093 | RATES_Get_Mean_Rates_Pre(tree->n_root,tree->n_root->v[2],NULL,r0,tree); |
---|
1094 | RATES_Get_Mean_Rates_Pre(tree->n_root,tree->n_root->v[1],NULL,r0,tree); |
---|
1095 | |
---|
1096 | /* RATES_Normalise_Rates(tree); */ |
---|
1097 | |
---|
1098 | RATES_Update_Cur_Bl(tree); |
---|
1099 | RATES_Initialize_True_Rates(tree); |
---|
1100 | |
---|
1101 | tree->n_root_pos = |
---|
1102 | tree->rates->cur_l[tree->n_root->v[2]->num] / |
---|
1103 | (tree->rates->cur_l[tree->n_root->v[2]->num] + tree->rates->cur_l[tree->n_root->v[1]->num]); |
---|
1104 | } |
---|
1105 | |
---|
1106 | ////////////////////////////////////////////////////////////// |
---|
1107 | ////////////////////////////////////////////////////////////// |
---|
1108 | |
---|
1109 | |
---|
1110 | void RATES_Set_Node_Times(t_tree *tree) |
---|
1111 | { |
---|
1112 | RATES_Set_Node_Times_Pre(tree->n_root,tree->n_root->v[2],tree); |
---|
1113 | RATES_Set_Node_Times_Pre(tree->n_root,tree->n_root->v[1],tree); |
---|
1114 | } |
---|
1115 | |
---|
1116 | ////////////////////////////////////////////////////////////// |
---|
1117 | ////////////////////////////////////////////////////////////// |
---|
1118 | |
---|
1119 | |
---|
1120 | void RATES_Init_Triplets(t_tree *tree) |
---|
1121 | { |
---|
1122 | int i; |
---|
1123 | For(i,2*tree->n_otu-1) tree->rates->triplet[i] = 0.0; |
---|
1124 | } |
---|
1125 | ////////////////////////////////////////////////////////////// |
---|
1126 | ////////////////////////////////////////////////////////////// |
---|
1127 | |
---|
1128 | |
---|
1129 | void RATES_Set_Node_Times_Pre(t_node *a, t_node *d, t_tree *tree) |
---|
1130 | { |
---|
1131 | if(d->tax) return; |
---|
1132 | else |
---|
1133 | { |
---|
1134 | t_node *v1, *v2; /* the two sons of d */ |
---|
1135 | phydbl t_sup, t_inf; |
---|
1136 | int i; |
---|
1137 | |
---|
1138 | v1 = v2 = NULL; |
---|
1139 | For(i,3) if((d->v[i] != a) && (d->b[i] != tree->e_root)) |
---|
1140 | { |
---|
1141 | if(!v1) v1 = d->v[i]; |
---|
1142 | else v2 = d->v[i]; |
---|
1143 | } |
---|
1144 | |
---|
1145 | t_inf = MIN(tree->rates->nd_t[v1->num],tree->rates->nd_t[v2->num]); |
---|
1146 | t_sup = tree->rates->nd_t[a->num]; |
---|
1147 | |
---|
1148 | if(t_sup > t_inf) |
---|
1149 | { |
---|
1150 | PhyML_Printf("\n. t_sup = %f t_inf = %f",t_sup,t_inf); |
---|
1151 | PhyML_Printf("\n. Run = %d",tree->mcmc->run); |
---|
1152 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
1153 | Warn_And_Exit(""); |
---|
1154 | } |
---|
1155 | |
---|
1156 | if(tree->rates->nd_t[d->num] > t_inf) |
---|
1157 | { |
---|
1158 | tree->rates->nd_t[d->num] = t_inf; |
---|
1159 | PhyML_Printf("\n. Time for t_node %d is larger than should be. Set it to %f",d->num,tree->rates->nd_t[d->num]); |
---|
1160 | } |
---|
1161 | else if(tree->rates->nd_t[d->num] < t_sup) |
---|
1162 | { |
---|
1163 | tree->rates->nd_t[d->num] = t_sup; |
---|
1164 | PhyML_Printf("\n. Time for t_node %d is lower than should be. Set it to %f",d->num,tree->rates->nd_t[d->num]); |
---|
1165 | } |
---|
1166 | |
---|
1167 | For(i,3) |
---|
1168 | { |
---|
1169 | if((d->v[i] != a) && (d->b[i] != tree->e_root)) |
---|
1170 | { |
---|
1171 | RATES_Set_Node_Times_Pre(d,d->v[i],tree); |
---|
1172 | } |
---|
1173 | } |
---|
1174 | } |
---|
1175 | } |
---|
1176 | |
---|
1177 | ////////////////////////////////////////////////////////////// |
---|
1178 | ////////////////////////////////////////////////////////////// |
---|
1179 | |
---|
1180 | |
---|
1181 | |
---|
1182 | phydbl RATES_Dmu1_And_Mu2_One_Jump_Two_Intervals(phydbl mu1, phydbl mu2, phydbl dt1, phydbl dt2, phydbl alpha, phydbl beta) |
---|
1183 | { |
---|
1184 | phydbl dens; |
---|
1185 | |
---|
1186 | if(mu2 < 1.E-10) |
---|
1187 | { |
---|
1188 | PhyML_Printf("\n. mu2=%G",mu2); |
---|
1189 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
1190 | Warn_And_Exit(""); |
---|
1191 | } |
---|
1192 | |
---|
1193 | if(mu1 < 1.E-10) |
---|
1194 | { |
---|
1195 | PhyML_Printf("\n. mu2=%G",mu1); |
---|
1196 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
1197 | Warn_And_Exit(""); |
---|
1198 | } |
---|
1199 | |
---|
1200 | dens = |
---|
1201 | ((dt1/(dt1+dt2)) * RATES_Dmu1_Given_V_And_N(mu1,mu2,1,dt1,alpha,beta) * Dgamma(mu2,alpha,beta)) + |
---|
1202 | ((dt2/(dt1+dt2)) * RATES_Dmu1_Given_V_And_N(mu2,mu1,1,dt2,alpha,beta) * Dgamma(mu1,alpha,beta)); |
---|
1203 | |
---|
1204 | return dens; |
---|
1205 | } |
---|
1206 | |
---|
1207 | ////////////////////////////////////////////////////////////// |
---|
1208 | ////////////////////////////////////////////////////////////// |
---|
1209 | |
---|
1210 | |
---|
1211 | phydbl RATES_Dmu1_Given_V_And_N(phydbl mu1, phydbl v, int n, phydbl dt1, phydbl a, phydbl b) |
---|
1212 | { |
---|
1213 | phydbl lbda,dens,h,u; |
---|
1214 | phydbl mean,var; |
---|
1215 | int n_points,i; |
---|
1216 | phydbl ndb; |
---|
1217 | phydbl end, beg; |
---|
1218 | |
---|
1219 | n_points = 20; |
---|
1220 | |
---|
1221 | end = MIN(mu1/v-0.01,0.99); |
---|
1222 | beg = 0.01; |
---|
1223 | |
---|
1224 | dens = 0.0; |
---|
1225 | |
---|
1226 | if(end > beg) |
---|
1227 | { |
---|
1228 | mean = a*b; |
---|
1229 | var = a*b*b*2./(n+1.); |
---|
1230 | ndb = (phydbl)n/dt1; |
---|
1231 | |
---|
1232 | h = (end - beg) / (phydbl)n_points; |
---|
1233 | |
---|
1234 | lbda = beg; |
---|
1235 | For(i,n_points-1) |
---|
1236 | { |
---|
1237 | lbda += h; |
---|
1238 | u = (mu1 - lbda*v)/(1.-lbda); |
---|
1239 | |
---|
1240 | if(u < 1.E-10) |
---|
1241 | { |
---|
1242 | PhyML_Printf("\n. u = %G",u); |
---|
1243 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
1244 | Warn_And_Exit(""); |
---|
1245 | } |
---|
1246 | |
---|
1247 | dens += Dgamma_Moments(u,mean,var) / (1.-lbda) * ndb * POW(1.-lbda,n-1); |
---|
1248 | } |
---|
1249 | dens *= 2.; |
---|
1250 | |
---|
1251 | lbda = beg; |
---|
1252 | u = (mu1 - lbda*v)/(1.-lbda); |
---|
1253 | if(u < 1.E-10) |
---|
1254 | { |
---|
1255 | PhyML_Printf("\n. mu1 = %f lambda = %f v = %f u = %G beg = %f end = %f",mu1,lbda,v,u,beg,end); |
---|
1256 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
1257 | Warn_And_Exit(""); |
---|
1258 | } |
---|
1259 | |
---|
1260 | dens += Dgamma_Moments(u,mean,var) / (1.-lbda) * ndb * POW(1.-lbda,n-1); |
---|
1261 | |
---|
1262 | lbda = end; |
---|
1263 | u = (mu1 - lbda*v)/(1.-lbda); |
---|
1264 | if(u < 1.E-10) |
---|
1265 | { |
---|
1266 | PhyML_Printf("\n. u = %G",u); |
---|
1267 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
1268 | Warn_And_Exit(""); |
---|
1269 | } |
---|
1270 | |
---|
1271 | dens += Dgamma_Moments(u,mean,var) / (1.-lbda) * ndb * POW(1.-lbda,n-1); |
---|
1272 | |
---|
1273 | dens *= (h/2.); |
---|
1274 | dens *= dt1; |
---|
1275 | } |
---|
1276 | |
---|
1277 | return(dens); |
---|
1278 | } |
---|
1279 | |
---|
1280 | ////////////////////////////////////////////////////////////// |
---|
1281 | ////////////////////////////////////////////////////////////// |
---|
1282 | |
---|
1283 | /* Joint density of mu1 and a minimum number of jumps occuring in the interval dt1+dt2 given mu1. |
---|
1284 | 1 jump occurs at the junction of the two intervals, which makes mu1 and mu2 independant */ |
---|
1285 | phydbl RATES_Dmu2_And_Mu1_Given_Min_N(phydbl mu1, phydbl mu2, phydbl dt1, phydbl dt2, int n_min, phydbl a, phydbl b, phydbl lexp) |
---|
1286 | { |
---|
1287 | phydbl density, lexpdt,cumpoiss,poiss; |
---|
1288 | int i; |
---|
1289 | int up,down; |
---|
1290 | |
---|
1291 | density = 0.0; |
---|
1292 | lexpdt = lexp * (dt1+dt2); |
---|
1293 | cumpoiss = 0.0; |
---|
1294 | |
---|
1295 | RATES_Bracket_N_Jumps(&up,&down,lexpdt); |
---|
1296 | |
---|
1297 | For(i,MAX(up,n_min)-1) |
---|
1298 | { |
---|
1299 | poiss = Dpois(i,lexpdt); |
---|
1300 | cumpoiss = cumpoiss + poiss; |
---|
1301 | } |
---|
1302 | |
---|
1303 | for(i=MAX(up,n_min);i<up;i++) |
---|
1304 | { |
---|
1305 | /* poiss = Dpois(i-1,lexpdt); /\* Complies with the no correlation model *\/ */ |
---|
1306 | poiss = Dpois(i,lexpdt); |
---|
1307 | cumpoiss = cumpoiss + poiss; |
---|
1308 | |
---|
1309 | density = density + poiss * RATES_Dmu2_And_Mu1_Given_N(mu1,mu2,dt1,dt2,i-1,a,b,lexp); |
---|
1310 | /* density = density + poiss * RATES_Dmu2_And_Mu1_Given_N_Full(mu1,mu2,dt1,dt2,i,a,b,lexp); */ |
---|
1311 | if(cumpoiss > 1.-1.E-6) break; |
---|
1312 | } |
---|
1313 | |
---|
1314 | if(density < 0.0) |
---|
1315 | { |
---|
1316 | PhyML_Printf("\n. density=%f cmpoiss = %f i=%d n_min=%d mu1=%f mu2=%f dt1=%f dt2=%f", |
---|
1317 | density,cumpoiss,i,n_min,mu1,mu2,dt1,dt2); |
---|
1318 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
1319 | Warn_And_Exit(""); |
---|
1320 | } |
---|
1321 | |
---|
1322 | return(density); |
---|
1323 | } |
---|
1324 | |
---|
1325 | ////////////////////////////////////////////////////////////// |
---|
1326 | ////////////////////////////////////////////////////////////// |
---|
1327 | |
---|
1328 | /* Joint density of mu1 and mu2 given the number of jumps (n) in the interval dt1+dt2, which are considered |
---|
1329 | as independant. Hence, for n jumps occuring in dt1+dt2, the number of jumps occuring in dt1 and dt2 are |
---|
1330 | n and 0, or n-1 and 1, or n-2 and 2 ... or 0 and n. This function sums over all these scenarios, with |
---|
1331 | weights corresponding to the probability of each partitition. |
---|
1332 | */ |
---|
1333 | |
---|
1334 | phydbl RATES_Dmu2_And_Mu1_Given_N(phydbl mu1, phydbl mu2, phydbl dt1, phydbl dt2, int n, phydbl a, phydbl b, phydbl lexp) |
---|
1335 | { |
---|
1336 | phydbl density,cumpoiss,poiss,abb,ab,LOGnf,LOGdt1,LOGdt2,nLOGdt; |
---|
1337 | int i; |
---|
1338 | |
---|
1339 | density = 0.0; |
---|
1340 | abb = a*b*b; |
---|
1341 | ab = a*b; |
---|
1342 | cumpoiss = 0.0; |
---|
1343 | poiss = 0.0; |
---|
1344 | LOGnf = LnFact(n); |
---|
1345 | LOGdt1 = LOG(dt1); |
---|
1346 | LOGdt2 = LOG(dt2); |
---|
1347 | nLOGdt = n*LOG(dt1+dt2); |
---|
1348 | |
---|
1349 | For(i,n+1) |
---|
1350 | { |
---|
1351 | poiss = LOGnf - LnFact(i) - LnFact(n-i) + i*LOGdt1 + (n-i)*LOGdt2 - nLOGdt; |
---|
1352 | poiss = EXP(poiss); |
---|
1353 | cumpoiss = cumpoiss + poiss; |
---|
1354 | |
---|
1355 | if(mu2 < 1.E-10) |
---|
1356 | { |
---|
1357 | PhyML_Printf("\n. mu2=%f",mu2); |
---|
1358 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
1359 | Warn_And_Exit(""); |
---|
1360 | } |
---|
1361 | |
---|
1362 | if(mu1 < 1.E-10) |
---|
1363 | { |
---|
1364 | PhyML_Printf("\n. mu1=%f",mu1); |
---|
1365 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
1366 | Warn_And_Exit(""); |
---|
1367 | } |
---|
1368 | |
---|
1369 | |
---|
1370 | density = density + poiss * Dgamma_Moments(mu2,ab,2./((n-i)+2.)*abb) * Dgamma_Moments(mu1,ab,2./(i+2.)*abb); |
---|
1371 | if(cumpoiss > 1.-1.E-6) break; |
---|
1372 | } |
---|
1373 | |
---|
1374 | return(density); |
---|
1375 | } |
---|
1376 | |
---|
1377 | ////////////////////////////////////////////////////////////// |
---|
1378 | ////////////////////////////////////////////////////////////// |
---|
1379 | |
---|
1380 | |
---|
1381 | void RATES_Initialize_True_Rates(t_tree *tree) |
---|
1382 | { |
---|
1383 | int i; |
---|
1384 | For(i,2*tree->n_otu-2) tree->rates->true_r[i] = tree->rates->br_r[i]; |
---|
1385 | } |
---|
1386 | ////////////////////////////////////////////////////////////// |
---|
1387 | ////////////////////////////////////////////////////////////// |
---|
1388 | |
---|
1389 | |
---|
1390 | void RATES_Get_Rates_From_Bl(t_tree *tree) |
---|
1391 | { |
---|
1392 | phydbl dt,cr; |
---|
1393 | t_node *left, *rght; |
---|
1394 | int i; |
---|
1395 | |
---|
1396 | dt = -1.0; |
---|
1397 | cr = tree->rates->clock_r; |
---|
1398 | |
---|
1399 | if(tree->n_root) |
---|
1400 | { |
---|
1401 | dt = FABS(tree->rates->nd_t[tree->n_root->num] - tree->rates->nd_t[tree->n_root->v[2]->num]); |
---|
1402 | tree->rates->br_r[tree->n_root->v[2]->num] = 0.5 * tree->e_root->l->v / (dt*cr); |
---|
1403 | dt = FABS(tree->rates->nd_t[tree->n_root->num] - tree->rates->nd_t[tree->n_root->v[1]->num]); |
---|
1404 | tree->rates->br_r[tree->n_root->v[1]->num] = 0.5 * tree->e_root->l->v / (dt*cr); |
---|
1405 | } |
---|
1406 | |
---|
1407 | |
---|
1408 | For(i,2*tree->n_otu-3) |
---|
1409 | { |
---|
1410 | if(tree->a_edges[i] != tree->e_root) |
---|
1411 | { |
---|
1412 | left = tree->a_edges[i]->left; |
---|
1413 | rght = tree->a_edges[i]->rght; |
---|
1414 | dt = FABS(tree->rates->nd_t[left->num] - tree->rates->nd_t[rght->num]); |
---|
1415 | |
---|
1416 | if(left->anc == rght) tree->rates->br_r[left->num] = tree->a_edges[i]->l->v / (dt*cr); |
---|
1417 | else tree->rates->br_r[rght->num] = tree->a_edges[i]->l->v / (dt*cr); |
---|
1418 | } |
---|
1419 | } |
---|
1420 | } |
---|
1421 | |
---|
1422 | ////////////////////////////////////////////////////////////// |
---|
1423 | ////////////////////////////////////////////////////////////// |
---|
1424 | |
---|
1425 | |
---|
1426 | phydbl RATES_Lk_Jumps(t_tree *tree) |
---|
1427 | { |
---|
1428 | int i,n_jps; |
---|
1429 | phydbl dens,dt,lexp; |
---|
1430 | t_node *n; |
---|
1431 | |
---|
1432 | n = NULL; |
---|
1433 | lexp = tree->rates->lexp; |
---|
1434 | n_jps = 0; |
---|
1435 | dt = 0.0; |
---|
1436 | dens = 0.0; |
---|
1437 | |
---|
1438 | For(i,2*tree->n_otu-2) |
---|
1439 | { |
---|
1440 | n = tree->a_nodes[i]; |
---|
1441 | dt = FABS(tree->rates->nd_t[n->num]-tree->rates->nd_t[n->anc->num]); |
---|
1442 | n_jps = tree->rates->n_jps[n->num]; |
---|
1443 | dens += LOG(Dpois(n_jps,lexp*dt)); |
---|
1444 | } |
---|
1445 | |
---|
1446 | tree->rates->c_lnL_jps = dens; |
---|
1447 | |
---|
1448 | return dens; |
---|
1449 | } |
---|
1450 | |
---|
1451 | ////////////////////////////////////////////////////////////// |
---|
1452 | ////////////////////////////////////////////////////////////// |
---|
1453 | |
---|
1454 | |
---|
1455 | void RATES_Posterior_Rates(t_tree *tree) |
---|
1456 | { |
---|
1457 | int node_num; |
---|
1458 | node_num = Rand_Int(0,2*tree->n_otu-3); |
---|
1459 | RATES_Posterior_One_Rate(tree->a_nodes[node_num]->anc,tree->a_nodes[node_num],NO,tree); |
---|
1460 | } |
---|
1461 | |
---|
1462 | ////////////////////////////////////////////////////////////// |
---|
1463 | ////////////////////////////////////////////////////////////// |
---|
1464 | |
---|
1465 | |
---|
1466 | void RATES_Posterior_Times(t_tree *tree) |
---|
1467 | { |
---|
1468 | int node_num; |
---|
1469 | node_num = Rand_Int(tree->n_otu,2*tree->n_otu-3); |
---|
1470 | RATES_Posterior_One_Time(tree->a_nodes[node_num]->anc,tree->a_nodes[node_num],NO,tree); |
---|
1471 | } |
---|
1472 | |
---|
1473 | ////////////////////////////////////////////////////////////// |
---|
1474 | ////////////////////////////////////////////////////////////// |
---|
1475 | |
---|
1476 | |
---|
1477 | void RATES_Posterior_One_Rate(t_node *a, t_node *d, int traversal, t_tree *tree) |
---|
1478 | { |
---|
1479 | phydbl like_mean, like_var; |
---|
1480 | phydbl prior_mean, prior_var; |
---|
1481 | phydbl post_mean, post_var, post_sd; |
---|
1482 | phydbl dt,rd,cr,cel,cvl; |
---|
1483 | int dim; |
---|
1484 | phydbl l_opp; /* length of the branch connected to the root, opposite to the one connected to d */ |
---|
1485 | t_edge *b; |
---|
1486 | int i; |
---|
1487 | t_node *v2,*v3; |
---|
1488 | phydbl T0,T1,T2,T3; |
---|
1489 | phydbl U0,U1,U2,U3; |
---|
1490 | phydbl V1,V2,V3; |
---|
1491 | phydbl r_min,r_max; |
---|
1492 | int err; |
---|
1493 | phydbl ratio,u; |
---|
1494 | phydbl cvr, cer; |
---|
1495 | phydbl nf; |
---|
1496 | phydbl new_lnL_data, cur_lnL_data, new_lnL_rate, cur_lnL_rate; |
---|
1497 | phydbl sd1,sd2,sd3; |
---|
1498 | phydbl inflate_var; |
---|
1499 | |
---|
1500 | if(d == tree->n_root) return; |
---|
1501 | |
---|
1502 | dim = 2*tree->n_otu-3; |
---|
1503 | err = NO; |
---|
1504 | |
---|
1505 | inflate_var = tree->rates->inflate_var; |
---|
1506 | |
---|
1507 | b = NULL; |
---|
1508 | if(a == tree->n_root) b = tree->e_root; |
---|
1509 | else For(i,3) if(d->v[i] == a) { b = d->b[i]; break; } |
---|
1510 | |
---|
1511 | v2 = v3 = NULL; |
---|
1512 | if(!d->tax) |
---|
1513 | { |
---|
1514 | For(i,3) |
---|
1515 | if((d->v[i] != a) && (d->b[i] != tree->e_root)) |
---|
1516 | { |
---|
1517 | if(!v2) { v2 = d->v[i]; } |
---|
1518 | else { v3 = d->v[i]; } |
---|
1519 | } |
---|
1520 | } |
---|
1521 | |
---|
1522 | T3 = T2 = 0.0; |
---|
1523 | T0 = tree->rates->nd_t[a->num]; |
---|
1524 | T1 = tree->rates->nd_t[d->num]; |
---|
1525 | U0 = tree->rates->br_r[a->num]; |
---|
1526 | U1 = tree->rates->br_r[d->num]; |
---|
1527 | U3 = U2 = -1.0; |
---|
1528 | |
---|
1529 | if(!d->tax) |
---|
1530 | { |
---|
1531 | T2 = tree->rates->nd_t[v2->num]; |
---|
1532 | T3 = tree->rates->nd_t[v3->num]; |
---|
1533 | U2 = tree->rates->br_r[v2->num]; |
---|
1534 | U3 = tree->rates->br_r[v3->num]; |
---|
1535 | } |
---|
1536 | |
---|
1537 | |
---|
1538 | V1 = tree->rates->nu * FABS(T1 - T0); |
---|
1539 | V2 = tree->rates->nu * FABS(T2 - T1); |
---|
1540 | V3 = tree->rates->nu * FABS(T3 - T1); |
---|
1541 | |
---|
1542 | dt = T1 - T0; |
---|
1543 | rd = U1; |
---|
1544 | cr = tree->rates->clock_r; |
---|
1545 | r_min = tree->rates->min_rate; |
---|
1546 | r_max = tree->rates->max_rate; |
---|
1547 | nf = tree->rates->norm_fact; |
---|
1548 | sd1 = SQRT(V1); |
---|
1549 | sd2 = SQRT(V2); |
---|
1550 | sd3 = SQRT(V3); |
---|
1551 | |
---|
1552 | |
---|
1553 | /* Likelihood */ |
---|
1554 | cel=0.0; |
---|
1555 | For(i,dim) |
---|
1556 | if(i != b->num) |
---|
1557 | cel += tree->rates->reg_coeff[b->num*dim+i] * (tree->rates->u_cur_l[i] - tree->rates->mean_l[i]); |
---|
1558 | cel += tree->rates->mean_l[b->num]; |
---|
1559 | cvl = tree->rates->cond_var[b->num]; |
---|
1560 | |
---|
1561 | l_opp = 0.0; |
---|
1562 | if(a == tree->n_root) |
---|
1563 | { |
---|
1564 | if(d == a->v[0]) l_opp = tree->rates->cur_l[a->v[1]->num]; |
---|
1565 | else l_opp = tree->rates->cur_l[a->v[0]->num]; |
---|
1566 | cel -= l_opp; |
---|
1567 | } |
---|
1568 | |
---|
1569 | |
---|
1570 | if(isnan(cvl) || isnan(cel) || cvl < .0) |
---|
1571 | { |
---|
1572 | For(i,dim) if(i != b->num) printf("\n. reg: %f %f %f nu=%f clock=%f", |
---|
1573 | tree->rates->reg_coeff[b->num*dim+i], |
---|
1574 | tree->rates->u_cur_l[i], |
---|
1575 | tree->rates->mean_l[i], |
---|
1576 | tree->rates->nu,tree->rates->clock_r); |
---|
1577 | PhyML_Printf("\n. cel=%f cvl=%f\n",cel,cvl); |
---|
1578 | PhyML_Printf("\n. Warning: invalid expected and/or std. dev. values. Skipping this step.\n"); |
---|
1579 | Exit("\n"); |
---|
1580 | } |
---|
1581 | |
---|
1582 | /* Model rates */ |
---|
1583 | /* if(tree->mod->log_l == YES) cel = EXP(cel); */ |
---|
1584 | /* like_mean = cel / (dt*cr*nf); */ |
---|
1585 | /* like_var = cvl / POW(dt*cr*nf,2); */ |
---|
1586 | |
---|
1587 | /* Model log of rates. Branch lengths are given in log units */ |
---|
1588 | if(tree->rates->model_log_rates == YES) |
---|
1589 | { |
---|
1590 | like_mean = cel - LOG(dt*cr*nf); |
---|
1591 | like_var = cvl - 2.*LOG(dt*cr*nf); |
---|
1592 | } |
---|
1593 | else |
---|
1594 | { |
---|
1595 | like_mean = cel / (dt*cr*nf); |
---|
1596 | like_var = cvl / POW(dt*cr*nf,2); |
---|
1597 | } |
---|
1598 | |
---|
1599 | /* Prior */ |
---|
1600 | if(!d->tax) |
---|
1601 | { |
---|
1602 | cvr = 1./(1./V1 + 1./V2 + 1./V3); |
---|
1603 | cer = cvr*(U0/V1 + U2/V2 + U3/V3); |
---|
1604 | } |
---|
1605 | else |
---|
1606 | { |
---|
1607 | cvr = V1; |
---|
1608 | cer = U0; |
---|
1609 | } |
---|
1610 | |
---|
1611 | if(cvr < 0.0) |
---|
1612 | { |
---|
1613 | PhyML_Printf("\n. cvr=%f d->tax=%d V1=%f v2=%f V3=%f",cvr,d->tax,V1,V2,V3); |
---|
1614 | PhyML_Printf("\n. T0=%f T1=%f T2=%f T3=%f",T0,T1,T2,T3); |
---|
1615 | Exit("\n"); |
---|
1616 | } |
---|
1617 | |
---|
1618 | prior_mean = cer; |
---|
1619 | prior_var = cvr; |
---|
1620 | |
---|
1621 | /* Posterior */ |
---|
1622 | post_mean = (prior_mean/prior_var + like_mean/like_var)/(1./prior_var + 1./like_var); |
---|
1623 | post_var = 1./(1./prior_var + 1./like_var); |
---|
1624 | |
---|
1625 | |
---|
1626 | /* Sample according to priors */ |
---|
1627 | if(tree->mcmc->use_data == NO) |
---|
1628 | { |
---|
1629 | post_mean = prior_mean; |
---|
1630 | post_var = prior_var; |
---|
1631 | } |
---|
1632 | |
---|
1633 | post_sd = SQRT(post_var); |
---|
1634 | |
---|
1635 | rd = Rnorm_Trunc(post_mean,inflate_var*post_sd,r_min,r_max,&err); |
---|
1636 | |
---|
1637 | if(err || isnan(rd)) |
---|
1638 | { |
---|
1639 | PhyML_Printf("\n"); |
---|
1640 | PhyML_Printf("\n. run: %d err=%d d->tax=%d",tree->mcmc->run,err,d->tax); |
---|
1641 | PhyML_Printf("\n. rd=%f cvr=%G dt=%G cr=%G",rd,cvr,dt,cr); |
---|
1642 | PhyML_Printf("\n. prior_mean = %G prior_var = %G",prior_mean,prior_var); |
---|
1643 | PhyML_Printf("\n. like_mean = %G like_var = %G",like_mean,like_var); |
---|
1644 | PhyML_Printf("\n. post_mean = %G post_var = %G",post_mean,post_var); |
---|
1645 | PhyML_Printf("\n. clock_r = %f",tree->rates->clock_r); |
---|
1646 | PhyML_Printf("\n. T0=%f T1=%f T2=%f T3=%f",T0,T1,T2,T3); |
---|
1647 | PhyML_Printf("\n. U0=%f U1=%f U2=%f U3=%f",U0,U1,U2,U3); |
---|
1648 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
1649 | Exit("\n"); |
---|
1650 | } |
---|
1651 | |
---|
1652 | /* !!!!!!!!!!!!!! */ |
---|
1653 | /* u = Uni(); */ |
---|
1654 | /* rd = U1 * EXP(1.*(u-0.5)); */ |
---|
1655 | |
---|
1656 | if(rd > r_min && rd < r_max) |
---|
1657 | { |
---|
1658 | |
---|
1659 | cur_lnL_data = tree->c_lnL; |
---|
1660 | cur_lnL_rate = tree->rates->c_lnL_rates; |
---|
1661 | new_lnL_data = tree->c_lnL; |
---|
1662 | new_lnL_rate = tree->rates->c_lnL_rates; |
---|
1663 | |
---|
1664 | tree->rates->br_r[d->num] = rd; |
---|
1665 | RATES_Update_Norm_Fact(tree); |
---|
1666 | RATES_Update_Cur_Bl(tree); |
---|
1667 | |
---|
1668 | if(tree->mcmc->use_data) new_lnL_data = Lk(b,tree); |
---|
1669 | /* new_lnL_rate = RATES_Lk_Rates(tree); */ |
---|
1670 | new_lnL_rate = |
---|
1671 | cur_lnL_rate - |
---|
1672 | (Log_Dnorm_Trunc(U1,U0,sd1,r_min,r_max,&err)) + |
---|
1673 | (Log_Dnorm_Trunc(rd,U0,sd1,r_min,r_max,&err)); |
---|
1674 | |
---|
1675 | if(!d->tax) |
---|
1676 | { |
---|
1677 | new_lnL_rate -= (Log_Dnorm_Trunc(U2,U1,sd2,r_min,r_max,&err) + Log_Dnorm_Trunc(U3,U1,sd3,r_min,r_max,&err)); |
---|
1678 | new_lnL_rate += (Log_Dnorm_Trunc(U2,rd,sd2,r_min,r_max,&err) + Log_Dnorm_Trunc(U3,rd,sd3,r_min,r_max,&err)); |
---|
1679 | } |
---|
1680 | |
---|
1681 | tree->rates->c_lnL_rates = new_lnL_rate; |
---|
1682 | |
---|
1683 | /* printf("\n. %f %f sd1=%f U1=%f rd=%f ra=%f a=%d d=%d [%f] [%f %f]", */ |
---|
1684 | /* new_lnL_rate,RATES_Lk_Rates(tree),sd1,U1,rd,ra,a->num,d->num,tree->rates->br_r[tree->n_root->num], */ |
---|
1685 | /* Log_Dnorm_Trunc(U1,ra,sd1,r_min,r_max,&err), */ |
---|
1686 | /* Log_Dnorm_Trunc(rd,ra,sd1,r_min,r_max,&err)); */ |
---|
1687 | |
---|
1688 | ratio = 0.0; |
---|
1689 | /* Proposal ratio */ |
---|
1690 | ratio += (Log_Dnorm_Trunc(U1,post_mean,inflate_var*post_sd,r_min,r_max,&err) - Log_Dnorm_Trunc(rd,post_mean,inflate_var*post_sd,r_min,r_max,&err)); |
---|
1691 | /* ratio += LOG(rd/U1); */ |
---|
1692 | /* Prior ratio */ |
---|
1693 | ratio += (new_lnL_rate - cur_lnL_rate); |
---|
1694 | /* Likelihood ratio */ |
---|
1695 | ratio += (new_lnL_data - cur_lnL_data); |
---|
1696 | |
---|
1697 | |
---|
1698 | ratio = EXP(ratio); |
---|
1699 | |
---|
1700 | /* printf("\n. R a=%3d T0=%6.1f T1=%6.1f T2=%6.1f T3=%6.1f ratio=%8f pm=%7f U1=%7.2f rd=%7.2f %f %f lr=%f %f ld=%f %f [%f]",a->num,T0,T1,T2,T3,ratio,post_mean,U1,rd, */ |
---|
1701 | /* Log_Dnorm_Trunc(U1,post_mean,post_sd,r_min,r_max,&err), */ |
---|
1702 | /* Log_Dnorm_Trunc(rd,post_mean,post_sd,r_min,r_max,&err), */ |
---|
1703 | /* new_lnL_rate,cur_lnL_rate, */ |
---|
1704 | /* new_lnL_data,cur_lnL_data, */ |
---|
1705 | /* ((Pnorm(r_max,U1,sd2)-Pnorm(r_min,U1,sd2)) * */ |
---|
1706 | /* (Pnorm(r_max,U1,sd3)-Pnorm(r_min,U1,sd3)))/ */ |
---|
1707 | /* ((Pnorm(r_max,rd,sd2)-Pnorm(r_min,rd,sd2)) * */ |
---|
1708 | /* (Pnorm(r_max,rd,sd3)-Pnorm(r_min,rd,sd3)))); */ |
---|
1709 | |
---|
1710 | |
---|
1711 | u = Uni(); |
---|
1712 | |
---|
1713 | if(u > MIN(1.,ratio)) |
---|
1714 | { |
---|
1715 | tree->rates->br_r[d->num] = U1; /* reject */ |
---|
1716 | tree->rates->c_lnL_rates = cur_lnL_rate; |
---|
1717 | tree->c_lnL = cur_lnL_data; |
---|
1718 | RATES_Update_Cur_Bl(tree); |
---|
1719 | Update_PMat_At_Given_Edge(b,tree); |
---|
1720 | } |
---|
1721 | else |
---|
1722 | { |
---|
1723 | tree->mcmc->acc_move[tree->mcmc->num_move_br_r+d->num]++; |
---|
1724 | } |
---|
1725 | |
---|
1726 | RATES_Update_Norm_Fact(tree); |
---|
1727 | tree->mcmc->acc_rate[tree->mcmc->num_move_br_r+d->num] = |
---|
1728 | (tree->mcmc->acc_move[tree->mcmc->num_move_br_r+d->num]+1.E-6)/ |
---|
1729 | (tree->mcmc->run_move[tree->mcmc->num_move_br_r+d->num]+1.E-6); |
---|
1730 | } |
---|
1731 | |
---|
1732 | tree->mcmc->run_move[tree->mcmc->num_move_br_r+d->num]++; |
---|
1733 | |
---|
1734 | |
---|
1735 | if(traversal == YES) |
---|
1736 | { |
---|
1737 | if(d->tax == YES) return; |
---|
1738 | else |
---|
1739 | { |
---|
1740 | For(i,3) |
---|
1741 | if(d->v[i] != a && d->b[i] != tree->e_root) |
---|
1742 | { |
---|
1743 | if(tree->io->lk_approx == EXACT && tree->mcmc->use_data) Update_P_Lk(tree,d->b[i],d); |
---|
1744 | /* if(tree->io->lk_approx == EXACT && tree->mcmc->use_data) { tree->both_sides = YES; Lk(tree); } */ |
---|
1745 | RATES_Posterior_One_Rate(d,d->v[i],YES,tree); |
---|
1746 | } |
---|
1747 | } |
---|
1748 | |
---|
1749 | if(tree->io->lk_approx == EXACT && tree->mcmc->use_data) Update_P_Lk(tree,b,d); |
---|
1750 | /* if(tree->io->lk_approx == EXACT && tree->mcmc->use_data) { tree->both_sides = YES; Lk(tree); } */ |
---|
1751 | } |
---|
1752 | } |
---|
1753 | |
---|
1754 | ////////////////////////////////////////////////////////////// |
---|
1755 | ////////////////////////////////////////////////////////////// |
---|
1756 | |
---|
1757 | |
---|
1758 | void RATES_Posterior_One_Time(t_node *a, t_node *d, int traversal, t_tree *tree) |
---|
1759 | { |
---|
1760 | /* |
---|
1761 | T0 (a) |
---|
1762 | | |
---|
1763 | | l1,U1,b1 |
---|
1764 | | |
---|
1765 | T1 (d) |
---|
1766 | / \ |
---|
1767 | l2,u2,b2 / \ L3,u3,b3 |
---|
1768 | / \ |
---|
1769 | t2 t3 |
---|
1770 | (v2) (v3) |
---|
1771 | |
---|
1772 | */ |
---|
1773 | |
---|
1774 | phydbl l1,l2,l3; |
---|
1775 | phydbl new_l1; |
---|
1776 | phydbl El1,El2,El3; |
---|
1777 | phydbl u0,r1,r2,r3; |
---|
1778 | phydbl t0,t1,t2,t3; |
---|
1779 | phydbl t_min, t_max; |
---|
1780 | /* phydbl t_max_12,t_max_13; */ |
---|
1781 | phydbl bl_min, bl_max; |
---|
1782 | phydbl t1_new; |
---|
1783 | phydbl EX,EY; |
---|
1784 | phydbl VX,VY; |
---|
1785 | phydbl cr; |
---|
1786 | int i,j; |
---|
1787 | phydbl *mu, *cov; |
---|
1788 | phydbl *cond_mu, *cond_cov; |
---|
1789 | short int *is_1; |
---|
1790 | phydbl sig11, sig1X, sig1Y, sigXX, sigXY, sigYY; |
---|
1791 | phydbl cov11,cov12,cov13,cov22,cov23,cov33; |
---|
1792 | int dim; |
---|
1793 | t_edge *b1, *b2, *b3; |
---|
1794 | t_node *v2,*v3; |
---|
1795 | phydbl *l2XY; |
---|
1796 | phydbl l_opp; |
---|
1797 | t_edge *buff_b; |
---|
1798 | t_node *buff_n; |
---|
1799 | int err; |
---|
1800 | int num_1, num_2, num_3; |
---|
1801 | phydbl nf; |
---|
1802 | phydbl u, ratio; |
---|
1803 | phydbl new_lnL_data, cur_lnL_data, new_lnL_rate, cur_lnL_rate; |
---|
1804 | int num_move; |
---|
1805 | phydbl inflate_var; |
---|
1806 | |
---|
1807 | dim = 2*tree->n_otu-3; |
---|
1808 | num_move = tree->mcmc->num_move_nd_t+d->num-tree->n_otu; |
---|
1809 | inflate_var = tree->rates->inflate_var; |
---|
1810 | |
---|
1811 | if(d->tax) return; |
---|
1812 | |
---|
1813 | if(FABS(tree->rates->t_prior_min[d->num] - tree->rates->t_prior_max[d->num]) < 1.E-10) return; |
---|
1814 | |
---|
1815 | l2XY = tree->rates->_2n_vect2; |
---|
1816 | mu = tree->rates->_2n_vect3; |
---|
1817 | cov = tree->rates->_2n2n_vect1; |
---|
1818 | cond_mu = tree->rates->_2n_vect1; |
---|
1819 | cond_cov = tree->rates->_2n2n_vect2; |
---|
1820 | is_1 = tree->rates->_2n_vect5; |
---|
1821 | err = NO; |
---|
1822 | |
---|
1823 | b1 = NULL; |
---|
1824 | if(a == tree->n_root) b1 = tree->e_root; |
---|
1825 | else For(i,3) if(d->v[i] == a) { b1 = d->b[i]; break; } |
---|
1826 | |
---|
1827 | b2 = b3 = NULL; |
---|
1828 | v2 = v3 = NULL; |
---|
1829 | For(i,3) |
---|
1830 | if((d->v[i] != a) && (d->b[i] != tree->e_root)) |
---|
1831 | { |
---|
1832 | if(!v2) { v2 = d->v[i]; b2 = d->b[i]; } |
---|
1833 | else { v3 = d->v[i]; b3 = d->b[i]; } |
---|
1834 | } |
---|
1835 | |
---|
1836 | t2 = tree->rates->nd_t[v2->num]; |
---|
1837 | t3 = tree->rates->nd_t[v3->num]; |
---|
1838 | |
---|
1839 | buff_n = NULL; |
---|
1840 | buff_b = NULL; |
---|
1841 | if(t3 > t2) |
---|
1842 | { |
---|
1843 | buff_n = v2; |
---|
1844 | v2 = v3; |
---|
1845 | v3 = buff_n; |
---|
1846 | |
---|
1847 | buff_b = b2; |
---|
1848 | b2 = b3; |
---|
1849 | b3 = buff_b; |
---|
1850 | } |
---|
1851 | |
---|
1852 | t0 = tree->rates->nd_t[a->num]; |
---|
1853 | t1 = tree->rates->nd_t[d->num]; |
---|
1854 | t2 = tree->rates->nd_t[v2->num]; |
---|
1855 | t3 = tree->rates->nd_t[v3->num]; |
---|
1856 | u0 = tree->rates->br_r[a->num]; |
---|
1857 | r1 = tree->rates->br_r[d->num]; |
---|
1858 | r2 = tree->rates->br_r[v2->num]; |
---|
1859 | r3 = tree->rates->br_r[v3->num]; |
---|
1860 | l1 = tree->rates->cur_l[d->num]; |
---|
1861 | l2 = tree->rates->cur_l[v2->num]; |
---|
1862 | l3 = tree->rates->cur_l[v3->num]; |
---|
1863 | cr = tree->rates->clock_r; |
---|
1864 | nf = tree->rates->norm_fact; |
---|
1865 | |
---|
1866 | For(i,dim) is_1[i] = 0; |
---|
1867 | is_1[b1->num] = 1; |
---|
1868 | is_1[b2->num] = 1; |
---|
1869 | is_1[b3->num] = 1; |
---|
1870 | |
---|
1871 | /* For(i,dim) cond_mu[i] = 0.0; */ |
---|
1872 | /* For(i,dim*dim) cond_cov[i] = 0.0; */ |
---|
1873 | /* Normal_Conditional(tree->rates->mean_l,tree->rates->cov_l,tree->rates->u_cur_l,dim,is_1,3,cond_mu,cond_cov); */ |
---|
1874 | |
---|
1875 | /* El1 = cond_mu[b1->num]; */ |
---|
1876 | /* El2 = cond_mu[b2->num]; */ |
---|
1877 | /* El3 = cond_mu[b3->num]; */ |
---|
1878 | |
---|
1879 | /* cov11 = cond_cov[b1->num*dim+b1->num]; */ |
---|
1880 | /* cov12 = cond_cov[b1->num*dim+b2->num]; */ |
---|
1881 | /* cov13 = cond_cov[b1->num*dim+b3->num]; */ |
---|
1882 | /* cov23 = cond_cov[b2->num*dim+b3->num]; */ |
---|
1883 | /* cov22 = cond_cov[b2->num*dim+b2->num]; */ |
---|
1884 | /* cov33 = cond_cov[b3->num*dim+b3->num]; */ |
---|
1885 | |
---|
1886 | |
---|
1887 | /* El1 = tree->rates->u_ml_l[b1->num]; */ |
---|
1888 | /* El2 = tree->rates->u_ml_l[b2->num]; */ |
---|
1889 | /* El3 = tree->rates->u_ml_l[b3->num]; */ |
---|
1890 | |
---|
1891 | /* cov11 = tree->rates->cov[b1->num*dim+b1->num]; */ |
---|
1892 | /* cov12 = tree->rates->cov[b1->num*dim+b2->num]; */ |
---|
1893 | /* cov13 = tree->rates->cov[b1->num*dim+b3->num]; */ |
---|
1894 | /* cov23 = tree->rates->cov[b2->num*dim+b3->num]; */ |
---|
1895 | /* cov22 = tree->rates->cov[b2->num*dim+b2->num]; */ |
---|
1896 | /* cov33 = tree->rates->cov[b3->num*dim+b3->num]; */ |
---|
1897 | |
---|
1898 | /* PhyML_Printf("\n- El1=%10f El2=%10f El3=%10f",El1,El2,El3); */ |
---|
1899 | /* PhyML_Printf("\n- cov11=%10f cov12=%10f cov13=%10f cov23=%10f cov22=%10f cov33=%10f", cov11,cov12,cov13,cov23,cov22,cov33); */ |
---|
1900 | |
---|
1901 | num_1 = num_2 = num_3 = -1; |
---|
1902 | if(b1->num < b2->num && b2->num < b3->num) { num_1 = 0; num_2 = 1; num_3 = 2; } |
---|
1903 | if(b1->num < b3->num && b3->num < b2->num) { num_1 = 0; num_2 = 2; num_3 = 1; } |
---|
1904 | if(b2->num < b1->num && b1->num < b3->num) { num_1 = 1; num_2 = 0; num_3 = 2; } |
---|
1905 | if(b2->num < b3->num && b3->num < b1->num) { num_1 = 2; num_2 = 0; num_3 = 1; } |
---|
1906 | if(b3->num < b2->num && b2->num < b1->num) { num_1 = 2; num_2 = 1; num_3 = 0; } |
---|
1907 | if(b3->num < b1->num && b1->num < b2->num) { num_1 = 1; num_2 = 2; num_3 = 0; } |
---|
1908 | |
---|
1909 | cov11 = tree->rates->trip_cond_cov[d->num * 9 + num_1 * 3 + num_1]; |
---|
1910 | cov12 = tree->rates->trip_cond_cov[d->num * 9 + num_1 * 3 + num_2]; |
---|
1911 | cov13 = tree->rates->trip_cond_cov[d->num * 9 + num_1 * 3 + num_3]; |
---|
1912 | cov23 = tree->rates->trip_cond_cov[d->num * 9 + num_2 * 3 + num_3]; |
---|
1913 | cov22 = tree->rates->trip_cond_cov[d->num * 9 + num_2 * 3 + num_2]; |
---|
1914 | cov33 = tree->rates->trip_cond_cov[d->num * 9 + num_3 * 3 + num_3]; |
---|
1915 | |
---|
1916 | El1=0.0; |
---|
1917 | For(i,dim) |
---|
1918 | if(i != b1->num && i != b2->num && i != b3->num) |
---|
1919 | El1 += tree->rates->trip_reg_coeff[d->num * (6*tree->n_otu-9) + num_1 * dim +i] * (tree->rates->u_cur_l[i] - tree->rates->mean_l[i]); |
---|
1920 | El1 += tree->rates->mean_l[b1->num]; |
---|
1921 | |
---|
1922 | El2=0.0; |
---|
1923 | For(i,dim) |
---|
1924 | if(i != b1->num && i != b2->num && i != b3->num) |
---|
1925 | El2 += tree->rates->trip_reg_coeff[d->num * (6*tree->n_otu-9) + num_2 * dim +i] * (tree->rates->u_cur_l[i] - tree->rates->mean_l[i]); |
---|
1926 | El2 += tree->rates->mean_l[b2->num]; |
---|
1927 | |
---|
1928 | El3=0.0; |
---|
1929 | For(i,dim) |
---|
1930 | if(i != b1->num && i != b2->num && i != b3->num) |
---|
1931 | El3 += tree->rates->trip_reg_coeff[d->num * (6*tree->n_otu-9) + num_3 * dim +i] * (tree->rates->u_cur_l[i] - tree->rates->mean_l[i]); |
---|
1932 | El3 += tree->rates->mean_l[b3->num]; |
---|
1933 | |
---|
1934 | |
---|
1935 | /* PhyML_Printf("\n+ El1=%10f El2=%10f El3=%10f",El1,El2,El3); */ |
---|
1936 | /* PhyML_Printf("\n+ cov11=%10f cov12=%10f cov13=%10f cov23=%10f cov22=%10f cov33=%10f", cov11,cov12,cov13,cov23,cov22,cov33); */ |
---|
1937 | |
---|
1938 | |
---|
1939 | t1_new = +1; |
---|
1940 | |
---|
1941 | t_min = MAX(t0,tree->rates->t_prior_min[d->num]); |
---|
1942 | t_max = MIN(MIN(t2,t3),tree->rates->t_prior_max[d->num]); |
---|
1943 | |
---|
1944 | t_min += tree->rates->min_dt; |
---|
1945 | t_max -= tree->rates->min_dt; |
---|
1946 | |
---|
1947 | if(t_min > t_max) |
---|
1948 | { |
---|
1949 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
1950 | Exit("\n"); |
---|
1951 | } |
---|
1952 | |
---|
1953 | bl_min = bl_max = -1.0; |
---|
1954 | |
---|
1955 | l_opp = 0.0; |
---|
1956 | if(a == tree->n_root) |
---|
1957 | { |
---|
1958 | l_opp = (d == a->v[0])?(tree->rates->cur_l[a->v[1]->num]):(tree->rates->cur_l[a->v[0]->num]); |
---|
1959 | El1 -= l_opp; |
---|
1960 | } |
---|
1961 | |
---|
1962 | EX = El1/r1 + El2/r2; |
---|
1963 | EY = El1/r1 + El3/r3; |
---|
1964 | |
---|
1965 | VX = cov11/(r1*r1) + cov22/(r2*r2) + 2.*cov12/(r1*r2); |
---|
1966 | VY = cov11/(r1*r1) + cov33/(r3*r3) + 2.*cov13/(r1*r3); |
---|
1967 | |
---|
1968 | mu[0] = El1; |
---|
1969 | mu[1] = EX; |
---|
1970 | mu[2] = EY; |
---|
1971 | |
---|
1972 | sig11 = cov11; |
---|
1973 | sig1X = cov11/r1 + cov12/r2; |
---|
1974 | sig1Y = cov11/r1 + cov13/r3; |
---|
1975 | sigXX = VX; |
---|
1976 | sigYY = VY; |
---|
1977 | sigXY = cov11/(r1*r1) + cov13/(r1*r3) + cov12/(r1*r2) + cov23/(r2*r3); |
---|
1978 | |
---|
1979 | cov[0*3+0] = sig11; cov[0*3+1] = sig1X; cov[0*3+2] = sig1Y; |
---|
1980 | cov[1*3+0] = sig1X; cov[1*3+1] = sigXX; cov[1*3+2] = sigXY; |
---|
1981 | cov[2*3+0] = sig1Y; cov[2*3+1] = sigXY; cov[2*3+2] = sigYY; |
---|
1982 | |
---|
1983 | l2XY[0] = 0.0; /* does not matter */ |
---|
1984 | l2XY[1] = (t2-t0)*cr*nf; /* constraint 1 */ |
---|
1985 | l2XY[2] = (t3-t0)*cr*nf; /* constraint 2 */ |
---|
1986 | |
---|
1987 | is_1[0] = is_1[1] = is_1[2] = 0; |
---|
1988 | is_1[0] = 1; |
---|
1989 | |
---|
1990 | Normal_Conditional(mu,cov,l2XY,3,is_1,1,cond_mu,cond_cov); |
---|
1991 | |
---|
1992 | |
---|
1993 | if(cond_cov[0*3+0] < 0.0) |
---|
1994 | { |
---|
1995 | PhyML_Printf("\n. a: %d d: %d",a->num,d->num); |
---|
1996 | PhyML_Printf("\n. Conditional mean=%G var=%G",cond_mu[0],cond_cov[0*3+0]); |
---|
1997 | PhyML_Printf("\n. t0=%G t1=%f t2=%f t3=%f l1=%G l2=%G l3=%G",t0,t1,t2,t3,l1,l2,l3); |
---|
1998 | PhyML_Printf("\n. El1=%G El2=%G El3=%G Nu=%G r1=%G r2=%G r3=%G cr=%G",El1,El2,El3,tree->rates->nu,r1,r2,r3,cr); |
---|
1999 | PhyML_Printf("\n. COV11=%f COV12=%f COV13=%f COV22=%f COV23=%f COV33=%f",cov11,cov12,cov13,cov22,cov23,cov33); |
---|
2000 | PhyML_Printf("\n. constraint1: %f constraints2: %f",l2XY[1],l2XY[2]); |
---|
2001 | PhyML_Printf("\n"); |
---|
2002 | For(i,3) |
---|
2003 | { |
---|
2004 | PhyML_Printf(". mu%d=%12lf\t",i,mu[i]); |
---|
2005 | For(j,3) |
---|
2006 | { |
---|
2007 | PhyML_Printf("%12lf ",cov[i*3+j]); |
---|
2008 | } |
---|
2009 | PhyML_Printf("\n"); |
---|
2010 | } |
---|
2011 | cond_cov[0*3+0] = 1.E-10; |
---|
2012 | } |
---|
2013 | |
---|
2014 | |
---|
2015 | bl_min = (t_min - t0) * r1 * cr * nf; |
---|
2016 | bl_max = (t_max - t0) * r1 * cr * nf; |
---|
2017 | |
---|
2018 | new_l1 = Rnorm_Trunc(cond_mu[0],inflate_var*SQRT(cond_cov[0*3+0]),bl_min,bl_max,&err); |
---|
2019 | /* new_l1 = Rnorm(cond_mu[0],SQRT(cond_cov[0*3+0])); */ |
---|
2020 | |
---|
2021 | |
---|
2022 | if(new_l1 < bl_min) new_l1 = l1; |
---|
2023 | if(new_l1 > bl_max) new_l1 = l1; |
---|
2024 | |
---|
2025 | t1_new = new_l1/(r1*cr*nf) + t0; |
---|
2026 | |
---|
2027 | |
---|
2028 | if(err) |
---|
2029 | { |
---|
2030 | PhyML_Printf("\n"); |
---|
2031 | PhyML_Printf("\n. Root ? %s",(tree->n_root==a)?("yes"):("no")); |
---|
2032 | PhyML_Printf("\n. %s %d %d",__FILE__,__LINE__,tree->mcmc->run); |
---|
2033 | PhyML_Printf("\n. t0=%f t1=%f t2=%f t3=%f",t0,t1,t2,t3); |
---|
2034 | PhyML_Printf("\n. t_min=%f t_max=%f",t_min,t_max); |
---|
2035 | PhyML_Printf("\n. bl_min=%f bl_max=%f",bl_min,bl_max); |
---|
2036 | PhyML_Printf("\n. cond_mu[0]=%f cond_cov[0]=%f",cond_mu[0],SQRT(cond_cov[0*3+0])); |
---|
2037 | PhyML_Printf("\n. El1=%f El2=%f El3=%f",El1,El2,El3); |
---|
2038 | PhyML_Printf("\n. l1=%f l2=%f l3=%f",l1,l2,l3); |
---|
2039 | PhyML_Printf("\n. u0=%f r1=%f r2=%f r3=%f",u0,r1,r2,r3); |
---|
2040 | PhyML_Printf("\n. COV11=%f COV22=%f",cov11,cov22); |
---|
2041 | PhyML_Printf("\n. Clock rate = %f",tree->rates->clock_r); |
---|
2042 | PhyML_Printf("\n. Setting t1_new to %f",t1); |
---|
2043 | t1_new = t1; |
---|
2044 | /* Exit("\n"); */ |
---|
2045 | } |
---|
2046 | /* } */ |
---|
2047 | /* else */ |
---|
2048 | /* { */ |
---|
2049 | /* bl_min = (t2 - t_max) * r2; */ |
---|
2050 | /* bl_max = (t2 - t_min) * r2; */ |
---|
2051 | |
---|
2052 | /* l2 = Rnorm_Trunc(cond_mu[0],SQRT(cond_cov[0*3+0]),bl_min,bl_max,&err); */ |
---|
2053 | /* t1_new = -l2/r2 + t2; */ |
---|
2054 | |
---|
2055 | /* if(err) */ |
---|
2056 | /* { */ |
---|
2057 | /* PhyML_Printf("\n"); */ |
---|
2058 | /* PhyML_Printf("\n. %s %d %d",__FILE__,__LINE__,tree->mcmc->run); */ |
---|
2059 | /* PhyML_Printf("\n. t0=%f t1=%f t2=%f t3=%f",t0,t1,t2,t3); */ |
---|
2060 | /* PhyML_Printf("\n. t_min=%f t_max=%f",t_min,t_max); */ |
---|
2061 | /* PhyML_Printf("\n. bl_min=%f bl_max=%f",bl_min,bl_max); */ |
---|
2062 | /* PhyML_Printf("\n. cond_mu[0]=%f cond_cov[0]=%f",cond_mu[0],SQRT(cond_cov[0*3+0])); */ |
---|
2063 | /* PhyML_Printf("\n. El1=%f El2=%f El3=%f",El1,El2,El3); */ |
---|
2064 | /* PhyML_Printf("\n. l1=%f l2=%f l3=%f",l1,l2,l3); */ |
---|
2065 | /* PhyML_Printf("\n. u0=%f r1=%f r2=%f r3=%f",u0,r1,r2,r3); */ |
---|
2066 | /* PhyML_Printf("\n. COV11=%f COV22=%f",cov11,cov22); */ |
---|
2067 | /* PhyML_Printf("\n. Clock rate = %f",tree->rates->clock_r); */ |
---|
2068 | /* PhyML_Printf("\n. Setting t1_new to %f",t1); */ |
---|
2069 | /* t1_new = t1; */ |
---|
2070 | /* /\* Exit("\n"); *\/ */ |
---|
2071 | /* } */ |
---|
2072 | /* } */ |
---|
2073 | |
---|
2074 | if(t1_new < t0) |
---|
2075 | { |
---|
2076 | t1_new = t0+1.E-4; |
---|
2077 | PhyML_Printf("\n"); |
---|
2078 | PhyML_Printf("\n. a is root -> %s",(a == tree->n_root)?("YES"):("NO")); |
---|
2079 | PhyML_Printf("\n. t0 = %f t1_new = %f t1 = %f",t0,t1_new,t1); |
---|
2080 | PhyML_Printf("\n. t_min=%f t_max=%f",t_min,t_max); |
---|
2081 | PhyML_Printf("\n. l1 = %f",l1); |
---|
2082 | PhyML_Printf("\n. bl_min = %f bl_max = %f",bl_min,bl_max); |
---|
2083 | PhyML_Printf("\n. (t1-t0)=%f (t2-t1)=%f",t1-t0,t2-t1); |
---|
2084 | PhyML_Printf("\n. l1 = %f l2 = %f cov11=%f cov22=%f cov33=%f",l1,l2,cov11,cov22,cov33); |
---|
2085 | PhyML_Printf("\n. clock=%G",tree->rates->clock_r); |
---|
2086 | PhyML_Printf("\n. u0=%f r1=%f r2=%f r3=%f",u0,r1,r2,r3); |
---|
2087 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
2088 | /* Exit("\n"); */ |
---|
2089 | } |
---|
2090 | if(t1_new > MIN(t2,t3)) |
---|
2091 | { |
---|
2092 | PhyML_Printf("\n"); |
---|
2093 | PhyML_Printf("\n. a is root -> %s",(a == tree->n_root)?("YES"):("NO")); |
---|
2094 | PhyML_Printf("\n. t0 = %f t1_new = %f t1 = %f t2 = %f t3 = %f MIN(t2,t3)=%f",t0,t1_new,t1,t2,t3,MIN(t2,t3)); |
---|
2095 | PhyML_Printf("\n. t_min=%f t_max=%f",t_min,t_max); |
---|
2096 | PhyML_Printf("\n. l2 = %f",l2); |
---|
2097 | PhyML_Printf("\n. bl_min = %f bl_max = %f",bl_min,bl_max); |
---|
2098 | PhyML_Printf("\n. (t1-t0)=%f (t2-t1)=%f",t1-t0,t2-t1); |
---|
2099 | PhyML_Printf("\n. l1 = %f l2 = %f cov11=%f cov22=%f cov33=%f",l1,l2,cov11,cov22,cov33); |
---|
2100 | PhyML_Printf("\n. clock=%G",tree->rates->clock_r); |
---|
2101 | PhyML_Printf("\n. u0=%f r1=%f r2=%f r3=%f",u0,r1,r2,r3); |
---|
2102 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
2103 | /* Exit("\n"); */ |
---|
2104 | } |
---|
2105 | |
---|
2106 | if(isnan(t1_new)) |
---|
2107 | { |
---|
2108 | PhyML_Printf("\n. run=%d",tree->mcmc->run); |
---|
2109 | PhyML_Printf("\n. mean=%G var=%G",cond_mu[0],cond_cov[0*3+0]); |
---|
2110 | PhyML_Printf("\n. t1=%f l1=%G r1=%G t0=%G",t1,l1,r1,t0); |
---|
2111 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
2112 | /* Exit("\n"); */ |
---|
2113 | } |
---|
2114 | |
---|
2115 | if(tree->mcmc->use_data == YES) |
---|
2116 | tree->rates->nd_t[d->num] = t1_new; |
---|
2117 | else |
---|
2118 | { |
---|
2119 | u = Uni(); |
---|
2120 | tree->rates->nd_t[d->num] = u*(t_max-t_min) + t_min; |
---|
2121 | } |
---|
2122 | |
---|
2123 | RATES_Update_Norm_Fact(tree); |
---|
2124 | RATES_Update_Cur_Bl(tree); |
---|
2125 | |
---|
2126 | cur_lnL_data = tree->c_lnL; |
---|
2127 | cur_lnL_rate = tree->rates->c_lnL_rates; |
---|
2128 | new_lnL_data = tree->c_lnL; |
---|
2129 | new_lnL_rate = tree->rates->c_lnL_rates; |
---|
2130 | |
---|
2131 | if(tree->mcmc->use_data) |
---|
2132 | { |
---|
2133 | /* !!!!!!!!!!!!!!!!! */ |
---|
2134 | /* tree->both_sides = NO; */ |
---|
2135 | /* new_lnL_data = Lk(tree); */ |
---|
2136 | |
---|
2137 | if(tree->io->lk_approx == EXACT) |
---|
2138 | { |
---|
2139 | Update_PMat_At_Given_Edge(b1,tree); |
---|
2140 | Update_PMat_At_Given_Edge(b2,tree); |
---|
2141 | Update_PMat_At_Given_Edge(b3,tree); |
---|
2142 | Update_P_Lk(tree,b1,d); |
---|
2143 | } |
---|
2144 | new_lnL_data = Lk(b1,tree); |
---|
2145 | } |
---|
2146 | |
---|
2147 | new_lnL_rate = RATES_Lk_Rates(tree); |
---|
2148 | |
---|
2149 | ratio = 0.0; |
---|
2150 | |
---|
2151 | /* Proposal ratio */ |
---|
2152 | if(tree->mcmc->use_data) |
---|
2153 | ratio += (Log_Dnorm_Trunc(l1, cond_mu[0],inflate_var*SQRT(cond_cov[0*3+0]),bl_min,bl_max,&err) - |
---|
2154 | Log_Dnorm_Trunc(new_l1,cond_mu[0],inflate_var*SQRT(cond_cov[0*3+0]),bl_min,bl_max,&err)); |
---|
2155 | |
---|
2156 | /* Prior ratio */ |
---|
2157 | ratio += (new_lnL_rate - cur_lnL_rate); |
---|
2158 | |
---|
2159 | /* Likelihood ratio */ |
---|
2160 | ratio += (new_lnL_data - cur_lnL_data); |
---|
2161 | |
---|
2162 | /* printf("\n* d:%d Ratio=%f l1=%f new_l1=%f mean=%f ml=%f sd=%f [%f %f]", */ |
---|
2163 | /* d->num, */ |
---|
2164 | /* EXP(ratio), */ |
---|
2165 | /* l1,new_l1, */ |
---|
2166 | /* cond_mu[0], */ |
---|
2167 | /* tree->rates->mean_l[b1->num], */ |
---|
2168 | /* SQRT(cond_cov[0*3+0]), */ |
---|
2169 | /* Log_Dnorm(l1,cond_mu[0],SQRT(cond_cov[0*3+0]),&err),Log_Dnorm(new_l1,cond_mu[0],SQRT(cond_cov[0*3+0]),&err)); */ |
---|
2170 | |
---|
2171 | ratio = EXP(ratio); |
---|
2172 | |
---|
2173 | |
---|
2174 | u = Uni(); |
---|
2175 | if(u > MIN(1.,ratio)) |
---|
2176 | { |
---|
2177 | tree->rates->nd_t[d->num] = t1; /* reject */ |
---|
2178 | tree->rates->c_lnL_rates = cur_lnL_rate; |
---|
2179 | tree->c_lnL = cur_lnL_data; |
---|
2180 | RATES_Update_Cur_Bl(tree); |
---|
2181 | if(tree->io->lk_approx == EXACT && tree->mcmc->use_data) |
---|
2182 | { |
---|
2183 | Update_PMat_At_Given_Edge(b1,tree); |
---|
2184 | Update_PMat_At_Given_Edge(b2,tree); |
---|
2185 | Update_PMat_At_Given_Edge(b3,tree); |
---|
2186 | Update_P_Lk(tree,b1,d); |
---|
2187 | } |
---|
2188 | } |
---|
2189 | else |
---|
2190 | { |
---|
2191 | tree->mcmc->acc_move[num_move]++; |
---|
2192 | } |
---|
2193 | |
---|
2194 | RATES_Update_Norm_Fact(tree); |
---|
2195 | |
---|
2196 | tree->mcmc->run_move[num_move]++; |
---|
2197 | |
---|
2198 | if(traversal == YES) |
---|
2199 | { |
---|
2200 | if(d->tax == YES) return; |
---|
2201 | else |
---|
2202 | { |
---|
2203 | For(i,3) |
---|
2204 | if(d->v[i] != a && d->b[i] != tree->e_root) |
---|
2205 | { |
---|
2206 | if(tree->io->lk_approx == EXACT && tree->mcmc->use_data) Update_P_Lk(tree,d->b[i],d); |
---|
2207 | RATES_Posterior_One_Time(d,d->v[i],YES,tree); |
---|
2208 | } |
---|
2209 | } |
---|
2210 | if(tree->io->lk_approx == EXACT && tree->mcmc->use_data) Update_P_Lk(tree,b1,d); |
---|
2211 | } |
---|
2212 | |
---|
2213 | } |
---|
2214 | |
---|
2215 | ////////////////////////////////////////////////////////////// |
---|
2216 | ////////////////////////////////////////////////////////////// |
---|
2217 | |
---|
2218 | |
---|
2219 | void RATES_Posterior_Time_Root(t_tree *tree) |
---|
2220 | { |
---|
2221 | /* |
---|
2222 | Root, T0 |
---|
2223 | / \ |
---|
2224 | l1 / \ l2 |
---|
2225 | / \ |
---|
2226 | v1,t1 v2,t2 |
---|
2227 | |
---|
2228 | */ |
---|
2229 | |
---|
2230 | phydbl t1,t2; |
---|
2231 | phydbl u0,u1,u2; |
---|
2232 | phydbl cel,cvl; |
---|
2233 | int i,dim; |
---|
2234 | t_edge *b; |
---|
2235 | t_node *root; |
---|
2236 | phydbl t0,t0_min, t0_max; |
---|
2237 | phydbl new_t0; |
---|
2238 | /* phydbl t_max_01, t_max_02; */ |
---|
2239 | int err; |
---|
2240 | phydbl cr; |
---|
2241 | phydbl bl_min, bl_max; |
---|
2242 | phydbl new_l; |
---|
2243 | phydbl new_lnL_data, cur_lnL_data, cur_lnL_rate; |
---|
2244 | phydbl u,ratio; |
---|
2245 | |
---|
2246 | dim = 2*tree->n_otu-3; |
---|
2247 | b = tree->e_root; |
---|
2248 | root = tree->n_root; |
---|
2249 | t0 = tree->rates->nd_t[root->num]; |
---|
2250 | t1 = tree->rates->nd_t[root->v[2]->num]; |
---|
2251 | t2 = tree->rates->nd_t[root->v[1]->num]; |
---|
2252 | u1 = tree->rates->br_r[root->v[2]->num]; |
---|
2253 | u2 = tree->rates->br_r[root->v[1]->num]; |
---|
2254 | u0 = 1.0; |
---|
2255 | cr = tree->rates->clock_r; |
---|
2256 | t0_min = -BIG; |
---|
2257 | t0_max = MIN(t1,t2); |
---|
2258 | |
---|
2259 | /* t0_max = MIN(t1 - (1./tree->rates->nu)*POW((u0-u1)/tree->rates->z_max,2), */ |
---|
2260 | /* t2 - (1./tree->rates->nu)*POW((u0-u2)/tree->rates->z_max,2)); */ |
---|
2261 | |
---|
2262 | /* if(u1 > u0) t_max_01 = t1 - (1./tree->rates->nu)*POW((u1-u0)/(PointNormal(tree->rates->p_max*Pnorm(.0,.0,1.))),2); */ |
---|
2263 | /* else t_max_01 = t1 - (1./tree->rates->nu)*POW((u1-u0)/(PointNormal(tree->rates->p_max*(1.-Pnorm(.0,.0,1.)))),2); */ |
---|
2264 | |
---|
2265 | /* if(u2 > u0) t_max_02 = t2 - (1./tree->rates->nu)*POW((u2-u0)/(PointNormal(tree->rates->p_max*Pnorm(.0,.0,1.))),2); */ |
---|
2266 | /* else t_max_02 = t2 - (1./tree->rates->nu)*POW((u2-u0)/(PointNormal(tree->rates->p_max*(1.-Pnorm(.0,.0,1.)))),2); */ |
---|
2267 | |
---|
2268 | |
---|
2269 | /* t_max_01 = RATES_Find_Max_Dt_Bisec(u1,u0,tree->rates->t_prior_min[root->num],t1,tree->rates->nu,tree->rates->p_max,(u1 < u0)?YES:NO); */ |
---|
2270 | /* t_max_02 = RATES_Find_Max_Dt_Bisec(u2,u0,tree->rates->t_prior_min[root->num],t2,tree->rates->nu,tree->rates->p_max,(u2 < u0)?YES:NO); */ |
---|
2271 | /* t0_max = MIN(t_max_01,t_max_02); */ |
---|
2272 | |
---|
2273 | /* RATES_Min_Max_Interval(u0,u1,u2,r3,t0,t2,t3,&t_min,&t_max,tree->rates->nu,tree->rates->p_max,tree); */ |
---|
2274 | |
---|
2275 | t0_min = MAX(t0_min,tree->rates->t_prior_min[root->num]); |
---|
2276 | t0_max = MIN(t0_max,tree->rates->t_prior_max[root->num]); |
---|
2277 | |
---|
2278 | t0_max -= tree->rates->min_dt; |
---|
2279 | |
---|
2280 | if(t0_min > t0_max) return; |
---|
2281 | |
---|
2282 | tree->rates->t_prior[root->num] = Uni()*(t0_max - t0_min) + t0_min; |
---|
2283 | |
---|
2284 | u0 *= cr; |
---|
2285 | u1 *= cr; |
---|
2286 | u2 *= cr; |
---|
2287 | |
---|
2288 | if(FABS(tree->rates->t_prior_min[root->num] - tree->rates->t_prior_max[root->num]) < 1.E-10) return; |
---|
2289 | |
---|
2290 | cel=0.0; |
---|
2291 | For(i,dim) if(i != b->num) cel += tree->rates->reg_coeff[b->num*dim+i] * (tree->rates->u_cur_l[i] - tree->rates->mean_l[i]); |
---|
2292 | cel += tree->rates->mean_l[b->num]; |
---|
2293 | |
---|
2294 | cvl = tree->rates->cond_var[b->num]; |
---|
2295 | |
---|
2296 | bl_min = u1 * (t1 - t0_max) + u2 * (t2 - t0_max); |
---|
2297 | bl_max = u1 * (t1 - t0_min) + u2 * (t2 - t0_min); |
---|
2298 | |
---|
2299 | if(bl_min > bl_max) return; |
---|
2300 | |
---|
2301 | new_l = Rnorm_Trunc(cel,SQRT(cvl),bl_min,bl_max,&err); |
---|
2302 | |
---|
2303 | new_t0 = (u1*t1 + u2*t2 - new_l)/(u1+u2); |
---|
2304 | |
---|
2305 | if(t0 > t1 || t0 > t2) |
---|
2306 | { |
---|
2307 | PhyML_Printf("\n"); |
---|
2308 | PhyML_Printf("\n. Run = %d",tree->mcmc->run); |
---|
2309 | PhyML_Printf("\n. t0=%f t1=%f t2=%f",t0,t1,t2); |
---|
2310 | PhyML_Printf("\n. t0_min=%f t0_max=%f",t0_min,t0_max); |
---|
2311 | PhyML_Printf("\n. new_l=%f cel=%f",new_l,cel); |
---|
2312 | PhyML_Printf("\n. u0=%f u1=%f u2=%f",u0/cr,u1/cr,u2/cr); |
---|
2313 | PhyML_Printf("\n. Nu = %f Clock = %f",tree->rates->nu,tree->rates->clock_r); |
---|
2314 | PhyML_Printf("\n. Setting t0 to %f",tree->rates->nd_t[root->num]); |
---|
2315 | return; |
---|
2316 | } |
---|
2317 | |
---|
2318 | if(t0 < t0_min || t0 > t0_max) |
---|
2319 | { |
---|
2320 | PhyML_Printf("\n"); |
---|
2321 | PhyML_Printf("\n. Run = %d",tree->mcmc->run); |
---|
2322 | PhyML_Printf("\n. t0=%f t1=%f t2=%f",t0,t1,t2); |
---|
2323 | PhyML_Printf("\n. t0_min=%f t0_max=%f",t0_min,t0_max); |
---|
2324 | PhyML_Printf("\n. u0=%f u1=%f u2=%f",u0/cr,u1/cr,u2/cr); |
---|
2325 | PhyML_Printf("\n. Nu = %f Clock = %f",tree->rates->nu,tree->rates->clock_r); |
---|
2326 | PhyML_Printf("\n. Setting t0 to %f",tree->rates->nd_t[root->num]); |
---|
2327 | t0 = tree->rates->nd_t[root->num]; |
---|
2328 | } |
---|
2329 | |
---|
2330 | /* Sample according to prior */ |
---|
2331 | /* tree->rates->nd_t[root->num] = tree->rates->t_prior[root->num]; */ |
---|
2332 | |
---|
2333 | /* Sample according to posterior */ |
---|
2334 | tree->rates->nd_t[root->num] = new_t0; |
---|
2335 | |
---|
2336 | RATES_Update_Norm_Fact(tree); |
---|
2337 | RATES_Update_Cur_Bl(tree); |
---|
2338 | |
---|
2339 | cur_lnL_data = tree->c_lnL; |
---|
2340 | cur_lnL_rate = tree->rates->c_lnL_rates; |
---|
2341 | new_lnL_data = tree->c_lnL; |
---|
2342 | |
---|
2343 | if(tree->mcmc->use_data) new_lnL_data = Lk(NULL,tree); |
---|
2344 | |
---|
2345 | ratio = 0.0; |
---|
2346 | /* Prior ratio */ |
---|
2347 | ratio += .0; |
---|
2348 | /* Likelihood ratio */ |
---|
2349 | ratio += new_lnL_data - cur_lnL_data; |
---|
2350 | |
---|
2351 | ratio = EXP(ratio); |
---|
2352 | u = Uni(); |
---|
2353 | if(u > MIN(1.,ratio)) |
---|
2354 | { |
---|
2355 | tree->rates->nd_t[root->num] = t0; /* reject */ |
---|
2356 | tree->rates->c_lnL_rates = cur_lnL_rate; |
---|
2357 | tree->c_lnL = cur_lnL_data; |
---|
2358 | } |
---|
2359 | else |
---|
2360 | { |
---|
2361 | tree->mcmc->acc_move[tree->mcmc->num_move_nd_t]++; |
---|
2362 | } |
---|
2363 | |
---|
2364 | RATES_Update_Norm_Fact(tree); |
---|
2365 | RATES_Update_Cur_Bl(tree); |
---|
2366 | |
---|
2367 | tree->mcmc->run_move[tree->mcmc->num_move_nd_t]++; |
---|
2368 | tree->mcmc->acc_rate[tree->mcmc->num_move_nd_t] = |
---|
2369 | (tree->mcmc->acc_move[tree->mcmc->num_move_nd_t]+1.E-6)/ |
---|
2370 | (tree->mcmc->run_move[tree->mcmc->num_move_nd_t]+1.E+6); |
---|
2371 | |
---|
2372 | } |
---|
2373 | |
---|
2374 | ////////////////////////////////////////////////////////////// |
---|
2375 | ////////////////////////////////////////////////////////////// |
---|
2376 | |
---|
2377 | |
---|
2378 | void RATES_Update_Cur_Bl(t_tree *tree) |
---|
2379 | { |
---|
2380 | RATES_Update_Norm_Fact(tree); |
---|
2381 | |
---|
2382 | RATES_Update_Cur_Bl_Pre(tree->n_root,tree->n_root->v[2],NULL,tree); |
---|
2383 | RATES_Update_Cur_Bl_Pre(tree->n_root,tree->n_root->v[1],NULL,tree); |
---|
2384 | |
---|
2385 | if(tree->mod && tree->mod->log_l == YES) |
---|
2386 | { |
---|
2387 | tree->e_root->l->v = |
---|
2388 | EXP(tree->rates->cur_l[tree->n_root->v[2]->num]) + |
---|
2389 | EXP(tree->rates->cur_l[tree->n_root->v[1]->num]) ; |
---|
2390 | tree->e_root->l->v = LOG(tree->e_root->l->v); |
---|
2391 | } |
---|
2392 | else |
---|
2393 | { |
---|
2394 | tree->e_root->l->v = |
---|
2395 | tree->rates->cur_l[tree->n_root->v[2]->num] + |
---|
2396 | tree->rates->cur_l[tree->n_root->v[1]->num] ; |
---|
2397 | } |
---|
2398 | |
---|
2399 | tree->rates->u_cur_l[tree->e_root->num] = tree->e_root->l->v; |
---|
2400 | tree->n_root_pos = tree->rates->cur_l[tree->n_root->v[2]->num] / tree->e_root->l->v; |
---|
2401 | |
---|
2402 | if(tree->rates->model == GUINDON) |
---|
2403 | { |
---|
2404 | phydbl t0,t1,t2; |
---|
2405 | t_node *n0, *n1; |
---|
2406 | |
---|
2407 | n0 = tree->n_root->v[2]; |
---|
2408 | n1 = tree->n_root->v[1]; |
---|
2409 | t1 = tree->rates->nd_t[tree->n_root->v[2]->num]; |
---|
2410 | t2 = tree->rates->nd_t[tree->n_root->v[1]->num]; |
---|
2411 | t0 = tree->rates->nd_t[tree->n_root->num]; |
---|
2412 | |
---|
2413 | tree->e_root->l->v = |
---|
2414 | (t1-t0)/(t1+t2-2.*t0)*tree->rates->cur_gamma_prior_mean[n0->num] + |
---|
2415 | (t2-t0)/(t1+t2-2.*t0)*tree->rates->cur_gamma_prior_mean[n1->num]; |
---|
2416 | |
---|
2417 | tree->e_root->l_var->v = |
---|
2418 | POW((t1-t0)/(t1+t2-2.*t0),2)*tree->rates->cur_gamma_prior_var[n0->num] + |
---|
2419 | POW((t2-t0)/(t1+t2-2.*t0),2)*tree->rates->cur_gamma_prior_var[n1->num]; |
---|
2420 | |
---|
2421 | /* printf("\n. ROOT: %f %f %f %f", */ |
---|
2422 | /* tree->rates->cur_gamma_prior_mean[n0->num], */ |
---|
2423 | /* tree->rates->cur_gamma_prior_var[n0->num], */ |
---|
2424 | /* tree->rates->cur_gamma_prior_mean[n1->num], */ |
---|
2425 | /* tree->rates->cur_gamma_prior_var[n1->num]); */ |
---|
2426 | } |
---|
2427 | } |
---|
2428 | |
---|
2429 | ////////////////////////////////////////////////////////////// |
---|
2430 | ////////////////////////////////////////////////////////////// |
---|
2431 | |
---|
2432 | |
---|
2433 | void RATES_Update_Cur_Bl_Pre(t_node *a, t_node *d, t_edge *b, t_tree *tree) |
---|
2434 | { |
---|
2435 | phydbl dt,rr,cr,ra,rd,ta,td,nu; |
---|
2436 | |
---|
2437 | tree->rates->br_do_updt[d->num] = YES; |
---|
2438 | |
---|
2439 | if(tree->rates->br_do_updt[d->num] == YES) |
---|
2440 | { |
---|
2441 | tree->rates->br_do_updt[d->num] = NO; |
---|
2442 | |
---|
2443 | dt = tree->rates->nd_t[d->num] - tree->rates->nd_t[a->num]; |
---|
2444 | cr = tree->rates->clock_r; |
---|
2445 | rd = tree->rates->br_r[d->num]; |
---|
2446 | ra = tree->rates->br_r[a->num]; |
---|
2447 | td = tree->rates->nd_t[d->num]; |
---|
2448 | ta = tree->rates->nd_t[a->num]; |
---|
2449 | nu = tree->rates->nu; |
---|
2450 | |
---|
2451 | |
---|
2452 | if(tree->rates->model_log_rates == YES) |
---|
2453 | { |
---|
2454 | /* Artihmetic average */ |
---|
2455 | rr = (EXP(ra) + EXP(rd))/2.; |
---|
2456 | } |
---|
2457 | else |
---|
2458 | { |
---|
2459 | rr = (ra+rd)/2.; |
---|
2460 | } |
---|
2461 | |
---|
2462 | tree->rates->cur_l[d->num] = dt*rr*cr; |
---|
2463 | |
---|
2464 | if(tree->rates->model == GUINDON) |
---|
2465 | { |
---|
2466 | phydbl m,v; |
---|
2467 | |
---|
2468 | Integrated_Geometric_Brownian_Bridge_Moments(dt,ra,rd,nu,&m,&v); |
---|
2469 | |
---|
2470 | m *= cr*dt; // the actual rate average is m * cr. We multiply by dt in order to derive the value for the branch length |
---|
2471 | v *= (cr*cr)*(dt*dt); |
---|
2472 | |
---|
2473 | tree->rates->cur_gamma_prior_mean[d->num] = m; |
---|
2474 | tree->rates->cur_gamma_prior_var[d->num] = v; |
---|
2475 | |
---|
2476 | tree->rates->cur_l[d->num] = tree->rates->cur_gamma_prior_mean[d->num]; // Required for having proper branch lengths in Write_Tree function |
---|
2477 | } |
---|
2478 | |
---|
2479 | if(tree->mod && tree->mod->log_l == YES) tree->rates->cur_l[d->num] = LOG(tree->rates->cur_l[d->num]); |
---|
2480 | |
---|
2481 | if(b) |
---|
2482 | { |
---|
2483 | b->l->v = tree->rates->cur_l[d->num]; |
---|
2484 | tree->rates->u_cur_l[b->num] = tree->rates->cur_l[d->num]; |
---|
2485 | b->l_var->v = tree->rates->cur_gamma_prior_var[d->num]; |
---|
2486 | } |
---|
2487 | |
---|
2488 | if(b && (isnan(b->l->v) || isnan(b->l_var->v))) |
---|
2489 | { |
---|
2490 | PhyML_Printf("\n. dt=%G rr=%G cr=%G ra=%G rd=%G nu=%G %f %f ",dt,rr,cr,ra,rd,nu,b->l_var->v,b->l->v); |
---|
2491 | PhyML_Printf("\n. ta=%G td=%G ra*cr=%G rd*cr=%G sd=%G", |
---|
2492 | ta,td,ra*cr,rd*cr, |
---|
2493 | SQRT(dt*nu)*cr); |
---|
2494 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
2495 | Exit("\n"); |
---|
2496 | } |
---|
2497 | } |
---|
2498 | |
---|
2499 | if(d->tax) return; |
---|
2500 | else |
---|
2501 | { |
---|
2502 | int i; |
---|
2503 | For(i,3) |
---|
2504 | if((d->v[i] != a) && (d->b[i] != tree->e_root)) |
---|
2505 | RATES_Update_Cur_Bl_Pre(d,d->v[i],d->b[i],tree); |
---|
2506 | } |
---|
2507 | } |
---|
2508 | |
---|
2509 | ////////////////////////////////////////////////////////////// |
---|
2510 | ////////////////////////////////////////////////////////////// |
---|
2511 | |
---|
2512 | void RATES_Bl_To_Bl(t_tree *tree) |
---|
2513 | { |
---|
2514 | RATES_Bl_To_Bl_Pre(tree->n_root,tree->n_root->v[2],NULL,tree); |
---|
2515 | RATES_Bl_To_Bl_Pre(tree->n_root,tree->n_root->v[1],NULL,tree); |
---|
2516 | /* tree->rates->cur_l[tree->n_root->v[2]->num] = tree->a_edges[tree->e_root->num]->l->v * tree->n_root_pos; */ |
---|
2517 | /* tree->rates->cur_l[tree->n_root->v[1]->num] = tree->a_edges[tree->e_root->num]->l->v * (1. - tree->n_root_pos); */ |
---|
2518 | tree->rates->cur_l[tree->n_root->v[2]->num] = tree->a_edges[tree->e_root->num]->l->v * 0.5; |
---|
2519 | tree->rates->cur_l[tree->n_root->v[1]->num] = tree->a_edges[tree->e_root->num]->l->v * (1. - 0.5); |
---|
2520 | } |
---|
2521 | |
---|
2522 | ////////////////////////////////////////////////////////////// |
---|
2523 | ////////////////////////////////////////////////////////////// |
---|
2524 | |
---|
2525 | void RATES_Bl_To_Bl_Pre(t_node *a, t_node *d, t_edge *b, t_tree *tree) |
---|
2526 | { |
---|
2527 | |
---|
2528 | if(b) |
---|
2529 | { |
---|
2530 | tree->rates->cur_l[d->num] = b->l->v; |
---|
2531 | } |
---|
2532 | |
---|
2533 | if(d->tax) return; |
---|
2534 | else |
---|
2535 | { |
---|
2536 | int i; |
---|
2537 | |
---|
2538 | For(i,3) |
---|
2539 | if((d->v[i] != a) && (d->b[i] != tree->e_root)) |
---|
2540 | RATES_Bl_To_Bl_Pre(d,d->v[i],d->b[i],tree); |
---|
2541 | } |
---|
2542 | } |
---|
2543 | |
---|
2544 | ////////////////////////////////////////////////////////////// |
---|
2545 | ////////////////////////////////////////////////////////////// |
---|
2546 | |
---|
2547 | void RATES_Bl_To_Ml(t_tree *tree) |
---|
2548 | { |
---|
2549 | RATES_Bl_To_Ml_Pre(tree->n_root,tree->n_root->v[2],NULL,tree); |
---|
2550 | RATES_Bl_To_Ml_Pre(tree->n_root,tree->n_root->v[1],NULL,tree); |
---|
2551 | tree->rates->u_ml_l[tree->e_root->num] = tree->a_edges[tree->e_root->num]->l->v; |
---|
2552 | tree->rates->ml_l[tree->n_root->v[2]->num] = tree->rates->u_ml_l[tree->e_root->num] * tree->n_root_pos; |
---|
2553 | tree->rates->ml_l[tree->n_root->v[1]->num] = tree->rates->u_ml_l[tree->e_root->num] * (1. - tree->n_root_pos); |
---|
2554 | } |
---|
2555 | |
---|
2556 | ////////////////////////////////////////////////////////////// |
---|
2557 | ////////////////////////////////////////////////////////////// |
---|
2558 | |
---|
2559 | void RATES_Bl_To_Ml_Pre(t_node *a, t_node *d, t_edge *b, t_tree *tree) |
---|
2560 | { |
---|
2561 | |
---|
2562 | if(b) |
---|
2563 | { |
---|
2564 | tree->rates->u_ml_l[b->num] = b->l->v; |
---|
2565 | tree->rates->ml_l[d->num] = b->l->v; |
---|
2566 | } |
---|
2567 | |
---|
2568 | if(d->tax) return; |
---|
2569 | else |
---|
2570 | { |
---|
2571 | int i; |
---|
2572 | |
---|
2573 | For(i,3) |
---|
2574 | if((d->v[i] != a) && (d->b[i] != tree->e_root)) |
---|
2575 | RATES_Bl_To_Ml_Pre(d,d->v[i],d->b[i],tree); |
---|
2576 | } |
---|
2577 | } |
---|
2578 | |
---|
2579 | ////////////////////////////////////////////////////////////// |
---|
2580 | ////////////////////////////////////////////////////////////// |
---|
2581 | |
---|
2582 | |
---|
2583 | void RATES_Get_Cov_Matrix_Rooted(phydbl *unroot_cov, t_tree *tree) |
---|
2584 | { |
---|
2585 | int i,dim; |
---|
2586 | |
---|
2587 | dim = 2*tree->n_otu-3; |
---|
2588 | |
---|
2589 | RATES_Get_Cov_Matrix_Rooted_Pre(tree->n_root,tree->n_root->v[2],NULL,unroot_cov,tree); |
---|
2590 | RATES_Get_Cov_Matrix_Rooted_Pre(tree->n_root,tree->n_root->v[1],NULL,unroot_cov,tree); |
---|
2591 | |
---|
2592 | For(i,dim+1) tree->rates->cov_l[i*(dim+1)+tree->n_root->v[2]->num] /= 2.; |
---|
2593 | For(i,dim+1) tree->rates->cov_l[i*(dim+1)+tree->n_root->v[1]->num] /= 2.; |
---|
2594 | For(i,dim+1) tree->rates->cov_l[tree->n_root->v[2]->num*(dim+1)+i] /= 2.; |
---|
2595 | For(i,dim+1) tree->rates->cov_l[tree->n_root->v[1]->num*(dim+1)+i] /= 2.; |
---|
2596 | |
---|
2597 | } |
---|
2598 | |
---|
2599 | ////////////////////////////////////////////////////////////// |
---|
2600 | ////////////////////////////////////////////////////////////// |
---|
2601 | |
---|
2602 | |
---|
2603 | void RATES_Get_Cov_Matrix_Rooted_Pre(t_node *a, t_node *d, t_edge *b, phydbl *cov, t_tree *tree) |
---|
2604 | { |
---|
2605 | int i, dim; |
---|
2606 | t_node *n; |
---|
2607 | |
---|
2608 | dim = 2*tree->n_otu-3; |
---|
2609 | n = NULL; |
---|
2610 | |
---|
2611 | For(i,dim) |
---|
2612 | { |
---|
2613 | if(tree->a_edges[i] != tree->e_root) |
---|
2614 | { |
---|
2615 | n = |
---|
2616 | (tree->a_edges[i]->left->anc == tree->a_edges[i]->rght)? |
---|
2617 | (tree->a_edges[i]->left): |
---|
2618 | (tree->a_edges[i]->rght); |
---|
2619 | |
---|
2620 | if(b) |
---|
2621 | { |
---|
2622 | tree->rates->cov_l[d->num*(dim+1) + n->num] = cov[b->num*dim + i]; |
---|
2623 | } |
---|
2624 | else |
---|
2625 | { |
---|
2626 | tree->rates->cov_l[d->num*(dim+1) + n->num] = cov[tree->e_root->num*dim + i]; |
---|
2627 | } |
---|
2628 | } |
---|
2629 | else |
---|
2630 | { |
---|
2631 | n = tree->e_root->left; |
---|
2632 | if(b) |
---|
2633 | tree->rates->cov_l[d->num*(dim+1) + n->num] = cov[b->num*dim + i]; |
---|
2634 | else |
---|
2635 | tree->rates->cov_l[d->num*(dim+1) + n->num] = cov[tree->e_root->num*dim + i]; |
---|
2636 | |
---|
2637 | n = tree->e_root->rght; |
---|
2638 | if(b) |
---|
2639 | tree->rates->cov_l[d->num*(dim+1) + n->num] = cov[b->num*dim + i]; |
---|
2640 | else |
---|
2641 | tree->rates->cov_l[d->num*(dim+1) + n->num] = cov[tree->e_root->num*dim + i]; |
---|
2642 | } |
---|
2643 | } |
---|
2644 | |
---|
2645 | |
---|
2646 | if(d->tax) return; |
---|
2647 | else |
---|
2648 | { |
---|
2649 | For(i,3) |
---|
2650 | if((d->v[i] != a) && (d->b[i] != tree->e_root)) |
---|
2651 | RATES_Get_Cov_Matrix_Rooted_Pre(d,d->v[i],d->b[i],cov,tree); |
---|
2652 | } |
---|
2653 | } |
---|
2654 | |
---|
2655 | ////////////////////////////////////////////////////////////// |
---|
2656 | ////////////////////////////////////////////////////////////// |
---|
2657 | |
---|
2658 | |
---|
2659 | void RATES_Covariance_Mu(t_tree *tree) |
---|
2660 | { |
---|
2661 | int i,j; |
---|
2662 | phydbl dt,var; |
---|
2663 | int dim; |
---|
2664 | int lca_num; |
---|
2665 | |
---|
2666 | dim = 2*tree->n_otu-2; |
---|
2667 | |
---|
2668 | For(i,dim*dim) tree->rates->cov_r[i] = 0.0; |
---|
2669 | |
---|
2670 | dt = tree->rates->nd_t[tree->n_root->v[2]->num] - tree->rates->nd_t[tree->n_root->num]; |
---|
2671 | var = dt * tree->rates->nu; |
---|
2672 | tree->rates->cov_r[tree->n_root->v[2]->num*dim+tree->n_root->v[2]->num] = var; |
---|
2673 | |
---|
2674 | |
---|
2675 | dt = tree->rates->nd_t[tree->n_root->v[1]->num] - tree->rates->nd_t[tree->n_root->num]; |
---|
2676 | var = dt * tree->rates->nu; |
---|
2677 | tree->rates->cov_r[tree->n_root->v[1]->num*dim+tree->n_root->v[1]->num] = var; |
---|
2678 | |
---|
2679 | RATES_Variance_Mu_Pre(tree->n_root,tree->n_root->v[2],tree); |
---|
2680 | RATES_Variance_Mu_Pre(tree->n_root,tree->n_root->v[1],tree); |
---|
2681 | |
---|
2682 | For(i,dim) |
---|
2683 | { |
---|
2684 | for(j=i+1;j<dim;j++) |
---|
2685 | { |
---|
2686 | lca_num = tree->rates->lca[i*(dim+1)+j]->num; |
---|
2687 | if(lca_num < dim) |
---|
2688 | { |
---|
2689 | tree->rates->cov_r[i*dim+j] = tree->rates->cov_r[lca_num*dim+lca_num]; |
---|
2690 | tree->rates->cov_r[j*dim+i] = tree->rates->cov_r[i*dim+j]; |
---|
2691 | } |
---|
2692 | else if(lca_num == dim) |
---|
2693 | { |
---|
2694 | tree->rates->cov_r[i*dim+j] = 0.0; |
---|
2695 | tree->rates->cov_r[j*dim+i] = 0.0; |
---|
2696 | } |
---|
2697 | else |
---|
2698 | { |
---|
2699 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
2700 | Exit("\n"); |
---|
2701 | } |
---|
2702 | } |
---|
2703 | } |
---|
2704 | } |
---|
2705 | |
---|
2706 | ////////////////////////////////////////////////////////////// |
---|
2707 | ////////////////////////////////////////////////////////////// |
---|
2708 | |
---|
2709 | |
---|
2710 | void RATES_Variance_Mu_Pre(t_node *a, t_node *d, t_tree *tree) |
---|
2711 | { |
---|
2712 | int dim; |
---|
2713 | phydbl var0; |
---|
2714 | phydbl dt1,var1; |
---|
2715 | phydbl dt2,var2; |
---|
2716 | int i; |
---|
2717 | int dir1, dir2; |
---|
2718 | |
---|
2719 | dim = 2*tree->n_otu-2; |
---|
2720 | |
---|
2721 | if(d->tax) return; |
---|
2722 | |
---|
2723 | dir1 = dir2 = -1; |
---|
2724 | For(i,3) |
---|
2725 | { |
---|
2726 | if((d->v[i] != a) && (d->b[i] != tree->e_root)) |
---|
2727 | { |
---|
2728 | if(dir1 < 0) dir1 = i; |
---|
2729 | else dir2 = i; |
---|
2730 | } |
---|
2731 | } |
---|
2732 | |
---|
2733 | |
---|
2734 | var0 = tree->rates->cov_r[d->num*dim+d->num]; |
---|
2735 | |
---|
2736 | dt1 = tree->rates->nd_t[d->v[dir1]->num] - tree->rates->nd_t[d->num]; |
---|
2737 | var1 = tree->rates->nu*dt1; |
---|
2738 | |
---|
2739 | dt2 = tree->rates->nd_t[d->v[dir2]->num] - tree->rates->nd_t[d->num]; |
---|
2740 | var2 = tree->rates->nu*dt2; |
---|
2741 | |
---|
2742 | tree->rates->cov_r[d->v[dir1]->num*dim+d->v[dir1]->num] = var0+var1; |
---|
2743 | tree->rates->cov_r[d->v[dir2]->num*dim+d->v[dir2]->num] = var0+var2; |
---|
2744 | |
---|
2745 | |
---|
2746 | For(i,3) |
---|
2747 | { |
---|
2748 | if((d->v[i] != a) && (d->b[i] != tree->e_root)) |
---|
2749 | { |
---|
2750 | RATES_Variance_Mu_Pre(d,d->v[i],tree); |
---|
2751 | } |
---|
2752 | } |
---|
2753 | } |
---|
2754 | |
---|
2755 | ////////////////////////////////////////////////////////////// |
---|
2756 | ////////////////////////////////////////////////////////////// |
---|
2757 | |
---|
2758 | |
---|
2759 | void RATES_Fill_Lca_Table(t_tree *tree) |
---|
2760 | { |
---|
2761 | int i,j; |
---|
2762 | int dim; |
---|
2763 | |
---|
2764 | dim = 2*tree->n_otu-1; |
---|
2765 | |
---|
2766 | For(i,dim) |
---|
2767 | { |
---|
2768 | for(j=i;j<dim;j++) |
---|
2769 | { |
---|
2770 | tree->rates->lca[i*dim+j] = Find_Lca_Pair_Of_Nodes(tree->a_nodes[i],tree->a_nodes[j],tree); |
---|
2771 | tree->rates->lca[j*dim+i] = tree->rates->lca[i*dim+j]; |
---|
2772 | } |
---|
2773 | } |
---|
2774 | } |
---|
2775 | |
---|
2776 | ////////////////////////////////////////////////////////////// |
---|
2777 | ////////////////////////////////////////////////////////////// |
---|
2778 | |
---|
2779 | /* Get V(L_{i} | L_{-i}) for all i */ |
---|
2780 | void RATES_Get_Conditional_Variances(t_tree *tree) |
---|
2781 | { |
---|
2782 | int i,j; |
---|
2783 | short int *is_1; |
---|
2784 | phydbl *a; |
---|
2785 | int dim; |
---|
2786 | t_edge *b; |
---|
2787 | phydbl *cond_mu, *cond_cov; |
---|
2788 | |
---|
2789 | dim = 2*tree->n_otu-3; |
---|
2790 | a = tree->rates->_2n_vect1; |
---|
2791 | is_1 = tree->rates->_2n_vect5; |
---|
2792 | b = NULL; |
---|
2793 | cond_mu = tree->rates->_2n_vect2; |
---|
2794 | cond_cov = tree->rates->_2n2n_vect1; |
---|
2795 | |
---|
2796 | For(i,dim) a[i] = tree->rates->mean_l[i] * (Uni() * 0.2 + 0.9); |
---|
2797 | |
---|
2798 | For(i,dim) |
---|
2799 | { |
---|
2800 | b = tree->a_edges[i]; |
---|
2801 | |
---|
2802 | For(j,dim) is_1[j] = 0; |
---|
2803 | is_1[b->num] = 1; |
---|
2804 | |
---|
2805 | For(j,dim*dim) cond_cov[j] = 0.0; |
---|
2806 | For(j,dim) cond_mu[j] = 0.0; |
---|
2807 | Normal_Conditional(tree->rates->mean_l,tree->rates->cov_l,a,dim,is_1,1,cond_mu,cond_cov); |
---|
2808 | |
---|
2809 | tree->rates->cond_var[b->num] = cond_cov[b->num*dim+b->num]; |
---|
2810 | } |
---|
2811 | } |
---|
2812 | |
---|
2813 | ////////////////////////////////////////////////////////////// |
---|
2814 | ////////////////////////////////////////////////////////////// |
---|
2815 | |
---|
2816 | |
---|
2817 | void RATES_Get_All_Reg_Coeff(t_tree *tree) |
---|
2818 | { |
---|
2819 | int i,j; |
---|
2820 | short int *is_1; |
---|
2821 | phydbl *a; |
---|
2822 | int dim; |
---|
2823 | t_edge *b; |
---|
2824 | |
---|
2825 | dim = 2*tree->n_otu-3; |
---|
2826 | a = tree->rates->_2n_vect1; |
---|
2827 | is_1 = tree->rates->_2n_vect5; |
---|
2828 | b = NULL; |
---|
2829 | |
---|
2830 | For(i,dim) a[i] = tree->rates->mean_l[i] * (Uni() * 0.2 + 0.9); |
---|
2831 | |
---|
2832 | For(i,dim) |
---|
2833 | { |
---|
2834 | b = tree->a_edges[i]; |
---|
2835 | |
---|
2836 | For(j,dim) is_1[j] = 0; |
---|
2837 | is_1[b->num] = 1; |
---|
2838 | |
---|
2839 | Get_Reg_Coeff(tree->rates->mean_l,tree->rates->cov_l,a,dim,is_1,1,tree->rates->reg_coeff+b->num*dim); |
---|
2840 | } |
---|
2841 | } |
---|
2842 | |
---|
2843 | ////////////////////////////////////////////////////////////// |
---|
2844 | ////////////////////////////////////////////////////////////// |
---|
2845 | |
---|
2846 | |
---|
2847 | /* Get V(L_{i} | L_{-i}) for all i */ |
---|
2848 | void RATES_Get_Trip_Conditional_Variances(t_tree *tree) |
---|
2849 | { |
---|
2850 | int i,j; |
---|
2851 | short int *is_1; |
---|
2852 | phydbl *a; |
---|
2853 | phydbl *cond_mu, *cond_cov; |
---|
2854 | t_node *n; |
---|
2855 | int n_otu; |
---|
2856 | |
---|
2857 | a = tree->rates->_2n_vect1; |
---|
2858 | is_1 = tree->rates->_2n_vect5; |
---|
2859 | cond_mu = tree->rates->_2n_vect2; |
---|
2860 | cond_cov = tree->rates->_2n2n_vect1; |
---|
2861 | n = NULL; |
---|
2862 | n_otu = tree->n_otu; |
---|
2863 | |
---|
2864 | For(i,2*n_otu-3) a[i] = tree->rates->mean_l[i] * (Uni() * 0.2 + 0.9); |
---|
2865 | |
---|
2866 | For(i,2*n_otu-2) |
---|
2867 | { |
---|
2868 | n = tree->a_nodes[i]; |
---|
2869 | if(!n->tax) |
---|
2870 | { |
---|
2871 | For(j,2*n_otu-3) is_1[j] = 0; |
---|
2872 | is_1[n->b[0]->num] = 1; |
---|
2873 | is_1[n->b[1]->num] = 1; |
---|
2874 | is_1[n->b[2]->num] = 1; |
---|
2875 | |
---|
2876 | Normal_Conditional_Unsorted(tree->rates->mean_l,tree->rates->cov_l,a,2*n_otu-3,is_1,3,cond_mu,cond_cov); |
---|
2877 | |
---|
2878 | For(j,9) tree->rates->trip_cond_cov[n->num*9+j] = cond_cov[j]; |
---|
2879 | } |
---|
2880 | } |
---|
2881 | } |
---|
2882 | |
---|
2883 | ////////////////////////////////////////////////////////////// |
---|
2884 | ////////////////////////////////////////////////////////////// |
---|
2885 | |
---|
2886 | |
---|
2887 | void RATES_Get_All_Trip_Reg_Coeff(t_tree *tree) |
---|
2888 | { |
---|
2889 | int i,j; |
---|
2890 | short int *is_1; |
---|
2891 | phydbl *a; |
---|
2892 | t_node *n; |
---|
2893 | int n_otu; |
---|
2894 | |
---|
2895 | a = tree->rates->_2n_vect1; |
---|
2896 | is_1 = tree->rates->_2n_vect5; |
---|
2897 | n = NULL; |
---|
2898 | n_otu = tree->n_otu; |
---|
2899 | |
---|
2900 | For(i,2*n_otu-3) a[i] = tree->rates->mean_l[i] * (Uni() * 0.2 + 0.9); |
---|
2901 | |
---|
2902 | For(i,2*n_otu-2) |
---|
2903 | { |
---|
2904 | n = tree->a_nodes[i]; |
---|
2905 | if(!n->tax) |
---|
2906 | { |
---|
2907 | For(j,2*n_otu-3) is_1[j] = 0; |
---|
2908 | is_1[n->b[0]->num] = 1; |
---|
2909 | is_1[n->b[1]->num] = 1; |
---|
2910 | is_1[n->b[2]->num] = 1; |
---|
2911 | |
---|
2912 | Get_Reg_Coeff(tree->rates->mean_l,tree->rates->cov_l,a,2*n_otu-3,is_1,3,tree->rates->trip_reg_coeff+n->num*(6*n_otu-9)); |
---|
2913 | } |
---|
2914 | } |
---|
2915 | } |
---|
2916 | |
---|
2917 | ////////////////////////////////////////////////////////////// |
---|
2918 | ////////////////////////////////////////////////////////////// |
---|
2919 | |
---|
2920 | |
---|
2921 | void RATES_Check_Lk_Rates(t_tree *tree, int *err) |
---|
2922 | { |
---|
2923 | int i; |
---|
2924 | phydbl u,u_anc; |
---|
2925 | phydbl t,t_anc; |
---|
2926 | |
---|
2927 | *err = 0; |
---|
2928 | |
---|
2929 | For(i,2*tree->n_otu-2) |
---|
2930 | { |
---|
2931 | u = tree->rates->br_r[i]; |
---|
2932 | u_anc = tree->rates->br_r[tree->a_nodes[i]->anc->num]; |
---|
2933 | t = tree->rates->nd_t[i]; |
---|
2934 | t_anc = tree->rates->nd_t[tree->a_nodes[i]->anc->num]; |
---|
2935 | |
---|
2936 | if(t_anc > t) |
---|
2937 | { |
---|
2938 | PhyML_Printf("\n. %d %d u=%f u_anc=%f t=%f t_anc=%f",i,tree->a_nodes[i]->anc->num,u,u_anc,t,t_anc); |
---|
2939 | PhyML_Printf("\n. %d %d %d",tree->n_root->num,tree->n_root->v[2]->num,tree->n_root->v[1]->num); |
---|
2940 | *err = 1; |
---|
2941 | } |
---|
2942 | } |
---|
2943 | } |
---|
2944 | |
---|
2945 | ////////////////////////////////////////////////////////////// |
---|
2946 | ////////////////////////////////////////////////////////////// |
---|
2947 | |
---|
2948 | |
---|
2949 | phydbl RATES_Expected_Tree_Length(t_tree *tree) |
---|
2950 | { |
---|
2951 | int n; |
---|
2952 | phydbl mean; |
---|
2953 | |
---|
2954 | n = 0; |
---|
2955 | mean = 0.0; |
---|
2956 | RATES_Expected_Tree_Length_Pre(tree->n_root,tree->n_root->v[2],1.0,&mean,&n,tree); |
---|
2957 | RATES_Expected_Tree_Length_Pre(tree->n_root,tree->n_root->v[1],1.0,&mean,&n,tree); |
---|
2958 | |
---|
2959 | if(n != 2*tree->n_otu-2) |
---|
2960 | { |
---|
2961 | PhyML_Printf("\n. n=%d 2n-2=%d",n,2*tree->n_otu-2); |
---|
2962 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
2963 | Exit("\n"); |
---|
2964 | } |
---|
2965 | |
---|
2966 | return mean; |
---|
2967 | } |
---|
2968 | |
---|
2969 | ////////////////////////////////////////////////////////////// |
---|
2970 | ////////////////////////////////////////////////////////////// |
---|
2971 | |
---|
2972 | |
---|
2973 | void RATES_Expected_Tree_Length_Pre(t_node *a, t_node *d, phydbl eranc, phydbl *mean, int *n, t_tree *tree) |
---|
2974 | { |
---|
2975 | phydbl erdes; |
---|
2976 | int i; |
---|
2977 | phydbl loc_mean; |
---|
2978 | int loc_n; |
---|
2979 | |
---|
2980 | |
---|
2981 | /* erdes = u_anc - */ |
---|
2982 | /* sd*(Dnorm((tree->rates->min_rate-u_anc)/sd,.0,1.) - Dnorm((tree->rates->max_rate-u_anc)/sd,.0,1.))/ */ |
---|
2983 | /* (Pnorm((tree->rates->max_rate-u_anc)/sd,.0,1.) - Pnorm((tree->rates->min_rate-u_anc)/sd,.0,1.)); */ |
---|
2984 | |
---|
2985 | /* erdes = Norm_Trunc_Mean(eranc,sd,tree->rates->min_rate,tree->rates->max_rate); */ |
---|
2986 | erdes = 1.0; |
---|
2987 | |
---|
2988 | loc_mean = *mean; |
---|
2989 | loc_n = *n; |
---|
2990 | |
---|
2991 | loc_mean *= loc_n; |
---|
2992 | loc_mean += erdes; |
---|
2993 | loc_mean /= (loc_n + 1); |
---|
2994 | |
---|
2995 | *mean = loc_mean; |
---|
2996 | *n = loc_n + 1; |
---|
2997 | |
---|
2998 | |
---|
2999 | if(d->tax) return; |
---|
3000 | else |
---|
3001 | For(i,3) |
---|
3002 | if(d->v[i] != a && d->b[i] != tree->e_root) |
---|
3003 | RATES_Expected_Tree_Length_Pre(d,d->v[i],erdes,mean,n,tree); |
---|
3004 | } |
---|
3005 | |
---|
3006 | ////////////////////////////////////////////////////////////// |
---|
3007 | ////////////////////////////////////////////////////////////// |
---|
3008 | |
---|
3009 | |
---|
3010 | void RATES_Update_Norm_Fact(t_tree *tree) |
---|
3011 | { |
---|
3012 | int i; |
---|
3013 | phydbl r,t,t_anc; |
---|
3014 | phydbl num,denom; |
---|
3015 | |
---|
3016 | num = denom = 0.0; |
---|
3017 | |
---|
3018 | For(i,2*tree->n_otu-2) |
---|
3019 | { |
---|
3020 | r = tree->rates->br_r[i]; |
---|
3021 | t = tree->rates->nd_t[i]; |
---|
3022 | t_anc = tree->rates->nd_t[tree->a_nodes[i]->anc->num]; |
---|
3023 | |
---|
3024 | num += (t-t_anc); |
---|
3025 | denom += (t-t_anc) * r; |
---|
3026 | |
---|
3027 | } |
---|
3028 | tree->rates->norm_fact = num/denom; |
---|
3029 | |
---|
3030 | /* !!!!!!!!!!!!!!!!!! */ |
---|
3031 | tree->rates->norm_fact = 1.0; |
---|
3032 | } |
---|
3033 | |
---|
3034 | ////////////////////////////////////////////////////////////// |
---|
3035 | ////////////////////////////////////////////////////////////// |
---|
3036 | |
---|
3037 | ////////////////////////////////////////////////////////////// |
---|
3038 | ////////////////////////////////////////////////////////////// |
---|
3039 | |
---|
3040 | |
---|
3041 | void RATES_Normalise_Rates(t_tree *tree) |
---|
3042 | { |
---|
3043 | phydbl expr,curr; |
---|
3044 | int i; |
---|
3045 | |
---|
3046 | curr = RATES_Average_Substitution_Rate(tree); |
---|
3047 | curr /= tree->rates->clock_r; |
---|
3048 | |
---|
3049 | /* Set expected mean rate to one such that clock_r is |
---|
3050 | easy to interpret regarding the mean values of br_r */ |
---|
3051 | expr = 1.0; |
---|
3052 | |
---|
3053 | For(i,2*tree->n_otu-2) |
---|
3054 | { |
---|
3055 | tree->rates->br_r[i] *= expr/curr; |
---|
3056 | |
---|
3057 | if(tree->rates->br_r[i] > tree->rates->max_rate) |
---|
3058 | tree->rates->br_r[i] = tree->rates->max_rate; |
---|
3059 | |
---|
3060 | if(tree->rates->br_r[i] < tree->rates->min_rate) |
---|
3061 | tree->rates->br_r[i] = tree->rates->min_rate; |
---|
3062 | } |
---|
3063 | |
---|
3064 | tree->rates->clock_r *= curr/expr; |
---|
3065 | /* Branch lengths therefore do not change */ |
---|
3066 | |
---|
3067 | /* if(tree->rates->clock_r < tree->rates->min_clock) */ |
---|
3068 | /* { */ |
---|
3069 | /* PhyML_Printf("\n. Curr mean rates: %G",curr); */ |
---|
3070 | /* PhyML_Printf("\n. Set clock rate to: %G",tree->rates->clock_r); */ |
---|
3071 | /* tree->rates->clock_r = tree->rates->min_clock; */ |
---|
3072 | /* } */ |
---|
3073 | /* if(tree->rates->clock_r > tree->rates->max_clock) */ |
---|
3074 | /* { */ |
---|
3075 | /* PhyML_Printf("\n. Curr mean rates: %G",curr); */ |
---|
3076 | /* PhyML_Printf("\n. Set clock rate to: %G",tree->rates->clock_r); */ |
---|
3077 | /* tree->rates->clock_r = tree->rates->max_clock; */ |
---|
3078 | /* } */ |
---|
3079 | |
---|
3080 | RATES_Update_Norm_Fact(tree); |
---|
3081 | RATES_Update_Cur_Bl(tree); |
---|
3082 | } |
---|
3083 | |
---|
3084 | ////////////////////////////////////////////////////////////// |
---|
3085 | ////////////////////////////////////////////////////////////// |
---|
3086 | |
---|
3087 | |
---|
3088 | phydbl RATES_Find_Max_Dt_Bisec(phydbl r, phydbl r_mean, phydbl ta, phydbl tc, phydbl nu, phydbl threshp, int inf) |
---|
3089 | { |
---|
3090 | phydbl cdfr,cdf0; |
---|
3091 | phydbl sd; |
---|
3092 | phydbl trunc_cdf; |
---|
3093 | phydbl ori_tc, ori_ta; |
---|
3094 | phydbl tb; |
---|
3095 | |
---|
3096 | ori_tc = tc; |
---|
3097 | ori_ta = ta; |
---|
3098 | |
---|
3099 | /* PhyML_Printf("\n Max %s r=%f r_mean%f",inf?"inf":"sup",r,r_mean); */ |
---|
3100 | do |
---|
3101 | { |
---|
3102 | tb = ta + (tc - ta)/2.; |
---|
3103 | |
---|
3104 | |
---|
3105 | sd = SQRT(nu*(ori_tc - tb)); |
---|
3106 | cdfr = Pnorm(r ,r_mean,sd); |
---|
3107 | cdf0 = Pnorm(.0,r_mean,sd); |
---|
3108 | |
---|
3109 | if(inf) |
---|
3110 | trunc_cdf = (cdfr - cdf0)/(1. - cdf0); |
---|
3111 | else |
---|
3112 | trunc_cdf = (1. - cdfr)/(1. - cdf0); |
---|
3113 | |
---|
3114 | /* PhyML_Printf("\n. ta=%15f tb=%15f tc=%15f cdf = %15f",ta,tb,tc,trunc_cdf); */ |
---|
3115 | |
---|
3116 | if(trunc_cdf > threshp) |
---|
3117 | { |
---|
3118 | ta = tb; |
---|
3119 | } |
---|
3120 | else |
---|
3121 | { |
---|
3122 | tc = tb; |
---|
3123 | } |
---|
3124 | |
---|
3125 | }while((tc - ta)/(ori_tc - ori_ta) > 0.001); |
---|
3126 | |
---|
3127 | if(tb < ori_ta) |
---|
3128 | { |
---|
3129 | PhyML_Printf("\n. tb < ta r=%f r_mean=%f",r,r_mean); |
---|
3130 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
3131 | Exit("\n"); |
---|
3132 | } |
---|
3133 | if(tb > ori_tc) |
---|
3134 | { |
---|
3135 | PhyML_Printf("\n. tb > tc r=%f r_mean=%f ori_ta=%f ori_tc=%f tb=%f",r,r_mean,ori_ta,ori_tc,tb); |
---|
3136 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
3137 | Exit("\n"); |
---|
3138 | } |
---|
3139 | |
---|
3140 | return tb; |
---|
3141 | } |
---|
3142 | |
---|
3143 | ////////////////////////////////////////////////////////////// |
---|
3144 | ////////////////////////////////////////////////////////////// |
---|
3145 | |
---|
3146 | |
---|
3147 | phydbl RATES_Find_Min_Dt_Bisec(phydbl r, phydbl r_mean, phydbl ta, phydbl tc, phydbl nu, phydbl threshp, int inf) |
---|
3148 | { |
---|
3149 | phydbl cdfr,cdf0; |
---|
3150 | phydbl sd; |
---|
3151 | phydbl trunc_cdf; |
---|
3152 | phydbl ori_tc, ori_ta; |
---|
3153 | phydbl tb; |
---|
3154 | |
---|
3155 | ori_tc = tc; |
---|
3156 | ori_ta = ta; |
---|
3157 | |
---|
3158 | /* PhyML_Printf("\n Min %s r=%f r_mean=%f",inf?"inf":"sup",r,r_mean); */ |
---|
3159 | do |
---|
3160 | { |
---|
3161 | |
---|
3162 | tb = ta + (tc - ta)/2.; |
---|
3163 | |
---|
3164 | |
---|
3165 | sd = SQRT(nu*(tb - ori_ta)); |
---|
3166 | cdfr = Pnorm(r ,r_mean,sd); |
---|
3167 | cdf0 = Pnorm(.0,r_mean,sd); |
---|
3168 | |
---|
3169 | if(inf) |
---|
3170 | trunc_cdf = (cdfr - cdf0)/(1. - cdf0); |
---|
3171 | else |
---|
3172 | trunc_cdf = (1. - cdfr)/(1. - cdf0); |
---|
3173 | |
---|
3174 | /* PhyML_Printf("\n. ta=%15f tb=%15f tc=%15f cdf = %15f",ta,tb,tc,trunc_cdf); */ |
---|
3175 | |
---|
3176 | if(trunc_cdf > threshp) |
---|
3177 | { |
---|
3178 | tc = tb; |
---|
3179 | } |
---|
3180 | else |
---|
3181 | { |
---|
3182 | ta = tb; |
---|
3183 | } |
---|
3184 | |
---|
3185 | }while((tc - ta)/(ori_tc - ori_ta) > 0.001); |
---|
3186 | |
---|
3187 | if(tb < ori_ta) |
---|
3188 | { |
---|
3189 | PhyML_Printf("\n. tb < ta r=%f r_mean=%f",r,r_mean); |
---|
3190 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
3191 | Exit("\n"); |
---|
3192 | } |
---|
3193 | if(tb > ori_tc) |
---|
3194 | { |
---|
3195 | PhyML_Printf("\n. tb > tc r=%f r_mean=%f ori_ta=%f ori_tc=%f tb=%f",r,r_mean,ori_ta,ori_tc,tb); |
---|
3196 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
3197 | Exit("\n"); |
---|
3198 | } |
---|
3199 | |
---|
3200 | return tb; |
---|
3201 | } |
---|
3202 | |
---|
3203 | ////////////////////////////////////////////////////////////// |
---|
3204 | ////////////////////////////////////////////////////////////// |
---|
3205 | |
---|
3206 | |
---|
3207 | void RATES_Min_Max_Interval(phydbl u0, phydbl u1, phydbl u2, phydbl u3, phydbl t0, phydbl t2, phydbl t3, |
---|
3208 | phydbl *t_min, phydbl *t_max, phydbl nu, phydbl p_thresh, t_tree *tree) |
---|
3209 | { |
---|
3210 | int n_eval; |
---|
3211 | int i; |
---|
3212 | phydbl sum_cdf; |
---|
3213 | phydbl *cdf; |
---|
3214 | phydbl t1; |
---|
3215 | phydbl mint2t3; |
---|
3216 | |
---|
3217 | mint2t3 = MIN(t2,t3); |
---|
3218 | n_eval = 5; |
---|
3219 | |
---|
3220 | cdf = (phydbl *)mCalloc(n_eval,sizeof(phydbl)); |
---|
3221 | |
---|
3222 | sum_cdf = .0; |
---|
3223 | For(i,n_eval) |
---|
3224 | { |
---|
3225 | t1 = t0 + (i + 1)*(mint2t3 - t0)/(n_eval + 1); |
---|
3226 | cdf[i] = |
---|
3227 | Dnorm_Trunc(u1,u0,SQRT(nu*(t1-t0)),tree->rates->min_rate,tree->rates->max_rate) * |
---|
3228 | Dnorm_Trunc(u2,u1,SQRT(nu*(t2-t1)),tree->rates->min_rate,tree->rates->max_rate) * |
---|
3229 | Dnorm_Trunc(u3,u1,SQRT(nu*(t3-t1)),tree->rates->min_rate,tree->rates->max_rate) * |
---|
3230 | (mint2t3 - t0) / (n_eval + 1); |
---|
3231 | if(i) cdf[i] += cdf[i-1]; |
---|
3232 | } |
---|
3233 | sum_cdf = cdf[i-1]; |
---|
3234 | |
---|
3235 | For(i,n_eval) cdf[i] /= sum_cdf; |
---|
3236 | |
---|
3237 | /* PhyML_Printf("\n"); */ |
---|
3238 | /* For(i,n_eval) PhyML_Printf("\n* %f %f",cdf[i],sum_cdf); */ |
---|
3239 | |
---|
3240 | For(i,n_eval) |
---|
3241 | if(cdf[i] > p_thresh) |
---|
3242 | { |
---|
3243 | *t_min = t0 + (i + 1)*(mint2t3 - t0)/(n_eval + 1); |
---|
3244 | break; |
---|
3245 | } |
---|
3246 | |
---|
3247 | For(i,n_eval) |
---|
3248 | if(cdf[i] > (1. - p_thresh)) |
---|
3249 | { |
---|
3250 | *t_max = t0 + (i + 1)*(mint2t3 - t0)/(n_eval + 1); |
---|
3251 | break; |
---|
3252 | } |
---|
3253 | |
---|
3254 | Free(cdf); |
---|
3255 | } |
---|
3256 | |
---|
3257 | ////////////////////////////////////////////////////////////// |
---|
3258 | ////////////////////////////////////////////////////////////// |
---|
3259 | |
---|
3260 | |
---|
3261 | phydbl RATES_Get_Correction_Factor(phydbl mode, phydbl sd, int *err, t_tree *tree) |
---|
3262 | { |
---|
3263 | |
---|
3264 | phydbl K,X0,X1,X2,Y0,Y1,Y2; |
---|
3265 | phydbl eps=0.01; |
---|
3266 | phydbl A,B; |
---|
3267 | phydbl slope,inter; |
---|
3268 | phydbl denom; |
---|
3269 | |
---|
3270 | *err = NO; |
---|
3271 | |
---|
3272 | |
---|
3273 | /* DO NOTHING */ |
---|
3274 | return 0.0; |
---|
3275 | |
---|
3276 | |
---|
3277 | |
---|
3278 | A = tree->rates->min_rate / sd; |
---|
3279 | B = tree->rates->max_rate / sd; |
---|
3280 | K = mode / sd; |
---|
3281 | |
---|
3282 | X0 = 0.; |
---|
3283 | /* Y0 = Dnorm(X0-K,.0,1.)/(1. - Pnorm(X0-K,.0,1.)) - X0; */ |
---|
3284 | Y0 = (Dnorm(A-K+X0,.0,1.)-Dnorm(B-K+X0,.0,1.))/(Pnorm(B-K+X0,.0,1.)-Pnorm(A-K+X0,.0,1.)) - X0; |
---|
3285 | |
---|
3286 | X1 = .1; |
---|
3287 | /* Y1 = Dnorm(X1-K,.0,1.)/(1. - Pnorm(X1-K,.0,1.)) - X1; */ |
---|
3288 | Y1 = (Dnorm(A-K+X1,.0,1.)-Dnorm(B-K+X1,.0,1.))/(Pnorm(B-K+X1,.0,1.)-Pnorm(A-K+X1,.0,1.)) - X1; |
---|
3289 | |
---|
3290 | /* printf("\n. ^^ mean=%f sd=%f",mode,sd); */ |
---|
3291 | |
---|
3292 | /* printf("\n. X0=%f Y0=%f X1=%f Y1=%f",X0,Y0,X1,Y1); */ |
---|
3293 | |
---|
3294 | do |
---|
3295 | { |
---|
3296 | |
---|
3297 | slope = (Y1-Y0)/(X1-X0); |
---|
3298 | inter = Y0 - X0*(Y1-Y0)/(X1-X0); |
---|
3299 | |
---|
3300 | X2 = -inter/slope; |
---|
3301 | |
---|
3302 | denom = (Pnorm(B-K+X2,.0,1.)-Pnorm(A-K+X2,.0,1.)); |
---|
3303 | |
---|
3304 | if(denom < 1.E-10) |
---|
3305 | { |
---|
3306 | /* printf("\n. X2 = %f Y2=%f num=%f denom=%f Y1=%f Y0=%f X1=%f X0=%f mode=%f sd=%f",X2,Y2,num,denom,Y1,Y0,X1,X0,mode,sd); */ |
---|
3307 | *err = YES; |
---|
3308 | break; |
---|
3309 | } |
---|
3310 | |
---|
3311 | /* Y2 = Dnorm(X2-K,.0,1.)/(1. - Pnorm(X2-K,.0,1.)) - X2; */ |
---|
3312 | Y2 = (Dnorm(A-K+X2,.0,1.)-Dnorm(B-K+X2,.0,1.))/(Pnorm(B-K+X2,.0,1.)-Pnorm(A-K+X2,.0,1.)) - X2; |
---|
3313 | |
---|
3314 | /* printf("\n. X2 = %f Y2=%f num=%f denom=%f Y1=%f Y0=%f X1=%f X0=%f",X2,Y2,num,denom,Y1,Y0,X1,X0); */ |
---|
3315 | |
---|
3316 | if(X2 > X1) |
---|
3317 | { |
---|
3318 | X0 = X1; |
---|
3319 | X1 = X2; |
---|
3320 | Y0 = Y1; |
---|
3321 | Y1 = Y2; |
---|
3322 | } |
---|
3323 | else |
---|
3324 | { |
---|
3325 | X1 = X0; |
---|
3326 | X0 = X2; |
---|
3327 | Y1 = Y0; |
---|
3328 | Y0 = Y2; |
---|
3329 | } |
---|
3330 | }while(fabs(Y2) > eps); |
---|
3331 | |
---|
3332 | /* printf("\n. shift = %f X2=%f Y2 = %f",X2*sd,X2,Y2); */ |
---|
3333 | |
---|
3334 | return X2 * sd; |
---|
3335 | } |
---|
3336 | |
---|
3337 | ////////////////////////////////////////////////////////////// |
---|
3338 | ////////////////////////////////////////////////////////////// |
---|
3339 | |
---|
3340 | |
---|
3341 | phydbl Sample_Average_Rate(t_node *a, t_node *d, t_tree *tree) |
---|
3342 | { |
---|
3343 | return(-1.); |
---|
3344 | } |
---|
3345 | |
---|
3346 | ////////////////////////////////////////////////////////////// |
---|
3347 | ////////////////////////////////////////////////////////////// |
---|
3348 | |
---|
3349 | |
---|
3350 | void RATES_Update_Mean_Br_Len(int iter, t_tree *tree) |
---|
3351 | { |
---|
3352 | int i,dim; |
---|
3353 | phydbl *mean; |
---|
3354 | |
---|
3355 | if(tree->rates->update_mean_l == NO) return; |
---|
3356 | |
---|
3357 | dim = 2*tree->n_otu-3; |
---|
3358 | mean = tree->rates->mean_l; |
---|
3359 | |
---|
3360 | For(i,dim) |
---|
3361 | { |
---|
3362 | mean[i] *= (phydbl)iter; |
---|
3363 | mean[i] += tree->a_edges[i]->l->v; |
---|
3364 | mean[i] /= (phydbl)(iter+1); |
---|
3365 | } |
---|
3366 | } |
---|
3367 | |
---|
3368 | ////////////////////////////////////////////////////////////// |
---|
3369 | ////////////////////////////////////////////////////////////// |
---|
3370 | |
---|
3371 | |
---|
3372 | void RATES_Update_Cov_Br_Len(int iter, t_tree *tree) |
---|
3373 | { |
---|
3374 | int i,j,dim; |
---|
3375 | phydbl *mean,*cov; |
---|
3376 | |
---|
3377 | if(tree->rates->update_cov_l == NO) return; |
---|
3378 | |
---|
3379 | dim = 2*tree->n_otu-3; |
---|
3380 | mean = tree->rates->mean_l; |
---|
3381 | cov = tree->rates->cov_l; |
---|
3382 | |
---|
3383 | For(i,dim) |
---|
3384 | { |
---|
3385 | For(j,dim) |
---|
3386 | { |
---|
3387 | cov[i*dim+j] += mean[i]*mean[j]; |
---|
3388 | cov[i*dim+j] *= (phydbl)tree->mcmc->run; |
---|
3389 | cov[i*dim+j] += tree->a_edges[i]->l->v * tree->a_edges[j]->l->v; |
---|
3390 | cov[i*dim+j] /= (phydbl)(tree->mcmc->run+1); |
---|
3391 | cov[i*dim+j] -= mean[i]*mean[j]; |
---|
3392 | |
---|
3393 | if(i == j && cov[i*dim+j] < MIN_VAR_BL) cov[i*dim+j] = MIN_VAR_BL; |
---|
3394 | } |
---|
3395 | } |
---|
3396 | } |
---|
3397 | |
---|
3398 | ////////////////////////////////////////////////////////////// |
---|
3399 | ////////////////////////////////////////////////////////////// |
---|
3400 | |
---|
3401 | |
---|
3402 | void RATES_Set_Mean_L(t_tree *tree) |
---|
3403 | { |
---|
3404 | int i; |
---|
3405 | For(i,2*tree->n_otu-3) |
---|
3406 | { |
---|
3407 | tree->rates->mean_l[i] = tree->a_edges[i]->l->v; |
---|
3408 | } |
---|
3409 | } |
---|
3410 | |
---|
3411 | ////////////////////////////////////////////////////////////// |
---|
3412 | ////////////////////////////////////////////////////////////// |
---|
3413 | |
---|
3414 | |
---|
3415 | void RATES_Record_Times(t_tree *tree) |
---|
3416 | { |
---|
3417 | int i; |
---|
3418 | |
---|
3419 | if(tree->rates->nd_t_recorded == YES) |
---|
3420 | { |
---|
3421 | PhyML_Printf("\n. Overwriting recorded times is forbidden.\n"); |
---|
3422 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
3423 | Exit("\n"); |
---|
3424 | } |
---|
3425 | |
---|
3426 | For(i,2*tree->n_otu-1) tree->rates->buff_t[i] = tree->rates->nd_t[i]; |
---|
3427 | } |
---|
3428 | |
---|
3429 | ////////////////////////////////////////////////////////////// |
---|
3430 | ////////////////////////////////////////////////////////////// |
---|
3431 | |
---|
3432 | |
---|
3433 | void RATES_Reset_Times(t_tree *tree) |
---|
3434 | { |
---|
3435 | int i; |
---|
3436 | tree->rates->nd_t_recorded = NO; |
---|
3437 | For(i,2*tree->n_otu-1) tree->rates->nd_t[i] = tree->rates->buff_t[i]; |
---|
3438 | } |
---|
3439 | |
---|
3440 | ////////////////////////////////////////////////////////////// |
---|
3441 | ////////////////////////////////////////////////////////////// |
---|
3442 | |
---|
3443 | |
---|
3444 | void RATES_Record_Rates(t_tree *tree) |
---|
3445 | { |
---|
3446 | int i; |
---|
3447 | |
---|
3448 | if(tree->rates->br_r_recorded == YES) |
---|
3449 | { |
---|
3450 | PhyML_Printf("\n. Overwriting recorded rates is forbidden.\n"); |
---|
3451 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
3452 | Exit("\n"); |
---|
3453 | } |
---|
3454 | |
---|
3455 | For(i,2*tree->n_otu-2) tree->rates->buff_r[i] = tree->rates->br_r[i]; |
---|
3456 | } |
---|
3457 | |
---|
3458 | ////////////////////////////////////////////////////////////// |
---|
3459 | ////////////////////////////////////////////////////////////// |
---|
3460 | |
---|
3461 | |
---|
3462 | void RATES_Reset_Rates(t_tree *tree) |
---|
3463 | { |
---|
3464 | int i; |
---|
3465 | tree->rates->br_r_recorded = NO; |
---|
3466 | For(i,2*tree->n_otu-2) tree->rates->br_r[i] = tree->rates->buff_r[i]; |
---|
3467 | } |
---|
3468 | |
---|
3469 | ////////////////////////////////////////////////////////////// |
---|
3470 | ////////////////////////////////////////////////////////////// |
---|
3471 | |
---|
3472 | void RATES_Set_Clock_And_Nu_Max(t_tree *tree) |
---|
3473 | { |
---|
3474 | phydbl dt,nu; |
---|
3475 | phydbl min_t; |
---|
3476 | int i; |
---|
3477 | phydbl step; |
---|
3478 | phydbl l_max; |
---|
3479 | phydbl max_clock; |
---|
3480 | phydbl r_max; |
---|
3481 | phydbl tune; |
---|
3482 | phydbl pa,pb; |
---|
3483 | |
---|
3484 | if(tree->rates->model == THORNE || |
---|
3485 | tree->rates->model == GUINDON) |
---|
3486 | { |
---|
3487 | tune = 1.05; |
---|
3488 | |
---|
3489 | if(tree->rates->model_log_rates == NO) |
---|
3490 | { |
---|
3491 | r_max = LOG(tree->rates->max_rate); |
---|
3492 | } |
---|
3493 | else |
---|
3494 | { |
---|
3495 | r_max = tree->rates->max_rate; |
---|
3496 | } |
---|
3497 | |
---|
3498 | l_max = tree->mod->l_max; |
---|
3499 | |
---|
3500 | min_t = .0; |
---|
3501 | For(i,2*tree->n_otu-1) if(tree->rates->t_prior_min[i] < min_t) min_t = tree->rates->t_prior_min[i]; |
---|
3502 | |
---|
3503 | dt = FABS(min_t); |
---|
3504 | max_clock = l_max / dt; |
---|
3505 | |
---|
3506 | nu = 1.E-10; |
---|
3507 | step = 1.E-1; |
---|
3508 | do |
---|
3509 | { |
---|
3510 | do |
---|
3511 | { |
---|
3512 | nu += step; |
---|
3513 | pa = Dnorm(0.0, 0.0,SQRT(nu*dt)); |
---|
3514 | pb = Dnorm(r_max,0.0,SQRT(nu*dt)); |
---|
3515 | }while(pa/pb > tune); |
---|
3516 | nu -= step; |
---|
3517 | step /= 10.; |
---|
3518 | }while(step > 1.E-10); |
---|
3519 | |
---|
3520 | tree->rates->max_nu = nu; |
---|
3521 | /* tree->rates->max_nu = 1.0; */ |
---|
3522 | tree->rates->max_clock = max_clock; |
---|
3523 | |
---|
3524 | PhyML_Printf("\n. Clock rate parameter upper bound set to %f expected subst./site/time unit",tree->rates->max_clock); |
---|
3525 | PhyML_Printf("\n. Autocorrelation parameter upper bound set to %f",tree->rates->max_nu); |
---|
3526 | } |
---|
3527 | } |
---|
3528 | |
---|
3529 | ////////////////////////////////////////////////////////////// |
---|
3530 | ////////////////////////////////////////////////////////////// |
---|
3531 | |
---|
3532 | void RATES_Set_Birth_Rate_Boundaries(t_tree *tree) |
---|
3533 | { |
---|
3534 | phydbl lbda; |
---|
3535 | phydbl p_above_min,p_below_max; |
---|
3536 | phydbl min,max; |
---|
3537 | int assign = YES; |
---|
3538 | |
---|
3539 | min = -0.01*tree->rates->t_prior_max[tree->n_root->num]; |
---|
3540 | max = -100.*tree->rates->t_prior_min[tree->n_root->num]; |
---|
3541 | |
---|
3542 | for(lbda = 0.0001; lbda < 10; lbda+=0.0001) |
---|
3543 | { |
---|
3544 | p_above_min = 1. - POW(1.-EXP(-lbda*min),tree->n_otu); |
---|
3545 | p_below_max = POW(1.-EXP(-lbda*max),tree->n_otu); |
---|
3546 | |
---|
3547 | if(p_above_min < 1.E-10) |
---|
3548 | { |
---|
3549 | tree->rates->birth_rate_max = lbda; |
---|
3550 | break; |
---|
3551 | } |
---|
3552 | if(p_below_max > 1.E-10 && assign==YES) |
---|
3553 | { |
---|
3554 | assign = NO; |
---|
3555 | tree->rates->birth_rate_min = lbda; |
---|
3556 | } |
---|
3557 | } |
---|
3558 | |
---|
3559 | /* tree->rates->birth_rate_min = 1.E-6; */ |
---|
3560 | /* tree->rates->birth_rate_max = 1.; */ |
---|
3561 | PhyML_Printf("\n. Birth rate lower bound set to %f.",tree->rates->birth_rate_min); |
---|
3562 | PhyML_Printf("\n. Birth rate upper bound set to %f.",tree->rates->birth_rate_max); |
---|
3563 | |
---|
3564 | } |
---|
3565 | |
---|
3566 | |
---|
3567 | ////////////////////////////////////////////////////////////// |
---|
3568 | ////////////////////////////////////////////////////////////// |
---|
3569 | |
---|
3570 | |
---|
3571 | void RATES_Write_Mean_R_On_Edge_Label(t_node *a, t_node *d, t_edge *b, t_tree *tree) |
---|
3572 | { |
---|
3573 | |
---|
3574 | if(b) |
---|
3575 | { |
---|
3576 | if(!b->labels) |
---|
3577 | { |
---|
3578 | Make_New_Edge_Label(b); |
---|
3579 | b->n_labels++; |
---|
3580 | } |
---|
3581 | sprintf(b->labels[0],"%f",tree->rates->mean_r[d->num] / (phydbl)(tree->mcmc->run/tree->mcmc->sample_interval+1.)); |
---|
3582 | } |
---|
3583 | |
---|
3584 | if(d->tax) return; |
---|
3585 | else |
---|
3586 | { |
---|
3587 | int i; |
---|
3588 | For(i,3) |
---|
3589 | { |
---|
3590 | if((d->v[i] != a) && (d->b[i] != tree->e_root)) |
---|
3591 | { |
---|
3592 | RATES_Write_Mean_R_On_Edge_Label(d,d->v[i],d->b[i],tree); |
---|
3593 | } |
---|
3594 | } |
---|
3595 | } |
---|
3596 | } |
---|
3597 | |
---|
3598 | ////////////////////////////////////////////////////////////// |
---|
3599 | ////////////////////////////////////////////////////////////// |
---|
3600 | |
---|
3601 | |
---|
3602 | |
---|
3603 | ////////////////////////////////////////////////////////////// |
---|
3604 | ////////////////////////////////////////////////////////////// |
---|
3605 | |
---|
3606 | |
---|
3607 | phydbl RATES_Get_Mean_Rate_In_Subtree(t_node *root, t_tree *tree) |
---|
3608 | { |
---|
3609 | phydbl sum; |
---|
3610 | int n; |
---|
3611 | |
---|
3612 | sum = 0.0; |
---|
3613 | n = 0; |
---|
3614 | |
---|
3615 | if(root->tax == NO) |
---|
3616 | { |
---|
3617 | if(root == tree->n_root) |
---|
3618 | { |
---|
3619 | RATES_Get_Mean_Rate_In_Subtree_Pre(root,root->v[2],&sum,&n,tree); |
---|
3620 | RATES_Get_Mean_Rate_In_Subtree_Pre(root,root->v[1],&sum,&n,tree); |
---|
3621 | } |
---|
3622 | else |
---|
3623 | { |
---|
3624 | int i; |
---|
3625 | For(i,3) |
---|
3626 | { |
---|
3627 | if(root->v[i] != root->anc && root->b[i] != tree->e_root) |
---|
3628 | { |
---|
3629 | RATES_Get_Mean_Rate_In_Subtree_Pre(root,root->v[i],&sum,&n,tree); |
---|
3630 | } |
---|
3631 | } |
---|
3632 | } |
---|
3633 | return sum/(phydbl)n; |
---|
3634 | } |
---|
3635 | else |
---|
3636 | { |
---|
3637 | return 0.0; |
---|
3638 | } |
---|
3639 | |
---|
3640 | |
---|
3641 | } |
---|
3642 | |
---|
3643 | ////////////////////////////////////////////////////////////// |
---|
3644 | ////////////////////////////////////////////////////////////// |
---|
3645 | |
---|
3646 | |
---|
3647 | void RATES_Get_Mean_Rate_In_Subtree_Pre(t_node *a, t_node *d, phydbl *sum, int *n, t_tree *tree) |
---|
3648 | { |
---|
3649 | (*sum) += EXP(tree->rates->nd_r[d->num]); |
---|
3650 | (*n) += 1; |
---|
3651 | |
---|
3652 | if(d->tax == YES) return; |
---|
3653 | else |
---|
3654 | { |
---|
3655 | int i; |
---|
3656 | For(i,3) |
---|
3657 | { |
---|
3658 | if(d->v[i] != a && d->b[i] != tree->e_root) |
---|
3659 | { |
---|
3660 | RATES_Get_Mean_Rate_In_Subtree_Pre(d,d->v[i],sum,n,tree); |
---|
3661 | } |
---|
3662 | } |
---|
3663 | } |
---|
3664 | } |
---|
3665 | |
---|
3666 | ////////////////////////////////////////////////////////////// |
---|
3667 | ////////////////////////////////////////////////////////////// |
---|
3668 | |
---|
3669 | |
---|
3670 | char *RATES_Get_Model_Name(int model) |
---|
3671 | { |
---|
3672 | char *s; |
---|
3673 | |
---|
3674 | s = (char *)mCalloc(T_MAX_NAME,sizeof(char)); |
---|
3675 | |
---|
3676 | switch(model) |
---|
3677 | { |
---|
3678 | case GUINDON : {strcpy(s,"gbs"); break;} |
---|
3679 | case THORNE : {strcpy(s,"gbd"); break;} |
---|
3680 | case GAMMA : {strcpy(s,"gamma"); break;} |
---|
3681 | case STRICTCLOCK : {strcpy(s,"clock"); break;} |
---|
3682 | default : |
---|
3683 | { |
---|
3684 | PhyML_Printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); |
---|
3685 | Exit("\n"); |
---|
3686 | break; |
---|
3687 | } |
---|
3688 | } |
---|
3689 | return s; |
---|
3690 | } |
---|
3691 | |
---|
3692 | ////////////////////////////////////////////////////////////// |
---|
3693 | ////////////////////////////////////////////////////////////// |
---|
3694 | |
---|
3695 | |
---|
3696 | void RATES_Get_Survival_Ranks(t_tree *tree) |
---|
3697 | { |
---|
3698 | int i,j; |
---|
3699 | phydbl rank; |
---|
3700 | |
---|
3701 | |
---|
3702 | For(i,2*tree->n_otu-2) |
---|
3703 | { |
---|
3704 | rank = 0.; |
---|
3705 | For(j,2*tree->n_otu-2) |
---|
3706 | { |
---|
3707 | if(tree->rates->br_r[i] > tree->rates->br_r[j]) rank += 1.0; |
---|
3708 | } |
---|
3709 | |
---|
3710 | tree->rates->survival_rank[i] = rank; |
---|
3711 | } |
---|
3712 | |
---|
3713 | |
---|
3714 | } |
---|
3715 | |
---|
3716 | ////////////////////////////////////////////////////////////// |
---|
3717 | ////////////////////////////////////////////////////////////// |
---|
3718 | |
---|
3719 | ////////////////////////////////////////////////////////////// |
---|
3720 | ////////////////////////////////////////////////////////////// |
---|
3721 | |
---|
3722 | ////////////////////////////////////////////////////////////// |
---|
3723 | ////////////////////////////////////////////////////////////// |
---|
3724 | |
---|
3725 | ////////////////////////////////////////////////////////////// |
---|
3726 | ////////////////////////////////////////////////////////////// |
---|
3727 | |
---|
3728 | ////////////////////////////////////////////////////////////// |
---|
3729 | ////////////////////////////////////////////////////////////// |
---|
3730 | |
---|
3731 | |
---|
3732 | |
---|