Quetzal-CoaTL
The Coalescence Template Library
Loading...
Searching...
No Matches
to_k_ary_tree.hpp
1// Copyright 2021 Arnaud Becheler <abechele@umich.edu>
2
10
11#pragma once
12
13#include "quetzal/coalescence/graph/k_ary_tree.hpp"
14#include "quetzal/io/newick/ast.hpp"
15#include "quetzal/io/newick/generator.hpp"
16#include "quetzal/io/newick/parser.hpp"
17
18#include <concepts>
19#include <ranges>
20#include <string>
21#include <tuple>
22#include <vector>
23
25{
26
30template <class VertexProperty = quetzal::format::newick::ast::node::vertex_property,
32std::tuple<quetzal::coalescence::k_ary_tree<VertexProperty, EdgeProperty>,
35{
37 using vertex_descriptor = typename tree_type::vertex_descriptor;
38 tree_type tree;
39 auto root = tree.add_vertex(VertexProperty{ast_root.name});
40
41 // A hacky recursive lambda (C++23 will make this much simpler)
42 static const auto populate = [](tree_type &graph, vertex_descriptor parent, const auto &ast) {
43 static auto recursive = [](tree_type &graph, vertex_descriptor parent, const auto &ast,
44 auto &self) mutable -> void {
45 if (ast.children.size() > 0)
46 {
47 assert(ast.children.size() != 0);
48 assert(ast.children.size() > 1);
49
50 std::vector<std::tuple<vertex_descriptor, EdgeProperty>> children;
51 children.reserve(ast.children.size());
52
53 for (const auto &ast_child : ast.children)
54 {
55 children.push_back(std::make_tuple(graph.add_vertex(VertexProperty{ast_child.name}),
56 EdgeProperty{ast_child.distance_to_parent}));
57 }
58
59 graph.add_edges(parent, children);
60
61 for (int i = 0; i < children.size(); i++)
62 {
63 self(graph, std::get<0>(children[i]), ast.children[i], self);
64 }
65 }
66 }; // end recursive
67 recursive(graph, parent, ast, recursive);
68 }; // end populate
69
70 populate(tree, root, ast_root);
71 return {std::move(tree), root};
72}
73
77template <class VertexProperty = quetzal::format::newick::ast::node::vertex_property,
79std::tuple<quetzal::coalescence::k_ary_tree<VertexProperty, EdgeProperty>,
81to_k_ary_tree(const std::string &newick)
82{
83 // We initialize the root of Abstract Syntax Tree (AST)
85 // We parse the input string into the AST
86 auto ok = quetzal::parse(begin(newick), end(newick), quetzal::format::newick::parser::tree, ast);
87 // todo: handling exceptions in boost::spirit
88 assert(ok);
89 return to_k_ary_tree<VertexProperty, EdgeProperty>(ast);
90}
91
92} // end namespace quetzal::format::newick
Definition k_ary_tree.hpp:349
Generic components for parsing or generating Newick strings.
Definition io.hpp:28
std::tuple< quetzal::coalescence::k_ary_tree< VertexProperty, EdgeProperty >, typename quetzal::coalescence::k_ary_tree< VertexProperty, EdgeProperty >::vertex_descriptor > to_k_ary_tree(quetzal::format::newick::ast::node ast_root)
Converts a standard Newick AST to a quetzal Tree graph.
Definition to_k_ary_tree.hpp:34
Abstract Syntax Tree structure.
Definition ast.hpp:30
double edge_property
The type of edge described by the AST.
Definition ast.hpp:39
vertex_property name
the name of the node
Definition ast.hpp:49
std::string vertex_property
The type of vertex described by the AST.
Definition ast.hpp:34