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 { |
---|
36 | GB_ERROR error = NULp; |
---|
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 | |
---|