source: tags/old_import_filter/SL/AP_TREE/AP_Tree.hxx

Last change on this file was 9533, checked in by westram, 11 years ago
  • use explicit override
    • rest
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.5 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : AP_Tree.hxx                                       //
4//   Purpose   :                                                   //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#ifndef AP_TREE_HXX
12#define AP_TREE_HXX
13
14#define AP_F_LOADED    ((AW_active)1)
15#define AP_F_NLOADED   ((AW_active)2)
16#define AP_F_SEQUENCES ((AW_active)4)
17#define AP_F_MATRIX    ((AW_active)8)
18#define AP_F_TREE      ((AW_active)16)
19#define AP_F_ALL       ((AW_active)-1)
20
21#define GROUPED_SUM 2   // min. no of species in a group which should be drawn as box
22
23#ifndef ARB_TREE_HXX
24#include <ARB_Tree.hxx>
25#endif
26#ifndef AP_SEQUENCE_HXX
27#include <AP_sequence.hxx>
28#endif
29#ifndef AW_COLOR_GROUPS_HXX
30#include <aw_color_groups.hxx>
31#endif
32
33enum {
34    AWT_GC_CURSOR=0,
35    AWT_GC_BRANCH_REMARK,
36    AWT_GC_BOOTSTRAP,
37    AWT_GC_BOOTSTRAP_LIMITED,
38    AWT_GC_GROUPS,
39    AWT_GC_SELECTED,        // == zero mismatches
40    AWT_GC_UNDIFF,
41    AWT_GC_NSELECTED,       // no hit
42    AWT_GC_SOME_MISMATCHES,
43
44    // for multiprobecoloring
45
46    AWT_GC_BLACK,   AWT_GC_YELLOW,
47    AWT_GC_RED,     AWT_GC_MAGENTA,
48    AWT_GC_GREEN,   AWT_GC_CYAN,
49    AWT_GC_BLUE,    AWT_GC_WHITE,
50
51    AWT_GC_FIRST_COLOR_GROUP,
52    AWT_GC_MAX = AWT_GC_FIRST_COLOR_GROUP+AW_COLOR_GROUPS
53};
54
55enum AP_STACK_MODE {
56    NOTHING   = 0,                                                      // nothing to buffer in AP_tree node
57    STRUCTURE = 1,                                                      // only structure
58    SEQUENCE  = 2,                                                      // only sequence
59    BOTH      = 3,                                                      // sequence & treestructure is buffered
60    ROOT      = 7                                                       // old root is buffered
61};
62
63enum AP_UPDATE_FLAGS {
64    AP_UPDATE_OK       = 0,
65    AP_UPDATE_RELINKED = -1,
66    AP_UPDATE_RELOADED = 1,
67    AP_UPDATE_ERROR    = 2
68};
69
70enum AP_TREE_SIDE {  // flags zum kennzeichnen von knoten
71    AP_LEFT,
72    AP_RIGHT,
73    AP_FATHER,
74    AP_LEFTSON,
75    AP_RIGHTSON,
76    AP_NONE
77};
78
79enum AWT_REMOVE_TYPE { // bit flags
80    AWT_REMOVE_MARKED        = GBT_REMOVE_MARKED,
81    AWT_REMOVE_NOT_MARKED    = GBT_REMOVE_NOT_MARKED,
82    AWT_REMOVE_DELETED       = GBT_REMOVE_DELETED,
83    AWT_REMOVE_NO_SEQUENCE   = 8,
84    AWT_REMOVE_BUT_DONT_FREE = 16
85
86    // please keep AWT_REMOVE_TYPE in sync with GBT_TREE_REMOVE_TYPE
87    // see ../../ARBDB/arbdbt.h@sync_GBT_TREE_REMOVE_TYPE_AWT_REMOVE_TYPE
88};
89
90class AP_rates : virtual Noncopyable {
91public:
92    AP_FLOAT  *rates;
93    long       rate_len;
94    AP_filter *filter;
95    long       update;
96
97    AP_rates();
98    char *init(AP_filter *fil);
99    char *init(AP_FLOAT * ra, AP_filter *fil);
100    ~AP_rates();
101    void print();
102};
103
104// ---------------------
105//      AP_tree_root
106
107class AP_tree;
108
109typedef void (*AP_rootChangedCb)(void *cd, AP_tree *old, AP_tree *newroot);
110typedef void (*AP_nodeDelCb)(void *cd, AP_tree *del);
111
112class AP_tree_root : public ARB_tree_root { // derived from a Noncopyable
113    AP_rootChangedCb  root_changed_cb;
114    void             *root_changed_cd;
115    AP_nodeDelCb      node_deleted_cb;
116    void             *node_deleted_cd;
117
118    GBDATA *gb_species_data;                        // @@@ needed ?
119
120public:
121    GBDATA   *gb_tree_gone;                         // if all leafs have been removed by tree operations, remember 'ARB_tree_root::gb_tree' here (see change_root)
122    GBDATA   *gb_table_data;
123    long      tree_timer;
124    long      species_timer;
125    long      table_timer;
126    AP_rates *rates;
127
128    AP_tree_root(AliView *aliView, const AP_tree& tree_proto, AP_sequence *seq_proto, bool add_delete_callbacks);
129    virtual ~AP_tree_root() OVERRIDE;
130    DEFINE_TREE_ROOT_ACCESSORS(AP_tree_root, AP_tree);
131
132    // ARB_tree_root interface
133
134    virtual void change_root(ARB_tree *old, ARB_tree *newroot) OVERRIDE;
135
136    virtual GB_ERROR loadFromDB(const char *name) OVERRIDE;
137    virtual GB_ERROR saveToDB() OVERRIDE;
138
139    // AP_tree_root interface
140
141    void update_timers();                           // update the timer
142    bool is_tree_updated();
143    bool is_species_updated();
144
145    void inform_about_delete(AP_tree *old);
146
147    void set_root_changed_callback(AP_rootChangedCb cb, void *cd);
148    void set_node_deleted_callback(AP_nodeDelCb cb, void *cd);
149
150    long remove_leafs(int awt_remove_type);
151    ARB_edge find_innermost_edge();
152};
153
154
155struct AP_tree_members {
156public:
157    // elements from struct a_tree_node
158
159    // struct arb_flags
160    unsigned int grouped : 1;   // indicates a folded group
161    unsigned int hidden : 1;    // not shown because a father is a folded group
162    unsigned int has_marked_children : 1; // at least one child is marked
163    unsigned int callback_exists : 1;
164    unsigned int gc : 6;        // color
165
166    char left_linewidth;
167    char right_linewidth;
168    // struct arb_data
169    int  leaf_sum;  // number of leaf children of this node
170    int  view_sum;  // virtual size of node for display ( isgrouped?sqrt(leaf_sum):leaf_sum )
171
172    float   tree_depth; // max length of path; for drawing triangles */
173    float   min_tree_depth; // min length of path; for drawing triangle
174    float   spread;
175
176    float   left_angle;
177    float   right_angle;
178
179    void clear() {
180        grouped             = 0;
181        hidden              = 0;
182        has_marked_children = 0;
183        callback_exists     = 0;
184        gc                  = 0;
185        left_linewidth      = 0;
186        right_linewidth     = 0;
187        leaf_sum            = 0;
188        view_sum            = 0;
189        tree_depth          = 0;
190        min_tree_depth      = 0;
191        spread              = 0;
192        left_angle          = 0;
193        right_angle         = 0;
194    }
195};
196
197struct AP_branch_members {
198public:
199    unsigned int touched : 1;       // nni and kl
200
201    void clear() {
202        touched = 0;
203    }
204};
205
206
207class AP_tree : public ARB_tree {
208public: // @@@ fix public member
209    AP_tree_members   gr;
210    AP_branch_members br;
211    unsigned long     stack_level;
212
213    // ------------------
214    //      functions
215private:
216    void load_node_info();                          // load linewidth etc from DB
217
218public:
219
220    explicit AP_tree(AP_tree_root *tree_root);
221    virtual ~AP_tree() OVERRIDE; // leave this here to force creation of virtual table
222    DEFINE_TREE_ACCESSORS(AP_tree_root, AP_tree);
223
224    // ARB_tree interface
225    virtual AP_tree *dup() const OVERRIDE;
226    // ARB_tree interface (end)
227
228    int compute_tree(GBDATA *gb_main);
229
230    int arb_tree_set_leafsum_viewsum();             // count all visible leafs -> gr.viewsum + gr.leafsum
231    int arb_tree_leafsum2();                        // count all leafs
232
233    void calc_hidden_flag(int father_is_hidden);
234    int  calc_color();                        // start a transaction first
235    int  calc_color_probes(GB_HASH *hashptr); // new function for coloring the tree; ak
236
237    GBT_LEN arb_tree_min_deep();
238    GBT_LEN arb_tree_deep();
239
240    virtual void insert(AP_tree *new_brother);
241    virtual void remove();                          // remove this+father (but do not delete)
242    virtual void swap_assymetric(AP_TREE_SIDE mode); // 0 = AP_LEFT_son  1=AP_RIGHT_son
243    void         swap_sons();                       // exchange sons
244
245    GB_ERROR     cantMoveTo(AP_tree *new_brother);  // use this to detect impossible moves
246    virtual void moveTo(AP_tree *new_brother, AP_FLOAT rel_pos); // move to new brother
247
248    virtual void set_root();
249
250    void remove_bootstrap();                        // remove bootstrap values from subtree
251    void reset_branchlengths();                     // reset branchlengths of subtree to 0.1
252    void scale_branchlengths(double factor);
253    void bootstrap2branchlen();                     // copy bootstraps to branchlengths
254    void branchlen2bootstrap();                     // copy branchlengths to bootstraps
255
256    virtual void move_gbt_info(GBT_TREE *tree) OVERRIDE;
257
258    GB_ERROR tree_write_tree_rek(GBDATA *gb_tree);
259    GB_ERROR relink() __ATTR__USERESULT; // @@@ used ? if yes -> move to AP_tree_root or ARB_tree_root
260
261    virtual AP_UPDATE_FLAGS check_update();
262
263    void update();
264
265    int get_linewidth() const {
266        if (!father) return 0;
267        return is_leftson(father) ? gr.left_linewidth : gr.right_linewidth;
268    }
269    // cppcheck-suppress functionConst
270    void set_linewidth(int width) {
271        ap_assert(width >= 1 && width < 128);
272        if (father) {
273            char& lw = is_leftson(father) ? gr.left_linewidth : gr.right_linewidth;
274            lw       = width;
275        }
276    }
277
278private:
279    void buildLeafList_rek(AP_tree **list, long& num);
280    void buildNodeList_rek(AP_tree **list, long& num);
281    void buildBranchList_rek(AP_tree **list, long& num, bool create_terminal_branches, int deep);
282
283    const AP_tree *flag_branch() const { return get_father()->get_father() ? this : get_father()->get_leftson(); }
284
285public:
286    void buildLeafList(AP_tree **&list, long &num); // returns a list of leafs
287    void buildNodeList(AP_tree **&list, long &num); // returns a list of inner nodes (w/o root)
288    void buildBranchList(AP_tree **&list, long &num, bool create_terminal_branches, int deep);
289
290    AP_tree **getRandomNodes(int nnodes); // returns a list of random nodes (no leafs)
291
292    void replace_self(AP_tree *new_son);
293    void set_brother(AP_tree *new_son);
294
295    void clear_branch_flags();
296
297    void touch_branch() { const_cast<AP_tree*>(flag_branch())->br.touched = 1; }
298    int get_branch_flag() const { return flag_branch()->br.touched; }
299
300    GB_ERROR move_group_info(AP_tree *new_group) __ATTR__USERESULT;
301
302    void mark_duplicates();
303    const char *mark_long_branches(double min_rel_diff, double min_abs_diff);
304    const char *mark_deep_leafs(int min_depth, double min_rootdist);
305    const char *mark_degenerated_branches(double degeneration_factor);
306    const char *analyse_distances();
307
308    void justify_branch_lenghs(GBDATA *gb_main);
309    void relink_tree(GBDATA *gb_main, void (*relinker)(GBDATA *&ref_gb_node, char *&ref_name, GB_HASH *organism_hash), GB_HASH *organism_hash);
310
311    void reset_spread();
312    void reset_rotation();
313    void reset_child_linewidths();
314
315    bool hasName(const char *Name) const {
316        return Name && name && Name[0] == name[0] && strcmp(Name, name) == 0;
317    }
318};
319
320#else
321#error AP_Tree.hxx included twice
322#endif // AP_TREE_HXX
Note: See TracBrowser for help on using the repository browser.