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 += "]"
39 + std::to_string(uniq) +
40 ":" + double2string(t);
41 return o;
42}
43
46string_t
48(const slate_t& tnow, const slate_t& tpar,
49 bool showdeme, bool extended) const
50{
51 string_t o1 = "", o2 = "", o3 = "";
52 int n = nchildren();
53 if (n > 0) {
54 o1 = "("; o3 = ")";
55 }
56 if (extended) {
57 o3 += "[&&PhyloPOMP ";
58 if (holds(blue))
59 o3 += "type=sample";
60 else if (holds_own())
61 o3 += "type=root";
62 else
63 o3 += "type=node";
64 if (showdeme && deme() != undeme)
65 o3 += " deme=" + std::to_string(deme());
66 o3 += "]";
67 }
68 n = 0;
69 for (ball_t *b : *this) {
70 node_t *p = 0;
71 switch (b->color) {
72 case green:
73 p = b->child();
74 if (p != this) {
75 if (n++ > 0) o2 += ",";
76 o2 += p->newick(tnow,slate,showdeme,extended);
77 }
78 break;
79 case black:
80 assert(extended);
81 if (n++ > 0) o2 += ",";
82 o2 += b->newick(tnow-slate,showdeme);
83 break;
84 case blue:
85 break;
86 }
87 }
88 return o1 + o2 + o3
89 + std::to_string(uniq)
90 + ":" + double2string(slate - tpar);
91}
92
94string_t
96(slate_t t, slate_t te, bool showdeme, bool extended) const
97{
98 string_t o = "";
99 for (node_t *p : *this) {
100 if (p->is_root()) {
101 o += p->newick(t,te,showdeme,extended) + ";";
102 }
103 }
104 return o;
105}
106
108string_t
110(bool extended) const
111{
112 return nodeseq_t::newick(time(),timezero(),(ndeme() > 0),extended);
113}
114
115extern "C" {
116
118 SEXP newick (SEXP State, SEXP Extended) {
119 PROTECT(Extended = AS_LOGICAL(Extended));
120 bool extended = *LOGICAL(Extended);
121 genealogy_t A(State);
122 UNPROTECT(1);
123 return mkString(A.newick(extended).c_str());
124 }
125
126}
@ 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
name_t uniq
Definition ball.h:35
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
Encodes a genealogy.
Definition genealogy.h:19
size_t ndeme(void) const
number of demes
Definition genealogy.h:59
slate_t & timezero(void)
view/set zero time.
Definition genealogy.h:153
slate_t & time(void)
view/set current time.
Definition genealogy.h:145
string_t newick(bool extended=true) const
put genealogy at current time into Newick format.
Definition newick.cc:110
Encodes a genealogical node.
Definition node.h:21
bool is_root(void) const
Definition node.h:121
node_t(name_t u=0, slate_t t=R_NaReal)
basic constructor for node class
Definition node.h:66
name_t deme(void) const
view deme
Definition node.h:96
name_t uniq
Definition node.h:32
string_t newick(const slate_t &tnow, const slate_t &tpar, bool showdeme, bool extended) const
Newick-format output.
Definition newick.cc:48
slate_t slate
Definition node.h:33
bool holds_own(void) const
Definition node.h:118
int nchildren(void) const
number of descendants
Definition node.h:131
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:96
bool holds(ball_t *b) const
does this node hold the given ball?
Definition pocket.h:109
#define err(...)
Definition internal.h:18
double slate_t
Definition internal.h:53
#define n
Definition lbdp_pomp.c:9
SEXP newick(SEXP State, SEXP Extended)
tree in newick format
Definition newick.cc:118
std::string double2string(double value)
Definition newick.cc:17