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 name_t scan_name (const string_t &s)
 
static color_t scan_color (const std::string &s)
 simple function for scanning the color
 
static slate_t scan_branch_length (string_t::const_iterator b, string_t::const_iterator e)
 Scan the branch length.
 
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 259 of file parse.cc.

259 {
260 PROTECT(X = AS_CHARACTER(X));
261 PROTECT(T0 = AS_NUMERIC(T0));
262 PROTECT(Tf = AS_NUMERIC(Tf));
263 double t0 = *REAL(T0);
264 double tf = *REAL(Tf);
265 // parse the Newick representation into a genealogy:
266 string_t x = CHAR(STRING_ELT(X,0));
267 genealogy_t G(t0);
268 G.parse(x);
269 if (!ISNA(tf)) {
270 G.curtail(tf,t0);
271 }
272 G.trace_lineages();
273 UNPROTECT(3);
274 return serial(G);
275 }
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_branch_length()

static slate_t scan_branch_length ( string_t::const_iterator b,
string_t::const_iterator e )
static

Scan the branch length.

Definition at line 90 of file parse.cc.

93{
94 double bl = 0.0;
95 if (b != e) {
96 std::smatch m;
97 if (std::regex_match(b,e,m,std::regex("^(?:\\[.*?\\])?([-+]?[0-9]*\\.?[0-9]+(?:[eE][-+]?[0-9]+)?)(?:\\[.*?\\])?$"))) {
98 try {
99 bl = stod(m[1].str());
100 }
101 catch (const std::invalid_argument& e) {
102 err("in '%s': invalid Newick format: branch length should be a non-negative decimal number.",__func__);
103 }
104 catch (const std::out_of_range& e) {
105 err("in '%s': invalid Newick format: branch length out of range.",__func__);
106 }
107 catch (const std::exception& e) {
108 err("in '%s': parsing branch-length: %s.",__func__,e.what());
109 }
110 catch (...) {
111 err("in '%s': other branch-length parsing error.",__func__);
112 }
113 if (bl < 0.0) err("in '%s': negative branch length detected.",__func__);
114 } else {
115 warn("in '%s': in branch-length spec '%s': no branch-length detected: assuming zero branch length.",__func__,string_t(b,e).c_str());
116 }
117 }
118 return bl;
119}
#define warn(...)
Definition internal.h:19
#define err(...)
Definition internal.h:18
Here is the caller 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 67 of file parse.cc.

69{
70 std::string copy(s);
71 const std::unordered_map<string_t,color_t> options({
72 {"sample",blue},{"extant",black},{"migration",green},
73 {"node",green},{"branch",green},{"root",green}
74 });
75 color_t col = green;
76 std::transform(copy.begin(),copy.end(),copy.begin(),
77 [](unsigned char c){return std::tolower(c);});
78 try {
79 col = options.at(copy);
80 }
81 catch (const std::out_of_range& e) {
82 err("in %s: invalid metadata: type '%s' not recognized.",__func__,s.c_str());
83 }
84 return col;
85}
color_t
BALL COLORS.
Definition ball.h:12
@ green
Definition ball.h:12
@ black
Definition ball.h:12
@ blue
Definition ball.h:12
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 41 of file parse.cc.

43{
44 int d;
45 try {
46 d = stoi(s);
47 if (d < 0) err("in '%s': negative deme number detected.",__func__);
48 }
49 catch (const std::invalid_argument& e) {
50 err("in '%s': invalid Newick format: deme should be indicated with an integer.",__func__);
51 }
52 catch (const std::out_of_range& e) {
53 err("in '%s': invalid Newick format: deme out of range.",__func__);
54 }
55 catch (const std::exception& e) {
56 err("in '%s': parsing deme label: %s.",__func__,e.what());
57 }
58 catch (...) {
59 err("in '%s': other deme-parsing error.",__func__);
60 }
61 return name_t(d);
62}
size_t name_t
Definition internal.h:54
Here is the caller graph for this function: