-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdgpef.c
1956 lines (1828 loc) · 54.6 KB
/
dgpef.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 "dgpe.h"
#include "aa_empirical.h"
char get_param(int *k, char **arg, char *value){
char opt='z';
*value='\0';
if(*k==0) return('z');
if(arg[(*k)][0]!='-'){ /* not an option, rather a value */
sprintf(value,"%s",arg[(*k)]);
*k = (*k)-1;
if(arg[(*k)][0]=='-') opt=arg[(*k)][1];
}
else{ /* is an option */
opt=arg[(*k)][1];
if(arg[(*k)][1]!='\0' && arg[(*k)][2]!='\0') sprintf(value,"%s",&arg[(*k)][2]);
}
*k = (*k)-1;
return(opt);
}
void winterleavef(FILE *fp, int numsp, int nsite, char name[][11], char *seq)
{
int nblock, i, j, k, l, kend;
nblock = nsite/60 + 1;
fprintf(fp,"%i %i\n", numsp, nsite);
for(l = 0; l < nblock; l++){
for(i = 0; i < numsp; i++){
if (l == 0)
for(j = 0; j < 10; j++) putc(name[i][j], fp);
else
for(j = 0; j < 10; j++) putc(' ', fp);
if (l == nblock - 1)
kend = nsite % 60;
else
kend = 60;
for(k = 0; k < kend; k++){
if (k % 10 == 0) putc(' ', fp);
putc(seq[k + 60*l + i*nsite], fp);
}
putc('\n', fp);
}
putc('\n', fp);
}
}
void dorder(int n, double *x, int *o){
int i,j,to,io;
double xm;
for(i = 0; i < n; i++) o[i] = i;
for(i = 0; i < n; i++){
xm = x[o[i]];
to = o[i];
io = i;
for(j = i+1; j < n; j++)
if (x[o[j]] < xm){
to = o[j];
xm = x[o[j]];
io = j;
}
o[io] = o[i];
o[i] = to;
}
}
double *dgpe_rate_est(FILE *seqfile, FILE *treefile, FILE *seqfileo,
double quant, int *nsites, double *llikg, double *alpha){
/* IN: seqfile - FILE pointer to sequence data
* NOTE: sequence data assumes names are at most 10 characters long
* treefile - FILE pointer to newick tree
* seqfileo - If not NULL, FILE pointer for output sequence data
* quant - sites with rates >= qth quantile are output to seqfileo.
* Only used if seqfileo is not NULL
* OUT: nsite - number of sites
* llikg - maximized log likelihood
* alpha - optimal alpha
* RETURN: ratee - ratee[i] gives the rate estimate for the ith site */
char aaRmat[]="lg.dat",*seqc,(*names)[11],*seqco;
int nchar2,endsite,fso,*seq,nchar=20,nsite,ntaxa,smodel=9,nrates=101,*s,nb;
int i,j,k,which=1,*o,start,*count,*locs,status;
double one=1.0,*fr,*rates,*wt,ub=10.0,*likmat,*utreec,*utreecn,*M,neg=-1.0;
double *ratee,*ratees,p,pl,q,bound,Qk,*W,*Lam;
/* FILE *ofile; /\* testing *\/ */
/* sequence */
fso=fscanf(seqfile,"%i %i", &ntaxa, &nsite);
*nsites=nsite;
if(fso<2){
printf("first two entries of sequence file should be ntaxa, nsite");
exit(1);
}
seqc = (char *) malloc((size_t) ntaxa*nsite*sizeof(char));
seq = (int *) malloc((size_t) ntaxa*nsite*sizeof(int));
count = (int *) malloc((size_t) nsite*sizeof(int));
names = (char (*)[11]) malloc((size_t) ntaxa*sizeof(*names));
locs = (int *) malloc((size_t) nsite*sizeof(int));
rinterleavef_nolim(seqfile,&ntaxa,&nsite,names,seqc);
for(i = 0; i < ntaxa*nsite; i++) seq[i]=l2ip(seqc[i]);
endsite=patt_freq_locs(ntaxa,nsite,seq,count,locs);
/* frequencies and substitution model*/
fr = (double *) malloc((size_t) nchar*sizeof(double));
char_freq_count(nchar,ntaxa,endsite,seq,count,fr);
subst_model_setparam(nchar,smodel,fr,&Qk,&W,&Lam);
/* rates and initial weights */
rates = (double *) malloc((size_t) nrates*sizeof(double));
wt = (double *) malloc((size_t) nrates*sizeof(double));
for(i = 0; i < nrates; i++) rates[i] = i*ub/(nrates-1);
for(i = 0; i < nrates; i++) wt[i] = 1.0/nrates;
/* Obtain likmat */
likmat = (double *) malloc((size_t) nsite*nrates*sizeof(double));
utreec=(double *) malloc((size_t) 4*(ntaxa-1)*sizeof(double));
utreecn=(double *) malloc((size_t) 4*(ntaxa-1)*sizeof(double));
s=(int *) malloc((size_t) ntaxa*sizeof(int));
nchar2 = nchar*nchar;
nb = 2*ntaxa-3;
M = (double *) malloc((size_t) nchar2*nb*sizeof(double));
read_treecsnl(treefile,ntaxa,names,utreec,1);
utreec[2+(ntaxa-2)*4] += utreec[3+(ntaxa-2)*4];
utreec[3+(ntaxa-2)*4] = 0.0;
memcpy(utreecn,utreec,(size_t) 4*(ntaxa-1)*sizeof(double));
for(j = 0; j < nrates; j++){
if(rates[j] <= 0.0){
for(k = 0; k < endsite; k++)
likmat[k+j*endsite] = lik_zero_rate_site(nchar,ntaxa,&seq[k],endsite,
fr);
}
else{
for(i = 0; i < (ntaxa-1); i++)
for(k = 2; k < 4; k++)
utreecn[k+i*4]=rates[j]*utreec[k+i*4];
transm_rate(smodel,nchar,ntaxa,utreecn,1,&one,neg,fr,W,Lam,0,M,&one,&one);
for(k = 0; k < endsite; k++){
for(i = 0; i < ntaxa; i++) s[i] = seq[k+i*endsite];
likmat[k+j*endsite] = lik_deriv_siteb(nchar,ntaxa,s,fr,utreecn,1,&one,
&one, M,&one,&one,0);
}
}
}
/* alpha estimate and lnl */
ratee=(double *) malloc((size_t) endsite*sizeof(double));
*llikg = gamma_mle(endsite, nrates, rates, count, likmat, alpha);
/* rate estimate*/
for(i=0; i<endsite; i++) ratee[i]=0.0;
cdfgam(&which, &pl, &q, &rates[0], alpha, alpha, &status, &bound);
for(i = 1; i < nrates; i++){
cdfgam(&which, &p, &q, &rates[i], alpha, alpha, &status, &bound);
wt[i-1] = p - pl;
pl = p;
}
wt[nrates-1] = 1 - p;
mean_rate_estf(endsite,nrates,rates,wt,likmat,ratee);
ratees=(double *) malloc((size_t) nsite*sizeof(double));
for(i=0; i<nsite; i++){
ratees[i]=ratee[locs[i]];
}
/* output sequence data */
if(seqfileo!=NULL){
o=(int *) malloc((size_t) nsite*sizeof(int));
dorder(nsite,ratees,o);
/* ofile=fopen("tmp.order","w"); */
/* for(i=0; i<nsite; i++){ */
/* fprintf(ofile,"%i\n",o[i]); */
/* } */
/* fclose(ofile); */
start=quant*nsite;
seqco=(char *) malloc((size_t) (nsite-start)*ntaxa*sizeof(double));
for(i=start; i<nsite; i++)
for(j=0; j<ntaxa; j++)
seqco[i-start+j*(nsite-start)]=seqc[o[i]+j*nsite];
winterleavef(seqfileo,ntaxa,nsite-start,names,seqco);
free(o); free(seqco);
}
free(seqc); free(seq); free(names); free(locs); free(fr); free(rates);
free(wt); free(W); free(Lam); free(likmat); free(utreec); free(utreecn);
free(s); free(ratee);
return(ratees);
}
void dist_est_paramf(FILE *fp, FILE **treefile, FILE **Qfile, FILE **ratefile,
FILE **seqfile, char *aaRmat, int *nchar, int *model,
int *nrate, double *kappa, double *ub, int *de){
char c,opt[100],value[100];
int is_option,done=0;
*treefile=NULL;
*Qfile=NULL;
*ratefile=NULL;
*seqfile=NULL;
*aaRmat='\0';
*nchar=4;
*nrate=101;
*ub=10.0;
*de=1; /* perform DE estimation */
while(!done){
c = ctl_paraml(fp,opt,value,&is_option);
if(is_option){
/* printf("option: %s value: %s\n",opt, value); */
if(strcmp(opt,"treefile")==0)
*treefile=fopene(value,"r","treefile is not available");
if(strcmp(opt,"nchar")==0) *nchar=atoi(value);
if(strcmp(opt,"model")==0) *model=atoi(value);
if(strcmp(opt,"aaRatefile")==0) sprintf(aaRmat,"%s",value);
if(strcmp(opt,"Qfile")==0)
*Qfile=fopene(value,"r","Qfile is not available");
if(strcmp(opt,"kappa")==0) *kappa=atof(value);
if(strcmp(opt,"ttratio")==0) *kappa=-atof(value);
if(strcmp(opt,"nrate")==0) *nrate=atoi(value);
if(strcmp(opt,"ratefile")==0)
*ratefile=fopene(value,"r","ratefile is not available");
if(strcmp(opt,"ub")==0) *ub=atof(value);
if(strcmp(opt,"seqfile")==0)
*seqfile=fopene(value,"r","seqfile is not available");
if(strcmp(opt,"DEest")==0) *de=atoi(value);
}
if(c==EOF) done=1;
}
}
double lik_zero_rate_site(int nchar, int ntaxa, int *s, int incs,
double *fr){
int is_const=1,c,i,ncs;
double l=0.0;
ncs = ntaxa*incs;
for(i=0; i<ncs; i+=incs)
if(s[i]<nchar){
c=s[i];
break;
}
for(; i < ncs; i+=incs)
if(s[i]<nchar && s[i] != c){
is_const=0;
break;
}
if(is_const) l=fr[c];
return(l);
}
void numder(double x, double alpha, double *f)
{
#define H 1.0e-6
double h = H;
int which = 1, status;
double p, ph, pnh, q, bound, anh, ah;
anh = alpha-h;
cdfgam(&which, &pnh, &q, &x, &anh, &anh, &status, &bound);
cdfgam(&which, &p, &q, &x, &alpha, &alpha, &status, &bound);
ah = alpha+h;
cdfgam(&which, &ph, &q, &x, &ah, &ah, &status, &bound);
f[0] = p;
f[1] = (ph - pnh)/(2.0*h);
f[2] = (ph - 2.0*p + pnh)/h;
}
void numderv(int nrates, double *wt, double *wtp, double *wtpp, double *br,
double alpha)
{
int i,k;
double fl[3], fu[3];
numder(br[0], alpha, fl);
for(i = 1; i < nrates; i++){
numder(br[i], alpha, fu);
wt[i-1] = fu[0] - fl[0];
wtp[i-1] = fu[1] - fl[1];
wtpp[i-1] = fu[2] - fl[2];
for(k = 0; k < 3; k++) fl[k] = fu[k];
}
wt[nrates-1] = 1-fl[0];
wtp[nrates-1] = -fl[1];
wtpp[nrates-1] = -fl[2];
/* for(i = 1; i < nrates; i++) */
/* printf("%.6e %.6e %.6e\n", wt[i], wtp[i], wtpp[i]); */
}
double gamma_mle(int endsite, int nrates, double *rates, int *count,
double *likmat, double *alpha_max)
{
#define ALPHAL 0.0005
#define ALPHAU 100.0
int i, j;
double lgi, lgip, lgipp, f[3], *br, *wt, *wtp, *wtpp;
double alpha, alphau=ALPHAU, alphal=ALPHAL;
br = (double *) malloc((size_t) nrates*sizeof(double));
wt = (double *) malloc((size_t) nrates*sizeof(double));
wtp = (double *) malloc((size_t) nrates*sizeof(double));
wtpp = (double *) malloc((size_t) nrates*sizeof(double));
br[0] = 0.0;
for(j = 1; j < nrates; j++) br[j] = (rates[j] + rates[j-1])/2.0;
alpha = 1.0;
do{
numderv(nrates, wt, wtp, wtpp, br, alpha);
f[0] = 0.0; f[1] = 0.0; f[2] = 0.0;
for(i = 0; i < endsite; i++){
lgi = 0.0; lgip = 0.0; lgipp = 0.0;
for(j = 0; j < nrates; j++){
lgi += wt[j]*likmat[i + j*endsite];
lgip += wtp[j]*likmat[i + j*endsite];
lgipp += wtpp[j]*likmat[i + j*endsite];
}
f[0] += count[i]*log(lgi);
f[1] += count[i]*lgip/lgi;
f[2] += count[i]*(lgipp/lgi - (lgip/lgi)*(lgip/lgi));
}
}while(bnewtr(&alphal, &alphau, &alpha, f));
*alpha_max = alpha;
printf("%.10e %.10e %.10e\n", alpha, f[0], f[1]);
free(br);
free(wt);
free(wtp);
free(wtpp);
return(f[0]);
}
double lik_calc(int endsite, int nrates, int *count, double *likmat, double *wt)
{
int i,j;
double llik, lg;
llik = 0.0;
for(i = 0; i < endsite; i++){
lg = 0.0;
for(j = 0; j < nrates; j++) lg += wt[j]*likmat[i+j*endsite];
llik += count[i]*log(lg);
}
return(llik);
}
void mean_rate_estf(int endsite, int nrates, double *rate, double *wt,
double *likmat, double *ratee){
int i,j;
double lg,joint;
for(i = 0; i < endsite; i++){
ratee[i] = 0.0;
lg = 0.0;
for(j = 0; j < nrates; j++){
joint=wt[j] * likmat[i+j*endsite];
ratee[i] += joint* rate[j];
lg += joint;
}
ratee[i] /= lg;
}
return;
}
void makeprotfreqs(double *fr, double *W, double *Lam)
{
long i, mineig,nchar=20;
mineig = 0;
for (i = 0; i < nchar; i++)
if (fabs(Lam[i]) < fabs(Lam[mineig]))
mineig = i;
for(i = 0; i < nchar; i++) fr[i] = fabs(W[i+mineig*nchar]);
}
void chQ_freq(int nbin, double *W, double *Lam, double *fr, double *fro)
{
/* binning/udistfbf.c
* Q = Pi^(-1) W Lam W'
* decomposition used to obtain P(t) as
* P(t) = Pi^(-1) W exp(Lam t) W'
* where W is determined from eigen-decomp
* Pi^(1/2) Q Pi^(-1/2) = U' Lam U, W = (Pi)^(1/2) U'
* To adjust for frequencies Pi*:
* obtain Q_ij* = Q_ij pi_j* / pi_j
* Q_ii* = - sum_{j neq j} Q_ij*
* M_ij* = sqrt(pi_i*) Q_ij* / sqrt(pi_j*)
* U and Lam from M = U' Lam U (eigen-decomp)
* W = (Pi*)^(1/2) U' */
int i,j,l;
double R[400], Offdiag[400], Rt, mu;
/* for(i = 0; i < nbin; i++) */
/* for(j = 0; j < nbin; j++){ */
/* Rt = 0.0; */
/* for(l = 0; l < nbin; l++) */
/* Rt += W[i+l*nbin]*W[j+l*nbin]*Lam[l]; */
/* Rt /= fro[i]; */
/* if (i != j) */
/* printf("%f\n", Rt); */
/* } */
/* exit(0); */
for(i = 0; i < nbin; i++){ /* R_ij = Q_ij / (pi_i pi_j) */
for(j = (i+1); j < nbin; j++){
Rt = 0.0;
for(l = 0; l < nbin; l++){
Rt += W[i+l*nbin]*W[j+l*nbin]*Lam[l];
}
Rt /= (fro[i]*fro[j]);
R[i+j*nbin] = Rt; R[j+i*nbin] = Rt;
}
}
for(i = 0; i < nbin; i++){ /* R_ii = Q_ii* */
Rt = 0.0;
for(j = 0; j < nbin; j++){
if (j != i)
Rt -= R[i+j*nbin]*fr[j];
}
R[i+i*nbin] = Rt/fr[i]; /* will cancel with fr[i] below */
}
/* for(i = 0; i < nbin; i++) */
/* for(j = 0; j < nbin; j++) printf("%f\n",R[i+j*nbin]*fr[j]); exit(0); */
for(i = 0; i < nbin; i++){ /* R_ij = entry of M_ij* above */
for(j = i; j < nbin; j++){
R[i+j*nbin] *= sqrt(fr[i]*fr[j]);
if (j != i) R[j+i*nbin] = R[i+j*nbin];
}
}
eigenRealSym(R, nbin, Lam, Offdiag); /* eigen-decomp of M* */
for(i = 0; i < nbin; i++){ /* W = (Pi*)^(1/2) U' */
for(j = 0; j < nbin; j++){
W[i+j*nbin] = R[j+i*nbin] * sqrt(fr[i]);
}
}
/* rescale eigenvalues so that -sum_i pi_i Q_ii = 1 */
mu = 0.0;
for(i = 0; i < nbin; i++)
for(l = 0; l < nbin; l++) mu -= Lam[l]*W[i+l*nbin]*W[i+l*nbin];
for(i = 0; i < nbin; i++) Lam[i] /= mu;
}
double int_elamrt(double t, double lam, double alpha, int deriv, double *de){
double te,dt;
te = -lam*t/alpha;
dt= exp(-alpha*log(1+te));
if(deriv==0) return(dt);
if(deriv==1 || deriv==2){
de[0]=lam*exp(-(alpha+1)*log(1+te));
if(deriv==2) de[1]=(alpha+1)*lam*lam*exp(-(alpha+2)*log(1+te))/alpha;
}
if(deriv== 3 || deriv==4){
de[0] = dt*(te/(1+te)-log(1+te));
if(deriv==4)
de[1]=de[0]*de[0]/dt +
dt*(te*te/(alpha*(1+te)*(1+te))-te/(alpha*(1+te)));
}
return(dt);
}
void pJCm(double t, int nchar, double *fr, int deriv,
double *M, double *Mp, double *Mpp){
int i,j;
double m,emt;
m = nchar/(nchar - 1.0);
emt=exp(-t*m);
for(i = 0; i < nchar; i++)
for(j = 0; j < nchar; j++){
if(i==j){
M[i+j*nchar]=1.0/nchar+(1.0-1.0/nchar)*emt;
if(deriv==1 || deriv==2) Mp[i+j*nchar]=-m*(1.0-1.0/nchar)*emt;
if(deriv==2) Mpp[i+j*nchar]=m*m*(1.0-1.0/nchar)*emt;
}
if(i!=j){
M[i+j*nchar]=1.0/nchar*(1.0-exp(-m*t));
if(deriv==1 || deriv==2) Mp[i+j*nchar]=m*1.0/nchar*emt;
if(deriv==2) Mpp[i+j*nchar]=-m*m*1.0/nchar*emt;
}
}
}
void pJCm_gen(double t, int nchar, double *fr, double *W, double *Lam,
int deriv, double *M, double *Mp, double *Mpp){
pJCm(t,nchar,fr,deriv,M,Mp,Mpp);
}
void pJCma(double t, int nchar, double *fr, int deriv, double alpha,
double *M, double *Mp, double *Mpp){
int i,j;
double mu,emut,demu[2];
mu = nchar/(nchar - 1.0);
emut=int_elamrt(t,-1.0*mu,alpha,deriv,demu);
for(i = 0; i < nchar; i++)
for(j = 0; j < nchar; j++){
if(i==j){
M[i+j*nchar]=1.0/nchar+(1.0-1.0/nchar)*emut;
if(deriv > 0)
Mp[i+j*nchar]=(1.0-1.0/nchar)*demu[0];
if(deriv==2 || deriv==4) Mpp[i+j*nchar]=(1.0-1.0/nchar)*demu[1];
}
if(i!=j){
M[i+j*nchar]=1.0/nchar-emut/nchar;
if(deriv > 0)
Mp[i+j*nchar]=-demu[0]/nchar;
if(deriv==2 || deriv==4) Mpp[i+j*nchar]=-demu[1]/nchar;
}
}
}
void pJCma_gen(double t, int nchar, double *fr, double *W, double *Lam,
int deriv, double alpha, double *M, double *Mp, double *Mpp){
pJCma(t,nchar,fr,deriv,alpha,M,Mp,Mpp);
}
double muF81f(int nchar, double *fr){
int l;
double mu = 0.0;
for(l = 0; l < nchar; l++) mu += fr[l]*(1.0-fr[l]);
return(1.0/mu);
}
void pF81m(double t, int nchar, double *fr, double mu, int deriv,
double *M, double *Mp, double *Mpp){
int i,j;
double emut;
emut=exp(-mu*t);
for(i = 0; i < nchar; i++)
for(j = 0; j < nchar; j++){
if(i==j){
M[i+j*nchar]=fr[j]+(1.0-fr[j])*emut;
if(deriv==1 || deriv==2) Mp[i+j*nchar]=-mu*(1.0-fr[j])*emut;
if(deriv==2) Mpp[i+j*nchar]=mu*mu*(1.0-fr[j])*emut;
}
if(i!=j){
M[i+j*nchar]=fr[j]*(1.0-exp(-mu*t));
if(deriv==1 || deriv==2) Mp[i+j*nchar]=mu*fr[j]*emut;
if(deriv==2) Mpp[i+j*nchar]=-mu*mu*fr[j]*emut;
}
}
}
void pF81m_gen(double t, int nchar, double *fr, double *mu, double *Lam,
int deriv, double *M, double *Mp, double *Mpp){
pF81m(t,nchar,fr,*mu,deriv,M,Mp,Mpp);
}
void pF81ma(double t, int nchar, double *fr, double mu, int deriv, double alpha,
double *M, double *Mp, double *Mpp){
int i,j;
double emut,demu[2];
emut=int_elamrt(t,-1.0*mu,alpha,deriv,demu);
for(i = 0; i < nchar; i++)
for(j = 0; j < nchar; j++){
if(i==j){
M[i+j*nchar]=fr[j]+(1.0-fr[j])*emut;
if(deriv > 0) Mp[i+j*nchar]=(1-fr[j])*demu[0];
if(deriv==2 || deriv==4) Mpp[i+j*nchar]=(1-fr[j])*demu[1];
}
if(i!=j){
M[i+j*nchar]=fr[j]-fr[j]*emut;
if(deriv > 0) Mp[i+j*nchar]=-fr[j]*demu[0];
if(deriv==2 || deriv==4) Mpp[i+j*nchar]=-fr[j]*demu[1];
}
}
}
void pF81ma_gen(double t, int nchar, double *fr, double *mu, double *Lam,
int deriv, double alpha, double *M, double *Mp, double *Mpp){
pF81ma(t,nchar,fr,*mu,deriv,alpha,M,Mp,Mpp);
}
int is_transitionf(int i, int j){
return((i==0 && j==2) || (i==2 && j==0) || (i==1 && j==3) || (i==3 && j==1));
}
double muF84f(double K, double *fr){
double mu = 0.0;
int l;
/* printf("%f\n",K); */
for(l = 0; l < 4; l++) mu += fr[l]*(1.0-fr[l]);
mu += 2*K*( fr[0]*fr[2]/(fr[0]+fr[2]) + fr[1]*fr[3]/(fr[1]+fr[3]) );
return(1.0/mu);
}
void pF84m(double t, double mu, double K, double *fr, int deriv,
double *M, double *Mp, double *Mpp){
int i,j;
double Pr,Py,Pj,muK,emuKt,emut;
Pr=fr[0]+fr[2];
Py=fr[1]+fr[3];
muK=mu*(K+1); emuKt=exp(-muK*t); emut=exp(-mu*t);
for(j = 0; j < 4; j++){
if (j == 0 || j == 2) Pj=Pr;
if (j == 1 || j == 3) Pj=Py;
for(i = 0; i < 4; i++){
if(i==j){
M[i+j*4]=fr[j]+fr[j]*(1/Pj-1)*emut+(1-fr[j]/Pj)*emuKt;
if(deriv==1 || deriv==2)
Mp[i+j*4]=-mu*fr[j]*(1/Pj-1)*emut-muK*(1-fr[j]/Pj)*emuKt;
if(deriv==2)
Mpp[i+j*4]=mu*mu*fr[j]*(1/Pj-1)*emut + muK*muK*(1-fr[j]/Pj)*emuKt;
}
if(i!=j){
if(is_transitionf(i,j)){
M[i+j*4]=fr[j]+fr[j]*(1/Pj-1)*emut-(fr[j]/Pj)*emuKt;
if(deriv==1 || deriv==2)
Mp[i+j*4]=-mu*fr[j]*(1/Pj-1)*emut+muK*(fr[j]/Pj)*emuKt;
if(deriv==2)
Mpp[i+j*4]=mu*mu*fr[j]*(1/Pj-1)*emut-muK*muK*(fr[j]/Pj)*emuKt;
}
else{
M[i+j*4]=fr[j]*(1.0-emut);
if(deriv==1 || deriv==2) Mp[i+j*4] = mu*fr[j]*emut;
if(deriv==2) Mpp[i+j*4]=-mu*mu*fr[j]*emut;
}
}
}
}
}
void pF84m_gen(double t, int nchar, double *fr, double *mu, double *K,
int deriv, double *M, double *Mp, double *Mpp){
pF84m(t,*mu,*K,fr,deriv,M,Mp,Mpp);
}
void pF84ma(double t, double mu, double K, double *fr, int deriv, double alpha,
double *M, double *Mp, double *Mpp){
int i,j;
double Pr,Py,Pj,emuKt,emut,demuK[2],demu[2];
Pr=fr[0]+fr[2];
Py=fr[1]+fr[3];
emut=int_elamrt(t,-1.0*mu,alpha,deriv,demu);
emuKt=int_elamrt(t,-1.0*mu*(K+1),alpha,deriv,demuK);
/* if (deriv==4) printf("%f %f\n",emut,emuKt); */
for(j = 0; j < 4; j++){
if (j == 0 || j == 2) Pj=Pr;
if (j == 1 || j == 3) Pj=Py;
for(i = 0; i < 4; i++){
if(i==j){
M[i+j*4]=fr[j]+fr[j]*(1/Pj-1)*emut+(1-fr[j]/Pj)*emuKt;
if(deriv > 0)
Mp[i+j*4]=fr[j]*(1/Pj-1)*demu[0]+(1-fr[j]/Pj)*demuK[0];
if(deriv==2 || deriv==4)
Mpp[i+j*4]=fr[j]*(1/Pj-1)*demu[1]+(1-fr[j]/Pj)*demuK[1];
}
if(i!=j){
if(is_transitionf(i,j)){
M[i+j*4]=fr[j]+fr[j]*(1/Pj-1)*emut-(fr[j]/Pj)*emuKt;
if(deriv > 0)
Mp[i+j*4]=fr[j]*(1/Pj-1)*demu[0]-(fr[j]/Pj)*demuK[0];
if(deriv==2 || deriv==4)
Mpp[i+j*4]=fr[j]*(1/Pj-1)*demu[1]-(fr[j]/Pj)*demuK[1];
}
else{
M[i+j*4]=fr[j]-fr[j]*emut;
if(deriv > 0) Mp[i+j*4] = -fr[j]*demu[0];
if(deriv==2 || deriv==4) Mpp[i+j*4]=-fr[j]*demu[1];
}
}
}
}
}
void pF84ma_gen(double t, int nchar, double *fr, double *mu, double *K,
int deriv, double alpha, double *M, double *Mp, double *Mpp){
pF84ma(t,*mu,*K,fr,deriv,alpha,M,Mp,Mpp);
}
double tt2K(double R, double *fr){
double pY,pR,K;
pY=fr[1]+fr[3]; pR=fr[0]+fr[2];
K=(R*pR*pY-fr[0]*fr[2]-fr[1]*fr[3])/(fr[0]*fr[2]/pR+fr[1]*fr[3]/pY);
return(K);
}
double K2tt(double K, double *fr){
double pY,pR,R;
pY=fr[1]+fr[3]; pR=fr[0]+fr[2];
R=K*(fr[0]*fr[2]/pR+fr[1]*fr[3]/pY)/(pR*pY) +
(fr[0]*fr[2]+fr[1]*fr[3])/(pR*pY);
return(R);
}
double muHKYf(double kappa, double *fr){
double mu;
mu=2.0*(kappa*(fr[0]*fr[2]+fr[1]*fr[3])+(fr[0]+fr[2])*(fr[1]+fr[3]));
return(1.0/mu);
}
void pHKYm(double t, int nchar, double *fr, double mu, double kappa,
int deriv, double *M, double *Mp, double *Mpp){
int i,j;
double Pr,Py,A,emuAt,emut,muA,Pj;
Pr=fr[0]+fr[2];
Py=fr[1]+fr[3];
emut=exp(-mu*t);
for(j = 0; j < 4; j++){
if (j == 0 || j == 2) Pj=Pr;
if (j == 1 || j == 3) Pj=Py;
A = 1+Pj*(kappa-1); muA=mu*A;
emuAt=exp(-muA*t);
for(i = 0; i < 4; i++){
if(i==j){
M[i+j*4] = fr[j]+fr[j]*(1/Pj-1)*emut+(1-fr[j]/Pj)*emuAt;
if(deriv==1 || deriv==2)
Mp[i+j*4] = -mu*fr[j]*(1/Pj-1)*emut-muA*(1-fr[j]/Pj)*emuAt;
if(deriv==2)
Mpp[i+j*4] = mu*mu*fr[j]*(1/Pj-1)*emut+muA*muA*(1-fr[j]/Pj)*emuAt;
}
if(i!=j){
if(is_transitionf(i,j)){
M[i+j*4] = fr[j]+fr[j]*(1/Pj-1)*emut-fr[j]*emuAt/Pj;
if(deriv==1 || deriv==2)
Mp[i+j*4] = -mu*fr[j]*(1/Pj-1)*emut+muA*fr[j]*emuAt/Pj;
if(deriv==2)
Mpp[i+j*4] = mu*mu*fr[j]*(1/Pj-1)*emut-muA*muA*fr[j]*emuAt/Pj;
}
else{
M[i+j*4] = fr[j]-fr[j]*emut;
if(deriv==1 || deriv==2) Mp[i+j*4] = mu*fr[j]*emut;
if(deriv==2) Mpp[i+j*4] = -mu*mu*fr[j]*emut;
}
}
}}
}
void pHKYm_gen(double t, int nchar, double *fr, double *mu, double *kappa,
int deriv, double *M, double *Mp, double *Mpp){
pHKYm(t,nchar,fr,*mu,*kappa,deriv,M,Mp,Mpp);
}
void pHKYma(double t, int nchar, double *fr, double mu, double kappa,
int deriv, double alpha, double *M, double *Mp, double *Mpp){
int i,j;
double Pr,Py,A,emuAt,demuA[2],emut,demu[2],muA,Pj;
Pr=fr[0]+fr[2];
Py=fr[1]+fr[3];
emut=int_elamrt(t,-1.0*mu,alpha,deriv,demu);
for(j = 0; j < 4; j++){
if (j == 0 || j == 2) Pj=Pr;
if (j == 1 || j == 3) Pj=Py;
A = 1+Pj*(kappa-1); muA=mu*A;
emuAt=int_elamrt(t,-1.0*muA,alpha,deriv,demuA);
for(i = 0; i < 4; i++){
if(i==j){
M[i+j*4] = fr[j]+fr[j]*(1/Pj-1)*emut+(1-fr[j]/Pj)*emuAt;
if(deriv > 0)
Mp[i+j*4] = fr[j]*(1/Pj-1)*demu[0]+(1-fr[j]/Pj)*demuA[0];
if(deriv==2 || deriv==4)
Mpp[i+j*4] = fr[j]*(1/Pj-1)*demu[1]+(1-fr[j]/Pj)*demuA[1];
}
if(i!=j){
if(is_transitionf(i,j)){
M[i+j*4] = fr[j]+fr[j]*(1/Pj-1)*emut-fr[j]*emuAt/Pj;
if(deriv > 0)
Mp[i+j*4] = fr[j]*(1/Pj-1)*demu[0]-fr[j]*demuA[0]/Pj;
if(deriv==2 || deriv==4)
Mpp[i+j*4] = fr[j]*(1/Pj-1)*demu[1]-fr[j]*demuA[1]/Pj;
}
else{
M[i+j*4] = fr[j]-fr[j]*emut;
if(deriv > 0) Mp[i+j*4] = -fr[j]*demu[0];
if(deriv==2 || deriv==4) Mpp[i+j*4] = -fr[j]*demu[1];
}
}
}}
}
void pHKYma_gen(double t, int nchar, double *fr, double *mu, double *kappa,
int deriv, double alpha, double *M, double *Mp, double *Mpp){
pHKYma(t,nchar,fr,*mu,*kappa,deriv,alpha,M,Mp,Mpp);
}
double tt2kappa(double R, double *fr){
double pR,pY;
pR=fr[0]+fr[2];
pY=fr[1]+fr[3];
return(R*pR*pY/(fr[0]*fr[2]+fr[1]*fr[3]));
}
double kappa2tt(double kappa, double *fr){
double pR,pY;
pR=fr[0]+fr[2];
pY=fr[1]+fr[3];
return(kappa*(fr[0]*fr[2]+fr[1]*fr[3])/(pR*pY));
}
void rmnegpv(int m, double *pv, int incp){
int i,mi;
double sum=0.0;
mi=m*incp;
for(i=0; i<mi; i+=incp)
if(pv[i] < 0.0)
pv[i] = 0.0;
else
sum += pv[i];
for(i=0; i<mi; i+=incp) pv[i] /= sum;
}
void rmnegtrans(int m, double *P){
int i,j;
for(i=0; i<m; i++)
for(j=0; j<m; j++)
if(P[i+j*m] < 0.0){
rmnegpv(m,&P[i],m);
break;
}
return;
}
void Q2frWLam(int nbin, double *Q, double *fr, double *W, double *Lam){
/* Q = Pi^(-1) W Lam W'
* decomposition used to obtain P(t) as
* P(t) = Pi^(-1) W exp(Lam t) W'
* where W is determined from eigen-decomp
* Pi^(1/2) Q Pi^(-1/2) = U' Lam U, W = (Pi)^(1/2) U'
* Assumes Q corresponds to GTR model
*/
int i,j;
double R[400], Offdiag[400], mu;
/* pi_j = [1 + sum_i neq j Q_ji/Q_ij]**(-1) */
for(j = 0; j < nbin; j++){
fr[j] = 1.0;
for(i = 0; i < nbin; i++)
if (j != i) fr[j] += Q[j+i*nbin]/Q[i+j*nbin];
fr[j] = 1.0 / fr[j];
}
for(i = 0; i < nbin; i++)
for(j = 0; j < nbin; j++) /* R = Pi^(1/2) Q Pi^(-1/2) */
R[i+j*nbin] = sqrt(fr[i])*Q[i+j*nbin]/sqrt(fr[j]);
/* eigen-decomp of Pi^(1/2) Q Pi^(-1/2) */
eigenRealSym(R, nbin, Lam, Offdiag);
for(i = 0; i < nbin; i++){ /* W = (Pi*)^(1/2) U' */
for(j = 0; j < nbin; j++)
W[i+j*nbin] = R[j+i*nbin] * sqrt(fr[i]);
}
/* rescale eigenvalues so that -sum_i pi_i Q_ii = 1 */
mu = 0.0;
for(i = 0; i < nbin; i++)
for(j = 0; j < nbin; j++) mu -= Lam[j]*W[i+j*nbin]*W[i+j*nbin];
for(i = 0; i < nbin; i++) Lam[i] /= mu;
}
void pGTRm(double t, int nchar, double *fr, double *W, double *Lam,
int deriv, double *M, double *Mp, double *Mpp){
int i,j,k;
double w;
for(i = 0; i < nchar*nchar; i++) M[i]=0.0;
if(deriv==1 || deriv==2) for(i = 0; i < nchar*nchar; i++) Mp[i]=0.0;
if(deriv==2) for(i = 0; i < nchar*nchar; i++) Mpp[i]=0.0;
for(i = 0; i < nchar; i++)
for(j = 0; j < nchar; j++){
for(k = 0; k < nchar; k++){
w=W[i + k*nchar] * W[j+ k*nchar] * exp(Lam[k]*t);
M[i+j*nchar] += w;
if(deriv==1 || deriv==2) Mp[i+j*nchar] += Lam[k]*w;
if(deriv==2) Mpp[i+j*nchar] += Lam[k]*Lam[k]*w;
}
M[i+j*nchar] /= fr[i];
if(deriv==1 || deriv==2) Mp[i+j*nchar] /= fr[i];
if(deriv==2) Mpp[i+j*nchar] /= fr[i];
}
rmnegtrans(nchar,M);
}
void pGTRma(double t, int nchar, double *fr, double *W, double *Lam,
int deriv, double alpha, double *M, double *Mp, double *Mpp){
int i,j,k;
double dt,de[2];
for(i = 0; i < nchar*nchar; i++) M[i]=0.0;
if(deriv > 0) for(i = 0; i < nchar*nchar; i++) Mp[i]=0.0;
if(deriv==2 || deriv==4) for(i = 0; i < nchar*nchar; i++) Mpp[i]=0.0;
for(i = 0; i < nchar; i++)
for(j = 0; j < nchar; j++){
for(k = 0; k < nchar; k++){
dt=int_elamrt(t,Lam[k],alpha,deriv,de);
M[i+j*nchar] += W[i + k*nchar] * W[j+ k*nchar] * dt;
if(deriv > 0){
Mp[i+j*nchar] += W[i + k*nchar] * W[j+ k*nchar] * de[0];
}
if(deriv==2 || deriv==4)
Mpp[i+j*nchar] += W[i + k*nchar] * W[j+ k*nchar] * de[1];
}
M[i+j*nchar] /= fr[i];
if(deriv > 0) Mp[i+j*nchar] /= fr[i];
if(deriv==2 || deriv==4) Mpp[i+j*nchar] /= fr[i];
}
rmnegtrans(nchar,M);
}
int subst_model_setparam(int nchar, int smodel, double *fr, double *Qk,
double **W, double **Lam){
/* IN:
* nchar - number of character states
* smodel - 0:JC, 1:F81, 2:F84, 3:GTR, 4:PAM, 5:JTT, 6:WAG, 7:mtREV24, 8:HKY,
* 9:LG
* Qk - rate matrix for GTR, ttratio, K or Kappa for F84, HKY, < 0 => ttratio;
* not used otherwise.
*
* IN/OUT:
* fr - frequencies; reset for JC, determined from Q for GTR
* if *fr < 0 for aa models, model-based frequencies used
* OUT:
* W,Lam - contain parameters for substitution model
*
* RETURN:
* 2,1 or 0 according to whether both W and Lam were allocated, just W or
* neither
*/
int allocWLam=0,aamodel=0,i;
double *fro,*Wo,*Lamo;
if (smodel==4 || smodel==5 || smodel==6 || smodel==7 || smodel==9) aamodel=1;
if (smodel==0){ /* JC */
for(i = 0; i < nchar; i++) fr[i] = 1.0/nchar;
}
if (smodel==1){ /* F81 */
*W=(double *) malloc((size_t) sizeof(double));
**W=muF81f(nchar,fr);
/* for(i = 0; i < nchar; i++) printf("%f\n",fr[i]); printf("\n"); */
/* printf("%f\n",**W); */
allocWLam=1;
}
if (smodel==2){ /* F84 */
*W=(double *) malloc((size_t) sizeof(double));
*Lam=(double *) malloc((size_t) sizeof(double));
/* printf("%.16e\n",*Qk); */
/* for(i = 0; i < nchar; i++) printf("%f\n",fr[i]); printf("\n"); */
**Lam=*Qk;
if(*Qk < 0)
**Lam=tt2K(-(*Qk),fr);
**W= muF84f(**Lam,fr);
/* printf("%f %f\n",**W,**Lam); */
allocWLam=2;
}
if (smodel==3){ /* GTR */
*W = (double *) malloc((size_t) nchar*nchar*sizeof(double));
*Lam = (double *) malloc((size_t) nchar*sizeof(double));
Q2frWLam(nchar,Qk,fr,*W,*Lam);
allocWLam=2;
}
if (smodel==4){ /* PAM */
Wo = pamprobmat; Lamo = pameigmat;
}
if (smodel==5){ /* JTT */
Wo = jttprobmat; Lamo = jtteigmat;
}
if (smodel==6){ /* WAG */
Wo = wagprobmat; Lamo = wageigmat;
}
if (smodel==7){ /* mtREV24 */
Wo = mtREV24probmat; Lamo = mtREV24eigmat;
}
if (smodel==8){ /* HKY */
*W=(double *) malloc((size_t) sizeof(double));
*Lam=(double *) malloc((size_t) sizeof(double));
**Lam=*Qk;
if(*Qk < 0)
**Lam=tt2kappa(-(*Qk),fr);
**W = muHKYf(**Lam,fr);
allocWLam=2;
}
if(smodel==9){ /* LG */
Wo = lgprobmat; Lamo = lgeigmat;
}
if (aamodel){ /* amino acid models */
if (*fr > 0){ /* different frequencies than for model */
*W = (double *) malloc((size_t) nchar*nchar*sizeof(double));
*Lam = (double *) malloc((size_t) nchar*sizeof(double));
fro = (double *) malloc((size_t) nchar*sizeof(double));
makeprotfreqs(fro,Wo,Lamo);
memcpy(*W,Wo,(size_t) nchar*nchar*sizeof(double));
memcpy(*Lam,Lamo,(size_t) nchar*sizeof(double));
chQ_freq(nchar,*W,*Lam,fr,fro);
allocWLam=2;
}
else{
*W=Wo; *Lam=Lamo;
makeprotfreqs(fr,*W,*Lam);
}
}
return(allocWLam);
}
int subst_model_setpm(int smodel,
void (*(*pm))(double t, int nchar, double *fr,
double *W, double *Lam, int deriv,
double *M, double *Mp,
double *Mpp)){
/* IN:
* smodel - 0:JC, 1:F81, 2:F84, 3:GTR, 4:PAM, 5:JTT, 6:WAG, 7:mtREV24, 8:HKY,
* 9:LG
*
* OUT:
* pm - transition matrix function
*/
switch(smodel){
case 0:
*pm=&pJCm_gen;
break;
case 1:
*pm=&pF81m_gen;
break;
case 2:
*pm=&pF84m_gen; ;
break;
case 3: case 4: case 5: case 6: case 7: case 9:
*pm=&pGTRm;
break;
case 8:
*pm=&pHKYm_gen;
break;
}
return(0);
}
int subst_model_setpma(int smodel,
void (*(*pm))(double t, int nchar, double *fr,
double *W, double *Lam, int deriv,
double alpha,
double *M, double *Mp, double *Mpp)){
/* IN:
* smodel - 0:JC, 1:F81, 2:F84, 3:GTR, 4:PAM, 5:JTT, 6:WAG, 7:mtREV24, 8:HKY,
* 9:LG
*