-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhypertraps.c
More file actions
2141 lines (1905 loc) · 63.4 KB
/
hypertraps.c
File metadata and controls
2141 lines (1905 loc) · 63.4 KB
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
// this is the workhorse code for the HyperTraPS-CT algorithm
// it takes command line arguments that dictate the data file(s), the structure of the inference run, and various parameters
// the output file is a set of samples from the posterior distribution inferred over hypercube parameters
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>
#ifdef _WIN32
#define RNDSEED(x) srand(x)
#define RND ((double)rand() / RAND_MAX)
#else
#define RNDSEED(x) srand48(x)
#define RND drand48()
#endif
// lazy constants (just for memory allocation) -- consider increasing if memory errors are coming up
#define _MAXN 20000 // maximum number of datapoints
#define _MAXF 1000 // maximum filename length
#define _MAXS 1000 // maximum string length for various labels
#define _MAXFEATS 1000 // maximum number of features
#define _MAXDATA 1e7 // maximum number of bits in the dataset
// maximum continuous-time value above which results are truncated
#define MAXCT 1000
// just used in assigning ordinal labels to different features
#define FLEN 100
// number of trajectories N_h, and frequencies of sampling for posteriors and for output
int BANK = 200;
int NTRAJ = 100;
int NSAMP = 10;
int TMODULE = 100;
// do we want to output every iteration (MCMC sample etc?)
int _EVERYITERATION = 0;
// (log)likelihood scaling -- usually won't be needed
double lscale = 1;
// tracking scale of numerical error
double num_error = 0;
// control output
int VERBOSE = 0;
int SPECTRUM_VERBOSE = 0;
int SUPERVERBOSE = 0;
int APM_VERBOSE = 0;
int POST_VERBOSE = 1;
// calling exit() from code within R crashes the environment; this is a more robust approach
void myexit(int code)
{
#ifndef _USE_CODE_FOR_R
exit(0);
#else
Rcpp::stop("exiting");
#endif
}
// impose limits on integer val to be between lo and hi
void limiti(int *val, int lo, int hi)
{
if(*val < lo) *val = lo;
if(*val > hi) *val = hi;
}
// impose limits on double val to be between lo and hi
void limitf(double *val, int lo, int hi)
{
if(*val < lo) *val = lo;
if(*val > hi) *val = hi;
}
// produce gaussian random number
double gsl_ran_gaussian(const double sigma)
{
double x, y, r2;
do
{
/* choose x,y in uniform square (-1,-1) to (+1,+1) */
x = -1 + 2 * RND;
y = -1 + 2 * RND;
/* see if it is in the unit circle */
r2 = x * x + y * y;
}
while (r2 > 1.0 || r2 == 0);
/* Box-Muller transform */
return sigma * y * sqrt (-2.0 * log (r2) / r2);
}
// quick 2^n
int mypow2(int r)
{
int s = 1;
int i;
for(i = 1; i <= r; i++)
s *= 2;
return s;
}
// convert a binary string of length LEN to an integer
int BinToDec(int *state, int LEN)
{
int v = 1;
int i;
int val = 0;
for(i = LEN-1; i >= 0; i--)
{
val += state[i]*v;
v *= 2;
}
return val;
}
// return the number of parameters of a model structure (not all these are independent; see paper)
int nparams(int model, int LEN)
{
switch(model)
{
case 0: return 0;
case 1: return LEN;
case 2: return LEN*LEN;
case 3: return LEN*LEN*LEN;
case 4: return LEN*LEN*LEN*LEN;
case -1: return mypow2(LEN)*LEN;
default: return 0;
}
}
// given an array of parameters (ntrans) and a model structure (model) for a number of features (LEN)
// return the weight of the edge that corresponds to changing feature "locus" from state "state"
double RetrieveEdge(int *state, int locus, double *ntrans, int LEN, int model)
{
double rate;
int i, j, k;
if(model == 0)
rate = 0;
if(model == 1) // pi[locus] = rate of locus
rate = ntrans[locus];
if(model == 2) // pi[i*LEN + locus] = influence of i on locus
{
rate = ntrans[locus*LEN+locus];
for(i = 0; i < LEN; i++)
rate += state[i]*ntrans[i*LEN+locus];
}
if(model == 3) // pi[j*LEN*LEN + i*LEN + locus] = influence of ij on locus, j>=i (j==i is influence of i on locus)
{
rate = ntrans[locus*LEN*LEN+locus*LEN+locus];
for(i = 0; i < LEN; i++)
{
for(j = i; j < LEN; j++)
{
rate += state[i]*state[j]*ntrans[j*LEN*LEN+i*LEN+locus];
}
}
}
if(model == 4) // pi[k*LEN*LEN*LEN + j*LEN*LEN + i*LEN + i] = influence of ijk on i, k>=j>=i (j==i is influence of ik, k==j==i is influence of i); what does k==j,i mean
{
rate = ntrans[locus*LEN*LEN*LEN+locus*LEN*LEN+locus*LEN+locus];
for(i = 0; i < LEN; i++)
{
for(j = i; j < LEN; j++)
{
for(k = j; k < LEN; k++)
{
rate += state[i]*state[j]*state[k]*ntrans[k*LEN*LEN*LEN+j*LEN*LEN+i*LEN+locus];
}
}
}
}
if(model == -1)
{
rate = ntrans[BinToDec(state, LEN)*LEN+locus];
}
return exp(rate);
}
// redundancy in these params for model 4:
// 111, 112, 113, 121, 122, 123, 131, 132, 133, 211, 212, 213, 221, 222, 223, 231, 232, 233, 311, 312, 313, 321, 322, 323, 331, 332, 333
// 111, 112, 113, 122, 123, 133, 222, 223, 233, 333
// 1 12 13 12 123 13 2 23 23 3
// set up a "null" (or random) initial parameter set for a given model
void InitialMatrix(double *trans, int len, int model, int userandom)
{
int NVAL;
int i;
NVAL = nparams(model, len);
for(i = 0; i < NVAL; i++)
trans[i] = (userandom ? RND : 0);
for(i = 0; i < len; i++)
{
switch(model)
{
case 1: trans[i] = 1; break;
case 2: trans[i*len+i] = 1; break;
case 3: trans[i*len*len + i*len + i] = 1; break;
case 4: trans[i*len*len*len + i*len*len + i*len + i] = 1; break;
}
}
}
// read a set of parameters from a file (e.g. for a previous inference run)
void ReadMatrix(double *trans, int len, int model, char *fname)
{
int NVAL;
int i;
FILE *fp;
int tmp;
fp = fopen(fname, "r");
if(fp == NULL)
{
printf("Couldn't find parameter file %s\n", fname);
myexit(0);
}
NVAL = nparams(model, len);
for(i = 0; i < NVAL; i++)
{
if(feof(fp))
{
printf("Couldn't find sufficient parameters in file %s\n", fname);
myexit(0);
}
tmp = fscanf(fp, "%lf", &(trans[i]));
}
fclose(fp);
}
// output detailed information from a fitted model (parameters in "ntrans"): state probabilities and individual transition rates for edges
// won't be feasible for larger numbers of features
void OutputStatesTrans(char *label, double *ntrans, int LEN, int model)
{
int i, j, k, a;
int statedec;
int src, dest;
int state[LEN];
double rate, totrate;
int *active, *newactive;
double *probs;
int nactive, newnactive;
int level;
int found;
char statefile[300], transfile[300];
FILE *fp;
// prepare output files
sprintf(transfile, "%s-trans.csv", label);
sprintf(statefile, "%s-states.csv", label);
fp = fopen(transfile, "w");
fprintf(fp, "From,To,Probability,Flux\n");
// allocate and initialise data structures
probs = (double*)malloc(sizeof(double)*mypow2(LEN));
active = (int*)malloc(sizeof(int)*mypow2(LEN));
newactive = (int*)malloc(sizeof(int)*mypow2(LEN));
// "probs" will store state probabilities; level the "level" of the hypercube we're currently at
// "active" tracks which paths are currently under active calculation
for(i = 0; i < mypow2(LEN); i++)
probs[i] = 0;
level = 0;
// start with probability in 0^L and a single active path
probs[0] = 1;
active[0] = 0;
nactive = 1;
// while we haven't crossed the whole cube
while(nactive > 0)
{
newnactive = 0;
// go through active paths
for(a = 0; a < nactive; a++)
{
// pull the state of this active path
src = active[a];
statedec = src;
for(j = LEN-1; j >= 0; j--)
{
if(statedec >= mypow2(j))
{
state[LEN-1-j] = 1;
statedec -= mypow2(j);
}
else
state[LEN-1-j] = 0;
}
// pull the transitions from this state
totrate = 0;
for(j = 0; j < LEN; j++)
{
/* ntrans must be the transition matrix. ntrans[i+i*LEN] is the bare rate for i. then ntrans[j*LEN+i] is the modifier for i from j*/
if(state[j] == 0)
{
rate = RetrieveEdge(state, j, ntrans, LEN, model);
totrate += rate;
}
}
// go through each outgoing edge, outputting its transition rate (and the probability flux at this point)
// and spawning a new active path if the destination node doesn't already have one
for(j = 0; j < LEN; j++)
{
/* ntrans must be the transition matrix. ntrans[i+i*LEN] is the bare rate for i. then ntrans[j*LEN+i] is the modifier for i from j*/
if(state[j] == 0)
{
dest = src+mypow2(LEN-1-j);
rate = RetrieveEdge(state, j, ntrans, LEN, model);
probs[dest] += probs[src] * rate/totrate;
fprintf(fp, "%i,%i,%e,%e\n", src, dest, rate/totrate, probs[src]*rate/totrate);
found = 0;
for(k = 0; k < newnactive; k++)
{
if(newactive[k] == dest) { found = 1; break; }
}
if(found == 0)
newactive[newnactive++] = dest;
}
}
}
// update the list of active paths
for(a = 0; a < newnactive; a++)
active[a] = newactive[a];
nactive = newnactive;
level++;
}
fclose(fp);
// output state probabilities
fp = fopen(statefile, "w");
fprintf(fp, "State,Probability\n");
for(dest = 0; dest < mypow2(LEN); dest++)
{
fprintf(fp, "%i,%e\n", dest, probs[dest]);
}
fclose(fp);
free(active);
free(newactive);
free(probs);
}
// pick a new locus to change in state "state"; return it in "locus" and keep track of the on-course probability in "prob". "ntrans" is the transition matrix
void PickLocus(int *state, double *ntrans, int *targ, int *locus, double *prob, double *beta, int LEN, int model)
{
int i;
double rate[LEN];
double totrate, nobiastotrate;
double cumsum[LEN];
double r;
nobiastotrate = 0;
/* compute the rate of loss of gene i given the current genome -- without bias */
for(i = 0; i < LEN; i++)
{
/* ntrans must be the transition matrix. ntrans[i+i*LEN] is the bare rate for i. then ntrans[j*LEN+i] is the modifier for i from j*/
if(state[i] == 0)
{
rate[i] = RetrieveEdge(state, i, ntrans, LEN, model);
}
else /* we've already lost this gene */
rate[i] = 0;
/* roulette wheel calculations as normal */
cumsum[i] = (i == 0 ? 0 : rate[i-1]+cumsum[i-1]);
nobiastotrate += rate[i];
}
totrate = 0;
/* compute the rate of loss of gene i given the current genome -- with bias */
for(i = 0; i < LEN; i++)
{
/* ntrans must be the transition matrix. ntrans[i+i*LEN] is the bare rate for i. then ntrans[j*LEN+i] is the modifier for i from j*/
if(state[i] == 0 && targ[i] != 0)
{
rate[i] = RetrieveEdge(state, i, ntrans, LEN, model);
}
else /* we've already lost this gene OR WE DON'T WANT IT*/
rate[i] = 0;
/* roulette wheel calculations as normal */
cumsum[i] = (i == 0 ? 0 : rate[i-1]+cumsum[i-1]);
totrate += rate[i];
}
/* normalised, additive rates -- is this sensible? */
for(i = 0; i < LEN; i++)
cumsum[i] /= totrate;
r = RND;
for(i = 0; i < LEN-1; i++)
{
if(cumsum[i] < r && cumsum[i+1] > r) { break; }
}
*locus = i;
*prob = totrate/nobiastotrate;
*beta = nobiastotrate;
}
// compute PLI probability of a transition from "startpos" to "targ" given transition matrix "P"
double LikelihoodMultiplePLI(int *targ, double *P, int LEN, int *startpos, double tau1, double tau2, int model)
{
int *bank;
int n0, n1;
double *reject;
int i, j, r;
int locus;
int attempt[LEN];
double mean;
double *prodreject;
int fail;
int *hitss, *hitsd, *mins, *mind;
double totalsum;
int endtarg[LEN];
double lik;
// new variables
double *recbeta;
// nobiastotrate is retain to match role in PickLocus but basically corresponds to -u
// allocate memory for BANK (N_h) trajectories
bank = (int*)malloc(sizeof(int)*LEN*BANK);
reject = (double*)malloc(sizeof(double)*BANK);
hitss = (int*)malloc(sizeof(int)*BANK);
hitsd = (int*)malloc(sizeof(int)*BANK);
mins = (int*)malloc(sizeof(int)*BANK);
mind = (int*)malloc(sizeof(int)*BANK);
prodreject = (double*)malloc(sizeof(double)*BANK);
recbeta = (double*)malloc(sizeof(double)*LEN*BANK);
// initialise each trajectory at 0^L
for(i = 0; i < LEN*BANK; i++)
bank[i] = 0;
n0 = 0;
for(i = 0; i < LEN; i++)
endtarg[i] = 1;
n1 = LEN;
mean = 1;
totalsum = 0;
for(i = 0; i < BANK; i++)
{
hitss[i] = hitsd[i] = 0;
mins[i] = mind[i] = LEN*LEN;
}
if(VERBOSE) {
printf("Source ");
for(i = 0; i < LEN; i++)
printf("%i", startpos[i]);
printf(" dest ");
for(i = 0; i < LEN; i++)
printf("%i", targ[i]);
}
// loop through each trajectory
for(i = 0; i < BANK; i++)
{
fail = 0;
// count whether we're there or not
for(j = 0; j < LEN; j++)
{
if(bank[LEN*i+j] != startpos[j] && startpos[j] != 2) fail++;
}
hitss[i] += (fail == 0);
if(fail < mins[i]) mins[i] = fail;
// if(VERBOSE && fail == 0) printf("Walker %i hit source (%i)\n", i, hitss[i]);
fail = 0;
// count whether we're there or not
for(j = 0; j < LEN; j++)
{
if(bank[LEN*i+j] != targ[j] && targ[j] != 2) fail++;
}
hitsd[i] += (fail == 0);
if(fail < mind[i]) mind[i] = fail;
// if(VERBOSE && fail == 0) printf("Walker %i hit dest (%i)\n", i, hitsd[i]);
}
// loop through the number of evolutionary steps we need to make
for(r = 0; r < LEN; r++)
{
// loop through each trajectory
for(i = 0; i < BANK; i++)
{
for(j = 0; j < LEN; j++)
attempt[j] = bank[LEN*i+j];
// pick the locus to change at this step, and record the probability that we stay on track to the target
PickLocus(&bank[LEN*i], P, endtarg, &locus, &reject[i], &recbeta[LEN*i + (r-n0)], LEN, model);
bank[LEN*i+locus] = 1;
/* if(VERBOSE)
{ printf("Walker %i at ", i);
for(j = 0; j < LEN; j++)
printf("%i", bank[LEN*i+j]);
printf("\n");
}*/
fail = 0;
// count whether we're there or not
for(j = 0; j < LEN; j++)
{
if(bank[LEN*i+j] != startpos[j] && startpos[j] != 2) fail++;
}
hitss[i] += (fail == 0);
if(fail < mins[i]) mins[i] = fail;
// if(VERBOSE && fail == 0) printf("Walker %i hit source (%i)\n", i, hitss[i]);
fail = 0;
// count whether we're there or not
for(j = 0; j < LEN; j++)
{
if(bank[LEN*i+j] != targ[j] && targ[j] != 2) fail++;
}
hitsd[i] += (fail == 0);
if(fail < mind[i]) mind[i] = fail;
//if(VERBOSE && fail == 0) printf("Walker %i hit dest (%i)\n", i, hitsd[i]);
}
}
lik = 0;
for(i = 0; i < BANK; i++)
{
lik += ((double)hitss[i]/LEN)*((double)hitsd[i]/LEN);
}
if(VERBOSE){
if(lik > 0)
printf("\nHit this record at least once\n");
else printf("\n*** didn't hit this record!\n");
}
free(bank);
free(reject);
free(hitss);
free(hitsd);
free(mins);
free(mind);
free(prodreject);
free(recbeta);
// myexit(0);
return lik/BANK;
}
// compute HyperTraPS probability of a transition from "startpos" to "targ" given transition matrix "P"
double LikelihoodMultiple(int *targ, double *P, int LEN, int *startpos, double tau1, double tau2, int model)
{
int *bank;
int n0, n1;
double *reject;
int i, j, r;
int locus;
int attempt[LEN];
double mean;
double *prodreject;
double summand[LEN];
int fail;
int *hits, *totalhits;
double totalsum;
int emission_count;
// new variables
double u, prob_path, vi, betaci, nobiastotrate;
double analyticI1, analyticI2;
double sumI1, sumI2;
int n;
double tmprate;
double *recbeta;
// nobiastotrate is retain to match role in PickLocus but basically corresponds to -u
int myexitcount = 0;
// allocate memory for BANK (N_h) trajectories
bank = (int*)malloc(sizeof(int)*LEN*BANK);
reject = (double*)malloc(sizeof(double)*BANK);
hits = (int*)malloc(sizeof(int)*BANK);
totalhits = (int*)malloc(sizeof(int)*BANK);
prodreject = (double*)malloc(sizeof(double)*BANK);
recbeta = (double*)malloc(sizeof(double)*LEN*BANK);
// initialise each trajectory at the start state; count 0s and 1s
for(i = 0; i < LEN*BANK; i++)
bank[i] = startpos[i%LEN];
n0 = 0;
for(i = 0; i < LEN; i++)
n0 += startpos[i];
// the final "layer" of the hypercube we're interested in is that of the target when we've set all ambiguous loci to 1
n1 = 0;
for(i = 0; i < LEN; i++)
{
n1 += (targ[i] == 1 || targ[i] == 2);
if(targ[i] == 2 && !(tau1 == 0 && tau2 == INFINITY)) {
printf("Uncertain observations not currently supported for the continuous time picture! Please re-run with the discrete time picture.\n");
myexit(0);
}
if(targ[i] == 0 && startpos[i] == 1) {
printf("Wrong ordering, or some other problem with input file. Data file rows should be ordered ancestor then descendant! Problem transition was:\n");
for(j = 0; j < LEN; j++) printf("%i", startpos[j]);
printf(" -> ");
for(j = 0; j < LEN; j++) printf("%i", targ[j]);
myexit(0);
}
}
if(n0 > n1)
{
// the target comes before the source
printf("Wrong ordering, or some other problem with input file. Data file rows should be ordered ancestor then descendant! Problem transition was:\n");
for(j = 0; j < LEN; j++) printf("%i", startpos[j]);
printf(" -> ");
for(j = 0; j < LEN; j++) printf("%i", targ[j]);
myexit(0);
}
mean = 1;
totalsum = 0;
emission_count = (n1-n0);
// check we're not already there
fail = 0;
for(i = 0; i < LEN; i++)
fail += (targ[i] != startpos[i]);
if(fail == 0) totalsum = 1;
for(i = 0; i < BANK; i++)
prodreject[i] = 1;
for(i = 0; i < BANK; i++)
totalhits[i] = 0;
// loop through the number of evolutionary steps we need to make
for(r = n0; r < n1; r++)
{
for(i = 0; i < BANK; i++)
hits[i] = 0;
// loop through each trajectory
for(i = 0; i < BANK; i++)
{
for(j = 0; j < LEN; j++)
attempt[j] = bank[LEN*i+j];
// pick the locus to change at this step, and record the probability that we stay on track to the target
PickLocus(&bank[LEN*i], P, targ, &locus, &reject[i], &recbeta[LEN*i + (r-n0)], LEN, model);
bank[LEN*i+locus] = 1;
if(SUPERVERBOSE)
{
printf("Now at ");
for(j = 0; j < LEN; j++)
printf("%i", bank[LEN*i+j]);
printf("\n");
}
fail = 0;
// count whether we're there or not
for(j = 0; j < LEN; j++)
{
if(bank[LEN*i+j] != targ[j] && targ[j] != 2) fail = 1;
if(!fail && SUPERVERBOSE)
printf("Hit target!\n");
}
hits[i] += (1-fail);
totalhits[i] += (1-fail);
}
// keep track of total probability so far, and record if we're there
summand[r] = 0;
for(i = 0; i < BANK; i++)
{
prodreject[i] *= reject[i];
summand[r] += prodreject[i]*hits[i];
}
summand[r] /= BANK;
if(SUPERVERBOSE)
{
printf("At step %i averaged summand is %e\n", i, summand[r]);
}
totalsum += summand[r];
}
if(SUPERVERBOSE)
{
printf("Total sum %e\n", totalsum);
}
// if we're not using continuous time, avoid this calculation and just return average path probability
if(tau1 == 0 && tau2 == INFINITY)
{
if(n0 == n1) {
prob_path = 1;
emission_count = 1;
}
else
{
prob_path = 0;
for(n = 0; n < BANK; n++)
{
// for non-missing data, the comparison of total hits to emission count (ie opportunities for emission) factors out of the likelihood. but for missing data, different paths may have different numbers of opportunities to emit an observation-compatible signal, which we need to account for
prob_path += (prodreject[n]*((double)(totalhits[n])/emission_count))/BANK;
}
}
free(bank);
free(reject);
free(hits);
free(totalhits);
free(prodreject);
free(recbeta);
return prob_path;
}
// we're using continuous time
// now, compute loss intensity from this state
nobiastotrate = 0;
for(i = 0; i < LEN; i++)
{
/* ntrans must be the transition matrix. ntrans[i+i*LEN] is the bare rate for i. then ntrans[j*LEN+i] is the modifier for i from j*/
if(targ[i] == 0)
{
tmprate = RetrieveEdge(targ, i, P, LEN, model);
}
else /* we've already lost this gene */
tmprate = 0;
nobiastotrate += tmprate;
}
u = -nobiastotrate;
// now go through the recorded paths and compute vi, betaci
analyticI1 = analyticI2 = 0;
if(n0 != n1)
{
for(n = 0; n < BANK; n++)
{
prob_path = prodreject[n]*1./BANK;
sumI1 = sumI2 = 0;
for(i = 0; i < n1-n0; i++)
{
vi = 1;
for(j = 0; j < n1-n0; j++)
{
vi *= recbeta[LEN*n+j];
if(j != i)
vi *= 1./(recbeta[LEN*n+j]-recbeta[LEN*n+i]);
if(SPECTRUM_VERBOSE)
printf("step %i looking at step %i recbeta %.4f vi %.4f\n", i, j, recbeta[LEN*n+j], vi);
}
betaci = recbeta[LEN*n+i];
sumI1 += vi*(exp(tau1*u)-exp(-tau1*(betaci)))/(betaci + u);
sumI2 += vi/betaci*(exp(-betaci*tau1) - exp(-betaci*tau2));
if(SPECTRUM_VERBOSE)
printf("walker %i: stepx %i vi %.4f betaci %.4f u %.4f | sumI1 %.4f sumI2 %.4f\n (tau1 %f tau2 %f)\n", n, i, vi, betaci, u, sumI1, sumI2, tau1, tau2);
}
if(sumI1 < 0 || sumI2 < 0)
{
if(num_error == 0) {
printf("I got a negative value for I1 (%e) or I2 (%e), which shouldn't happen and suggests a lack of numerical convergence. This can happen with large numbers of features. I'm setting to zero but this may be worth examining.\n", sumI1, sumI2);
}
if(sumI1 < 0) {
if(-sumI1 > num_error) {
num_error = -sumI1;
printf("New scale of numerical error %e\n", num_error);
}
sumI1 = 0;
}
if(sumI2 < 0) {
if(-sumI2 > num_error) {
num_error = -sumI2;
printf("New scale of numerical error %e\n", num_error);
}
sumI2 = 0;
}
}
// debugging example, run --obs VerifyData/synth-bigcross-90-hard-samples.txt --times VerifyData/synth-bigcross-90-hard-times.txt --length 4 --outputtransitions 0 --kernel 3 --label VerifyData/test-bigcross-hard-ct-90-db --spectrumverbose
analyticI1 += (prob_path*sumI1);
analyticI2 += (prob_path*sumI2);
if(SPECTRUM_VERBOSE)
printf("prob_path %.4f sumI1 %.4f sumI2 %.4f | analyticI1 %.4f analyticI2 %.4f\n", prob_path, sumI1, sumI2, analyticI1, analyticI2);
myexitcount++;
// if(myexitcount == 3)
//myexit(0);
}
}
else
{
analyticI1 += exp(u*tau1); // just probability of dwelling at start
}
free(bank);
free(reject);
free(hits);
free(totalhits);
free(prodreject);
free(recbeta);
/* if(analyticI1+analyticI2 > 100)
{
printf("Myexiting at line 283\n");
myexit(0);
}*/
return analyticI1+analyticI2;
}
// get total likelihood for a set of changes
double GetLikelihoodCoalescentChange(int *matrix, int len, int ntarg, double *ntrans, double *tau1s, double *tau2s, int model, int PLI)
{
double loglik, tloglik, tlik;
int i, j;
int startpos[len];
// initialise and start at one corner of the hypercube
loglik = 0;
for(i = 0; i < len; i++)
startpos[i] = 0;
// loop through each ancestor/descendant pair
for(i = 0; i < ntarg/2; i++)
{
// output if desired
if(VERBOSE)
{
printf("Target %i: ", i);
for(j = 0; j < len; j++) printf("%i", matrix[2*i*len+len+j]);
printf(" parent is: " );
for(j = 0; j < len; j++) { startpos[j] = matrix[2*i*len+j]; printf("%i", startpos[j]); }
printf("\n");
}
// initialise start position
for(j = 0; j < len; j++)
startpos[j] = (matrix[2*i*len+j]);
// get log-likelihood contribution from this pair (transition) using HyperTraPS
if(PLI)
tlik = lscale*LikelihoodMultiplePLI(&(matrix[2*i*len+len]), ntrans, len, startpos, tau1s[i], tau2s[i], model);
else
tlik = lscale*LikelihoodMultiple(&(matrix[2*i*len+len]), ntrans, len, startpos, tau1s[i], tau2s[i], model);
tloglik = log(tlik);
if(tlik <= 0)
{
printf("Somehow I have a negative or zero likelihood, suggesting a lack of numerical convergence. Terminating to avoid unreliable posteriors.\n");
printf("This was at observation %i, which is\n", i);
for(j = 0; j < len; j++) printf("%i", matrix[2*i*len+len+j]);
printf(" parent is: " );
for(j = 0; j < len; j++) { startpos[j] = matrix[2*i*len+j]; printf("%i", startpos[j]); }
printf("\n");
//myexit(0);
}
// output if required
if(VERBOSE)
printf("--- %i %f %f\n", i, exp(tloglik), tloglik);
loglik += tloglik;
}
// return total log likelihood
return loglik;
}
// estimate gradients of likelihood from a given parameterisation
void GetGradients(int *matrix, int len, int ntarg, double *trans, double *tau1s, double *tau2s, double *gradients, double scale, int model, int PLI)
{
double lik, newlik;
int i;
lik = GetLikelihoodCoalescentChange(matrix, len, ntarg, trans, tau1s, tau2s, model, PLI);
for(i = 0; i < nparams(model, len); i++)
{
trans[i] += scale;
newlik = GetLikelihoodCoalescentChange(matrix, len, ntarg, trans, tau1s, tau2s, model, PLI);
gradients[i] = (newlik-lik)/scale;
trans[i] -= scale;
}
}
// stepwise regularise a parameter set by minimising likelihood loss as parameters are pruned
void Regularise(int *matrix, int len, int ntarg, double *ntrans, double *tau1s, double *tau2s, int model, char *labelstr, int PLI, int outputtransitions)
{
int i, j;
int NVAL;
double lik, nlik;
double oldval;
int biggestindex;
double biggest;
int pcount;
FILE *fp;
char fstr[200];
double AIC, BIC, bestIC;
double *best;
double normedval;
if(model == -1) normedval = -20;
else normedval = 0;
// initialise the setup and estimate initial likelihood and ICs
NVAL = nparams(model, len);
best = (double*)malloc(sizeof(double)*NVAL);
lik = GetLikelihoodCoalescentChange(matrix, len, ntarg, ntrans, tau1s, tau2s, model, PLI);
AIC = 2*NVAL-2*lik;
BIC = log(ntarg)*NVAL-2*lik;
bestIC = AIC;
for(i = 0; i < NVAL; i++)
best[i] = ntrans[i];
// prepare output file
sprintf(fstr, "%s-regularising.csv", labelstr);
fp = fopen(fstr, "w");
fprintf(fp, "nparam,removed,log.lik,AIC,BIC\n");
fprintf(fp, "%i,%i,%e,%e,%e\n", NVAL, -1, lik, AIC, BIC);
printf("Regularising...\npruning ");
// remove parameters stepwise
for(j = 0; j < NVAL; j++)
{
printf("%i of %i\n", j+1, NVAL);
// find parameter that retains highest likelihood when removed
biggest = 0;
for(i = 0; i < NVAL; i++)
{
oldval = ntrans[i];
ntrans[i] = normedval;
nlik = GetLikelihoodCoalescentChange(matrix, len, ntarg, ntrans, tau1s, tau2s, model, PLI);
ntrans[i] = oldval;
if((biggest == 0 || nlik > biggest) && ntrans[i] != normedval)
{
biggest = nlik;
biggestindex = i;
}
}
// set this param to zero and count new param set
ntrans[biggestindex] = normedval;
pcount = 0;
for(i = 0; i < NVAL; i++)
{
if(ntrans[i] != normedval) pcount++;
}
// output
AIC = 2*pcount-2*biggest;
BIC = log(ntarg)*pcount-2*biggest;
fprintf(fp, "%i,%i,%e,%e,%e\n", pcount, biggestindex, biggest, AIC, BIC);
if(AIC < bestIC)
{
bestIC = AIC;
for(i = 0; i < NVAL; i++)
best[i] = ntrans[i];
}
}
// output statistics of the process
sprintf(fstr, "%s-regularised.txt", labelstr);
fp = fopen(fstr, "w");
for(i = 0; i < NVAL; i++)
fprintf(fp, "%e ", best[i]);
fprintf(fp, "\n");
fclose(fp);
sprintf(fstr, "%s-regularised-lik.csv", labelstr);
fp = fopen(fstr, "w"); fprintf(fp, "Step,LogLikelihood1,LogLikelihood2\n");
fprintf(fp, "0,%e,%e\n", GetLikelihoodCoalescentChange(matrix, len, ntarg, best, tau1s, tau2s, model, PLI), GetLikelihoodCoalescentChange(matrix, len, ntarg, best, tau1s, tau2s, model, PLI));
fclose(fp);