A library for working with phylogenetic and population genetic data.
v0.27.0
placement/formats/newick_writer.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_PLACEMENT_FORMATS_NEWICK_WRITER_H_
2 #define GENESIS_PLACEMENT_FORMATS_NEWICK_WRITER_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2019 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
34 #include <cassert>
35 #include <stdexcept>
36 #include <string>
37 #include <vector>
38 
44 
45 namespace genesis {
46 namespace placement {
47 
48 // =================================================================================================
49 // Placement Tree Newick Writer Plugin
50 // =================================================================================================
51 
56 {
57 public:
58 
59  // -------------------------------------------------------------------------
60  // Constructor and Rule of Five
61  // -------------------------------------------------------------------------
62 
64  virtual ~PlacementTreeNewickWriterPlugin() = default;
65 
68 
71 
72  // -------------------------------------------------------------------------
73  // Properties
74  // -------------------------------------------------------------------------
75 
76  bool enable_edge_nums() const
77  {
78  return enable_edge_nums_;
79  }
80 
81  void enable_edge_nums(bool value)
82  {
83  enable_edge_nums_ = value;
84  }
85 
87  {
88  return enable_placement_counts_;
89  }
90 
91  void enable_placement_counts(bool value)
92  {
93  enable_placement_counts_ = value;
94  }
95 
96  void prepare_sample( Sample const& smp )
97  {
98  auto place_map = placements_per_edge( smp );
99  placement_counts_.resize( place_map.size(), 0 );
100 
101  for( size_t edge_i = 0; edge_i < place_map.size(); ++edge_i ) {
102  placement_counts_[ edge_i ] = place_map[ edge_i ].size();
103  }
104  }
105 
106  // -------------------------------------------------------------------------
107  // Plugin Functions
108  // -------------------------------------------------------------------------
109 
110  void edge_to_element( tree::TreeEdge const& edge, tree::NewickBrokerElement& element ) const
111  {
112  if (enable_edge_nums_) {
113  element.tags.push_back( std::to_string(
115  ));
116  }
117  if (enable_placement_counts_) {
118  element.comments.push_back(std::to_string( placement_counts_[ edge.index() ]));
119  }
120  }
121 
122  void register_with( tree::NewickWriter& writer ) const
123  {
124  // Set edge functions.
125  writer.edge_to_element_plugins.push_back(
126  [&]( tree::TreeEdge const& edge, tree::NewickBrokerElement& element ) {
128  }
129  );
130  }
131 
132  // -------------------------------------------------------------------------
133  // Data Members
134  // -------------------------------------------------------------------------
135 
136 private:
137 
138  bool enable_edge_nums_ = true;
139  bool enable_placement_counts_ = false;
140 
141  std::vector<size_t> placement_counts_;
142 };
143 
144 // =================================================================================================
145 // Placement Tree Newick Writer
146 // =================================================================================================
147 
149  : public tree::NewickWriter
152 {
153 public:
154 
155  // -------------------------------------------------------------------------
156  // Constructor and Rule of Five
157  // -------------------------------------------------------------------------
158 
160  {
161  CommonTreeNewickWriterPlugin::register_with( *this );
163  }
164 };
165 
166 } // namespace placement
167 } // namespace genesis
168 
169 #endif // include guard
genesis::placement::PlacementTreeNewickWriterPlugin::register_with
void register_with(tree::NewickWriter &writer) const
Definition: placement/formats/newick_writer.hpp:122
genesis::placement::PlacementTreeNewickWriterPlugin::edge_to_element
void edge_to_element(tree::TreeEdge const &edge, tree::NewickBrokerElement &element) const
Definition: placement/formats/newick_writer.hpp:110
genesis::placement::PlacementTreeNewickWriterPlugin::prepare_sample
void prepare_sample(Sample const &smp)
Definition: placement/formats/newick_writer.hpp:96
genesis::placement::PlacementTreeNewickWriterPlugin::PlacementTreeNewickWriterPlugin
PlacementTreeNewickWriterPlugin()=default
genesis::tree::NewickBrokerElement::comments
std::vector< std::string > comments
Arbitrary strings that can be attached to a node, e.g. in Newick format via "[]".
Definition: element.hpp:132
genesis::placement::PlacementTreeNewickWriterPlugin::operator=
PlacementTreeNewickWriterPlugin & operator=(PlacementTreeNewickWriterPlugin const &)=default
genesis::placement::PlacementEdgeData::edge_num
EdgeNumType edge_num() const
Return the edge_num of this edge. This value is defined by the jplace standard.
Definition: placement_tree.hpp:194
placement_tree.hpp
genesis::tree::NewickWriter
Write a Tree to Newick format.
Definition: tree/formats/newick/writer.hpp:102
genesis::placement::Sample
Manage a set of Pqueries along with the PlacementTree where the PqueryPlacements are placed on.
Definition: sample.hpp:68
genesis::tree::TreeEdge::index
size_t index() const
Return the index of this Edge.
Definition: edge.hpp:106
genesis::population::to_string
std::string to_string(GenomeLocus const &locus)
Definition: functions/genome_locus.hpp:48
genesis::placement::PlacementEdgeData
Data class for PlacementTreeEdges. Stores the branch length of the edge, and the edge_num,...
Definition: placement_tree.hpp:139
genesis::placement::PlacementTreeNewickWriterPlugin::enable_placement_counts
void enable_placement_counts(bool value)
Definition: placement/formats/newick_writer.hpp:91
newick_writer.hpp
genesis::tree::NewickWriter::edge_to_element_plugins
std::vector< edge_to_element_function > edge_to_element_plugins
Collect all functions to be called for each TreeEdge in order to translate it to a Newick representat...
Definition: tree/formats/newick/writer.hpp:218
genesis::tree::NewickBrokerElement::tags
std::vector< std::string > tags
Arbitrary strings that can be attached to a node, e.g. in Newick format via "{}".
Definition: element.hpp:127
genesis::tree::TreeEdge
Definition: edge.hpp:60
writer.hpp
genesis::placement::PlacementTreeNewickWriterPlugin::~PlacementTreeNewickWriterPlugin
virtual ~PlacementTreeNewickWriterPlugin()=default
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::placement::PlacementTreeNewickWriterPlugin::enable_placement_counts
bool enable_placement_counts() const
Definition: placement/formats/newick_writer.hpp:86
genesis::tree::CommonTreeNewickWriterPlugin
Provide a set of plugin functions for NewickWriter to write a CommonTree.
Definition: tree/common_tree/newick_writer.hpp:51
genesis::placement::PlacementTreeNewickWriter
Definition: placement/formats/newick_writer.hpp:148
genesis::placement::PlacementTreeNewickWriterPlugin::enable_edge_nums
bool enable_edge_nums() const
Definition: placement/formats/newick_writer.hpp:76
genesis::placement::placements_per_edge
std::vector< std::vector< PqueryPlacement const * > > placements_per_edge(Sample const &smp, bool only_max_lwr_placements)
Return a mapping from each PlacementTreeEdges to the PqueryPlacements that are placed on that edge.
Definition: placement/function/helper.cpp:106
genesis::placement::PlacementTreeNewickWriterPlugin
Definition: placement/formats/newick_writer.hpp:55
sample.hpp
genesis::tree::TreeEdge::data
EdgeDataType & data()
Definition: edge.hpp:217
genesis::tree::NewickBrokerElement
Store the information for one element of a Newick tree.
Definition: element.hpp:60
genesis::placement::PlacementTreeNewickWriter::PlacementTreeNewickWriter
PlacementTreeNewickWriter()
Definition: placement/formats/newick_writer.hpp:159
genesis::placement::PlacementTreeNewickWriterPlugin::enable_edge_nums
void enable_edge_nums(bool value)
Definition: placement/formats/newick_writer.hpp:81
helper.hpp