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
13string_t
15(const slate_t &t, bool showdeme) const
16{
17 assert(color==black);
18 string_t o = "[&&PhyloPOMP type=extant";
19 if (showdeme)
20 o += " deme=" + std::to_string(deme());
21 o += "]"
22 + std::to_string(uniq) +
23 ":" + std::to_string(t);
24 return o;
25}
26
29string_t
31(const slate_t& tnow, const slate_t& tpar,
32 bool showdeme, bool extended) const
33{
34 string_t o1 = "", o2 = "", o3 = "";
35 int n = nchildren();
36 if (n > 0) {
37 o1 = "("; o3 = ")";
38 }
39 if (extended) {
40 o3 += "[&&PhyloPOMP ";
41 if (holds(blue))
42 o3 += "type=sample";
43 else if (holds_own())
44 o3 += "type=root";
45 else
46 o3 += "type=node";
47 if (showdeme)
48 o3 += " deme=" + std::to_string(deme());
49 o3 += "]";
50 }
51 n = 0;
52 for (ball_t *b : *this) {
53 node_t *p = 0;
54 switch (b->color) {
55 case green:
56 p = b->child();
57 if (p != this) {
58 if (n++ > 0) o2 += ",";
59 o2 += p->newick(tnow,slate,showdeme,extended);
60 }
61 break;
62 case black:
63 assert(extended);
64 if (n++ > 0) o2 += ",";
65 o2 += b->newick(tnow-slate,showdeme);
66 break;
67 case blue:
68 break;
69 }
70 }
71 return o1 + o2 + o3
72 + std::to_string(uniq)
73 + ":" + std::to_string(slate - tpar);
74}
75
77string_t
79(slate_t t, bool showdeme, bool extended) const
80{
81 slate_t te = dawn();
82 string_t o = "";
83 for (node_t *p : *this) {
84 if (p->is_root()) {
85 o += p->newick(t,te,showdeme,extended) + ";";
86 }
87 }
88 return o;
89}
90
92string_t
94(bool extended) const
95{
96 return nodeseq_t::newick(time(),(ndeme() > 0),extended);
97}
98
99extern "C" {
100
102 SEXP newick (SEXP State, SEXP Extended) {
103 PROTECT(Extended = AS_LOGICAL(Extended));
104 bool extended = *LOGICAL(Extended);
105 genealogy_t A(State);
106 UNPROTECT(1);
107 return mkString(A.newick(extended).c_str());
108 }
109
110}
@ green
Definition ball.h:12
@ black
Definition ball.h:12
@ blue
Definition ball.h:12
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:15
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 & 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:94
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:31
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
slate_t dawn(void) const
Earliest time in the sequence.
Definition nodeseq.h:110
string_t newick(slate_t t, bool showdeme, bool extended) const
put genealogy at time t into Newick format.
Definition newick.cc:79
bool holds(ball_t *b) const
does this node hold the given ball?
Definition pocket.h:109
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:102