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

A template for a class that renders newick trees as SVG. More...

#include <jade.svg_tree.hpp>

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

Public Types

typedef TValue value_type
 The value type. More...
 
typedef basic_newick_node< value_typenode_type
 The Newick node type. More...
 
typedef basic_vec2< value_typevec2_type
 The vector type. More...
 

Public Member Functions

 basic_svg_tree (const node_type &node)
 Initializes a new instance of the class based on the specified node. More...
 
void optimize_positions ()
 Optimizes the positions of the vertices using the Nelder-Mead Simplex method. More...
 
std::string str () const
 Returns the SVG scene as a string. More...
 
void write (std::ostream &out) const
 Writes the SVG scene to the specified output stream. More...
 
void write (char const *const path) const
 Writes the SVG scene to the specified output file. More...
 
void write (const std::string &path) const
 Writes the SVG scene to the specified output file. More...
 

Detailed Description

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

A template for a class that renders newick trees as SVG.

Definition at line 20 of file jade.svg_tree.hpp.

Member Typedef Documentation

◆ node_type

template<typename TValue >
typedef basic_newick_node<value_type> jade::basic_svg_tree< TValue >::node_type

The Newick node type.

Definition at line 27 of file jade.svg_tree.hpp.

◆ value_type

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

The value type.

Definition at line 24 of file jade.svg_tree.hpp.

◆ vec2_type

template<typename TValue >
typedef basic_vec2<value_type> jade::basic_svg_tree< TValue >::vec2_type

The vector type.

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

Constructor & Destructor Documentation

◆ basic_svg_tree()

template<typename TValue >
jade::basic_svg_tree< TValue >::basic_svg_tree ( const node_type node)
inlineexplicit

Initializes a new instance of the class based on the specified node.

Parameters
nodeThe newick node.

Definition at line 36 of file jade.svg_tree.hpp.

38  : _root (node.get_id())
39  , _table ()
40  {
41  _init_table(node);
42  _update_table(node);
43  }

Member Function Documentation

◆ optimize_positions()

template<typename TValue >
void jade::basic_svg_tree< TValue >::optimize_positions ( )
inline

Optimizes the positions of the vertices using the Nelder-Mead Simplex method.

Definition at line 49 of file jade.svg_tree.hpp.

50  {
51  typedef basic_simplex<value_type> simplex_type;
52  typedef typename simplex_type::container_type container_type;
53  typedef typename simplex_type::execute_args execute_args_type;
54  typedef typename simplex_type::options options_type;
55 
56  //
57  // Updates the radians and positions of each node based on a
58  // specified Nelder-Mead parameter set.
59  //
60  const auto import_params = [this](
61  const container_type & params) -> void
62  {
63  auto dst = _table.begin();
64  for (auto src = params.begin(); src != params.end(); ++src)
65  (dst++)->second.radians = *src;
66  _update_table(*_table.find(_root)->second.node);
67  };
68 
69  //
70  // Computes the objective function for the Nelder-Mead method. The
71  // value returned is the sum of the inverses of the distances
72  // between all pairs of distinct nodes.
73  //
74  const auto objfunc = [this, import_params](
75  const container_type & params) -> value_type
76  {
77  import_params(params);
78  auto sum = value_type(0);
79  for (const auto & a0 : _table)
80  for (const auto & b0 : _table)
81  if (a0.first != b0.first)
82  {
83  const auto a = a0.second.position;
84  const auto b = b0.second.position;
85  const auto d = vec2_type::distance_squared(a, b);
86  if (d < 1e-6) // epsilon
87  return std::numeric_limits<value_type>::max();
88  sum += value_type(1) / d;
89  }
90  return sum;
91  };
92 
93  //
94  // Converts a specified number of degrees to radians.
95  //
96  const auto to_radians = [](const value_type deg) -> value_type
97  {
98  static const auto tau = value_type(2.0 * std::acos(-1.0));
99  return deg * tau / value_type(360.0);
100  };
101 
102  const auto n = _table.size();
103 
104  //
105  // Construct the simplex using a unit size of one degree and the
106  // current positions of the nodes.
107  //
108  options_type opts (n);
109  opts.unit = to_radians(1);
110  opts.vertex.clear();
111  for (const auto & p : _table)
112  opts.vertex.push_back(p.second.radians);
113  simplex_type simplex (objfunc, opts);
114 
115  //
116  // Execute the Nelder-Mead method until the simplex has collapsed
117  // or a maximum timeout occurs; execute at least a minimum number
118  // of iterations.
119  //
120  execute_args_type exe_args_1;
121  exe_args_1.max_iterations = 100;
122  exe_args_1.max_seconds = 0.5;
123  simplex.execute(objfunc, exe_args_1);
124 
125  execute_args_type exe_args_2;
126  exe_args_2.min_length = to_radians(0.01) * value_type(n);
127  exe_args_2.max_seconds = 0.5;
128  simplex.execute(objfunc, exe_args_2);
129 
130  //
131  // Import the final parameters from the simplex.
132  //
133  import_params(simplex.get_vertex());
134  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ str()

template<typename TValue >
std::string jade::basic_svg_tree< TValue >::str ( ) const
inline

Returns the SVG scene as a string.

Returns
The SVG scene as a string.

Definition at line 141 of file jade.svg_tree.hpp.

142  {
143  std::ostringstream out;
144  write(out);
145  return out.str();
146  }
+ Here is the call graph for this function:

◆ write() [1/3]

template<typename TValue >
void jade::basic_svg_tree< TValue >::write ( char const *const  path) const
inline

Writes the SVG scene to the specified output file.

Parameters
pathThe path to the output file.

Definition at line 168 of file jade.svg_tree.hpp.

171  {
172  assert(path != nullptr);
173  std::ofstream out (path);
174  if (!out.good())
175  throw error() << "error opening '" << path << "' for writing";
176  write(out);
177  }
+ Here is the call graph for this function:

◆ write() [2/3]

template<typename TValue >
void jade::basic_svg_tree< TValue >::write ( const std::string &  path) const
inline

Writes the SVG scene to the specified output file.

Parameters
pathThe path to the output file.

Definition at line 182 of file jade.svg_tree.hpp.

185  {
186  write(path.c_str());
187  }
+ Here is the call graph for this function:

◆ write() [3/3]

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

Writes the SVG scene to the specified output stream.

Parameters
outThe output stream.

Definition at line 151 of file jade.svg_tree.hpp.

154  {
155  const auto & root = *_table.find(_root)->second.node;
156  metrics_data metrics;
157  _create_metrics(metrics);
158 
159  _write_svg_header(out, metrics);
160  _write_edges(out, metrics, root);
161  _write_nodes(out, metrics, root);
162  _write_svg_footer(out);
163  }
+ Here is the caller graph for this function:

The documentation for this class was generated from the following file:
jade::basic_svg_tree::value_type
TValue value_type
The value type.
Definition: jade.svg_tree.hpp:24
jade::basic_svg_tree::write
void write(std::ostream &out) const
Writes the SVG scene to the specified output stream.
Definition: jade.svg_tree.hpp:151
jade::basic_vec2< value_type >::distance_squared
static value_type distance_squared(const basic_vec2 &a, const basic_vec2 &b)
Definition: jade.vec2.hpp:69