19 #define tree_assert(cond) arb_assert(cond)
22 char *unwanted = strpbrk(str, toReplace);
25 unwanted = strpbrk(unwanted+1, toReplace);
29 inline bool isQuoteChar(
char c) {
return c ==
'"' || c ==
'\''; }
34 return leafNode->
name;
37 return innerNode->
name;
41 for (
int i = 0; label[i]; ++i) {
65 const char *disallowed_chars =
" \t\'\"()[]:;,";
66 const char *problem_chars = disallowed_chars+4;
69 bool need_quotes = strpbrk(label, disallowed_chars);
73 if (force_quotes || need_quotes) {
80 size_t label_length = strlen(label);
85 memmove(fixed_label, fixed_label+1, label_length-2);
86 fixed_label[label_length-2] = 0;
96 char used_quote_as_string[] = { used_quote, 0 };
100 need_quotes = strpbrk(fixed_label, disallowed_chars);
101 if (!need_quotes) used_quote = 0;
109 if (used_quote)
fputc(used_quote, out);
110 fputs(fixed_label, out);
111 if (used_quote)
fputc(used_quote, out);
124 for (
int i = 0; i < indent; i++) {
137 const TreeLabeler& labeler,
bool save_branchlengths,
149 if (pretty)
fputs(
"(\n", out);
152 error =
export_tree_node_print(gb_main, out, tree->get_leftson(), tree_name, pretty, indent+1, labeler, save_branchlengths, bootstrap_style, save_groupnames, qmode);
157 error =
export_tree_node_print(gb_main, out, tree->get_rightson(), tree_name, pretty, indent+1, labeler, save_branchlengths, bootstrap_style, save_groupnames, qmode);
200 const char *
group = labeler.groupLabel(gb_main, tree->
gb_node, tree, tree_name);
203 "(to export a tree, you have to correct all keeled groups)", group);
207 size_t glen = strlen(group);
209 char *group_unquoted =
ARB_strndup(group+1, glen-2);
212 free(group_unquoted);
254 const TreeLabeler& labeler,
bool skip_folded,
const string& parent_id,
int& parent_son_counter) {
258 XML_Tag item_tag(
"ITEM");
261 item_tag.add_attribute(
"itemname", labeler.
speciesLabel(gb_main, tree->
gb_node, tree, tree_name));
265 char *bootstrap =
NULp;
276 char *groupname =
NULp;
280 groupname = strdup(buf);
288 if (my_length || bootstrap || groupname) {
289 bool hide_this_group = skip_folded && folded;
291 XML_Tag branch_tag(hide_this_group ?
"FOLDED_GROUP" :
"BRANCH");
294 branch_tag.add_attribute(
"name", my_id);
300 branch_tag.add_attribute(
"bootstrap", bootstrap);
304 branch_tag.add_attribute(
"groupname", groupname);
306 if (folded) branch_tag.add_attribute(
"folded",
"1");
312 if (hide_this_group) {
316 int my_son_counter = 0;
332 FILE *
output = fopen(path,
"w");
334 if (!output) error =
GB_export_errorf(
"file '%s' could not be opened for writing", path);
347 XML_Document xml_doc(
"ARB_TREE",
"arb_tree.dtd", output);
349 xml_doc.add_attribute(
"database", db_name);
350 xml_doc.add_attribute(
"treename", tree_name);
355 XML_Tag remark_tag(
"COMMENT");
360 int my_son_counter = 0;
373 int openBrackets = 0;
376 for (
int o = 0; comment[o]; ++o) {
377 switch (comment[o]) {
382 if (openBrackets == 0) {
396 while (openBrackets>0) {
411 FILE *
output = fopen(path,
"w");
413 if (!output) error =
GBS_global_string(
"file '%s' could not be opened for writing", path);
448 #define QUOTING_REQUIRED_WHEN_SAVING "It is required to allow quoting of tree labels when saving "
455 #undef QUOTING_REQUIRED_WHEN_SAVING
460 error =
export_tree_node_print(gb_main, output, tree, tree_name, pretty, 0, labeler, save_branchlengths, bootstrap_style, save_groupnames, quoteMode);
466 fprintf(output,
";\n");
481 while (
char c = str[i]) {
482 if (c ==
'\'' || c ==
'\"') str[i] =
'.';
491 dquot ?
" \"%s\" " :
" '%s' ",
496 export_tree_rek(tree->get_leftson(), out, export_branchlens, dquot);
if (export_branchlens) fprintf(out,
":%.5f,", tree->
leftlen);
497 export_tree_rek(tree->get_rightson(), out, export_branchlens, dquot);
if (export_branchlens) fprintf(out,
":%.5f", tree->
rightlen);
503 dquot ?
"\"%s\"" :
"'%s'",
521 one = tree->get_leftson();
522 two = tree->get_rightson()->get_leftson();
523 three = tree->get_rightson()->get_rightson();
526 one = tree->get_leftson()->get_leftson();
527 two = tree->get_leftson()->get_rightson();
528 three = tree->get_rightson();
531 export_tree_rek(one, out, export_branchlens, dquot);
if (export_branchlens) fprintf(out,
":%.5f", 1.0);
fputc(
',', out);
532 export_tree_rek(two, out, export_branchlens, dquot);
if (export_branchlens) fprintf(out,
":%.5f", 1.0);
fputc(
',', out);
533 export_tree_rek(three, out, export_branchlens, dquot);
if (export_branchlens) fprintf(out,
":%.5f", 1.0);
GB_ERROR TREE_export_tree(GBDATA *, FILE *out, TreeNode *tree, bool triple_root, bool export_branchlens, bool dquot)
size_t GBT_count_leafs(const TreeNode *tree)
void GB_warning(const char *message)
void indentTo(int indent, FILE *out)
GB_ERROR TREE_write_XML(GBDATA *gb_main, const char *db_name, const char *tree_name, const TreeLabeler &labeler, bool skip_folded, const char *path)
GB_ERROR GBT_link_tree(TreeNode *tree, GBDATA *gb_main, bool show_status)
virtual const char * speciesLabel(GBDATA *gb_main, GBDATA *gb_species, TreeNode *species, const char *tree_name) const =0
CONSTEXPR_INLINE unsigned char safeCharIndex(char c)
bool has_group_info() const
void GB_unlink_or_warn(const char *path, GB_ERROR *error)
static GB_ERROR export_tree_node_print(GBDATA *gb_main, FILE *out, TreeNode *tree, const char *tree_name, bool pretty, int indent, const TreeLabeler &labeler, bool save_branchlengths, BootstrapSaveStyle bootstrap_style, bool save_groupnames, LabelQuoting qmode)
static const char * export_tree_node_print_xml(GBDATA *gb_main, TreeNode *tree, double my_length, const char *tree_name, const TreeLabeler &labeler, bool skip_folded, const string &parent_id, int &parent_son_counter)
const char * ARB_date_string()
TreeNode * GBT_read_tree(GBDATA *gb_main, const char *tree_name, TreeRoot *troot)
const char * GBS_global_string(const char *templat,...)
GB_ERROR GB_export_error(const char *error)
GB_ERROR GB_await_error()
char * ARB_strduplen(const char *p, unsigned len)
string buildNodeIdentifier(const string &parent_id, int &son_counter)
const char * speciesLabel(GBDATA *, GBDATA *, TreeNode *species, const char *) const OVERRIDE
static void export_tree_node_print_remove(char *str)
GBT_RemarkType parse_bootstrap(double &bootstrap, WarningConsumer report)
GB_ERROR TREE_write_Newick(GBDATA *gb_main, const char *tree_name, const TreeLabeler &labeler, bool save_branchlengths, BootstrapSaveStyle bootstrap_style, bool save_groupnames, bool pretty, LabelQuoting quoteMode, const char *path)
static void error(const char *msg)
void replace_by_underscore(char *str, const char *toReplace)
bool is_root_node() const
static char * complete_newick_comment(const char *comment)
char * GBS_log_action_to(const char *comment, const char *action, bool stamp)
fputs(TRACE_PREFIX, stderr)
GB_ERROR GB_export_errorf(const char *templat,...)
char * ARB_strndup(const char *start, int len)
const char * GBT_len2string(GBT_LEN length, bool lowPrecision)
const char * groupLabel(GBDATA *, GBDATA *, TreeNode *species, const char *) const OVERRIDE
bool whole_label_quoted(const char *label, size_t length)
int GB_read_byte(GBDATA *gbd)
static void export_tree_rek(TreeNode *tree, FILE *out, bool export_branchlens, bool dquot)
char * GB_read_string(GBDATA *gbd)
static GB_ERROR export_tree_label(const char *label, FILE *out, LabelQuoting qmode)
#define QUOTING_REQUIRED_WHEN_SAVING
void print_branchlength(GBT_LEN length, FILE *out)
GBDATA * GBT_find_tree(GBDATA *gb_main, const char *tree_name)
GB_transaction ta(gb_var)
void destroy(TreeNode *that)
const char * get_remark() const
char first_non_ascii_char(const char *label)
GBDATA * GB_entry(GBDATA *father, const char *key)
char * GBS_global_string_copy(const char *templat,...)
#define tree_assert(cond)