ARB
ARB_Tree.hxx
Go to the documentation of this file.
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 
31 typedef void (*ARB_tree_node_del_cb)(GBDATA*, class ARB_seqtree*);
32 
33 class ARB_seqtree_root;
34 class ARB_seqtree;
35 class ARB_edge;
36 class AP_weights;
37 class AliView;
38 
39 #ifdef GCC_TOO_SMART_FOR_USEFUL_FINAL_TYPE_SUGGESTION
40 # pragma GCC diagnostic push
41 # pragma GCC diagnostic ignored "-Wsuggest-final-types"
42 #endif
43 
44 class ARB_seqtree_root : public TreeRoot { // derived from Noncopyable
45  AliView *ali;
46 
47  AP_sequence *seqTemplate;
48 
49  // following variables are set, if the tree has been loaded from DB
50  char *tree_name; // name of the tree in DB
51  GBDATA *gb_tree; // tree container in DB
52 
53  bool isLinkedToDB;
54  bool addDeleteCallbacks;
55 
56 protected:
57  void set_gb_tree(GBDATA *gbTree) {
58  at_assert(implicated(gb_tree, gb_tree == gbTree));
59  at_assert(tree_name);
60  gb_tree = gbTree;
61  }
62  void set_gb_tree_and_name(GBDATA *gbTree, const char *name) {
63  at_assert(!gb_tree);
64  at_assert(!tree_name);
65  gb_tree = gbTree;
66  tree_name = ARB_strdup(name);
67  }
68 
69 public:
70  ARB_seqtree_root(AliView *aliView, AP_sequence *seqTempl, bool add_delete_callbacks);
72 
74 
75  const AliView *get_aliview() const { return ali; }
76 
77  const AP_filter *get_filter() const { return ali->get_filter(); }
78  const AP_weights *get_weights() const { return ali->get_weights(); }
79 
80  GBDATA *get_gb_main() const { return ali->get_gb_main(); }
81 
82  GBDATA *get_gb_tree() const { return gb_tree; } // NULp if no tree loaded (or tree disappeared from DB)
83  void tree_deleted_cb(GBDATA *gb_tree_del); // internal
84 
85  const char *get_tree_name() const { return tree_name; } // NULp if no tree loaded (or tree disappeared from DB)
86 
87  virtual GB_ERROR loadFromDB(const char *name) __ATTR__USERESULT;
88  virtual GB_ERROR saveToDB() __ATTR__USERESULT;
89 
90  const AP_sequence *get_seqTemplate() const { return seqTemplate; }
91 
92  GB_ERROR linkToDB_and_count(int *zombies, int *duplicates) __ATTR__USERESULT;
93  GB_ERROR linkToDB() __ATTR__USERESULT { return linkToDB_and_count(NULp, NULp); }
94  void unlinkFromDB(); // @@@ is (but should not be) unused
96 };
97 MARK_NONFINAL_CLASS(ARB_seqtree_root); // does not work for too smart gcc|s
98 #ifdef GCC_TOO_SMART_FOR_USEFUL_FINAL_TYPE_SUGGESTION
99 # pragma GCC diagnostic pop
100 #endif
101 
102 MARK_NONFINAL_FUNCTION(ARB_seqtree_root,GB_ERROR,loadFromDB,(const char*),NULp);
104 
106  size_t leafs;
107  size_t innerNodes;
108  size_t groups;
109  size_t unlinked; // unlinked leafs (same as 'leafs' if tree is unlinked)
110  size_t marked; // leafs linked to marked species
111  // size_t with_sequence; // @@@ add when AP_sequence is member of ARB_seqtree
112 
113  ARB_tree_info();
114 
115  size_t nodes() const { return innerNodes+leafs; }
116  size_t linked() const { return leafs-unlinked; }
117  size_t unmarked() const { return linked()-marked; }
118 };
119 
120 class ARB_seqtree : public TreeNode { // derived from Noncopyable
121  friend GB_ERROR ARB_seqtree_root::loadFromDB(const char *name);
122  friend GB_ERROR ARB_seqtree_root::linkToDB_and_count(int *zombies, int *duplicates);
123  friend void ARB_seqtree_root::unlinkFromDB();
124 
125  AP_sequence *seq; /* NULp if tree is unlinked
126  * otherwise automatically valid for leafs with gb_node!
127  * may be set manually for inner nodes
128  */
129 
130  // ------------------
131  // functions
132 
133  void unloadSequences();
134  GB_ERROR preloadLeafSequences();
135 
137  void remove_delete_cb_rec(ARB_tree_node_del_cb cb);
138 
139 protected:
140  AP_sequence *take_seq() { // afterwards not has no seq and caller is responsible for sequence
141  AP_sequence *result = seq;
142  seq = NULp;
143  return result;
144  }
145  void replace_seq(AP_sequence *sequence);
146 
148 
149 public:
151  TreeNode(root),
152  seq(NULp)
153  {}
154 
156 
158 
159  // order in dendogram:
160  bool is_upper_son() const { return is_leftson(); }
161  bool is_lower_son() const { return is_rightson(); }
162 
163  AP_sequence *get_seq() { return seq; }
164  const AP_sequence *get_seq() const { return seq; }
166  at_assert(!seq); // already set
167 #ifndef UNIT_TESTS // UT_DIFF
168  // unit tests are allowed to leave sequence undefined // @@@ better solution?
169  at_assert(sequence);
170 #endif
171  seq = sequence;
172  return seq;
173  }
174 
175  bool hasSequence() const { return seq && seq->hasSequence(); }
176 
177  void mark_subtree();
179 
180 };
181 
182 #define OVERRIDE_SEQ_ACCESSORS(SEQTYPE,BASETREETYPE) \
183  SEQTYPE *get_seq() { return DOWNCAST(SEQTYPE*,BASETREETYPE::get_seq()); } \
184  const SEQTYPE *get_seq() const { return DOWNCAST(const SEQTYPE*,BASETREETYPE::get_seq()); } \
185  SEQTYPE *set_seq(AP_sequence *sequence) { return DOWNCAST(SEQTYPE*, BASETREETYPE::set_seq(sequence)); } \
186 
188  virtual ~ARB_tree_predicate() {}
189  virtual bool selects(const ARB_seqtree& tree) const = 0;
190 };
191 
192 // ------------------------
193 // ARB_countedTree
194 // tree that knows its size
195 
196 class ARB_countedTree : public ARB_seqtree {
197 protected:
199 public:
200  explicit ARB_countedTree(ARB_seqtree_root *tree_root_)
201  : ARB_seqtree(tree_root_)
202  {}
204 
205  // @@@ TODO:
206  // - add debug code (checking init_tree() is called exactly once)
207  // - init_tree() might be called automatically via announce_tree_constructed()
208 
209  virtual void init_tree() = 0; /* impl. shall initialize the tree
210  * (including some kind of leaf counter)
211  * needs to be called manually */
212 
213  void compute_tree() OVERRIDE {} // ARB_countedTree always is informed about its subtrees
214 
215  size_t relative_position_in(const ARB_countedTree *upgroup) const;
216 };
217 
218 
219 
220 #else
221 #error ARB_Tree.hxx included twice
222 #endif // ARB_TREE_HXX
GBDATA * get_gb_main() const
Definition: ARB_Tree.hxx:80
const char * GB_ERROR
Definition: arb_core.h:25
bool is_rightson() const
Definition: TreeNode.h:285
const AliView * get_aliview() const
Definition: ARB_Tree.hxx:75
string result
void compute_tree() OVERRIDE
Definition: ARB_Tree.hxx:213
size_t marked
Definition: ARB_Tree.hxx:110
#define implicated(hypothesis, conclusion)
Definition: arb_assert.h:289
size_t nodes() const
Definition: ARB_Tree.hxx:115
bool is_upper_son() const
Definition: ARB_Tree.hxx:160
bool hasSequence() const
Definition: ARB_Tree.hxx:175
PREPARE_MARK_NONFINAL_CLASS(ARB_seqtree_root)
size_t groups
Definition: ARB_Tree.hxx:108
size_t unlinked
Definition: ARB_Tree.hxx:109
size_t unmarked() const
Definition: ARB_Tree.hxx:117
char * ARB_strdup(const char *str)
Definition: arb_string.h:27
void calcTreeInfo(ARB_tree_info &info)
Definition: ARB_Tree.cxx:163
ARB_seqtree_root(AliView *aliView, AP_sequence *seqTempl, bool add_delete_callbacks)
Definition: ARB_Tree.cxx:28
#define at_assert(cond)
Definition: ARB_Tree.hxx:29
const AP_weights * get_weights() const
Definition: AliView.hxx:45
#define cb(action)
const AP_filter * get_filter() const
Definition: ARB_Tree.hxx:77
size_t leafs
Definition: ARB_Tree.hxx:106
MARK_NONFINAL_FUNCTION(ARB_seqtree_root, GB_ERROR, loadFromDB,(const char *), NULp)
size_t relative_position_in(const ARB_countedTree *upgroup) const
Definition: ARB_Tree.cxx:267
void replace_seq(AP_sequence *sequence)
Definition: ARB_Tree.cxx:256
const char * get_tree_name() const
Definition: ARB_Tree.hxx:85
MARK_NONFINAL_CLASS(ARB_seqtree_root)
GBDATA * get_gb_tree() const
Definition: ARB_Tree.hxx:82
DEFINE_TREE_ACCESSORS(ARB_seqtree_root, ARB_seqtree)
const AP_weights * get_weights() const
Definition: ARB_Tree.hxx:78
virtual GB_ERROR saveToDB() __ATTR__USERESULT
Definition: ARB_Tree.cxx:107
bool is_leftson() const
Definition: TreeNode.h:280
virtual bool selects(const ARB_seqtree &tree) const =0
virtual GB_ERROR loadFromDB(const char *name) __ATTR__USERESULT
Definition: ARB_Tree.cxx:66
bool hasSequence() const
Definition: AP_sequence.hxx:75
~ARB_seqtree() OVERRIDE
Definition: ARB_Tree.cxx:184
void unlinkFromDB()
Definition: ARB_Tree.cxx:147
size_t innerNodes
Definition: ARB_Tree.hxx:107
DEFINE_TREE_ACCESSORS(ARB_seqtree_root, ARB_countedTree)
AP_sequence * get_seq()
Definition: ARB_Tree.hxx:163
const AP_sequence * get_seq() const
Definition: ARB_Tree.hxx:164
AP_sequence * set_seq(AP_sequence *sequence)
Definition: ARB_Tree.hxx:165
GB_ERROR linkToDB() __ATTR__USERESULT
Definition: ARB_Tree.hxx:93
ARB_countedTree(ARB_seqtree_root *tree_root_)
Definition: ARB_Tree.hxx:200
const AP_filter * get_filter() const
Definition: AliView.hxx:42
DEFINE_TREE_ROOT_ACCESSORS(ARB_seqtree_root, ARB_seqtree)
~ARB_seqtree_root() OVERRIDE
Definition: ARB_Tree.cxx:49
~ARB_countedTree() OVERRIDE
Definition: ARB_Tree.hxx:198
virtual ~ARB_tree_predicate()
Definition: ARB_Tree.hxx:188
virtual void init_tree()=0
AP_sequence * take_seq()
Definition: ARB_Tree.hxx:140
#define OVERRIDE
Definition: cxxforward.h:112
#define __ATTR__USERESULT
Definition: attributes.h:58
char * name
Definition: TreeNode.h:226
bool contains_marked_species()
Definition: ARB_Tree.cxx:198
#define NULp
Definition: cxxforward.h:116
size_t linked() const
Definition: ARB_Tree.hxx:116
void(* ARB_tree_node_del_cb)(GBDATA *, class ARB_seqtree *)
Definition: ARB_Tree.hxx:31
void set_gb_tree_and_name(GBDATA *gbTree, const char *name)
Definition: ARB_Tree.hxx:62
const AP_sequence * get_seqTemplate() const
Definition: ARB_Tree.hxx:90
void mark_subtree()
Definition: ARB_Tree.cxx:188
static int info[maxsites+1]
bool is_lower_son() const
Definition: ARB_Tree.hxx:161
GBDATA * get_gb_main() const
Definition: AliView.hxx:46
void set_gb_tree(GBDATA *gbTree)
Definition: ARB_Tree.hxx:57
void tree_deleted_cb(GBDATA *gb_tree_del)
Definition: ARB_Tree.cxx:57
GB_ERROR linkToDB_and_count(int *zombies, int *duplicates) __ATTR__USERESULT
Definition: ARB_Tree.cxx:130