Augustus 3.4.0
Loading...
Searching...
No Matches
phylotree.hh
1/*
2 * phylotree.hh
3 *
4 * License: Artistic License, see file LICENSE.TXT or
5 * https://opensource.org/licenses/artistic-license-1.0
6 */
7
8#ifndef _PHYLOTREE_HH
9#define _PHYLOTREE_HH
10
11#include <list>
12#include <string>
13#include <vector>
14
15// project includes
16#include "contTimeMC.hh"
17
18//forward declarations
22class Treenode;
23
31class PhyloTree;
32
36class Evo;
37
41class ExonEvo;
42
43typedef std::vector<bool> bit_vector;
44
46
47private:
48 //tree topology
49 std::string species; // speciesname (only leaf nodes)
50 double distance; // branch length to parent
51 int idx; // index of speciesname as in RandSeqAccess:speciesIndex (interior nodes: -1)
52 Treenode *parent; // parent node
53 std::list<Treenode*> children; // child nodes
54 gsl_matrix *logP; // log probability matrix for branch length t='distance'
55
56 /*
57 * tables needed for storing recursion variables
58 * during pruning algorithm and MAP inference
59 *
60 * i-th entry is the best assignment given that the parent is assigned to i
61 * only needed for MAP inference
62 */
63 std::vector<int> bestAssign;
64 /*
65 * recursion variables:
66 * Felsenstein pruning algorithm: i-the entry is the probability of the subtree rooted in this node
67 * given that the root is assigned to i
68 * MAP inference: i-the entry is the probability of the most likely assignment of all nodes of the subtree
69 * rooted in this node given that the root is assigned to i
70 */
71 std::vector<double> table;
72
73public:
74 Treenode(std::string s="", double t=0.0, int i=-1, Treenode *p=NULL):
75 species(s),
76 distance(t),
77 idx(i),
78 parent(p),
79 logP(NULL)
80 {}
81 ~Treenode(){}
82 void addDistance(double dist){this->distance=dist;}
83 void addSpecies(string s){this->species=s;}
84 void addChild(Treenode *child);
85 void removeChild(Treenode *child){children.remove(child);}
86 void makeRoot();
87 bool isLeaf() const {return (this->children.empty());}
88 bool isRoot() const {return (this->parent == NULL);}
89 void setTable(int pos, double value){table.at(pos)=value;}
90 double getTable(int pos) const{return table.at(pos);}
91 double getDist() const {return distance;}
92 int getIdx() const {return idx;}
93 void setIdx(int i) {this->idx = i;}
94 gsl_matrix* getLogP() const {return logP;}
95 void setLogP(gsl_matrix *m) {logP = m;}
96 void resizeTable(int size, double val=0);
97 std::string getSpecies() const {return species;}
98 Treenode *getParent() const {return this->parent;}
99 void printNode() const;
100
101 friend class PhyloTree;
102};
103
105
106private:
107 std::list<Treenode*> treenodes; // leaf to root order!
108 int numSp; // number of species
109public:
110
111 static double phylo_factor;
112
113 PhyloTree(std::string filename);
114 //create star-like tree with unit branch length b from a set of species identifiers
115 PhyloTree(const std::vector<std::string> &speciesnames, double b=1);
116 //copy constructor
117 PhyloTree(const PhyloTree &other);
118 ~PhyloTree();
119
120 /*
121 * general look-up functions
122 */
123 void getBranchLengths(std::vector<double> &branchset) const;
124 void scaleTree(double factor); // scale tree by multiplying each branch length by a factor
125 double getDiversity(); // sum of branch lengths
126 void getSpeciesNames(std::vector<std::string> &speciesnames);
127 int numSpecies(){return numSp;}
128 Treenode *getLeaf(std::string species) const;
129 void printTree() const;
130 void printNewick(std::string filename) const; // output tree in Newick format
131 void recursiveNWK(std::ofstream &file, Treenode *node) const; // subroutine of printNewick()
132
133 /*
134 * functions to remove terminal branches and possibly interior nodes
135 * drop removes a terminal branch given a leaf node or the species name
136 * if after removing a terminal branch the junction node has only one child left, the node
137 * will be collapsed and the branch length will be added to its child
138 */
139 void drop(std::string species);
140 void drop(Treenode *node, Evo *evo);
141 void collapse(Treenode *node, Evo *evo);
142 /*
143 * prune all leaf nodes of species that are not present in the tree as indicated by a bit vector
144 * (i-th bit in the vector is 1 if species i is present and 0 if species i is absent)
145 */
146 void prune(bit_vector &bv, Evo *evo);
147 /*
148 * Felsensteins Pruning Algorithm
149 */
150 double pruningAlgor(std::string labelpattern, Evo *evo, int u=0);
151 double pruningAlgor(std::vector<int> &tuple, Evo *evo, int u=0);
152 void printRecursionTable() const;
153
154 /*
155 * MAP inference given a set of weights for leaf nodes
156 * if the flag 'fixLeafLabels' is turned on MAP inference is carried out
157 * with the leaf labels fixed to the corresponding labels in the hect
158 * (needed in 'optimization via dual decomposition' to make the solutions
159 * of the two subproblems consistent)
160 */
161 double MAP(std::vector<int> &labels, std::vector<double> &weights, Evo *evo, bool fixLeafLabels=false);
162 void MAPbacktrack(std::vector<int> &labels, Treenode* root, int bestAssign, bool fixLeafLabels);
163 int fitch(vector<int> &labels, int states=64); // Fitch Algorithm, to count number of substitutions
164};
165
166
167/*
168 * Locus tree
169 * Charlotte Janas playground
170 */
172
173};
174
175#endif
abstract base class for codon and exon evolution
Definition contTimeMC.hh:31
Definition contTimeMC.hh:116
Definition phylotree.hh:171
Definition phylotree.hh:104
Definition phylotree.hh:45