phylopomp
Phylodynamics for POMPs
Loading...
Searching...
No Matches
newick.cc
Go to the documentation of this file.
1#include "ball.h"
2#include "node.h"
3#include "nodeseq.h"
4#include "genealogy.h"
5#include "internal.h"
6
7#include <R.h>
8#include <Rmath.h>
9#include <Rdefines.h>
10
11#include <cstring>
12#include <charconv>
13#include <iostream>
14
15std::string
17(double value)
18{
19 char buffer[32];
20 auto [ptr, ec] = std::to_chars(buffer,buffer+sizeof(buffer),value);
21 if (ec == std::errc()) {
22 return string_t(buffer,ptr);
23 } else {
24 err("error in %s",__func__); // #nocov
25 }
26}
27
30string_t
32(const slate_t &t, bool showdeme) const
33{
34 assert(color==black);
35 string_t o = "[&&PhyloPOMP type=extant";
36 if (showdeme && deme() != undeme)
37 o += " deme=" + std::to_string(deme());
38 o += "]:" + double2string(t);
39 return o;
40}
41
44string_t
46(const slate_t& tnow, const slate_t& tpar,
47 bool showdeme, bool extended) const
48{
49 string_t o1 = "", o2 = "", o3 = "";
50 int n = nchildren();
51 if (n > 0) {
52 o1 = "("; o3 = ")";
53 }
54 if (extended) {
55 o3 += "[&&PhyloPOMP ";
56 if (holds(blue))
57 o3 += "type=sample";
58 else if (is_root())
59 o3 += "type=root";
60 else
61 o3 += "type=node";
62 if (showdeme && deme() != undeme)
63 o3 += " deme=" + std::to_string(deme());
64 o3 += "]";
65 }
66 n = 0;
67 for (ball_t *b : *this) {
68 node_t *p = 0;
69 switch (b->color) {
70 case green:
71 p = b->child();
72 if (p != this) {
73 if (n++ > 0) o2 += ",";
74 o2 += p->newick(tnow,slate,showdeme,extended);
75 }
76 break;
77 case black:
78 assert(extended);
79 if (n++ > 0) o2 += ",";
80 o2 += b->newick(tnow-slate,showdeme);
81 break;
82 case blue:
83 break;
84 }
85 }
86 return o1 + o2 + o3
87 + ":" + double2string(slate - tpar);
88}
89
91string_t
93(slate_t t, slate_t te, bool showdeme, bool extended) const
94{
95 string_t o = "";
96 for (node_t *p : *this) {
97 if (p->is_root()) {
98 o += p->newick(t,te,showdeme,extended) + ";";
99 }
100 }
101 return o;
102}
103
105string_t
107(bool extended) const
108{
109 return nodeseq_t::newick(time(),timezero(),(ndeme() > 0),extended);
110}
@ green
Definition ball.h:12
@ black
Definition ball.h:12
@ blue
Definition ball.h:12
static const name_t undeme
Definition ball.h:15
Balls function as pointers.
Definition ball.h:27
name_t deme(void) const
view deme
Definition ball.h:84
string_t newick(const slate_t &t, bool showdeme) const
Definition newick.cc:32
color_t color
Definition ball.h:36
node_t * child(void) const
a child is the owner of a green ball
Definition ball.h:102
size_t ndeme(void) const
number of demes
Definition genealogy.h:60
slate_t & timezero(void)
view/set zero time.
Definition genealogy.h:154
slate_t & time(void)
view/set current time.
Definition genealogy.h:146
string_t newick(bool extended=true) const
put genealogy at current time into Newick format.
Definition newick.cc:107
Encodes a genealogical node.
Definition node.h:23
bool is_root(void) const
Definition node.h:120
node_t(name_t u=0, slate_t t=R_NaReal)
basic constructor for node class
Definition node.h:68
name_t deme(void) const
view deme
Definition node.h:98
string_t newick(const slate_t &tnow, const slate_t &tpar, bool showdeme, bool extended) const
Newick-format output.
Definition newick.cc:46
slate_t slate
Definition node.h:35
int nchildren(void) const
number of descendants
Definition node.h:130
string_t newick(slate_t t, slate_t te, bool showdeme, bool extended) const
put genealogy at time t into Newick format.
Definition newick.cc:93
bool holds(ball_t *b) const
does this node hold the given ball?
Definition pocket.h:101
#define err(...)
Definition internal.h:18
double slate_t
Definition internal.h:53
#define n
Definition lbdp_pomp.c:9
std::string double2string(double value)
Definition newick.cc:17