37 if (node->is_leaf()) {
38 out << ((
void *)node) <<
'(' << node->name <<
')';
41 static int notTooDeep;
44 out << ((
void *)node);
45 if (!node->father) out <<
" (ROOT)";
50 out <<
"NODE(" << ((
void *)node);
56 out <<
", father=" << node->father;
59 out <<
", leftson=" << node->leftson
60 <<
", rightson=" << node->rightson
61 <<
", edge[0]=" << node->edge[0]
62 <<
", edge[1]=" << node->edge[1]
63 <<
", edge[2]=" << node->edge[2]
73 int AP_tree_nlen::unusedEdgeIndex()
const {
74 for (
int e=0; e<3; e++)
if (!edge[e])
return e;
78 AP_tree_edge* AP_tree_nlen::edgeTo(
const AP_tree_nlen *neighbour)
const {
79 for (
int e=0; e<3; e++) {
80 if (edge[e] && edge[e]->
node[1-index[e]]==neighbour) {
94 return edge[afterThatEdge ? ((indexOf(afterThatEdge)+1) % 3) : 0];
102 *edgePtr1 = edge[0]->
unlink();
103 *edgePtr2 = edge[1]->
unlink();
104 *edgePtr3 = edge[2]->
unlink();
113 edge2->
relink(
this, get_leftson());
114 edge3->
relink(
this, get_rightson());
120 #if defined(PROVIDE_TREE_STRUCTURE_TESTS)
123 #define DUMP_INVALID_SUBTREES
126 #if defined(DEVEL_RALF)
127 #define CHECK_CORRECT_INVALIDATION // recombines all up-to-date nodes to find missing invalidations (VERY slow)
131 #if defined(DUMP_INVALID_SUBTREES)
132 inline void dumpSubtree(
const char *msg,
const AP_tree_nlen *
node) {
133 fprintf(stderr,
"%s:\n", msg);
135 fputs(printable, stderr);
141 inline const AP_tree_edge *edge_between(
const AP_tree_nlen *node1,
const AP_tree_nlen *node2) {
144 #if defined(ASSERTION_USED)
152 inline const char *no_valid_edge_between(
const AP_tree_nlen *node1,
const AP_tree_nlen *node2) {
156 if (edge_12 == edge_21) {
157 return edge_12 ?
NULp :
"edge missing";
159 return "edge inconsistent";
162 #if defined(DUMP_INVALID_SUBTREES)
163 #define PRINT_BAD_EDGE(msg,node) dumpSubtree(msg,node)
164 #else // !defined(DUMP_INVALID_SUBTREES)
165 #define PRINT_BAD_EDGE(msg,node) fprintf(stderr, "Warning: %s (at node=%p)\n", (msg), (node))
168 #define SHOW_BAD_EDGE(format,str,node) do{ \
169 char *msg = GBS_global_string_copy(format,str); \
170 PRINT_BAD_EDGE(msg, node); \
174 Validity AP_tree_nlen::has_valid_edges()
const {
179 const char *invalid = no_valid_edge_between(
this, get_brother());
181 SHOW_BAD_EDGE(
"root-%s. root", invalid,
get_father());
182 valid =
Validity(
false,
"no valid edge between sons of root");
185 const char *invalid = no_valid_edge_between(
this,
get_father());
186 if (!invalid || !strstr(invalid,
"missing")) {
187 SHOW_BAD_EDGE(
"unexpected edge (%s) between root and son", invalid ? invalid :
"valid",
this);
188 valid =
Validity(
false,
"unexpected edge between son-of-root and root");
192 const char *invalid = no_valid_edge_between(
this,
get_father());
194 SHOW_BAD_EDGE(
"son-%s. father", invalid,
get_father());
195 SHOW_BAD_EDGE(
"parent-%s. son", invalid,
this);
196 valid =
Validity(
false,
"invalid edge between son and father");
202 if (valid) valid = get_leftson()->has_valid_edges();
203 if (valid) valid = get_rightson()->has_valid_edges();
208 Validity AP_tree_nlen::sequence_state_valid()
const {
217 bool leftson_hasSequence = get_leftson()->hasSequence();
218 bool rightson_hasSequence = get_rightson()->hasSequence();
220 #if defined(DUMP_INVALID_SUBTREES)
221 if (!leftson_hasSequence) dumpSubtree(
"left subtree has no sequence", get_leftson());
222 if (!rightson_hasSequence) dumpSubtree(
"right subtree has no sequence", get_rightson());
223 if (!(leftson_hasSequence && rightson_hasSequence)) {
224 dumpSubtree(
"while father HAS sequence",
this);
228 valid =
Validity(leftson_hasSequence && rightson_hasSequence,
"node has sequence and son w/o sequence");
230 #if defined(CHECK_CORRECT_INVALIDATION)
238 valid =
Validity(recombined->
combinedEquals(sequence),
"recombining changed existing sequence (missing invalidation?)");
240 Mutations expected_mutrate = mutations_from_combine + get_leftson()->mutations + get_rightson()->mutations;
241 valid =
Validity(expected_mutrate == mutations,
"invalid mutation_rate");
246 #if defined(DUMP_INVALID_SUBTREES)
248 dumpSubtree(valid.
why_not(),
this);
255 #if defined(ASSERTION_USED)
263 if (valid) valid = get_leftson()->sequence_state_valid();
264 if (valid) valid = get_rightson()->sequence_state_valid();
270 Validity AP_tree_nlen::is_valid()
const {
273 Validity valid = AP_tree::is_valid();
274 if (valid) valid = has_valid_edges();
275 if (valid) valid = sequence_state_valid();
280 #endif // PROVIDE_TREE_STRUCTURE_TESTS
294 for (AP_tree_nlen *upnode = nodeBelow->get_father();
296 upnode = upnode->get_father())
303 if ((*e1)->Age() > (*e2)->Age()) {
314 void AP_tree_nlen::initial_insert(AP_tree_nlen *newBrother, AP_pars_root *troot) {
328 void AP_tree_nlen::insert(AP_tree_nlen *newBrother) {
339 AP_tree_nlen *brothersFather = newBrother->get_father();
340 if (brothersFather) {
344 if (brothersFather->get_father()) {
345 AP_tree_edge *oldEdge = newBrother->edgeTo(newBrother->get_father())->unlink();
350 AP_tree_nlen *brothersOldBrother = newBrother->get_brother();
366 AP_tree_nlen *lson = newBrother->get_leftson();
367 AP_tree_nlen *rson = newBrother->get_rightson();
376 oldEdge->
relink(
this, newBrother);
385 AP_tree_nlen *AP_tree_nlen::REMOVE() {
399 AP_tree_nlen *oldBrother = get_brother();
420 if (grandPa->father) {
421 oldEdge =
get_father()->edgeTo(grandPa)->unlink();
423 oldEdge->
relink(oldBrother, grandPa);
426 AP_tree_nlen *uncle =
get_father()->get_brother();
429 oldEdge =
get_father()->edgeTo(uncle)->unlink();
431 oldEdge->
relink(oldBrother, uncle);
439 if (oldBrother->is_leaf()) {
450 #if defined(ASSERTION_USED)
451 AP_pars_root *troot = get_tree_root();
452 #endif // ASSERTION_USED
468 AP_tree_nlen *lson = oldBrother->get_leftson();
469 AP_tree_nlen *rson = oldBrother->get_rightson();
480 oldEdge = oldBrother->edgeTo(rson)->
unlink();
482 oldEdge->relink(lson, rson);
484 ap_assert(lson->get_tree_root()->get_root_node() == oldBrother);
496 void AP_tree_nlen::swap_sons() {
513 AP_tree_nlen *oldBrother = get_brother();
514 AP_tree_nlen *movedSon = mode ==
AP_LEFT ? get_leftson() : get_rightson();
520 if (!oldBrother->is_leaf()) {
521 AP_tree_nlen *nephew = oldBrother->get_leftson();
533 swap(leftson->father, nephew->father);
534 swap(leftson, oldBrother->leftson);
537 swap(rightson->father, nephew->father);
538 swap(rightson, oldBrother->leftson);
541 edge2->
relink(
this, nephew);
542 edge1->
relink(oldBrother, movedSon);
544 if (nephew->gr.mark_sum != movedSon->gr.mark_sum) {
545 get_brother()->recalc_marked_from_sons();
546 this->recalc_marked_from_sons_and_forward_upwards();
562 swap(leftson->father, oldBrother->father);
563 if (
father->leftson ==
this) {
571 swap(rightson->father, oldBrother->father);
572 if (
father->leftson ==
this) {
580 edge2->
relink(
this, oldBrother);
583 if (oldBrother->gr.mark_sum != movedSon->gr.mark_sum) {
584 recalc_marked_from_sons_and_forward_upwards();
589 void AP_tree_nlen::set_root() {
590 if (at_root())
return;
596 AP_tree_nlen *son_of_root =
NULp;
597 AP_tree_nlen *old_root =
NULp;
610 AP_tree_nlen *other_son_of_root = son_of_root->get_brother();
615 ap_assert(old_root->has_valid_root_remarks());
618 for (AP_tree_nlen *node = son_of_root; node ; node = node->get_father()) {
619 node->recalc_marked_from_sons();
623 void AP_tree_nlen::moveNextTo(AP_tree_nlen *newBrother,
AP_FLOAT rel_pos) {
644 if (grandpa->father) {
656 if (!get_brother()->
is_leaf()) {
663 if (newBrother->father) {
664 AP_tree_nlen *newBrothersFather = newBrother->get_father();
666 if (newBrothersFather->is_son_of_root()) {
670 if (newBrother->is_son_of_root()) {
677 AP_tree_nlen *grandFather = thisFather->get_father();
678 AP_tree_nlen *oldBrother = get_brother();
679 AP_tree_nlen *newBrothersFather = newBrother->get_father();
682 if (thisFather==newBrothersFather->get_father()) {
684 if (grandFather->get_father()) {
686 thisFather->unlinkAllEdges(&e1, &e2, &e3);
692 e1->
relink(oldBrother, grandFather);
693 thisFather->linkAllEdges(e2, e3, e4);
697 AP_tree_nlen *uncle = thisFather->get_brother();
699 thisFather->unlinkAllEdges(&e1, &e2, &e3);
705 e1->
relink(oldBrother, uncle);
706 thisFather->linkAllEdges(e2, e3, e4);
711 oldBrother->unlinkAllEdges(&e1, &e2, &e3);
713 thisFather->linkAllEdges(e1, e2, e3);
716 else if (grandFather==newBrothersFather) {
717 if (grandFather->father) {
719 thisFather->unlinkAllEdges(&e1, &e2, &e3);
725 e1->
relink(oldBrother, grandFather);
726 thisFather->linkAllEdges(e2, e3, e4);
739 oldBrother->unlinkAllEdges(&e1, &e2, &e3);
745 e1->
relink(oldBrother->get_leftson(), oldBrother->get_rightson());
746 thisFather->linkAllEdges(e2, e3, e4);
748 else if (!grandFather->get_father()) {
749 if (newBrothersFather->is_son_of_root()) {
750 thisFather->unlinkAllEdges(&e1, &e2, &e3);
756 e1->
relink(oldBrother, newBrothersFather);
757 thisFather->linkAllEdges(e2, e3, e4);
760 AP_tree_nlen *uncle = thisFather->get_brother();
762 thisFather->unlinkAllEdges(&e1, &e2, &e3);
768 e1->
relink(oldBrother, uncle);
769 thisFather->linkAllEdges(e2, e3, e4);
773 if (!newBrothersFather->get_father()) {
774 AP_tree_nlen *newBrothersBrother = newBrother->get_brother();
776 thisFather->unlinkAllEdges(&e1, &e2, &e3);
782 e1->
relink(oldBrother, grandFather);
783 thisFather->linkAllEdges(e2, e3, e4);
786 thisFather->unlinkAllEdges(&e1, &e2, &e3);
792 e1->
relink(oldBrother, grandFather);
793 thisFather->linkAllEdges(e2, e3, e4);
805 void AP_tree_nlen::unhash_sequence() {
814 bool AP_tree_nlen::acceptCurrentState(
Level frame_level) {
819 if (remembered_for_frame != frame_level) {
827 Level next_frame_level = frame_level-1;
830 if (!next_frame_level) {
833 else if (stored_frame_level == next_frame_level) {
844 if (common != state->
mode) {
859 remembered_for_frame = next_frame_level;
874 if (remembered_for_frame == frame_level) {
877 if (0 == (mode & ~is_stored->
mode)) {
886 store =
new NodeState(remembered_for_frame);
889 remembered_for_frame = frame_level;
898 store->
leftson = get_leftson();
902 store->
root = get_tree_root();
908 for (
int e=0; e<3; e++) {
909 store->
edge[e] = edge[e];
916 if (!(store->
mode & SEQUENCE)) {
939 if ((what & (STRUCTURE|SEQUENCE)) && !(target.
mode & (STRUCTURE|SEQUENCE))) {
942 if (what & STRUCTURE) {
953 for (
int e=0; e<3; e++) {
954 target.
edge[e] = edge[e];
958 if (what & SEQUENCE) {
968 void AP_tree_nlen::restore_structure(
const NodeState& state) {
974 set_tree_root(state.
root);
982 for (
int e=0; e<3; e++) {
983 edge[e] = state.
edge[e];
986 edge[e]->index[index[e]] = e;
987 edge[e]->node[index[e]] =
this;
991 void AP_tree_nlen::restore_sequence(
NodeState& state) {
997 void AP_tree_nlen::restore_sequence_nondestructive(
const NodeState& state) {
1001 void AP_tree_nlen::restore_root(
const NodeState& state) {
1002 state.
root->change_root(state.
root->get_root_node(),
this);
1005 void AP_tree_nlen::restore(
NodeState& state) {
1008 if (mode&STRUCTURE) restore_structure(state);
1009 if (mode&SEQUENCE) restore_sequence(state);
1010 if (
ROOT==mode) restore_root(state);
1012 void AP_tree_nlen::restore_nondestructive(
const NodeState& state) {
1015 if (mode&STRUCTURE) restore_structure(state);
1016 if (mode&SEQUENCE) restore_sequence_nondestructive(state);
1017 if (
ROOT==mode) restore_root(state);
1021 ap_assert(remembered_for_frame == curr_frameLevel);
1024 #if defined(ASSERTION_USED)
1032 remembered_for_frame = previous->
frameNr;
1036 void AP_tree_nlen::parsimony_rec(
char *mutPerSite) {
1045 sequence = set_seq(get_tree_root()->get_seqTemplate()->dup());
1050 AP_tree_nlen *lson = get_leftson();
1051 AP_tree_nlen *rson = get_rightson();
1056 lson->parsimony_rec(mutPerSite);
1057 rson->parsimony_rec(mutPerSite);
1066 mutations = lson->mutations + rson->mutations + mutations_for_combine;
1071 Mutations AP_tree_nlen::costs(
char *mutPerSite) {
1074 ap_assert(get_tree_root()->get_seqTemplate());
1077 parsimony_rec(mutPerSite);
1083 return rootEdge()->nni_rec(whichEdges, mode,
NULp,
true);
1112 if (rec_depth == 0) {
1119 AP_tree_nlen *son = sonNode();
1120 AP_tree_nlen *notSon = otherNode(son);
1122 descend[0] = notSon->nextEdge(
this);
1123 descend[2] = notSon->nextEdge(descend[0]);
1127 descend[4] = son->nextEdge(
this);
1128 descend[6] = son->nextEdge(descend[4]);
1133 descend[1] = descend[0];
1134 descend[3] = descend[2];
1135 descend[5] = descend[4];
1136 descend[7] = descend[6];
1146 int rec_width_dynamic = 0;
1147 int visited_subtrees = 0;
1148 int better_subtrees = 0;
1152 #if defined(ASSERTION_USED)
1153 int forbidden_descends = 0;
1157 for (
int i = 0; i < 8; i++) {
1163 !subedge->kl_visited &&
1170 if (pars[i] < pars_best) {
1172 pars_best = pars[i];
1174 if (pars[i] < schwellwert) {
1175 rec_width_dynamic++;
1182 #if defined(ASSERTION_USED)
1183 if (subedge && subedge->kl_visited) {
1184 forbidden_descends++;
1193 for (
int i=7, t=0; t<i; t++) {
1202 for (
int t = visited_subtrees - 1; t > 0; t--) {
1203 bool bubbled =
false;
1204 for (
int i = 0; i < t; i++) {
1205 if (pars[i] > pars[i+1]) {
1211 if (!bubbled)
break;
1215 #if defined(ASSERTION_USED)
1221 if (!is_root_edge()) {
1222 switch (rec_depth) {
1238 switch (rec_depth) {
1254 if (better_subtrees) {
1255 rec_width = better_subtrees;
1262 rec_width = visited_subtrees;
1265 rec_width =
std::min(rec_width, rec_width_static);
1268 rec_width =
std::min(rec_width, rec_width_dynamic);
1273 bool found_better =
false;
1274 for (
int i=0; i<rec_width && !found_better; i++) {
1278 subedge->kl_visited =
true;
1282 if (better_subtrees) {
1287 subedge->
kl_rec(modified, rec_depth+1, pars_best);
1288 found_better =
true;
1291 found_better = subedge->
kl_rec(KL, rec_depth+1, pars_best);
1294 subedge->kl_visited =
false;
1299 return found_better;
1302 void AP_tree_nlen::exchange(AP_tree_nlen *other) {
1321 AP_tree_nlen *connected = myEdge->
otherNode(
this);
1328 father->rightson = other;
1333 other->set_tree_root(root);
1334 set_tree_root(
NULp);
1336 myEdge->
relink(other, connected);
1342 const char* AP_tree_nlen::sortByName() {
1343 if (name)
return name;
1345 const char *n1 = get_leftson()->sortByName();
1346 const char *n2 = get_rightson()->sortByName();
1348 if (strcmp(n1, n2)<0)
return n1;
1355 const char *AP_tree_nlen::fullname()
const {
1358 char *lName =
ARB_strdup(get_leftson()->fullname());
1359 char *rName =
ARB_strdup(get_rightson()->fullname());
1360 int len = strlen(lName)+strlen(rName)+4;
1362 if (buffer) free(buffer);
1366 strcpy(buffer,
"[");
1367 strcat(buffer, lName);
1368 strcat(buffer,
",");
1369 strcat(buffer, rName);
1370 strcat(buffer,
"]");
1382 char* AP_tree_nlen::getSequenceCopy() {
1385 AP_sequence_parsimony *pseq =
DOWNCAST(AP_sequence_parsimony*, get_seq());
1388 size_t len = pseq->get_sequence_length();
1389 char *
s =
new char[len];
1390 memcpy(s, pseq->get_sequence(), len);
1396 GB_ERROR AP_pars_root::saveToDB() {
1397 has_been_saved =
true;
ostream & operator<<(ostream &out, const AP_tree_nlen *node)
void sortOldestFirst(AP_tree_edge **e1, AP_tree_edge **e2)
#define implicated(hypothesis, conclusion)
CONSTEXPR_INLINE AP_TREE_SIDE idx2side(const int idx)
virtual void moveNextTo(AP_tree *new_brother, AP_FLOAT rel_pos)
const char * why_not() const
Mutations nni_rec(EdgeSpec whichEdges, AP_BL_MODE mode, AP_tree_nlen *skipNode, bool includeStartEdge)
char * GBT_tree_2_newick(const TreeNode *tree, NewickFormat format, bool compact)
POS_TREE1 * get_father() const
char * ARB_strdup(const char *str)
AP_tree_nlen * sonNode() const
void relink(AP_tree_nlen *node1, AP_tree_nlen *node2)
ARB_edge rootEdge(TreeRoot *root)
void swap_sons() OVERRIDE
int rec_width[CUSTOM_DEPTHS]
char buffer[MESSAGE_BUFFERSIZE]
virtual void insert(AP_tree *new_brother)
#define DOWNCAST(totype, expr)
virtual AP_tree * REMOVE()
void ensure_sequence_loaded() const
bool push_node(AP_tree_nlen *node, AP_STACK_MODE)
POS_TREE1 * get_father() const
void push_all_upnode_sequences(AP_tree_nlen *nodeBelow)
TYPE * ARB_alloc(size_t nelem)
virtual AP_sequence * dup() const =0
AP_tree_edge * makeEdge(AP_tree_nlen *n1, AP_tree_nlen *n2)
bool is_leaf_edge() const
void accept_if(bool cond)
virtual AP_combinableSeq * dup() const =0
bool is_bound_to_species() const
CONSTEXPR_INLINE_Cxx14 void swap(unsigned char &c1, unsigned char &c2)
AP_tree_nlen * rootNode()
CONSTEXPR_INLINE bool valid(SpeciesCreationMode m)
Mutations noncounting_combine_seq(const AP_combinableSeq *lefts, const AP_combinableSeq *rights)
bool next_to_folded_group() const
KL_RECURSION_TYPE rec_type
bool combinedEquals(const AP_combinableSeq *other) const
GB_ERROR saveToDB() OVERRIDE
bool knownNonNull(const void *nonnull)
QuadraticThreshold thresFunctor
virtual void initial_insert(AP_tree *new_brother, AP_tree_root *troot)
fputs(TRACE_PREFIX, stderr)
#define IF_ASSERTION_USED(x)
AP_tree_nlen * otherNode(const AP_tree_nlen *n) const
virtual Mutations combine_seq(const AP_combinableSeq *lefts, const AP_combinableSeq *rights, char *mutation_per_site=NULp)=0
double calculate(double x) const
#define ASSERT_VALID_TREE(tree)
bool kl_rec(const KL_params &KL, const int rec_depth, Mutations pars_best)
void destroyEdge(AP_tree_edge *edge)
void move_info_to(NodeState &target, AP_STACK_MODE what)
virtual AP_UPDATE_FLAGS check_update()
GB_write_int const char s