phylopomp
Phylodynamics for POMPs
Loading...
Searching...
No Matches
parse.cc File Reference
#include "genealogy.h"
#include "generics.h"
#include "internal.h"
#include <regex>
#include <unordered_map>
Include dependency graph for parse.cc:

Go to the source code of this file.

Functions

static slate_t scan_slate (const string_t &s)
 
static name_t scan_name (const string_t &s)
 
static color_t scan_color (const std::string &s)
 simple function for scanning the color
 
SEXP parse_newick (SEXP X, SEXP T0, SEXP Tf)
 

Function Documentation

◆ parse_newick()

SEXP parse_newick ( SEXP X,
SEXP T0,
SEXP Tf )

A parser for Newick code. Returns a genealogy in the phylopomp format.

Definition at line 236 of file parse.cc.

236 {
237 PROTECT(X = AS_CHARACTER(X));
238 PROTECT(T0 = AS_NUMERIC(T0));
239 PROTECT(Tf = AS_NUMERIC(Tf));
240 double t0 = *REAL(T0);
241 double tf = *REAL(Tf);
242 // parse the Newick representation into a genealogy:
243 string_t x = CHAR(STRING_ELT(X,0));
244 genealogy_t G(t0);
245 G.parse(x);
246 if (!ISNA(tf)) {
247 G.curtail(tf,t0);
248 }
249 G.trace_lineages();
250 UNPROTECT(3);
251 return serial(G);
252 }
Encodes a genealogy.
Definition genealogy.h:19
SEXP serial(const TYPE &X)
binary serialization
Definition generics.h:33
Here is the call graph for this function:

◆ scan_color()

static color_t scan_color ( const std::string & s)
static

simple function for scanning the color

Definition at line 77 of file parse.cc.

79{
80 std::string copy(s);
81 const std::unordered_map<string_t,color_t> options({
82 {"sample",blue},{"extant",black},{"migration",green},
83 {"node",green},{"branch",green},{"root",green}
84 });
85 color_t col = green;
86 std::transform(copy.begin(),copy.end(),copy.begin(),
87 [](unsigned char c){return std::tolower(c);});
88 try {
89 col = options.at(copy);
90 }
91 catch (const std::out_of_range& e) {
92 err("in %s: invalid metadata: type '%s' not recognized.",__func__,s.c_str());
93 }
94 return col;
95}
color_t
BALL COLORS.
Definition ball.h:12
@ green
Definition ball.h:12
@ black
Definition ball.h:12
@ blue
Definition ball.h:12
#define err(...)
Definition internal.h:18
Here is the caller graph for this function:

◆ scan_name()

static name_t scan_name ( const string_t & s)
static

simple function for scanning a name_t from a string (with error trapping)

Definition at line 51 of file parse.cc.

53{
54 int d;
55 try {
56 d = stoi(s);
57 if (d < 0) err("in '%s': negative deme number detected.",__func__);
58 }
59 catch (const std::invalid_argument& e) {
60 err("in '%s': invalid Newick format: deme should be indicated with an integer.",__func__);
61 }
62 catch (const std::out_of_range& e) {
63 err("in '%s': invalid Newick format: deme out of range.",__func__);
64 }
65 catch (const std::exception& e) {
66 err("in '%s': parsing deme label: %s.",__func__,e.what());
67 }
68 catch (...) {
69 err("in '%s': other deme-parsing error.",__func__);
70 }
71 return name_t(d);
72}
size_t name_t
Definition internal.h:54
Here is the caller graph for this function:

◆ scan_slate()

static slate_t scan_slate ( const string_t & s)
static

simple function for scanning a slate_t from a string (with error trapping)

Definition at line 24 of file parse.cc.

26{
27 double bl;
28 try {
29 bl = (s.empty()) ? 0.0 : stod(s);
30 }
31 catch (const std::invalid_argument& e) {
32 err("in '%s': invalid Newick format: branch length should be a non-negative decimal number.",__func__);
33 }
34 catch (const std::out_of_range& e) {
35 err("in '%s': invalid Newick format: branch length out of range.",__func__);
36 }
37 catch (const std::exception& e) {
38 err("in '%s': parsing branch-length: %s.",__func__,e.what());
39 }
40 catch (...) {
41 err("in '%s': other branch-length parsing error.",__func__);
42 }
43 if (bl < 0.0) err("in '%s': negative branch length detected.",__func__);
44 return slate_t(bl);
45}
double slate_t
Definition internal.h:53
Here is the caller graph for this function: