source: tags/arb-6.0-rc1/SL/TREEDISP/TreeCallbacks.cxx

Last change on this file was 11706, checked in by epruesse, 11 years ago

don't jump in tree if displayed_root is NULL (fixes #477)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.3 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : TreeCallbacks.cxx                                 //
4//   Purpose   :                                                   //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#include "TreeCallbacks.hxx"
12
13#include <aw_awars.hxx>
14#include <aw_advice.hxx>
15#include <aw_msg.hxx>
16#include <aw_root.hxx>
17#include <mode_text.h>
18
19#include <cctype>
20
21using namespace AW;
22
23// AISC_MKPT_PROMOTE:#ifndef TREEDISPLAY_HXX
24// AISC_MKPT_PROMOTE:#include <TreeDisplay.hxx>
25// AISC_MKPT_PROMOTE:#endif
26
27void nt_mode_event(UNFIXED, AWT_canvas *ntw, AWT_COMMAND_MODE mode) {
28    const char *text;
29
30    switch (mode) {
31        case AWT_MODE_ZOOM:  text = MODE_TEXT_STANDARD_ZOOMMODE(); break;
32        case AWT_MODE_EMPTY: text = MODE_TEXT_PLACEHOLDER();       break;
33
34        case AWT_MODE_SELECT: text = MODE_TEXT_1BUTTON("SELECT", "select species or open/close group");                  break;
35        case AWT_MODE_INFO:   text = MODE_TEXT_1BUTTON("INFO",   "click for info");                                      break;
36        case AWT_MODE_WWW:    text = MODE_TEXT_1BUTTON("WEB",    "Launch node dependent URL (see <Properties/WWW...>)"); break;
37
38        case AWT_MODE_SWAP:       text = MODE_TEXT_2BUTTONS("SWAP",         "swap child branches",        "flip whole subtree");          break;
39        case AWT_MODE_MARK:       text = MODE_TEXT_2BUTTONS("MARK",         "mark subtree",               "unmark subtree");              break;
40        case AWT_MODE_GROUP:      text = MODE_TEXT_2BUTTONS("GROUP",        "fold/unfold group",          "create/rename/destroy group"); break;
41        case AWT_MODE_NNI:        text = MODE_TEXT_2BUTTONS("OPTI(NNI)",    "subtree",                    "whole tree");                  break;
42        case AWT_MODE_KERNINGHAN: text = MODE_TEXT_2BUTTONS("OPTI(KL)",     "subtree",                    "whole tree");                  break;
43        case AWT_MODE_OPTIMIZE:   text = MODE_TEXT_2BUTTONS("OPTI(NNI&KL)", "subtree",                    "whole tree");                  break;
44        case AWT_MODE_SETROOT:    text = MODE_TEXT_2BUTTONS("REROOT",       "set root to clicked branch", "search optimal root");         break;
45
46        case AWT_MODE_ROTATE: text = MODE_TEXT_1BUTTON_KEYS("ROTATE", "drag branch to rotate",         KEYINFO_ABORT_AND_RESET); break;
47        case AWT_MODE_SPREAD: text = MODE_TEXT_1BUTTON_KEYS("SPREAD", "drag branch to spread subtree", KEYINFO_ABORT_AND_RESET); break;
48
49        case AWT_MODE_LENGTH:    text = MODE_TEXT_2BUTTONS_KEYS("LENGTH",       "drag branch/ruler", "use discrete lengths", KEYINFO_ABORT_AND_RESET); break;
50        case AWT_MODE_MULTIFURC: text = MODE_TEXT_2BUTTONS_KEYS("MULTIFURC",    "drag branch",       "use discrete lengths", KEYINFO_ABORT_AND_RESET); break;
51        case AWT_MODE_LINE:      text = MODE_TEXT_2BUTTONS_KEYS("LINE",         "drag branch/ruler", "whole subtree",        KEYINFO_ABORT_AND_RESET); break;
52        case AWT_MODE_MOVE:      text = MODE_TEXT_2BUTTONS_KEYS("MOVE",         "drag branch/ruler", "move groupinfo only",  KEYINFO_ABORT);           break;
53        case AWT_MODE_LZOOM:     text = MODE_TEXT_2BUTTONS_KEYS("LOGICAL ZOOM", "show only subtree", "go up one step",       KEYINFO_RESET);           break;
54
55        default: text = no_mode_text_defined(); break;
56    }
57
58    td_assert(strlen(text) < AWAR_FOOTER_MAX_LEN); // text too long!
59
60    ntw->awr->awar(AWAR_FOOTER)->write_string(text);
61    ntw->set_mode(mode);
62}
63
64// ---------------------------------------
65//      Basic mark/unmark callbacks :
66
67static void count_mark_all_cb(UNFIXED, AWT_canvas *ntw) {
68    GB_push_transaction(ntw->gb_main);
69
70    GBDATA *gb_species_data = GBT_get_species_data(ntw->gb_main);
71    long    count           = GB_number_of_marked_subentries(gb_species_data);
72
73    GB_pop_transaction(ntw->gb_main);
74
75    char buf[256];
76    switch (count) {
77        case 0: strcpy(buf, "There are NO marked species"); break;
78        case 1: strcpy(buf, "There is 1 marked species"); break;
79        default: sprintf(buf, "There are %li marked species", count); break;
80    }
81    strcat(buf, ". (The number of species is displayed in the top area as well)");
82    aw_message(buf);
83}
84
85static int species_has_alignment(GBDATA *gb_species, void *cd_use) {
86    return GBT_read_sequence(gb_species, (const char*)cd_use) != 0;
87}
88
89static int sequence_is_partial(GBDATA *gb_species, void *cd_partial) {
90    long wanted  = (long)cd_partial;
91    td_assert(wanted == 0 || wanted == 1);
92    int partial = GBT_is_partial(gb_species, 1-wanted, 0);
93
94    return partial == wanted;
95}
96
97#define MARK_MODE_LOWER_BITS (1|2)
98#define MARK_MODE_UPPER_BITS (4|8|16)
99
100void NT_mark_all_cb(UNFIXED, AWT_canvas *ntw, int mark_mode) {
101    // Bits 0 and 1 of mark_mode:
102    //
103    // mark_mode&3  == 0 -> unmark
104    // mark_mode&3  == 1 -> mark
105    // mark_mode&3  == 2 -> toggle mark
106    //
107    // Bits 2 .. 4 of mark_mode:
108    //
109    // mark_mode&12 == 4 -> affect only full sequences
110    // mark_mode&12 == 8 -> affect only partial sequences
111    // mark_mode&12 == 16 -> affect only species with data in current alignment
112    // else -> affect all sequences
113
114    GB_transaction ta(ntw->gb_main);
115
116    switch (mark_mode&MARK_MODE_UPPER_BITS) {
117        case 0:                 // all sequences
118            GBT_mark_all(ntw->gb_main, mark_mode&MARK_MODE_LOWER_BITS);
119            break;
120        case 4:                 // full sequences only
121            GBT_mark_all_that(ntw->gb_main, mark_mode&MARK_MODE_LOWER_BITS, sequence_is_partial, (void*)0);
122            break;
123        case 8:                 // partial sequences only
124            GBT_mark_all_that(ntw->gb_main, mark_mode&MARK_MODE_LOWER_BITS, sequence_is_partial, (void*)1);
125            break;
126        case 16: {               // species with data in alignment only
127            char *ali = GBT_get_default_alignment(ntw->gb_main);
128            if (ali) GBT_mark_all_that(ntw->gb_main, mark_mode&MARK_MODE_LOWER_BITS, species_has_alignment, (void*)ali);
129            free(ali);
130            break;
131        }
132        default:
133            td_assert(0); // illegal mode
134            break;
135    }
136
137    ntw->refresh();
138}
139
140static void mark_tree_cb(UNFIXED, AWT_canvas *ntw, int mark_mode) {
141    AWT_graphic_tree *gtree = AWT_TREE(ntw);
142    GB_transaction    ta(ntw->gb_main);
143
144    gtree->check_update(ntw->gb_main);
145    AP_tree *tree_root = gtree->get_root_node();
146
147    switch (mark_mode&MARK_MODE_UPPER_BITS) {
148        case 0:                 // all sequences
149            gtree->mark_species_in_tree(tree_root, mark_mode&MARK_MODE_LOWER_BITS);
150            break;
151        case 4:                 // full sequences only
152            gtree->mark_species_in_tree_that(tree_root, mark_mode&MARK_MODE_LOWER_BITS, sequence_is_partial, (void*)0);
153            break;
154        case 8:                 // partial sequences only
155            gtree->mark_species_in_tree_that(tree_root, mark_mode&MARK_MODE_LOWER_BITS, sequence_is_partial, (void*)1);
156            break;
157        case 16: {               // species with data in alignment only
158            char *ali = GBT_get_default_alignment(ntw->gb_main);
159            if (ali) gtree->mark_species_in_tree_that(tree_root, mark_mode&MARK_MODE_LOWER_BITS, species_has_alignment, (void*)ali);
160            free(ali);
161            break;
162        }
163        default:
164            td_assert(0); // illegal mode
165            break;
166    }
167    ntw->refresh();
168}
169
170struct mark_nontree_cb_data {
171    int      mark_mode_upper_bits;
172    char    *ali;               // current alignment (only if mark_mode_upper_bits == 16)
173    GB_HASH *hash;
174};
175
176static int are_not_in_tree(GBDATA *gb_species, void *cb_data) {
177    struct mark_nontree_cb_data *data    = (mark_nontree_cb_data*)cb_data;
178    const char                  *name    = GBT_read_name(gb_species);
179    bool                         mark_me = false;
180
181    if (GBS_read_hash(data->hash, name) == (long)gb_species) { // species is not in tree!
182        switch (data->mark_mode_upper_bits) {
183            case 0:             // all sequences
184                mark_me = true;
185                break;
186            case 4:             // full sequences only
187                mark_me = sequence_is_partial(gb_species, (void*)0);
188                break;
189            case 8:             // partial sequences only
190                mark_me = sequence_is_partial(gb_species, (void*)1);
191                break;
192            case 16:            // species with data in alignment only
193                mark_me = species_has_alignment(gb_species, data->ali);
194                break;
195            default:
196                td_assert(0); // illegal mode
197                break;
198        }
199    }
200
201    return mark_me;
202}
203
204static void mark_nontree_cb(UNFIXED, AWT_canvas *ntw, int mark_mode) {
205    AWT_graphic_tree            *gtree = AWT_TREE(ntw);
206    GB_transaction               ta(ntw->gb_main);
207    struct mark_nontree_cb_data  cd;
208
209    if ((mark_mode&MARK_MODE_LOWER_BITS) == 0) {   // unmark is much faster
210        cd.hash = GBT_create_marked_species_hash(ntw->gb_main); // because it only hashes marked species
211    }
212    else {
213        cd.hash = GBT_create_species_hash(ntw->gb_main); // for mark we have to hash ALL species
214    }
215
216    NT_remove_species_in_tree_from_hash(gtree->get_root_node(), cd.hash);
217
218    cd.mark_mode_upper_bits = mark_mode&MARK_MODE_UPPER_BITS;
219    cd.ali                  = cd.mark_mode_upper_bits == 16 ? GBT_get_default_alignment(ntw->gb_main) : 0;
220
221    GBT_mark_all_that(ntw->gb_main, mark_mode&MARK_MODE_LOWER_BITS, are_not_in_tree, (void*)&cd);
222
223    free(cd.ali);
224
225    ntw->refresh();
226}
227
228static char *create_mark_menu_entry(const char *attrib, const char *entry_template) {
229    char *entry = 0;
230    if (attrib) {
231        bool append = attrib[0] == '-'; // if attrib starts with '-' then append (otherwise prepend)
232        if (append) ++attrib; // skip '-'
233
234        if (append) {
235            char *spaced_attrib = GBS_global_string_copy(" %s", attrib);
236            entry               = GBS_global_string_copy(entry_template, "", spaced_attrib);
237            free(spaced_attrib);
238        }
239        else {
240            char *spaced_attrib = GBS_global_string_copy("%s ", attrib);
241            entry               = GBS_global_string_copy(entry_template, spaced_attrib, "");
242
243            if (islower(entry[0])) entry[0] = toupper(entry[0]); // Caps prepended lowercase 'attrib'
244
245            free(spaced_attrib);
246        }
247    }
248    else {
249        entry = GBS_global_string_copy(entry_template, "", "");
250    }
251    return entry;
252}
253static char *create_mark_menu_id(const char *attrib, const char *id_suffix) {
254    char *id = 0;
255    if (attrib) {
256        id = GBS_global_string_copy("%s_%s", attrib[0] == '-' ? attrib+1 : attrib, id_suffix);
257    }
258    else {
259        id = strdup(id_suffix);
260    }
261    return id;
262}
263
264static void insert_mark_topic(AW_window_menu_modes *awm, AW_active mask, const char *attrib, const char *id_suffix, const char *entry_template,
265                              const char *hotkey, const char *helpfile, const WindowCallback& wcb)
266{
267    char *entry = create_mark_menu_entry(attrib, entry_template);
268    char *id    = create_mark_menu_id(attrib, id_suffix);
269
270    awm->insert_menu_topic(id, entry, hotkey, helpfile, mask, wcb);
271
272    free(id);
273    free(entry);
274}
275
276static void insert_mark_topics(AW_window_menu_modes *awm, AW_active mask, AWT_canvas *ntw, int affect, const char *attrib)
277{
278    td_assert(affect == (affect&MARK_MODE_UPPER_BITS)); // only bits 2 .. 4 are allowed
279
280    insert_mark_topic(awm, mask, attrib, "mark_all",    "Mark all %sSpecies%s",            "M", "sp_mrk_all.hlp",    makeWindowCallback(NT_mark_all_cb, ntw, 1+affect));
281    insert_mark_topic(awm, mask, attrib, "unmark_all",  "Unmark all %sSpecies%s",          "U", "sp_umrk_all.hlp",   makeWindowCallback(NT_mark_all_cb, ntw, 0+affect));
282    insert_mark_topic(awm, mask, attrib, "swap_marked", "Invert marks of all %sSpecies%s", "I", "sp_invert_mrk.hlp", makeWindowCallback(NT_mark_all_cb, ntw, 2+affect));
283    awm->sep______________();
284
285    char *label = create_mark_menu_entry(attrib, "%sSpecies%s in Tree");
286
287    awm->insert_sub_menu(label, "T");
288    insert_mark_topic(awm, mask, attrib, "mark_tree",        "Mark %sSpecies%s in Tree",            "M", "sp_mrk_tree.hlp",   makeWindowCallback(mark_tree_cb, ntw, 1+affect));
289    insert_mark_topic(awm, mask, attrib, "unmark_tree",      "Unmark %sSpecies%s in Tree",          "U", "sp_umrk_tree.hlp",  makeWindowCallback(mark_tree_cb, ntw, 0+affect));
290    insert_mark_topic(awm, mask, attrib, "swap_marked_tree", "Invert marks of %sSpecies%s in Tree", "I", "sp_invert_mrk.hlp", makeWindowCallback(mark_tree_cb, ntw, 2+affect));
291    awm->close_sub_menu();
292
293    freeset(label, create_mark_menu_entry(attrib, "%sSpecies%s NOT in Tree"));
294
295    awm->insert_sub_menu(label, "N");
296    insert_mark_topic(awm, mask, attrib, "mark_nontree",        "Mark %sSpecies%s NOT in Tree",            "M", "sp_mrk_tree.hlp",   makeWindowCallback(mark_nontree_cb, ntw, 1+affect));
297    insert_mark_topic(awm, mask, attrib, "unmark_nontree",      "Unmark %sSpecies%s NOT in Tree",          "U", "sp_umrk_tree.hlp",  makeWindowCallback(mark_nontree_cb, ntw, 0+affect));
298    insert_mark_topic(awm, mask, attrib, "swap_marked_nontree", "Invert marks of %sSpecies%s NOT in Tree", "I", "sp_invert_mrk.hlp", makeWindowCallback(mark_nontree_cb, ntw, 2+affect));
299    awm->close_sub_menu();
300
301    free(label);
302}
303
304void NT_insert_mark_submenus(AW_window_menu_modes *awm, AWT_canvas *ntw, int insert_as_submenu) {
305    if (insert_as_submenu) {
306        awm->insert_sub_menu("Mark species", "M");
307    }
308
309    {
310        awm->insert_menu_topic("count_marked", "Count Marked Species", "C", "sp_count_mrk.hlp", AWM_ALL, makeWindowCallback(count_mark_all_cb, ntw));
311        awm->sep______________();
312        insert_mark_topics(awm, AWM_ALL, ntw, 0, 0);
313        awm->sep______________();
314
315        awm->insert_sub_menu("Complete sequences", "o");
316        insert_mark_topics(awm, AWM_EXP, ntw, 4, "complete");
317        awm->close_sub_menu();
318
319        awm->insert_sub_menu("Partial sequences", "P");
320        insert_mark_topics(awm, AWM_EXP, ntw, 8, "partial");
321        awm->close_sub_menu();
322
323        awm->insert_sub_menu("Current Alignment", "A");
324        insert_mark_topics(awm, AWM_EXP, ntw, 16, "-with data");
325        awm->close_sub_menu();
326    }
327
328    if (insert_as_submenu) {
329        awm->close_sub_menu();
330    }
331}
332
333static void save_changed_tree(AWT_canvas *ntw) {
334    GB_ERROR error = AWT_TREE(ntw)->save(ntw->gb_main, 0, 0, 0);
335    if (error) aw_message(error);
336    ntw->zoom_reset_and_refresh();
337}
338
339// ----------------------------------------
340//      Automated collapse/expand tree
341
342static void group_and_save_tree(AWT_canvas *ntw, int mode, int color_group) {
343    GB_transaction ta(ntw->gb_main);
344
345    AWT_TREE(ntw)->check_update(ntw->gb_main);
346    AWT_TREE(ntw)->group_tree(AWT_TREE(ntw)->get_root_node(), mode, color_group);
347    save_changed_tree(ntw);
348}
349
350void NT_group_tree_cb      (UNFIXED, AWT_canvas *ntw) { group_and_save_tree(ntw, 0, 0); }
351void NT_group_not_marked_cb(UNFIXED, AWT_canvas *ntw) { group_and_save_tree(ntw, 1, 0); }
352void NT_group_terminal_cb  (UNFIXED, AWT_canvas *ntw) { group_and_save_tree(ntw, 2, 0); }
353void NT_ungroup_all_cb     (UNFIXED, AWT_canvas *ntw) { group_and_save_tree(ntw, 4, 0); }
354
355static void NT_group_not_color_cb(UNFIXED, AWT_canvas *ntw, int colornum) { group_and_save_tree(ntw, 8, colornum); }
356
357void NT_insert_color_collapse_submenu(AW_window_menu_modes *awm, AWT_canvas *ntree_canvas) {
358#define MAXLABEL 30
359#define MAXENTRY (AW_COLOR_GROUP_NAME_LEN+10)
360
361    td_assert(ntree_canvas != 0);
362
363    awm->insert_sub_menu("Group all except Color ...", "C");
364
365    char        label_buf[MAXLABEL+1];
366    char        entry_buf[MAXENTRY+1];
367    char hotkey[]       = "x";
368    const char *hotkeys = "N1234567890AB";
369
370    for (int i = 0; i <= AW_COLOR_GROUPS; ++i) {
371        sprintf(label_buf, "tree_group_not_color_%i", i);
372
373        hotkey[0]                       = hotkeys[i];
374        if (hotkey[0] == ' ') hotkey[0] = 0;
375
376        if (i) {
377            char *color_group_name = AW_get_color_group_name(awm->get_root(), i);
378            sprintf(entry_buf, "%s group '%s'", hotkey, color_group_name);
379            free(color_group_name);
380        }
381        else {
382            strcpy(entry_buf, "No color group");
383        }
384
385        awm->insert_menu_topic(awm->local_id(label_buf), entry_buf, hotkey, "tgroupcolor.hlp", AWM_ALL, makeWindowCallback(NT_group_not_color_cb, ntree_canvas, i));
386    }
387
388    awm->close_sub_menu();
389
390#undef MAXLABEL
391#undef MAXENTRY
392}
393
394// ------------------------
395//      tree sorting :
396
397GB_ERROR NT_with_displayed_tree_do(AWT_canvas *ntw, bool (*displayed_tree_cb)(RootedTree *tree, GB_ERROR& error)) {
398    // 'displayed_tree_cb' has to return true if tree was changed and needs to be saved
399
400    GB_transaction ta(ntw->gb_main);
401    AWT_TREE(ntw)->check_update(ntw->gb_main);
402
403    GB_ERROR error = NULL;
404    if (displayed_tree_cb(AWT_TREE(ntw)->get_root_node(), error)) {
405        save_changed_tree(ntw);
406    }
407    return error;
408}
409
410void NT_resort_tree_cb(UNFIXED, AWT_canvas *ntw, TreeOrder order) {
411    GB_transaction ta(ntw->gb_main);
412    AWT_TREE(ntw)->check_update(ntw->gb_main);
413    AWT_TREE(ntw)->reorder_tree(order);
414    save_changed_tree(ntw);
415}
416
417void NT_reset_lzoom_cb(UNFIXED, AWT_canvas *ntw) {
418    GB_transaction ta(ntw->gb_main);
419    AWT_TREE(ntw)->check_update(ntw->gb_main);
420    AWT_TREE(ntw)->displayed_root = AWT_TREE(ntw)->get_root_node();
421    ntw->zoom_reset_and_refresh();
422}
423
424void NT_reset_pzoom_cb(UNFIXED, AWT_canvas *ntw) {
425    GB_transaction ta(ntw->gb_main);
426    AWT_TREE(ntw)->check_update(ntw->gb_main);
427    ntw->zoom_reset_and_refresh();
428}
429
430void NT_set_tree_style(UNFIXED, AWT_canvas *ntw, AP_tree_display_type type) {
431    GB_transaction ta(ntw->gb_main);
432    AWT_TREE(ntw)->check_update(ntw->gb_main);
433    AWT_TREE(ntw)->set_tree_type(type, ntw);
434    ntw->zoom_reset_and_refresh();
435}
436
437void NT_remove_leafs(UNFIXED, AWT_canvas *ntw, AWT_RemoveType mode) {
438    GB_transaction ta(ntw->gb_main);
439
440    AWT_TREE(ntw)->check_update(ntw->gb_main);
441    AP_tree *tree_root = AWT_TREE(ntw)->get_root_node();
442    if (tree_root) {
443        AWT_TREE(ntw)->tree_static->remove_leafs(mode);
444
445        tree_root = AWT_TREE(ntw)->get_root_node(); // root might have changed -> get again
446        if (tree_root) tree_root->compute_tree();
447        save_changed_tree(ntw);
448    }
449    else {
450        aw_message("Got no tree");
451    }
452}
453
454void NT_remove_bootstrap(UNFIXED, AWT_canvas *ntw) { // delete all bootstrap values
455    GB_transaction ta(ntw->gb_main);
456
457    AWT_TREE(ntw)->check_update(ntw->gb_main);
458
459    AP_tree *tree_root = AWT_TREE(ntw)->get_root_node();
460    if (tree_root) {
461        tree_root->remove_bootstrap();
462        tree_root->compute_tree();
463        save_changed_tree(ntw);
464    }
465}
466void NT_toggle_bootstrap100(UNFIXED, AWT_canvas *ntw) { // toggle 100% bootstrap values
467    GB_transaction ta(ntw->gb_main);
468
469    AWT_TREE(ntw)->check_update(ntw->gb_main);
470
471    AP_tree *tree_root = AWT_TREE(ntw)->get_root_node();
472    if (tree_root) {
473        tree_root->toggle_bootstrap100();
474        tree_root->compute_tree();
475        save_changed_tree(ntw);
476    }
477}
478
479void NT_reset_branchlengths(UNFIXED, AWT_canvas *ntw) { // set all branchlengths to tree_defaults::LENGTH
480    GB_transaction ta(ntw->gb_main);
481    AWT_TREE(ntw)->check_update(ntw->gb_main);
482
483    AP_tree *tree_root = AWT_TREE(ntw)->get_root_node();
484    if (tree_root) {
485        tree_root->reset_branchlengths();
486        tree_root->compute_tree();
487        save_changed_tree(ntw);
488    }
489}
490
491void NT_multifurcate_tree(AWT_canvas *ntw, const RootedTree::multifurc_limits& below) {
492    GB_transaction ta(ntw->gb_main);
493    AWT_TREE(ntw)->check_update(ntw->gb_main);
494    AWT_TREE(ntw)->get_root_node()->multifurcate_whole_tree(below);
495    save_changed_tree(ntw);
496}
497
498void NT_move_boot_branch(UNFIXED, AWT_canvas *ntw, int direction) { // copy branchlengths to bootstraps (or vice versa)
499    GB_transaction ta(ntw->gb_main);
500
501    AWT_TREE(ntw)->check_update(ntw->gb_main);
502
503    AP_tree *tree_root = AWT_TREE(ntw)->get_root_node();
504    if (tree_root) {
505        if (direction == 0) tree_root->bootstrap2branchlen();
506        else                tree_root->branchlen2bootstrap();
507
508        tree_root->compute_tree();
509        save_changed_tree(ntw);
510
511        char *adviceText = GBS_global_string_copy("Please note, that you just overwrote your existing %s.",
512                                                  direction ? "bootstrap values" : "branchlengths");
513        AW_advice(adviceText, AW_ADVICE_TOGGLE_AND_HELP, 0, "tbl_boot2len.hlp");
514        free(adviceText);
515    }
516}
517
518void NT_scale_tree(UNFIXED, AWT_canvas *ntw) { // scale branchlengths
519    char *answer = aw_input("Enter scale factor", "Scale branchlengths by factor:", "100");
520    if (answer) {
521        double factor = atof(answer);
522        GB_transaction ta(ntw->gb_main);
523
524        AP_tree *tree_root = AWT_TREE(ntw)->get_root_node();
525        if (tree_root) {
526            tree_root->scale_branchlengths(factor);
527            tree_root->compute_tree();
528            save_changed_tree(ntw);
529        }
530        free(answer);
531    }
532}
533
534inline AP_tree *common_ancestor(AP_tree *t1, AP_tree *t2) {
535    return DOWNCAST(AP_tree*, t1->ancestor_common_with(t2));
536}
537
538static bool make_node_visible(AWT_canvas *ntw, AP_tree *node) {
539    bool changed = false;
540    while (node) {
541        if (node->gr.grouped) {
542            node->gr.grouped = 0;
543            changed          = true;
544        }
545        node = node->get_father();
546    }
547    if (changed) {
548        AWT_TREE(ntw)->get_root_node()->compute_tree();
549        GB_ERROR error = AWT_TREE(ntw)->save(ntw->gb_main, 0, 0, 0);
550        if (error) {
551            aw_message(error);
552            return false;
553        }
554        ntw->zoom_reset();
555    }
556    return true;
557}
558
559void NT_jump_cb(UNFIXED, AWT_canvas *ntw, AP_tree_jump_type jumpType) {
560    AW_window        *aww   = ntw->aww;
561    AWT_graphic_tree *gtree = AWT_TREE(ntw);
562   
563    if (!gtree) return;
564    if (!gtree->displayed_root) return;
565
566    GB_transaction ta(ntw->gb_main);
567    gtree->check_update(ntw->gb_main);
568
569    const char *name     = aww->get_root()->awar(AWAR_SPECIES_NAME)->read_char_pntr();
570    char       *msg      = NULL;
571    bool        verboose = jumpType & AP_JUMP_BE_VERBOOSE;
572
573    if (name[0]) {
574        AP_tree *found   = NULL;
575        bool     is_tree = sort_is_tree_style(gtree->tree_sort);
576
577        if (is_tree) {
578            found = gtree->displayed_root->findLeafNamed(name);
579            if (!found && gtree->is_logically_zoomed()) {
580                found = gtree->get_root_node()->findLeafNamed(name);
581                if (found) { // species is invisible because it is outside logically zoomed tree
582                    if (jumpType & AP_JUMP_UNFOLD_GROUPS) {
583                        gtree->displayed_root = common_ancestor(found, gtree->displayed_root);
584                        ntw->zoom_reset();
585                    }
586                    else {
587                        if (verboose) msg = GBS_global_string_copy("Species '%s' is outside logical zoomed subtree", name);
588
589                        found = NULL;
590                    }
591                }
592            }
593
594            if (found) {
595                if (jumpType&AP_JUMP_UNFOLD_GROUPS) {
596                    if (!make_node_visible(ntw, found)) found = NULL;
597                }
598                else if (found->is_inside_folded_group()) {
599                    found = NULL;
600                }
601            }
602        }
603
604        if (found || !is_tree) {
605            bool repeat   = gtree->tree_sort == AP_TREE_IRS;
606            bool do_jump  = true;
607            while (do_jump) {
608                do_jump = false;
609
610                AW_device_size *device = aww->get_size_device(AW_MIDDLE_AREA);
611                device->set_filter(AW_SIZE|AW_SIZE_UNSCALED);
612                device->reset();
613                ntw->init_device(device);
614                ntw->gfx->show(device);
615
616                const AW_screen_area& screen = device->get_area_size();
617
618                const Position& cursor = gtree->get_cursor();
619                if (are_distinct(Origin, cursor)) {
620                    Position S = device->transform(cursor);
621
622                    int scroll_x = 0;
623                    int scroll_y = 0;
624
625                    if (S.xpos()<0.0) scroll_x      = (int)(S.xpos() - screen.r * .1);
626                    if (S.xpos()>screen.r) scroll_x = (int)(S.xpos() - screen.r * .5);
627
628                    if (gtree->tree_sort == AP_TREE_IRS) {
629                        // always scroll IRS tree
630                        // position a bit below vertical center
631                        scroll_y = (int) (S.ypos() - screen.b * .6);
632                    }
633                    else if (S.ypos()<0.0 || S.ypos()>screen.b) {
634                        scroll_y = (int) (S.ypos() - screen.b * .5);
635                    }
636
637                    if (jumpType & AP_JUMP_CENTER_IF_VISIBLE) {
638                        // additional JUMP button clicks -> center vertically, then horizontally
639                        if (!scroll_x && !scroll_y) {
640                            scroll_y                = (int) (S.ypos() - screen.b * .5);
641                            if (!scroll_y) scroll_x = (int) (S.xpos() - screen.r * (is_tree ? .5 : .02));
642                        }
643                    }
644
645                    if (scroll_x || scroll_y) ntw->scroll(scroll_x, scroll_y);
646                    if (repeat) {
647                        // reposition jump in IRS tree (reduces jump failure rate)
648                        repeat  = false;
649                        do_jump = true;
650                    }
651                }
652                else {
653                    td_assert(!is_tree);
654                    if (verboose) msg = GBS_global_string_copy("Species '%s' is no member of this list", name);
655                }
656            }
657        }
658
659        if (!found && is_tree && verboose && !msg) {
660            msg = GBS_global_string_copy("Species '%s' is no member of this %s", name, gtree->tree_sort == AP_LIST_NDS ? "list" : "tree");
661        }
662    }
663    else if (verboose) {
664        msg = strdup("No species selected");
665    }
666
667    ntw->refresh(); // always do refresh to show change of selected species
668
669    if (msg) {
670        td_assert(verboose);
671        aw_message(msg);
672        free(msg);
673    }
674}
675
676void TREE_auto_jump_cb(UNFIXED, AWT_canvas *ntw) {   // jump only if auto jump is set
677    if (ntw->aww->get_root()->awar(AWAR_DTREE_AUTO_JUMP)->read_int()) {
678        NT_jump_cb(NULL, ntw, AP_JUMP_AUTO);
679    }
680}
681
682inline const char *plural(int val) {
683    return "s"+(val == 1);
684}
685
686void NT_reload_tree_event(AW_root *awr, AWT_canvas *ntw, bool expose) {
687    GB_push_transaction(ntw->gb_main);
688    char     *tree_name = awr->awar(ntw->user_awar)->read_string();
689    GB_ERROR  error     = ntw->gfx->load(ntw->gb_main, tree_name, 0, 0);
690    if (error) {
691        aw_message(error);
692    }
693    else {
694        int zombies, duplicates;
695        DOWNCAST(AWT_graphic_tree*, ntw->gfx)->get_zombies_and_duplicates(zombies, duplicates);
696
697        if (zombies || duplicates) {
698            const char *msg = 0;
699            if (duplicates) {
700                if (zombies) msg = GBS_global_string("%i zombie%s and %i duplicate%s", zombies, plural(zombies), duplicates, plural(duplicates));
701                else msg         = GBS_global_string("%i duplicate%s", duplicates, plural(duplicates));
702            }
703            else {
704                td_assert(zombies);
705                msg = GBS_global_string("%i zombie%s", zombies, plural(zombies));
706            }
707            aw_message(GBS_global_string("%s in '%s'", msg, tree_name));
708        }
709    }
710    free(tree_name);
711    if (expose) {
712        ntw->zoom_reset();
713        AWT_expose_cb(NULL, ntw);
714    }
715    GB_pop_transaction(ntw->gb_main);
716}
717
718void TREE_recompute_cb(UNFIXED, AWT_canvas *ntw) {
719    AWT_graphic_tree *gt = DOWNCAST(AWT_graphic_tree*, ntw->gfx);
720    gt->get_root_node()->compute_tree();
721    AWT_expose_cb(NULL, ntw);
722}
723
724void NT_reinit_treetype(UNFIXED, AWT_canvas *ntw) {
725    AWT_graphic_tree *gt = DOWNCAST(AWT_graphic_tree*, ntw->gfx);
726    gt->set_tree_type(gt->tree_sort, ntw);
727    AWT_resize_cb(NULL, ntw);
728}
729
730void NT_remove_species_in_tree_from_hash(AP_tree *tree, GB_HASH *hash) {
731    if (!tree) return;
732    if (tree->is_leaf && tree->name) {
733        GBS_write_hash(hash, tree->name, 0); // delete species in hash table
734    }
735    else {
736        NT_remove_species_in_tree_from_hash(tree->get_leftson(), hash);
737        NT_remove_species_in_tree_from_hash(tree->get_rightson(), hash);
738    }
739}
740
Note: See TracBrowser for help on using the repository browser.