23 long AP_tree_edge::timeStamp = 0;
26 : next_in_chain(
NULp),
45 if (!node->is_leaf()) {
61 while (tree->get_father()) tree = tree->get_father();
68 new AP_tree_edge(tree->get_leftson(), tree->get_rightson());
76 edge = tree->get_leftson()->edgeTo(tree->get_rightson());
107 node1->edge[index[0] = node1->unusedEdgeIndex()] =
this;
108 node2->edge[index[1] = node2->unusedEdgeIndex()] =
this;
110 node1->index[index[0]] = 0;
111 node2->index[index[1]] = 1;
114 size_t AP_tree_edge::buildChainInternal(
EdgeSpec whichEdges,
bool depthFirst,
const AP_tree_nlen *skip,
AP_tree_edge **&prevNextPtr) {
135 if (use && !depthFirst) {
137 next_in_chain =
NULp;
138 prevNextPtr = &next_in_chain;
142 for (
int n=0; n<2; n++) {
144 for (
int e=0; e<3; e++) {
148 if (descend && (whichEdges&SKIP_UNMARKED_EDGES)) descend = Edge->
has_marked();
151 added += Edge->buildChainInternal(whichEdges, depthFirst,
node[n], prevNextPtr);
158 if (use && depthFirst) {
162 next_in_chain =
NULp;
163 prevNextPtr = &next_in_chain;
170 bool EdgeChain::exists =
false;
184 #if defined(DEVEL_RALF)
185 # if defined(ASSERTION_USED) || defined(UNIT_TESTS)
186 if (whichEdges & SKIP_UNMARKED_EDGES) {
187 AP_tree_nlen *son = startEgde->
sonNode();
188 bool flags_valid = son->has_correct_mark_flags();
190 flags_valid = startEgde->
otherNode(son)->has_correct_mark_flags();
193 GBK_terminate(
"detected invalid flags while building chain");
204 len = startEgde->buildChainInternal(whichEdges, depthFirst, skip, prev);
208 if (prev == &startEgde->next_in_chain) {
209 if (start == startEgde) {
215 for (
int n = 0; n<=1; ++n) {
216 AP_tree_edge *e1 = startEgde->node[n]->nextEdge(startEgde);
217 if (e1->next_in_chain == startEgde) e1->next_in_chain =
NULp;
219 if (e2->next_in_chain == startEgde) e2->next_in_chain =
NULp;
223 #if defined(ASSERTION_USED)
239 if (start == startEgde) {
240 start = start->next_in_chain;
265 return Data+block*length;
273 int sum[3] = { 0, 0, 0 };
274 for (
int i=0; i<seq_len; i++) {
275 int diff = ne[i] - old[i];
276 if (diff > 1 || diff < -1) {
278 fprintf(stderr,
"diff by nni at one position not in [-1,1]: %i:%i - %i", diff, old[i], ne[i]);
288 for (
int i=0; i<3; i++) asum += sum[i];
292 for (
int i=0; i<3; i++) {
293 freq[i] = sum[i] / double(asum);
295 log_freq[i] = log(freq[i]);
298 log_freq[i] = -1e100;
304 double log_eps = log(1e-11);
307 for (
int tsum_add = 1; tsum_add >= -1; tsum_add -= 2) {
308 int tsum = sum[2]-sum[0];
309 if (tsum <= 0) tsum = 1;
310 for (; tsum < max && tsum > 0; tsum += tsum_add) {
311 if (tsum_add < 0 && tsum == sum[2]-sum[0])
continue;
316 for (
int minus_add = 1; minus_add>=-1; minus_add-=2) {
318 for (
int n_minus = sum[0]; n_minus<max_minus && n_minus>=0; first_minus = 0, n_minus+=minus_add) {
319 if (minus_add < 0 && first_minus)
continue;
320 int n_zeros = seq_len - n_minus * 2 - tsum;
321 int n_plus = tsum + n_minus;
324 n_minus * log_freq[0] +
325 n_zeros * log_freq[1] +
326 n_plus * log_freq[2] +
329 if (log_a < log_eps) {
330 if (first_minus && minus_add>0)
goto end;
333 double a = exp(log_a);
345 if (!son_node->is_leaf()) {
346 size_t seq_len = son_node->get_seq()->get_sequence_length();
354 if (two<one) one = two;
357 double bootstrap = one<1.0 ? 100.0 * one : 100.0;
358 son_node->set_bootstrap(bootstrap);
359 son_node->get_brother()->use_as_remark(son_node->get_remark_ptr());
366 if (leaf->is_leaf() &&
370 AP_FLOAT Seq_len = leaf->get_seq()->weighted_base_count();
371 if (Seq_len <= 1.0) Seq_len = 1.0;
382 GBT_LEN blen = mutations/Seq_len;
383 leaf->set_branchlength_unrooted(blen);
387 void AP_tree_edge::set_inner_branch_length_and_calc_adj_leaf_lengths(
AP_FLOAT bcosts) {
396 ap_assert(son->get_seq()->hasSequence());
397 ap_assert(otherSon->get_seq()->hasSequence());
400 (son ->get_seq()->weighted_base_count() +
401 otherSon->get_seq()->weighted_base_count()
404 if (seq_len < 0.1) seq_len = 0.1;
408 son->set_branchlength_unrooted(blen);
419 #if defined(ASSERTION_USED) || defined(UNIT_TESTS)
422 if (tree->get_branchlength_unrooted() ==
AP_UNDEF_BL)
return false;
424 if (tree->is_leaf())
return true;
450 if (recalc_lengths) {
459 EdgeChain chain(
this, whichEdges, !recalc_lengths, skipNode, includeStartEdge);
462 if (recalc_lengths) {
470 rootNode()->leftlen = AP_UNDEF_BL *.5;
471 rootNode()->rightlen = AP_UNDEF_BL *.5;
475 while (chain && (recalc_lengths || !progress.aborted())) {
497 new_parsimony =
rootNode()->costs();
499 return new_parsimony;
512 son->unhash_sequence();
513 son->get_father()->unhash_sequence();
516 brother->unhash_sequence();
519 pars_one = root->costs(mps->
data(0));
521 #if defined(ASSERTION_USED)
532 pars_two = root->costs(mps ? mps->
data(1) :
NULp);
534 if (pars_two <= parsbest) {
547 pars_three = root->costs(mps ? mps->
data(2) :
NULp);
549 if (pars_three <= parsbest) {
551 parsbest = pars_three;
559 GBT_LEN bcosts = (pars_one + pars_two + pars_three) - (3.0 * parsbest);
560 if (bcosts <0) bcosts = 0;
563 set_inner_branch_length_and_calc_adj_leaf_lengths(bcosts);
573 static int notTooDeep;
577 if (notTooDeep || !e) {
582 out <<
"AP_tree_edge(" << ((
void*)e)
583 <<
", node[0]=" << e->node[0]
584 <<
", node[1]=" << e->node[1]
594 long edges = chain.
size();
597 while (repeat-- && !progress.
aborted()) {
602 if (percent>=100 ||
GB_random(100)<percent) {
622 void TEST_edgeChain() {
627 AP_tree_nlen *rootN = root->sonNode()->get_father();
630 AP_tree_nlen *leftSon = rootN->get_leftson();
631 AP_tree_nlen *rightSon = rootN->get_rightson();
633 const size_t ALL_EDGES = 27;
634 const size_t LEAF_EDGES = 15;
645 const size_t MV_RIGHT = 8;
646 const size_t MV_LEFT = 6;
647 const size_t MARKED_VIS = MV_RIGHT + MV_LEFT - 1;
649 const EdgeSpec MARKED_VISIBLE_EDGES =
EdgeSpec(SKIP_UNMARKED_EDGES|SKIP_FOLDED_EDGES);
658 const size_t V_RIGHT = 9;
659 const size_t V_LEFT = 12;
660 const size_t VISIBLE = V_RIGHT + V_LEFT -1;
668 AP_tree_edge *subtreeEdge = rightSon->edgeTo(rightSon->get_leftson());
669 AP_tree_nlen *stFather = subtreeEdge->
notSonNode();
684 subtreeEdge = leftSon->edgeTo(leftSon->get_leftson());
698 leftSon->gr.grouped =
true;
705 rightSon->gr.grouped =
true;
712 leftSon->gr.grouped =
false;
719 rightSon->gr.grouped =
false;
769 void TEST_tree_flags_needed_by_EdgeChain() {
793 AP_tree_nlen *CorGluta =
rootNode()->findLeafNamed(
"CorGluta");
794 AP_tree_nlen *CelBiazo =
rootNode()->findLeafNamed(
"CelBiazo");
795 AP_tree_nlen *CurCitre =
rootNode()->findLeafNamed(
"CurCitre");
796 AP_tree_nlen *CloTyro2 =
rootNode()->findLeafNamed(
"CloTyro2");
797 AP_tree_nlen *CloCarni =
rootNode()->findLeafNamed(
"CloCarni");
799 AP_tree_nlen *CurCitre_father = CurCitre->get_father();
800 AP_tree_nlen *CurCitre_grandfather = CurCitre_father->get_father();
806 CorGluta->set_root();
809 CelBiazo->set_root();
812 CloTyro2->set_root();
816 CurCitre->set_root();
819 CurCitre_father->set_root();
822 CurCitre_grandfather->set_root();
837 CorGluta->moveNextTo(CurCitre, 0.5);
843 CurCitre_father->moveNextTo(CelBiazo, 0.5);
849 CurCitre_father->moveNextTo(CloCarni, 0.5);
855 CurCitre->moveNextTo(CloCarni, 0.5);
861 CorGluta->moveNextTo(CloTyro2, 0.5);
884 CorGluta->get_father()->swap_assymetric(
AP_RIGHT);
889 CorGluta->get_father()->swap_assymetric(
AP_LEFT);
896 AP_tree_nlen *CloTyro2_father = CloTyro2->get_father();
898 CloTyro2_father->swap_assymetric(
AP_LEFT);
903 AP_tree_nlen *CloTyro2_grandfather = CloTyro2_father->get_father();
904 ap_assert(CloTyro2_grandfather->gr.grouped);
906 CloTyro2_grandfather->swap_assymetric(
AP_LEFT);
911 CloTyro2_grandfather->swap_assymetric(
AP_RIGHT);
918 CloCarni->get_father()->swap_assymetric(
AP_LEFT);
927 void TEST_undefined_branchlength() {
931 AP_tree_nlen *root = env.root_node();
932 AP_tree_nlen *CorAquat = root->findLeafNamed(
"CorAquat");
933 AP_tree_nlen *inner = CorAquat->get_father()->get_father();
935 AP_tree_nlen *sonOfRoot = root->get_leftson();
945 AP_tree_nlen *nodes[] = {
956 AP_tree_nlen *
node = nodes[n];
957 GBT_LEN oldLen = node->get_branchlength_unrooted();
959 node->set_branchlength_unrooted(testLen);
962 node->set_branchlength_unrooted(oldLen);
963 TEST_EXPECT(node->get_branchlength_unrooted() == oldLen);
bool allBranchlengthsAreDefined(AP_tree_nlen *tree)
#define implicated(hypothesis, conclusion)
GB_ERROR GBT_restore_marked_species(GBDATA *gb_main, const char *stored_marked)
long GBT_mark_all(GBDATA *gb_main, int flag)
AP_tree_edge(AP_tree_nlen *node1, AP_tree_nlen *node2)
Mutations nni_rec(EdgeSpec whichEdges, AP_BL_MODE mode, AP_tree_nlen *skipNode, bool includeStartEdge)
AP_tree_nlen * sonNode() const
const char * GBS_global_string(const char *templat,...)
void relink(AP_tree_nlen *node1, AP_tree_nlen *node2)
ARB_edge rootEdge(TreeRoot *root)
#define ARRAY_ELEMS(array)
ostream & operator<<(ostream &out, const AP_tree_edge *e)
EdgeChain(AP_tree_edge *start_, EdgeSpec whichEdges, bool depthFirst, const AP_tree_nlen *skip=NULp, bool includeStart=true)
static HelixNrInfo * start
bool push_node(AP_tree_nlen *node, AP_STACK_MODE)
static int diff(int v1, int v2, int v3, int v4, int st, int en)
static void buildSonEdges(AP_tree_nlen *node)
#define TEST_EXPECT(cond)
static void destroy(AP_tree_nlen *root)
void GBK_terminate(const char *error) __ATTR__NORETURN
bool is_leaf_edge() const
void accept_if(bool cond)
void update_undefined_leaf_branchlength(AP_tree_nlen *leaf)
AP_tree_nlen * rootNode()
static double ap_calc_bootstrap_remark_sub(int seq_len, const char *old, const char *ne)
bool next_to_folded_group() const
const char * data(int block) const
TYPE * ARB_calloc(size_t nelem)
const GBT_LEN AP_UNDEF_BL
AP_tree_nlen * otherNode(const AP_tree_nlen *n) const
friend class AP_tree_nlen
bool is_root_edge() const
#define TEST_EXPECT_NO_ERROR(call)
MutationsPerSite(size_t len)
static void initialize(AP_tree_nlen *root)
GB_transaction ta(gb_var)
void mixTree(int repeat, int percent, EdgeSpec whichEdges)
static void ap_calc_bootstrap_remark(AP_tree_nlen *son_node, AP_BL_MODE mode, const MutationsPerSite &mps)
#define TEST_EXPECT_EQUAL(expr, want)
void undefine_branchlengths(AP_tree_nlen *node)
Mutations nni_mutPerSite(Mutations pars_one, AP_BL_MODE mode, MutationsPerSite *mps)
#define TEST_EXPECT__BROKEN(cond)
AP_tree_nlen * notSonNode() const