ʻOhana
Population structure, admixture history, and selection using learning methods.
Data Structures | Public Types | Public Member Functions
jade::basic_neighbor_joining< TValue > Class Template Reference

A template for a class that implements the neighbor joining algorithm. More...

#include <jade.neighbor_joining.hpp>

+ Collaboration diagram for jade::basic_neighbor_joining< TValue >:

Public Types

typedef TValue value_type
 The value type. More...
 
typedef basic_matrix< value_typematrix_type
 The matrix type. More...
 

Public Member Functions

 basic_neighbor_joining (const matrix_type &distances)
 Initializes a new instance of the class. More...
 
std::string str () const
 
void write (std::ostream &out) const
 Writes the constructed tree in Newick format to the specified output stream. More...
 

Detailed Description

template<typename TValue>
class jade::basic_neighbor_joining< TValue >

A template for a class that implements the neighbor joining algorithm.

Definition at line 18 of file jade.neighbor_joining.hpp.

Member Typedef Documentation

◆ matrix_type

template<typename TValue >
typedef basic_matrix<value_type> jade::basic_neighbor_joining< TValue >::matrix_type

The matrix type.

Definition at line 25 of file jade.neighbor_joining.hpp.

◆ value_type

template<typename TValue >
typedef TValue jade::basic_neighbor_joining< TValue >::value_type

The value type.

Definition at line 22 of file jade.neighbor_joining.hpp.

Constructor & Destructor Documentation

◆ basic_neighbor_joining()

template<typename TValue >
jade::basic_neighbor_joining< TValue >::basic_neighbor_joining ( const matrix_type distances)
inlineexplicit

Initializes a new instance of the class.

Parameters
distancesThe distance matrix.

Definition at line 30 of file jade.neighbor_joining.hpp.

32  : _children ()
33  , _lengths ()
34  , _names ()
35  , _root (invalid_id)
36  {
37  //
38  // Allow empty matrices even though no tree will be produced from
39  // the write method.
40  //
41  if (distances.is_empty())
42  return;
43 
44  //
45  // Allow just one node even though the tree produced from the write
46  // method will consist on only the node name ("0").
47  //
48  assert(distances.is_square());
49  auto n = distances.get_height();
50  id_type next_id = 0;
51  if (n == 1)
52  {
53  _root = _add_leaf(next_id);
54  return;
55  }
56 
57  //
58  // Prepare a list of ids for the initial set of nodes.
59  //
60  typedef std::vector<id_type> vector_type;
61  vector_type x;
62  for (size_t i = 0; i < n; i++)
63  x.push_back(_add_leaf(next_id));
64 
65  //
66  // Prepare the distance matrix that will be reduced by the
67  // algorithm.
68  //
69  matrix_type d (distances);
70 
71  //
72  // Loop until there are only two nodes remaining in the distance
73  // matrix.
74  //
75  while (n > 2)
76  {
77  //
78  // Find the minimum Q value in the matrix, and use it to find
79  // the two nodes that will be joined. Join them by creating a
80  // new parent node.
81  //
82  const q_data q (d);
83  const id_type id (next_id++);
84  _add_parent(id, x[q.i], q.d_ik);
85  _add_parent(id, x[q.j], q.d_jk);
86 
87  //
88  // Prepare the new, reduced distance matrix as well as the
89  // corresponding id vector.
90  //
91  matrix_type dd (n - 1, n - 1);
92  vector_type xx { id };
93  for (size_t r = 0, rr = 1; r < n; r++)
94  {
95  if (r == q.i || r == q.j)
96  continue;
97  xx.push_back(x[r]);
98  dd(rr, 0) = value_type(0.5) *
99  (d(r, q.i) + d(r, q.j) - q.d_ij);
100  for (size_t c = 0, cc = 1; c < r; c++)
101  if (c != q.i && c != q.j)
102  dd(rr, cc++) = d(r, c);
103  rr++;
104  }
105 
106  //
107  // Copy the lower triangle to the upper triangle so the data
108  // in the next Q matrix matches the expected values.
109  //
110  dd.copy_lower_to_upper();
111 
112  d.swap(dd);
113  x.swap(xx);
114  n--;
115  }
116 
117  //
118  // Connect the last two nodes; note the loop above places new nodes
119  // at index zero, so here it is known that the leaf node must be at
120  // index 1, and so the root note must be at index 0.
121  //
122  _root = x[0];
123  _add_parent(_root, x[1], d(1, 0));
124  }
+ Here is the call graph for this function:

Member Function Documentation

◆ str()

template<typename TValue >
std::string jade::basic_neighbor_joining< TValue >::str ( ) const
inline
Returns
A string representation of this class.

Definition at line 129 of file jade.neighbor_joining.hpp.

130  {
131  std::ostringstream out;
132  write(out);
133  return out.str();
134  }
+ Here is the call graph for this function:

◆ write()

template<typename TValue >
void jade::basic_neighbor_joining< TValue >::write ( std::ostream &  out) const
inline

Writes the constructed tree in Newick format to the specified output stream.

Parameters
outThe output stream.

Definition at line 140 of file jade.neighbor_joining.hpp.

143  {
144  if (_root != invalid_id)
145  {
146  _write(out, _root);
147  out << ';';
148  }
149  }
+ Here is the caller graph for this function:

The documentation for this class was generated from the following file:
jade::basic_neighbor_joining::write
void write(std::ostream &out) const
Writes the constructed tree in Newick format to the specified output stream.
Definition: jade.neighbor_joining.hpp:140
jade::basic_neighbor_joining::matrix_type
basic_matrix< value_type > matrix_type
The matrix type.
Definition: jade.neighbor_joining.hpp:25
jade::basic_neighbor_joining::value_type
TValue value_type
The value type.
Definition: jade.neighbor_joining.hpp:22