81 memcpy(o,A,
sizeof(A)); o +=
sizeof(A);
82 memcpy(o,B,
sizeof(B)); o +=
sizeof(B);
83 return reinterpret_cast<const nodeseq_t&
>(G) >> o;
90 memcpy(A,o,
sizeof(A)); o +=
sizeof(A);
91 memcpy(B,o,
sizeof(B)); o +=
sizeof(B);
93 err(
"in %s (%s line %d) corrupted genealogy serialization.",
94 __func__,__FILE__,__LINE__);
97 return o >>
reinterpret_cast<nodeseq_t&
>(G);
118 err(
"in %s (%s line %d): cannot deserialize a NULL.",
119 __func__,__FILE__,__LINE__);
120 PROTECT(o = AS_RAW(o));
172 int *
ell,
int *sat,
int *etype)
const {
174 for (
size_t j = 0; j <
_ndeme; j++) {
180 for (
node_it i = begin(); i != end(); i++) {
182 if (tcur < p->slate) {
186 for (
size_t j = 0; j <
_ndeme; j++) {
199 for (
size_t j = 0; j <
_ndeme; j++) {
208 SEXP tout,
deme,
ell, sat, etype, out, outn;
211 PROTECT(tout = NEW_NUMERIC(nl));
212 PROTECT(
deme = NEW_INTEGER(nl));
213 PROTECT(
ell = NEW_INTEGER(nl));
214 PROTECT(sat = NEW_INTEGER(nl));
215 PROTECT(etype = NEW_INTEGER(nl));
216 PROTECT(out = NEW_LIST(5));
217 PROTECT(outn = NEW_CHARACTER(5));
225 INTEGER(sat),INTEGER(etype));
231 void gendat (
double *tout,
int *anc,
int *lin,
233 int *index,
int *child)
const {
236 for (k = 0,
n = 0, i = begin(); i != end(); i++,
n++) {
251 child[
n] = NA_INTEGER;
255 for (m = 0, j = begin(); j != i; j++, m++) {
265 for (k = 0,
n = 0, i = begin(); i != end(); i++,
n++) {
268 for (m =
n+1; j != end(); m++, j++) {
278 SEXP tout, anc, lin, sat, type, index, child, ns, nn;
281 PROTECT(tout = NEW_NUMERIC(
n+1));
282 PROTECT(type = NEW_INTEGER(
n));
283 PROTECT(lin = NEW_INTEGER(
n));
284 PROTECT(sat = NEW_INTEGER(
n));
285 PROTECT(index = NEW_INTEGER(
n));
286 PROTECT(child = NEW_INTEGER(
n));
287 PROTECT(anc = NEW_INTEGER(
n));
288 PROTECT(ns = NEW_INTEGER(1));
289 PROTECT(nn = NEW_INTEGER(1));
290 PROTECT(out = NEW_LIST(9));
291 PROTECT(outn = NEW_CHARACTER(9));
302 gendat(REAL(tout),INTEGER(anc),INTEGER(lin),INTEGER(sat),
303 INTEGER(type),INTEGER(index),INTEGER(child));
313 for (
node_it k = cbegin(); k != cend(); k++) {
314 if ((*k)->holds(
blue))
n++;
323 SEXP O, On, T0, Time, Nodes;
324 PROTECT(O = NEW_LIST(3));
325 PROTECT(On = NEW_CHARACTER(3));
326 PROTECT(Time = NEW_NUMERIC(1));
327 *REAL(Time) = double(
time());
328 PROTECT(T0 = NEW_NUMERIC(1));
343 std::string o =
"t0 = " + std::to_string(
double(
timezero()))
344 +
"\ntime = " + std::to_string(
double(
time())) +
"\n"
352 virtual std::string
yaml (std::string tab =
"")
const {
354 std::string t = tab +
" ";
355 o = tab +
"t0: " + std::to_string(
timezero()) +
"\n"
356 + tab +
"time: " + std::to_string(
time()) +
"\n"
376 if (size() > maxq+grace) {
377 err(
"maximum genealogy size exceeded!");
378 }
else if (size() > maxq) {
461 std::pair<node_it, node_it>
extant (
void)
const {
462 return std::pair<node_it,node_it>(cbegin(),cend());
467 while (!blacks->empty()) {
468 ball_t *b = *(blacks->begin());
479 while (!blacks->empty()) {
480 ball_t *a = *(blacks->begin());
486 for (
node_it i = begin(); i != end(); i++) {
499 while (!empty() && p->
slate > tnew) {
501 while (p->size() > 1) {
505 p->erase(b);
delete b;
523 if (!empty()) p = back();
554 err(
"in '%s' (%s line %d): invalid Newick format: empty label.",__func__,__FILE__,__LINE__);
567 err(
"in '%s' (%s line %d): invalid Newick label: expected one of 'b','g','m', or 'o', got '%c'.",
568 __func__,__FILE__,__LINE__,s[0]);
571 while (i <
n && s[i] ==
'_') i++;
573 err(
"in '%s': invalid Newick format: premature termination.",__func__);
574 if (s[i] ==
'(' || s[i] ==
')' || s[i] ==
',' || s[i] ==
';')
575 err(
"in '%s' (%s line %d): invalid Newick format.",__func__,__FILE__,__LINE__);
583 catch (
const std::invalid_argument& e) {
584 err(
"in '%s' (%s line %d): invalid Newick format: deme should be indicated with an integer.",
585 __func__,__FILE__,__LINE__);
587 catch (
const std::out_of_range& e) {
588 err(
"in '%s': invalid Newick format: deme out of range.",__func__);
590 catch (
const std::exception& e) {
591 err(
"in '%s': parsing deme label: %s.",__func__,e.what());
594 err(
"in '%s': other deme-parsing error.",__func__);
598 while (i <
n && s[i] !=
':' &&
599 s[i] !=
'(' && s[i] !=
')' && s[i] !=
',' && s[i] !=
';') i++;
600 if (i ==
n || s[i] !=
':')
601 err(
"in '%s': invalid Newick format: missing or invalid branch length.",__func__);
606 catch (
const std::invalid_argument& e) {
607 err(
"in '%s': invalid Newick format: branch length should be a non-negative decimal number.",__func__);
609 catch (
const std::out_of_range& e) {
610 err(
"in '%s': invalid Newick format: branch length out of range.",__func__);
612 catch (
const std::exception& e) {
613 err(
"in '%s': parsing branch-length: %s.",__func__,e.what());
616 err(
"in '%s': other branch-length parsing error.",__func__);
632 if (col !=
black)
err(
"in '%s': bad Newick string (1)",__func__);
633 if (p == 0)
err(
"in '%s': bad Newick string (2)",__func__);
666 size_t i = 1, j = 1, k;
668 while (j < n && stack > 0) {
682 err(
"in '%s': premature end of Newick string.",__func__);
707 q->erase(g); p->insert(g); g->
holder() = p;
717 q->erase(g); p->insert(g); g->
holder() = p;
724 case ',':
case ';':
case ' ':
case '\n':
case '\t':
728 err(
"in '%s': invalid Newick string: unbalanced parentheses.",__func__);
731 err(
"in '%s': invalid Newick string.",__func__);
Balls function as pointers.
name_t deme(void) const
view deme
node_t * holder(void) const
in whose pocket do I lie?
ball_t * birth(ball_t *a, slate_t t, name_t d)
birth into deme d
slate_t time(void) const
view current time.
size_t nsample(void) const
number of samples
~genealogy_t(void)
destructor
ball_t * birth(node_t *p, name_t d)
birth of second or subsequent sibling into deme d
SEXP structure(void) const
R list description.
genealogy_t & operator+=(genealogy_t &G)
std::string newick(void) const
put genealogy at current time into Newick format.
genealogy_t & operator=(const genealogy_t &G)
copy assignment operator
virtual std::string yaml(std::string tab="") const
machine-readable info
genealogy_t(double t0=0, name_t u=0, size_t nd=1)
void valid(void) const
check the validity of the genealogy.
genealogy_t(SEXP o)
constructor from RAW SEXP (containing binary serialization)
void death(ball_t *a, slate_t t)
death
size_t scan_ball(const std::string &s, const slate_t t0, node_t *p)
SEXP lineage_count(void) const
lineage count and saturation
size_t bytesize(void) const
size of serialized binary form
genealogy_t & prune(void)
prune the tree (drop all black balls)
genealogy_t & parse(const std::string &s, slate_t t0, node_t *p=0)
Parse a Newick string and create the indicated genealogy.
std::string describe(void) const
human-readable info
std::pair< node_it, node_it > extant(void) const
size_t ndeme(void) const
number of demes
genealogy_t(const genealogy_t &G)
copy constructor
bool check_genealogy_size(size_t grace=0) const
check the size of the genealogy (to prevent memory exhaustion).
size_t scan_label(const std::string &s, color_t *col, name_t *deme, slate_t *time) const
slate_t timezero(void) const
get zero time.
slate_t & time(void)
view/set current time.
slate_t _time
The current time.
size_t scan_node(const std::string &s, const slate_t t0, node_t **q)
Scan the Newick string and create the indicated node.
SEXP gendat(void) const
nodelist in data-frame format
size_t _ndeme
The number of demes.
ball_t * migrate(ball_t *a, slate_t t, name_t d=0)
movement into deme d
node_t * make_node(name_t d)
void sample_death(ball_t *a, slate_t t)
insert a sample node and simultaneously terminate the lineage
genealogy_t(raw_t *o)
constructor from serialized binary form
genealogy_t & obscure(void)
erase all deme information
ball_t * graft(slate_t t, name_t d)
graft a new lineage into deme d
size_t scan_tree(const std::string &s, const slate_t t0, node_t **root)
static const name_t magic
name_t unique(void)
get the next unique name
size_t scan_color(const std::string &s, color_t *col) const
size_t & ndeme(void)
number of demes
void sample(ball_t *a, slate_t t)
insert a sample node
void lineage_count(double *tout, int *deme, int *ell, int *sat, int *etype) const
slate_t _t0
The initial time.
void gendat(double *tout, int *anc, int *lin, int *sat, int *type, int *index, int *child) const
nodelist in data-frame format
name_t _unique
The next unique name.
friend raw_t * operator>>(const genealogy_t &G, raw_t *o)
binary serialization
genealogy_t(genealogy_t &&)=default
move constructor
void curtail(slate_t tnew)
Encodes a genealogical node.
name_t lineage(void) const
view lineage
node_t * parent(void) const
ball_t * green_ball(void) const
pointer to my green ball
name_t deme(void) const
view deme
void lineage_incr(int *incr, int *sat, int *etype) const
int nchildren(void) const
number of descendants
std::string describe(void) const
human-readable info
virtual std::string yaml(std::string tab="") const
human- & machine-readable info
size_t ntime(slate_t t) const
Number of distinct timepoints.
void sort(void)
order nodes in order of increasing time
void destroy_node(node_t *p)
remove a dead root node
SEXP structure(void) const
R list description.
size_t bytesize(void) const
size of serialized binary form
void swap(ball_t *a, ball_t *b)
swap balls a and b, wherever they lie
size_t length(void) const
Number of nodes in the sequence.
pocket_t * colored(color_t col) const
Get all balls of a color.
std::string newick(slate_t t) const
put genealogy at time t into Newick format.
void add(node_t *p, ball_t *a)
A pocket is a set of balls.
ball_t * last_ball(void) const
retrieve the last ball
bool holds(ball_t *b) const
does this node hold the given ball?
static const size_t MEMORY_MAX
static int set_list_elem(SEXP list, SEXP names, SEXP element, const char *name, int pos)
std::list< node_t * >::const_iterator node_it