-
Notifications
You must be signed in to change notification settings - Fork 0
/
proposal.c
17206 lines (15173 loc) · 546 KB
/
proposal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* MrBayes 3
*
* (c) 2002-2013
*
* John P. Huelsenbeck
* Dept. Integrative Biology
* University of California, Berkeley
* Berkeley, CA 94720-3140
*
* Fredrik Ronquist
* Swedish Museum of Natural History
* Box 50007
* SE-10405 Stockholm, SWEDEN
*
* With important contributions by
*
* Paul van der Mark ([email protected])
* Maxim Teslenko ([email protected])
*
* and by many users (run 'acknowledgments' to see more info)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details (www.gnu.org).
*
*/
#include "bayes.h"
#include "best.h"
#include "command.h"
#include "mcmc.h"
#include "model.h"
#include "proposal.h"
#include "utils.h"
const char* const svnRevisionProposalC = "$Rev: 1032 $"; /* Revision keyword which is expanded/updated by svn on each commit/update */
/* debugging compiler statements */
#undef DEBUG_LOCAL
#undef DEBUG_UNROOTED_SLIDER
#undef DEBUG_ParsSPR
#undef DEBUG_ExtSS
#undef DEBUG_CSLIDER
#undef DEBUG_ExtSPRClock
#undef DEBUG_ParsSPRClock
#undef DEBUG_ExtTBR
#undef DEBUG_NNIClock
#undef DEBUG_SPLITMERGE
#undef DEBUG_FBDPR
extern int *chainId;
int gTopologyHasChanged; /* flag whether topology has changed */
void TouchAllTreeNodes (ModelInfo *m, int chain);
int Move_Aamodel (Param *param, int chain, RandLong *seed, MrBFlt *lnPriorRatio, MrBFlt *lnProposalRatio, MrBFlt *mvp)
{
/* Change amino acid model for model mixing
amino acid model ID's
AAMODEL_POISSON 0
AAMODEL_JONES 1
AAMODEL_DAY 2
AAMODEL_MTREV 3
AAMODEL_MTMAM 4
AAMODEL_WAG 5
AAMODEL_RTREV 6
AAMODEL_CPREV 7
AAMODEL_VT 8
AAMODEL_BLOSUM 9 */
int i, oldM, newM;
MrBFlt *bs, *subValue;
ModelParams *mp;
/* get model params */
mp = &modelParams[param->relParts[0]];
subValue = GetParamSubVals(param, chain, state[chain]);
/* get old value of model */
newM = oldM = (int)*GetParamVals(param, chain, state[chain]);
/* get a new model ID */
do
{
newM = (int)(RandomNumber(seed) * 10);
} while (newM == oldM);
/* set proposal ratio */
*lnProposalRatio = 0.0;
/* set prior ratio */
*lnPriorRatio = subValue[newM] - subValue[oldM];
/* copy new amino acid model ID back */
*GetParamVals(param, chain, state[chain]) = (MrBFlt)newM;
/* set amino acid frequencies */
bs = GetParamSubVals (modelSettings[param->relParts[0]].stateFreq, chain, state[chain]);
if (newM == AAMODEL_POISSON)
{
for (i=0; i<mp->nStates; i++)
bs[i] = 1.0 / 20.0;
}
else if (newM == AAMODEL_JONES)
{
for (i=0; i<mp->nStates; i++)
bs[i] = jonesPi[i];
}
else if (newM == AAMODEL_DAY)
{
for (i=0; i<mp->nStates; i++)
bs[i] = dayhoffPi[i];
}
else if (newM == AAMODEL_MTREV)
{
for (i=0; i<mp->nStates; i++)
bs[i] = mtrev24Pi[i];
}
else if (newM == AAMODEL_MTMAM)
{
for (i=0; i<mp->nStates; i++)
bs[i] = mtmamPi[i];
}
else if (newM == AAMODEL_WAG)
{
for (i=0; i<mp->nStates; i++)
bs[i] = wagPi[i];
}
else if (newM == AAMODEL_RTREV)
{
for (i=0; i<mp->nStates; i++)
bs[i] = rtrevPi[i];
}
else if (newM == AAMODEL_CPREV)
{
for (i=0; i<mp->nStates; i++)
bs[i] = cprevPi[i];
}
else if (newM == AAMODEL_VT)
{
for (i=0; i<mp->nStates; i++)
bs[i] = vtPi[i];
}
else if (newM == AAMODEL_BLOSUM)
{
for (i=0; i<mp->nStates; i++)
bs[i] = blosPi[i];
}
/* Set update flags for all partitions that share this amino acid model. Note that the conditional
likelihood update flags have been set before we even call this function. */
for (i=0; i<param->nRelParts; i++)
TouchAllTreeNodes(&modelSettings[param->relParts[i]],chain);
/* Set update flags for cijks for all affected partitions. */
for (i=0; i<param->nRelParts; i++)
modelSettings[param->relParts[i]].upDateCijk = YES;
return (NO_ERROR);
MrBayesPrint ("%lf", *mvp); /* just because I am tired of seeing the unused parameter error msg */
}
int Move_AddDeleteCPPEvent (Param *param, int chain, RandLong *seed, MrBFlt *lnPriorRatio, MrBFlt *lnProposalRatio, MrBFlt *mvp)
{
/* add or delete one Poisson process event */
int i, k, addEvent, *nEvents, numEvents;
MrBFlt sigma, m, lognormalLnProb, **position, **rateMultiplier, length, pos, rate;
TreeNode *p, *q;
ModelInfo *model;
Tree *t;
/* get the model settings */
model = &modelSettings[param->relParts[0]];
/* get cpp rate */
rate = *GetParamVals (model->cppRate, chain, state[chain]);
/* get sigma of lognormal of rate multipliers */
sigma = *GetParamVals (model->cppMultDev, chain, state[chain]);
/* get the cpp event data */
nEvents = param->nEvents[2*chain+state[chain]];
position = param->position[2*chain+state[chain]];
rateMultiplier = param->rateMult[2*chain+state[chain]];
/* get tree */
t = GetTree (param, chain, state[chain]);
/* pick a branch */
do
{
p = t->allDownPass[(int)(RandomNumber(seed) * (t->nNodes -2))];
} while (p->anc == NULL || (p->anc->anc == NULL));
/* get number of events for convenience */
numEvents = nEvents[p->index];
/* add or delete ? */
addEvent = NO;
if (numEvents == 0)
addEvent = YES;
else if (RandomNumber(seed) < 0.5)
addEvent = YES;
if (addEvent == NO)
{
/* delete event */
/* choose random event */
k = (int) (RandomNumber(seed) * numEvents);
/* save multiplier to be deleted */
m = rateMultiplier[p->index][k];
/* rearrange and reduce */
for (i=k; i<numEvents-1; i++)
{
position[p->index][i] = position[p->index][i+1];
rateMultiplier[p->index][i] = rateMultiplier[p->index][i+1];
}
if (numEvents-1 > 0)
{
position[p->index] = (MrBFlt *) SafeRealloc ((void *) position[p->index], (numEvents-1)*sizeof(MrBFlt));
rateMultiplier[p->index] = (MrBFlt *) SafeRealloc ((void *) rateMultiplier[p->index], (numEvents-1)*sizeof(MrBFlt));
assert (position[p->index] != NULL && rateMultiplier[p->index] != NULL);
}
else
{
free (position[p->index]);
free (rateMultiplier[p->index]);
position[p->index] = rateMultiplier[p->index] = NULL;
}
/* update number of events */
nEvents[p->index]--;
}
else /* if (addEvent == YES) */
{
/* add event */
/* generate new multiplier */
m = LogNormalRandomVariable (0.0, sigma, seed);
/* generate new position */
pos = RandomNumber(seed);
/* find place in current array */
for (k=0; k<numEvents; k++)
{
if (position[p->index][k] > pos)
break;
}
/* rearrange and insert */
position[p->index] = (MrBFlt *) SafeRealloc ((void *)position[p->index], (numEvents+1)*sizeof(MrBFlt));
rateMultiplier[p->index] = (MrBFlt *) SafeRealloc ((void *)rateMultiplier[p->index], (numEvents+1)*sizeof(MrBFlt));
assert (position[p->index] != NULL && rateMultiplier[p->index] != NULL);
for (i=numEvents; i>k; i--)
{
position[p->index][i] = position[p->index][i-1];
rateMultiplier[p->index][i] = rateMultiplier[p->index][i-1];
}
position[p->index][k] = pos;
rateMultiplier[p->index][k] = m;
/* update number of events */
nEvents[p->index]++;
}
/* the CPP process is relative to expected substitutions */
length = p->length;
lognormalLnProb = LnProbLogNormal(0.0, sigma, m);
if (addEvent == YES)
(*lnPriorRatio) = lognormalLnProb + log (rate);
else
(*lnPriorRatio) = -(lognormalLnProb + log(rate));
if (addEvent == YES)
/* note that nEvents[p->index] now contains k+1 after addition */
(*lnProposalRatio) = log (length / ((double) nEvents[p->index])) - lognormalLnProb;
else
/* note that nEvents[p->index] contains k after deletion */
(*lnProposalRatio) = log ((double)(nEvents[p->index]+1) / length) + lognormalLnProb;
/* take care of asymmetric add and delete probabilities around 0 and 1 events */
if (addEvent == YES && nEvents[p->index] == 1)
(*lnProposalRatio) += log (0.5);
else if (addEvent == NO && nEvents[p->index] == 0)
(*lnProposalRatio) += log (2.0);
/* update evolLengths in subtree above new event */
if (UpdateCppEvolLengths (param, p, chain)==ERROR)
{
abortMove=YES;
return (NO_ERROR);
}
/* set update of cond likes down to root */
/* crown tree update flags set in UpdateCppEvolLengths */
q = p->anc;
while (q->anc != NULL)
{
q->upDateCl = YES;
q = q->anc;
}
return (NO_ERROR);
MrBayesPrint ("%lf", *mvp); /* just because I am tired of seeing the unused parameter error msg */
}
int Move_Adgamma (Param *param, int chain, RandLong *seed, MrBFlt *lnPriorRatio, MrBFlt *lnProposalRatio, MrBFlt *mvp)
{
/* Change correlation parameter (-1, 1) of adgamma model */
int i, isValidP;
MrBFlt oldP, newP, window, minP, maxP, ran, *markovTiValues;
ModelParams *mp;
/* get size of window, centered on current rho */
window = mvp[0];
/* get model params */
mp = &modelParams[param->relParts[0]];
/* get minimum and maximum values for rho */
minP = mp->corrUni[0];
maxP = mp->corrUni[1];
/* get address of markovTi */
markovTiValues = GetParamSubVals (param, chain, state[chain]);
/* get old value of rho */
newP = oldP = *GetParamVals(param, chain, state[chain]);
/* change value for rho */
ran = RandomNumber(seed);
if (maxP-minP < window)
{
window = maxP-minP;
}
newP = oldP + window * (ran - 0.5);
/* check that new value is valid */
isValidP = NO;
do
{
if (newP < minP)
newP = 2* minP - newP;
else if (newP > maxP)
newP = 2 * maxP - newP;
else
isValidP = YES;
} while (isValidP == NO);
/* get proposal ratio */
*lnProposalRatio = 0.0;
/* get prior ratio */
*lnPriorRatio = 0.0;
/* copy new rho value back */
*GetParamVals(param, chain, state[chain]) = newP;
/* fill in new Markov trans probs */
AutodGamma (markovTiValues, newP, mp->numGammaCats);
/* Set update flags for all partitions that share this rho. Note that the conditional
likelihood update flags have been set before we even call this function. */
for (i=0; i<param->nRelParts; i++)
TouchAllTreeNodes(&modelSettings[param->relParts[i]],chain);
/* Update flags for divisions already set */
return (NO_ERROR);
}
int Move_Beta (Param *param, int chain, RandLong *seed, MrBFlt *lnPriorRatio, MrBFlt *lnProposalRatio, MrBFlt *mvp)
{
/* change symmetric Dirichlet variance using multiplier */
int i, j, k, isValidB, isPriorExp, nStates;
MrBFlt oldB, newB, minB, maxB, priorExp=0.0, *bs, ran, factor, tuning,
x, y;
ModelParams *mp;
/* get tuning parameter */
tuning = mvp[0]; /* multiplier tuning parameter lambda */
/* get model paramaters */
mp = &modelParams[param->relParts[0]];
/* get prior, minimum and maximum values for rate */
if (!strcmp(mp->symPiPr,"Uniform"))
{
isPriorExp = NO;
minB = mp->symBetaUni[0];
maxB = mp->symBetaUni[1];
}
else
{
isPriorExp = YES;
priorExp = mp->symBetaExp;
minB = SYMPI_MIN;
maxB = SYMPI_MAX;
}
/* get old value of symDir */
oldB = *GetParamVals(param, chain, state[chain]);
/* change value */
ran = RandomNumber(seed);
factor = exp(tuning * (ran - 0.5));
newB = oldB * factor;
/* check validity */
isValidB = NO;
do
{
if (newB < minB)
newB = minB * minB / newB;
else if (newB > maxB)
newB = maxB * maxB / newB;
else
isValidB = YES;
} while (isValidB == NO);
/* set new value of symDir */
*GetParamVals(param, chain, state[chain]) = newB;
/* get proposal ratio */
*lnProposalRatio = log (newB / oldB);
/* get prior ratio */
if (isPriorExp == YES)
{
*lnPriorRatio = priorExp * (oldB - newB);
}
else
*lnPriorRatio = 0.0;
/* fill in the new betacat frequencies */
bs = GetParamStdStateFreqs(param, chain, state[chain]);
k = mp->numBetaCats;
BetaBreaks (newB, newB, bs, k);
k *= 2;
for (i=k-2; i>0; i-=2)
{
bs[i] = bs[i/2];
}
for (i=1; i<k; i+=2)
{
bs[i] = 1.0 - bs[i-1];
}
/* if there are multistate characters, update prior probability of current pis */
bs += 2 * mp->numBetaCats;
for (i=0; i<param->nSympi; i++)
{
/* get number of states */
nStates = param->sympinStates[i];
/* get prior ratio update */
x = LnGamma(newB*nStates) - nStates*LnGamma(newB);
y = LnGamma(oldB*nStates) - nStates*LnGamma(oldB);
for (j=0; j<nStates; j++)
{
x += (newB-1.0)*log(bs[j]);
y += (oldB-1.0)*log(bs[j]);
}
(*lnPriorRatio) += x - y;
/* update pointer to state freqs */
bs += nStates;
}
/* Set update flags for all tree nodes. Note that the conditional
likelihood update flags have been set for the relevant partitions
before we even call the move function. */
for (i=0; i<param->nRelParts; i++)
TouchAllTreeNodes(&modelSettings[param->relParts[i]],chain);
/* may need to hit update flag for cijks if we have multistate characters */
for (i=0; i<param->nRelParts; i++)
{
if (modelSettings[param->relParts[i]].nCijkParts > 0)
modelSettings[param->relParts[i]].upDateCijk = YES;
}
return (NO_ERROR);
}
int Move_BrLen (Param *param, int chain, RandLong *seed, MrBFlt *lnPriorRatio, MrBFlt *lnProposalRatio, MrBFlt *mvp)
{
/* change one branch length */
MrBFlt tuning, maxV, minV, m, newM, brlensPrExp=0.0;
TreeNode *p;
ModelParams *mp;
Tree *t;
int isVPriorExp;
tuning = mvp[0]; /* Larget & Simon's tuning parameter lambda */
mp = &modelParams[param->relParts[0]];
/* max and min brlen */
if (param->paramId == BRLENS_UNI)
{
minV = mp->brlensUni[0];
maxV = mp->brlensUni[1];
isVPriorExp = NO;
}
else if (param->paramId == BRLENS_GamDir)
{
minV = BRLENS_MIN;
maxV = BRLENS_MAX;
isVPriorExp = 2;
}
else if (param->paramId == BRLENS_iGmDir)
{
minV = BRLENS_MIN;
maxV = BRLENS_MAX;
isVPriorExp = 3;
}
else if (param->paramId == BRLENS_twoExp)
{
minV = BRLENS_MIN;
maxV = BRLENS_MAX;
isVPriorExp = 4;
}
else
{
minV = BRLENS_MIN;
maxV = BRLENS_MAX;
brlensPrExp = mp->brlensExp;
isVPriorExp = YES;
}
/* get tree */
t = GetTree (param, chain, state[chain]);
/* Dirichlet or twoExp prior */
if (isVPriorExp > 1)
(*lnPriorRatio) = -LogDirPrior(t, mp, isVPriorExp);
/* pick a branch */
do {
p = t->allDownPass[(int)(RandomNumber(seed) * t->nNodes)];
}
while (p->anc == NULL || (t->isRooted == YES && p->anc->anc == NULL));
/* determine new length */
m = p->length;
newM = m * exp(tuning * (RandomNumber(seed) - 0.5));
/* reflect new length if necessary */
while (newM < minV || newM > maxV)
{
if (newM < minV)
newM = minV * minV / newM;
else if (newM > maxV)
newM = maxV * maxV / newM;
}
p->length = newM;
/* calculate proposal ratio */
/* must be based on new length after reflection */
(*lnProposalRatio) = log(newM / m);
/* set flags for update of transition probabilities at p */
p->upDateTi = YES;
/* set the update flag for cond likes if p is connected to root in unrooted */
/* tree, if this is not done, cond likes are not updated in this case */
if (t->isRooted == NO && p->anc->anc == NULL)
p->upDateCl = YES;
/* set flags for update of cond likes from p->anc and down to root */
while (p->anc->anc != NULL)
{
p = p->anc;
p->upDateCl = YES;
}
/* update prior if exponential prior on branch lengths */
if (param->paramId == BRLENS_EXP)
(*lnPriorRatio) = brlensPrExp * (m - newM);
/* Dirichlet or twoExp prior */
else if (isVPriorExp > 1)
(*lnPriorRatio) += LogDirPrior(t, mp, isVPriorExp);
return (NO_ERROR);
}
int Move_ClockRate_M (Param *param, int chain, RandLong *seed, MrBFlt *lnPriorRatio, MrBFlt *lnProposalRatio, MrBFlt *mvp)
{
/* change clock rate using multiplier */
int i, j, k, *nEvents;
MrBFlt oldRate, newRate, factor, lambda, nu, igrvar, *brlens, *igrRate, *tk02Rate,
N, newTheta, oldTheta, growth, newLnPrior, oldLnPrior;
Tree *t, *oldT;
TreeNode *p, *q;
Param *treeParam, *subParm;
ModelParams *mp;
ModelInfo *m;
/* get old value of clock rate */
oldRate = *GetParamVals(param, chain, state[chain]);
/* Rely on general algorithm to change the value */
Move_PosRealMultiplier(param, chain, seed, lnPriorRatio, lnProposalRatio, mvp);
if (abortMove == YES)
return NO_ERROR;
/* get new value of clock rate */
newRate = *GetParamVals(param, chain, state[chain]);
/* calculate factor */
factor = newRate / oldRate;
/* clock rate applies to all clock trees */
for (i = 0; i < numTrees; i++)
{
t = GetTreeFromIndex(i, chain, state[chain]);
if (t->isClock == NO)
continue;
if (!strcmp(modelParams[t->relParts[0]].clockPr, "Fixed"))
continue;
oldT = GetTreeFromIndex(i, chain, 1^state[chain]);
treeParam = modelSettings[t->relParts[0]].brlens;
/* adjust the node depths and lengths */
for (j = 0; j < t->nNodes-1; j++)
{
p = t->allDownPass[j];
q = oldT->allDownPass[j];
p->nodeDepth *= factor; /* no harm done if nodeDepth==0.0 (undated tips) */
p->length *= factor; /* no harm done if length==0.0 (root or fossil ancestors)*/
if (p->length < 0.0 || p->length > BRLENS_MAX ||
(q->length > BRLENS_MIN && p->length < BRLENS_MIN) ||
(q->length < TIME_MIN && p->length > TIME_MIN))
{ /* consider ancestral fossil (brl=0) in fossilized bd tree */
abortMove = YES;
return (NO_ERROR);
}
}
/* prior ratio for coalecent tree, as theta is changed */
mp = &modelParams[t->relParts[0]];
if (!strcmp(mp->clockPr,"Coalescence"))
{
m = &modelSettings[t->relParts[0]];
N = *GetParamVals(m->popSize, chain, state[chain]);
if (!strcmp(mp->ploidy, "Diploid"))
N *= 4.0;
else if (!strcmp(mp->ploidy, "Zlinked"))
N *= 3.0;
else
N *= 2.0;
oldTheta = N * oldRate;
newTheta = N * newRate;
if (!strcmp(mp->growthPr, "Fixed"))
growth = mp->growthFix;
else
growth = *GetParamVals(m->growthRate, chain, state[chain]);
if (LnCoalescencePriorPr (oldT, &oldLnPrior, oldTheta, growth) == ERROR)
{
MrBayesPrint ("%s Problem calculating prior for coalescent process\n", spacer);
return (ERROR);
}
if (LnCoalescencePriorPr (t, &newLnPrior, newTheta, growth) == ERROR)
{
MrBayesPrint ("%s Problem calculating prior for coalescent process\n", spacer);
return (ERROR);
}
(*lnPriorRatio) += newLnPrior - oldLnPrior;
}
/* adjust proposal and prior ratio for relaxed clock models */
for (k = 0; k < treeParam->nSubParams; k++)
{
subParm = treeParam->subParams[k];
if (subParm->paramType == P_CPPEVENTS)
{
nEvents = subParm->nEvents[2*chain+state[chain]];
lambda = *GetParamVals (modelSettings[subParm->relParts[0]].cppRate, chain, state[chain]);
/* proposal ratio */
for (j=0; j<t->nNodes-2; j++)
{
p = t->allDownPass[j];
q = oldT->allDownPass[j];
(*lnProposalRatio) += nEvents[p->index ] * log (p->length / q->length);
}
/* prior ratio */
(*lnPriorRatio) += lambda * (TreeLen(oldT) - TreeLen(t));
/* update effective evolutionary lengths */
if (UpdateCppEvolLengths (subParm, t->root->left, chain) == ERROR)
{
abortMove = YES;
return (NO_ERROR);
}
}
else if ( subParm->paramType == P_TK02BRANCHRATES ||
(subParm->paramType == P_MIXEDBRCHRATES && *GetParamIntVals(subParm, chain, state[chain]) == RCL_TK02))
{
if (subParm->paramType == P_TK02BRANCHRATES)
nu = *GetParamVals (modelSettings[subParm->relParts[0]].tk02var, chain, state[chain]);
else
nu = *GetParamVals (modelSettings[subParm->relParts[0]].mixedvar, chain, state[chain]);
tk02Rate = GetParamVals (subParm, chain, state[chain]);
brlens = GetParamSubVals (subParm, chain, state[chain]);
/* no proposal ratio effect */
/* prior ratio and update of brlens */
for (j = 0; j < t->nNodes-2; j++)
{
p = t->allDownPass[j];
q = oldT->allDownPass[j];
if (p->length > 0.0) // not ancestral fossil
{
(*lnPriorRatio) -= LnProbTK02LogNormal (tk02Rate[q->anc->index], nu*q->length, tk02Rate[q->index]);
(*lnPriorRatio) += LnProbTK02LogNormal (tk02Rate[p->anc->index], nu*p->length, tk02Rate[p->index]);
brlens[p->index] = p->length * (tk02Rate[p->anc->index]+tk02Rate[p->index])/2.0;
}
}
}
else if ( subParm->paramType == P_IGRBRANCHRATES ||
(subParm->paramType == P_MIXEDBRCHRATES && *GetParamIntVals(subParm, chain, state[chain]) == RCL_IGR))
{
if (subParm->paramType == P_IGRBRANCHRATES)
igrvar = *GetParamVals (modelSettings[subParm->relParts[0]].igrvar, chain, state[chain]);
else
igrvar = *GetParamVals (modelSettings[subParm->relParts[0]].mixedvar, chain, state[chain]);
igrRate = GetParamVals (subParm, chain, state[chain]);
brlens = GetParamSubVals (subParm, chain, state[chain]);
/* prior ratio and update of brlens */
for (j = 0; j < t->nNodes-2; j++)
{
p = t->allDownPass[j];
q = oldT->allDownPass[j];
if (p->length > 0.0) // not ancestral fossil
{
(*lnPriorRatio) -= LnProbGamma (q->length/igrvar, q->length/igrvar, igrRate[q->index]);
(*lnPriorRatio) += LnProbGamma (p->length/igrvar, p->length/igrvar, igrRate[p->index]);
brlens[p->index] = igrRate[p->index] * p->length;
}
}
}
}
}
return (NO_ERROR);
}
int Move_CPPEventPosition (Param *param, int chain, RandLong *seed, MrBFlt *lnPriorRatio, MrBFlt *lnProposalRatio, MrBFlt *mvp)
{
/* move the position of one CPP event */
int i, j, k, *nEvents;
MrBFlt pos, temp, **position, **rateMultiplier;
TreeNode *p=NULL, *q;
Tree *t;
/* get the cpp event data */
nEvents = param->nEvents[2*chain+state[chain]];
position = param->position[2*chain+state[chain]];
rateMultiplier = param->rateMult[2*chain+state[chain]];
/* get tree */
t = GetTree (param, chain, state[chain]);
/* pick a branch and an event */
for (i=j=0; i<t->nNodes - 2; i++)
{
p = t->allDownPass[i];
j += nEvents[p->index];
}
if (j == 0)
{
abortMove = YES;
return (NO_ERROR);
}
k = (int) (RandomNumber(seed) * j);
for (i=j=0; i<t->nNodes - 2; i++)
{
p = t->allDownPass[i];
j += nEvents[p->index];
if (j > k)
break;
}
if (position[p->index] == NULL)
getchar();
/* find local index */
k = k - (j - nEvents[p->index]);
/* find new position */
pos = RandomNumber(seed);
if (pos < POS_MIN || 1.0 - pos < POS_MIN)
{
abortMove = YES;
return (NO_ERROR);
}
position[p->index][k] = pos;
/* sort events; bubble sort for now */
for (i=0; i<nEvents[p->index]; i++)
{
for (j=i+1; j<nEvents[p->index]; j++)
{
if (position[p->index][j] < position[p->index][i])
{
temp = position[p->index][i];
position[p->index][i] = position[p->index][j];
position[p->index][j] = temp;
temp = rateMultiplier[p->index][i];
rateMultiplier[p->index][i] = rateMultiplier[p->index][j];
rateMultiplier[p->index][j] = temp;
}
}
}
/* calculate prior and proposal ratio */
(*lnPriorRatio) = (*lnProposalRatio) = 0.0;
/* update branch evolution lengths */
if (UpdateCppEvolLengths (param, p, chain) == ERROR)
{
abortMove = YES;
return (NO_ERROR);
}
/* set update of cond likes down to root */
/* update of crowntree set in UpdateCppEvolLengths */
q = p->anc;
while (q->anc != NULL)
{
q->upDateCl = YES;
q = q->anc;
}
return (NO_ERROR);
MrBayesPrint ("%lf", *mvp); /* just because I am tired of seeing the unused parameter error msg */
}
int Move_CPPRate (Param *param, int chain, RandLong *seed, MrBFlt *lnPriorRatio, MrBFlt *lnProposalRatio, MrBFlt *mvp)
{
/* move the CPP rate (lambda) using multiplier */
int i, j, *nEvents, sumEvents;
MrBFlt oldLambda, newLambda, treeLength, tuning;
Model *mp;
TreeNode *p;
Tree *t;
/* get tuning parameter */
tuning = mvp[0];
/* get model params */
mp = &modelParams[param->relParts[0]];
/* get the CPP rate */
oldLambda = *GetParamVals (param, chain, state[chain]);
/* set new value */
newLambda = oldLambda * exp ((0.5 - RandomNumber(seed))*tuning);
/* reflect if necessary */
while (newLambda < CPPLAMBDA_MIN || newLambda > CPPLAMBDA_MAX)
{
if (newLambda < CPPLAMBDA_MIN)
newLambda = CPPLAMBDA_MIN * CPPLAMBDA_MIN / newLambda;
if (newLambda > CPPLAMBDA_MAX)
newLambda = CPPLAMBDA_MAX * CPPLAMBDA_MAX / newLambda;
}
/* store new value */
(*GetParamVals (param, chain, state[chain])) = newLambda;
/* calculate prior ratio */
(*lnPriorRatio) = 0.0;
for (i=0; i<param->nSubParams; i++)
{
nEvents = param->subParams[i]->nEvents[2*chain+state[chain]];
sumEvents = 0;
t = GetTree (param->subParams[i], chain, state[chain]);
treeLength = 0.0;
for (j=0; j<t->nNodes-2; j++)
{
p = t->allDownPass[j];
sumEvents += nEvents[p->index];
treeLength += p->length;
}
(*lnPriorRatio) += (oldLambda - newLambda) * treeLength;
(*lnPriorRatio) += sumEvents * log (newLambda / oldLambda);
}
/* adjust for prior on cppRate */
if (!strcmp(mp->cppRatePr,"Exponential"))
(*lnPriorRatio) += mp->cppRateExp * (oldLambda - newLambda);
/* calculate proposal ratio */
(*lnProposalRatio) = log (newLambda / oldLambda);
/* we do not need to update likelihoods */
for (i=0; i<param->nRelParts; i++)
{
modelSettings[param->relParts[i]].upDateCl = NO;
}
return (NO_ERROR);
}
int Move_CPPRateMultiplier_M (Param *param, int chain, RandLong *seed, MrBFlt *lnPriorRatio, MrBFlt *lnProposalRatio, MrBFlt *mvp)
{
/* move one CPP rate multiplier using multiplier */
int i, j, k, *nEvents;
MrBFlt newRateMultiplier, oldRateMultiplier, tuning, minM, maxM, sigma, **rateMultiplier;
TreeNode *p = NULL;
ModelInfo *m;
Tree *t;
TreeNode *q;
/* get the tuning parameter */
tuning = mvp[0];
/* get the model settings */
m = &modelSettings[param->relParts[0]];
/* get tree */
t = GetTree (param, chain, state[chain]);
/* get CPP event data */
nEvents = param->nEvents[2*chain+state[chain]];
rateMultiplier = param->rateMult[2*chain+state[chain]];
/* get minimum and maximum of CPP rate multiplier */
minM = param->min;
maxM = param->max;
/* pick a branch and a rateMultiplier */
for (i=j=0; i<t->nNodes - 2; i++)
{
p = t->allDownPass[i];
j += nEvents[p->index];
}
if (j == 0)
{
abortMove = YES;
return (NO_ERROR);
}
k = (int) (RandomNumber(seed) * j);
for (i=j=0; i<t->nNodes - 2; i++)
{
p = t->allDownPass[i];
j += nEvents[p->index];
if (j > k)
break;
}
/* find local index */
k = nEvents[p->index] - (j - k);
/* find new rateMultiplier */
oldRateMultiplier = rateMultiplier[p->index][k];
newRateMultiplier = oldRateMultiplier * (exp (0.5 - RandomNumber(seed) * tuning));
/* reflect if necessary */
while (newRateMultiplier < minM || newRateMultiplier > maxM)
{
if (newRateMultiplier < minM)