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++) {
132 const TreeLabeler& labeler,
bool save_branchlengths,
133 bool save_bootstraps,
bool save_groupnames,
LabelQuoting qmode)
144 if (pretty)
fputs(
"(\n", out);
147 error =
export_tree_node_print(gb_main, out, tree->get_leftson(), tree_name, pretty, indent+1, labeler, save_branchlengths, save_bootstraps, save_groupnames, qmode);
149 if (save_branchlengths) fprintf(out,
":%.5f", tree->
leftlen);
152 error =
export_tree_node_print(gb_main, out, tree->get_rightson(), tree_name, pretty, indent+1, labeler, save_branchlengths, save_bootstraps, save_groupnames, qmode);
155 if (save_branchlengths) fprintf(out,
":%.5f", tree->
rightlen);
161 char *bootstrap =
NULp;
162 if (save_bootstraps) {
175 const char *
group = labeler.groupLabel(gb_main, tree->
gb_node, tree, tree_name);
178 "(to export a tree, you have to correct all keeled groups)", group);
185 else if (bootstrap) label = bootstrap;
207 const TreeLabeler& labeler,
bool skip_folded,
const string& parent_id,
int& parent_son_counter) {
211 XML_Tag item_tag(
"ITEM");
214 item_tag.add_attribute(
"itemname", labeler.
speciesLabel(gb_main, tree->
gb_node, tree, tree_name));
218 char *bootstrap =
NULp;
229 char *groupname =
NULp;
233 groupname = strdup(buf);
241 if (my_length || bootstrap || groupname) {
242 bool hide_this_group = skip_folded && folded;
244 XML_Tag branch_tag(hide_this_group ?
"FOLDED_GROUP" :
"BRANCH");
247 branch_tag.add_attribute(
"name", my_id);
253 branch_tag.add_attribute(
"bootstrap", bootstrap);
257 branch_tag.add_attribute(
"groupname", groupname);
259 if (folded) branch_tag.add_attribute(
"folded",
"1");
265 if (hide_this_group) {
269 int my_son_counter = 0;
285 FILE *
output = fopen(path,
"w");
287 if (!output) error =
GB_export_errorf(
"file '%s' could not be opened for writing", path);
300 XML_Document xml_doc(
"ARB_TREE",
"arb_tree.dtd", output);
302 xml_doc.add_attribute(
"database", db_name);
303 xml_doc.add_attribute(
"treename", tree_name);
308 XML_Tag remark_tag(
"COMMENT");
313 int my_son_counter = 0;
326 int openBrackets = 0;
329 for (
int o = 0; comment[o]; ++o) {
330 switch (comment[o]) {
335 if (openBrackets == 0) {
349 while (openBrackets>0) {
364 FILE *
output = fopen(path,
"w");
366 if (!output) error =
GBS_global_string(
"file '%s' could not be opened for writing", path);
398 error =
export_tree_node_print(gb_main, output, tree, tree_name, pretty, 0, labeler, save_branchlengths, save_bootstraps, save_groupnames, quoteMode);
405 fprintf(output,
";\n");
420 while (
char c = str[i]) {
421 if (c ==
'\'' || c ==
'\"') str[i] =
'.';
430 dquot ?
" \"%s\" " :
" '%s' ",
435 export_tree_rek(tree->get_leftson(), out, export_branchlens, dquot);
if (export_branchlens) fprintf(out,
":%.5f,", tree->
leftlen);
436 export_tree_rek(tree->get_rightson(), out, export_branchlens, dquot);
if (export_branchlens) fprintf(out,
":%.5f", tree->
rightlen);
442 dquot ?
"\"%s\"" :
"'%s'",
460 one = tree->get_leftson();
461 two = tree->get_rightson()->get_leftson();
462 three = tree->get_rightson()->get_rightson();
465 one = tree->get_leftson()->get_leftson();
466 two = tree->get_leftson()->get_rightson();
467 three = tree->get_rightson();
470 export_tree_rek(one, out, export_branchlens, dquot);
if (export_branchlens) fprintf(out,
":%.5f", 1.0);
fputc(
',', out);
471 export_tree_rek(two, out, export_branchlens, dquot);
if (export_branchlens) fprintf(out,
":%.5f", 1.0);
fputc(
',', out);
472 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 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)
GBT_RemarkType parse_bootstrap(double &bootstrap) const
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 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 GBT_link_tree(TreeNode *tree, GBDATA *gb_main, bool show_status, int *zombies, int *duplicates)
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)
static void error(const char *msg)
void replace_by_underscore(char *str, const char *toReplace)
static char * complete_newick_comment(const char *comment)
GB_ERROR TREE_write_Newick(GBDATA *gb_main, const char *tree_name, const TreeLabeler &labeler, bool save_branchlengths, bool save_bootstraps, bool save_groupnames, bool pretty, LabelQuoting quoteMode, const char *path)
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,...)
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_node_print(GBDATA *gb_main, FILE *out, TreeNode *tree, const char *tree_name, bool pretty, int indent, const TreeLabeler &labeler, bool save_branchlengths, bool save_bootstraps, bool save_groupnames, LabelQuoting qmode)
static GB_ERROR export_tree_label(const char *label, FILE *out, LabelQuoting qmode)
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)