-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluateGenericSpecial.c
3309 lines (2587 loc) · 113 KB
/
evaluateGenericSpecial.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
/**
* PLL (version 1.0.0) a software library for phylogenetic inference
* Copyright (C) 2013 Tomas Flouri and Alexandros Stamatakis
*
* Derived from
* RAxML-HPC, a program for sequential and parallel estimation of phylogenetic
* trees by Alexandros Stamatakis
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* For any other enquiries send an Email to Tomas Flouri
* Tomas.Flouri@h-its.org
*
* When publishing work that uses PLL please cite PLL
*
* @file evaluateGenericSpecial.c
*
* @brief Functions for computing the log likelihood at a given branch of the tree (i.e. a virtual root that is placed at this branch)
*/
#include "mem_alloc.h"
#include "systypes.h"
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include "pll.h"
#include "pllInternal.h"
#ifdef __MIC_NATIVE
#include "mic_native.h"
#endif
/* the set of functions in here computes the log likelihood at a given branch (the virtual root of a tree) */
/* includes for using SSE3 intrinsics */
#ifdef __SSE3
#if defined(__ARM_NEON)
#include "sse2neon.h"
#else
#include <xmmintrin.h>
#include <pmmintrin.h>
#endif
#endif
/** @defgroup evaluateLikelihoodGroup Likelihood evaluation
This set of functions deals with the evaluation of likelihood for the current topology
*/
/* below are the function headers for unreadeble highly optimized versions of the above functions
for DNA and protein data that also use SSE3 intrinsics and implement some memory saving tricks.
The actual functions can be found at the end of this source file.
All other likelihood function implementation files:
newviewGenericSpacial.c
makenewzSpecial.c
evaluatePartialGenericSpecial.c
are also structured like this
To decide which set of function implementations to use you will have to undefine or define _OPTIMIZED_FUNCTIONS
in the Makefile
*/
#if (defined(__SSE3) || defined(__AVX))
static double evaluateGTRGAMMAPROT_LG4(int *ex1, int *ex2, int *wptr,
double *x1, double *x2,
double *tipVector[4],
unsigned char *tipX1, int n, double *diagptable, const pllBoolean fastScaling,
double * lg4_weights);
/* GAMMA for proteins with memory saving */
static double evaluateGTRGAMMAPROT_GAPPED_SAVE (const pllBoolean fastScaling, int *ex1, int *ex2, int *wptr,
double *x1, double *x2,
double *tipVector,
unsigned char *tipX1, int n, double *diagptable,
double *x1_gapColumn, double *x2_gapColumn, unsigned int *x1_gap, unsigned int *x2_gap);
/* GAMMA for proteins */
static double evaluateGTRGAMMAPROT (const pllBoolean fastScaling, int *ex1, int *ex2, int *wptr,
double *x1, double *x2,
double *tipVector,
unsigned char *tipX1, int n, double *diagptable);
/* CAT for proteins */
static double evaluateGTRCATPROT (const pllBoolean fastScaling, int *ex1, int *ex2, int *cptr, int *wptr,
double *x1, double *x2, double *tipVector,
unsigned char *tipX1, int n, double *diagptable_start);
/* CAT for proteins with memory saving */
static double evaluateGTRCATPROT_SAVE (const pllBoolean fastScaling, int *ex1, int *ex2, int *cptr, int *wptr,
double *x1, double *x2, double *tipVector,
unsigned char *tipX1, int n, double *diagptable_start,
double *x1_gapColumn, double *x2_gapColumn, unsigned int *x1_gap, unsigned int *x2_gap);
/* analogous DNA fuctions */
static double evaluateGTRCAT_SAVE (const pllBoolean fastScaling, int *ex1, int *ex2, int *cptr, int *wptr,
double *x1_start, double *x2_start, double *tipVector,
unsigned char *tipX1, int n, double *diagptable_start,
double *x1_gapColumn, double *x2_gapColumn, unsigned int *x1_gap, unsigned int *x2_gap);
static double evaluateGTRGAMMA_GAPPED_SAVE(const pllBoolean fastScaling, int *ex1, int *ex2, int *wptr,
double *x1_start, double *x2_start,
double *tipVector,
unsigned char *tipX1, const int n, double *diagptable,
double *x1_gapColumn, double *x2_gapColumn, unsigned int *x1_gap, unsigned int *x2_gap);
static double evaluateGTRGAMMA(const pllBoolean fastScaling, int *ex1, int *ex2, int *wptr,
double *x1_start, double *x2_start,
double *tipVector,
unsigned char *tipX1, const int n, double *diagptable);
static double evaluateGTRCAT (const pllBoolean fastScaling, int *ex1, int *ex2, int *cptr, int *wptr,
double *x1_start, double *x2_start, double *tipVector,
unsigned char *tipX1, int n, double *diagptable_start);
#endif
#if (defined(__AVX) || defined(__SSE3))
static double evaluateGTRGAMMA_BINARY(int *ex1, int *ex2, int *wptr,
double *x1_start, double *x2_start,
double *tipVector,
unsigned char *tipX1, const int n, double *diagptable, const pllBoolean fastScaling);
static double evaluateGTRCAT_BINARY (int *ex1, int *ex2, int *cptr, int *wptr,
double *x1_start, double *x2_start, double *tipVector,
unsigned char *tipX1, int n, double *diagptable_start, const pllBoolean fastScaling);
#endif
/*
global variables of pthreads version, reductionBuffer is the global array
that is used for implementing deterministic reduction operations, that is,
the total log likelihood over the partial log lieklihoods for the sites that each thread has computed
NumberOfThreads is just the number of threads.
Note the volatile modifier here, that guarantees that the compiler will not do weird optimizations
rearraengements of the code accessing those variables, because it does not know that several concurrent threads
will access those variables simulatenously
UPDATE: reductionBuffer is now merged with globalResult
*/
/* a pre-computed 32-bit integer mask */
extern const unsigned int mask32[32];
/* the function below computes the P matrix from the decomposition of the Q matrix and the respective rate categories for a single partition */
/** @brief Compute the diagonal of P matrix for a specific edge
This function computes the diagonal of P matrix for a branch of length \a z
from the decomposition of the Q matrix specified in \a EIGN and the respective
rate categories \a rptr for a single partition. The diagonal is then stored in
\a diagptable.
@param z Length of edge
@param states Number of states
@param numberOfCategories Number of categories in the rate heterogeneity rate arrays
@param rptr Rate heterogeneity rate arrays
@param EIGN Eigenvalues
@param diagptable Where to store the resulting P matrix
*/
static void calcDiagptable(const double z, const int states, const int numberOfCategories, const double *rptr, const double *EIGN, double *diagptable)
{
int
i,
l;
double
lz,
*lza = (double *)rax_malloc(sizeof(double) * states);
/* transform the root branch length to the log and check if it is not too small */
if (z < PLL_ZMIN)
lz = log(PLL_ZMIN);
else
lz = log(z);
/* do some pre-computations to avoid redundant computations further below */
for(i = 1; i < states; i++)
lza[i] = EIGN[i] * lz;
/* loop over the number of per-site or discrete gamma rate categories */
for(i = 0; i < numberOfCategories; i++)
{
/*
diagptable is a pre-allocated array of doubles that stores the P-Matrix
the first entry is always 1.0
*/
diagptable[i * states] = 1.0;
/* compute the P matrix for all remaining states of the model */
for(l = 1; l < states; l++)
diagptable[i * states + l] = exp(rptr[i] * lza[l]);
}
rax_free(lza);
}
/** @brief Compute the diagonal of P matrix for a specific edge for the LG4 model
This function computes the diagonal of P matrix for a branch of length \a z
from the decomposition of the 4 LG4 Q matrices specified in \a EIGN and the respective
rate categories \a rptr for a single partition. The diagonal is then stored in
\a diagptable.
@param z
Length of edge
@param states
Number of states
@param numberOfCategories
Number of categories in the rate heterogeneity rate arrays
@param rptr
Rate heterogeneity rate arrays
@param EIGN
Eigenvalues of the 4 Q matrices
@param diagptable
Where to store the resulting P matrix
@param numStates
Number of states
*/
static void calcDiagptableFlex_LG4(double z, int numberOfCategories, double *rptr, double *EIGN[4], double *diagptable, const int numStates)
{
int
i,
l;
double
lz;
assert(numStates <= 64);
if (z < PLL_ZMIN)
lz = log(PLL_ZMIN);
else
lz = log(z);
for(i = 0; i < numberOfCategories; i++)
{
diagptable[i * numStates + 0] = 1.0;
for(l = 1; l < numStates; l++)
diagptable[i * numStates + l] = exp(rptr[i] * EIGN[i][l] * lz);
}
}
static void ascertainmentBiasSequence(unsigned char tip[32], int numStates)
{
assert(numStates <= 32 && numStates > 1);
switch(numStates)
{
case 2:
tip[0] = 1;
tip[1] = 2;
break;
case 4:
tip[0] = 1;
tip[1] = 2;
tip[2] = 4;
tip[3] = 8;
break;
default:
{
int
i;
for(i = 0; i < numStates; i++)
{
tip[i] = i;
//printf("%c ", inverseMeaningPROT[i]);
}
//printf("\n");
}
break;
}
}
static double evaluateCatAsc(int *ex1, int *ex2,
double *x1, double *x2,
double *tipVector,
unsigned char *tipX1, int n, double *diagptable, const int numStates)
{
double
exponent,
sum = 0.0,
unobserved,
term,
*left,
*right;
int
i,
l;
unsigned char
tip[32];
ascertainmentBiasSequence(tip, numStates);
if(tipX1)
{
for (i = 0; i < n; i++)
{
left = &(tipVector[numStates * tip[i]]);
right = &(x2[i * numStates]);
term = 0.0;
for(l = 0; l < numStates; l++)
term += left[l] * right[l] * diagptable[l];
/* assumes that pow behaves as expected/specified for underflows
from the man page:
If result underflows, and is not representable,
a range error occurs and 0.0 is returned.
*/
exponent = pow(PLL_MINLIKELIHOOD, (double)ex2[i]);
unobserved = fabs(term) * exponent;
#ifdef _DEBUG_ASC
if(ex2[i] > 0)
{
printf("s %d\n", ex2[i]);
assert(0);
}
#endif
sum += unobserved;
}
}
else
{
for (i = 0; i < n; i++)
{
term = 0.0;
left = &(x1[i * numStates]);
right = &(x2[i * numStates]);
for(l = 0; l < numStates; l++)
term += left[l] * right[l] * diagptable[l];
/* assumes that pow behaves as expected/specified for underflows
from the man page:
If result underflows, and is not representable,
a range error occurs and 0.0 is returned.
*/
exponent = pow(PLL_MINLIKELIHOOD, (double)(ex1[i] + ex2[i]));
unobserved = fabs(term) * exponent;
#ifdef _DEBUG_ASC
if(ex2[i] > 0 || ex1[i] > 0)
{
printf("s %d %d\n", ex1[i], ex2[i]);
assert(0);
}
#endif
sum += unobserved;
}
}
return sum;
}
static double evaluateGammaAsc(int* ex1, int* ex2,
double* x1, double* x2,
double* tipVector,
unsigned char* tipX1, int n, double* diagptable, const int numStates)
{
double
exponent,
sum = 0.0,
unobserved,
term;
int
i,
j,
l;
const int
gammaStates = numStates * 4;
unsigned char
tip[32];
ascertainmentBiasSequence(tip, numStates);
if (tipX1)
{
for (i = 0; i < n; i++) {
double* left = tipVector + numStates * tip[i];
for (j = 0, term = 0.0; j < 4; j++) {
double* right = x2 + gammaStates * i + numStates * j;
for (l = 0; l < numStates; l++) {
term += left[l] * right[l] * diagptable[j * numStates + l];
}
}
/* assumes that pow behaves as expected/specified for underflows
from the man page:
If result underflows, and is not representable,
a range error occurs and 0.0 is returned.
*/
exponent = pow(PLL_MINLIKELIHOOD, (double)ex2[i]);
unobserved = fabs(term) * exponent;
#ifdef _DEBUG_ASC
if (ex2[i] > 0)
{
printf("s %d\n", ex2[i]);
assert(0);
}
#endif
sum += unobserved;
}
}
else
{
for (i = 0; i < n; i++) {
for (j = 0, term = 0.0; j < 4; j++) {
double* left = x1 + gammaStates * i + numStates * j;
double* right = x2 + gammaStates * i + numStates * j;
for (l = 0; l < numStates; l++) {
term += left[l] * right[l] * diagptable[j * numStates + l];
}
}
/* assumes that pow behaves as expected/specified for underflows
from the man page:
If result underflows, and is not representable,
a range error occurs and 0.0 is returned.
*/
exponent = pow(PLL_MINLIKELIHOOD, (double)(ex1[i] + ex2[i]));
unobserved = fabs(term) * exponent;
#ifdef _DEBUG_ASC
if (ex2[i] > 0 || ex1[i] > 0)
{
printf("s %d %d\n", ex1[i], ex2[i]);
assert(0);
}
#endif
sum += unobserved;
}
}
return sum;
}
/** @ingroup evaluateLikelihoodGroup
@brief A generic (and slow) implementation of log likelihood evaluation of a tree using the GAMMA model of rate heterogeneity
Computes the log likelihood of the topology for a specific partition, assuming
that the GAMMA model of rate heterogeneity is used. The likelihood is computed at
a virtual root placed at an edge whose two end-points (nodes) have the conditional
likelihood vectors \a x1 and \a x2.
Furthermore, if \a getPerSiteLikelihoods is set to \b PLL_TRUE, then the log
likelihood for each site is also computed and stored at the corresponding position
in the array \a perSiteLikelihoods.
@param fastScaling
If set to \b PLL_FALSE, then the likelihood of each site is also multiplied by \a log(PLL_MINLIKELIHOOD) times the number
of times it has been scaled down
@param ex1
An array that holds how many times a site has been scaled and points at the entries for node \a p. This
parameter is used if \a fastScaling is set to \b PLL_FALSE.
@param ex2
An array that holds how many times a site has been scaled and points at the entries for node \a q. This
parameter is used if \a fastScaling is set to \b PLL_TRUE.
@param wptr
Array holding the weight for each site in the compressed partition alignment
@param x1_start
Conditional likelihood vectors for one of the two end-points of the specific edge for which we are evaluating the likelihood
@param x2_start
Conditional likelihood vectors for the other end-point of the specific edge for which we are evaluating the likelihood
@param tipVector
Precomputed table where the number of rows is equal to the number of possible basepair characters for the current data
type, i.e.16 for DNA and 23 for AA, and each rows contains \a states elements each of which contains transition
probabilities computed from the eigenvectors of the decomposed Q matrix.
@param tipX1
If one of the two end-points (nodes) of the specific edge (for which we are evaluating the likelihood) is a tip, then
this holds a pointer to the sequence data (basepairs) already converted in the internal integer representation, and \a x2
holds the conditional likelihood vectors for the internal node.
@param n
Number of sites for which we are doing the evaluation. For the single-thread version this is the
number of sites in the current partition, for multi-threads this is the number of sites assigned
to the running thread from the current partition.
@param diagptable
Start of the array that contains the P-Matrix diagonal of the specific edge for which we are
evaluating the likehood, and for each category of the GAMMA model
@param states
Number of states (4 for DNA, 20 for AA)
@param perSiteLikelihoods
Array to store per-site log likelihoods if \a getPerSiteLikelihoods is set to \b PLL_TRUE
@param getPerSiteLikelihoods
If set to \b PLL_TRUE then per-site log likelihoods are also computed and stored in \a perSiteLikelihoods
@return
The evaluated log likelihood of the tree topology
*/
static double evaluateGAMMA_FLEX(const pllBoolean fastScaling, int *ex1, int *ex2, int *wptr,
double *x1_start, double *x2_start,
double *tipVector,
unsigned char *tipX1, const int n, double *diagptable, const int states, double *perSiteLikelihoods, pllBoolean getPerSiteLikelihoods)
{
double
sum = 0.0,
term,
*x1,
*x2;
int
i,
j,
k;
/* span is the offset within the likelihood array at an inner node that gets us from the values
of site i to the values of site i + 1 */
const int
span = states * 4;
/* we distingusih between two cases here: one node of the two nodes defining the branch at which we put the virtual root is
a tip. Both nodes can not be tips because we do not allow for two-taxon trees ;-)
Nota that, if a node is a tip, this will always be tipX1. This is done for code simplicity and the flipping of the nodes
is done before when we compute the traversal descriptor.
*/
/* the left node is a tip */
if(tipX1)
{
/* loop over the sites of this partition */
for (i = 0; i < n; i++)
{
/* access pre-computed tip vector values via a lookup table */
x1 = &(tipVector[states * tipX1[i]]);
/* access the other(inner) node at the other end of the branch */
x2 = &(x2_start[span * i]);
/* loop over GAMMA rate categories, hard-coded as 4 in RAxML */
for(j = 0, term = 0.0; j < 4; j++)
/* loop over states and multiply them with the P matrix */
for(k = 0; k < states; k++)
term += x1[k] * x2[j * states + k] * diagptable[j * states + k];
/* take the log of the likelihood and multiply the per-gamma rate likelihood by 1/4.
Under the GAMMA model the 4 discrete GAMMA rates all have the same probability
of 0.25 */
if(!fastScaling)
term = log(0.25 * fabs(term)) + (ex2[i] * log(PLL_MINLIKELIHOOD));
else
term = log(0.25 * fabs(term));
/* if required get the per-site log likelihoods.
note that these are the plain per site log-likes, not
multiplied with the pattern weight value */
if(getPerSiteLikelihoods)
perSiteLikelihoods[i] = term;
sum += wptr[i] * term;
}
}
else
{
for (i = 0; i < n; i++)
{
/* same as before, only that now we access two inner likelihood vectors x1 and x2 */
x1 = &(x1_start[span * i]);
x2 = &(x2_start[span * i]);
for(j = 0, term = 0.0; j < 4; j++)
for(k = 0; k < states; k++)
term += x1[j * states + k] * x2[j * states + k] * diagptable[j * states + k];
if(!fastScaling)
term = log(0.25 * fabs(term)) + ((ex1[i] + ex2[i])*log(PLL_MINLIKELIHOOD));
else
term = log(0.25 * fabs(term));
if(getPerSiteLikelihoods)
perSiteLikelihoods[i] = term;
sum += wptr[i] * term;
}
}
return sum;
}
#if (defined(__SSE3) || defined(__AVX))
/** @ingroup evaluateLikelihoodGroup
@brief Memory saving version of the generic (and slow) implementation of log likelihood evaluation of a tree using the GAMMA model of rate heterogeneity
Computes the log likelihood of the topology for a specific partition, assuming
that the GAMMA model of rate heterogeneity is used and memory saving technique
is enabled. The likelihood is computed at a virtual root placed at an edge whose
two end-points (nodes) have the conditional likelihood vectors \a x1 and \a x2.
Furthermore, if \a getPerSiteLikelihoods is set to \b PLL_TRUE, then the log
likelihood for each site is also computed and stored at the corresponding position
in the array \a perSiteLikelihoods.
@param fastScaling
If set to \b PLL_FALSE, then the likelihood of each site is also multiplied by \a log(PLL_MINLIKELIHOOD) times the number
of times it has been scaled down
@param ex1
An array that holds how many times a site has been scaled and points at the entries for node \a p. This
parameter is used if \a fastScaling is set to \b PLL_FALSE.
@param ex2
An array that holds how many times a site has been scaled and points at the entries for node \a q. This
parameter is used if \a fastScaling is set to \b PLL_TRUE.
@param wptr
Array holding the weight for each site in the compressed partition alignment
@param x1_start
Conditional likelihood vectors for one of the two end-points of the specific edge for which we are evaluating the likelihood
@param x2_start
Conditional likelihood vectors for the other end-point of the specific edge for which we are evaluating the likelihood
@param tipVector
Precomputed table where the number of rows is equal to the number of possible basepair characters for the current data
type, i.e.16 for DNA and 23 for AA, and each rows contains \a states elements each of which contains transition
probabilities computed from the eigenvectors of the decomposed Q matrix.
@param tipX1
If one of the two end-points (nodes) of the specific edge (for which we are evaluating the likelihood) is a tip, then
this holds a pointer to the sequence data (basepairs) already converted in the internal integer representation, and \a x2
holds the conditional likelihood vectors for the internal node.
@param n
Number of sites for which we are doing the evaluation. For the single-thread version this is the
number of sites in the current partition, for multi-threads this is the number of sites assigned
to the running thread from the current partition.
@param diagptable
Start of the array that contains the P-Matrix diagonal of the specific edge for which we are
evaluating the likehood, and for each category of the GAMMA model
@param states
Number of states (4 for DNA, 20 for AA)
@param perSiteLikelihoods
Array to store per-site log likelihoods if \a getPerSiteLikelihoods is set to \b PLL_TRUE
@param getPerSiteLikelihoods
If set to \b PLL_TRUE then per-site log likelihoods are also computed and stored in \a perSiteLikelihoods
@param x1_gapColumn
@param x2_gapColumn
@param x1_gap
Gap bitvector for the left child node
@param x2_gap
Gap bitvector for the right child node
@return
The evaluated log likelihood of the tree topology
@todo
Document x1_gapColumn, x2_gapColumn, x1_gap, x2_gap and add a brief description of how this technique works
*/
static double evaluateGAMMA_FLEX_SAVE(const pllBoolean fastScaling, int *ex1, int *ex2, int *wptr,
double *x1_start, double *x2_start,
double *tipVector,
unsigned char *tipX1, const int n, double *diagptable, const int states, double *perSiteLikelihoods, pllBoolean getPerSiteLikelihoods,
double *x1_gapColumn, double *x2_gapColumn, unsigned int *x1_gap, unsigned int *x2_gap)
{
double
sum = 0.0,
term,
*x1,
*x2,
*x1_ptr = x1_start,
*x2_ptr = x2_start;
int
i,
j,
k;
/* span is the offset within the likelihood array at an inner node that gets us from the values
of site i to the values of site i + 1 */
const int
span = states * 4;
/* we distingusih between two cases here: one node of the two nodes defining the branch at which we put the virtual root is
a tip. Both nodes can not be tips because we do not allow for two-taxon trees ;-)
Nota that, if a node is a tip, this will always be tipX1. This is done for code simplicity and the flipping of the nodes
is done before when we compute the traversal descriptor.
*/
/* the left node is a tip */
if(tipX1)
{
/* loop over the sites of this partition */
for (i = 0; i < n; i++)
{
/* access pre-computed tip vector values via a lookup table */
x1 = &(tipVector[states * tipX1[i]]);
/* access the other(inner) node at the other end of the branch */
if(x2_gap[i / 32] & mask32[i % 32])
x2 = x2_gapColumn;
else
{
x2 = x2_ptr;
x2_ptr += span;
}
/* loop over GAMMA rate categories, hard-coded as 4 in RAxML */
for(j = 0, term = 0.0; j < 4; j++)
/* loop over states and multiply them with the P matrix */
for(k = 0; k < states; k++)
term += x1[k] * x2[j * states + k] * diagptable[j * states + k];
/* take the log of the likelihood and multiply the per-gamma rate likelihood by 1/4.
Under the GAMMA model the 4 discrete GAMMA rates all have the same probability
of 0.25 */
if(!fastScaling)
term = log(0.25 * fabs(term)) + (ex2[i] * log(PLL_MINLIKELIHOOD));
else
term = log(0.25 * fabs(term));
/* if required get the per-site log likelihoods.
note that these are the plain per site log-likes, not
multiplied with the pattern weight value */
if(getPerSiteLikelihoods)
perSiteLikelihoods[i] = term;
sum += wptr[i] * term;
}
}
else
{
for (i = 0; i < n; i++)
{
/* same as before, only that now we access two inner likelihood vectors x1 and x2 */
if(x1_gap[i / 32] & mask32[i % 32])
x1 = x1_gapColumn;
else
{
x1 = x1_ptr;
x1_ptr += span;
}
if(x2_gap[i / 32] & mask32[i % 32])
x2 = x2_gapColumn;
else
{
x2 = x2_ptr;
x2_ptr += span;
}
for(j = 0, term = 0.0; j < 4; j++)
for(k = 0; k < states; k++)
term += x1[j * states + k] * x2[j * states + k] * diagptable[j * states + k];
if(!fastScaling)
term = log(0.25 * fabs(term)) + ((ex1[i] + ex2[i])*log(PLL_MINLIKELIHOOD));
else
term = log(0.25 * fabs(term));
if(getPerSiteLikelihoods)
perSiteLikelihoods[i] = term;
sum += wptr[i] * term;
}
}
return sum;
}
#endif
/** @ingroup evaluateLikelihoodGroup
@brief A generic (and slow) implementation of log likelihood evaluation of a tree using the CAT model of rate heterogeneity
Computes the log likelihood of the topology for a specific partition, assuming
that the CAT model of rate heterogeneity is used. The likelihood is computed at
a virtual root placed at an edge whose two end-points (nodes) have the conditional
likelihood vectors \a x1 and \a x2.
Furthermore, if \a getPerSiteLikelihoods is set to \b PLL_TRUE, then the log
likelihood for each site is also computed and stored at the corresponding position
in the array \a perSiteLikelihoods.
@param fastScaling
If set to \b PLL_FALSE, then the likelihood of each site is also multiplied by \a log(PLL_MINLIKELIHOOD) times the number
of times it has been scaled down
@param ex1
An array that holds how many times a site has been scaled and points at the entries for node \a p. This
parameter is used if \a fastScaling is set to \b PLL_FALSE.
@param ex2
An array that holds how many times a site has been scaled and points at the entries for node \a q. This
parameter is used if \a fastScaling is set to \b PLL_TRUE.
@param cptr
Array holding the rate for each site in the compressed partition alignment
@param wptr
Array holding the weight for each site in the compressed partition alignment
@param x1
Conditional likelihood vectors for one of the two end-points of the specific edge for which we are evaluating the likelihood
@param x2
Conditional likelihood vectors for the other end-point of the specific edge for which we are evaluating the likelihood
@param tipVector
Precomputed table where the number of rows is equal to the number of possible basepair characters for the current data type,
i.e.16 for DNA and 23 for AA, and each rows contains \a states elements each of which contains transition probabilities
computed from the eigenvectors of the decomposed Q matrix.
@param tipX1
If one of the two end-points (nodes) of the specific edge (for which we are evaluating the likelihood) is a tip, then
this holds a pointer to the sequence data (basepairs) already converted in the internal integer representation, and \a x2
holds the conditional likelihood vectors for the internal node.
@param n
Number of sites for which we are doing the evaluation. For the single-thread version this is the number of sites in the
current partition, for multi-threads this is the number of sites assigned to the running thread from the current partition.
@param diagptable_start
Start of the array that contains the P-Matrix diagonal of the specific edge for which we are evaluating the likehood,
and for each category of the CAT model
@param states
Number of states (4 for DNA, 20 for AA)
@param perSiteLikelihoods
Array to store per-site log likelihoods if \a getPerSiteLikelihoods is set to \b PLL_TRUE
@param getPerSiteLikelihoods
If set to \b PLL_TRUE then per-site log likelihoods are also computed and stored in \a perSiteLikelihoods
@return
The evaluated log likelihood of the tree topology
*/
static double evaluateCAT_FLEX (const pllBoolean fastScaling, int *ex1, int *ex2, int *cptr, int *wptr,
double *x1, double *x2, double *tipVector,
unsigned char *tipX1, int n, double *diagptable_start, const int states, double *perSiteLikelihoods, pllBoolean getPerSiteLikelihoods)
{
double
sum = 0.0,
term,
*diagptable,
*left,
*right;
int
i,
l;
/* chosing between tip vectors and non tip vectors is identical in all flavors of this function ,regardless
of whether we are using CAT, GAMMA, DNA or protein data etc */
if(tipX1)
{
for (i = 0; i < n; i++)
{
/* same as in the GAMMA implementation */
left = &(tipVector[states * tipX1[i]]);
right = &(x2[states * i]);
/* important difference here, we do not have, as for GAMMA
4 P matrices assigned to each site, but just one. However those
P-Matrices can be different for the sites.
Hence we index into the precalculated P-matrices for individual sites
via the category pointer cptr[i]
*/
diagptable = &diagptable_start[states * cptr[i]];
/* similar to gamma, with the only difference that we do not integrate (sum)
over the discrete gamma rates, but simply compute the likelihood of the
site and the given P-matrix */
for(l = 0, term = 0.0; l < states; l++)
term += left[l] * right[l] * diagptable[l];
/* take the log */
if(!fastScaling)
term = log(fabs(term)) + (ex2[i] * log(PLL_MINLIKELIHOOD));
else
term = log(fabs(term));
/* if required get the per-site log likelihoods.
note that these are the plain per site log-likes, not
multiplied with the pattern weight value */
if(getPerSiteLikelihoods)
perSiteLikelihoods[i] = term;
/*
multiply the log with the pattern weight of this site.
The site pattern for which we just computed the likelihood may
represent several alignment columns sites that have been compressed
into one site pattern if they are exactly identical AND evolve under the same model,
i.e., form part of the same partition.
*/
sum += wptr[i] * term;
}
}
else
{
for (i = 0; i < n; i++)
{
/* as before we now access the likelihood arrayes of two inner nodes */
left = &x1[states * i];
right = &x2[states * i];
diagptable = &diagptable_start[states * cptr[i]];
for(l = 0, term = 0.0; l < states; l++)
term += left[l] * right[l] * diagptable[l];