-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_t_NW.c
1158 lines (1063 loc) · 41.7 KB
/
update_t_NW.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
/* MigSelect 2013-2019 Arun Sethuraman, Vitor Sousa, Jody Hey */
/* This code was developed based on IMa2 2009-2010 Jody Hey, Rasmus Nielsen and Sang Chul Choi*/
#undef GLOBVARS
#include "imamp.h"
#include "update_gtree_common.h"
#include "update_gtree.h"
#include "updateassignment.h"
/* this file holds stuff for changet_NW()
This is a t update modeled on the way Nielsen and Wakeley (2001)did it as implemented in the midv program, by moving a split time up or down
and adding or erasing migration events.
check out mathematica file: newt_t_updating_8_21_08.nb
*/
/* declarded in update_gtree_common.h */
/* extern int holddownA[MAXLINKED]; not used here */
/* extern int medgedrop; not used here */
/* extern int mrootdrop; not used here */
/* extern int rootmove; not used here */
/* extern double lmedgedrop; not used here */
/* extern double lmrootdrop; not used here */
/* extern double holdsisdlikeA[MAXLINKED]; not used here */
/* extern struct genealogy_weights holdgweight_updategenealogy;
extern struct genealogy_weights holdallgweight_updategenealogy;
extern struct probcalc holdallpcalc_updategenealogy;
extern struct genealogy holdgtree; not used here */
/*********** local to this file ***************/
/* struct migrationinfo_tNW
holds info on each of the simulations that are done in the update
each element can hold info on one or two edges
a static array, large enough to hold the largest of genealogies in the data set,
is set up the first time through
*/
struct migrationinfo_tNW
{
int n; // 1 or 2 for # of edges
int edgeid[2]; // edge numbers
int upa[2]; // the population state at the top of (but just below) the split interval, after the update
int upb[2]; // the population state at the top of (but just below) the split interval, before the update
int da; // the population state just before the bottom of the split interval, after the update
int db; // the population state just before the bottom of the split interval, before the update
double uptime[2]; // time at top of edge
double mtime[2]; // time of edge that is in the interval between the new and old split times
int mcount[2]; // # of migrations in that time interval before the update
int mnew[2]; // # of migrations in that interval after the uptdate
int npopsb; // # of populations in period before
int npopsa; // # of populations in period after
int cm2_b[2]; // population state before the second to last migration before the update
int cm2_a[2]; // population state before the second to last migration after the update
double mrate[2]; // migration rate over that interval before udpate
double mrate_r[2]; // migration rate over that interval after udpate
double logpfpop; // probability of simulating da for the update
double logpfpop_r; // probability of simulating da for the reverse update
} *minfo;
static struct genealogy *holdallgtree_t_NW;
static int largestsamp;
static int ci;
static struct genealogy *G;
static double oldt, newt, tu, td;
static struct genealogy_weights holdallgweight_t_NW;
static struct genealogy_weights holdgweight_t_NW[MAXLOCI];
static struct probcalc holdallpcalc_t_NW;
// VS
// variables to save the gweights and pcalc for the groups of loci
// depends on variable MAXGROUPS
static struct genealogy_weights holdgweight_theta_t_NW[MAXGROUPS];
static struct genealogy_weights holdgweight_mig_t_NW[MAXGROUPS];
static struct probcalc holdpcalc_theta_t_NW[MAXGROUPS];
static struct probcalc holdpcalc_mig_t_NW[MAXGROUPS];
static int mforward, mreverse;
/******* local prototypes ********/
static void copy_all_gtree (int mode);
static int simmpath_tNW (int edge, struct edge *gtree, int numm, int lastm,
double timein, double upt, int period, int pop,
int constrainpop);
void initminfo(int j);
static void addmigration_NW(struct edge *gtree, int period, int numupdates);
static double getmprob_NW(int period, double mrate, double mtime, int mcount, int uppop, int dpop, int cm2pop, int numpops);
static double update_mig_tNW (int li, struct edge *gtree, int period);
// copy_all_gtree makes a copy of all genealogies in a given chain
// just like copy_gtree() but for all loci in a chain
// could set it up so it calls copy_gtree() but that function
// uses a local holdgtree variable - could change this to cut down on code
// if mode==1 copy *G into holdgtree if mode==0 copy holdgtree into *G
// used only for NW update
void
copy_all_gtree (int mode)
{
struct edge *togtree, *fromgtree;
struct genealogy *fromG, *toG;
int li, i, j, ai;
for (li = 0; li < nloci; li++)
{
if (mode)
{
toG = &holdallgtree_t_NW[li];
fromG = &C[ci]->G[li];
}
else
{
toG = &C[ci]->G[li];
fromG = &holdallgtree_t_NW[li];
}
togtree = toG->gtree;
fromgtree = fromG->gtree;
toG->length = fromG->length;
toG->mignum = fromG->mignum;
toG->root = fromG->root;
toG->roottime = fromG->roottime;
toG->tlength = fromG->tlength;
for (i = 0; i < L[li].numlines; i++)
{
togtree[i].down = fromgtree[i].down;
j = -1;
do
{
j++;
checkmig (j, &(togtree[i].mig), &(togtree[i].cmm));
togtree[i].mig[j] = fromgtree[i].mig[j];
}
while (fromgtree[i].mig[j].mt > -0.5);
togtree[i].time = fromgtree[i].time;
togtree[i].pop = fromgtree[i].pop;
togtree[i].up[0] = fromgtree[i].up[0];
togtree[i].up[1] = fromgtree[i].up[1];
if (L[li].model == STEPWISE || L[li].model == JOINT_IS_SW)
for (ai = (L[li].model == JOINT_IS_SW); ai < L[li].nlinked; ai++)
{
togtree[i].A[ai] = fromgtree[i].A[ai];
togtree[i].dlikeA[ai] = fromgtree[i].dlikeA[ai];
}
}
}
} /* copy_all_gtree */
/* simulate the migration path - very similar to simmpath() */
int
simmpath_tNW (int edge, struct edge *gtree, int numm, int lastm,
double timein, double upt, int period, int pop,
int constrainpop)
{
int i, lastpop, startm, lastpop_2;
/* CR 110715.1 */
int dupCheck; /* flag turns off dup migration time check of mig events */
int migIndex; /* index used to look for duplicate migration times */
assert (numm > 0);
startm = lastm + 1;
lastm = lastm + numm;
do
{
for (i = startm; i <= lastm; i++)
{
checkmig (i, &(gtree[edge].mig), &(gtree[edge].cmm));
gtree[edge].mig[i].mt = upt + uniform () * timein;
}
gtree[edge].mig[i].mt = -1;
dupCheck=0;
if (numm > 1)
{
hpsortmig (>ree[edge].mig[startm] - 1, numm);
/* look for duplicate migration times in sorted list.
* This solves a charateristic of the Mersennes Twister
* random number generator in which identical random numbers may be
* returned from the random number sequence in a very small number
* of calls. With some seeds it was noted as small as within 4 calls.
*/
for (migIndex = startm; migIndex < lastm; ++migIndex)
{
if ( gtree[edge].mig[migIndex].mt != gtree[edge].mig[migIndex + 1].mt )
{
continue;
}
else
{ /* if duplicate time found, a new migration path must be simulated */
dupCheck=1;
break;
}
}
}
else
{
dupCheck=0;
}
} while (dupCheck == 1); /* when no duplicate times found, exit loop */
lastpop = pop;
lastpop_2 = -1;
if (constrainpop < 0)
{
for (i = startm; i <= lastm; i++)
{
gtree[edge].mig[i].mp =
picktopop (lastpop, C[ci]->plist[period], npops - period);
lastpop = gtree[edge].mig[i].mp;
}
}
else
{
if (numm >= 2)
{
i = startm;
while (i < lastm - 1)
{
gtree[edge].mig[i].mp =
picktopop (lastpop, C[ci]->plist[period], npops - period);
lastpop = gtree[edge].mig[i].mp;
i++;
}
lastpop_2 = lastpop;
gtree[edge].mig[lastm - 1].mp =
picktopop2 (lastpop, C[ci]->plist[period], npops - period,
constrainpop);
gtree[edge].mig[lastm].mp = constrainpop;
}
else
{
if (numm == 1)
{
gtree[edge].mig[lastm].mp = constrainpop;
lastpop = constrainpop;
}
}
}
return lastpop_2;
} /* simmpath_tNW */
void addmigration_NW(struct edge *gtree, int period, int numupdates)
{
int i, j, k, ei, mi, mii;
int lastabovem, numskip, numheld;
struct migstruct holdmig[ABSMIGMAX];
for (j=0;j<numupdates;j++)
{
for (i=0;i<minfo[j].n;i++)
{
ei = minfo[j].edgeid[i];
if (ei != G->root)
{
/* determine position in mig array of the first migration event in the relevant period */
mi = 0;
if (minfo[j].uptime[i] < tu)
while (gtree[ei].mig[mi].mt > -0.5 && gtree[ei].mig[mi].mt < tu)
{
mi++;
}
lastabovem = mi - 1; // position to start simulating
/* count how many migrations must be erased for the update */
mii = mi;
while (gtree[ei].mig[mii].mt > -0.5 && gtree[ei].mig[mii].mt < td)
{
mii++;
}
numskip = mii - mi;
/* count and save the migrations after the relevant period */
if (gtree[ei].mig[mii].mt < 0)
{
numheld = 0;
}
else
{
k = 0;
while (gtree[ei].mig[mii].mt > -0.5)
{
holdmig[k] = gtree[ei].mig[mii];
mii++;
k++;
}
holdmig[k].mt = -1;
numheld = k;
}
if (newt < td && period == lastperiodnumber) // do not add migration
{
assert(minfo[j].mnew[i] == 0);
gtree[ei].mig[lastabovem + 1].mt = -1;
}
else
{
if (minfo[j].mnew[i] > 0)
{
minfo[j].cm2_a[i] =
simmpath_tNW (ei, gtree, minfo[j].mnew[i], lastabovem, minfo[j].mtime[i],
DMAX (tu, minfo[j].uptime[i]), period, minfo[j].upa[i],
minfo[j].da);
/* put back stored migration events */
if (numheld > 0)
{
mi = lastabovem + minfo[j].mnew[i] + 1;
for (k = 0; k < numheld; k++, mi++)
{
checkmig (mi, &(gtree[ei].mig), &(gtree[ei].cmm));
gtree[ei].mig[mi] = holdmig[k];
}
gtree[ei].mig[mi].mt = -1;
}
}
else
{
if (numskip > 0)
{
/* put back stored migration events */
if (numheld > 0)
{
for (mi = lastabovem + 1, k = 0; k < numheld; k++, mi++)
gtree[ei].mig[mi] = holdmig[k];
gtree[ei].mig[mi].mt = -1;
}
else
{
gtree[ei].mig[lastabovem + 1].mt = -1;
}
}
}
}
}
}
}
} // addmigration_NW
double
getmprob_NW(int period, double mrate, double mtime, int mcount, int uppop, int dpop, int cm2pop, int numpops)
{
double logb, logs, lognp;
if (period ==lastperiodnumber)
return 0;
if (period == lastperiodnumber - 1)
{
if (ODD(mcount))
return mcount * log(mrate/mtime) - mylogsinh (mrate);
else
return mcount * log(mrate/mtime) - mylogcosh (mrate);
}
// else period > lastperiodnumber - 1
assert(period < lastperiodnumber - 1);
if (uppop == dpop)
logs = log (1 - mrate * exp (-mrate));
else
logs = log (1 - exp (-mrate));
switch (mcount)
{
case 0 :
return -mrate - logs;
case 1 :
return log(mrate/mtime) - mrate - logs;
default :
{
assert(cm2pop >= 0);
assert(mcount > 0);
lognp = log(numpops-1);
if (cm2pop == dpop)
logb = - lognp;
else
logb = - log(numpops - 2);
return mcount * log(mrate/mtime) + (2- mcount)* lognp + logb - mrate - logs;
}
}
} //getmprob_NW
void initminfo(int j)
{
minfo[j].n = minfo[j].edgeid[0] = minfo[j].edgeid[1] = -1;
minfo[j].upa[0] = minfo[j].upa[1] = minfo[j].upb[0] = minfo[j].upb[1] = -1;
minfo[j].db = minfo[j].da = -1;
minfo[j].uptime[0] =minfo[j].uptime[1] = -1;
minfo[j].mtime[0] = minfo[j].mtime[1] = -1;
minfo[j].mcount[0] = minfo[j].mcount[1] = minfo[j].mnew[0] =minfo[j].mnew[1] = -1;
minfo[j].npopsb = minfo[j].npopsa = -1;
minfo[j].cm2_b[0] = minfo[j].cm2_b[1] = minfo[j].cm2_a[0] =minfo[j].cm2_a[1] = -1;
minfo[j].mrate[0] = minfo[j].mrate[1] = minfo[j].mrate_r[0] = minfo[j].mrate_r[1] = -1;
minfo[j].logpfpop = minfo[j].logpfpop_r = -1;
}
#define MIGSIMFRAC 0.999 /* fraction of updated edges with a final population the same as a starting population,
in cases when there are two possible final populations */
double
update_mig_tNW (int li, struct edge *gtree, int period)
{
double uptime; //, tu, td;
int i, j, k, sis;
double mproposedenom, mproposenum;
int period_a, period_b, periodp1;
int numupdates, ei,mi;
int mstart, checkup0, checkup1;
int setfpop[2*MAXGENES-1]; // contains the position in the minfo[] array in which the update information for an edge is contained
if (newt < oldt)
{
tu = newt;
td = oldt;
period_a = period + 1;
period_b = period;
}
else
{
tu = oldt;
td = newt;
period_a = period;
period_b = period + 1;
}
periodp1 = period+1;
for (i = 0; i < L[li].numlines; i++)
setfpop[i] = -1;
/*********** Setup minfo - indicate which branches need attention ***********/
// read about struct migrationinfo_tNW at the top of this file
for (i = 0, j=0; i < L[li].numlines; i++)
{
uptime = (i < L[li].numgenes) ? 0 : gtree[gtree[i].up[0]].time;
// identify edges that need updating
// identify the population state of the bottoms of edges before and after the update
if ((gtree[i].time > tu && uptime <= td) /*&& i != G->root */)
{
if (setfpop[i] == -1)
{
initminfo(j);
minfo[j].uptime[0] = uptime;
/* set the population states at the bottom of the time interval, db and da */
/* set the pfpop (logpfpop and logpfpop_r) terms */
if (gtree[i].time > td) // bottom of edge crosses lower time, so only a single edge is done
{
setfpop[i] = j;
minfo[j].n = 1;
minfo[j].edgeid[0] = i;
minfo[j].db = nowedgepop (ci, >ree[i], td);
if (oldt < newt)
{
if (minfo[j].db == C[ci]->addpop[periodp1])
{
/* minfo[j].logpfpop = -LOG2;
minfo[j].logpfpop_r = 0.0;
minfo[j].da = C[ci]->droppops[periodp1][bitran()];
// see what upa is, */
minfo[j].logpfpop_r = 0.0;
checkup0 = nowedgepop (ci, >ree[i], tu);
if (uptime < tu && (checkup0 == C[ci]->droppops[periodp1][0] || checkup0 == C[ci]->droppops[periodp1][1])) // then we know what upa will be and can use it to simulate db;
{
if (uniform() < MIGSIMFRAC)
{
minfo[j].da = checkup0;
minfo[j].logpfpop = log(MIGSIMFRAC);
}
else
{
if (checkup0 == C[ci]->droppops[periodp1][0])
minfo[j].da = C[ci]->droppops[periodp1][1];
else
minfo[j].da = C[ci]->droppops[periodp1][0];
minfo[j].logpfpop = log(1.0 - MIGSIMFRAC);
}
}
else
{
minfo[j].logpfpop = -LOG2;
minfo[j].da = C[ci]->droppops[periodp1][bitran()];
}
}
else
{
minfo[j].da = minfo[j].db;
minfo[j].logpfpop = minfo[j].logpfpop_r = 0.0;
}
}
else //oldt > newt
{
if (minfo[j].db == C[ci]->droppops[periodp1][0] ||minfo[j].db == C[ci]->droppops[periodp1][1])
{
minfo[j].logpfpop = 0.0;
minfo[j].da = C[ci]->addpop[periodp1];
checkup0 = nowedgepop (ci, >ree[i], tu);
if (uptime < tu && (checkup0 == C[ci]->droppops[periodp1][0] || checkup0 == C[ci]->droppops[periodp1][1])) // then we know what upb will be and can use when imagining simulating db;
{
if (checkup0 == minfo[j].db)
minfo[j].logpfpop_r = log(MIGSIMFRAC);
else
minfo[j].logpfpop_r = log(1.0 - MIGSIMFRAC);
}
else
{
minfo[j].logpfpop_r = -LOG2;
}
}
else
{
minfo[j].da = minfo[j].db;
minfo[j].logpfpop = minfo[j].logpfpop_r = 0.0;
}
}
}
else // gtree[i].time <= td two edges need to be done
{
setfpop[i] = j;
if (i== gtree[gtree[i].down].up[0])
sis = gtree[gtree[i].down].up[1];
else
sis = gtree[gtree[i].down].up[0];
minfo[j].uptime[1] = (sis < L[li].numgenes) ? 0 : gtree[gtree[sis].up[0]].time;
setfpop[sis] = j;
minfo[j].n = 2;
minfo[j].edgeid[0] = i;
minfo[j].edgeid[1] = sis;
minfo[j].db = nowedgepop (ci, >ree[i], gtree[i].time);
if (oldt < newt)
{
minfo[j].logpfpop_r = 0.0;
if (minfo[j].db == C[ci]->addpop[periodp1])
{
checkup0 = nowedgepop (ci, >ree[i], tu);
checkup1 = nowedgepop (ci, >ree[sis], tu);
if (minfo[j].uptime[0] < tu && minfo[j].uptime[1] < tu && checkup0 == checkup1 &&
(checkup0 == C[ci]->droppops[periodp1][0] || checkup0 == C[ci]->droppops[periodp1][1]))
{
if (uniform() < MIGSIMFRAC)
{
minfo[j].da = checkup0;
minfo[j].logpfpop = log(MIGSIMFRAC)/2.0;
}
else
{
if (checkup0 == C[ci]->droppops[periodp1][0])
minfo[j].da = C[ci]->droppops[periodp1][1];
else
minfo[j].da = C[ci]->droppops[periodp1][0];
minfo[j].logpfpop = log(1.0 - MIGSIMFRAC)/2.0;
}
}
else
{
minfo[j].logpfpop = - LOG2HALF;
minfo[j].da = C[ci]->droppops[periodp1][bitran()];
}
}
else
{
minfo[j].da = minfo[j].db;
minfo[j].logpfpop = 0.0;
}
}
else //newt < oldt
{
minfo[j].logpfpop = 0.0;
if (minfo[j].db == C[ci]->droppops[periodp1][0] ||minfo[j].db == C[ci]->droppops[periodp1][1])
{
checkup0 = nowedgepop (ci, >ree[i], tu);
checkup1 = nowedgepop (ci, >ree[sis], tu);
minfo[j].da = C[ci]->addpop[periodp1];
if (minfo[j].uptime[0] < tu && minfo[j].uptime[1] < tu && checkup0 == checkup1 &&
(checkup0 == C[ci]->droppops[periodp1][0] || checkup0 == C[ci]->droppops[periodp1][1]))
{
if (checkup0 == minfo[j].db)
minfo[j].logpfpop_r = log(MIGSIMFRAC)/2.0;
else
minfo[j].logpfpop_r = log(1.0 - MIGSIMFRAC)/2.0;
}
else
{
minfo[j].logpfpop_r = -LOG2HALF;
}
}
else
{
minfo[j].da = minfo[j].db;
minfo[j].logpfpop_r = 0.0;
}
}
}
j++;
}
}
}
numupdates = j;
for (j=0;j<numupdates;j++)
{
// identify population states at the top of the edge within the interval, before and after the update
for (i=0;i<minfo[j].n;i++)
{
ei = minfo[j].edgeid[i];
if (newt < oldt)
{
if (minfo[j].uptime[i] < tu)
{
minfo[j].upb[i] = nowedgepop (ci, >ree[ei], tu);
if (minfo[j].upb[i] == C[ci]->droppops[periodp1][0] ||minfo[j].upb[i] == C[ci]->droppops[periodp1][1])
minfo[j].upa[i] = C[ci]->addpop[periodp1];
else
minfo[j].upa[i] = minfo[j].upb[i];
}
else // uptime falls in the interval
{
minfo[j].upb[i] = minfo[setfpop[gtree[ei].up[0]]].db;
assert(minfo[j].upb[i] == nowedgepop (ci, >ree[ei], minfo[j].uptime[i]));
minfo[j].upa[i] = minfo[setfpop[gtree[ei].up[0]]].da;
gtree[ei].pop = minfo[j].upa[i];
}
}
else // (newt > oldt)
{
if (minfo[j].uptime[i] < tu)
{
minfo[j].upb[i] = nowedgepop (ci, >ree[ei], tu * (1 + DBL_EPSILON)); // just below tu
if (minfo[j].upb[i] == C[ci]->addpop[periodp1])
minfo[j].upa[i] = nowedgepop (ci, >ree[ei], tu); // just at tu
else
minfo[j].upa[i] = minfo[j].upb[i];
}
else // uptime falls in the interval
{
minfo[j].upb[i] = minfo[setfpop[gtree[ei].up[0]]].db;
assert(minfo[j].upb[i] == nowedgepop (ci, >ree[ei], minfo[j].uptime[i]));
minfo[j].upa[i] = minfo[setfpop[gtree[ei].up[0]]].da;
gtree[ei].pop = minfo[j].upa[i];
}
}
}
}
/* determine migration counts and rates */
for (j=0;j<numupdates;j++)
{
minfo[j].npopsb = npops - period_b;
minfo[j].npopsa = npops - period_a;
for (i=0;i<minfo[j].n;i++)
{
ei = minfo[j].edgeid[i];
if (ei != G->root)
{
minfo[j].mtime[i] = DMIN (td, gtree[ei].time) - DMAX (tu, minfo[j].uptime[i]);
assert(minfo[j].mtime[i] > 0);
/* determine mcount */
mi = 0;
k = 0;
mstart = -1;
while (gtree[ei].mig[k].mt > -0.5 && gtree[ei].mig[k].mt < td)
{
if (gtree[ei].mig[k].mt > tu)
{
if (mi==0)
mstart = k;
mi++;
}
k++;
}
if (k >= 2 && mstart >= 0 && (k- mstart >= 2))
{
if (k==2)
minfo[j].cm2_b[i] = minfo[j].upb[i];
else
minfo[j].cm2_b[i] = gtree[ei].mig[k-3].mp;
}
else
minfo[j].cm2_b[i] = -1;
assert(mi >= 0);
minfo[j].mcount[i] = mi;
assert(period_b < lastperiodnumber || mi==0);
/* set mrate */
if (period_a < lastperiodnumber)
{
minfo[j].mrate[i] = calcmrate(minfo[j].mcount[i],minfo[j].mtime[i])*minfo[j].mtime[i];
}
else
{
minfo[j].mrate[i] = 0;
}
/* determine mnew */
if (minfo[j].npopsa == 1)
minfo[j].mnew[i] = 0;
else
{
if (minfo[j].npopsa == 2)
{
if (minfo[j].upa[i] == minfo[j].da)
minfo[j].mnew[i] = poisson (minfo[j].mrate[i],0);
else
minfo[j].mnew[i] = poisson (minfo[j].mrate[i],1);
}
else
{
if (minfo[j].upa[i] == minfo[j].da)
minfo[j].mnew[i] = poisson (minfo[j].mrate[i],3);
else
minfo[j].mnew[i] = poisson (minfo[j].mrate[i],2);
}
}
/* set the reverse update rate */
if (period_b < lastperiodnumber)
{
minfo[j].mrate_r[i] = calcmrate(minfo[j].mnew[i],minfo[j].mtime[i])*minfo[j].mtime[i];
}
else
{
minfo[j].mrate_r[i] = 0.0;
}
mforward += minfo[j].mnew[i];
mreverse += minfo[j].mcount[i];
}
}
}
/* simulate migration events */
addmigration_NW(gtree, period_a, numupdates);
/* calculate the Hastings term associated with the migration events before and after the update */
mproposedenom = mproposenum = 0;
for (j=0;j<numupdates;j++)
{
for (i=0;i<minfo[j].n;i++)
{
assert(minfo[j].mrate[i]!=0 || minfo[j].logpfpop == 0);
if (minfo[j].mrate[i] > 0)
{
mproposedenom += (minfo[j].logpfpop +
getmprob_NW(period_a,minfo[j].mrate[i],minfo[j].mtime[i],minfo[j].mnew[i],minfo[j].upa[i],minfo[j].da, minfo[j].cm2_a[i], minfo[j].npopsa) );
}
assert(minfo[j].mrate_r[i]!=0 || minfo[j].logpfpop_r == 0);
if (minfo[j].mrate_r[i] > 0)
{
mproposenum += (minfo[j].logpfpop_r +
getmprob_NW(period_b,minfo[j].mrate_r[i],minfo[j].mtime[i],minfo[j].mcount[i],minfo[j].upb[i],minfo[j].db, minfo[j].cm2_b[i], minfo[j].npopsb) );
}
}
}
/* debug - check minfo */
/*
for (j=0;j<numupdates;j++)
{
assert(minfo[j].da >= 0 && minfo[j].da <= numtreepops);
assert(minfo[j].db >= 0 && minfo[j].db <= numtreepops);
for (i=0;i<minfo[j].n;i++)
{
ei =minfo[j].edgeid[i];
//assert(ei != G->root);
assert(minfo[j].upa[i] >= 0 && minfo[j].upa[i] <= numtreepops);
assert(minfo[j].upb[i] >= 0 && minfo[j].upb[i] <= numtreepops);
if (gtree[ei].time < td)
{
if (C[ci]->G[li].root != gtree[ei].down)
{
if (gtree[ei].down == minfo[setfpop[gtree[ei].down]].edgeid[0])
{
assert(minfo[j].db == minfo[setfpop[gtree[ei].down]].upb[0]);
assert(minfo[j].da == minfo[setfpop[gtree[ei].down]].upa[0]);
}
else
{
assert(minfo[j].db == minfo[setfpop[gtree[ei].down]].upb[1]);
assert(minfo[j].da == minfo[setfpop[gtree[ei].down]].upa[1]);
}
}
}
uptime = (ei < L[li].numgenes) ? 0 : gtree[gtree[ei].up[0]].time;
if (uptime > tu)
{
assert(minfo[j].upb[i] == minfo[setfpop[gtree[ei].up[0]]].db);
assert(minfo[j].upb[i] == minfo[setfpop[gtree[ei].up[1]]].db);
assert(minfo[j].upa[i] == minfo[setfpop[gtree[ei].up[0]]].da);
assert(minfo[j].upa[i] == minfo[setfpop[gtree[ei].up[1]]].da);
}
}
} */
return mproposenum - mproposedenom;
} /* update_mig_tNW */
/**************************************/
/* GLOBAL FUNCTIONS in update_t_NW.c */
/**************************************/
// VS
// function changed to initialized the gweights for groups of loci
void
init_t_NW (void)
{
int li, j;
int gp; // VS
init_genealogy_weights (&holdallgweight_t_NW);
/* CR:110331.1
* Incorrect structure used when allocating memory to a genealogy pointer.
*/
holdallgtree_t_NW = calloc ((size_t) nloci, sizeof (struct genealogy));
for (li = 0; li < nloci; li++)
{
init_genealogy_weights (&holdgweight_t_NW[li]);
init_holdgtree (&holdallgtree_t_NW[li], L[li].numgenes);
}
init_probcalc (&holdallpcalc_t_NW);
for (largestsamp = 0, j = 0; j < nloci; j++)
if (largestsamp < L[j].numlines)
largestsamp = L[j].numlines;
minfo = malloc ((2 * largestsamp - 1) * sizeof (struct migrationinfo_tNW));
// VS
// initialize the holdgweight and holdpcalc for each group of loci
for(gp=0; gp<nbgroupsloci_theta; gp++) {
init_genealogy_weights(&holdgweight_theta_t_NW[gp]);
init_probcalc (&holdpcalc_theta_t_NW[gp]);
}
for(gp=0; gp<nbgroupsloci_mig; gp++) {
init_genealogy_weights(&holdgweight_mig_t_NW[gp]);
init_probcalc (&holdpcalc_mig_t_NW[gp]);
}
} /* init_t_NW */
// VS
// changed to free the arrays with gweights and pcalc for groups of loci
void
free_t_NW (void)
{
int li;
int gp; // VS
free_genealogy_weights (&holdallgweight_t_NW);
for (li = 0; li < nloci; li++)
{
free_genealogy_weights (&holdgweight_t_NW[li]);
free_holdgtree (&holdallgtree_t_NW[li], L[li].numgenes);
}
XFREE (holdallgtree_t_NW);
free_probcalc (&holdallpcalc_t_NW);
XFREE (minfo);
// XFREE (NW_t_upinf);
// VS
// free holdgweight and holdpcalc for each group of loci
for(gp=0; gp<nbgroupsloci_theta; gp++) {
free_genealogy_weights(&holdgweight_theta_t_NW[gp]);
free_probcalc (&holdpcalc_theta_t_NW[gp]);
}
for(gp=0; gp<nbgroupsloci_mig; gp++) {
free_genealogy_weights(&holdgweight_mig_t_NW[gp]);
free_probcalc (&holdpcalc_mig_t_NW[gp]);
}
} /* init_t_NW */
/* changet_NW
this is modelled on the update of t in Nielsen and Wakeley (2001)
does nothing whatsover to branch lengths so no change in pdg
This update applies to all genealogies in the chain
*/
/* The update is done to the actual genealogies
Could improve speed by doing the update to a copy of the genealogies,
This is because most updates are rejected, so if the copy is being changed and it is rejected
then there would be no need to restore it
to make this change would have to change a few lines of code (below)
also have to pass treeweight a genealogy pointer and not just the genealogy number
there will no doubt be other stuff
spent a couple hours trying this on 8/28/08, but wasn't working so gave up. maybe try later
Current Setup - work on the original:
--------------------------------------
copy_all_gtree(1); - copy each C[ci]->G to holdallgtree_t_NW
copy_treeinfo (&holdallgweight_t_NW, &C[ci]->allgweight); - copy allgweight to holdallgweight_t_NW - then empty allgweight
copy_probcalc (&holdallpcalc_t_NW, &C[ci]->allpcalc); - copy allpcalc to holdallpcalc_t_NW (stuff for integrating)
setzero_genealogy_weights (&C[ci]->allgweight); - empty allgweight
going thru the loop:
copy_treeinfo (&holdgweight_t_NW[li], &G->gweight); - copy gweight to holdgweight_t_NW for each locus
update C[ci]->G
setzero_genealogy_weights (&G->gweight); - set gweight to zero
treeweight (ci, li); - calculate weights
sum_treeinfo (&C[ci]->allgweight, &G->gweight); - sum allgweight
integrate_tree_prob (ci, &C[ci]->allgweight, &C[ci]->allpcalc); - integrate and reset allpcalc
if accept,
everything is already in gweight, allgweight and C[ci]->G
if reject:
copy_all_gtree(0); - copy holdallgtree_t_NW back to each C[ci]->G
copy_treeinfo (&C[ci]->allgweight, &holdallgweight_t_NW); - copy holdallgweight_t_NW back to allgweight
copy_probcalc (&C[ci]->allpcalc, &holdallpcalc_t_NW); - copy holdallpcalc_t_NW back to allpcalc
for (li = 0; li < nloci; li++) - loop thru loci
{
copy_treeinfo (&C[ci]->G[li].gweight, &holdgweight_t_NW[li]); - copy each locus's from holdgweight_t_NW back to gweight
}
If we reverse it :
------------------------------------
copy_all_gtree(1); - copy each C[ci]->G to holdallgtree_t_NW
setzero_genealogy_weights (&holdallgweight_t_NW); - empty holdallgweight_t_NW
going thru the loop:
update holdgallgtree[li]
setzero_genealogy_weights (&holdgweight_t_NW[li]); - set holdgweight_t_NW to zero
local_treeweight (ci, li, holdallgtree_t_NW[li]); - calculate weights
sum_treeinfo (&holdallgweight_t_NW, &holdgweight_t_NW[li]); - sum allgweight
integrate_tree_prob (ci, &holdallgweight_t_NW, &holdallpcalc_t_NW); - integrate and reset allpcalc
if accept,
copy_all_gtree(0); - copy holdallgtree_t_NW to each C[ci]->G
copy_treeinfo (&C[ci]->allgweight, &holdallgweight_t_NW); - copy holdallgweight_t_NW into allgweight
copy_probcalc (&C[ci]->allpcalc, &holdallpcalc_t_NW); - copy holdallpcalc_t_NW to allpcalc
for (li = 0; li < nloci; li++) - loop thru loci
{
copy_treeinfo (&C[ci]->G[li].gweight, &holdgweight_t_NW[li]); - copy each locus's from holdgweight_t_NW back to gweight
}
if reject:
don't do anything because nothing in C[ci]->G, allgweight or allpcalc was changed.
*/
/* let u refer to the more recent time and d to the older time */
/* 9/25/08 updated this
revised genealogy updating so that the current migration rate is based on the current number of migration events and the
current length of the branch that is being updated
reasoned that this might work better than using the rate that occurs for the entitre genealogy - e.g. help to avoid promoting
correlations and improve mixing */
/* only works for nonzero migration priors */
int
changet_NW (int chain, int timeperiod)
{
// VS
int gp, gp_theta, gp_mig; // VS
double sum_probg_gp=0.0; // VS
double metropolishastingsterm, tpw; //newt, oldt, tpw;
int li, i;
double U;
struct edge *gtree;
double t_d, t_u, t_u_prior, t_d_prior;
double migweight;
double holdt[MAXPERIODS];
if (assignmentoptions[POPULATIONASSIGNMENTCHECKPOINT] == 1)
{
assertgenealogy (chain);
}
mforward = mreverse = 0;
ci = chain;
/* select a new time */
t_u = (timeperiod == 0) ? 0 : C[ci]->tvals[timeperiod - 1];
t_d = (timeperiod == (lastperiodnumber - 1)) ? TIMEMAX : C[ci]->tvals[timeperiod + 1];
t_d_prior = DMIN (T[timeperiod].pr.max, t_d);
t_u_prior = DMAX (T[timeperiod].pr.min, t_u);
oldt = C[ci]->tvals[timeperiod];
newt = getnewt (timeperiod, t_u_prior, t_d_prior, oldt, 0);
assert (newt < T[timeperiod].pr.max);
/* store stuff and prepare for adding to storing allgweight */
for (i = 0; i < lastperiodnumber; i++)
holdt[i] = C[ci]->tvals[i];
copy_all_gtree (1);
copy_treeinfo (&holdallgweight_t_NW, &C[ci]->allgweight);
copy_probcalc (&holdallpcalc_t_NW, &C[ci]->allpcalc);
setzero_genealogy_weights (&C[ci]->allgweight);
// VS
// copy all the gweights and pcalc of groups of loci
// 1. save gweights and pcalc for groups of loci
// 2. set the gweights to zero
for(gp=0; gp<nbgroupsloci_theta; gp++) {
// 1.1. save gweights for theta
copy_treeinfo (&holdgweight_theta_t_NW[gp], &C[ci]->groupgweight_theta[gp]);
// check gweights for theta
check_gweight_vs (&holdgweight_theta_t_NW[gp], 1);
// 1.2. save pcalc for groups of loci for theta