-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProbabilityMatrixSet.h
804 lines (735 loc) · 27.9 KB
/
ProbabilityMatrixSet.h
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
#ifndef PROBABILITYMATRIXSET_H
#define PROBABILITYMATRIXSET_H
#include <vector>
#include <set>
#include "MatrixSize.h"
#include "TransitionMatrix.h"
#include "AlignedMalloc.h"
#include "CodonFrequencies.h"
#include "MathSupport.h"
#include "Exceptions.h"
#ifdef USE_LAPACK
#include "blas.h"
#endif
#ifdef USE_MKL_VML
#include <mkl_vml_functions.h>
#endif
/// Set of probability matrices for all branches of a tree.
///
/// @author Mario Valle - Swiss National Supercomputing Centre (CSCS)
/// @date 2011-04-05 (initial version)
/// @version 1.1
///
class ProbabilityMatrixSet {
protected:
/// Create matrix set. This is called only by subclasses.
///
/// @param[in] aNumMatrices The number of matrices to be managed (is the
/// number of branches of the tree)
/// @param[in] aNumSets How many sets to allocate (one set is composed by the
/// bg and fg matrices for one of the tree traversals)
/// @param[in] aNumMatSets How many sets of matrices to allocate
///
/// @exception FastCodeMLMemoryError Cannot allocate mMatrixSpace or mMatrices
///
ProbabilityMatrixSet(size_t aNumMatrices, unsigned int aNumSets,
unsigned int aNumMatSets)
: mMatrixSpace(NULL), mMatrices(NULL),
mNumMatrices(static_cast<int>(aNumMatrices)), mNumSets(aNumSets),
mFgBranch(0) {
mMatrixSpace = static_cast<double *>(
alignedMalloc(sizeof(double) * aNumMatSets * aNumMatrices * MATRIX_SLOT,
CACHE_LINE_ALIGN));
if (!mMatrixSpace)
throw FastCodeMLMemoryError("Cannot allocate mMatrixSpace");
mMatrices = static_cast<double **>(alignedMalloc(
sizeof(double *) * aNumSets * aNumMatrices, CACHE_LINE_ALIGN));
if (!mMatrices)
throw FastCodeMLMemoryError("Cannot allocate mMatrices");
#ifdef NEW_LIKELIHOOD
mInvCodonFreq = CodonFrequencies::getInstance()->getInvCodonFrequencies();
#endif
}
public:
/// Destructor.
///
~ProbabilityMatrixSet() {
alignedFree(mMatrixSpace);
alignedFree(mMatrices);
}
/// Return the number of sets contained in this ProbabilityMatrixSet
///
/// @return The number of sets
///
unsigned int size(void) const { return mNumSets; }
/// Initialize only the foreground branch value.
/// It leaves the mMatrix array of pointers uninitialized. This is OK if the
/// set is used only to store the matrices for later usage.
///
/// @param[in] aFgBranch Number of the foreground branch (as branch number not
/// as internal branch number!)
///
void initializeFgBranch(unsigned int aFgBranch) {
mFgBranch = static_cast<int>(aFgBranch);
}
#ifndef NEW_LIKELIHOOD
/// Multiply the aGin vector by the precomputed exp(Q*t) matrix
///
/// @param[in] aSetIdx Which set to use (starts from zero)
/// @param[in] aBranch Which branch
/// @param[in] aGin The input vector to be multiplied by the matrix
/// exponential
/// @param[out] aGout The resulting vector
///
void doTransition(unsigned int aSetIdx, unsigned int aBranch,
const double *aGin, double *aGout) const {
#ifdef USE_LAPACK
dsymv_("U", &N, &D1, mMatrices[aSetIdx * mNumMatrices + aBranch], &N, aGin,
&I1, &D0, aGout, &I1);
// The element wise multiplication has been moved up one level
//#if !defined(BUNDLE_ELEMENT_WISE_MULT)
// elementWiseMult(aGout, mInvCodonFreq);
//#endif
#else
for (int r = 0; r < N; ++r) {
double x = 0;
for (int c = 0; c < N; ++c)
x += mMatrices[aSetIdx * mNumMatrices + aBranch][r * N + c] * aGin[c];
aGout[r] = x;
}
#endif
}
/// Multiply the aGin vector by the precomputed exp(Q*t) matrix
///
/// @param[in] aSetIdx Which set to use (starts from zero)
/// @param[in] aBranch Which branch
/// @param[in] aCodon The leaf codon id (will supplant aGin)
/// @param[out] aGout The resulting vector
///
void doTransitionAtLeaf(unsigned int aSetIdx, unsigned int aBranch,
int aCodon, double *aGout) const {
#ifdef USE_LAPACK
// Simply copy the symmetric matrix column
// instead of: dsymv_("U", &N, &D1, mMatrices[aSetIdx*mNumMatrices+aBranch],
// &N, aGin, &I1, &D0, aGout, &I1);
// The method is:
// for(i=0; i < aCodon; ++i) aGout[i] =
// mMatrices[aSetIdx*mNumMatrices+aBranch][aCodon*N+i];
// for(i=aCodon; i < N; ++i) aGout[i] =
// mMatrices[aSetIdx*mNumMatrices+aBranch][i*N+aCodon];
// The special cases below are for accellerate the routine
switch (aCodon) {
case 0:
for (int i = 0; i < N; ++i)
aGout[i] = mMatrices[aSetIdx * mNumMatrices + aBranch][i * N];
break;
case 1:
aGout[0] = mMatrices[aSetIdx * mNumMatrices + aBranch][N];
for (int i = 1; i < N; ++i)
aGout[i] = mMatrices[aSetIdx * mNumMatrices + aBranch][i * N + 1];
break;
default:
memcpy(aGout, mMatrices[aSetIdx * mNumMatrices + aBranch] + aCodon * N,
aCodon * sizeof(double));
for (int i = aCodon; i < N; ++i)
aGout[i] = mMatrices[aSetIdx * mNumMatrices + aBranch][i * N + aCodon];
break;
}
// The element wise multiplication has been moved up one level
//#if !defined(BUNDLE_ELEMENT_WISE_MULT)
// elementWiseMult(aGout, mInvCodonFreq);
//#endif
#else
for (int r = 0; r < N; ++r) {
aGout[r] = mMatrices[aSetIdx * mNumMatrices + aBranch][r * N + aCodon];
}
#endif
}
#else
/// Multiply the aMin fat-vector by the precomputed exp(Q*t) matrix
///
/// @param[in] aSetIdx Which set to use (starts from zero)
/// @param[in] aBranch Which branch
/// @param[in] aNumSites Number of sites composing the fat-vector
/// @param[in] aMin The input fat-vector to be multiplied by the matrix
/// exponential
/// @param[out] aMout The resulting fat-vector
///
void doTransition(unsigned int aSetIdx, unsigned int aBranch, int aNumSites,
const double *aMin, double *aMout) const {
#ifdef USE_LAPACK
dsymm_("L", "U", &N, &aNumSites, &D1,
mMatrices[aSetIdx * mNumMatrices + aBranch], &N, aMin, &VECTOR_SLOT,
&D0, aMout, &VECTOR_SLOT);
#ifdef USE_MKL_VML
for (int c = 0; c < aNumSites; ++c) {
vdMul(N, &aMout[c * VECTOR_SLOT], mInvCodonFreq, &aMout[c * VECTOR_SLOT]);
}
#else
for (int r = 0; r < N; ++r) {
dscal_(&aNumSites, &mInvCodonFreq[r], aMout + r, &VECTOR_SLOT);
}
#endif
#else
for (int r = 0; r < N; ++r) {
for (int c = 0; c < aNumSites; ++c) {
double x = 0;
for (int k = 0; k < N; ++k)
x += mMatrices[aSetIdx * mNumMatrices + aBranch][r * N + k] *
aMin[c * VECTOR_SLOT + k]; // aMin is transposed
aMout[c * VECTOR_SLOT + r] = x; // also aMout is transposed
}
}
#endif
}
#endif
protected:
double *mMatrixSpace; ///< Starts of the matrix storage area
double **mMatrices; ///< Access to the matrix set (contains pointers to
/// mMatrixSpaces matrices)
#ifdef NEW_LIKELIHOOD
const double *mInvCodonFreq; ///< Inverse of the codon frequencies
#endif
int mNumMatrices; ///< Number of matrices in each set (should be int)
unsigned int mNumSets; ///< Number of sets
int mFgBranch; ///< Foreground branch number (should be int)
};
/// Set of probability matrices for all branches of a tree for the null
/// hypothesis.
///
/// @author Mario Valle - Swiss National Supercomputing Centre (CSCS)
/// @date 2012-09-07 (initial version)
/// @version 1.1
///
class ProbabilityMatrixSetH0 : public ProbabilityMatrixSet {
public:
/// Create matrix set. It allocates 3 sets.
///
/// @param[in] aNumMatrices The number of matrices to be managed (it is the
/// number of branches of the tree)
///
explicit ProbabilityMatrixSetH0(size_t aNumMatrices)
: ProbabilityMatrixSet(aNumMatrices, 3, 2) {}
/// Initialize the set for a given foreground branch number for H0
///
/// @param[in] aFgBranch Number of the foreground branch (as branch number not
/// as internal branch number!)
///
void initializeSet(unsigned int aFgBranch);
/// Compute the three sets of matrices for the H0 hypothesis.
/// The sets are (these are the bg and fg matrices):
/// - set 0: w0, w0
/// - set 1: w1, w1
/// - set 2: w0, w1
///
/// @param[in] aQw0 The mQw0 transition matrix
/// @param[in] aQ1 The mQ1 transition matrix
/// @param[in] aSbg Background Q matrix scale
/// @param[in] aSfg Foreground Q matrix scale
/// @param[in] aParams Optimization parameters. First the branch lengths, then
/// the variable parts (p0+p1, p0/(p0+p1), w0, k, w2)
///
void fillMatrixSet(const TransitionMatrix &aQw0, const TransitionMatrix &aQ1,
double aSbg, double aSfg,
const std::vector<double> &aParams);
/// Restore the previous value for the aBranch matrices.
///
/// @param[in] aBranch Branch for which the matrices should be restored
///
void restoreSavedMatrix(size_t aBranch);
/// Save the previous value for the aBranch matrices.
///
/// @param[in] aBranch Branch for which the matrices should be saved
///
void saveMatrix(size_t aBranch);
/// Access the matrices corresponding to the given branch.
///
/// @param[in] aBranch The branch for which the matrices are to be accessed
///
/// @return Pointer to an array of two pointers to the matrices to be accessed
///
const double **getChangedMatrices(size_t aBranch);
/// Set the matrices for branch aBranch from the return value of a
/// getChangedMatrices routine
///
/// @param[in] aBranch The branch for which the matrices are to be accessed
/// @param[in] aMatricesPtr array of pointers as returned by the
/// getChangedMatrices routine
///
void setMatrices(size_t aBranch, const double **aMatricesPtr);
private:
const double *
mMatricesPtr[2]; ///< Pointers to the changed matrices to be restored
double mSaveQw0[N * N]; ///< Save the previous value for the Qw0 matrix
double mSaveQ1[N * N]; ///< Save the previous value for the Q1 matrix
};
/// Set of probability matrices for all branches of a tree for the alternate
/// hypothesis.
///
/// @author Mario Valle - Swiss National Supercomputing Centre (CSCS)
/// @date 2012-09-07 (initial version)
/// @version 1.1
///
class ProbabilityMatrixSetH1 : public ProbabilityMatrixSet {
public:
/// Create matrix set
///
/// @param[in] aNumMatrices The number of matrices to be managed (it is the
/// number of branches of the tree)
///
explicit ProbabilityMatrixSetH1(size_t aNumMatrices)
: ProbabilityMatrixSet(aNumMatrices, 4, 3) {}
/// Initialize the set for a given foreground branch number for H1
///
/// @param[in] aFgBranch Number of the foreground branch (as branch number not
/// as internal branch number!)
///
void initializeSet(unsigned int aFgBranch);
/// Compute the four sets of matrices for the H1 hypothesis.
/// The sets are (these are the bg and fg matrices):
/// - set 0: w0, w0
/// - set 1: w1, w1
/// - set 2: w0, w2
/// - set 3: w1, w2
///
/// @param[in] aQw0 The mQw0 transition matrix
/// @param[in] aQ1 The mQ1 transition matrix
/// @param[in] aQw2 The mQw2 transition matrix
/// @param[in] aSbg Background Q matrix scale
/// @param[in] aSfg Foreground Q matrix scale
/// @param[in] aParams Optimization parameters. First the branch lengths, then
/// the variable parts (p0+p1, p0/(p0+p1), w0, k, w2)
///
void fillMatrixSet(const TransitionMatrix &aQw0, const TransitionMatrix &aQ1,
const TransitionMatrix &aQw2, double aSbg, double aSfg,
const std::vector<double> &aParams);
/// Restore the previous value for the aBranch matrices.
///
/// @param[in] aBranch Branch for which the matrices should be restored
///
void restoreSavedMatrix(size_t aBranch);
/// Save the previous value for the aBranch matrices.
///
/// @param[in] aBranch Branch for which the matrices should be saved
///
void saveMatrix(size_t aBranch);
/// Access the matrices corresponding to the given branch.
///
/// @param[in] aBranch The branch for which the matrices are to be accessed
///
/// @return Pointer to an array of three pointers to the matrices to be
/// accessed
///
const double **getChangedMatrices(size_t aBranch);
/// Set the matrices for branch aBranch from the return value of a
/// getChangedMatrices routine
///
/// @param[in] aBranch The branch for which the matrices are to be accessed
/// @param[in] aMatricesPtr array of pointers as returned by the
/// getChangedMatrices routine
///
void setMatrices(size_t aBranch, const double **aMatricesPtr);
private:
const double *
mMatricesPtr[3]; ///< Pointers to the changed matrices to be restored
double mSaveQw0[N * N]; ///< Save the previous value for the Qw0 matrix
double mSaveQ1[N * N]; ///< Save the previous value for the Q1 matrix
double mSaveQw2[N * N]; ///< Save the previous value for the Qw2 matrix
};
/// Set of probability matrices for all branches of a tree for the BEB
/// computation.
///
/// @author Mario Valle - Swiss National Supercomputing Centre (CSCS)
/// @date 2012-09-20 (initial version)
/// @version 1.1
///
class ProbabilityMatrixSetBEB : public ProbabilityMatrixSet {
public:
/// Create matrix set.
///
/// @param[in] aNumMatrices The number of matrices to be managed (it is the
/// number of branches of the tree)
///
explicit ProbabilityMatrixSetBEB(size_t aNumMatrices)
: ProbabilityMatrixSet(aNumMatrices, 1, 1) {
for (int branch = 0; branch < mNumMatrices; ++branch) {
mMatrices[branch] = &mMatrixSpace[branch * MATRIX_SLOT];
}
}
/// Compute the sets of matrices for the BEB computation.
/// The sets are (these are the bg and fg matrices):
/// - set 0: w0, w0
/// - set 1: w1, w1
/// - set 2: w0, w2
/// - set 3: w1, w2
///
/// @param[in] aQfg The transition matrix for the fg branch
/// @param[in] aQbg The transition matrix for the bg branch
/// @param[in] aSbg Background Q matrix scale
/// @param[in] aSfg Foreground Q matrix scale
/// @param[in] aParams Optimization parameters. First the branch lengths, then
/// the variable parts (p0+p1, p0/(p0+p1), w0, k, w2)
///
void fillMatrixSet(const TransitionMatrix &aQfg, const TransitionMatrix &aQbg,
double aSbg, double aSfg,
const std::vector<double> &aParams);
};
/* equivalent classes supporting multiple
* foreground branches */
class mfgProbabilityMatrixSet {
protected:
/// Create matrix set. This is called only by subclasses.
///
/// @param[in] aNumMatrices The number of matrices to be managed (is the
/// number of branches of the tree)
/// @param[in] aNumSets How many sets to allocate (one set is composed by the
/// bg and fg matrices for one of the tree traversals)
/// @param[in] aNumMatSets How many sets of matrices to allocate
///
/// @exception FastCodeMLMemoryError Cannot allocate mMatrixSpace or mMatrices
///
mfgProbabilityMatrixSet(size_t aNumMatrices, unsigned int aNumSets,
unsigned int aNumMatSets)
: mMatrixSpace(NULL), mMatrices(NULL),
mNumMatrices(static_cast<int>(aNumMatrices)), mNumSets(aNumSets) {
mMatrixSpace = static_cast<double *>(
alignedMalloc(sizeof(double) * aNumMatSets * aNumMatrices * MATRIX_SLOT,
CACHE_LINE_ALIGN));
if (!mMatrixSpace)
throw FastCodeMLMemoryError("Cannot allocate mMatrixSpace");
mMatrices = static_cast<double **>(alignedMalloc(
sizeof(double *) * aNumSets * aNumMatrices, CACHE_LINE_ALIGN));
if (!mMatrices)
throw FastCodeMLMemoryError("Cannot allocate mMatrices");
#ifdef NEW_LIKELIHOOD
mInvCodonFreq = CodonFrequencies::getInstance()->getInvCodonFrequencies();
#endif
}
public:
/// Destructor.
///
~mfgProbabilityMatrixSet() {
alignedFree(mMatrixSpace);
alignedFree(mMatrices);
}
/// Return the number of sets contained in this ProbabilityMatrixSet
///
/// @return The number of sets
///
unsigned int size(void) const { return mNumSets; }
/// Initialize the foreground branch values.
/// It leaves the mMatrix array of pointers uninitialized. This is OK if the
/// set is used only to store the matrices for later usage.
///
/// @param[in] aFgBranch Number of the foreground branch (as branch number not
/// as internal branch number!)
///
void initializeFgBranch(std::set<int> aFgBranchSet) {
mFgBranchSet = aFgBranchSet;
}
#ifndef NEW_LIKELIHOOD
/// Multiply the aGin vector by the precomputed exp(Q*t) matrix
///
/// @param[in] aSetIdx Which set to use (starts from zero)
/// @param[in] aBranch Which branch
/// @param[in] aGin The input vector to be multiplied by the matrix
/// exponential
/// @param[out] aGout The resulting vector
///
void doTransition(unsigned int aSetIdx, unsigned int aBranch,
const double *aGin, double *aGout) const {
#ifdef USE_LAPACK
dsymv_("U", &N, &D1, mMatrices[aSetIdx * mNumMatrices + aBranch], &N, aGin,
&I1, &D0, aGout, &I1);
// The element wise multiplication has been moved up one level
//#if !defined(BUNDLE_ELEMENT_WISE_MULT)
// elementWiseMult(aGout, mInvCodonFreq);
//#endif
#else
for (int r = 0; r < N; ++r) {
double x = 0;
for (int c = 0; c < N; ++c)
x += mMatrices[aSetIdx * mNumMatrices + aBranch][r * N + c] * aGin[c];
aGout[r] = x;
}
#endif
}
/// Multiply the aGin vector by the precomputed exp(Q*t) matrix
///
/// @param[in] aSetIdx Which set to use (starts from zero)
/// @param[in] aBranch Which branch
/// @param[in] aCodon The leaf codon id (will supplant aGin)
/// @param[out] aGout The resulting vector
///
void doTransitionAtLeaf(unsigned int aSetIdx, unsigned int aBranch,
int aCodon, double *aGout) const {
#ifdef USE_LAPACK
// Simply copy the symmetric matrix column
// instead of: dsymv_("U", &N, &D1, mMatrices[aSetIdx*mNumMatrices+aBranch],
// &N, aGin, &I1, &D0, aGout, &I1);
// The method is:
// for(i=0; i < aCodon; ++i) aGout[i] =
//mMatrices[aSetIdx*mNumMatrices+aBranch][aCodon*N+i];
// for(i=aCodon; i < N; ++i) aGout[i] =
//mMatrices[aSetIdx*mNumMatrices+aBranch][i*N+aCodon];
// The special cases below are for accellerate the routine
switch (aCodon) {
case 0:
for (int i = 0; i < N; ++i)
aGout[i] = mMatrices[aSetIdx * mNumMatrices + aBranch][i * N];
break;
case 1:
aGout[0] = mMatrices[aSetIdx * mNumMatrices + aBranch][N];
for (int i = 1; i < N; ++i)
aGout[i] = mMatrices[aSetIdx * mNumMatrices + aBranch][i * N + 1];
break;
default:
memcpy(aGout, mMatrices[aSetIdx * mNumMatrices + aBranch] + aCodon * N,
aCodon * sizeof(double));
for (int i = aCodon; i < N; ++i)
aGout[i] = mMatrices[aSetIdx * mNumMatrices + aBranch][i * N + aCodon];
break;
}
// The element wise multiplication has been moved up one level
//#if !defined(BUNDLE_ELEMENT_WISE_MULT)
// elementWiseMult(aGout, mInvCodonFreq);
//#endif
#else
for (int r = 0; r < N; ++r) {
aGout[r] = mMatrices[aSetIdx * mNumMatrices + aBranch][r * N + aCodon];
}
#endif
}
#else
/// Multiply the aMin fat-vector by the precomputed exp(Q*t) matrix
///
/// @param[in] aSetIdx Which set to use (starts from zero)
/// @param[in] aBranch Which branch
/// @param[in] aNumSites Number of sites composing the fat-vector
/// @param[in] aMin The input fat-vector to be multiplied by the matrix
/// exponential
/// @param[out] aMout The resulting fat-vector
///
void doTransition(unsigned int aSetIdx, unsigned int aBranch, int aNumSites,
const double *aMin, double *aMout) const {
#ifdef USE_LAPACK
dsymm_("L", "U", &N, &aNumSites, &D1,
mMatrices[aSetIdx * mNumMatrices + aBranch], &N, aMin, &VECTOR_SLOT,
&D0, aMout, &VECTOR_SLOT);
#ifdef USE_MKL_VML
for (int c = 0; c < aNumSites; ++c) {
vdMul(N, &aMout[c * VECTOR_SLOT], mInvCodonFreq, &aMout[c * VECTOR_SLOT]);
}
#else
for (int r = 0; r < N; ++r) {
dscal_(&aNumSites, &mInvCodonFreq[r], aMout + r, &VECTOR_SLOT);
}
#endif
#else
for (int r = 0; r < N; ++r) {
for (int c = 0; c < aNumSites; ++c) {
double x = 0;
for (int k = 0; k < N; ++k)
x += mMatrices[aSetIdx * mNumMatrices + aBranch][r * N + k] *
aMin[c * VECTOR_SLOT + k]; // aMin is transposed
aMout[c * VECTOR_SLOT + r] = x; // also aMout is transposed
}
}
#endif
}
#endif
protected:
double *mMatrixSpace; ///< Starts of the matrix storage area
double **mMatrices; ///< Access to the matrix set (contains pointers to
///mMatrixSpaces matrices)
#ifdef NEW_LIKELIHOOD
const double *mInvCodonFreq; ///< Inverse of the codon frequencies
#endif
int mNumMatrices; ///< Number of matrices in each set (should be int)
unsigned int mNumSets; ///< Number of sets
std::set<int> mFgBranchSet; ///< Foreground branch numbers (should be int)
};
/// Set of probability matrices for all branches of a tree for the null
/// hypothesis.
///
/// @author Mario Valle - Swiss National Supercomputing Centre
///(CSCS)
/// @date 2012-09-07 (initial version)
/// @version 1.1
///
class mfgProbabilityMatrixSetH0 : public mfgProbabilityMatrixSet {
public:
/// Create matrix set. It allocates 3 sets.
///
/// @param[in] aNumMatrices The number of matrices to be managed (it is the
/// number of branches of the tree)
///
explicit mfgProbabilityMatrixSetH0(size_t aNumMatrices)
: mfgProbabilityMatrixSet(aNumMatrices, 3, 2) {}
/// Initialize the set for a given foreground branch number for H0
///
/// @param[in] aFgBranch Number of the foreground branch (as branch number not
/// as internal branch number!)
///
void initializeSet(std::set<int> aFgBranchSet);
/// Compute the three sets of matrices for the H0 hypothesis.
/// The sets are (these are the bg and fg matrices):
/// - set 0: w0, w0
/// - set 1: w1, w1
/// - set 2: w0, w1
///
/// @param[in] aQw0 The mQw0 transition matrix
/// @param[in] aQ1 The mQ1 transition matrix
/// @param[in] aSbg Background Q matrix scale
/// @param[in] aSfg Foreground Q matrix scale
/// @param[in] aParams Optimization parameters. First the branch lengths, then
/// the variable parts (p0+p1, p0/(p0+p1), w0, k, w2)
///
void fillMatrixSet(const TransitionMatrix &aQw0, const TransitionMatrix &aQ1,
double aSbg, double aSfg,
const std::vector<double> &aParams);
/// Restore the previous value for the aBranch matrices.
///
/// @param[in] aBranch Branch for which the matrices should be restored
///
void restoreSavedMatrix(size_t aBranch);
/// Save the previous value for the aBranch matrices.
///
/// @param[in] aBranch Branch for which the matrices should be saved
///
void saveMatrix(size_t aBranch);
/// Access the matrices corresponding to the given branch.
///
/// @param[in] aBranch The branch for which the matrices are to be accessed
///
/// @return Pointer to an array of two pointers to the matrices to be accessed
///
const double **getChangedMatrices(size_t aBranch);
/// Set the matrices for branch aBranch from the return value of a
/// getChangedMatrices routine
///
/// @param[in] aBranch The branch for which the matrices are to be accessed
/// @param[in] aMatricesPtr array of pointers as returned by the
/// getChangedMatrices routine
///
void setMatrices(size_t aBranch, const double **aMatricesPtr);
private:
const double
*mMatricesPtr[2]; ///< Pointers to the changed matrices to be restored
double mSaveQw0[N * N]; ///< Save the previous value for the Qw0 matrix
double mSaveQ1[N * N]; ///< Save the previous value for the Q1 matrix
};
/// Set of probability matrices for all branches of a tree for the alternate
/// hypothesis.
///
/// @author Mario Valle - Swiss National Supercomputing Centre
///(CSCS)
/// @date 2012-09-07 (initial version)
/// @version 1.1
///
class mfgProbabilityMatrixSetH1 : public mfgProbabilityMatrixSet {
public:
/// Create matrix set
///
/// @param[in] aNumMatrices The number of matrices to be managed (it is the
/// number of branches of the tree)
///
explicit mfgProbabilityMatrixSetH1(size_t aNumMatrices)
: mfgProbabilityMatrixSet(aNumMatrices, 4, 3) {}
/// Initialize the set for a given foreground branch number for H1
///
/// @param[in] aFgBranch Number of the foreground branch (as branch number not
/// as internal branch number!)
///
void initializeSet(std::set<int> aFgBranchSet);
/// Compute the four sets of matrices for the H1 hypothesis.
/// The sets are (these are the bg and fg matrices):
/// - set 0: w0, w0
/// - set 1: w1, w1
/// - set 2: w0, w2
/// - set 3: w1, w2
///
/// @param[in] aQw0 The mQw0 transition matrix
/// @param[in] aQ1 The mQ1 transition matrix
/// @param[in] aQw2 The mQw2 transition matrix
/// @param[in] aSbg Background Q matrix scale
/// @param[in] aSfg Foreground Q matrix scale
/// @param[in] aParams Optimization parameters. First the branch lengths, then
/// the variable parts (p0+p1, p0/(p0+p1), w0, k, w2)
///
void fillMatrixSet(const TransitionMatrix &aQw0, const TransitionMatrix &aQ1,
const TransitionMatrix &aQw2, double aSbg, double aSfg,
const std::vector<double> &aParams);
/// Restore the previous value for the aBranch matrices.
///
/// @param[in] aBranch Branch for which the matrices should be restored
///
void restoreSavedMatrix(size_t aBranch);
/// Save the previous value for the aBranch matrices.
///
/// @param[in] aBranch Branch for which the matrices should be saved
///
void saveMatrix(size_t aBranch);
/// Access the matrices corresponding to the given branch.
///
/// @param[in] aBranch The branch for which the matrices are to be accessed
///
/// @return Pointer to an array of three pointers to the matrices to be
/// accessed
///
const double **getChangedMatrices(size_t aBranch);
/// Set the matrices for branch aBranch from the return value of a
/// getChangedMatrices routine
///
/// @param[in] aBranch The branch for which the matrices are to be accessed
/// @param[in] aMatricesPtr array of pointers as returned by the
/// getChangedMatrices routine
///
void setMatrices(size_t aBranch, const double **aMatricesPtr);
private:
const double
*mMatricesPtr[3]; ///< Pointers to the changed matrices to be restored
double mSaveQw0[N * N]; ///< Save the previous value for the Qw0 matrix
double mSaveQ1[N * N]; ///< Save the previous value for the Q1 matrix
double mSaveQw2[N * N]; ///< Save the previous value for the Qw2 matrix
};
/// Set of probability matrices for all branches of a tree for the BEB
/// computation.
///
/// @author Mario Valle - Swiss National Supercomputing Centre
///(CSCS)
/// @date 2012-09-20 (initial version)
/// @version 1.1
///
class mfgProbabilityMatrixSetBEB : public mfgProbabilityMatrixSet {
public:
/// Create matrix set.
///
/// @param[in] aNumMatrices The number of matrices to be managed (it is the
/// number of branches of the tree)
///
explicit mfgProbabilityMatrixSetBEB(size_t aNumMatrices)
: mfgProbabilityMatrixSet(aNumMatrices, 1, 1) {
for (int branch = 0; branch < mNumMatrices; ++branch) {
mMatrices[branch] = &mMatrixSpace[branch * MATRIX_SLOT];
}
}
/// Compute the sets of matrices for the BEB computation.
/// The sets are (these are the bg and fg matrices):
/// - set 0: w0, w0
/// - set 1: w1, w1
/// - set 2: w0, w2
/// - set 3: w1, w2
///
/// @param[in] aQfg The transition matrix for the fg branch
/// @param[in] aQbg The transition matrix for the bg branch
/// @param[in] aSbg Background Q matrix scale
/// @param[in] aSfg Foreground Q matrix scale
/// @param[in] aParams Optimization parameters. First the branch lengths, then
/// the variable parts (p0+p1, p0/(p0+p1), w0, k, w2)
///
void fillMatrixSet(const TransitionMatrix &aQfg, const TransitionMatrix &aQbg,
double aSbg, double aSfg,
const std::vector<double> &aParams);
};
#endif