A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tree/drawing/functions.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2018 Lucas Czech and HITS gGmbH
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
32 
39 
45 
51 
55 
56 #include <fstream>
57 #include <memory>
58 #include <stdexcept>
59 
60 namespace genesis {
61 namespace tree {
62 
63 // =================================================================================================
64 // Newick Functions
65 // =================================================================================================
66 
68  DefaultTree const& tree,
69  std::string const& newick_filename
70 ) {
71  DefaultTreeNewickWriter().to_file( tree, newick_filename );
72 }
73 
74 // =================================================================================================
75 // Phyloxml Functions
76 // =================================================================================================
77 
79  DefaultTree const& tree,
80  std::string const& phyloxml_filename
81 ) {
83  tree, {}, phyloxml_filename
84  );
85 }
86 
88  DefaultTree const& tree,
89  std::vector<utils::Color> const& color_per_branch,
90  std::string const& phyloxml_filename
91 ) {
92  // We use a normal Phyloxml writer...
93  auto writer = DefaultTreePhyloxmlWriter();
94 
95  // ... but also wrap it in a Color Mixin in order to allow for color branches if needed.
96  auto color_plugin = tree::PhyloxmlColorWriterPlugin();
97  if( ! color_per_branch.empty() ) {
98  color_plugin.register_with( writer );
99  color_plugin.edge_colors( color_per_branch );
100  }
101 
102  writer.to_file( tree, phyloxml_filename );
103 }
104 
106  DefaultTree const& tree,
107  std::vector<double> const& value_per_branch,
108  utils::ColorMap const& color_map,
109  utils::ColorNormalization const& color_norm,
110  std::string const& phyloxml_filename
111 ) {
113  tree, color_map( color_norm, value_per_branch ), phyloxml_filename
114  );
115 }
116 
117 // =================================================================================================
118 // Nexus Functions
119 // =================================================================================================
120 
122  DefaultTree const& tree,
123  std::string const& nexus_filename
124 ) {
126  tree, {}, nexus_filename
127  );
128 }
129 
131  DefaultTree const& tree,
132  std::vector<utils::Color> const& color_per_branch,
133  std::string const& nexus_filename
134 ) {
135  // We use a normal Newick writer...
136  auto newick_writer = DefaultTreeNewickWriter();
137 
138  // ... but also wrap it in a Color Mixin in order to allow for color branches if needed.
139  auto color_plugin = tree::NewickColorWriterPlugin();
140  if( ! color_per_branch.empty() ) {
141  color_plugin.register_with( newick_writer );
142  color_plugin.edge_colors( color_per_branch );
143  }
144 
145  // Create an (empty) Nexus document.
146  auto nexus_doc = utils::NexusDocument();
147 
148  // Add the taxa of the tree to the document.
149  auto taxa = utils::make_unique<utils::NexusTaxa>();
150  taxa->add_taxa( node_names( tree ));
151  nexus_doc.set_block( std::move( taxa ));
152 
153  // Add the tree itself to the document.
154  auto trees = utils::make_unique<utils::NexusTrees>();
155  trees->add_tree( "tree1", newick_writer.to_string(tree) );
156  nexus_doc.set_block( std::move(trees) );
157 
158  // Write the document to a Nexus file.
159  auto nexus_writer = utils::NexusWriter();
160  nexus_writer.to_file( nexus_doc, nexus_filename );
161 }
162 
164  DefaultTree const& tree,
165  std::vector<double> const& value_per_branch,
166  utils::ColorMap const& color_map,
167  utils::ColorNormalization const& color_norm,
168  std::string const& nexus_filename
169 ) {
171  tree, color_map( color_norm, value_per_branch ), nexus_filename
172  );
173 }
174 
175 // =================================================================================================
176 // SVG Functions
177 // =================================================================================================
178 
180  DefaultTree const& tree,
181  LayoutParameters const& params,
182  std::string const& svg_filename
183 ) {
185  tree, params, std::vector<utils::Color>{}, {}, {}, svg_filename
186  );
187 }
188 
190  DefaultTree const& tree,
191  LayoutParameters const& params,
192  std::vector<utils::Color> const& color_per_branch,
193  std::string const& svg_filename
194 ) {
196  tree, params, color_per_branch, {}, {}, svg_filename
197  );
198 }
199 
201  DefaultTree const& tree,
202  LayoutParameters const& params,
203  std::vector<double> const& value_per_branch,
204  utils::ColorMap const& color_map,
205  utils::ColorNormalization const& color_norm,
206  std::string const& svg_filename
207 ) {
209  tree, params, color_map( color_norm, value_per_branch ), color_map, color_norm, svg_filename
210  );
211 }
212 
214  DefaultTree const& tree,
215  LayoutParameters const& params,
216  std::vector<utils::Color> const& color_per_branch,
217  utils::ColorMap const& color_map,
218  utils::ColorNormalization const& color_norm,
219  std::string const& svg_filename
220 ) {
221 
222  // Make a layout tree.
223  std::unique_ptr<LayoutBase> layout = [&]() -> std::unique_ptr<LayoutBase> {
224  if( params.shape == LayoutShape::kCircular ) {
225  return utils::make_unique<CircularLayout>( tree, params.type, params.ladderize );
226  }
227  if( params.shape == LayoutShape::kRectangular ) {
228  return utils::make_unique<RectangularLayout>( tree, params.type, params.ladderize );
229  }
230  throw std::runtime_error( "Unknown Tree shape parameter." );
231  }();
232 
233  // Set edge colors.
234  if( ! color_per_branch.empty() ) {
235  std::vector<utils::SvgStroke> strokes;
236  for( auto const& color : color_per_branch ) {
237  auto stroke = params.stroke;
238  stroke.color = color;
239  stroke.line_cap = utils::SvgStroke::LineCap::kRound;
240  strokes.push_back( std::move( stroke ));
241  }
242  layout->set_edge_strokes( strokes );
243  }
244 
245  // Prepare svg doc.
246  auto svg_doc = layout->to_svg_document();
247 
248  // Add scale.
249  if( ! color_map.empty() ) {
250  // Make the scale.
251  auto svg_pal = utils::SvgColorBarSettings();
252  svg_pal.height = svg_doc.bounding_box().height() / 2.0;
253  svg_pal.width = svg_pal.height / 10.0;
254  auto svg_scale = make_svg_color_bar( svg_pal, color_map, color_norm );
255 
256  // Move it to the bottom right corner.
257  if( params.shape == LayoutShape::kCircular ) {
258  svg_scale.second.transform.append( utils::SvgTransform::Translate(
259  svg_doc.bounding_box().width() / 2.0, 0.0
260  ));
261  }
262  if( params.shape == LayoutShape::kRectangular ) {
263  svg_scale.second.transform.append( utils::SvgTransform::Translate(
264  svg_doc.bounding_box().width(), svg_pal.height / 2.0
265  ));
266  }
267 
268  // Add it to the doc.
269  if( ! svg_scale.first.empty() ) {
270  svg_doc.defs.push_back( svg_scale.first );
271  }
272  svg_doc.add( svg_scale.second );
273  }
274 
275  // Write to file.
276  svg_doc.margin.left = svg_doc.margin.top = svg_doc.margin.bottom = svg_doc.margin.right = 200;
277  std::ofstream ofs;
278  utils::file_output_stream( svg_filename, ofs );
279  svg_doc.write( ofs );
280 }
281 
282 } // namespace tree
283 } // namespace genesis
bool empty() const
Return whether the Palette is empty, that is, no colors were set.
Definition: map.hpp:230
std::unordered_set< std::string > node_names(Tree const &tree, bool leaves_only)
Returns an unordered set of all TreeNode names of a Tree.
void file_output_stream(std::string const &filename, std::ofstream &out_stream, std::ios_base::openmode mode=std::ios_base::out)
Helper function to obtain an output stream to a file.
Plugin class for PhyloXML output that allows coloring of edges.
Store a list of colors and offer them as a map for values in range [ 0.0, 1.0 ].
Definition: map.hpp:61
void write_tree_to_phyloxml_file(DefaultTree const &tree, std::string const &phyloxml_filename)
Write a phyloxml file containing a tree.
utils::SvgDocument to_svg_document() const
Write default Newick trees, i.e., trees with names and branch lengths.
Provides some valuable additions to STD.
void write_tree_to_nexus_file(DefaultTree const &tree, std::string const &nexus_filename)
Write a nexus file containing a tree.
void to_file(Tree const &tree, std::string const &filename) const
Writes the tree to a file in Newick format.
void write_color_tree_to_nexus_file(DefaultTree const &tree, std::vector< utils::Color > const &color_per_branch, std::string const &nexus_filename)
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:95
Plugin class for Newick output that allows coloring of edges.
void write_color_tree_to_phyloxml_file(DefaultTree const &tree, std::vector< utils::Color > const &color_per_branch, std::string const &phyloxml_filename)
Provides functions for accessing the file system.
Base class for color normalization.
void set_edge_strokes(utils::SvgStroke const &stroke)
Definition: layout_base.cpp:74
std::pair< SvgGradientLinear, SvgGroup > make_svg_color_bar(SvgColorBarSettings const &settings, ColorMap const &map, ColorNormalization const &norm, std::string const &id)
Definition: color_bar.cpp:317
Default Tree functions.
void write_color_tree_to_svg_file(DefaultTree const &tree, LayoutParameters const &params, std::vector< utils::Color > const &color_per_branch, std::string const &svg_filename)
void write_tree_to_svg_file(DefaultTree const &tree, LayoutParameters const &params, std::string const &svg_filename)
void write_tree_to_newick_file(DefaultTree const &tree, std::string const &newick_filename)
Write a newick file containing a tree.