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
107public:
108
110 pocket_t* colored (color_t col) const {
111 pocket_t *p = new pocket_t;
112 for (node_t *q : *this) {
113 for (ball_t *b : *q ) {
114 if (b->is(col)) p->insert(b);
115 }
116 }
117 return p;
118 };
119
120 size_t ntime (slate_t t) const {
121 size_t count = 1;
122 for (node_t *p : *this) {
123 if (t < p->slate) {
124 t = p->slate;
125 count++;
126 }
127 }
128 return count;
129 };
130
131 size_t length (void) const {
132 return this->size();
133 };
134
136 int i = 0;
137 node_it k = cbegin();
138 while (i < n && k != cend()) {
139 i++; k++;
140 }
141 assert(k != cend());
142 return *k;
143 };
144
145public:
146
148 void move (ball_t *b, node_t *p, node_t *q) {
149 assert(b->holder() == p);
150 p->erase(b); q->insert(b);
151 };
152
153 void swap (ball_t *a, ball_t *b) {
154 node_t *p = a->holder();
155 node_t *q = b->holder();
156 if (p != q) {
157 p->erase(a); q->insert(a);
158 q->erase(b); p->insert(b);
159 }
160 };
161
163 void attach (node_t *p, node_t *q) {
164 move(q->green_ball(),q,p);
165 };
166
168 void detach (node_t *p) {
169 move(p->green_ball(),p->parent(),p);
170 };
171
173 void add (node_t *p, ball_t *a) {
174 swap(a,p->green_ball());
175 p->deme() = a->deme();
176 push_back(p);
177 };
178
181 void drop (ball_t *a) {
182 assert(a->is(black));
183 node_t *p = a->holder();
184 if (p->size() > 1) {
185 p->erase(a);
186 delete a;
187 if (p->dead_root()) { // remove isolated root
188 destroy_node(p);
189 }
190 } else {
191 swap(a,p->green_ball());
192 destroy_node(p);
193 drop(a); // recurse
194 }
195 };
196
198 assert(p->dead_root());
199 remove(p);
200 delete p;
201 };
202
203 void weed (void) {
204 node_nit j = begin();
205 while (j != end()) {
206 if ((*j)->dead_root()) {
207 destroy_node(*(j++));
208 } else {
209 j++;
210 }
211 }
212 };
213
215 void comb (void) {
216 for (node_t *p : *this) {
217 if (p->size() == 1 && p->holds(green)) {
218 swap(p->last_ball(),p->green_ball());
219 }
220 }
221 weed();
222 };
223
224private:
225
230 node_t *p = b->holder();
231 while (p->lineage() == null_lineage) {
232 p->lineage() = u;
233 p = p->parent();
234 }
235 };
236
237public:
238
242 void trace_lineages (void) {
243 // we trace each lineage in turn.
244 // because we move from early to late,
245 // the order is guaranteed to be valid.
246 name_t u = 0;
247 for (node_t *p : *this ) {
248 for (ball_t *b : *p) {
249 if (b->color==blue) {
250 trace_lineage(b,u);
251 u++;
252 }
253 }
254 }
255 };
256
257public:
258
260 string_t yaml (string_t tab = "") const;
262 SEXP structure (void) const;
264 string_t newick (slate_t t, slate_t te, bool showdeme, bool extended) const;
265
266};
267
268#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:203
void detach(node_t *p)
Definition nodeseq.h:168
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:229
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
size_t ntime(slate_t t) const
Number of distinct timepoints.
Definition nodeseq.h:120
void comb(void)
Definition nodeseq.h:215
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:197
void attach(node_t *p, node_t *q)
Definition nodeseq.h:163
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:181
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:153
size_t length(void) const
Number of nodes in the sequence.
Definition nodeseq.h:131
void move(ball_t *b, node_t *p, node_t *q)
move ball b from p to q
Definition nodeseq.h:148
pocket_t * colored(color_t col) const
Get all balls of a color.
Definition nodeseq.h:110
void trace_lineages(void)
Definition nodeseq.h:242
node_t * position(int n)
traverse to nth node, retrieve pointer
Definition nodeseq.h:135
~nodeseq_t(void)
destructor
Definition nodeseq.h:30
void add(node_t *p, ball_t *a)
Definition nodeseq.h:173
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