phylopomp
Phylodynamics for POMPs
Loading...
Searching...
No Matches
gendat.cc
Go to the documentation of this file.
1// gendat: genealogy information extraction
2
3#include "genealogy.h"
4#include "internal.h"
5
7void
9(double *tout, int *anc, int *lin,
10 int *sat, int *type, int *deme,
11 int *index, int *child) const {
12 int m, n, k;
13 node_it i, j;
14 for (k = 0, n = 0, i = begin(); i != end(); i++, n++) {
15 node_t *p = *i;
16 assert(!p->holds(black)); // tree should be pruned first
17 tout[n] = p->slate;
18 deme[n] = p->deme();
19 if (p->is_root()) {
20 type[n] = 0; // root node
21 } else if (p->holds(blue)) {
22 type[n] = 1; // sample node
23 deme[n] = p->ball(blue)->deme();
24 } else {
25 type[n] = 2; // internal node
26 }
27 lin[n] = p->lineage(); // 0-based indexing
28 sat[n] = p->nchildren();
29 index[n] = k;
30 k += sat[n];
31 child[n] = NA_INTEGER;
32 if (p->is_root()) {
33 anc[n] = n; // 0-based indexing
34 } else {
35 for (m = 0, j = begin(); j != i; j++, m++) {
36 node_t *q = *j;
37 if (p->parent()->uniq == q->uniq) {
38 anc[n] = m;
39 break;
40 }
41 }
42 }
43 }
44 tout[n] = time();
45 for (k = 0, n = 0, i = begin(); i != end(); i++, n++) {
46 node_t *p = *i;
47 j = i; j++;
48 for (m = n+1; j != end(); m++, j++) {
49 node_t *q = *j;
50 if (p->uniq == q->parent()->uniq) {
51 child[k++] = m;
52 }
53 }
54 }
55}
56
58SEXP
60(void) const {
61 SEXP t0, tout, anc, lin, sat, type, deme, index, child, ns, nr, nn;
62 SEXP out, outn;
63 size_t n = length();
64 PROTECT(t0 = NEW_NUMERIC(1));
65 PROTECT(tout = NEW_NUMERIC(n+1));
66 PROTECT(type = NEW_INTEGER(n));
67 PROTECT(deme = NEW_INTEGER(n));
68 PROTECT(lin = NEW_INTEGER(n));
69 PROTECT(sat = NEW_INTEGER(n));
70 PROTECT(index = NEW_INTEGER(n));
71 PROTECT(child = NEW_INTEGER(n));
72 PROTECT(anc = NEW_INTEGER(n));
73 PROTECT(ns = NEW_INTEGER(1));
74 PROTECT(nr = NEW_INTEGER(1));
75 PROTECT(nn = NEW_INTEGER(1));
76 PROTECT(out = NEW_LIST(12));
77 PROTECT(outn = NEW_CHARACTER(12));
78 set_list_elem(out,outn,t0,"t0",0);
79 set_list_elem(out,outn,tout,"nodetime",1);
80 set_list_elem(out,outn,type,"nodetype",2);
81 set_list_elem(out,outn,deme,"deme",3);
82 set_list_elem(out,outn,lin,"lineage",4);
83 set_list_elem(out,outn,sat,"saturation",5);
84 set_list_elem(out,outn,index,"index",6);
85 set_list_elem(out,outn,child,"child",7);
86 set_list_elem(out,outn,anc,"ancestor",8);
87 set_list_elem(out,outn,ns,"nsample",9);
88 set_list_elem(out,outn,nr,"nroot",10);
89 set_list_elem(out,outn,nn,"nnode",11);
90 SET_NAMES(out,outn);
91 gendat(REAL(tout),INTEGER(anc),INTEGER(lin),INTEGER(sat),
92 INTEGER(type),INTEGER(deme),INTEGER(index),INTEGER(child));
93 *REAL(t0) = double(timezero()); // zero-time
94 *INTEGER(ns) = nsample(); // number of samples
95 *INTEGER(nr) = nroot(); // number of roots
96 *INTEGER(nn) = length(); // number of nodes
97 UNPROTECT(14);
98 return out;
99}
100
101extern "C" {
102
104 SEXP gendat (SEXP State, SEXP Obscure) {
105 genealogy_t A = State;
106 A.prune();
107 if (*LOGICAL(Obscure)) A.obscure();
108 A.trace_lineages();
109 return A.gendat();
110 }
111
112}
@ black
Definition ball.h:12
@ blue
Definition ball.h:12
name_t deme(void) const
view deme
Definition ball.h:84
Encodes a genealogy.
Definition genealogy.h:19
size_t nsample(void) const
number of samples
Definition genealogy.h:183
genealogy_t & prune(void)
prune the tree (drop all black balls)
Definition genealogy.h:310
SEXP gendat(void) const
genealogy information in list format
Definition gendat.cc:60
void gendat(double *tout, int *anc, int *lin, int *sat, int *type, int *deme, int *index, int *child) const
genealogy information in list format
Definition gendat.cc:9
slate_t & timezero(void)
view/set zero time.
Definition genealogy.h:153
slate_t & time(void)
view/set current time.
Definition genealogy.h:145
size_t nroot(void) const
number of roots
Definition genealogy.h:192
genealogy_t & obscure(void)
erase all deme information
Definition genealogy.h:321
Encodes a genealogical node.
Definition node.h:21
node_t * parent(void) const
Definition node.h:115
name_t lineage(void) const
view lineage
Definition node.h:104
bool is_root(void) const
Definition node.h:121
name_t deme(void) const
view deme
Definition node.h:96
name_t uniq
Definition node.h:32
slate_t slate
Definition node.h:33
int nchildren(void) const
number of descendants
Definition node.h:131
size_t length(void) const
Number of nodes in the sequence.
Definition nodeseq.h:142
void trace_lineages(void)
Definition nodeseq.h:253
ball_t * ball(const color_t c) const
retrieve the first ball of the specified color.
Definition pocket.h:130
bool holds(ball_t *b) const
does this node hold the given ball?
Definition pocket.h:109
SEXP gendat(SEXP State, SEXP Obscure)
data-frame format
Definition gendat.cc:104
static int set_list_elem(SEXP list, SEXP names, SEXP element, const char *name, int pos)
Definition internal.h:76
static const int deme
Definition lbdp.cc:7
#define n
Definition lbdp_pomp.c:9
std::list< node_t * >::const_iterator node_it
Definition nodeseq.h:12