source: tags/arb-6.0-rc1/SL/ARB_TREE/ARB_Tree.hxx

Last change on this file was 11888, checked in by westram, 10 years ago
  • highlight differences in production code triggered by build-flag 'UNIT_TESTS'
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : ARB_Tree.hxx                                      //
4//   Purpose   : Tree types with sequence knowledge                //
5//                                                                 //
6//   Coded by Ralf Westram (coder@reallysoft.de) in October 2009   //
7//   Institute of Microbiology (Technical University Munich)       //
8//   http://www.arb-home.de/                                       //
9//                                                                 //
10// =============================================================== //
11
12#ifndef ARB_TREE_HXX
13#define ARB_TREE_HXX
14
15#ifndef ALIVIEW_HXX
16#include <AliView.hxx>
17#endif
18#ifndef DOWNCAST_H
19#include <downcast.h>
20#endif
21#ifndef ROOTEDTREE_H
22#include <RootedTree.h>
23#endif
24
25#define at_assert(cond) arb_assert(cond)
26
27typedef void (*ARB_tree_node_del_cb)(GBDATA*, class ARB_seqtree*);
28
29class ARB_seqtree_root;
30class ARB_seqtree;
31class ARB_edge;
32class AP_weights;
33class AP_sequence;
34class AliView;
35
36class ARB_seqtree_root : public TreeRoot { // derived from Noncopyable
37    AliView     *ali;
38
39    AP_sequence *seqTemplate;
40
41    // following variables are set, if the tree has been loaded from DB
42    char   *tree_name;                              // name of the tree in DB
43    GBDATA *gb_tree;                                // tree container in DB
44
45    bool isLinkedToDB;
46    bool addDeleteCallbacks;
47
48protected:
49    void set_gb_tree(GBDATA *gbTree) {
50        at_assert(implicated(gb_tree, gb_tree == gbTree));
51        at_assert(tree_name);
52        gb_tree = gbTree;
53    }
54
55public:
56    ARB_seqtree_root(AliView *aliView, RootedTreeNodeFactory *nodeMaker_, AP_sequence *seqTempl, bool add_delete_callbacks);
57    ~ARB_seqtree_root() OVERRIDE;
58
59    DEFINE_TREE_ROOT_ACCESSORS(ARB_seqtree_root, ARB_seqtree);
60
61    const AliView *get_aliview() const { return ali; }
62
63    const AP_filter *get_filter() const { return ali->get_filter(); }
64    const AP_weights *get_weights() const { return ali->get_weights(); }
65
66    GBDATA *get_gb_main() const { return ali->get_gb_main(); }
67
68    GBDATA *get_gb_tree() const { return gb_tree; } // NULL if no tree loaded (or tree disappeared from DB)
69    void tree_deleted_cb(GBDATA *gb_tree_del);      // internal
70
71    const char *get_tree_name() const { return tree_name; } // NULL if no tree loaded (or tree disappeared from DB)
72
73    virtual GB_ERROR loadFromDB(const char *name) __ATTR__USERESULT;
74    virtual GB_ERROR saveToDB() __ATTR__USERESULT;
75
76    const AP_sequence *get_seqTemplate() const { return seqTemplate; }
77
78    GB_ERROR linkToDB(int *zombies, int *duplicates) __ATTR__USERESULT;
79    void unlinkFromDB(); // @@@ is (but should not be) unused
80};
81
82
83struct ARB_tree_info {
84    size_t leafs;
85    size_t innerNodes;
86    size_t groups;
87    size_t unlinked;                                // unlinked leafs (same as 'leafs' if tree is unlinked)
88    size_t marked;                                  // leafs linked to marked species
89    // size_t with_sequence; // @@@ add when AP_sequence is member of ARB_seqtree
90
91    ARB_tree_info();
92
93    size_t nodes()    const { return innerNodes+leafs; }
94    size_t linked()   const { return leafs-unlinked; }
95    size_t unmarked() const { return linked()-marked; }
96};
97
98
99class ARB_seqtree : public RootedTree { // derived from Noncopyable
100    friend GB_ERROR ARB_seqtree_root::loadFromDB(const char *name);
101    friend GB_ERROR ARB_seqtree_root::linkToDB(int *zombies, int *duplicates);
102    friend void     ARB_seqtree_root::unlinkFromDB();
103
104    AP_sequence   *seq;                          /* NULL if tree is unlinked
105                                                  * otherwise automatically valid for leafs with gb_node!
106                                                  * may be set manually for inner nodes
107                                                  */
108
109    // ------------------
110    //      functions
111
112    void unloadSequences();
113    void preloadLeafSequences();
114
115    GB_ERROR add_delete_cb_rec(ARB_tree_node_del_cb cb) __ATTR__USERESULT;
116    void remove_delete_cb_rec(ARB_tree_node_del_cb cb);
117
118protected:
119    AP_sequence *take_seq() { // afterwards not has no seq and caller is responsible for sequence
120        AP_sequence *result = seq;
121        seq = NULL;
122        return result;
123    }
124    void replace_seq(AP_sequence *sequence);
125
126public:
127    ARB_seqtree(ARB_seqtree_root *root)
128        : RootedTree(root),
129          seq(NULL)
130    {}
131    ~ARB_seqtree() OVERRIDE;
132
133    DEFINE_TREE_ACCESSORS(ARB_seqtree_root, ARB_seqtree);
134
135    void calcTreeInfo(ARB_tree_info& info);
136
137    // order in dendogram:
138    bool is_upper_son() const { return is_leftson(); }
139    bool is_lower_son() const { return is_rightson(); }
140
141    AP_sequence *get_seq() { return seq; }
142    const AP_sequence *get_seq() const { return seq; }
143    AP_sequence * set_seq(AP_sequence *sequence) {
144        at_assert(!seq); // already set
145#ifndef UNIT_TESTS // UT_DIFF
146        // unit tests are allowed to leave sequence undefined // @@@ better solution?
147        at_assert(sequence);
148#endif
149        seq = sequence;
150        return seq;
151    }
152
153    void mark_subtree();
154    bool contains_marked_species();
155
156};
157
158struct ARB_tree_predicate {
159    virtual ~ARB_tree_predicate() {}
160    virtual bool selects(const ARB_seqtree& tree) const = 0;
161};
162
163// ------------------------
164//      ARB_countedTree
165//      tree that knows its size
166
167struct ARB_countedTree : public ARB_seqtree {
168    explicit ARB_countedTree(ARB_seqtree_root *tree_root_)
169        : ARB_seqtree(tree_root_)
170    {}
171    DEFINE_TREE_ACCESSORS(ARB_seqtree_root, ARB_countedTree);
172
173    // @@@ TODO:
174    // - add debug code (checking init_tree() is called exactly once)
175    // - init_tree() might be called automatically via announce_tree_constructed()
176
177    virtual void init_tree()              = 0;      /* impl. shall initialize the tree
178                                                     * (including some kind of leaf counter)
179                                                     * needs to be called manually */
180
181    void compute_tree() OVERRIDE {} // ARB_countedTree always is informed about its subtrees
182
183    size_t relative_position_in(const ARB_countedTree *upgroup) const;
184};
185
186
187
188#else
189#error ARB_Tree.hxx included twice
190#endif // ARB_TREE_HXX
Note: See TracBrowser for help on using the repository browser.