MDA
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups
Tree.hpp
Go to the documentation of this file.
1 /*
2  * Tree.h
3  *
4  * Created on: May 14, 2012
5  * Author: Carsten Kemena
6  *
7  * This file is part of MDAT.
8  *
9  * MDAT is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * MDAT is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with MDAT. If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23 
28 #ifndef TREE_H_
29 #define TREE_H_
30 
31 
32 // C header
33 #include <cfloat>
34 #include <cstdio>
35 #include <cmath>
36 #include <cstdlib>
37 
38 // C++ header
39 #include <map>
40 #include <stack>
41 #include <string>
42 #include <vector>
43 #include <memory>
44 
45 // MDAT header
46 #include "../Sequence/SequenceSet.hpp"
47 #include "../utils/Matrix.hpp"
48 
49 
50 namespace MDAT {
51 
52 
53 struct TreeNode
54 {
55  unsigned int id;
56  std::string name;
57  float edge_length;
58  std::vector<std::shared_ptr<TreeNode> > children;
59 
60  TreeNode():id(0), name(""), edge_length(-1), children()
61  {}
62 
63  TreeNode(unsigned int id_):id(id_), name(""), edge_length(-1), children()
64  {}
65 
66  TreeNode(const std::string &name_):id(0), name(name_), edge_length(-1), children()
67  {}
68 
69  TreeNode(unsigned int id_, const std::string &name_):id(id_),name(name_), edge_length(-1), children()
70  {}
71 
72  bool
73  is_leaf() const
74  {
75  return (!children.empty());
76  }
77 
78 
79 };
80 
81 
85 class Tree {
86 private:
87  TreeNode *_root;
88  size_t _n_species;
89 
90 public:
91 
93 
94 
97  Tree();
98  Tree(const MDAT::Tree&) = delete;
99  virtual ~Tree();
102  Tree& operator=(const Tree&) = delete;
103  Tree& operator=(Tree &&other)
104  {
105  if (this != &other)
106  {
107  clear();
108  _root=other._root;
109  _n_species=other._n_species;
110  other._root=nullptr;
111  other._n_species=0;
112  }
113  return *this;
114  }
115 
117 
118 
128  void nj(const Matrix<float> &dist_mat, const std::vector<std::string> &names);
129 
138  //void upgma(const Matrix<float> &dist_mat, const std::vector<std::string> &names);
139  void upgma(const Matrix<float> &dist_mat, const std::vector<std::string> &names, std::vector<int> &n_members);
146  void clear();
147 
152  size_t n_species() const
153  {
154  return _n_species;
155  }
156 
161  const TreeNode* root() const
162  {
163  return _root;
164  }
165 
166  TreeNode* root()
167  {
168  return _root;
169  }
170 
175  bool empty() const
176  {
177  return (_n_species==0);
178  }
179 };
180 
181 
182 } /* namespace MDAT */
183 
184 #endif /* TREE_H_ */