ARB
TreeNode.h
Go to the documentation of this file.
1 // ================================================================ //
2 // //
3 // File : TreeNode.h //
4 // Purpose : //
5 // //
6 // Coded by Ralf Westram (coder@reallysoft.de) in December 2013 //
7 // Institute of Microbiology (Technical University Munich) //
8 // http://www.arb-home.de/ //
9 // //
10 // ================================================================ //
11 
12 #ifndef TREENODE_H
13 #define TREENODE_H
14 
15 #ifndef ARBDBT_H
16 #include "arbdbt.h"
17 #endif
18 #ifndef _GLIBCXX_ALGORITHM
19 #include <algorithm>
20 #endif
21 
22 #define rt_assert(cond) arb_assert(cond)
23 
24 #if defined(DEBUG) || defined(UNIT_TESTS) // UT_DIFF
25 # define PROVIDE_TREE_STRUCTURE_TESTS
26 #endif
27 #if defined(DEVEL_RALF) && defined(PROVIDE_TREE_STRUCTURE_TESTS)
28 # define AUTO_CHECK_TREE_STRUCTURE // Note: dramatically slows down most tree operations
29 #endif
30 
31 class TreeRoot;
32 class TreeNode;
33 class ARB_edge;
34 
35 enum TreeOrder { // contains bit values!
36  ORDER_BIG_DOWN = 1, // bit 0 set -> big branches down
37  ORDER_BIG_TO_EDGE = 2, // bit 1 set -> big branches to edge
38  ORDER_BIG_TO_CENTER = 4, // bit 2 set -> big branches to center
39  ORDER_ALTERNATING = 8, // bit 3 set -> alternate bit 0
40 
41  // user visible orders:
47 };
48 
49 #define DEFINE_READ_ACCESSORS(TYPE, ACCESS, MEMBER) \
50  TYPE ACCESS() { return MEMBER; } \
51  const TYPE ACCESS() const { return MEMBER; }
52 
53 class TreeRoot : virtual Noncopyable {
54  TreeNode *rootNode; // root node of the tree
55 
56  bool deleteWithNodes;
57  bool seenBootstrapDuringLoad;
58  bool autoModified;
59 
60 protected:
61  void predelete() {
62  // should be called from dtor of derived class defining makeNode/destroyNode
63  if (rootNode) {
64  destroyNode(rootNode);
65  rt_assert(!rootNode);
66  }
67  }
68 public:
69 
70 #if defined(UNIT_TESTS)
71  bool detect_groups_without_gbnode;
72 #endif
73 
74  explicit TreeRoot(bool deleteWithNodes_) :
75  rootNode(NULp),
76  deleteWithNodes(deleteWithNodes_),
77  seenBootstrapDuringLoad(false),
78  autoModified(false)
79  {
99 #if defined(UNIT_TESTS)
100  detect_groups_without_gbnode = false;
101 #endif
102  }
103  virtual ~TreeRoot();
104  virtual void change_root(TreeNode *old, TreeNode *newroot);
105 
106  void delete_by_node() {
107  if (deleteWithNodes) {
108  rt_assert(!rootNode);
109  delete this;
110  }
111  }
112 
113  bool has_bootstrap() const {
114  return seenBootstrapDuringLoad;
115  }
116  void set_bootstrap_seen(bool seen) {
117  seenBootstrapDuringLoad = seen;
118  }
119 
120  bool was_auto_modified() const {
121  return autoModified;
122  }
123  void set_auto_modified(bool modified) {
124  autoModified = modified;
125  }
126 
127  virtual TreeNode *makeNode() const = 0;
128  virtual void destroyNode(TreeNode *node) const = 0;
129 
130  DEFINE_READ_ACCESSORS(TreeNode*, get_root_node, rootNode);
131 
133 };
135 
136 inline GBT_RemarkType parse_remark(const char *remark, double& bootstrap) {
140  if (!remark) return REMARK_NONE;
141 
142  const char *end = NULp;
143  bootstrap = strtod(remark, (char**)&end);
144 
145  bool is_bootstrap = end[0] == '%' && end[1] == 0;
146  return is_bootstrap ? REMARK_BOOTSTRAP : REMARK_OTHER;
147 }
148 
149 inline bool parse_treelabel(const char*& label, double& bootstrap, char*& remark) {
170  // @@@ reimplement using parse_remark
171 
172  const char *end = NULp;
173  bootstrap = strtod(label, (char**)&end);
174 
175  remark = NULp;
176 
177  bool is_bootstrap = end != label;
178  if (is_bootstrap) {
179  if (end[0] == '%') {
180  ++end;
181  bootstrap = bootstrap/100.0; // percent -> [0..1]
182  }
183  switch (end[0]) {
184  case ':': label = end+1; break;
185  case 0: label = NULp; break;
186  default: {
187  is_bootstrap = false; // enter !is_bootstrap-branch below
188  bootstrap = 0.0; // cleanup partial parsing
189  break;
190  }
191  }
192  }
193  if (!is_bootstrap) {
194  const char *sep = strchr(label, ':');
195  if (sep) {
196  if (label<sep) remark = ARB_strpartdup(label, sep-1); // do not report empty remark
197  label = sep+1;
198  }
199  // otherwise take whole label as groupname
200  }
201 
202  if (label && !label[0]) label = NULp; // do not report empty groupname
203 
204  gb_assert(implicated(remark, !is_bootstrap));
205  gb_assert(implicated(is_bootstrap, !remark));
206 
207  return is_bootstrap;
208 }
209 inline bool remark_will_parse_as_bootstrap(const char *label) {
210  double bootstrap;
211  char *remark;
212  bool parses_as_bootstrap = parse_treelabel(label, bootstrap, remark);
213  free(remark);
214 
215  // @@@ reimplement using parse_remark()
216 
217  return parses_as_bootstrap;
218 }
219 
220 #define KEELED_INDICATOR '!' // prefixed to names of "keeled groups" (not meant to be changed)
221 
222 struct TreeNode : virtual Noncopyable {
226  char *name;
227 
228 private:
229  bool leaf;
230  bool keeledOver; // node has group info and tree-root was moved "inside" that group -> group changed meaning (see #735)
231  bool inverseLeft; // (only if keeledOver) true -> left son contains "inverse" of original group; false -> right son dito
232 
233  SmartCharPtr remark_branch; // remark_branch normally contains some bootstrap value in format 'xx%'
234  // if you store other info there, please make sure that this info does not start with digits!!
235  // Otherwise the tree export routines will not work correctly!
236 
237  GBT_LEN& length_ref() { return is_leftson() ? father->leftlen : father->rightlen; }
238  const GBT_LEN& length_ref() const { return const_cast<TreeNode*>(this)->length_ref(); }
239 
240  void keelOver(TreeNode *prev, TreeNode *next, double len);
241 
242 protected:
244  return is_leftson() ? father->leftson : father->rightson;
245  }
247  if (father) {
248  self_ref() = NULp;
249  father = NULp;
250  }
251  }
252 
253  inline void swap_node_info(TreeNode *other, bool ofKeeledGroups);
255  if (father->keeledOver) {
256  father->inverseLeft = is_leftson();
258  }
259  }
260 
261 public:
262 
263  bool is_leaf() const { return leaf; }
264  void markAsLeaf() {
265  rt_assert(!is_leaf());
266  rt_assert(!leftson && !rightson); // only allowed during setup!
267  leaf = true;
268  }
269 
271  DEFINE_READ_ACCESSORS(TreeNode*, get_leftson, leftson);
272  DEFINE_READ_ACCESSORS(TreeNode*, get_rightson, rightson);
273 
274  // Note: unittests for these attributes are in ../NTREE/ad_trees.cxx@TEST_TreeNode_attributes
275 
276  bool is_son_of(const TreeNode *Father) const {
277  return father == Father &&
278  (father->leftson == this || father->rightson == this);
279  }
280  bool is_leftson() const {
281  // left when root is at bottom; see also ../SL/ARB_TREE/ARB_Tree.hxx@is_upper_son
282  gb_assert(is_son_of(get_father())); // do only call with sons!
283  return father->leftson == this;
284  }
285  bool is_rightson() const {
286  gb_assert(is_son_of(get_father())); // do only call with sons!
287  return father->rightson == this;
288  }
289 
290  bool is_inside(const TreeNode *subtree) const {
291  return this == subtree || (father && get_father()->is_inside(subtree));
292  }
293  bool is_ancestor_of(const TreeNode *descendant) const {
294  return !is_leaf() && descendant != this && descendant->is_inside(this);
295  }
296  bool in_same_branch_as(const TreeNode *other) const {
297  // returns true if 'this' and 'other' are in ONE branch
298  return this == other || is_ancestor_of(other) || other->is_ancestor_of(this);
299  }
300  bool in_other_branch_than(const TreeNode *other) const {
301  // returns true if 'this' and 'other' are NOT in one branch
302  return !in_same_branch_as(other);
303  }
304  const TreeNode *ancestor_common_with(const TreeNode *other) const;
305  TreeNode *ancestor_common_with(TreeNode *other) { return const_cast<TreeNode*>(ancestor_common_with(const_cast<const TreeNode*>(other))); }
306 
307  bool is_son_of_root() const {
308  return father && !father->father && father->is_root_node();
309  }
310 
311  GBT_LEN get_branchlength() const { return length_ref(); }
312  void set_branchlength(GBT_LEN newlen) {
313  gb_assert(!is_nan_or_inf(newlen));
314  length_ref() = newlen;
315  }
316 
319  if (father->is_root_node()) {
320  return father->leftlen+father->rightlen;
321  }
322  return get_branchlength();
323  }
326  if (father->is_root_node()) {
327  father->leftlen = newlen/2;
328  father->rightlen = newlen-father->leftlen; // make sure sum equals newlen
329  }
330  else {
331  set_branchlength(newlen);
332  }
333  }
334 
335  GBT_LEN sum_child_lengths() const;
338  return father ? get_branchlength()+father->root_distance() : 0.0;
339  }
340  GBT_LEN intree_distance_to(const TreeNode *other) const {
341  const TreeNode *ancestor = ancestor_common_with(other);
342  return root_distance() + other->root_distance() - 2*ancestor->root_distance();
343  }
344 
345  void remove_bootstrap(); // remove bootstrap values from subtree
346  GB_ERROR apply_aci_to_remarks(const char *aci, const GBL_call_env& callEnv);
347 
348  void reset_branchlengths(); // reset branchlengths of subtree to tree_defaults::LENGTH
349  void scale_branchlengths(double factor);
350 
351  void bootstrap2branchlen(); // copy bootstraps to branchlengths
352  void branchlen2bootstrap(); // copy branchlengths to bootstraps
353  void branchlenXbootstrap(); // swap branchlengths and bootstraps
354 
355  GBT_RemarkType parse_bootstrap(double& bootstrap, WarningConsumer report);
356 
357  const char *get_remark() const {
358  rt_assert(!is_leaf()); // only inner nodes may have bootstraps
359  rt_assert(implicated(remark_branch.content(), remark_branch.content()[0])); // expect remark_branch is not empty
360  return remark_branch.content();
361  }
362  const SmartCharPtr& get_remark_ptr() const {
363  rt_assert(!is_leaf()); // only inner nodes may have bootstraps
364  return remark_branch;
365  }
366  bool is_inner_node_with_remark() const { return !is_leaf() && get_remark_ptr().isSet(); }
367  void use_as_remark(const SmartCharPtr& newRemark) {
368  rt_assert(!is_leaf()); // only inner nodes may have bootstraps
369  rt_assert(implicated(newRemark.content(), newRemark.content()[0]));
370  remark_branch = newRemark;
371  }
372  void set_remark(const char *newRemark) {
373  use_as_remark(strdup(newRemark));
374  }
375  void set_bootstrap(double bootstrap);
376  void remove_remark() {
377  SmartCharPtr norem;
378  use_as_remark(norem);
379  }
380 #if defined(ASSERTION_USED) || defined(PROVIDE_TREE_STRUCTURE_TESTS)
381  bool has_no_remark() const { return remark_branch.isNull(); }
382  bool has_valid_root_remarks() const;
383 #endif
384 
385 private:
386 
387  friend void TreeRoot::change_root(TreeNode *old, TreeNode *newroot);
388 
389  TreeRoot *tree_root;
390 
391  // ------------------
392  // functions
393 
394  void reorder_subtree(TreeOrder mode);
395 
396 protected:
397  void set_tree_root(TreeRoot *new_root);
398 
399  bool at_root() const {
401  return !father || !father->father;
402  }
403  virtual ~TreeNode() {
404  if (tree_root) {
405  rt_assert(tree_root->get_root_node() == this); // you may only free the root-node or unlinked nodes (i.e. nodes where tree_root is NULp)
406 
407  TreeRoot *root = tree_root;
408  root->TreeRoot::change_root(this, NULp);
409  root->delete_by_node();
410  }
411  delete leftson; gb_assert(!leftson); // cannot use destroy here
412  delete rightson; gb_assert(!rightson);
413 
415 
416  free(name);
417  }
418  void destroy() {
419  rt_assert(knownNonNull(this));
420  TreeRoot *myRoot = get_tree_root();
421  rt_assert(myRoot); // if this fails, you need to use destroy(TreeRoot*), i.e. destroy(TreeNode*, TreeRoot*)
422  myRoot->destroyNode(this);
423  }
424  void destroy(TreeRoot *viaRoot) {
425  rt_assert(knownNonNull(this));
426 #if defined(ASSERTION_USED)
427  TreeRoot *myRoot = get_tree_root();
428  rt_assert(!myRoot || myRoot == viaRoot);
429 #endif
430  viaRoot->destroyNode(this);
431  }
432 
433 public:
435  father(NULp), leftson(NULp), rightson(NULp),
436  leftlen(0.0), rightlen(0.0),
437  gb_node(NULp),
438  name(NULp),
439  leaf(false),
440  keeledOver(false),
441  inverseLeft(false),
442  tree_root(root)
443  {}
444  static void destroy(TreeNode *that) { // replacement for destructor
445  if (that) that->destroy();
446  }
447  static void destroy(TreeNode *that, TreeRoot *root) {
448  if (that) that->destroy(root);
449  }
450 
451  TreeNode *fixDeletedSon(); // @@@ review (design)
452 
453  void unlink_from_DB();
454 
455  void announce_tree_constructed() { // @@@ use this function or just call change_root instead?
456  // (has to be) called after tree has been constructed
457  gb_assert(!father); // has to be called with root-node!
458  gb_assert(has_no_remark()); // root node may not have a remark
459  // @@@ also check has_valid_root_remarks()
460  get_tree_root()->change_root(NULp, this);
461  }
462 
463  virtual unsigned get_leaf_count() const = 0;
464  virtual void compute_tree() = 0;
465 
468  leftson = NULp;
469  rightson = NULp;
470  father = NULp;
471  }
472 
473  TreeRoot *get_tree_root() const { return tree_root; }
474 
475  const TreeNode *get_root_node() const {
476  // only works properly if tree is completely constructed!
477  if (!tree_root) return NULp; // nodes removed from tree have no root-node
478 
479  const TreeNode *root = tree_root->get_root_node();
480  rt_assert(!root // if tree is under construction => no root
481  || is_inside(root)); // if c'ted => 'this' should be member of tree (otherwise sth is wrong with link)
482  return root;
483  }
484  TreeNode *get_root_node() { return const_cast<TreeNode*>(const_cast<const TreeNode*>(this)->get_root_node()); }
485 
486  bool is_root_node() const { return !father && get_root_node() == this; }
487  virtual void set_root();
488 
490  rt_assert(!is_root_node()); // root node has no brother
491  rt_assert(father); // this is a removed node (not root, but no father)
492  return is_leftson() ? get_father()->get_rightson() : get_father()->get_leftson();
493  }
494  const TreeNode *get_brother() const {
495  return const_cast<const TreeNode*>(const_cast<TreeNode*>(this)->get_brother());
496  }
497 
498  bool has_group_info() const {
499  rt_assert(!is_leaf()); // a leaf never has group info (useless call)
500 
501  bool old_condition = gb_node && name; // this is only true, if tree is saved into DB and has been loaded from there.
502  bool new_condition = name;
503 
504  if (new_condition && !old_condition) {
505 #if defined(UNIT_TESTS)
506  // required for GBT_tree_2_newick (testcode-only)
507  if (tree_root->detect_groups_without_gbnode) {
508  return true;
509  }
510 #endif
511  rt_assert(0); // if this fails => check situation (new condition would change behavior)
512  }
513  return old_condition; // stick with old condition
514  }
516  return (has_group_info() && keeledOver) ? (inverseLeft ? get_leftson() : get_rightson()) : NULp;
517  }
518  const TreeNode *keelTarget() const {
519  return const_cast<TreeNode*>(this)->keelTarget();
520  }
521  bool keelsDownGroup(const TreeNode *toSon) const {
522  // returns true if node has a group keeled down 'toSon'
523  return keelTarget() == toSon;
524  }
525  void unkeelGroup() {
527  keeledOver = false;
528  }
529  int keeledStateInfo() const { // keeled-state as stored in database
530  return keeledOver ? (inverseLeft ? 1 : 2) : 0;
531  }
532  void setKeeledState(int keeledState) {
533  keeledOver = keeledState;
534  inverseLeft = keeledState == 1;
535  }
536 
537  bool is_normal_group() const {
538  // returns true when node shall show a "normal" group
539  rt_assert(!is_leaf()); // useless call (a normal group never may occur at leaf)
540  return has_group_info() && !keeledOver;
541  }
542  bool is_keeled_group() const {
543  // returns true when node shall show a "keeled" group.
544  // (i.e. when father has a keeled group oriented towards 'this')
545  return father && father->keelsDownGroup(this);
546  }
547  bool is_clade() const {
548  // return true, if a clickable group shall be displayed in tree
549  // (Note: keeled groups may appear at leafs)
550  return (!is_leaf() && is_normal_group()) || is_keeled_group();
551  }
552 
553  const char *get_group_name() const {
554  return
555  !is_leaf() && is_normal_group()
556  ? name
557  : (is_keeled_group() ? father->name : NULp);
558  }
559 
560  const TreeNode *find_parent_with_groupInfo(bool skipKeeledBrothers = false) const {
561  const TreeNode *child = this;
562  const TreeNode *parent = get_father();
563 
564  while (parent) {
565  if (parent->has_group_info()) {
566  if (!skipKeeledBrothers) break; // report any group
567 
568  const TreeNode *keeled = parent->keelTarget();
569  if (!keeled || keeled == child) break;
570 
571  // continue with next parent if keeled to other branch
572  }
573  child = parent;
574  parent = child->get_father();
575  }
576  return parent;
577  }
578  TreeNode *find_parent_with_groupInfo(bool skipKeeledBrothers = false) {
579  return const_cast<TreeNode*>(const_cast<const TreeNode*>(this)->find_parent_with_groupInfo(skipKeeledBrothers));
580  }
581 
582  const TreeNode *find_parent_clade() const {
583  // opposed to find_parent_with_groupInfo this reports only nodes where a group is DISPLAYED
584  // (i.e. in case of keeled groups at son node)
585 
586  const TreeNode *parent = find_parent_with_groupInfo();
587  const TreeNode *myBranch = this; // me or any ancestor
588  while (parent) {
589  const TreeNode *keeled = parent->keelTarget();
590  if (!keeled) break; // use parent
591 
592  if (parent != father && keeled->in_same_branch_as(myBranch)) {
593  parent = keeled; // use keeled
594  break;
595  }
596 
597  // either keeled to self, to brother or to brother of some of my ancestors -> step up
598  rt_assert(keeled == this || keeled == get_brother() || keeled->get_brother()->is_ancestor_of(this));
599 
600  myBranch = parent;
601  parent = parent->find_parent_with_groupInfo();
602  }
603 
604  rt_assert(implicated(parent, parent->is_clade()));
605 
606  return parent;
607  }
609  return const_cast<TreeNode*>(const_cast<const TreeNode*>(this)->find_parent_clade());
610  }
611  int calc_clade_level() const {
612  int taxLev = is_clade();
613  const TreeNode *parent = find_parent_clade();
614  if (parent) taxLev += parent->calc_clade_level();
615  return taxLev;
616  }
617 
618  int count_clades() const;
619 
620  virtual void swap_sons() {
621  rt_assert(!is_leaf()); // only possible for inner nodes!
622 
623  std::swap(leftson, rightson);
624  std::swap(leftlen, rightlen);
625  inverseLeft = !inverseLeft;
626  }
627  void rotate_subtree(); // flip whole subtree ( = recursive swap_sons())
628  void reorder_tree(TreeOrder mode);
629 
630  TreeNode *findLeafNamed(const char *wantedName);
631 
634  if (!is_leaf()) remove_remark();
637  return len;
638  }
639 
641  double bootstrap;
642  double branchlength;
644  multifurc_limits(double bootstrap_, double branchlength_, bool applyAtLeafs_)
645  : bootstrap(bootstrap_),
646  branchlength(branchlength_),
647  applyAtLeafs(applyAtLeafs_)
648  {}
649  };
650  class LengthCollector;
651 
652  void multifurcate();
653  void set_branchlength_preserving(GBT_LEN new_len);
654 
655  void multifurcate_whole_tree(const multifurc_limits& below);
656 private:
657  void eliminate_and_collect(const multifurc_limits& below, LengthCollector& collect);
658 public:
659 
660 #if defined(PROVIDE_TREE_STRUCTURE_TESTS)
661  Validity is_valid() const;
662 #endif // PROVIDE_TREE_STRUCTURE_TESTS
663 };
664 MARK_NONFINAL_METHOD(TreeNode,swap_sons,());
665 MARK_NONFINAL_METHOD(TreeNode,set_root,());
666 
667 inline void destroy(TreeNode *that) {
668  TreeNode::destroy(that);
669 }
670 inline void destroy(TreeNode *that, TreeRoot *root) {
671  TreeNode::destroy(that, root);
672 }
673 
674 // ---------------------------------------------------------------------------------------
675 // macros to overwrite accessors in classes derived from TreeRoot or TreeNode:
676 
677 #define DEFINE_TREE_ROOT_ACCESSORS(RootType, TreeType) \
678  DEFINE_DOWNCAST_ACCESSORS(TreeType, get_root_node, TreeRoot::get_root_node())
679 
680 #define DEFINE_TREE_RELATIVES_ACCESSORS(TreeType) \
681  DEFINE_DOWNCAST_ACCESSORS(TreeType, get_father, father); \
682  DEFINE_DOWNCAST_ACCESSORS(TreeType, get_leftson, leftson); \
683  DEFINE_DOWNCAST_ACCESSORS(TreeType, get_rightson, rightson); \
684  DEFINE_DOWNCAST_ACCESSORS(TreeType, get_brother, TreeNode::get_brother()); \
685  DEFINE_DOWNCAST_ACCESSORS(TreeType, get_root_node, TreeNode::get_root_node()); \
686  TreeType *findLeafNamed(const char *wantedName) { return DOWNCAST(TreeType*, TreeNode::findLeafNamed(wantedName)); }
687 
688 #define DEFINE_TREE_ACCESSORS(RootType, TreeType) \
689  DEFINE_DOWNCAST_ACCESSORS(RootType, get_tree_root, TreeNode::get_tree_root()); \
690  DEFINE_TREE_RELATIVES_ACCESSORS(TreeType)
691 
692 
693 // -------------------------
694 // structure tests
695 
696 #if defined(PROVIDE_TREE_STRUCTURE_TESTS)
697 template <typename TREE>
698 inline Validity tree_is_valid(const TREE *tree, bool acceptNULL) {
699  if (tree) return tree->is_valid();
700  return Validity(acceptNULL, "NULp tree");
701 }
702 template <typename TREE>
703 inline bool tree_is_valid_or_dump(const TREE *tree, bool acceptNULL) {
704  Validity valid = tree_is_valid(tree, acceptNULL);
705  if (!valid) fprintf(stderr, "\ntree is NOT valid (Reason: %s)\n", valid.why_not());
706  return valid;
707 }
708 #endif
709 
710 #if defined(AUTO_CHECK_TREE_STRUCTURE)
711 #define ASSERT_VALID_TREE(tree) rt_assert(tree_is_valid_or_dump(tree, false))
712 #define ASSERT_VALID_TREE_OR_NULL(tree) rt_assert(tree_is_valid_or_dump(tree, true))
713 #else
714 #define ASSERT_VALID_TREE(tree)
715 #define ASSERT_VALID_TREE_OR_NULL(tree)
716 #endif // AUTO_CHECK_TREE_STRUCTURE
717 
718 #if defined(PROVIDE_TREE_STRUCTURE_TESTS) && defined(UNIT_TESTS)
719 
720 #define TEST_EXPECT_VALID_TREE(tree) TEST_VALIDITY(tree_is_valid(tree, false))
721 #define TEST_EXPECT_VALID_TREE_OR_NULL(tree) TEST_VALIDITY(tree_is_valid(tree, true))
722 #define TEST_EXPECT_VALID_TREE__BROKEN(tree,why) TEST_VALIDITY__BROKEN(tree_is_valid(tree, false), why)
723 #define TEST_EXPECT_VALID_TREE_OR_NULL__BROKEN(tree,why) TEST_VALIDITY__BROKEN(tree_is_valid(tree, true), why)
724 
725 #else
726 
727 #define TEST_EXPECT_VALID_TREE(tree)
728 #define TEST_EXPECT_VALID_TREE_OR_NULL(tree)
729 #define TEST_EXPECT_VALID_TREE__BROKEN(tree)
730 #define TEST_EXPECT_VALID_TREE_OR_NULL__BROKEN(tree)
731 
732 #endif
733 
734 // --------------------
735 // SimpleTree
736 
737 struct SimpleRoot : public TreeRoot {
738  inline SimpleRoot();
739  inline TreeNode *makeNode() const OVERRIDE;
740  inline void destroyNode(TreeNode *node) const OVERRIDE;
741 };
742 
743 class SimpleTree FINAL_TYPE : public TreeNode {
744 protected:
746  friend class SimpleRoot;
747 public:
748  SimpleTree(SimpleRoot *sroot) : TreeNode(sroot) {}
749 
750  // TreeNode interface
751  unsigned get_leaf_count() const OVERRIDE {
752  rt_assert(0); // @@@ impl?
753  return 0;
754  }
756 };
757 
759 inline TreeNode *SimpleRoot::makeNode() const { return new SimpleTree(const_cast<SimpleRoot*>(this)); }
760 inline void SimpleRoot::destroyNode(TreeNode *node) const { delete DOWNCAST(SimpleTree*,node); }
761 
762 // ----------------------
763 // ARB_edge_type
764 
766  EDGE_TO_ROOT, // edge points towards the root node
767  EDGE_TO_LEAF, // edge points away from the root node
768  ROOT_EDGE, // edge between sons of root node
769 };
770 
771 class ARB_edge {
772  // ARB_edge is a directional edge between two non-root-nodes of the same tree
773  // (can act as iterator for TreeNode)
774 
775  TreeNode *from, *to;
776  ARB_edge_type type;
777 
778  ARB_edge_type detectType() const {
779  rt_assert(to != from);
780  rt_assert(!from->is_root_node()); // edges cannot be at root - use edge between sons of root!
781  rt_assert(!to->is_root_node());
782 
783  if (from->father == to) return EDGE_TO_ROOT;
784  if (to->father == from) return EDGE_TO_LEAF;
785 
786  rt_assert(from->get_brother() == to); // no edge exists between 'from' and 'to'
787  rt_assert(to->get_father()->is_root_node());
788  return ROOT_EDGE;
789  }
790 
791  GBT_LEN adjacent_distance() const;
792  GBT_LEN length_or_adjacent_distance() const {
793  {
794  GBT_LEN len = length();
795  if (len>0.0) return len;
796  }
797  return adjacent_distance();
798  }
799 
800  void virtually_add_or_distribute_length_forward(GBT_LEN len, TreeNode::LengthCollector& collect) const;
801  void virtually_distribute_length_forward(GBT_LEN len, TreeNode::LengthCollector& collect) const;
802 public:
803  void virtually_distribute_length(GBT_LEN len, TreeNode::LengthCollector& collect) const; // @@@ hm public :(
804 private:
805 
806 #if defined(UNIT_TESTS) // UT_DIFF
807  friend void TEST_edges();
808 #endif
809 
810 public:
811  ARB_edge(TreeNode *From, TreeNode *To) :
812  from(From),
813  to(To),
814  type(detectType())
815  {}
817  from(From),
818  to(To),
819  type(Type)
820  {
821  rt_assert(type == detectType());
822  }
823  ARB_edge(const ARB_edge& otherEdge) :
824  from(otherEdge.from),
825  to(otherEdge.to),
826  type(otherEdge.type)
827  {
828  rt_assert(type == detectType());
829  }
830 
832 
833  ARB_edge_type get_type() const { return type; }
834  TreeNode *source() const { return from; }
835  TreeNode *dest() const { return to; }
836 
837  TreeNode *son() const { return type == EDGE_TO_ROOT ? from : to; }
838  TreeNode *other() const { return type == EDGE_TO_ROOT ? to : from; }
839 
840  GBT_LEN length() const {
841  if (type == ROOT_EDGE) return from->get_branchlength() + to->get_branchlength();
842  return son()->get_branchlength();
843  }
844  void set_length(GBT_LEN len) {
845  if (type == ROOT_EDGE) {
846  from->set_branchlength(len/2);
847  to->set_branchlength(len/2);
848  }
849  else {
850  son()->set_branchlength(len);
851  }
852  }
855  if (type == ROOT_EDGE) {
857  }
858  return son()->reset_length_and_bootstrap();
859  }
860 
861  ARB_edge inverse() const {
862  return ARB_edge(to, from, ARB_edge_type(type == ROOT_EDGE ? ROOT_EDGE : (EDGE_TO_LEAF+EDGE_TO_ROOT)-type));
863  }
864 
865  // iterator functions: endlessly iterate over all edges of tree
866  // - next: forward (=towards dest())
867  // - previous: backward (=back before source())
868  // - counter: forward descends left (=upper) son first
869  // - non-counter: forward descends right (=lower) son first
870 
871  ARB_edge next() const { // descends rightson first (traverses leaf-edges from bottom to top)
872  if (type == EDGE_TO_ROOT) {
873  rt_assert(from->is_son_of(to));
874  if (from->is_rightson()) return ARB_edge(to, to->get_leftson(), EDGE_TO_LEAF);
875  TreeNode *father = to->get_father();
876  if (father->is_root_node()) return ARB_edge(to, to->get_brother(), ROOT_EDGE);
877  return ARB_edge(to, father, EDGE_TO_ROOT);
878  }
879  if (is_edge_to_leaf()) return inverse();
880  return ARB_edge(to, to->get_rightson(), EDGE_TO_LEAF);
881  }
882  ARB_edge previous() const { // inverse of next(). (traverses leaf-edges from top to bottom)
883  if (type == EDGE_TO_LEAF) {
884  rt_assert(to->is_son_of(from));
885  if (to->is_leftson()) return ARB_edge(from->get_rightson(), from, EDGE_TO_ROOT);
886  TreeNode *father = from->get_father();
887  if (father->is_root_node()) return ARB_edge(from->get_brother(), from, ROOT_EDGE);
888  return ARB_edge(father, from, EDGE_TO_LEAF);
889  }
890  if (is_edge_from_leaf()) return inverse();
891  return ARB_edge(from->get_leftson(), from, EDGE_TO_ROOT);
892  }
893 
894  ARB_edge counter_next() const { // descends leftson first (traverses leaf-edges from top to bottom)
895  if (type == EDGE_TO_ROOT) {
896  rt_assert(from->is_son_of(to));
897  if (from->is_leftson()) return ARB_edge(to, to->get_rightson(), EDGE_TO_LEAF);
898  TreeNode *father = to->get_father();
899  if (father->is_root_node()) return ARB_edge(to, to->get_brother(), ROOT_EDGE);
900  return ARB_edge(to, father, EDGE_TO_ROOT);
901  }
902  if (is_edge_to_leaf()) return inverse();
903  return ARB_edge(to, to->get_leftson(), EDGE_TO_LEAF);
904  }
905  ARB_edge counter_previous() const { // inverse of counter_next(). (traverses leaf-edges from bottom to top)
906  if (type == EDGE_TO_LEAF) {
907  rt_assert(to->is_son_of(from));
908  if (to->is_rightson()) return ARB_edge(from->get_leftson(), from, EDGE_TO_ROOT);
909  TreeNode *father = from->get_father();
910  if (father->is_root_node()) return ARB_edge(from->get_brother(), from, ROOT_EDGE);
911  return ARB_edge(father, from, EDGE_TO_LEAF);
912  }
913  if (is_edge_from_leaf()) return inverse();
914  return ARB_edge(from->get_rightson(), from, EDGE_TO_ROOT);
915  }
916 
917  static int iteration_count(int leafs_in_tree) {
921  return leafs_in_tree<2 ? 0 : leafs_2_edges(leafs_in_tree, UNROOTED) * 2;
922  }
923 
924  bool operator == (const ARB_edge& otherEdge) const {
925  return from == otherEdge.from && to == otherEdge.to;
926  }
927  bool operator != (const ARB_edge& otherEdge) const {
928  return !operator == (otherEdge);
929  }
930 
931  bool is_edge_to_leaf() const {
933  return dest()->is_leaf();
934  }
935  bool is_edge_from_leaf() const {
937  return source()->is_leaf();
938  }
939  bool is_inner_edge() const {
941  return !is_edge_to_leaf() && !is_edge_from_leaf();
942  }
943 
944  void set_root() { son()->set_root(); }
945 
946  void multifurcate();
947 
948 };
949 
954  TreeNode *father = son->get_father();
955  rt_assert(father);
956 
957  if (father->is_root_node()) return ARB_edge(son, son->get_brother(), ROOT_EDGE);
958  return ARB_edge(son, father, EDGE_TO_ROOT);
959 }
960 inline ARB_edge leafEdge(TreeNode *leaf) {
961  rt_assert(leaf->is_leaf());
962  return parentEdge(leaf).inverse();
963 }
964 
965 inline ARB_edge rootEdge(TreeRoot *root) {
966  TreeNode *root_node = root->get_root_node();
967  return ARB_edge(root_node->get_leftson(), root_node->get_rightson(), ROOT_EDGE);
968 }
969 
970 #else
971 #error TreeNode.h included twice
972 #endif // TREENODE_H
ARB_edge(const ARB_edge &otherEdge)
Definition: TreeNode.h:823
void set_bootstrap(double bootstrap)
Definition: TreeNode.cxx:841
void set_branchlength_unrooted(GBT_LEN newlen)
Definition: TreeNode.h:324
bool was_auto_modified() const
Definition: TreeNode.h:120
void compute_tree() OVERRIDE
Definition: TreeNode.h:755
const char * GB_ERROR
Definition: arb_core.h:25
bool is_rightson() const
Definition: TreeNode.h:285
DECLARE_ASSIGNMENT_OPERATOR(ARB_edge)
void unlink_from_DB()
Definition: adtree.cxx:1035
ARB_edge(TreeNode *From, TreeNode *To)
Definition: TreeNode.h:811
virtual ~TreeRoot()
Definition: TreeNode.cxx:22
void destroyNode(TreeNode *node) const OVERRIDE
Definition: TreeNode.h:760
void virtually_distribute_length(GBT_LEN len, TreeNode::LengthCollector &collect) const
Definition: TreeNode.cxx:522
void bootstrap2branchlen()
Definition: TreeNode.cxx:708
bool operator==(const ARB_edge &otherEdge) const
Definition: TreeNode.h:924
virtual void swap_sons()
Definition: TreeNode.h:620
TreeNode * makeNode() const OVERRIDE
Definition: TreeNode.h:759
void destroy()
Definition: TreeNode.h:418
#define implicated(hypothesis, conclusion)
Definition: arb_assert.h:289
TreeNode * findLeafNamed(const char *wantedName)
Definition: TreeNode.cxx:276
MARK_NONFINAL_METHOD(TreeRoot, change_root,(TreeNode *, TreeNode *))
int calc_clade_level() const
Definition: TreeNode.h:611
const TreeNode * get_root_node() const
Definition: TreeNode.h:475
TreeNode * find_parent_clade()
Definition: TreeNode.h:608
bool has_group_info() const
Definition: TreeNode.h:498
virtual void set_root()
Definition: TreeNode.cxx:208
void unlink_from_father()
Definition: TreeNode.h:246
const char * why_not() const
Definition: arb_error.h:171
GBT_LEN get_branchlength_unrooted() const
Definition: TreeNode.h:317
virtual void compute_tree()=0
bool is_clade() const
Definition: TreeNode.h:547
void forget_origin()
Definition: TreeNode.h:466
bool is_edge_from_leaf() const
Definition: TreeNode.h:935
ARB_edge_type get_type() const
Definition: TreeNode.h:833
void setKeeledState(int keeledState)
Definition: TreeNode.h:532
TreeRoot * get_tree_root() const
Definition: TreeNode.h:473
ARB_edge inverse() const
Definition: TreeNode.h:861
TreeNode *& self_ref()
Definition: TreeNode.h:243
GBT_LEN reset_length_and_bootstrap()
Definition: TreeNode.h:632
TreeNode * ancestor_common_with(TreeNode *other)
Definition: TreeNode.h:305
bool remark_will_parse_as_bootstrap(const char *label)
Definition: TreeNode.h:209
ARB_edge previous() const
Definition: TreeNode.h:882
TreeNode * other() const
Definition: TreeNode.h:838
GBT_LEN length() const
Definition: TreeNode.h:840
bool operator!=(const ARB_edge &otherEdge) const
Definition: TreeNode.h:927
char * ARB_strpartdup(const char *start, const char *end)
Definition: arb_string.h:51
TreeNode * son() const
Definition: TreeNode.h:837
const TreeNode * find_parent_with_groupInfo(bool skipKeeledBrothers=false) const
Definition: TreeNode.h:560
ARB_edge rootEdge(TreeRoot *root)
Definition: TreeNode.h:965
void use_as_remark(const SmartCharPtr &newRemark)
Definition: TreeNode.h:367
multifurc_limits(double bootstrap_, double branchlength_, bool applyAtLeafs_)
Definition: TreeNode.h:644
void branchlenXbootstrap()
Definition: TreeNode.cxx:743
void destroy(TreeRoot *viaRoot)
Definition: TreeNode.h:424
bool keelsDownGroup(const TreeNode *toSon) const
Definition: TreeNode.h:521
void reset_branchlengths()
Definition: TreeNode.cxx:680
GBT_LEN leftlen
Definition: TreeNode.h:224
TreeNode * rightson
Definition: TreeNode.h:223
#define DOWNCAST(totype, expr)
Definition: downcast.h:141
SimpleTree(SimpleRoot *sroot)
Definition: TreeNode.h:748
bool has_valid_root_remarks() const
Definition: TreeNode.cxx:914
ARB_edge next() const
Definition: TreeNode.h:871
ARB_edge parentEdge(TreeNode *son)
Definition: TreeNode.h:950
GBT_RemarkType parse_remark(const char *remark, double &bootstrap)
Definition: TreeNode.h:136
CONSTEXPR_INLINE bool is_nan_or_inf(const T &n)
Definition: arbtools.h:178
const TreeNode * get_brother() const
Definition: TreeNode.h:494
void set_root()
Definition: TreeNode.h:944
POS_TREE1 * get_father() const
Definition: probe_tree.h:49
void branchlen2bootstrap()
Definition: TreeNode.cxx:727
ARB_edge(TreeNode *From, TreeNode *To, ARB_edge_type Type)
Definition: TreeNode.h:816
static void destroy(TreeNode *that, TreeRoot *root)
Definition: TreeNode.h:447
const TreeNode * keelTarget() const
Definition: TreeNode.h:518
bool in_other_branch_than(const TreeNode *other) const
Definition: TreeNode.h:300
virtual ~TreeNode()
Definition: TreeNode.h:403
bool is_son_of_root() const
Definition: TreeNode.h:307
const TreeNode * ancestor_common_with(const TreeNode *other) const
Definition: TreeNode.cxx:901
virtual TreeNode * makeNode() const =0
void swap_node_info(TreeNode *other, bool ofKeeledGroups)
Definition: AP_Tree.cxx:577
int keeledStateInfo() const
Definition: TreeNode.h:529
bool is_leftson() const
Definition: TreeNode.h:280
#define true
Definition: ureadseq.h:14
CONSTEXPR_INLINE int leafs_2_edges(int leafs, TreeModel model)
Definition: arbdbt.h:62
ARB_edge leafEdge(TreeNode *leaf)
Definition: TreeNode.h:960
GBT_RemarkType parse_bootstrap(double &bootstrap, WarningConsumer report)
Definition: TreeNode.cxx:763
DEFINE_READ_ACCESSORS(TreeNode *, get_father, father)
#define false
Definition: ureadseq.h:13
void multifurcate()
Definition: TreeNode.cxx:580
TreeNode * father
Definition: TreeNode.h:223
bool is_root_node() const
Definition: TreeNode.h:486
CONSTEXPR_INLINE_Cxx14 void swap(unsigned char &c1, unsigned char &c2)
Definition: ad_io_inline.h:19
bool is_keeled_group() const
Definition: TreeNode.h:542
#define that(thing)
Definition: test_unit.h:1043
bool parse_treelabel(const char *&label, double &bootstrap, char *&remark)
Definition: TreeNode.h:149
bool in_same_branch_as(const TreeNode *other) const
Definition: TreeNode.h:296
void unkeelGroup()
Definition: TreeNode.h:525
TreeNode * get_root_node()
Definition: TreeNode.h:484
TreeNode * get_brother()
Definition: TreeNode.h:489
ARB_edge counter_previous() const
Definition: TreeNode.h:905
static void destroy(TreeNode *that)
Definition: TreeNode.h:444
void multifurcate_whole_tree(const multifurc_limits &below)
Definition: TreeNode.cxx:625
virtual unsigned get_leaf_count() const =0
void rotate_subtree()
Definition: TreeNode.cxx:166
bool at_root() const
Definition: TreeNode.h:399
bool is_son_of(const TreeNode *Father) const
Definition: TreeNode.h:276
CONSTEXPR_INLINE bool valid(SpeciesCreationMode m)
Definition: ed4_class.hxx:2248
TreeRoot(bool deleteWithNodes_)
Definition: TreeNode.h:74
TreeNode * leftson
Definition: TreeNode.h:223
GBT_RemarkType
Definition: arbdbt.h:30
~SimpleTree() OVERRIDE
Definition: TreeNode.h:745
GBT_LEN rightlen
Definition: TreeNode.h:224
TreeNode * find_parent_with_groupInfo(bool skipKeeledBrothers=false)
Definition: TreeNode.h:578
bool is_ancestor_of(const TreeNode *descendant) const
Definition: TreeNode.h:293
int count_clades() const
Definition: TreeNode.cxx:908
void reorder_tree(TreeOrder mode)
Definition: TreeNode.cxx:159
bool knownNonNull(const void *nonnull)
Definition: arb_assert.h:368
GBT_LEN root_distance() const
Definition: TreeNode.h:336
void remove_remark()
Definition: TreeNode.h:376
ARB_edge_type
Definition: TreeNode.h:765
void predelete()
Definition: TreeNode.h:61
bool has_no_remark() const
Definition: TreeNode.h:381
bool has_bootstrap() const
Definition: TreeNode.h:113
TreeNode * dest() const
Definition: TreeNode.h:835
#define rt_assert(cond)
Definition: TreeNode.h:22
bool is_edge_to_leaf() const
Definition: TreeNode.h:931
bool is_leaf() const
Definition: TreeNode.h:263
void set_branchlength_preserving(GBT_LEN new_len)
Definition: TreeNode.cxx:598
TreeNode * fixDeletedSon()
Definition: TreeNode.cxx:855
#define gb_assert(cond)
Definition: arbdbt.h:11
void multifurcate()
Definition: TreeNode.cxx:590
xml element
GBT_LEN intree_distance_to(const TreeNode *other) const
Definition: TreeNode.h:340
bool is_inner_node_with_remark() const
Definition: TreeNode.h:366
bool is_inside(const TreeNode *subtree) const
Definition: TreeNode.h:290
TreeNode(TreeRoot *root)
Definition: TreeNode.h:434
DEFINE_READ_ACCESSORS(TreeNode *, get_root_node, rootNode)
#define OVERRIDE
Definition: cxxforward.h:112
char * name
Definition: TreeNode.h:226
void announce_tree_constructed()
Definition: TreeNode.h:455
SimpleRoot()
Definition: TreeNode.h:758
void fixKeeledOrientation()
Definition: TreeNode.h:254
void scale_branchlengths(double factor)
Definition: TreeNode.cxx:689
void set_branchlength(GBT_LEN newlen)
Definition: TreeNode.h:312
TreeOrder
Definition: TreeNode.h:35
void set_tree_root(TreeRoot *new_root)
Definition: TreeNode.cxx:86
void set_remark(const char *newRemark)
Definition: TreeNode.h:372
GBT_LEN sum_child_lengths() const
Definition: TreeNode.cxx:699
void remove_bootstrap()
Definition: TreeNode.cxx:646
ARB_edge counter_next() const
Definition: TreeNode.h:894
float GBT_LEN
Definition: arbdb_base.h:34
bool is_inner_edge() const
Definition: TreeNode.h:939
#define NULp
Definition: cxxforward.h:116
GB_ERROR apply_aci_to_remarks(const char *aci, const GBL_call_env &callEnv)
Definition: TreeNode.cxx:654
ARB_edge find_innermost_edge()
Definition: TreeNode.cxx:397
void markAsLeaf()
Definition: TreeNode.h:264
TreeNode * keelTarget()
Definition: TreeNode.h:515
GBT_LEN get_branchlength() const
Definition: TreeNode.h:311
virtual void destroyNode(TreeNode *node) const =0
void destroy(TreeNode *that)
Definition: TreeNode.h:667
GBDATA * gb_node
Definition: TreeNode.h:225
GBT_LEN eliminate()
Definition: TreeNode.h:853
virtual void change_root(TreeNode *old, TreeNode *newroot)
Definition: TreeNode.cxx:28
void set_length(GBT_LEN len)
Definition: TreeNode.h:844
const char * get_remark() const
Definition: TreeNode.h:357
const TreeNode * find_parent_clade() const
Definition: TreeNode.h:582
const SmartCharPtr & get_remark_ptr() const
Definition: TreeNode.h:362
void set_bootstrap_seen(bool seen)
Definition: TreeNode.h:116
void delete_by_node()
Definition: TreeNode.h:106
void forget_relatives()
Definition: TreeNode.h:467
void(* WarningConsumer)(const char *message)
Definition: arbdbt.h:84
const char * get_group_name() const
Definition: TreeNode.h:553
unsigned get_leaf_count() const OVERRIDE
Definition: TreeNode.h:751
bool is_normal_group() const
Definition: TreeNode.h:537
void set_auto_modified(bool modified)
Definition: TreeNode.h:123
TreeNode * source() const
Definition: TreeNode.h:834
const char * label
static int iteration_count(int leafs_in_tree)
Definition: TreeNode.h:917