A toolkit for working with phylogenetic data.
v0.18.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2017 Lucas Czech
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 <assert.h>
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  DefaultTreeNewickWriterPlugin::register_with( *this );
163  }
164 };
165 
166 } // namespace placement
167 } // namespace genesis
168 
169 #endif // include guard
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...
size_t index() const
Return the index of this Edge.
Definition: edge.cpp:45
Data class for PlacementTreeEdges. Stores the branch length of the edge, and the edge_num, as defined in the jplace standard.
Provide a set of plugin functions for NewickWriter to write a DefaultTree.
Write a Tree to Newick format.
PlacementTreeNewickWriterPlugin & operator=(PlacementTreeNewickWriterPlugin const &)=default
std::string to_string(T const &v)
Return a string representation of a given value.
Definition: string.hpp:300
std::vector< std::string > comments
Arbitrary strings that can be attached to a node, e.g. in Newick format via "[]". ...
Definition: element.hpp:114
std::vector< std::string > tags
Arbitrary strings that can be attached to a node, e.g. in Newick format via "{}". ...
Definition: element.hpp:109
Manage a set of Pqueries along with the PlacementTree where the PqueryPlacements are placed on...
Definition: sample.hpp:68
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...
EdgeDataType & data()
Definition: edge.hpp:118
void edge_to_element(tree::TreeEdge const &edge, tree::NewickBrokerElement &element) const
Store the information for one element of a Newick tree.
Definition: element.hpp:60
int edge_num() const
Return the edge_num of this edge. This value is defined by the jplace standard.