ARB
arb_export_tree.cxx
Go to the documentation of this file.
1 // =============================================================== //
2 // //
3 // File : arb_export_tree.cxx //
4 // Purpose : //
5 // //
6 // Institute of Microbiology (Technical University Munich) //
7 // http://www.arb-home.de/ //
8 // //
9 // =============================================================== //
10 
11 #include <TreeWrite.h>
12 #include <TreeNode.h>
13 #include <arb_handlers.h>
14 #include <arb_global_defs.h>
15 
16 int main(int argc, char **argv) {
17  int exitcode = EXIT_SUCCESS;
18 
19  ARB_redirect_handlers_to(stderr, stderr);
20 
21  if (argc < 2) {
22  fprintf(stderr,
23  "\n"
24  "arb_export_tree - exports tree from running ARB to stdout\n"
25  "syntax: arb_export_tree [--bifurcated] TREE_NAME [DBNAME]\n"
26  " --bifurcated write a bifurcated tree (default is a trifurcated tree)\n"
27  " --nobranchlens do not write branchlengths\n"
28  " --doublequotes use doublequotes (default is singlequotes)\n"
29  "\n"
30  "Note: If TREE_NAME is '\?\?\?\?' or '' an empty tree is written.\n"
31  " If DBNAME is not specified, the default DB server ':' will be used.\n"
32  );
33  exitcode = EXIT_FAILURE;
34  }
35  else {
37  bool trifurcated = true;
38  bool branchlens = true;
39  bool doublequotes = false;
40  const char *tree_name = NULp;
41  const char *db_name = NULp;
42 
43  for (int a = 1; a < argc && !error; a++) {
44  if (strcmp(argv[a], "--bifurcated") == 0) trifurcated = false;
45  else if (strcmp(argv[a], "--nobranchlens") == 0) branchlens = false;
46  else if (strcmp(argv[a], "--doublequotes") == 0) doublequotes = true;
47  else {
48  if (!tree_name) tree_name = argv[a];
49  else if (!db_name) db_name = argv[a];
50  else {
51  error = GBS_global_string("Superfluous argument '%s'", argv[a]);
52  }
53  }
54  }
55 
56  if (!tree_name && !error) error = "Missing argument TREE_NAME";
57 
58  if (!error) {
59  if (!db_name) db_name = ":";
60 
61  GB_shell shell;
62  GBDATA *gb_main = GBT_open(db_name, "r");
63 
64  if (!gb_main) error = GB_await_error();
65  else {
66  {
67  GB_transaction ta(gb_main);
68 
69  TreeNode *tree = GBT_read_tree(gb_main, tree_name, new SimpleRoot);
70  if (tree) {
71  error = TREE_export_tree(gb_main, stdout, tree, trifurcated, branchlens, doublequotes);
72  UNCOVERED();
73  destroy(tree);
74  }
75  else {
76  GB_ERROR why_cant_read = GB_await_error();
77  if (tree_name[0] && strcmp(tree_name, NO_TREE_SELECTED) != 0) {
78  // if no tree selected -> no error, just export empty tree
79  char *warning = GBS_global_string_copy("arb_export_tree from '%s': %s", db_name, why_cant_read);
80  GBT_message(gb_main, warning);
81  free(warning);
82  }
83  }
84  }
85 
86  if (!error) fprintf(stdout, ";\n"); // aka empty tree
87  GB_close(gb_main);
88  }
89  }
90 
91  if (error) {
92  fprintf(stderr, "\narb_export_tree: Error: %s\n", error);
93  exitcode = EXIT_FAILURE;
94  }
95 
96  }
97  return exitcode;
98 }
99 
const char * GB_ERROR
Definition: arb_core.h:25
GB_ERROR TREE_export_tree(GBDATA *, FILE *out, TreeNode *tree, bool triple_root, bool export_branchlens, bool dquot)
Definition: TreeWrite.cxx:431
#define NO_TREE_SELECTED
TreeNode * GBT_read_tree(GBDATA *gb_main, const char *tree_name, TreeRoot *troot)
Definition: adtree.cxx:791
const char * GBS_global_string(const char *templat,...)
Definition: arb_msg.cxx:204
void warning(int warning_num, const char *warning_message)
Definition: util.cxx:61
#define EXIT_SUCCESS
Definition: arb_a2ps.c:154
GBDATA * GBT_open(const char *path, const char *opent)
Definition: adtools.cxx:524
GB_ERROR GB_await_error()
Definition: arb_msg.cxx:353
static void error(const char *msg)
Definition: mkptypes.cxx:96
#define EXIT_FAILURE
Definition: arb_a2ps.c:157
int main(int argc, char **argv)
void GBT_message(GBDATA *gb_main, const char *msg)
Definition: adtools.cxx:238
#define NULp
Definition: cxxforward.h:97
GB_transaction ta(gb_var)
void destroy(TreeNode *that)
Definition: TreeNode.h:559
GBDATA * gb_main
Definition: adname.cxx:33
void ARB_redirect_handlers_to(FILE *errStream, FILE *outStream)
char * GBS_global_string_copy(const char *templat,...)
Definition: arb_msg.cxx:195
void GB_close(GBDATA *gbd)
Definition: arbdb.cxx:625
#define UNCOVERED()
Definition: arb_assert.h:380