ʻOhana
Population structure, admixture history, and selection using learning methods.
jade.cov2nwk.hpp
1 /* -------------------------------------------------------------------------
2  Ohana
3  Copyright (c) 2015-2020 Jade Cheng (\___/)
4  Jade Cheng <info@jade-cheng.com> (='.'=)
5  ------------------------------------------------------------------------- */
6 
7 #ifndef JADE_COV2NWK_HPP__
8 #define JADE_COV2NWK_HPP__
9 
10 #include "jade.neighbor_joining.hpp"
11 #include "jade.verification.hpp"
12 
13 namespace jade
14 {
15  ///
16  /// A template for a class that approximates covariance matrices as
17  /// Newick-formatted trees.
18  ///
19  template <typename TValue>
21  {
22  public:
23  /// The value type.
24  typedef TValue value_type;
25 
26  ///
27  /// Executes the program through the specified streams.
28  ///
29  static void execute(
30  std::istream & in, ///< The input stream.
31  std::ostream & out) ///< The output stream.
32  {
33  typedef basic_matrix<value_type> matrix_type;
34  const matrix_type c (in);
35 
36  typedef basic_verification<value_type> verification_type;
37  verification_type::validate_c(c);
38 
39  const auto rk = c.get_height();
40  const auto k = rk + 1;
41 
42  matrix_type padded_c (k, k);
43  for (size_t i = 0; i < rk; i++)
44  for (size_t j = 0; j < rk; j++)
45  padded_c(i + 1, j + 1) = c(i, j);
46 
47  matrix_type distances (k, k);
48  for (size_t i = 0; i < k; i++)
49  {
50  const auto c_ii = padded_c(i, i);
51  for (size_t j = 0; j < k; j++)
52  {
53  const auto c_jj = padded_c(j, j);
54  const auto c_ij = padded_c(i, j);
55  distances(i, j) = c_ii + c_jj - c_ij - c_ij;
56  }
57  }
58 
59  typedef basic_neighbor_joining<value_type> algorithm_type;
60  const algorithm_type algorithm (distances);
61 
62  algorithm.write(out);
63  out << std::endl;
64  }
65  };
66 }
67 
68 #endif // JADE_COV2NWK_HPP__
jade::basic_neighbor_joining
A template for a class that implements the neighbor joining algorithm.
Definition: jade.neighbor_joining.hpp:19
jade::basic_verification
A template for a class that performs validation on various types of matrices.
Definition: jade.verification.hpp:20
jade::basic_cov2nwk::execute
static void execute(std::istream &in, std::ostream &out)
Executes the program through the specified streams.
Definition: jade.cov2nwk.hpp:29
jade::basic_cov2nwk::value_type
TValue value_type
The value type.
Definition: jade.cov2nwk.hpp:24
jade::basic_cov2nwk
A template for a class that approximates covariance matrices as Newick-formatted trees.
Definition: jade.cov2nwk.hpp:21
jade::basic_matrix< value_type >