-
Notifications
You must be signed in to change notification settings - Fork 20
/
pairlk.c
1175 lines (943 loc) · 26.5 KB
/
pairlk.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
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#ifndef LENTYPE
#define LENTYPE (50) /* max number of different types min(Lm,(K+1)*(K+1)-1) */
#endif
#ifndef RHO
#define RHO_Q (0) /*recombination rate for Qb matrices*/
#endif
#ifndef EXP_TIMES
#define EXP_TIMES (25*(10)) /* terms in series approx of EXP */
#define QB_MAX (500) /*dim of Qb*/
#endif
extern unsigned long seed[2];
extern void seed_set(FILE *a);
extern void seed_put(FILE *a);
extern double runif(void);
extern void priorpr_l_imp(double *logp, double *rem_p,int nt, int *n, int *rec,double *m,int n_pts,double *rho,double theta[2]);
extern void add_new(int *type,int *nd,int *new);
extern void remov(int *type, int *nd, int c);
extern double *dou_vec_init(const size_t num);
extern short *sh_vec_init(const size_t num);
extern void Qmat(double ****Q, double *****Qb, double theta[],int K,int L,int M, double *mu, double **P);
extern double **P;
extern long BS_LEN;
unsigned long seed[2];
void micro_satt(void);
void micro_satt_g(void);
void seed_set(FILE *a);
void seed_put(FILE *a);
double *dou_vec_init(const size_t num);
short *sh_vec_init(const size_t num);
double runif(void);
void priorpr_l_imp(double *logp, double *rem_p,int nt, int *n, int *rec,double *m,int n_pts,double *rho,double theta[2]);
void add_new(int *type,int *nd,int *new);
void remov(int *type, int *nd, int c);
/*S+ runif routine*/
double runif(void)
{
unsigned long n,x, lambda=69069;
*seed = *seed * lambda;
*(seed+1) ^= *(seed+1) >> 15;
*(seed+1) ^= *(seed+1) << 17;
n = *seed ^ *(seed+1);
x = ((n>>1) & 017777777777);
return( (double)(x+0.5) / 2147483648. );
}
/***********************************
* *
* improved procal routine *
* no logs prob= remp *log(logp) *
* *
***********************************/
void priorpr_l_imp(double *logp, double *rem_p,int nt, int *n, int *rec,double *m,int n_pts,double *rho,double theta[2])
{
int i,j,event;
double norm,mut,reco,rate;
double EXP25=72004899337.3859;
mut=0;
reco=0;
if(*rec>0) reco=*rec;
for(j=0;j<2;j++) mut += *(n+j)* *(theta+j);
if(*(m+1)==0){
event=0;
rate=*m;
}
if(*(m+1)==1){
event=1;
rate=*m;
}
if(*(m+1)>=2){
event=2;
rate=*m* *(theta+(int)*(m+1)-2);
}
for(i=0;i<n_pts;i++){
/* calculate denominator of prior prob = total rate out of state n(n-1) n.theta + rec.rho*/
norm=nt*(nt-1)+*(rho+i)*reco+mut;
/* rate =rate*v(event)/norm */
if(event!=1)
*(rem_p+i)*=(rate)/norm;
else
*(rem_p+i)*= rate* *(rho+i)/norm;
if(*(rem_p+i)>0){
while(*(rem_p+i)*EXP25<1){
*(rem_p+i)*=EXP25;
*(logp+i)-=25.0;
}
}
}
}
/*********************************
* *
* Routine to read seed for file *
* *
*********************************/
void seed_set(FILE *file)
{
long i,temp;
unsigned long max=42949672;
char ch;
for(i=0;i<2;i++){
/*ignore white space*/
while(1){
ch = fgetc(file);
if(ch != '\n' && ch != '\n' && ch != '\t') break;
}
temp= ch-'0';
while(1){
ch = fgetc(file);
if(ch == '\n' || ch == '\n' || ch == '\t' || ch ==EOF) break;
temp= (10*temp+ch-'0') % max;
}
*(seed+i)=temp;
}
}
/***********************
* *
* put seed in file *
* file_r and file_w *
* point to same file *
* but for read (r) *
* and write (w) *
* *
***********************/
void seed_put(FILE *file_w)
{
(void)fprintf(file_w,"%lu\n%lu", *seed, *(seed+1));
}
/*add element new to sample*/
void add_new(int *type,int *nd,int *new)
{
int i,j,c,flag;
c=-1;
for(i=0;i<*nd && c==-1;i++){
flag=1;
for(j=0;j<2 && flag==1;j++) flag= (*(type+(3)*i+j)==new[j]);
if(flag==1) c=i;
}
if(c>-1) *(type+(3)*c+2)+=1;
else{
for(j=0;j<2;j++) *(type+(3)* *nd+j)=new[j];
*(type+(3)* *nd+2)=1;
*nd+=1;
}
return;
}
/*remove element *(type+3*i+(0,1,2) ) */
void remov(int *type, int *nd, int c)
{
int i,j;
if(*(type+(3)*c+2)>1){
*(type+(3)*c+2)-=1;
return;
}
else{
for(i=c;i<(*nd-1);i++){
for(j=0;j<3;j++) *(type+(3)*i+j)=*(type+(3)*(i+1)+j);
}
*nd-=1;
return;
}
}
double *dou_vec_init(const size_t num)
{
double *p;
if((p = (double *) calloc(num, sizeof(double))) == NULL)
perror("initialise_array():calloc failed!");
return(p);
}
short *sh_vec_init(const size_t num)
{
short *p;
if((p = (short *) calloc(num, sizeof(short))) == NULL)
perror("initialise_array():calloc failed!");
return(p);
}
/**********************************************************
* program to calcalate Q matrices for the K-allele case *
* L-loci, simple version *
*********************************************************/
/*include NAG routines for inverting a matrix*/
#define ERROR_MIN (0.000000001)
#define TINY 1.0e-20;
void Qmat (double ****Q, double *****Qb, double theta[],int K,int L,int M, double *mu, double **P);
void matrix_inverse_new(double **a,long n);
void exp_matrix(double **out, double a,int K, double **P);
void Qmat (double ****Q, double *****Qb, double theta[],int K,int L,int M, double *mu, double **P)
{
long i,j,l,k,ii,jj;
double lambda,con, **Id,**temp;
double s[]={0.3225476896192312,1.74576110115834658,4.53662029692112798,9.39507091230113313};
Id = (double **)calloc(K,sizeof(double *));
temp = (double **)calloc(K,sizeof(double *));
for (i=0;i<K;i++){
temp[i]=dou_vec_init(K);
Id[i]= dou_vec_init(K);
}
/*generate Identity matrix*/
for(i=0;i<K;i++){
for(j=0;j<K;j++){
Id[i][j]= (i==j);
}
}
/* do first entry- matrix of rows of mu */
for(i=0;i<K;i++){
for(j=0;j<K;j++){
for(l=0;l<L;l++) Q[i][j][l][0]= mu[j];
}
}
/* do other M entries */
for(i=1;i<(M+1);i++){
for(l=0;l<L;l++){
lambda= *(theta+l)/((double)i+*(theta+l));
/* calculate (I-lambdaP) */
for(j=0;j<K;j++){
for(k=0;k<K;k++){
temp[k][j]=Id[k][j]-lambda*P[k][j];
}
}
matrix_inverse_new(temp,K); /* invert - to temp */
/* update Q - (1-lambda)*temp */
for(j=0;j<K;j++){
for(k=0;k<K;k++){
Q[k][j][l][i]=(1.0-lambda)*temp[k][j];
}
}
}
}
/*calculate Qb matrix*/
for(i=0;i<(QB_MAX);i++){
for(j=0;j<4;j++){
for(l=0;l<L;l++){
lambda = (i+1); /*should include a rho-which one (all different
possibiliities?!)*/
/* calculate exp_matrix( - theta s / lambda *(I- P)) */
exp_matrix(temp,*(theta+l)*s[j]/lambda,K,P);
con=exp(-1* (*(theta+l)*s[j]/lambda));
for(ii=0;ii<K;ii++){
for(jj=0;jj<K;jj++){
Qb[ii][jj][l][j][i]= temp[ii][jj]*con;
}
}
}
}
/*(void)fprintf(stderr,"Calc matrix: %d",i+1);*/
}
for(i=0;i<K;i++){
free(Id[i]);
free(temp[i]);
}
free(Id);
free(temp);
}
void exp_matrix(double **out, double a,int K, double **P)
{
double **temp,**mult,**temp2, **out_temp,err,sum;
long i,j,l,ii;
temp = (double **)calloc(K,sizeof(double *));
for (i=0;i<K;i++) temp[i]= dou_vec_init(K);
temp2 = (double **)calloc(K,sizeof(double *));
for (i=0;i<K;i++) temp2[i]= dou_vec_init(K);
mult = (double **)calloc(K,sizeof(double *));
for (i=0;i<K;i++) mult[i]= dou_vec_init(K);
out_temp = (double **)calloc(K,sizeof(double *));
for (i=0;i<K;i++) out_temp[i]= dou_vec_init(K);
/* initial temp is the identity */
for (i=0;i<K;i++){
for (j=0;j<K;j++){
temp[i][j]= (i==j);
out_temp[i][j]= temp[i][j];
}
}
/* mult is the matrix in the exponent */
for (i=0;i<K;i++){
for (j=0;j<K;j++){
mult[i][j]=a* P[i][j];
/*mult[i][j]*= -a;*/
}
}
/*approx exponent*/
err=1;
for(l=1;l<EXP_TIMES && err>(ERROR_MIN);l++){
/* calculate mult*temp/l =mult^l/l!*/
for (i=0;i<K;i++){
for (j=0;j<K;j++){
temp2[i][j]=0;
for( ii=0;ii<K;ii++){
temp2[i][j]+=mult[i][ii]*temp[ii][j]/((double)l);
}
}
}
err*= a/((double)l);
/* set temp=temp2 out +=temp2/(l!) */
for (i=0;i<K;i++){
for (j=0;j<K;j++){
temp[i][j]=temp2[i][j];
out_temp[i][j] +=temp2[i][j];
}
}
}
for (i=0;i<K;i++){
for (j=0;j<K;j++){
out[i][j] =(double)out_temp[i][j];
}
}
for(i=0;i<K;i++){
free(temp[i]);
free(temp2[i]);
free(out_temp[i]);
free(mult[i]);
}
free(temp);
free(temp2);
free(out_temp);
free(mult);
}
/*inefficient Gauss-Jordan elimination*/
void matrix_inverse_new(double **a,long n)
{
long i,j,k,imax;
double big,temp;
double **I;
/*set-up I*/
I = (double **)calloc(n,sizeof(double *));
for(i=0;i<n;i++) I[i]=dou_vec_init(n);
for(i=0;i<n;i++) for(j=0;j<n;j++) I[i][j]=0.0;
for(i=0;i<n;i++) I[i][i]=1.0;
/*loop over columns*/
for(j=0;j<n;j++){
/*firstly find largest a[i][j], i>=j*/
big=0.0;
for(i=j;i<n;i++){
if(fabs(a[i][j])>big){
big=fabs(a[i][j]);
imax=i;
}
}
if(big<=0.0){
(void)fprintf(stderr,"Error in matrix_inverse routine: singular matrix? \n");
abort();
}
/*swap rows*/
for(k=0;k<n;k++){
temp=a[j][k];
a[j][k]=a[imax][k];
a[imax][k]=temp;
temp=I[j][k];
I[j][k]=I[imax][k];
I[imax][k]=temp;
}
/*divide row by a[j][j]*/
temp=a[j][j];
for(k=0;k<n;k++){
a[j][k]/=temp;
I[j][k]/=temp;
}
/*calculate jth column*/
for(i=0;i<n;i++){
if(j!=i){
temp=a[i][j];
for(k=0;k<n;k++){
a[i][k]-=temp*a[j][k];
I[i][k]-=temp*I[j][k];
}
}
}
}
/*replace A by I*/
for(i=0;i<n;i++) for(j=0;j<n;j++) a[i][j]=I[i][j];
/*free I*/
for(i=0;i<n;i++) free(I[i]);
free(I);
}
double ***pi; /* prob -factor into pis */
int kall_l(int *type, int *nd,double *rho,double theta[2], double *logq, double *logp,double *rem_p,int k, int n_pts,int K,int L,int M,double *mu,double **P, double ****Q, double *****Qb);
void update(int *type, double rho, double theta[2],int k, int *nd, int *nt, int *n,double *logq,double *m,int *rec,int K,int L, double **P,double ****Q, double *****Qb);
void procal_l(int *type, int nd, int k, int *n, int K, int L, double *mu,double ****Q, double *****Qb);
double pical(int *type,int k, int *new, int nt, int nd, double rho,int L,double ****Q, double *****Qb);
int kall_l(int *type, int *nd,double *rho,double theta[2], double *logq, double *logp,double *rem_p,int kk, int n_pts,int K,int L,int M,double *mu,double **P, double ****Q, double *****Qb)
{
/* Remember P and Q matrices global. K, L and M are defined */
short int flag=0; /* continue loop? */
int min,max;
int *n,*rec,i,j,nt=M; /*number of individuals ancestral at loci 1,2,..*/
int *n_st,*rec_st,nt_st;
double u,tot,m[2],fac; /* runif, sum of `probabilities', m[0] is prior rate, m[1] is type of transition */
double pro[LENTYPE]; /*probalities -event happening to ind, and approx cond post (loci (L) changed to allele (K)) */
unsigned long seed_store[2]; /* store initial seed- print out if error */
double time;/*,length,time_sq,length_sq*/
n=(int *)calloc(L,sizeof(int));
rec=(int *)calloc(L-1,sizeof(int));
n_st=(int *)calloc(L,sizeof(int));
rec_st=(int *)calloc(L-1,sizeof(int));
pi = (double ***) calloc(K,sizeof(double **));
for(i=0;i<K;i++){
pi[i]= (double **) calloc(L,sizeof(double *));
for(j=0;j<L;j++) pi[i][j]=(double *)calloc(4,sizeof(double));
}
/* set logq and logp to 0 rem_p to 1*/
*logq=0;
for(i=0;i<n_pts;i++){
*(logp+i)=0;
*(rem_p+i)=1;
}
*seed_store = *seed;
*(seed_store+1) = *(seed+1);
/* initiate n and rec */
for(i=0;i<L;i++) *(n+i)=M;
for(i=0;i<L-1;i++) *(rec+i)=M;
#ifdef TEST
fprintf(stderr,"\n seed %u \t %u",*(seed),*(seed+1));
#endif /*TEST*/
while(!flag){
/*store nt,n,rec for use in priorpr_l*/
for(i=0;i<L;i++) *(n_st+i)=*(n+i);
for(i=0;i<L-1;i++) *(rec_st+i)=*(rec+i);
nt_st=nt;
/*calculate probability of event happening to 1,...*ndth type*/
tot=0;
for(i=0;i<*nd;i++){
*(pro+i)= *(type+(L+1)*i+L); /*number*/
fac= (nt-1); /*coalescence*/
min=-1;
max=-1;
for(j=0;j<L;j++){
if( *(type+(L+1)*i+j)>0){
fac+= *(theta+j);
}
}
if( *(type+(L+1)*i)>0 && *(type+(L+1)*i+1)>0)
fac+= rho[kk];
*(pro+i) *= fac;
tot+= *(pro+i);
}
/* error */
if(tot<=0){
(void)fprintf(stdout,"Error log of non-positive no (tot) . \n");
(void)fprintf(stdout,"initial seed %lu \t %lu \n\n",*(seed_store), *(seed_store+1));
return(1);
}
/* choose individual. (type+3*i+(0,1,2,..L)) is individual chosen */
u=tot*runif();
i=0;
while(u>0){
u-=*(pro+i);
i++;
}
i--;
/* update logq */
if(*(pro+i)>0){
*logq+=log(*(pro+i)/(tot));
}else{
(void)fprintf(stdout,"Error log of non-positive no. (pro) \n");
(void)fprintf(stdout,"initial seed %lu \t %lu \n\n",*(seed_store), *(seed_store+1));
return(1);
}
/* calculate approximation of conditional posteriors-single loci */
procal_l(type,*nd,i,n,K,L,mu,Q,Qb);
/*choose event and update type nd m and logq */
update(type,rho[kk],theta,i,nd,&nt,n,logq,m,rec,K,L,P,Q,Qb);
/*error*/
if(*logq==2 || *logq==1){
(void)fprintf(stdout,"Error in update logq = %f \n seed %lu %lu \n n %d %d \n",*logq,*seed_store,*(seed_store+1),*n,*(n+1));
return(1);
}
/*update logp*/
priorpr_l_imp(logp,rem_p,nt_st,n_st,rec_st,m,n_pts,rho,theta);
#ifdef TEST
fprintf(stderr,"\n1 %d \t2 %d \tancestral %d \tdouble %d ",*n,*(n+1),nt,*rec);
fprintf(stderr,"logq %f logp %f",*logq,*(logp)+log(*(rem_p)));
#endif /*TEST*/
if(*nd>LENTYPE) return(1);
/* update flag */
flag=1;
for(j=0;j<L;j++){
flag *= (*(n+j)==1);
}
}
/* update logq to take account of mutation stationary distance*/
for(i=0;i<*nd;i++){
for(j=0;j<L;j++){
if(*(type+(L+1)*i+j)>0) *logq -= log(mu[ *(type+(L+1)*i+j)-1 ]);
}
}
/*TMRCA=time;*/
free(n);
free(rec);
free(n_st);
free(rec_st);
for(i=0;i<K;i++){
for(j=0;j<L;j++) free(pi[i][j]);
free(pi[i]);
}
free(pi);
return(0);
}
/* calculate pi[K][L]- independent loci. input types, number distinct,
*/
void procal_l(int *type, int nd, int k, int *n, int K, int L, double *mu,double ****Q, double *****Qb)
{
int i,j,m,l, *n_copy;
n_copy=(int *)calloc(L,sizeof(int));
/*copy n and omit individual k from the sample */
for(i=0;i<L;i++){
n_copy[i]=n[i];
if (*(type+(L+1)*k+i)>0) n_copy[i]-=1;
}
*(type+(L+1)*k+L)-=1;
for(i=0;i<L;i++){
/*init pi*/
for(j=0;j<K;j++) for(l=0;l<4;l++) pi[j][i][l]=0;
if(n_copy[i]>=QB_MAX) n_copy[i]=QB_MAX-1;
/* providing there are some ancestral types at locus i- calc pi */
if(n_copy[i]>0){
for(m=0;m<nd;m++){
if(*(type+(L+1)*m+i)>0){
for(j=0;j<K;j++){
for(l=0;l<4;l++){
pi[j][i][l]+= *(type+(L+1)*m+L) * Qb[*(type+(L+1)*m+i)-1][j][i][l][n_copy[i]];
}
}
}
}
for(j=0;j<K;j++) for(l=0;l<4;l++) pi[j][i][l]/= (double)(n_copy[i]);
}
else{ /*else use stationary probs */
for(j=0;j<K;j++) for(l=0;l<4;l++) pi[j][i][l]= mu[j];
}
}
/* add individual k back to sample */
*(type+(L+1)*k+L)+=1;
free(n_copy);
}
void update(int *type, double rho, double theta[2],int k, int *nd, int *nt, int *n,double *logq,double *m,int *rec,int K,int L, double **P,double ****Q, double *****Qb)
{
double *pr,u,tot,pitemp,pitemp2; /*event prob*/
int i,j,a,e,*new,min,max,omin,omax,*rec1,*rec2,*old;
rec2=(int *)calloc(L,sizeof(int));
rec1=(int *)calloc(L,sizeof(int));
new=(int *)calloc(L,sizeof(int));
old=(int *)calloc(L,sizeof(int));
pr=dou_vec_init(K*L+L-1+LENTYPE);
for(i=0;i<L;i++) {
new[i]=*(type+(L+1)*k+i);
rec1[i]=0;
rec2[i]=new[i];
}
/*calc prob of each event prob*pi(type) */
/*mutations*/
for(i=0;i<L;i++){ /*Locus*/
if(*(type+(L+1)*k+i)>0){
a=*(type+(L+1)*k+i)-1;
for(j=0;j<K;j++){
if(P[j][a]>0){
new[i]=j+1;
pitemp=pical(type,k,new,*nt,*nd,rho,L,Q,Qb);
new[i]=a+1;
pr[K*i+j]= *(theta+i) * pitemp *P[j][a];
}
else
pr[K*i+j]=0;
}
}
else
for(j=0;j<K;j++) pr[K*i+j]=0;
}
/*recombination*/
min=-1;
max=-1;
for(i=0;i<L;i++){
pr[K*L+i]=0;
if(*(type+(L+1)*k+i)>0){
max=i;
if(min<0) min=i;
}
}
for(i=min;i<max;i++){
rec1[i]=rec2[i];
rec2[i]=0;
pitemp=pical(type,k,rec1,*nt,*nd,rho,L,Q,Qb);
pitemp2=pical(type,k,rec2,*nt,*nd,rho,L,Q,Qb);
pr[K*L+i]=rho*pitemp*pitemp2;
}
/*coalescence*/
for(i=0;i<*nd;i++){
if(i!=k){
pr[K*L+L-1+i]=0;
for(j=0;j<L;j++){
if(*(type+(L+1)*i+j)==*(type+(L+1)*k+j))
new[j]=*(type+(L+1)*i+j);
else{
if(*(type+(L+1)*i+j)>0 && *(type+(L+1)*k+j)>0) j=L+1;
else{
if(*(type+(L+1)*i+j)>0) new[j]=*(type+(L+1)*i+j);
else
new[j]=*(type+(L+1)*k+j);
}
}
if(j<L) old[j]=*(type+(L+1)*i+j);
}
if(j==L){
pitemp=pical(type,k,new,*nt,*nd,rho,L,Q,Qb);
pitemp2=pical(type,k,old,*nt,*nd,rho,L,Q,Qb);
pr[K*L+L-1+i]= *(type+(L+1)*i+L) * pitemp / pitemp2;
}
}
else pr[K*L+L-1+i]= *(type+(L+1)*i+L)-1;
}
/*update sample and logq*/
tot=0;
for(e=0;e<(L*K+L+*nd-1);e++){
tot+=*(pr+e);
}
if (tot<=0){
*logq=1;
return;}
u=runif() * tot;
e=0;
while(u >= 0){
u-= *(pr+e);
e++;
}
if (*(pr+e-1)>0) {*logq+= log( *(pr+e-1)/(tot));
}else{
(void)fprintf(stdout,"\n e %d prob %f tot %f\n ",e,*(pr+e-1),tot);
(void)fprintf(stdout,"\n types \n \n");
for(i=0;i<*nd;i++){
for(j=0;j<(L+1);j++) (void)fprintf(stdout,"%d ",*(type+(L+1)*i+j));
(void)fprintf(stdout,"\n");
}
*logq= 2;
return;
}
/*updates for each event, update type, nd, n, nt*/
if(e<= K*L){ /*mutation*/
a=(e-1)%K; /*new allele type-1*/
i=(e-a-1)/K; /*locus*/
/*update m*/
m[0]=*(type+(L+1)*k+L)* P[a][*(type+(L+1)*k+i)-1];
m[1]=L+i;
/*calculate new type*/
for(j=0;j<L;j++) new[j]=*(type+(L+1)*k+j);
new[i]=a+1;
add_new(type,nd,new);
/*remove old type-update nd*/
remov(type,nd,k);
}
if(e>K*L && e <=(K*L+L-1)){ /*recombination*/
i=e-K*L; /*locus*/
/*update m*/
m[0]= *(type+(L+1)*k+L);
m[1]=i;
/*calculate new type*/
max=-1;
for(j=0;j<i;j++){
new[j]=*(type+(L+1)*k+j);
if(*(type+(L+1)*k+j)>0) min=j;}
for(j=(i);j<L;j++){
new[j]=0;
if(max==-1 && *(type+(L+1)*k+j)>0 ) max=j;
}
/*update rec*/
for(j=min;j<max;j++) *(rec+j)-=1;
/*add new type*/
add_new(type,nd,new);
for(j=0;j<i;j++) new[j]=0;
for(j=(i);j<L;j++) new[j]=*(type+(L+1)*k+j);
/*add new type*/
add_new(type,nd,new);
/*remove old type*/
remov(type,nd,k);
*nt+=1;
}
if(e>(K*L+L-1)){ /*coalesence*/
i=e-(K*L+L); /*individual*/
/*update rec*/
min=-1;
omin=-1;
for(j=0;j<L;j++){
if(*(type+(L+1)*i+j)>0){
omax=j;
if(omin==-1) omin=j;
}
if(*(type+(L+1)*k+j)>0){
max=j;
if(min==-1) min=j;
}
}
min= min+(omin-min)*(omin>min);
max= max+(omax-max)*(omax<max);
if(min<=max) for(j=min;j<max;j++) *(rec+j)-=1;
else for(j=max;j<min;j++) *(rec+j)+=1;
for(j=0;j<L;j++){
if(*(type+(L+1)*k+j)==0) new[j]= *(type+(L+1)*i+j);
else{
new[j]= *(type+(L+1)*k+j);
if( *(type+(L+1)*i+j)>0) n[j]-=1;
}
}
/*calc m*/
m[0]= *(type+(L+1)*k+L) *( *(type+(L+1)*i+L) - (i==k));
m[1]=0.0;
/*add new type*/
add_new(type,nd,new);
/*remove two types*/
if ( *(type+(L+1)*k+L)==1 && k<i) i--;
remov(type,nd,k);
remov(type,nd,i);
*nt-=1;
}
free(new);
free(rec2);
free(pr);
free(rec1);
free(old);
}
/*prob of nth member new given n-1 members (type - individual k).
nt=n, nd = number of distinct individuals-NEW METHOD*/
double pical(int *type,int k, int *new, int nt, int nd,double rho,int L,double ****Q, double *****Qb)
{
int i,j,l,*index,index_len=0,a,e,d;
double temp=0.0;
double int_temp;
double w[]={0.6031541043416, 0.3574186924378, 0.0388879085150, 0.0005392947056}; /*weights for num int*/
double rec;
double p[4][LENTYPE];
index=(int *)calloc(L,sizeof(int));
/*remove type k*/
nt--;
*(type+k*(L+1)+L)-=1;
/*set up index-loci ancetral in new*/
for(i=0;i<L;i++){
if(*(new+i)>0){ *(index+index_len)=i;
index_len++;}
}
if(index_len==0){
/*add type k back*/
*(type+k*(L+1)+L)+=1;
free(index);
return(1.0);
}
if(index_len==1){
/*add type k back*/
*(type+k*(L+1)+L)+=1;
temp=0;
for(l=0;l<4;l++) temp+=w[l]*pi[*(new+*index)-1][*index][l];
free(index);
return(temp);
}
for(i=0;i<4;i++) for(j=0;j<nd;j++) p[i][j]=1.0;
temp=1.0;
for(i=0;i<index_len;i++){
/*calc recombination rate*/
if(i>0){
rec=0.0;
for(j=index[i-1];j<*(index+i);j++) rec+=rho;
rec=rec/(rec+nt);
}
else rec=0.0;
if((nt+(int)(rec+.5))>QB_MAX) a=QB_MAX-1;
else
a=nt+(int)(rec+.5)-1;
d=new[index[i]];
for(j=0;j<nd;j++){
if(*(type+j*(L+1)+L)>0){
e=*(type+j*(L+1)+index[i]);
if(e>0){
for(l=0;l<4;l++) p[l][j]=((1-rec)*p[l][j]+rec*temp)*Qb[e-1][d-1][index[i]][l][a];
}
else{
for(l=0;l<4;l++) p[l][j]=((1-rec)*p[l][j]+rec*temp)*pi[d-1][index[i]][l];
}
}
}
temp=0;
for(j=0;j<nd;j++) for(l=0;l<4;l++) temp+=w[l]* *(type+(L+1)*j+L)*p[l][j];
temp/=nt;
}
/*add type k back*/
*(type+k*(L+1)+L)+=1;
free(index);
return(temp);
}
void pairs(int nd, int *data, int K, double **P, double *mu, double theta[2], double rho_max, int n_pts, int NRUN, double *log_lik);
extern int kall_l(int *type, int *nd,double *rho,double theta[2], double *logq, double *logp,double *rem_p,int k, int n_pts,int K,int L,int M,double *mu,double **P, double ****Q, double *****Qb);
/***********************************************************
*
* Routine for calculating pairwise likelihood curve.
* Input:
*
* nd_store = number of distinct haplotypes
* data = vector of length 3*nd; type at each locus
* and multiplicity (type = {1,..,K})
* K = number of alleles
* P = mutation matrix; K by K
* mu = mutation stationary distribution
* theta = mutation rate at each locus
* rho_max = maximum value of rho