source: tags/ms_r18q1/SL/ARB_TREE/ARB_Tree.hxx

Last change on this file was 16880, checked in by westram, 6 years ago
  • reintegrates 'multicore' into 'trunk'
    • derive sequence classes able to be combined from new class AP_combinableSeq
      • elim dummy functions from rest of hierarchy
    • implements non-writing combine for add-species (speedup: 25-35%)
    • introduce type Mutations ⇒ uses long everywhere (before mostly, but not always used float)
    • allows use of futures
      • nucleotide combine is able to calculate async (disabled via define atm → NEVER_COMBINE_ASYNC; needs more work on calling algorithm)
  • adds: log:branches/multicore@16769:16879
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 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 TREENODE_H
22#include <TreeNode.h>
23#endif
24#ifndef AP_SEQUENCE_HXX
25#include <AP_sequence.hxx>
26#endif
27
28
29#define at_assert(cond) arb_assert(cond)
30
31typedef void (*ARB_tree_node_del_cb)(GBDATA*, class ARB_seqtree*);
32
33class ARB_seqtree_root;
34class ARB_seqtree;
35class ARB_edge;
36class AP_weights;
37class AliView;
38
39class ARB_seqtree_root : public TreeRoot { // derived from Noncopyable
40    AliView     *ali;
41
42    AP_sequence *seqTemplate;
43
44    // following variables are set, if the tree has been loaded from DB
45    char   *tree_name;                              // name of the tree in DB
46    GBDATA *gb_tree;                                // tree container in DB
47
48    bool isLinkedToDB;
49    bool addDeleteCallbacks;
50
51protected:
52    void set_gb_tree(GBDATA *gbTree) {
53        at_assert(implicated(gb_tree, gb_tree == gbTree));
54        at_assert(tree_name);
55        gb_tree = gbTree;
56    }
57    void set_gb_tree_and_name(GBDATA *gbTree, const char *name) {
58        at_assert(!gb_tree);
59        at_assert(!tree_name);
60        gb_tree   = gbTree;
61        tree_name = ARB_strdup(name);
62    }
63
64public:
65    ARB_seqtree_root(AliView *aliView, AP_sequence *seqTempl, bool add_delete_callbacks);
66    ~ARB_seqtree_root() OVERRIDE;
67
68    DEFINE_TREE_ROOT_ACCESSORS(ARB_seqtree_root, ARB_seqtree);
69
70    const AliView *get_aliview() const { return ali; }
71
72    const AP_filter *get_filter() const { return ali->get_filter(); }
73    const AP_weights *get_weights() const { return ali->get_weights(); }
74
75    GBDATA *get_gb_main() const { return ali->get_gb_main(); }
76
77    GBDATA *get_gb_tree() const { return gb_tree; } // NULp if no tree loaded (or tree disappeared from DB)
78    void tree_deleted_cb(GBDATA *gb_tree_del);      // internal
79
80    const char *get_tree_name() const { return tree_name; } // NULp if no tree loaded (or tree disappeared from DB)
81
82    virtual GB_ERROR loadFromDB(const char *name) __ATTR__USERESULT;
83    virtual GB_ERROR saveToDB() __ATTR__USERESULT;
84
85    const AP_sequence *get_seqTemplate() const { return seqTemplate; }
86
87    GB_ERROR linkToDB(int *zombies, int *duplicates) __ATTR__USERESULT;
88    void unlinkFromDB(); // @@@ is (but should not be) unused
89};
90MARK_NONFINAL_CLASS(ARB_seqtree_root);
91MARK_NONFINAL_METHOD(ARB_seqtree_root,GB_ERROR,loadFromDB,(const char*));
92MARK_NONFINAL_METHOD(ARB_seqtree_root,GB_ERROR,saveToDB,());
93
94struct ARB_tree_info {
95    size_t leafs;
96    size_t innerNodes;
97    size_t groups;
98    size_t unlinked;                                // unlinked leafs (same as 'leafs' if tree is unlinked)
99    size_t marked;                                  // leafs linked to marked species
100    // size_t with_sequence; // @@@ add when AP_sequence is member of ARB_seqtree
101
102    ARB_tree_info();
103
104    size_t nodes()    const { return innerNodes+leafs; }
105    size_t linked()   const { return leafs-unlinked; }
106    size_t unmarked() const { return linked()-marked; }
107};
108
109class ARB_seqtree : public TreeNode { // derived from Noncopyable
110    friend GB_ERROR ARB_seqtree_root::loadFromDB(const char *name);
111    friend GB_ERROR ARB_seqtree_root::linkToDB(int *zombies, int *duplicates);
112    friend void     ARB_seqtree_root::unlinkFromDB();
113
114    AP_sequence *seq; /* NULp if tree is unlinked
115                       * otherwise automatically valid for leafs with gb_node!
116                       * may be set manually for inner nodes
117                       */
118
119    // ------------------
120    //      functions
121
122    void unloadSequences();
123    GB_ERROR preloadLeafSequences();
124
125    GB_ERROR add_delete_cb_rec(ARB_tree_node_del_cb cb) __ATTR__USERESULT;
126    void remove_delete_cb_rec(ARB_tree_node_del_cb cb);
127
128protected:
129    AP_sequence *take_seq() { // afterwards not has no seq and caller is responsible for sequence
130        AP_sequence *result = seq;
131        seq = NULp;
132        return result;
133    }
134    void replace_seq(AP_sequence *sequence);
135
136    ~ARB_seqtree() OVERRIDE;
137
138public:
139    ARB_seqtree(ARB_seqtree_root *root) :
140        TreeNode(root),
141        seq(NULp)
142    {}
143
144    DEFINE_TREE_ACCESSORS(ARB_seqtree_root, ARB_seqtree);
145
146    void calcTreeInfo(ARB_tree_info& info);
147
148    // order in dendogram:
149    bool is_upper_son() const { return is_leftson(); }
150    bool is_lower_son() const { return is_rightson(); }
151
152    AP_sequence *get_seq() { return seq; }
153    const AP_sequence *get_seq() const { return seq; }
154    AP_sequence *set_seq(AP_sequence *sequence) {
155        at_assert(!seq); // already set
156#ifndef UNIT_TESTS // UT_DIFF
157        // unit tests are allowed to leave sequence undefined // @@@ better solution?
158        at_assert(sequence);
159#endif
160        seq = sequence;
161        return seq;
162    }
163
164    bool hasSequence() const { return seq && seq->hasSequence(); }
165
166    void mark_subtree();
167    bool contains_marked_species();
168
169};
170
171#define OVERRIDE_SEQ_ACCESSORS(SEQTYPE,BASETREETYPE)                                                            \
172    SEQTYPE *get_seq() { return DOWNCAST(SEQTYPE*,BASETREETYPE::get_seq()); }                                   \
173    const SEQTYPE *get_seq() const { return DOWNCAST(const SEQTYPE*,BASETREETYPE::get_seq()); }                 \
174    SEQTYPE *set_seq(AP_sequence *sequence) { return DOWNCAST(SEQTYPE*, BASETREETYPE::set_seq(sequence)); }     \
175
176struct ARB_tree_predicate {
177    virtual ~ARB_tree_predicate() {}
178    virtual bool selects(const ARB_seqtree& tree) const = 0;
179};
180
181// ------------------------
182//      ARB_countedTree
183//      tree that knows its size
184
185class ARB_countedTree : public ARB_seqtree {
186protected:
187    ~ARB_countedTree() OVERRIDE {}
188public:
189    explicit ARB_countedTree(ARB_seqtree_root *tree_root_)
190        : ARB_seqtree(tree_root_)
191    {}
192    DEFINE_TREE_ACCESSORS(ARB_seqtree_root, ARB_countedTree);
193
194    // @@@ TODO:
195    // - add debug code (checking init_tree() is called exactly once)
196    // - init_tree() might be called automatically via announce_tree_constructed()
197
198    virtual void init_tree()              = 0;      /* impl. shall initialize the tree
199                                                     * (including some kind of leaf counter)
200                                                     * needs to be called manually */
201
202    void compute_tree() OVERRIDE {} // ARB_countedTree always is informed about its subtrees
203
204    size_t relative_position_in(const ARB_countedTree *upgroup) const;
205};
206
207
208
209#else
210#error ARB_Tree.hxx included twice
211#endif // ARB_TREE_HXX
Note: See TracBrowser for help on using the repository browser.