-
Notifications
You must be signed in to change notification settings - Fork 0
/
Forest.h
491 lines (442 loc) · 17.1 KB
/
Forest.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
#ifndef FOREST_H
#define FOREST_H
#include <vector>
#include "PhyloTree.h"
#include "Genes.h"
#include "ForestNode.h"
#include "TransitionMatrix.h"
#include "ProbabilityMatrixSet.h"
#include "MatrixSize.h"
#ifdef NEW_LIKELIHOOD
#include "FatVectorTransform.h"
#endif
#include "CodonFrequencies.h"
#include "Types.h"
#include "ForestExport.h"
#ifdef USE_DAG
#include "DAGScheduler.h"
#endif
#include "TreeAndSetsDependencies.h"
#include "CmdLine.h"
/// The phylogenetic tree's forest.
/// This class encapsulates the forest of phylogenetic tree that will be used
/// for computing the tree's maximum likelihood
///
/// @author Mario Valle - Swiss National Supercomputing Centre (CSCS)
/// @date 2011-02-23 (initial version)
/// @version 1.1
///
class Forest {
public:
/// Constructor
///
/// @param[in] aVerbose The verbosity level
///
explicit Forest(unsigned int aVerbose = 0)
: mNumSites(0), mCodonFreq(NULL), mInvCodonFreq(NULL),
mInv2CodonFreq(NULL), mNumBranches(0), mNumInternalBranches(0),
mMarkedInternalBranch(UINT_MAX), mVerbose(aVerbose) {}
/// Destructor
///
~Forest() {
mRoots.clear();
mNodeNames.clear();
mBranchLengths.clear();
mProbs.clear();
mSiteMultiplicity.clear();
mTableInternalToBranchID.clear();
#ifdef NEW_LIKELIHOOD
mProbsOut.clear();
mNodesByLevel.clear();
#endif
#ifdef NON_RECURSIVE_VISIT
mVisitTree.clear();
mVisitTreeParents.clear();
#endif
}
/// Build the forest and reduces the subtrees.
///
/// @param[in] aTree The phylogenetic tree
/// @param[in] aGenes The corresponding genes
/// @param[in] aCodonFrequencyModel Model to be used to compute the codon
/// empirical frequencies.
///
/// @exception FastCodeMLFatal Invalid codon found
///
void
loadTreeAndGenes(const PhyloTree &aTree, const Genes &aGenes,
CodonFrequencies::CodonFrequencyModel aCodonFrequencyModel);
/// Print the class statistics as: cout << r;.
///
/// @param[in] aOut Output stream
/// @param[in] aForest The forest to be printed
///
/// @return The output stream
///
friend std::ostream &operator<<(std::ostream &aOut, const Forest &aForest);
/// Get the first and last branches to be marked as foreground.
///
/// @param[in] aCmdLine The parameters from the command line of the main
/// program
/// @param[out] aBranchStart The first branch to be marked as foreground
/// @param[out] aBranchEnd The last branch to be marked as foreground
/// @param[out] aFgSet list of id of foreground branches
/// @param[out] aIbSet list of id of internal branches
///
/// @return True if all branches are selected
///
/// @exception FastCodeMLFatal Invalid range from command line
///
bool getBranchRange(const CmdLine &aCmdLine, size_t &aBranchStart,
size_t &aBranchEnd, std::set<int> &aFgSet,
std::set<int> &aIbSet) const;
/// Reduce common subtrees on the whole forest.
///
void reduceSubtrees(void);
#ifndef NEW_LIKELIHOOD
/// Add more aggressive subtree reduction.
///
/// @param[in] aNode The tree node from which the walker should start (no
/// argument starts from the root)
///
/// @exception FastCodeMLMemoryError Cannot allocate mOtherTreeProb
///
void addAggressiveReduction(ForestNode *aNode = NULL);
#endif
/// Remove all work data used for reduction.
///
/// @param[in] aNode The node from which to start. Pass zero to start from the
/// root of all the trees in the forest.
///
void cleanReductionWorkingData(ForestNode *aNode = NULL);
#if !defined(NON_RECURSIVE_VISIT) && !defined(NEW_LIKELIHOOD)
/// Compute likelihood visiting the trees in a non-recursive way
///
/// @param[in] aSet Set of exp(Q*t) matrices
/// @param[out] aLikelihoods Values of the codon probabilities at the tree
/// root (one set for each set of matrices)
/// @param[in] aDependencies The dependency list between sets of trees
///
void computeLikelihoods(const ProbabilityMatrixSet &aSet,
CacheAlignedDoubleVector &aLikelihoods,
const ListDependencies &aDependencies);
void computeLikelihoods(const mfgProbabilityMatrixSet &aSet,
CacheAlignedDoubleVector &aLikelihoods,
const ListDependencies &aDependencies);
#endif
#ifdef NON_RECURSIVE_VISIT
/// Prepare the list of threading pointers for non-recursive trees visit
///
void prepareNonRecursiveVisit(void);
/// Compute likelihood visiting the trees in a non-recursive way
///
/// @param[in] aSet Set of exp(Q*t) matrices
/// @param[out] aLikelihoods Values of the codon probabilities at the tree
/// root (one set for each set of matrices)
/// @param[in] aHyp The hypothesis to be computed (H0: 0; H1: 1)
///
void computeLikelihoods(const ProbabilityMatrixSet &aSet,
CacheAlignedDoubleVector &aLikelihoods,
unsigned int aHyp);
#endif
#ifdef NEW_LIKELIHOOD
/// Compute the log likelihood of the forest given the set of precomputed
/// matrices.
/// If NEW_LIKELIHOOD is defined, this routine adopts the experimental "Long
/// Vector" approach.
///
/// @param[in] aSet Set of exp(Q*t) matrices
/// @param[out] aLikelihoods Values of the codon probabilities at the tree
/// root (one set for each set of matrices)
/// @param[in] aHyp The hypothesis to be computed (H0: 0; H1: 1) (currently
/// ignored)
///
void computeLikelihoods(const ProbabilityMatrixSet &aSet,
CacheAlignedDoubleVector &aLikelihoods,
unsigned int /*aHyp*/);
#endif
/// Export the forest as graph file
///
friend class ForestExport;
/// Return the total number of branches
///
/// @return The total number of branches
///
size_t getNumBranches(void) const { return mNumBranches; }
/// Return the number of internal branches (i.e.\ the ones that do not connect
/// to leaves)
///
/// @return The number of internal branches
///
size_t getNumInternalBranches(void) const { return mNumInternalBranches; }
/// Get the number of sites
///
/// @return The number of sites
///
size_t getNumSites(void) const { return mNumSites; }
/// Get the marked internal branch
///
/// @return The internal branch index of the branch marked in the tree file.
/// UINT_MAX otherwise.
///
size_t getMarkedInternalBranch(void) const { return mMarkedInternalBranch; }
/// Get all marked branches
///
/// @return The internal branch index of the branches marked in the tree file.
/// UINT_MAX otherwise.
///
std::set<int> getMarkedBranches(void) const { return mMarkedBranches; }
/// Get all internal branches
///
/// @return The internal branch index of the internal branches in the tree
/// file. UINT_MAX otherwise.
///
std::set<int> getInternalBranches(void) const { return mInternalBranches; }
/// Get site multiplicity values.
///
/// @return Reference to the array of site multiplicities
///
const std::vector<double> &getSiteMultiplicity(void) const {
return mSiteMultiplicity;
}
/// Set the times (i.e.\ the branch lengths) from the values read from the
/// tree file
///
/// @param[out] aTimes The array with all the tree times
/// @param[in] aNode The node from which to start (if zero starts from the
/// root)
///
void setTimesFromLengths(std::vector<double> &aTimes,
const ForestNode *aNode = NULL) const;
/// Set the times (i.e.\ the branch lengths) on the tree from the values read
/// from the times array
///
/// @param[in] aTimes The array with all the tree times
/// @param[out] aNode The node from which to start (if zero starts from the
/// root)
///
void setLengthsFromTimes(const std::vector<double> &aTimes,
ForestNode *aNode = NULL);
/// Change the internal branch identifier for the foreground branch into the
/// corresponding internal branch index.
///
/// @param[in] aFgBranch Number of the foreground branch
///
/// @return The node index corresponding to the foreground branch
///
unsigned int adjustFgBranchIdx(size_t aFgBranch) const {
return mTableInternalToBranchID[aFgBranch];
}
/// Access the global list of node names.
///
/// @return A reference to the list of node names.
///
const std::vector<std::string> &getNodeNames(void) const {
return mNodeNames;
}
/// Get the mapping from the internal site number to the original site.
///
/// @return Multimap with key the internal site, and value one of the original
/// sites.
///
const std::multimap<size_t, size_t> &getSitesMappingToOriginal(void) {
return mSitesMappingToOriginal;
}
#ifdef NEW_LIKELIHOOD
/// All the preparatory steps needed for the Fat Vector approach.
///
void postLoad(void);
/// Analyze the forest to prepare the operation to be done to restore the
/// contiguity to the grouped vector approach.
///
/// @param[in] aNode The node from which to start. If null then starts from
/// all the trees' roots.
///
void prepareNewReduction(ForestNode *aNode = NULL);
/// Prepare the data for a forest that has not been reduced
///
void prepareNewReductionNoReuse(void);
#endif
#ifdef USE_DAG
/// Load the forest into a DAG
///
/// @param[in] aMaxCopies Max number of forest copies for the various codon
/// classes
/// @param[in] aCopyId The current copy entered
/// @param[in] aNode If null starts from the roots. It is used for recursive
/// visit
///
void loadForestIntoDAG(unsigned int aMaxCopies, unsigned int aCopyId = 0,
const ForestNode *aNode = NULL);
#endif
/// Access the dependency list.
/// result[tj] = [t1 t2 t3] means: tj can be done after: t1 t2 t3.
///
/// @return List of lists of dependencies
///
const std::vector<std::vector<unsigned int> > &
getTreeDependencies(void) const {
return mTreeDependencies;
}
/// Access the reverse dependency list.
/// result[tj] = [t1 t2 t3] means: tj should be ready before: t1 t2 t3
///
/// @return List of lists of reverse dependencies
///
const std::vector<std::vector<unsigned int> > &
getTreeRevDependencies(void) const {
return mTreeRevDependencies;
}
/// Get the computation cost for all sites from the root to the leaves.
///
/// @param[out] aEfforts The cost for each site
/// @param[in] aCostAtLeaf The cost associated to a leaf
/// @param[in] aCostIntern The cost associated to an internal node
/// @param[in] aCostPtr The cost associated to a pointer to another node
///
void getEffortPerSite(std::vector<unsigned int> &aEfforts,
unsigned int aCostAtLeaf, unsigned int aCostIntern,
unsigned int aCostPtr) const;
private:
/// Reduce the common subtree between two (sub)trees
///
/// @param[in] aNode The subtree to be tested (i.e.\ if it exists in both
/// trees)
/// @param[in] aNodeDependent The dependent tree (i.e.\ it could point to
/// subtrees of aNode)
///
void reduceSubtreesWalker(ForestNode *aNode, ForestNode *aNodeDependent);
#if !defined(NON_RECURSIVE_VISIT) && !defined(NEW_LIKELIHOOD)
/// Walker for the computation of tree likelihood
///
/// @param[in] aNode The node from which the visit should start
/// @param[in] aSet Set of exp(Q*t) matrices
/// @param[in] aSetIdx Identifier of the set of matrices to be used
///
/// @return The vector of codons probabilities at the aNode node
///
double *computeLikelihoodsWalkerTC(const ForestNode *aNode,
const ProbabilityMatrixSet &aSet,
unsigned int aSetIdx);
double *computeLikelihoodsWalkerTC(const ForestNode *aNode,
const mfgProbabilityMatrixSet &aSet,
unsigned int aSetIdx);
#endif
#ifdef NON_RECURSIVE_VISIT
/// Walker to prepare the non recursive visit list
///
/// @param[in] aNode The current node to be visited
/// @param[in] aParentNode Parent node for aNode
/// @param[in] aSite The current site
/// @param[in,out] aVisitList The list of nodes to be visited in the order of
/// visit.
/// @param[in,out] aParentList The corresponding parent nodes
///
void prepareNonRecursiveVisitWalker(ForestNode *aNode,
ForestNode *aParentNode,
unsigned int aSite,
std::vector<ForestNode *> &aVisitList,
std::vector<ForestNode *> &aParentList);
/// Walker for the computation of tree likelihood
///
/// @param[in] aSet Set of exp(Q*t) matrices
/// @param[in] aSetIdx Identifier of the set of matrices to be used
/// @param[in] aSiteIdx The site under computation
///
void computeLikelihoodsWalkerNR(const ProbabilityMatrixSet &aSet,
unsigned int aSetIdx, unsigned int aSiteIdx);
#endif
/// Walk the tree to fill the mMapInternalToBranchID map.
///
/// @param[in] aNode The node from which to start
/// @param[out] aMapInternalToBranchID Maps internal branch id to branch id
///
void mapInternalToBranchIdWalker(
const ForestNode *aNode,
std::map<unsigned int, unsigned int> &aMapInternalToBranchID);
private:
size_t mNumSites; ///< Number of sites
const double *mCodonFreq; ///< Experimental codon frequencies
const double *mInvCodonFreq; ///< Inverse of the codon frequencies
const double *mInv2CodonFreq; ///< Squared inverse of the codon frequencies
size_t mNumBranches; ///< Total number of branches of the original tree
std::vector<ForestNode> mRoots; ///< The roots of the forest's trees. Its
///length is the number of valid sites
std::vector<double> mSiteMultiplicity; ///< Multiplicity of the valid sites
size_t
mNumInternalBranches; ///< Total number of branches of the original tree
std::vector<unsigned int> mTableInternalToBranchID; ///< Map from internal
///branch number to branch
///number
/// Here are global data that will be removed from the various (site) trees
std::vector<std::string> mNodeNames; ///< List of node names. Zero is the
///root, then its first child and so on
std::vector<double> mBranchLengths; ///< List of branch lengths (read from
///file or stored here to be exported in
///the tree file)
size_t mMarkedInternalBranch; ///< Number of the internal branch as marked in
///the tree file
std::set<int> mMarkedBranches; ///< id of the marked branches in the tree file
std::set<int>
mInternalBranches; ///< id of the internal branches in the tree file
#ifdef NEW_LIKELIHOOD
/// New loglikelihood computation support
/// The mProbs and mProbsOut layout
///
/// [site0][site1][site2]... [site0][site1][site2]... each is
/// VECTOR_SLOT bytes long (for which only the first N are significant)
/// [ set 0 ][ set 1 ]... there are
/// 4 (Nt) sets
/// [ node 0 ]...
///
/// site_index = node*(Nt*NumSites*VECTOR_SLOT) + set*(NumSites*VECTOR_SLOT) +
/// site*(VECTOR_SLOT)
///
CacheAlignedDoubleVector mProbs; ///< The concatenation of all the probability
/// vectors for all the nodes and all the
/// classes
CacheAlignedDoubleVector
mProbsOut; ///< mProbs after multiplication by exp(Qt)
std::vector<std::vector<ForestNode *> >
mNodesByLevel; ///< Each level contains
/// a list of pointers to
/// nodes at this level.
/// List start from the
/// root.
FatVectorTransform mFatVectorTransform; ///< Compute and manage the
/// transformations to pack the "long
/// vector" based on subtree pruning
#else
/// Unified array for each branch probability vector
CacheAlignedDoubleVector mProbs; ///< The concatenation of all the probability
/// vectors for all the nodes and all the
/// classes
#endif
std::vector<std::vector<unsigned int> >
mTreeDependencies; ///< mTreeDependencies[tj] = [t1 t2 t3] means: tj can
/// be done after: t1 t2 t3
std::vector<std::vector<unsigned int> >
mTreeRevDependencies; ///< mTreeRevDependencies[tj] = [t1 t2 t3] means: tj
/// should be ready before: t1 t2 t3
#ifdef NON_RECURSIVE_VISIT
std::vector<std::vector<ForestNode *> > mVisitTree; ///< List of pointers to
/// tree nodes (a list per
/// site) in the
/// non-recursive visit
/// order
std::vector<std::vector<ForestNode *> > mVisitTreeParents; ///< List of parent
/// pointers for the
/// corresponding
/// nodes in the
/// mVisitTree
#endif
#ifdef USE_DAG
DAGScheduler mDAG; ///< DAG Scheduler
#endif
unsigned int mVerbose; ///< If greater than zero prints more info
std::multimap<size_t, size_t> mSitesMappingToOriginal; ///< Map reduced site
/// num. to list of
/// corresponding
/// original sites.
};
#endif