phylopomp
Phylodynamics for POMPs
Loading...
Searching...
No Matches
nodeseq.h
Go to the documentation of this file.
1// -*- C++ -*-
2// NODE SEQUENCE CLASS
3
4#ifndef _NODESEQ_H_
5#define _NODESEQ_H_
6
7#include <list>
8#include <unordered_map>
9#include "node.h"
10#include "internal.h"
11
12typedef typename std::list<node_t*>::const_iterator node_it;
13typedef typename std::list<node_t*>::iterator node_nit;
14typedef typename std::list<node_t*>::const_reverse_iterator node_rev_it;
15
17class nodeseq_t : public std::list<node_t*> {
18
19private:
20
22 void clean (void) {
23 for (node_t *p : *this) delete p;
24 clear();
25 };
26
27public:
28
30 ~nodeseq_t (void) {
31 clean();
32 };
33
34public:
35
36 // SERIALIZATION
38 size_t bytesize (void) const {
39 size_t s = sizeof(size_t);
40 for (node_t *p : *this)
41 s += p->bytesize();
42 return s;
43 };
44
45 friend raw_t* operator>> (const nodeseq_t& G, raw_t* o) {
46 size_t nnode = G.size();
47 memcpy(o,&nnode,sizeof(size_t)); o += sizeof(size_t);
48 for (node_t *p : G) {
49 o = (*p >> o);
50 }
51 return o;
52 };
53
54 friend raw_t* operator>> (raw_t* o, nodeseq_t& G) {
55 G.clean();
56 std::unordered_map<name_t,node_t*> node_names;
57 std::unordered_map<name_t,ball_t*> ball_names;
58 size_t nnode = 0;
59 memcpy(&nnode,o,sizeof(size_t)); o += sizeof(size_t);
60 node_names.reserve(nnode);
61 ball_names.reserve(nnode);
62 for (size_t i = 0; i < nnode; i++) {
63 node_t *p = new node_t();
64 o = (o >> *p);
65 G.push_back(p);
66 node_names.insert({p->uniq,p});
67 }
68 for (node_t *q : G) {
69 q->repair_owners(node_names,&ball_names);
70 }
71 G.repair_owners(ball_names);
73 return o;
74 };
75
76private:
77
80 void repair_owners (std::unordered_map<name_t,ball_t*>& names) {
81 std::unordered_map<name_t,ball_t*>::const_iterator n;
82 for (node_t *p : *this) {
83 n = names.find(p->uniq);
84 assert(n != names.end());
85 ball_t *b = n->second;
86 p->green_ball() = b;
87 }
88 };
89
90public:
91
95 static bool compare (node_t* p, node_t* q) {
96 return (p->slate < q->slate) ||
97 ((p->slate == q->slate) &&
98 ((p==q->green_ball()->holder()) ||
99 ((q!=p->green_ball()->holder()) && (p->uniq < q->uniq))));
100 };
101
103 void sort (void) {
104 std::list<node_t*>::sort(compare);
105 };
106
107private:
108
110 slate_t dawn (void) const {
111 return (empty()) ? R_NaReal : front()->slate;
112 };
113
114 slate_t dusk (void) const {
115 return (empty()) ? R_NaReal : back()->slate;
116 }
117
118public:
119
121 pocket_t* colored (color_t col) const {
122 pocket_t *p = new pocket_t;
123 for (node_t *q : *this) {
124 for (ball_t *b : *q ) {
125 if (b->is(col)) p->insert(b);
126 }
127 }
128 return p;
129 };
130
131 size_t ntime (slate_t t) const {
132 size_t count = 1;
133 for (node_t *p : *this) {
134 if (t < p->slate) {
135 t = p->slate;
136 count++;
137 }
138 }
139 return count;
140 };
141
142 size_t length (void) const {
143 return this->size();
144 };
145
147 int i = 0;
148 node_it k = cbegin();
149 while (i < n && k != cend()) {
150 i++; k++;
151 }
152 assert(k != cend());
153 return *k;
154 };
155
156public:
157
159 void move (ball_t *b, node_t *p, node_t *q) {
160 assert(b->holder() == p);
161 p->erase(b); q->insert(b);
162 };
163
164 void swap (ball_t *a, ball_t *b) {
165 node_t *p = a->holder();
166 node_t *q = b->holder();
167 if (p != q) {
168 p->erase(a); q->insert(a);
169 q->erase(b); p->insert(b);
170 }
171 };
172
174 void attach (node_t *p, node_t *q) {
175 move(q->green_ball(),q,p);
176 };
177
179 void detach (node_t *p) {
180 move(p->green_ball(),p->parent(),p);
181 };
182
184 void add (node_t *p, ball_t *a) {
185 swap(a,p->green_ball());
186 p->deme() = a->deme();
187 push_back(p);
188 };
189
192 void drop (ball_t *a) {
193 assert(a->is(black));
194 node_t *p = a->holder();
195 if (p->size() > 1) {
196 p->erase(a);
197 delete a;
198 if (p->dead_root()) { // remove isolated root
199 destroy_node(p);
200 }
201 } else {
202 swap(a,p->green_ball());
203 destroy_node(p);
204 drop(a); // recurse
205 }
206 };
207
209 assert(p->dead_root());
210 remove(p);
211 delete p;
212 };
213
214 void weed (void) {
215 node_nit j = begin();
216 while (j != end()) {
217 if ((*j)->dead_root()) {
218 destroy_node(*(j++));
219 } else {
220 j++;
221 }
222 }
223 };
224
226 void comb (void) {
227 for (node_t *p : *this) {
228 if (p->size() == 1 && p->holds(green)) {
229 swap(p->last_ball(),p->green_ball());
230 }
231 }
232 weed();
233 };
234
235private:
236
241 node_t *p = b->holder();
242 while (p->lineage() == null_lineage) {
243 p->lineage() = u;
244 p = p->parent();
245 }
246 };
247
248public:
249
253 void trace_lineages (void) {
254 // we trace each lineage in turn.
255 // because we move from early to late,
256 // the order is guaranteed to be valid.
257 name_t u = 0;
258 for (node_t *p : *this ) {
259 for (ball_t *b : *p) {
260 if (b->color==blue) {
261 trace_lineage(b,u);
262 u++;
263 }
264 }
265 }
266 };
267
268public:
269
271 string_t yaml (string_t tab = "") const;
273 SEXP structure (void) const;
275 string_t newick (slate_t t, bool showdeme, bool extended) const;
276
277};
278
279#endif
color_t
BALL COLORS.
Definition ball.h:12
@ 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
node_t * holder(void) const
in whose pocket do I lie?
Definition ball.h:107
bool is(color_t c) const
is a given ball of the given color?
Definition ball.h:115
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
size_t bytesize(void) const
size of binary serialization
Definition node.h:38
bool dead_root(void) const
Definition node.h:124
name_t deme(void) const
view deme
Definition node.h:96
name_t uniq
Definition node.h:32
ball_t * green_ball(void) const
pointer to my green ball
Definition node.h:88
void insert(ball_t *a)
insert a ball into the pocket of a node
Definition node.h:156
slate_t slate
Definition node.h:33
A sequence of nodes.
Definition nodeseq.h:17
void weed(void)
drop all dead roots
Definition nodeseq.h:214
slate_t dawn(void) const
Earliest time in the sequence.
Definition nodeseq.h:110
void detach(node_t *p)
Definition nodeseq.h:179
void clean(void)
clean up: delete all nodes, reset globals
Definition nodeseq.h:22
void trace_lineage(ball_t *b, name_t u)
Definition nodeseq.h:240
size_t ntime(slate_t t) const
Number of distinct timepoints.
Definition nodeseq.h:131
void comb(void)
Definition nodeseq.h:226
void sort(void)
order nodes in order of increasing time
Definition nodeseq.h:103
friend raw_t * operator>>(const nodeseq_t &G, raw_t *o)
binary serialization
Definition nodeseq.h:45
void destroy_node(node_t *p)
remove a dead root node
Definition nodeseq.h:208
void attach(node_t *p, node_t *q)
Definition nodeseq.h:174
SEXP structure(void) const
R list description.
Definition structure.cc:71
size_t bytesize(void) const
size of serialized binary form
Definition nodeseq.h:38
void drop(ball_t *a)
Definition nodeseq.h:192
string_t yaml(string_t tab="") const
human/machine-readable info
Definition yaml.cc:52
void repair_owners(std::unordered_map< name_t, ball_t * > &names)
Definition nodeseq.h:80
static bool compare(node_t *p, node_t *q)
Definition nodeseq.h:95
void swap(ball_t *a, ball_t *b)
swap balls a and b, wherever they lie
Definition nodeseq.h:164
size_t length(void) const
Number of nodes in the sequence.
Definition nodeseq.h:142
void move(ball_t *b, node_t *p, node_t *q)
move ball b from p to q
Definition nodeseq.h:159
pocket_t * colored(color_t col) const
Get all balls of a color.
Definition nodeseq.h:121
void trace_lineages(void)
Definition nodeseq.h:253
slate_t dusk(void) const
Latest time in the sequence.
Definition nodeseq.h:114
node_t * position(int n)
traverse to nth node, retrieve pointer
Definition nodeseq.h:146
~nodeseq_t(void)
destructor
Definition nodeseq.h:30
void add(node_t *p, ball_t *a)
Definition nodeseq.h:184
string_t newick(slate_t t, bool showdeme, bool extended) const
put genealogy at time t into Newick format.
Definition newick.cc:79
A pocket is a set of balls.
Definition pocket.h:30
ball_t * last_ball(void) const
retrieve the last ball
Definition pocket.h:126
bool holds(ball_t *b) const
does this node hold the given ball?
Definition pocket.h:109
Rbyte raw_t
Definition internal.h:52
size_t name_t
Definition internal.h:54
double slate_t
Definition internal.h:53
#define n
Definition lbdp_pomp.c:9
static const name_t null_lineage
Definition node.h:11
std::list< node_t * >::const_reverse_iterator node_rev_it
Definition nodeseq.h:14
std::list< node_t * >::const_iterator node_it
Definition nodeseq.h:12
std::list< node_t * >::iterator node_nit
Definition nodeseq.h:13