source: tags/ms_r16q2/NTREE/ad_trees.cxx

Last change on this file was 15019, checked in by westram, 8 years ago
  • move some code used by "Sort tree by other tree" to header
    • rename classes PosInfoTreeRelativePosition + SpeciesPositionTreePositionLookup
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 57.7 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : ad_trees.cxx                                      //
4//   Purpose   :                                                   //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#include "tree_position.h"
12#include "ad_trees.h"
13#include "NT_tree_cmp.h"
14
15#include <CT_ctree.hxx>
16
17#include <TreeAdmin.h>
18#include <TreeRead.h>
19#include <TreeWrite.h>
20#include <TreeCallbacks.hxx>
21
22#include <awt_sel_boxes.hxx>
23#include <awt_modules.hxx>
24#include <awt_TreeAwars.hxx>
25
26#include <aw_awars.hxx>
27#include <aw_edit.hxx>
28#include <aw_file.hxx>
29#include <aw_msg.hxx>
30#include <aw_root.hxx>
31#include <aw_select.hxx>
32
33#include <arb_strbuf.h>
34#include <arb_file.h>
35#include <arb_diff.h>
36
37#include <cctype>
38#include <awt_config_manager.hxx>
39
40#define AWAR_TREE_SAV "ad_tree/"
41#define AWAR_TREE_TMP "tmp/ad_tree/"
42
43#define AWAR_TREE_SECURITY         AWAR_TREE_TMP "tree_security"
44#define AWAR_TREE_REM              AWAR_TREE_TMP "tree_rem"
45#define AWAR_TREE_IMPORT           AWAR_TREE_TMP "import_tree"
46#define AWAR_NODE_INFO_ONLY_MARKED AWAR_TREE_TMP "import_only_marked_node_info"
47
48#define AWAR_TREE_EXPORT_FILEBASE AWAR_TREE_TMP "export_tree"
49#define AWAR_TREE_EXPORT_FILTER   AWAR_TREE_EXPORT_FILEBASE "/filter"
50#define AWAR_TREE_EXPORT_NAME     AWAR_TREE_EXPORT_FILEBASE "/file_name"
51
52#define AWAR_TREE_EXPORT_SAV AWAR_TREE_SAV "export_tree/"
53
54#define AWAR_TREE_EXPORT_FORMAT             AWAR_TREE_EXPORT_SAV "format"
55#define AWAR_TREE_EXPORT_NDS                AWAR_TREE_EXPORT_SAV "NDS"
56#define AWAR_TREE_EXPORT_INCLUDE_BOOTSTRAPS AWAR_TREE_EXPORT_SAV "bootstraps"
57#define AWAR_TREE_EXPORT_INCLUDE_BRANCHLENS AWAR_TREE_EXPORT_SAV "branchlens"
58#define AWAR_TREE_EXPORT_INCLUDE_GROUPNAMES AWAR_TREE_EXPORT_SAV "groupnames"
59#define AWAR_TREE_EXPORT_HIDE_FOLDED_GROUPS AWAR_TREE_EXPORT_SAV "hide_folded"
60#define AWAR_TREE_EXPORT_QUOTEMODE          AWAR_TREE_EXPORT_SAV "quote_mode"
61#define AWAR_TREE_EXPORT_REPLACE            AWAR_TREE_EXPORT_SAV "replace"
62
63
64#define AWAR_TREE_CONSENSE_TMP AWAR_TREE_TMP "consense/"
65#define AWAR_TREE_CONSENSE_SAV AWAR_TREE_SAV "consense/"
66
67#define AWAR_TREE_CONSENSE_TREE     AWAR_TREE_CONSENSE_SAV "tree"
68#define AWAR_TREE_CONSENSE_SELECTED AWAR_TREE_CONSENSE_TMP "selected"
69
70static void tree_vars_callback(AW_root *aw_root) // Map tree vars to display objects
71{
72    if (GLOBAL.gb_main) {
73        GB_push_transaction(GLOBAL.gb_main);
74        char *treename = aw_root->awar(AWAR_TREE_NAME)->read_string();
75        GBDATA *ali_cont = GBT_find_tree(GLOBAL.gb_main, treename);
76        if (!ali_cont) {
77            aw_root->awar(AWAR_TREE_SECURITY)->unmap();
78            aw_root->awar(AWAR_TREE_REM)->unmap();
79        }
80        else {
81            GBDATA *tree_prot = GB_search(ali_cont, "security", GB_FIND);
82            if (!tree_prot) GBT_readOrCreate_int(ali_cont, "security", GB_read_security_write(ali_cont));
83            tree_prot         = GB_search(ali_cont, "security", GB_INT);
84
85            GBDATA *tree_rem = GB_search(ali_cont, "remark",   GB_STRING);
86            aw_root->awar(AWAR_TREE_SECURITY)->map(tree_prot);
87            aw_root->awar(AWAR_TREE_REM)     ->map(tree_rem);
88        }
89        char *suffix = aw_root->awar(AWAR_TREE_EXPORT_FILTER)->read_string();
90        char *fname  = GBS_string_eval(treename, GBS_global_string("*=*1.%s:tree_*=*1", suffix), 0);
91        aw_root->awar(AWAR_TREE_EXPORT_NAME)->write_string(fname); // create default file name
92        free(fname);
93        free(suffix);
94        GB_pop_transaction(GLOBAL.gb_main);
95        free(treename);
96    }
97}
98
99static void update_default_treename_cb(AW_root *aw_root) {
100    // update import tree name depending on file name
101    GB_transaction ta(GLOBAL.gb_main);
102
103    char *treename        = aw_root->awar(AWAR_TREE_IMPORT "/file_name")->read_string();
104    char *treename_nopath = strrchr(treename, '/');
105
106    if (treename_nopath) {
107        ++treename_nopath;
108    }
109    else {
110        treename_nopath = treename;
111    }
112
113    char *fname = GBS_string_eval(treename_nopath, "*.tree=tree_*1:*.ntree=tree_*1:*.xml=tree_*1:.=:-=_: =_", 0);
114    aw_root->awar(AWAR_TREE_IMPORT "/tree_name")->write_string(fname);
115
116    free(fname);
117    free(treename);
118}
119
120static void ad_tree_set_security(AW_root *aw_root)
121{
122    if (GLOBAL.gb_main) {
123        GB_transaction ta(GLOBAL.gb_main);
124        char *treename = aw_root->awar(AWAR_TREE_NAME)->read_string();
125        GBDATA *ali_cont = GBT_find_tree(GLOBAL.gb_main, treename);
126        if (ali_cont) {
127            long prot = aw_root->awar(AWAR_TREE_SECURITY)->read_int();
128            long old;
129            old = GB_read_security_delete(ali_cont);
130            GB_ERROR error = 0;
131            if (old != prot) {
132                error = GB_write_security_delete(ali_cont, prot);
133                if (!error)
134                    error = GB_write_security_write(ali_cont, prot);
135            }
136            if (error) aw_message(error);
137        }
138        free(treename);
139    }
140}
141
142enum ExportTreeType {
143    AD_TREE_EXPORT_FORMAT_NEWICK,
144    AD_TREE_EXPORT_FORMAT_XML,
145    AD_TREE_EXPORT_FORMAT_NEWICK_PRETTY,
146};
147
148enum ExportNodeType {
149    AD_TREE_EXPORT_NODE_SPECIES_NAME,
150    AD_TREE_EXPORT_NODE_NDS
151};
152
153static void update_filter_cb(AW_root *root) {
154    const char *filter_type = 0;
155
156    switch (ExportTreeType(root->awar(AWAR_TREE_EXPORT_FORMAT)->read_int())) {
157        case AD_TREE_EXPORT_FORMAT_XML: filter_type = "xml"; break;
158        case AD_TREE_EXPORT_FORMAT_NEWICK:
159        case AD_TREE_EXPORT_FORMAT_NEWICK_PRETTY:
160            switch (ExportNodeType(root->awar(AWAR_TREE_EXPORT_NDS)->read_int())) {
161                case AD_TREE_EXPORT_NODE_SPECIES_NAME:  filter_type = "tree"; break;
162                case AD_TREE_EXPORT_NODE_NDS:           filter_type = "ntree"; break;
163                default: nt_assert(0); break;
164            }
165            break;
166        default: nt_assert(0); break;
167    }
168
169    nt_assert(filter_type);
170    root->awar(AWAR_TREE_EXPORT_FILTER)->write_string(filter_type);
171}
172
173void create_trees_var(AW_root *aw_root, AW_default aw_def) {
174    AW_awar *awar_tree_name = aw_root->awar_string(AWAR_TREE_NAME, 0, aw_def)->set_srt(GBT_TREE_AWAR_SRT);
175
176    TreeAdmin::create_awars(aw_root, aw_def, true);
177
178    aw_root->awar_int   (AWAR_TREE_SECURITY, 0, aw_def);
179    aw_root->awar_string(AWAR_TREE_REM,      0, aw_def);
180
181    AW_create_fileselection_awars(aw_root, AWAR_TREE_EXPORT_FILEBASE, "", ".tree", "treefile");
182    aw_root->awar_int(AWAR_TREE_EXPORT_FORMAT, AD_TREE_EXPORT_FORMAT_NEWICK, aw_def)-> add_callback(update_filter_cb);
183    aw_root->awar_int(AWAR_TREE_EXPORT_NDS,  AD_TREE_EXPORT_NODE_SPECIES_NAME, aw_def)-> add_callback(update_filter_cb);
184
185    aw_root->awar_int(AWAR_TREE_EXPORT_INCLUDE_BOOTSTRAPS,  0, aw_def);
186    aw_root->awar_int(AWAR_TREE_EXPORT_INCLUDE_BRANCHLENS,  1, aw_def);
187    aw_root->awar_int(AWAR_TREE_EXPORT_HIDE_FOLDED_GROUPS,  0, aw_def);
188    aw_root->awar_int(AWAR_TREE_EXPORT_INCLUDE_GROUPNAMES,  1, aw_def);
189    aw_root->awar_int(AWAR_TREE_EXPORT_QUOTEMODE, TREE_SINGLE_QUOTES, aw_def); // old default behavior
190    aw_root->awar_int(AWAR_TREE_EXPORT_REPLACE, 0, aw_def); // old default behavior
191
192    AW_create_fileselection_awars(aw_root, AWAR_TREE_IMPORT, "", ".tree", "treefile");
193
194    aw_root->awar_string(AWAR_TREE_IMPORT "/tree_name", "tree_",    aw_def)->set_srt(GBT_TREE_AWAR_SRT);
195
196    aw_root->awar(AWAR_TREE_IMPORT "/file_name")->add_callback(update_default_treename_cb);
197    awar_tree_name->add_callback(tree_vars_callback);
198    awar_tree_name->map(AWAR_TREE);
199    aw_root->awar(AWAR_TREE_SECURITY)->add_callback(ad_tree_set_security);
200
201    aw_root->awar_int(AWAR_NODE_INFO_ONLY_MARKED, 0,    aw_def);
202
203    aw_root->awar_string(AWAR_TREE_CONSENSE_TREE, "tree_consensus", aw_def)->set_srt(GBT_TREE_AWAR_SRT);
204    AW_awar *ctree_awar = aw_root->awar_string(AWAR_TREE_CONSENSE_SELECTED, "", aw_def);
205    AWT_registerTreeAwarSimple(ctree_awar);
206
207    update_filter_cb(aw_root);
208    tree_vars_callback(aw_root);
209}
210
211static void tree_save_cb(AW_window *aww) {
212    AW_root  *aw_root   = aww->get_root();
213    char     *tree_name = aw_root->awar(AWAR_TREE_NAME)->read_string();
214
215    GB_ERROR error = 0;
216
217    if (!tree_name || !strlen(tree_name)) {
218        error = "Please select a tree first";
219    }
220    else {
221        char *fname   = aw_root->awar(AWAR_TREE_EXPORT_NAME)->read_string();
222        char *db_name = aw_root->awar(AWAR_DB_NAME)->read_string();
223
224        bool                use_NDS    = ExportNodeType(aw_root->awar(AWAR_TREE_EXPORT_NDS)->read_int()) == AD_TREE_EXPORT_NODE_NDS;
225        ExportTreeType      exportType = static_cast<ExportTreeType>(aw_root->awar(AWAR_TREE_EXPORT_FORMAT)->read_int());
226        TREE_node_text_gen *node_gen   = use_NDS ? new TREE_node_text_gen(make_node_text_init, make_node_text_nds) : 0;
227
228        switch (exportType) {
229            case AD_TREE_EXPORT_FORMAT_XML:
230                error = TREE_write_XML(GLOBAL.gb_main, db_name, tree_name, node_gen,
231                                       aw_root->awar(AWAR_TREE_EXPORT_HIDE_FOLDED_GROUPS)->read_int(),
232                                       fname);
233                break;
234
235            case AD_TREE_EXPORT_FORMAT_NEWICK:
236            case AD_TREE_EXPORT_FORMAT_NEWICK_PRETTY:
237                TREE_node_quoting quoteMode = TREE_node_quoting(aw_root->awar(AWAR_TREE_EXPORT_QUOTEMODE)->read_int());
238                if (aw_root->awar(AWAR_TREE_EXPORT_REPLACE)->read_int()) {
239                    quoteMode = TREE_node_quoting(quoteMode|TREE_FORCE_REPLACE);
240                }
241
242                error = TREE_write_Newick(GLOBAL.gb_main, tree_name, node_gen,
243                                          aw_root->awar(AWAR_TREE_EXPORT_INCLUDE_BRANCHLENS)->read_int(),
244                                          aw_root->awar(AWAR_TREE_EXPORT_INCLUDE_BOOTSTRAPS)->read_int(),
245                                          aw_root->awar(AWAR_TREE_EXPORT_INCLUDE_GROUPNAMES)->read_int(),
246                                          exportType == AD_TREE_EXPORT_FORMAT_NEWICK_PRETTY,
247                                          quoteMode,
248                                          fname);
249                break;
250        }
251
252        AW_refresh_fileselection(aw_root, AWAR_TREE_EXPORT_FILEBASE);
253
254        delete node_gen;
255        free(db_name);
256        free(fname);
257    }
258
259    aww->hide_or_notify(error);
260    free(tree_name);
261}
262
263static AWT_config_mapping_def tree_export_config_mapping[] = {
264    { AWAR_TREE_EXPORT_FORMAT,             "format" },
265    { AWAR_TREE_EXPORT_NDS,                "nodetype" },
266    { AWAR_TREE_EXPORT_INCLUDE_BRANCHLENS, "lengths" },
267    { AWAR_TREE_EXPORT_INCLUDE_BOOTSTRAPS, "bootstraps" },
268    { AWAR_TREE_EXPORT_INCLUDE_GROUPNAMES, "groupnames" },
269    { AWAR_TREE_EXPORT_HIDE_FOLDED_GROUPS, "hidefolded" },
270    { AWAR_TREE_EXPORT_QUOTEMODE,          "quotemode" },
271    { AWAR_TREE_EXPORT_REPLACE,            "replacechars" },
272
273    { 0, 0 }
274};
275
276static AW_window *create_tree_export_window(AW_root *root) {
277    AW_window_simple *aws = new AW_window_simple;
278    aws->init(root, "SAVE_TREE", "TREE SAVE");
279    aws->load_xfig("tree_export.fig");
280
281    aws->at("close");
282    aws->callback(AW_POPDOWN);
283    aws->create_button("CLOSE", "CLOSE", "C");
284
285    aws->at("help");
286    aws->callback(makeHelpCallback("tr_export.hlp"));
287    aws->create_button("HELP", "HELP", "H");
288
289    AW_create_standard_fileselection(aws, AWAR_TREE_EXPORT_FILEBASE);
290
291    aws->auto_space(10, 10);
292
293    aws->at("user");
294    aws->create_option_menu(AWAR_TREE_EXPORT_FORMAT, true);
295    aws->insert_option("NEWICK TREE FORMAT",                   "N", AD_TREE_EXPORT_FORMAT_NEWICK);
296    aws->insert_option("NEWICK TREE FORMAT (pretty, but big)", "P", AD_TREE_EXPORT_FORMAT_NEWICK_PRETTY);
297    aws->insert_option("ARB_XML TREE FORMAT",                  "X", AD_TREE_EXPORT_FORMAT_XML);
298    aws->update_option_menu();
299
300    aws->at("user2");
301    aws->label("Nodetype");
302    aws->create_toggle_field(AWAR_TREE_EXPORT_NDS, 1);
303    aws->insert_default_toggle("Species ID ('name')", "S", 0);
304    aws->insert_toggle("NDS", "N", 1);
305    aws->update_toggle_field();
306
307    aws->at_newline(); aws->label("Save branch lengths"); aws->create_toggle(AWAR_TREE_EXPORT_INCLUDE_BRANCHLENS);
308    aws->at_newline(); aws->label("Save bootstrap values"); aws->create_toggle(AWAR_TREE_EXPORT_INCLUDE_BOOTSTRAPS);
309    aws->at_newline(); aws->label("Save group names"); aws->create_toggle(AWAR_TREE_EXPORT_INCLUDE_GROUPNAMES);
310    aws->at_newline(); aws->label("Hide folded groups (XML only)"); aws->create_toggle(AWAR_TREE_EXPORT_HIDE_FOLDED_GROUPS);
311
312    aws->at_newline();
313    aws->label("Name quoting (Newick only)");
314    aws->create_option_menu(AWAR_TREE_EXPORT_QUOTEMODE, true);
315    aws->insert_option("none",            "n", TREE_DISALLOW_QUOTES);
316    aws->insert_option("single",          "s", TREE_SINGLE_QUOTES);
317    aws->insert_option("double",          "d", TREE_DOUBLE_QUOTES);
318    aws->insert_option("single (forced)", "i", TREE_SINGLE_QUOTES|TREE_FORCE_QUOTES);
319    aws->insert_option("double (forced)", "o", TREE_DOUBLE_QUOTES|TREE_FORCE_QUOTES);
320    aws->update_option_menu();
321
322    aws->at_newline(); aws->label("Replace problem chars"); aws->create_toggle(AWAR_TREE_EXPORT_REPLACE);
323
324    aws->at_newline();
325    aws->button_length(10);
326
327    aws->callback(tree_save_cb);
328    aws->create_button("SAVE", "SAVE", "o");
329
330    AWT_insert_config_manager(aws, AW_ROOT_DEFAULT, "tree_export", tree_export_config_mapping);
331
332    aws->window_fit();
333    update_filter_cb(root);
334
335    return aws;
336}
337
338static char *readXmlTree(char *fname) {
339    // create a temp file
340    char tempFile[]  = "newickXXXXXX";
341    int createTempFile = mkstemp(tempFile);
342
343    if (createTempFile) {
344        GBS_strstruct *buf = GBS_stropen(strlen(fname));
345
346        // extract path from fname in order to place a copy of dtd file required to validate xml file
347        {
348            char *tmpFname = strdup(fname);
349            for (char *tok = strtok(tmpFname, "/"); tok;) {
350                char *tmp = tok;
351                tok = strtok(0, "/");
352                if (tok) {
353                    GBS_strcat(buf, "/");
354                    GBS_strcat(buf, tmp);
355                }
356            }
357            free(tmpFname);
358        }
359
360        char *path = GBS_strclose(buf);
361
362        // linking arb_tree.dtd file to the Path from where xml file is loaded
363#if defined(WARN_TODO)
364#warning fix hack
365#endif
366        char *command = GBS_global_string_copy("ln -s %s/lib/dtd/arb_tree.dtd %s/.", GB_getenvARBHOME(), path);
367        GB_xcmd(command, false, true);
368
369        // execute xml2newick to convert xml format tree to newick format tree
370        command = GBS_global_string_copy("xml2newick %s %s", fname, tempFile);
371        GB_xcmd(command, false, true);
372
373        free(command);
374        free(path);
375
376        // return newick format tree file
377        return strdup(tempFile);
378    }
379    else {
380        printf("Failed to create Temporary File to Parse xml file!\n");
381        return 0;
382    }
383}
384
385static void tree_load_cb(AW_window *aww) {
386    GB_ERROR  error     = 0;
387    AW_root  *aw_root   = aww->get_root();
388    char     *tree_name = aw_root->awar(AWAR_TREE_IMPORT "/tree_name")->read_string();
389
390    {
391        char *pcTreeFormat = aw_root->awar(AWAR_TREE_IMPORT "/filter")->read_string();
392        char *fname        = aw_root->awar(AWAR_TREE_IMPORT "/file_name")->read_string();
393        char *warnings     = 0;
394        char *tree_comment = 0;
395
396        TreeNode *tree;
397        if (strcmp(pcTreeFormat, "xml") == 0) {
398            char *tempFname = readXmlTree(fname);
399            tree = TREE_load(tempFname, new SimpleRoot, &tree_comment, true, &warnings);
400            GB_unlink_or_warn(tempFname, NULL);
401            free(tempFname);
402        }
403        else {
404            tree = TREE_load(fname, new SimpleRoot, &tree_comment, true, &warnings);
405        }
406
407        if (!tree) error = GB_await_error();
408        else {
409            if (warnings) GBT_message(GLOBAL.gb_main, warnings);
410
411            {
412                GB_transaction ta(GLOBAL.gb_main);
413                error = GBT_write_tree_with_remark(GLOBAL.gb_main, tree_name, tree, tree_comment);
414                error = ta.close(error);
415            }
416
417            if (!error) aw_root->awar(AWAR_TREE)->write_string(tree_name); // show new tree
418
419            UNCOVERED();
420            destroy(tree);
421        }
422
423        free(warnings);
424        free(tree_comment);
425        free(fname);
426        free(pcTreeFormat);
427    }
428
429    aww->hide_or_notify(error);
430    free(tree_name);
431}
432
433static AW_window *create_tree_import_window(AW_root *root)
434{
435    AW_window_simple *aws = new AW_window_simple;
436    aws->init(root, "LOAD_TREE", "TREE LOAD");
437    aws->load_xfig("sel_box_tree.fig");
438
439    aws->at("close");
440    aws->callback(AW_POPDOWN);
441    aws->create_button("CLOSE", "CLOSE", "C");
442
443    aws->at("help");
444    aws->callback(makeHelpCallback("tr_import.hlp"));
445    aws->create_button("HELP", "HELP", "H");
446
447    aws->at("format");
448    aws->create_option_menu(AWAR_TREE_IMPORT "/filter", false);
449    aws->insert_default_option("Newick", "t", "tree");
450    aws->insert_option("XML", "x", "xml");
451    aws->update_option_menu();
452
453    aws->at("user");
454    aws->label("Tree name");
455    aws->create_input_field(AWAR_TREE_IMPORT "/tree_name", 15);
456
457    AW_create_standard_fileselection(aws, AWAR_TREE_IMPORT);
458
459    aws->at("save2");
460    aws->callback(tree_load_cb);
461    aws->create_button("LOAD", "LOAD", "o");
462
463    aws->window_fit();
464
465    return aws;
466}
467
468static void ad_move_tree_info(AW_window *aww, TreeInfoMode mode) {
469    bool nodes_with_marked_only = false;
470
471    char     *log_file = 0;
472    GB_ERROR  error    = 0;
473
474    if (mode == TREE_INFO_COPY || mode == TREE_INFO_ADD) {
475        // move or add node-info writes a log file (containing errors)
476        // compare_node_info only sets remark branches
477        char *log_name       = GB_unique_filename("arb_node", "log");
478        log_file             = GB_create_tempfile(log_name);
479        if (!log_file) error = GB_await_error();
480        free(log_name);
481
482        nodes_with_marked_only = aww->get_root()->awar(AWAR_NODE_INFO_ONLY_MARKED)->read_int();
483    }
484
485    if (!error) {
486        AW_root *awr      = aww->get_root();
487        char    *src_tree = TreeAdmin::source_tree_awar(awr)->read_string();
488        char    *dst_tree = TreeAdmin::dest_tree_awar(awr)->read_string();
489
490        error = AWT_move_info(GLOBAL.gb_main, src_tree, dst_tree, log_file, mode, nodes_with_marked_only);
491        if (log_file) {
492            AW_edit(log_file);
493            GB_remove_on_exit(log_file);
494        }
495
496        free(dst_tree);
497        free(src_tree);
498    }
499
500    if (error) aw_message(error);
501    else aww->hide();
502
503    free(log_file);
504}
505
506static void swap_source_dest_cb(AW_window *aww) {
507    AW_root *root = aww->get_root();
508
509    AW_awar *s = TreeAdmin::source_tree_awar(root);
510    AW_awar *d = TreeAdmin::dest_tree_awar(root);
511
512    char *old_src = s->read_string();
513    s->write_string(d->read_char_pntr());
514    d->write_string(old_src);
515    free(old_src);
516}
517
518static void copy_tree_awar_cb(UNFIXED, AW_awar *aw_source, AW_awar *aw_dest) {
519    const char *tree = aw_source->read_char_pntr();
520    if (tree && tree[0]) aw_dest->write_string(tree);
521}
522
523static AW_window_simple *create_select_two_trees_window(AW_root *root, const char *winId, const char *winTitle, const char *helpFile) {
524    AW_window_simple *aws = new AW_window_simple;
525    aws->init(root, winId, winTitle);
526    aws->load_xfig("ad_two_trees.fig");
527
528    aws->at("close");
529    aws->auto_space(10, 3);
530
531    aws->callback(AW_POPDOWN);
532    aws->create_button("CLOSE", "Close", "C");
533
534    aws->at("help");
535    aws->callback(makeHelpCallback(helpFile));
536    aws->create_button("HELP", "Help", "H");
537
538    aws->at("tree1");
539    awt_create_TREE_selection_list(GLOBAL.gb_main, aws, TreeAdmin::source_tree_awar(root)->awar_name, true);
540    aws->at("tree2");
541    awt_create_TREE_selection_list(GLOBAL.gb_main, aws, TreeAdmin::dest_tree_awar(root)->awar_name, false);
542
543    AW_awar *awar_displayed_tree = root->awar(AWAR_TREE_NAME);
544
545    aws->at("select1");
546    aws->callback(makeWindowCallback(copy_tree_awar_cb, awar_displayed_tree, TreeAdmin::source_tree_awar(root)));  aws->create_autosize_button("SELECT_DISPLAYED1", "Use");
547    aws->callback(makeWindowCallback(copy_tree_awar_cb, TreeAdmin::source_tree_awar(root), awar_displayed_tree));  aws->create_autosize_button("DISPLAY_SELECTED1", "Display");
548
549    aws->callback(swap_source_dest_cb);
550    aws->create_autosize_button("SWAP", "Swap");
551
552    aws->at("select2");
553    aws->callback(makeWindowCallback(copy_tree_awar_cb, awar_displayed_tree, TreeAdmin::dest_tree_awar(root)));  aws->create_autosize_button("SELECT_DISPLAYED2", "Use");
554    aws->callback(makeWindowCallback(copy_tree_awar_cb, TreeAdmin::dest_tree_awar(root), awar_displayed_tree));  aws->create_autosize_button("DISPLAY_SELECTED2", "Display");
555
556    aws->at("user");
557
558    return aws;
559}
560
561static AW_window_simple *create_select_other_tree_window(AW_root *root, const char *winId, const char *winTitle, const char *helpFile, const char *displayed_tree_awarname) {
562    AW_window_simple *aws = new AW_window_simple;
563    aws->init(root, winId, winTitle);
564    aws->load_xfig("ad_one_tree.fig");
565
566    aws->at("close");
567    aws->auto_space(10, 3);
568
569    aws->callback(AW_POPDOWN);
570    aws->create_button("CLOSE", "Close", "C");
571
572    aws->at("help");
573    aws->callback(makeHelpCallback(helpFile));
574    aws->create_button("HELP", "Help", "H");
575
576    AW_awar *awar_displayed_tree = root->awar(displayed_tree_awarname);
577
578    aws->at("tree");
579    awt_create_TREE_selection_list(GLOBAL.gb_main, aws, TreeAdmin::source_tree_awar(root)->awar_name, true);
580
581    aws->at("select");
582    aws->callback(makeWindowCallback(copy_tree_awar_cb, awar_displayed_tree, TreeAdmin::source_tree_awar(root)));  aws->create_autosize_button("SELECT_DISPLAYED", "Use");
583    aws->callback(makeWindowCallback(copy_tree_awar_cb, TreeAdmin::source_tree_awar(root), awar_displayed_tree));  aws->create_autosize_button("DISPLAY_SELECTED", "Display");
584
585    aws->at("user");
586
587    return aws;
588}
589
590static AW_window *create_tree_diff_window(AW_root *root) {
591    AW_window_simple *aws = create_select_two_trees_window(root, "CMP_TOPOLOGY", "Compare tree topologies", "tree_diff.hlp");
592
593    aws->callback(makeWindowCallback(ad_move_tree_info, TREE_INFO_COMPARE));
594    aws->create_autosize_button("CMP_TOPOLOGY", "Compare topologies");
595
596    return aws;
597}
598
599static AW_window *create_tree_cmp_window(AW_root *root) {
600    AW_window_simple *aws = create_select_two_trees_window(root, "COPY_NODE_INFO_OF_TREE", "Move tree node info", "tree_cmp.hlp");
601
602    aws->button_length(11);
603
604    aws->callback(makeWindowCallback(ad_move_tree_info, TREE_INFO_COPY));
605    aws->create_button("COPY_INFO", "Copy info");
606
607    aws->label("copy/add only info containing marked species");
608    aws->create_toggle(AWAR_NODE_INFO_ONLY_MARKED);
609
610    aws->at_newline();
611
612    aws->callback(makeWindowCallback(ad_move_tree_info, TREE_INFO_ADD));
613    aws->create_button("ADD_INFO", "Add info");
614
615    return aws;
616}
617
618static void reorder_trees_cb(AW_window *aww, awt_reorder_mode dest) {
619    // moves the tree in the list of trees
620
621    char     *tree_name = aww->get_root()->awar(AWAR_TREE_NAME)->read_string();
622    GB_ERROR  error     = NULL;
623
624    GB_transaction ta(GLOBAL.gb_main);
625    GBDATA *gb_treedata   = GBT_get_tree_data(GLOBAL.gb_main);
626    GBDATA *gb_moved_tree = GB_entry(gb_treedata, tree_name);
627
628    if (!gb_moved_tree) {
629        error = "No tree selected";
630    }
631    else {
632        GBT_ORDER_MODE  move_mode;
633        GBDATA         *gb_target_tree = NULL;
634
635        switch (dest) {
636            case ARM_UP:
637                move_mode      = GBT_INFRONTOF;
638                gb_target_tree = GBT_tree_infrontof(gb_moved_tree);
639                if (gb_target_tree) break;
640                // fall-through (move top-tree up = move to bottom)
641            case ARM_BOTTOM:
642                move_mode      = GBT_BEHIND;
643                gb_target_tree = GBT_find_bottom_tree(GLOBAL.gb_main);
644                break;
645
646            case ARM_DOWN:
647                move_mode      = GBT_BEHIND;
648                gb_target_tree = GBT_tree_behind(gb_moved_tree);
649                if (gb_target_tree) break;
650                // fall-through (move bottom-tree down = move to top)
651            case ARM_TOP:
652                move_mode      = GBT_INFRONTOF;
653                gb_target_tree = GBT_find_top_tree(GLOBAL.gb_main);
654                break;
655        }
656
657        if (gb_target_tree && gb_target_tree != gb_moved_tree) {
658            error = GBT_move_tree(gb_moved_tree, move_mode, gb_target_tree);
659        }
660    }
661
662    if (error) aw_message(error);
663    free(tree_name);
664}
665
666void popup_tree_admin_window(AW_window *aws) {
667    popup_tree_admin_window(aws->get_root());
668}
669void popup_tree_admin_window(AW_root *aw_root) {
670    static AW_window_simple *aws = 0;
671    if (!aws) {
672        aws = new AW_window_simple;
673        aws->init(aw_root, "TREE_ADMIN", "TREE ADMIN");
674        aws->load_xfig("ad_tree.fig");
675
676        aws->callback(AW_POPDOWN);
677        aws->at("close");
678        aws->create_button("CLOSE", "CLOSE", "C");
679
680        aws->callback(makeHelpCallback("treeadm.hlp"));
681        aws->at("help");
682        aws->create_button("HELP", "HELP", "H");
683
684        aws->button_length(40);
685
686        aws->at("sel");
687        aws->create_button(0, AWAR_TREE_NAME, 0, "+");
688
689        aws->at("security");
690        aws->create_option_menu(AWAR_TREE_SECURITY, true);
691        aws->insert_option("0", "0", 0);
692        aws->insert_option("1", "1", 1);
693        aws->insert_option("2", "2", 2);
694        aws->insert_option("3", "3", 3);
695        aws->insert_option("4", "4", 4);
696        aws->insert_option("5", "5", 5);
697        aws->insert_default_option("6", "6", 6);
698        aws->update_option_menu();
699
700        aws->at("rem");
701        aws->create_text_field(AWAR_TREE_REM);
702
703
704        aws->button_length(20);
705
706        static TreeAdmin::Spec spec(GLOBAL.gb_main, AWAR_TREE_NAME);
707
708        aws->at("delete");
709        aws->help_text("treeadm.hlp");
710        aws->callback(makeWindowCallback(TreeAdmin::delete_tree_cb, &spec));
711        aws->create_button("DELETE", "Delete", "D");
712
713        aws->at("rename");
714        aws->help_text("treeadm.hlp");
715        aws->callback(makeCreateWindowCallback(TreeAdmin::create_rename_window, &spec));
716        aws->create_button("RENAME", "Rename", "R");
717
718        aws->at("copy");
719        aws->help_text("treeadm.hlp");
720        aws->callback(makeCreateWindowCallback(TreeAdmin::create_copy_window, &spec));
721        aws->create_button("COPY", "Copy", "C");
722
723        aws->at("cmp");
724        aws->help_text("tree_diff.hlp");
725        aws->callback(create_tree_diff_window);
726        aws->sens_mask(AWM_EXP);
727        aws->create_button("CMP_TOPOLOGY", "Compare topology", "T");
728        aws->sens_mask(AWM_ALL);
729
730        aws->at("move");
731        aws->help_text("tree_cmp.hlp");
732        aws->callback(create_tree_cmp_window);
733        aws->create_button("MOVE_NODE_INFO", "Move node info", "C");
734
735        aws->at("export");
736        aws->help_text("tr_export.hlp");
737        aws->callback(create_tree_export_window);
738        aws->create_button("EXPORT", "Export", "E");
739
740        aws->at("import");
741        aws->help_text("tr_import.hlp");
742        aws->callback(create_tree_import_window);
743        aws->create_button("IMPORT", "Import", "I");
744
745        aws->button_length(0);
746
747        aws->at("list");
748        awt_create_TREE_selection_list(GLOBAL.gb_main, aws, AWAR_TREE_NAME, true);
749
750        aws->at("sort");
751        awt_create_order_buttons(aws, reorder_trees_cb);
752    }
753
754    aws->activate();
755}
756
757// -----------------------
758//      consense tree
759
760
761static void create_consense_tree_cb(AW_window *aww, AW_selection *selected_trees) {
762    AW_root  *aw_root = aww->get_root();
763    GB_ERROR  error   = NULL;
764
765    const char *cons_tree_name = aw_root->awar(AWAR_TREE_CONSENSE_TREE)->read_char_pntr();
766    if (!cons_tree_name || !cons_tree_name[0]) {
767        error = "No name specified for the consensus tree";
768    }
769    else {
770        StrArray tree_names;
771        selected_trees->get_values(tree_names);
772
773        if (tree_names.size()<2) {
774            error = "Not enough trees selected (at least 2 needed)";
775        }
776        else {
777            GBDATA *gb_main = GLOBAL.gb_main;
778            GB_transaction ta(gb_main);
779
780            {
781                arb_progress progress("Building consensus tree", 2); // 2 steps: deconstruct, reconstruct
782                ConsensusTreeBuilder tree_builder;
783
784                progress.subtitle("loading input trees");
785                for (size_t t = 0; t<tree_names.size() && !error; ++t) {
786                    TreeRoot      *root = new SizeAwareRoot; // will be deleted when tree gets deleted
787                    SizeAwareTree *tree = DOWNCAST(SizeAwareTree*, GBT_read_tree(gb_main, tree_names[t], root));
788                    if (!tree) {
789                        error = GB_await_error();
790                    }
791                    else {
792                        tree_builder.add(tree, tree_names[t], 1.0);
793                    }
794                }
795
796                if (!error) {
797                    size_t    species_count;
798                    TreeNode *cons_tree = tree_builder.get(species_count, error); // triggers 2 implicit progress increments
799
800                    if (!error && progress.aborted()) {
801                        error = "user abort";
802                    }
803
804                    nt_assert(contradicted(cons_tree, error));
805                    if (cons_tree) {
806                        char *comment = tree_builder.get_remark();
807                        error         = GBT_write_tree_with_remark(gb_main, cons_tree_name, cons_tree, comment);
808                        free(comment);
809                        UNCOVERED();
810                        destroy(cons_tree);
811                    }
812                }
813                if (error) progress.done();
814            }
815            error = ta.close(error);
816        }
817    }
818
819    if (!error) {
820        aw_root->awar(AWAR_TREE_NAME)->write_string(cons_tree_name); // show in main window
821    }
822
823    aw_message_if(error);
824}
825
826static void use_selected_as_target_cb(AW_window *aww) {
827    AW_root *aw_root = aww->get_root();
828    aw_root->awar(AWAR_TREE_CONSENSE_TREE)->write_string(aw_root->awar(AWAR_TREE_CONSENSE_SELECTED)->read_char_pntr());
829}
830
831AW_window *NT_create_consense_window(AW_root *aw_root) {
832    static AW_window_simple *aws = 0;
833    if (!aws) {
834        aws = new AW_window_simple;
835        aws->init(aw_root, "CONSENSE_TREE", "Consensus Tree");
836        aws->load_xfig("ad_cons_tree.fig");
837
838        aws->auto_space(10, 10);
839
840        aws->callback(AW_POPDOWN);
841        aws->at("close");
842        aws->create_button("CLOSE", "CLOSE", "C");
843
844        aws->callback(makeHelpCallback("consense_tree.hlp"));
845        aws->at("help");
846        aws->create_button("HELP", "HELP", "H");
847
848        aws->at("list");
849        AW_DB_selection *all_trees      = awt_create_TREE_selection_list(GLOBAL.gb_main, aws, AWAR_TREE_CONSENSE_SELECTED, true);
850        AW_selection    *selected_trees = awt_create_subset_selection_list(aws, all_trees->get_sellist(), "selected", "add", "sort");
851
852        aws->at("name");
853        aws->create_input_field(AWAR_TREE_CONSENSE_TREE);
854
855        aws->callback(use_selected_as_target_cb);
856        aws->create_button("USE_AS_TARGET", "#moveLeft.xpm", 0);
857
858        aws->at("build");
859        aws->callback(makeWindowCallback(create_consense_tree_cb, selected_trees));
860        aws->create_autosize_button("BUILD", "Build consensus tree", "B");
861    }
862    return aws;
863}
864
865class CombinedPosInfo {
866    // combines relative positions of a subtree in 2 trees (source- and target-tree).
867    // provides compare operations for SortByTopo
868
869    TreeRelativePosition source; // in source tree ("ordering tree")
870    TreeRelativePosition target; // in target tree ("modified tree")
871
872public:
873
874    CombinedPosInfo(const TreeRelativePosition& s, const TreeRelativePosition& t)
875        : source(s),
876          target(t)
877    {
878        nt_assert(target.is_known());
879    }
880    CombinedPosInfo(const CombinedPosInfo& c1, const CombinedPosInfo& c2)
881        : source(c1.source, c2.source),
882          target(c1.target, c2.target)
883    {}
884
885    int compare(const CombinedPosInfo &right) const {
886        // result similar to strcmp(this, right)
887        if (!source.is_known() || !right.source.is_known()) {
888            // one subtree is completely unknown in source-tree
889            // => keep target-tree order
890            return target.compare(right.target);
891        }
892        return source.compare(right.source);
893    }
894};
895
896class SortByTopo : virtual Noncopyable {
897    TreePositionLookup        source_pos; // in ordering topology
898    const TreePositionLookup *target_pos; // in target topology (used where source_pos does not provide order)
899
900    CombinedPosInfo reorder_subtree_rec(TreeNode *node) { // similar to ../ARBDB/TreeNode.cxx@reorder_subtree
901        static const char *smallest_leafname; // has to be set to the alphabetically smallest name (when function exits)
902
903        if (node->is_leaf) {
904            smallest_leafname = node->name;
905            return CombinedPosInfo(source_pos.relative(node->name),
906                                   target_pos->relative(node->name));
907        }
908
909        CombinedPosInfo  leftInfo       = reorder_subtree_rec(node->get_leftson());
910        const char      *smallest_left  = smallest_leafname;
911        CombinedPosInfo  rightInfo      = reorder_subtree_rec(node->get_rightson());
912        const char      *smallest_right = smallest_leafname;
913
914        bool left_leafname_bigger = strcmp(smallest_left, smallest_right)>0;
915        smallest_leafname         = left_leafname_bigger ? smallest_right : smallest_left;
916
917        {
918            int cmp = leftInfo.compare(rightInfo);
919            if (cmp>0 || (cmp == 0 && left_leafname_bigger)) {
920                node->swap_sons();
921            }
922        }
923
924        return CombinedPosInfo(leftInfo, rightInfo);
925    }
926public:
927
928    SortByTopo(const TreeNode *by)
929        : source_pos(by),
930          target_pos(NULL)
931    {}
932
933#if defined(UNIT_TESTS)
934    TreeRelativePosition sourcePos(const char *name) { return source_pos.relative(name); }
935#endif
936
937    void reorder_subtree(TreeNode *tree) {
938        TreePositionLookup tpos(tree);
939        LocallyModify<const TreePositionLookup*> provide(target_pos, &tpos);
940        reorder_subtree_rec(tree);
941    }
942};
943
944static GB_ERROR sort_tree_by_other_tree(GBDATA *gb_main, TreeNode *tree, const char *other_tree) {
945    GB_ERROR       error = NULL;
946    GB_transaction ta(gb_main);
947
948    TreeNode *otherTree   = GBT_read_tree(gb_main, other_tree, new SimpleRoot);
949    if (!otherTree) error = GB_await_error();
950    else {
951        SortByTopo sorter(otherTree);
952        destroy(otherTree);
953        sorter.reorder_subtree(tree);
954    }
955    return error;
956}
957
958static bool sort_dtree_by_other_tree_cb(TreeNode *tree, GB_ERROR& error) {
959    const char *other_tree = TreeAdmin::source_tree_awar(AW_root::SINGLETON)->read_char_pntr();
960    error = sort_tree_by_other_tree(GLOBAL.gb_main, tree, other_tree);
961    return !error;
962}
963
964static void sort_tree_by_other_tree_cb(UNFIXED, AWT_canvas *ntw) {
965    GB_ERROR error = NT_with_displayed_tree_do(ntw, sort_dtree_by_other_tree_cb);
966    aw_message_if(error);
967}
968
969AW_window *NT_create_sort_tree_by_other_tree_window(AW_root *aw_root, AWT_canvas *ntw) {
970    AW_window_simple *aws = create_select_other_tree_window(aw_root, ntw->aww->local_id("SORT_BY_OTHER"), "Sort tree by other tree", "resortbyother.hlp", ntw->user_awar);
971
972    aws->callback(makeWindowCallback(sort_tree_by_other_tree_cb, ntw));
973    aws->create_autosize_button("RESORT", "Sort according to source tree");
974
975    return aws;
976}
977
978// ---------------------------
979//      multifurcate tree
980
981#define AWAR_MFURC                    "tree/multifurc/"
982#define AWAR_MFURC_CONSIDER_BOOTSTRAP AWAR_MFURC "use_bs"
983#define AWAR_MFURC_CONSIDER_LENGTH    AWAR_MFURC "use_len"
984#define AWAR_MFURC_CONSIDER_TERMINALS AWAR_MFURC "terminals"
985#define AWAR_MFURC_LENGTH_LIMIT       AWAR_MFURC "len"
986#define AWAR_MFURC_BOOTSTRAP_LIMIT    AWAR_MFURC "bs"
987
988void NT_create_multifurcate_tree_awars(AW_root *aw_root, AW_default props) {
989    aw_root->awar_int  (AWAR_MFURC_CONSIDER_BOOTSTRAP, 0,   props);
990    aw_root->awar_int  (AWAR_MFURC_CONSIDER_LENGTH,    1,   props);
991    aw_root->awar_int  (AWAR_MFURC_CONSIDER_TERMINALS, 0,   props);
992    aw_root->awar_float(AWAR_MFURC_LENGTH_LIMIT,       0.1, props);
993    aw_root->awar_float(AWAR_MFURC_BOOTSTRAP_LIMIT,    50,  props);
994}
995static void multifurcation_cb(UNFIXED, AWT_canvas *ntw) {
996    AW_root *aw_root = ntw->aww->get_root();
997
998    float below_bootstrap = 101.0;
999    float below_length    = 1000000.0;
1000    bool  applyAtLeafs    = aw_root->awar(AWAR_MFURC_CONSIDER_TERMINALS)->read_int();
1001
1002    if (aw_root->awar(AWAR_MFURC_CONSIDER_BOOTSTRAP)->read_int()) below_bootstrap = aw_root->awar(AWAR_MFURC_BOOTSTRAP_LIMIT)->read_float();
1003    if (aw_root->awar(AWAR_MFURC_CONSIDER_LENGTH)   ->read_int()) below_length    = aw_root->awar(AWAR_MFURC_LENGTH_LIMIT)   ->read_float();
1004
1005    NT_multifurcate_tree(ntw, TreeNode::multifurc_limits(below_bootstrap, below_length, applyAtLeafs));
1006}
1007AW_window *NT_create_multifurcate_tree_window(AW_root *aw_root, AWT_canvas *ntw) {
1008    AW_window_simple *aws = new AW_window_simple;
1009
1010    aws->init(aw_root, ntw->aww->local_id("multifurcate"), "Multifurcate tree");
1011    aws->at(10, 10);
1012    aws->auto_space(10, 10);
1013
1014    aws->callback(AW_POPDOWN);
1015    aws->create_button("CLOSE", "CLOSE", "C");
1016
1017    aws->callback(makeHelpCallback("multifurcate.hlp"));
1018    aws->create_button("HELP", "HELP", "H");
1019
1020    const int LABEL_LENGTH = 46;
1021    aws->label_length(LABEL_LENGTH);
1022
1023    aws->at_newline();
1024    aws->label("Multifurcate branches with branchlength below");
1025    aws->create_toggle(AWAR_MFURC_CONSIDER_LENGTH);
1026    aws->create_input_field(AWAR_MFURC_LENGTH_LIMIT, 10);
1027
1028    aws->at_newline();
1029    aws->label("                          AND bootstrap below");
1030    aws->create_toggle(AWAR_MFURC_CONSIDER_BOOTSTRAP);
1031    aws->create_input_field(AWAR_MFURC_BOOTSTRAP_LIMIT, 10);
1032
1033    aws->label_length(0);
1034    aws->at_newline();
1035    aws->label("Also apply to terminal branches");
1036    aws->create_toggle(AWAR_MFURC_CONSIDER_TERMINALS);
1037
1038    aws->at_newline();
1039    aws->callback(makeWindowCallback(multifurcation_cb, ntw));
1040    aws->create_autosize_button("MULTIFURCATE", "Multifurcate", "M");
1041
1042    return aws;
1043}
1044
1045// --------------------------------------------------------------------------------
1046
1047#ifdef UNIT_TESTS
1048#ifndef TEST_UNIT_H
1049#include <test_unit.h>
1050#endif
1051
1052static GB_ERROR sort_namedtree_by_other_tree(GBDATA *gb_main, const char *tree, const char *other_tree) {
1053    GB_ERROR        error = NULL;
1054    GB_transaction  ta(gb_main);
1055    SizeAwareTree  *Tree  = DOWNCAST(SizeAwareTree*, GBT_read_tree(gb_main, tree, new SizeAwareRoot));
1056    if (!Tree) error      = GB_await_error();
1057    else {
1058        Tree->compute_tree();
1059        error             = sort_tree_by_other_tree(gb_main, Tree, other_tree);
1060        if (!error) error = GBT_write_tree(gb_main, tree, Tree);
1061    }
1062    destroy(Tree);
1063    return error;
1064}
1065
1066void TEST_sort_tree_by_other_tree() {
1067    GB_shell  shell;
1068    GBDATA   *gb_main = GB_open("TEST_trees.arb", "rw");
1069    TEST_REJECT_NULL(gb_main);
1070
1071    const char *topo_test   = "(((((((CloTyro3:1.046,CloTyro4:0.061):0.026,CloTyro2:0.017):0.017,CloTyrob:0.009):0.274,CloInnoc:0.371):0.057,CloBifer:0.388):0.124,(((CloButy2:0.009,CloButyr:0.000):0.564,CloCarni:0.120):0.010,CloPaste:0.179):0.131):0.081,((((CorAquat:0.084,CurCitre:0.058):0.103,CorGluta:0.522):0.053,CelBiazo:0.059):0.207,CytAquat:0.711):0.081);";
1072    const char *topo_center = "(((CloPaste:0.179,((CloButy2:0.009,CloButyr:0.000):0.564,CloCarni:0.120):0.010):0.131,((CloInnoc:0.371,((CloTyro2:0.017,(CloTyro3:1.046,CloTyro4:0.061):0.026):0.017,CloTyrob:0.009):0.274):0.057,CloBifer:0.388):0.124):0.081,((CelBiazo:0.059,((CorAquat:0.084,CurCitre:0.058):0.103,CorGluta:0.522):0.053):0.207,CytAquat:0.711):0.081);";
1073    const char *topo_bottom = "((CytAquat:0.711,(CelBiazo:0.059,(CorGluta:0.522,(CorAquat:0.084,CurCitre:0.058):0.103):0.053):0.207):0.081,((CloPaste:0.179,(CloCarni:0.120,(CloButy2:0.009,CloButyr:0.000):0.564):0.010):0.131,(CloBifer:0.388,(CloInnoc:0.371,(CloTyrob:0.009,(CloTyro2:0.017,(CloTyro3:1.046,CloTyro4:0.061):0.026):0.017):0.274):0.057):0.124):0.081);";
1074
1075    const char *topo_vs_nj_bs = "(((((((CloTyro3:1.046,CloTyro4:0.061):0.026,CloTyro2:0.017):0.017,CloTyrob:0.009):0.274,CloInnoc:0.371):0.057,CloBifer:0.388):0.124,(((CloButyr:0.000,CloButy2:0.009):0.564,CloCarni:0.120):0.010,CloPaste:0.179):0.131):0.081,(((CorGluta:0.522,(CorAquat:0.084,CurCitre:0.058):0.103):0.053,CelBiazo:0.059):0.207,CytAquat:0.711):0.081);";
1076
1077    TEST_EXPECT_DIFFERENT(topo_test,   topo_center);
1078    TEST_EXPECT_DIFFERENT(topo_test,   topo_bottom);
1079    TEST_EXPECT_DIFFERENT(topo_center, topo_bottom);
1080
1081    // create sorted copies of tree_test
1082    {
1083        GB_transaction  ta(gb_main);
1084        SizeAwareTree  *tree = DOWNCAST(SizeAwareTree*, GBT_read_tree(gb_main, "tree_test", new SizeAwareRoot));
1085        TEST_REJECT_NULL(tree);
1086        TEST_EXPECT_NEWICK(nLENGTH, tree, topo_test);
1087
1088        tree->reorder_tree(BIG_BRANCHES_TO_CENTER); TEST_EXPECT_NO_ERROR(GBT_write_tree(gb_main, "tree_sorted_center", tree)); TEST_EXPECT_NEWICK(nLENGTH, tree, topo_center);
1089        tree->reorder_tree(BIG_BRANCHES_TO_BOTTOM); TEST_EXPECT_NO_ERROR(GBT_write_tree(gb_main, "tree_sorted_bottom", tree)); TEST_EXPECT_NEWICK(nLENGTH, tree, topo_bottom);
1090
1091        // test SortByTopo
1092        {
1093            SortByTopo   sbt(tree);
1094            const double EPSILON = 0.0001;
1095
1096            TEST_EXPECT_SIMILAR(sbt.sourcePos("CytAquat").value(), 0.0, EPSILON); // leftmost species (in topo_bottom)
1097            TEST_EXPECT_SIMILAR(sbt.sourcePos("CloTyro4").value(), 1.0, EPSILON); // rightmost species
1098
1099            TEST_EXPECT_SIMILAR(sbt.sourcePos("CurCitre").value(), 0.2857, EPSILON); // (5 of 15)
1100            TEST_EXPECT_SIMILAR(sbt.sourcePos("CloButy2").value(), 0.5,    EPSILON); // center species (8 of 15)
1101            TEST_EXPECT_SIMILAR(sbt.sourcePos("CloTyrob").value(), 0.7857, EPSILON); // (12 of 15)
1102
1103            TEST_REJECT(sbt.sourcePos("Un-Known").is_known()); // unknown species
1104        }
1105
1106        tree->reorder_tree(BIG_BRANCHES_TO_EDGE); TEST_EXPECT_NO_ERROR(GBT_write_tree(gb_main, "tree_work", tree));
1107
1108        destroy(tree);
1109    }
1110
1111
1112    TEST_EXPECT_NO_ERROR(sort_namedtree_by_other_tree(gb_main, "tree_work", "tree_sorted_center")); TEST_EXPECT_SAVED_NEWICK(nLENGTH, gb_main, "tree_work", topo_center);
1113    TEST_EXPECT_NO_ERROR(sort_namedtree_by_other_tree(gb_main, "tree_work", "tree_sorted_bottom")); TEST_EXPECT_SAVED_NEWICK(nLENGTH, gb_main, "tree_work", topo_bottom);
1114    TEST_EXPECT_NO_ERROR(sort_namedtree_by_other_tree(gb_main, "tree_work", "tree_test"));          TEST_EXPECT_SAVED_NEWICK(nLENGTH, gb_main, "tree_work", topo_test);
1115    TEST_EXPECT_NO_ERROR(sort_namedtree_by_other_tree(gb_main, "tree_work", "tree_nj_bs"));         TEST_EXPECT_SAVED_NEWICK(nLENGTH, gb_main, "tree_work", topo_vs_nj_bs);
1116
1117    // TEST_EXPECT_NO_ERROR(GB_save_as(gb_main, "TEST_trees_save.arb", "b")); // test-save db to examine tree (do not commit)
1118
1119    GB_close(gb_main);
1120}
1121TEST_PUBLISH(TEST_sort_tree_by_other_tree);
1122
1123void TEST_move_node_info() {
1124    GB_shell  shell;
1125    GBDATA   *gb_main = GB_open("TEST_trees.arb", "r");
1126
1127#define GROUP_TEST "(CloTyrob,(CloTyro2,(CloTyro3,CloTyro4)))"
1128
1129#define NAMED_GROUP_TEST       GROUP_TEST "'test'"
1130#define OVERWRITTEN_GROUP_TEST GROUP_TEST "'g2 [was: test]'"
1131
1132    const char *org_topo = "((CloInnoc," GROUP_TEST "),(CloBifer,((CloCarni,CurCitre),((CloPaste,(Zombie1,(CloButy2,CloButyr))),(CytAquat,(CelBiazo,(CorGluta,(CorAquat,Zombie2))))))));";
1133
1134    const char *unwanted_topo1 = "((CytAquat,(CelBiazo,(CorGluta,(CorAquat,Zombie2)))),((CloPaste,(Zombie1,(CloButy2,CloButyr))),((CloCarni,CurCitre),(CloBifer,(CloInnoc," NAMED_GROUP_TEST ")))));";
1135    const char *unwanted_topo2 = "((CloButy2,CloButyr),(Zombie1,(CloPaste,((((CloInnoc," OVERWRITTEN_GROUP_TEST "),CloBifer),(CloCarni,CurCitre)),(CytAquat,(CelBiazo,(CorGluta,(CorAquat,Zombie2)))))))'outer');";
1136
1137    const char *sorted_topo1 = "(((((CloInnoc," NAMED_GROUP_TEST "),CloBifer),(CloCarni,CurCitre)),(CloPaste,(Zombie1,(CloButy2,CloButyr)))),(CytAquat,(CelBiazo,(CorGluta,(CorAquat,Zombie2)))));";
1138    const char *sorted_topo2 = "(((((((CloInnoc," OVERWRITTEN_GROUP_TEST "),CloBifer),(CloCarni,CurCitre)),(CytAquat,(CelBiazo,(CorGluta,(CorAquat,Zombie2))))),CloPaste),Zombie1)'outer',(CloButy2,CloButyr));";
1139
1140    const char *compared_topo = "(((((((CloInnoc,(CloTyrob,(CloTyro2,(CloTyro3,CloTyro4)))),CloBifer),(CloCarni,CurCitre)'# 2')'# 2',(CytAquat,(CelBiazo,(CorGluta,(CorAquat,Zombie2)'# 1')'# 1')'# 1')'# 1')'# 1',CloPaste),Zombie1),(CloButy2,CloButyr));";
1141
1142    const char *LOG = "move_node_info.log";
1143
1144    // create copy of 'tree_removal'
1145    {
1146        GB_transaction  ta(gb_main);
1147        TreeNode       *tree = GBT_read_tree(gb_main, "tree_removal", new SimpleRoot);
1148
1149        TEST_EXPECT_NEWICK(nSIMPLE, tree, org_topo);
1150        TEST_EXPECT_NO_ERROR(GBT_write_tree(gb_main, "tree_removal_copy", tree));
1151        destroy(tree);
1152    }
1153
1154    // move node info
1155    {
1156        TEST_EXPECT_NO_ERROR(AWT_move_info(gb_main, "tree_test", "tree_removal", LOG, TREE_INFO_COPY, false));
1157
1158        TEST_EXPECT_SAVED_NEWICK__BROKEN(nSIMPLE, gb_main, "tree_removal", org_topo); // @@@ moving node info modifies topology (might be necessary to insert groups)
1159        TEST_EXPECT_SAVED_NEWICK(nGROUP, gb_main, "tree_removal", unwanted_topo1);
1160
1161        // @@@ when we have a function to set the root according to another tree (#449),
1162        // use that function here. sorting tree after that, should again result in 'org_topo'!
1163
1164        TEST_EXPECT_NO_ERROR(sort_namedtree_by_other_tree(gb_main, "tree_removal", "tree_removal_copy"));
1165        TEST_EXPECT_SAVED_NEWICK(nGROUP, gb_main, "tree_removal", sorted_topo1);
1166    }
1167
1168    // add node info
1169    {
1170        TEST_EXPECT_NO_ERROR(AWT_move_info(gb_main, "tree_tree2", "tree_removal", LOG, TREE_INFO_ADD, false));
1171
1172        TEST_EXPECT_SAVED_NEWICK__BROKEN(nSIMPLE, gb_main, "tree_removal", org_topo); // @@@ moving node info modifies topology (might be necessary to insert groups)
1173        TEST_EXPECT_SAVED_NEWICK(nGROUP, gb_main, "tree_removal", unwanted_topo2);
1174
1175        // @@@ when we have a function to set the root according to another tree (#449),
1176        // use that function here. sorting tree after that, should again result in 'org_topo'!
1177
1178        TEST_EXPECT_NO_ERROR(sort_namedtree_by_other_tree(gb_main, "tree_removal", "tree_removal_copy"));
1179        TEST_EXPECT_SAVED_NEWICK(nGROUP, gb_main, "tree_removal", sorted_topo2);
1180    }
1181
1182    // compare node info
1183    {
1184        TEST_EXPECT_NO_ERROR(AWT_move_info(gb_main, "tree_test", "tree_removal", NULL, TREE_INFO_COMPARE, false));
1185        TEST_EXPECT_SAVED_NEWICK(nREMARK, gb_main, "tree_removal", compared_topo);
1186    }
1187
1188    GB_unlink(LOG);
1189    GB_close(gb_main);
1190}
1191
1192void TEST_edges() {
1193    GB_shell  shell;
1194    GBDATA   *gb_main = GB_open("TEST_trees.arb", "rw");
1195    TEST_REJECT_NULL(gb_main);
1196
1197    {
1198        GB_transaction  ta(gb_main);
1199        TreeNode       *tree = GBT_read_tree(gb_main, "tree_test", new SizeAwareRoot);
1200
1201        TreeNode *left  = tree->findLeafNamed("CloTyro3"); TEST_REJECT_NULL(left);
1202        TreeNode *node  = left->get_father();              TEST_REJECT_NULL(node);
1203        TreeNode *right = node->findLeafNamed("CloTyro4"); TEST_REJECT_NULL(right);
1204
1205        TEST_EXPECT(node == right->get_father());
1206        TEST_EXPECT(node->get_leftson()  == left);
1207        TEST_EXPECT(node->get_rightson() == right);
1208
1209        TreeNode *parent  = node->get_father();                TEST_REJECT_NULL(parent);
1210        TreeNode *brother = parent->findLeafNamed("CloTyro2"); TEST_REJECT_NULL(brother);
1211
1212        TEST_EXPECT(node->get_brother() == brother);
1213
1214        TreeNode *grandpa  = parent->get_father(); TEST_REJECT_NULL(grandpa);
1215
1216        // topology:
1217        //
1218        //            grandpa
1219        //              /
1220        //             /
1221        //            /
1222        //          parent
1223        //           /\              .
1224        //          /  \             .
1225        //         /    \            .
1226        //       node  brother
1227        //        /\                 .
1228        //       /  \                .
1229        //      /    \               .
1230        //    left right
1231
1232        // test next() and otherNext() for inner edge 'node->parent'
1233        {
1234            ARB_edge nodeUp = parentEdge(node);
1235
1236            TEST_EXPECT(node->is_leftson()); // if child is left son..
1237            TEST_EXPECT(nodeUp.next().dest()      == grandpa); // .. next() continues rootwards
1238            TEST_EXPECT(nodeUp.otherNext().dest() == brother);
1239
1240            ARB_edge brotherUp = parentEdge(brother);
1241
1242            TEST_EXPECT(brother->is_rightson());               // if child is right son..
1243            TEST_EXPECT(brotherUp.next().dest()      == node); // .. next() continues with other son
1244            TEST_EXPECT(brotherUp.otherNext().dest() == grandpa);
1245
1246            ARB_edge down = nodeUp.inverse();
1247
1248            TEST_EXPECT(down.next().dest()      == right); // next descends into right son
1249            TEST_EXPECT(down.otherNext().dest() == left);
1250
1251            ARB_edge toLeaf(node, left);
1252            TEST_EXPECT(toLeaf.at_leaf());
1253
1254            // both iterators should turn around at leaf:
1255            TEST_EXPECT(toLeaf.next().dest()      == node);
1256            TEST_EXPECT(toLeaf.otherNext().dest() == node);
1257
1258            // test adjacent_distance
1259            const double EPSILON = 0.000001;
1260
1261            const double NLEN = 0.025806;
1262            const double BLEN = 0.017316;
1263            const double PLEN = 0.017167;
1264            const double LLEN = 1.045690;
1265            const double RLEN = 0.060606;
1266
1267            TEST_EXPECT_SIMILAR(node->get_branchlength(),             NLEN, EPSILON);
1268            TEST_EXPECT_SIMILAR(nodeUp.length(),                      NLEN, EPSILON);
1269            TEST_EXPECT_SIMILAR(down.length(),                        NLEN, EPSILON);
1270            TEST_EXPECT_SIMILAR(nodeUp.length_or_adjacent_distance(), NLEN, EPSILON);
1271            TEST_EXPECT_SIMILAR(down.length_or_adjacent_distance(),   NLEN, EPSILON);
1272
1273            TEST_EXPECT_SIMILAR(brother->get_branchlength(), BLEN,      EPSILON);
1274            TEST_EXPECT_SIMILAR(parent ->get_branchlength(), PLEN,      EPSILON);
1275            TEST_EXPECT_SIMILAR(nodeUp.adjacent_distance(),  BLEN+PLEN, EPSILON);
1276
1277            TEST_EXPECT_SIMILAR(left ->get_branchlength(), LLEN,      EPSILON);
1278            TEST_EXPECT_SIMILAR(right->get_branchlength(), RLEN,      EPSILON);
1279            TEST_EXPECT_SIMILAR(down.adjacent_distance(),  LLEN+RLEN, EPSILON);
1280
1281            // modify lengths
1282            const double MOD_NLEN = 0.123456;
1283            const double MOD_LLEN = 0.246802;
1284
1285            toLeaf.set_length(MOD_LLEN);
1286            nodeUp.set_length(MOD_NLEN);
1287
1288            TEST_EXPECT_SIMILAR(toLeaf.length(), MOD_LLEN, EPSILON);
1289            TEST_EXPECT_SIMILAR(nodeUp.length(), MOD_NLEN, EPSILON);
1290            TEST_EXPECT_SIMILAR(down.length(),   MOD_NLEN, EPSILON);
1291        }
1292
1293        destroy(tree);
1294    }
1295
1296    GB_close(gb_main);
1297}
1298
1299void TEST_toggle_bootstraps100() {
1300    GB_shell  shell;
1301    GBDATA   *gb_main = GB_open("TEST_trees.arb", "rw");
1302    TEST_REJECT_NULL(gb_main);
1303
1304    {
1305        GB_transaction  ta(gb_main);
1306        TreeNode       *tree = GBT_read_tree(gb_main, "tree_test", new SizeAwareRoot);
1307        TEST_REJECT_NULL(tree);
1308
1309        const char *topo_org   = "(((((((CloTyro3,CloTyro4)'40%',CloTyro2)'0%',CloTyrob)'97%',CloInnoc)'0%',CloBifer)'53%',(((CloButy2,CloButyr)'100%',CloCarni)'33%',CloPaste)'97%')'100%',((((CorAquat,CurCitre)'100%',CorGluta)'17%',CelBiazo)'40%',CytAquat)'100%');";
1310        const char *topo_no100 = "(((((((CloTyro3,CloTyro4)'40%',CloTyro2)'0%',CloTyrob)'97%',CloInnoc)'0%',CloBifer)'53%',(((CloButy2,CloButyr)"    ",CloCarni)'33%',CloPaste)'97%')"    ",((((CorAquat,CurCitre)"    ",CorGluta)'17%',CelBiazo)'40%',CytAquat)"    ");";
1311        const char *topo_rem   = "(((((((CloTyro3,CloTyro4),CloTyro2),CloTyrob),CloInnoc),CloBifer),(((CloButy2,CloButyr),CloCarni),CloPaste)),((((CorAquat,CurCitre),CorGluta),CelBiazo),CytAquat));";
1312
1313        TEST_EXPECT_NEWICK(nREMARK, tree, topo_org);
1314
1315        tree->toggle_bootstrap100();
1316        TEST_EXPECT_NEWICK(nREMARK, tree, topo_no100);
1317
1318        tree->toggle_bootstrap100();
1319        TEST_EXPECT_NEWICK(nREMARK, tree, topo_org);
1320
1321        tree->remove_bootstrap();
1322        TEST_EXPECT_NEWICK(nREMARK, tree, topo_rem);
1323
1324        destroy(tree);
1325    }
1326
1327    GB_close(gb_main);
1328}
1329
1330void TEST_multifurcate_tree() {
1331    GB_shell  shell;
1332    GBDATA   *gb_main = GB_open("TEST_trees.arb", "rw");
1333    TEST_REJECT_NULL(gb_main);
1334
1335    const char *topo_test               = "(((((((CloTyro3:1.046,CloTyro4:0.061)'40%':0.026,CloTyro2:0.017)'0%':0.017,CloTyrob:0.009)'97%:test':0.274,CloInnoc:0.371)'0%':0.057,CloBifer:0.388)'53%':0.124,(((CloButy2:0.009,CloButyr:0.000)'100%':0.564,CloCarni:0.120)'33%':0.010,CloPaste:0.179)'97%':0.131)'100%':0.081,((((CorAquat:0.084,CurCitre:0.058)'100%':0.103,CorGluta:0.522)'17%':0.053,CelBiazo:0.059)'40%':0.207,CytAquat:0.711)'100%':0.081);";
1336    // changes                          = "                                                                                                    +0.307         -0.371     +0.064 "
1337    const char *topo_single             = "(((((((CloTyro3:1.046,CloTyro4:0.061)'40%':0.026,CloTyro2:0.017)'0%':0.017,CloTyrob:0.009)'97%:test':0.581,CloInnoc:0.000)'0%':0.121,CloBifer:0.388)'53%':0.124,(((CloButy2:0.009,CloButyr:0.000)'100%':0.564,CloCarni:0.120)'33%':0.010,CloPaste:0.179)'97%':0.131)'100%':0.081,((((CorAquat:0.084,CurCitre:0.058)'100%':0.103,CorGluta:0.522)'17%':0.053,CelBiazo:0.059)'40%':0.207,CytAquat:0.711)'100%':0.081);";
1338    const char *topo_bs_less_101_005    = "(((((((CloTyro3:1.098,CloTyro4:0.064)"   ":0.000,CloTyro2:0.000)"  ":0.000,CloTyrob:0.000)'97%:test':0.287,CloInnoc:0.371)'0%':0.057,CloBifer:0.388)'53%':0.124,(((CloButy2:0.000,CloButyr:0.000)'100%':0.578,CloCarni:0.121)"   ":0.000,CloPaste:0.181)'97%':0.132)'100%':0.081,((((CorAquat:0.084,CurCitre:0.058)'100%':0.103,CorGluta:0.522)'17%':0.053,CelBiazo:0.059)'40%':0.207,CytAquat:0.711)'100%':0.081);";
1339    const char *topo_bs_less_101_005_NT = "(((((((CloTyro3:1.078,CloTyro4:0.062)"   ":0.000,CloTyro2:0.018)"  ":0.000,CloTyrob:0.009)'97%:test':0.282,CloInnoc:0.371)'0%':0.057,CloBifer:0.388)'53%':0.124,(((CloButy2:0.009,CloButyr:0.000)'100%':0.570,CloCarni:0.121)"   ":0.000,CloPaste:0.181)'97%':0.132)'100%':0.081,((((CorAquat:0.084,CurCitre:0.058)'100%':0.103,CorGluta:0.522)'17%':0.053,CelBiazo:0.059)'40%':0.207,CytAquat:0.711)'100%':0.081);";
1340    const char *topo_bs_less_30_005     = "(((((((CloTyro3:1.046,CloTyro4:0.061)'40%':0.027,CloTyro2:0.018)"  ":0.000,CloTyrob:0.009)'97%:test':0.288,CloInnoc:0.371)'0%':0.057,CloBifer:0.388)'53%':0.124,(((CloButy2:0.009,CloButyr:0.000)'100%':0.564,CloCarni:0.120)'33%':0.010,CloPaste:0.179)'97%':0.131)'100%':0.081,((((CorAquat:0.084,CurCitre:0.058)'100%':0.103,CorGluta:0.522)'17%':0.053,CelBiazo:0.059)'40%':0.207,CytAquat:0.711)'100%':0.081);";
1341    const char *topo_bs_less_30         = "(((((((CloTyro3:1.046,CloTyro4:0.061)'40%':0.027,CloTyro2:0.018)"  ":0.000,CloTyrob:0.009)'97%:test':0.302,CloInnoc:0.390)"  ":0.000,CloBifer:0.407)'53%':0.131,(((CloButy2:0.009,CloButyr:0.000)'100%':0.564,CloCarni:0.120)'33%':0.010,CloPaste:0.179)'97%':0.131)'100%':0.081,((((CorAquat:0.084,CurCitre:0.058)'100%':0.109,CorGluta:0.554)"   ":0.000,CelBiazo:0.062)'40%':0.220,CytAquat:0.711)'100%':0.081);";
1342    const char *topo_all                = "(((((((CloTyro3:0.000,CloTyro4:0.000)"   ":0.000,CloTyro2:0.000)"  ":0.000,CloTyrob:0.000)'"  "test':0.000,CloInnoc:0.000)"  ":0.000,CloBifer:0.000)"   ":0.000,(((CloButy2:0.000,CloButyr:0.000)"    ":0.000,CloCarni:0.000)"   ":0.000,CloPaste:0.000)"   ":0.000)"    ":0.000,((((CorAquat:0.000,CurCitre:0.000)"    ":0.000,CorGluta:0.000)"   ":0.000,CelBiazo:0.000)"   ":0.000,CytAquat:0.000)"    ":0.000);";
1343
1344    const double STABLE_LENGTH = 5.362750;
1345    const double EPSILON       = 0.000001;
1346
1347    for (int test = 1; test<=6; ++test) {
1348        GB_transaction  ta(gb_main);
1349        TreeNode       *tree = GBT_read_tree(gb_main, "tree_test", new SizeAwareRoot);
1350
1351        TEST_REJECT_NULL(tree);
1352        if (test == 1) {
1353            TEST_EXPECT_NEWICK(nALL, tree, topo_test);
1354            TEST_EXPECT_SIMILAR(tree->sum_child_lengths(), STABLE_LENGTH, EPSILON);
1355        }
1356
1357        switch (test) {
1358            case 1:
1359                tree->multifurcate_whole_tree(TreeNode::multifurc_limits(101, 0.05, true));
1360                TEST_EXPECT_NEWICK(nALL, tree, topo_bs_less_101_005);
1361                TEST_EXPECT_SIMILAR(tree->sum_child_lengths(), STABLE_LENGTH, EPSILON);
1362                break;
1363            case 6:
1364                tree->multifurcate_whole_tree(TreeNode::multifurc_limits(101, 0.05, false));
1365                TEST_EXPECT_NEWICK(nALL, tree, topo_bs_less_101_005_NT);
1366                TEST_EXPECT_SIMILAR(tree->sum_child_lengths(), STABLE_LENGTH, EPSILON);
1367                break;
1368            case 2:
1369                tree->multifurcate_whole_tree(TreeNode::multifurc_limits(30, 0.05, true));
1370                TEST_EXPECT_NEWICK(nALL, tree, topo_bs_less_30_005);
1371                TEST_EXPECT_SIMILAR(tree->sum_child_lengths(), STABLE_LENGTH, EPSILON);
1372                break;
1373            case 3:
1374                tree->multifurcate_whole_tree(TreeNode::multifurc_limits(30, 1000, true));
1375                TEST_EXPECT_NEWICK(nALL, tree, topo_bs_less_30);
1376                TEST_EXPECT_SIMILAR(tree->sum_child_lengths(), STABLE_LENGTH, EPSILON);
1377                break;
1378            case 4:
1379                tree->multifurcate_whole_tree(TreeNode::multifurc_limits(101, 1000, true)); // multifurcate all
1380                TEST_EXPECT_NEWICK(nALL, tree, topo_all);
1381                TEST_EXPECT_SIMILAR(tree->sum_child_lengths(), 0.0, EPSILON);
1382                break;
1383            case 5: {
1384                TreeNode *CloInnoc = tree->findLeafNamed("CloInnoc");
1385                TEST_REJECT_NULL(CloInnoc);
1386
1387                parentEdge(CloInnoc).multifurcate();
1388                TEST_EXPECT_NEWICK(nALL, tree, topo_single);
1389
1390                TEST_EXPECT_SIMILAR(tree->sum_child_lengths(), STABLE_LENGTH, EPSILON);
1391                break;
1392            }
1393            default:
1394                nt_assert(0);
1395                break;
1396        }
1397
1398        destroy(tree);
1399    }
1400
1401    GB_close(gb_main);
1402}
1403
1404#endif // UNIT_TESTS
1405
1406// --------------------------------------------------------------------------------
1407
Note: See TracBrowser for help on using the repository browser.