phylopomp
Phylodynamics for POMPs
Loading...
Searching...
No Matches
getinfo.cc
Go to the documentation of this file.
1// Get information about a genealogy
2
3#include "genealogy.h"
4#include "generics.h"
5#include "internal.h"
6
7static size_t matchargs (const char *prov, const char **set, size_t n) {
8 size_t i;
9 for (i = 0; i < n; i++) {
10 if (strcmp(prov,set[i]) == 0) break;
11 }
12 return i;
13}
14
15extern "C" {
16
19 SEXP getInfo (SEXP args) {
20 const char *argname[] = {
21 "object","prune","obscure","extended",
22 "t0","time","nsample","nroot","ndeme",
23 "structure","yaml","newick",
24 "lineages","gendat","genealogy"};
25 const int narg = sizeof(argname)/sizeof(const char *);
26 bool flag[narg];
27 SEXP object = R_NilValue;
28 size_t nout = 0;
29 int k;
30
31 for (k = 0; k < narg; k++) flag[k] = false;
32 args = CDR(args);
33
34 while (args != R_NilValue) {
35 const char *name = isNull(TAG(args)) ? "" : CHAR(PRINTNAME(TAG(args)));
36 SEXP arg = CAR(args);
37 size_t j = matchargs(name,argname,narg);
38 if (j == 0) {
39 object = arg;
40 flag[0] = true;
41 } else if (j < 4) {
42 flag[j] = *LOGICAL(AS_LOGICAL(arg));
43 } else if (j < narg) {
44 flag[j] = *LOGICAL(AS_LOGICAL(arg));
45 if (flag[j]) nout++;
46 } else {
47 err("unrecognized argument '%s' in '%s'.",name,__func__);
48 }
49 args = CDR(args);
50 }
51
52 if (!flag[0]) err("no genealogy furnished to '%s'",__func__);
53 genealogy_t A = object;
54
55 // prune and/or obscure if requested
56 const bool *f = flag+1;
57 if (*(f++)) A.prune();
58 if (*(f++)) A.obscure();
60 bool extended = false;
61 if (*(f++)) {
62 extended = true;
63 } else {
64 A.insert_zlb();
65 }
66
67 SEXP out, outnames;
68 PROTECT(out = NEW_LIST(nout));
69 PROTECT(outnames = NEW_CHARACTER(nout));
70 k = 0;
71 if (*(f++)) { // t0
72 k = set_list_elem(out,outnames,timezero(A),"t0",k);
73 }
74 if (*(f++)) { // time
75 k = set_list_elem(out,outnames,time(A),"time",k);
76 }
77 if (*(f++)) { // nsample
78 k = set_list_elem(out,outnames,nsample(A),"nsample",k);
79 }
80 if (*(f++)) { // nroot
81 k = set_list_elem(out,outnames,nroot(A),"nroot",k);
82 }
83 if (*(f++)) { // ndeme
84 k = set_list_elem(out,outnames,ndeme(A),"ndeme",k);
85 }
86 if (*(f++)) { // structure
87 k = set_list_elem(out,outnames,structure(A),"structure",k);
88 }
89 if (*(f++)) { // yaml
90 k = set_list_elem(out,outnames,yaml(A),"yaml",k);
91 }
92 if (*(f++)) { // newick
93 k = set_list_elem(out,outnames,newick(A,extended),"newick",k);
94 }
95 if (*(f++)) { // lineages
96 k = set_list_elem(out,outnames,lineage_count(A),"lineages",k);
97 }
98 if (*(f++)) { // gendat
99 k = set_list_elem(out,outnames,gendat(A),"gendat",k);
100 }
101 if (*(f++)) { // genealogy
102 SEXP S;
103 PROTECT(S = serial(A));
104 SET_ATTR(S,install("class"),mkString("gpgen"));
105 k = set_list_elem(out,outnames,S,"genealogy",k);
106 UNPROTECT(1);
107 }
108 SET_NAMES(out,outnames);
109 UNPROTECT(2);
110 return out;
111 }
112}
Encodes a genealogy.
Definition genealogy.h:19
genealogy_t & prune(void)
prune the tree (drop all black balls)
Definition genealogy.h:310
void insert_zlb(void)
insert zero-length branches for samples where needed
Definition genealogy.h:358
genealogy_t & obscure(void)
erase all deme information
Definition genealogy.h:321
void trace_lineages(void)
Definition nodeseq.h:253
SEXP gendat(SEXP State, SEXP Obscure)
data-frame format
Definition gendat.cc:104
SEXP ndeme(TYPE &X)
Definition generics.h:7
SEXP nroot(TYPE &X)
Definition generics.h:17
SEXP timezero(TYPE &X)
Definition generics.h:22
SEXP time(TYPE &X)
Definition generics.h:27
SEXP structure(const TYPE &X)
structure in R list format
Definition generics.h:49
SEXP serial(const TYPE &X)
binary serialization
Definition generics.h:33
SEXP yaml(const TYPE &X)
human/machine readable output
Definition generics.h:43
SEXP lineage_count(const TYPE &G)
number of lineages through time
Definition generics.h:61
SEXP nsample(TYPE &X)
Definition generics.h:12
SEXP newick(const TYPE &X, bool extended)
tree in newick format
Definition generics.h:55
static size_t matchargs(const char *prov, const char **set, size_t n)
Definition getinfo.cc:7
SEXP getInfo(SEXP args)
Definition getinfo.cc:19
static int set_list_elem(SEXP list, SEXP names, SEXP element, const char *name, int pos)
Definition internal.h:76
#define err(...)
Definition internal.h:18
#define n
Definition lbdp_pomp.c:9
#define S
Definition seirs_pomp.c:37