diff --git a/src/free.c b/src/free.c index dd453489..c773b57f 100644 --- a/src/free.c +++ b/src/free.c @@ -24,52 +24,40 @@ void Free_Clade(t_clad *this) ////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// -void Free_All_Nodes_Light(t_tree *mixt_tree) +void Free_All_Nodes_Light(t_tree *tree) { - int i; - t_tree *tree; - - tree = mixt_tree; - do + if(tree->a_nodes != NULL) { - for(i=0;i<2*tree->n_otu-1;++i) if(tree->a_nodes[i] != NULL) Free_Node(tree->a_nodes[i]); + for(int i=0;i<2*tree->n_otu-1;++i) if(tree->a_nodes[i] != NULL) Free_Node(tree->a_nodes[i]); Free(tree->a_nodes); - tree = tree->next; } - while(tree); - } ////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// -void Free_All_Edges_Light(t_tree *mixt_tree) +void Free_All_Edges_Light(t_tree *tree) { - int i; - t_tree *tree; - - tree = mixt_tree; - - for(i=0;i<2*tree->n_otu-1;++i) + if(tree->a_edges != NULL) { - if(tree->a_edges[i] != NULL) - { - Free_Scalar_Dbl(tree->a_edges[i]->l); - Free_Scalar_Dbl(tree->a_edges[i]->l_old); - Free_Scalar_Dbl(tree->a_edges[i]->l_var); - Free_Scalar_Dbl(tree->a_edges[i]->l_var_old); - } + for(int i=0;i<2*tree->n_otu-1;++i) + if(tree->a_edges[i] != NULL) + Free_Edge(tree->a_edges[i]); + Free(tree->a_edges); } +} - do +////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////// + +void Free_All_Edges_Lens(t_tree *tree) +{ + if(tree->a_edges != NULL) { - for(i=0;i<2*tree->n_otu-1;++i) + for(int i=0;i<2*tree->n_otu-1;++i) if(tree->a_edges[i] != NULL) - Free_Edge(tree->a_edges[i]); - Free(tree->a_edges); - tree = tree->next; + Free_Edge_Length(tree->a_edges[i]); } - while(tree); } ////////////////////////////////////////////////////////////// @@ -77,10 +65,10 @@ void Free_All_Edges_Light(t_tree *mixt_tree) void Free_Edge_Length(t_edge *b) { - if(b->l) Free_Scalar_Dbl(b->l); - if(b->l_old) Free_Scalar_Dbl(b->l_old); - if(b->l_var) Free_Scalar_Dbl(b->l_var); - if(b->l_var_old) Free_Scalar_Dbl(b->l_var_old); + if(b->l != NULL) Free_Scalar_Dbl(b->l); + if(b->l_old != NULL) Free_Scalar_Dbl(b->l_old); + if(b->l_var != NULL) Free_Scalar_Dbl(b->l_var); + if(b->l_var_old != NULL) Free_Scalar_Dbl(b->l_var_old); } ////////////////////////////////////////////////////////////// @@ -120,11 +108,7 @@ void Free_Node(t_node *n) } if(n->ori_name) { Free(n->ori_name); n->ori_name = NULL; } - - /* if(n->name) { Free(n->name); n->name = NULL; } */ - /* Don't do that: see Copy_Tax_Names_To_Tip_Labels - tree->a_nodes[i]->ori_name = tree->a_nodes[i]->name; */ - + Free(n); } @@ -160,58 +144,35 @@ void Free_Mat(matrix *mat) void Free_Partial_Lk(phydbl *p_lk, int len, int n_catg) { Free(p_lk); - -/* int i,j; */ -/* for(i=0;iis_mixt_tree == YES) + { + MIXT_Free_Tree(tree); + } + else { if(tree->mat) Free_Mat(tree->mat); - Free(tree->t_dir); + if(tree->t_dir) Free(tree->t_dir); if(tree->short_l) Free(tree->short_l); - if(tree->mutmap) Free(tree->mutmap); + if(tree->mutmap) Free(tree->mutmap); Free_Bip(tree); - Free(tree->curr_path); - tree = tree->next; - } - while(tree); - - Free_All_Edges_Light(mixt_tree); - Free_All_Nodes_Light(mixt_tree); - - tree = mixt_tree; - next = mixt_tree->next; - do - { + Free(tree->curr_path); + Free_All_Edges_Lens(tree); + Free_All_Edges_Light(tree); + Free_All_Nodes_Light(tree); Free(tree); - tree = next; - if(!tree) break; - next = next->next; } - while(tree); - } ////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// - void Free_Bip(t_tree *tree) { int i,j; @@ -219,11 +180,11 @@ void Free_Bip(t_tree *tree) if(tree->has_bip) { For(i,2*tree->n_otu-2) - { - Free(tree->a_nodes[i]->bip_size); - for(j=0;j<3;j++) Free(tree->a_nodes[i]->bip_node[j]); - Free(tree->a_nodes[i]->bip_node); - } + { + Free(tree->a_nodes[i]->bip_size); + for(j=0;j<3;j++) Free(tree->a_nodes[i]->bip_node[j]); + Free(tree->a_nodes[i]->bip_node); + } } tree->has_bip = NO; } @@ -334,36 +295,27 @@ void Free_Tree_Ins_Tar(t_tree *tree) ////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// -void Free_Tree_Pars(t_tree *mixt_tree) +void Free_Tree_Pars(t_tree *tree) { int i; - t_tree *tree; - tree = mixt_tree; - do + Free(tree->step_mat); + Free(tree->site_pars); + + for(i=0;i<2*tree->n_otu-3;++i) Free_Edge_Pars(tree->a_edges[i]); + + if(tree->n_root) { - Free(tree->step_mat); - Free(tree->site_pars); - - for(i=0;i<2*tree->n_otu-3;++i) - { - Free_Edge_Pars(tree->a_edges[i]); - } - - if(tree->n_root) - { - Free_Edge_Pars_Left(tree->n_root->b[1]); - Free_Edge_Pars_Left(tree->n_root->b[2]); + Free_Edge_Pars_Left(tree->n_root->b[1]); + Free_Edge_Pars_Left(tree->n_root->b[2]); } - else - { - Free_Edge_Pars(tree->a_edges[2*tree->n_otu-3]); - Free_Edge_Pars(tree->a_edges[2*tree->n_otu-2]); - } - - tree = tree->next; + else + { + Free_Edge_Pars(tree->a_edges[2*tree->n_otu-3]); + Free_Edge_Pars(tree->a_edges[2*tree->n_otu-2]); } - while(tree); + + if(tree->is_mixt_tree == YES) MIXT_Repeat_Task(Free_Tree_Pars,tree); } ////////////////////////////////////////////////////////////// @@ -400,82 +352,76 @@ void Free_Edge_Pars(t_edge *b) ////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// -void Free_Tree_Lk(t_tree *mixt_tree) +void Free_Tree_Lk(t_tree *tree) { int i; - t_tree *tree; - tree = mixt_tree; - do + Free(tree->big_lk_array); + Free(tree->c_lnL_sorted); + Free(tree->cur_site_lk); + Free(tree->old_site_lk); + Free(tree->site_lk_cat); + Free(tree->fact_sum_scale); + Free(tree->unscaled_site_lk_cat); + Free(tree->expl); + + for(i=0;i<3;i++) Free(tree->log_lks_aLRT[i]); + Free(tree->log_lks_aLRT); + + for(i=0;i<2*tree->n_otu-1;++i) Free_NNI(tree->a_edges[i]->nni); + for(i=0;i<2*tree->n_otu-3;++i) Free_Edge_Lk(tree->a_edges[i]); + for(i=0;i<2*tree->n_otu-3;++i) Free_Edge_Loc(tree->a_edges[i]); + + if(tree->is_mixt_tree == NO) { - Free(tree->big_lk_array); - Free(tree->c_lnL_sorted); - Free(tree->cur_site_lk); - Free(tree->old_site_lk); - Free(tree->site_lk_cat); - Free(tree->fact_sum_scale); - Free(tree->unscaled_site_lk_cat); - Free(tree->expl); - - if(tree->is_mixt_tree == NO) - { - Free(tree->dot_prod); - Free(tree->p_lk_left_pi); - Free(tree->l_ev); - + Free(tree->dot_prod); + Free(tree->p_lk_left_pi); + Free(tree->l_ev); + #if (defined(__AVX__) || defined(__AVX2__) || defined(__SSE__) || defined(__SSE2__) || defined(__SSE3__)) - Free(tree->_tPij1); - Free(tree->_tPij2); - Free(tree->_pmat1plk1); - Free(tree->_pmat2plk2); - Free(tree->_plk0); - Free(tree->_l_ev); - Free(tree->_r_ev); - Free(tree->_prod_left); - Free(tree->_prod_rght); + Free(tree->_tPij1); + Free(tree->_tPij2); + Free(tree->_pmat1plk1); + Free(tree->_pmat2plk2); + Free(tree->_plk0); + Free(tree->_l_ev); + Free(tree->_r_ev); + Free(tree->_prod_left); + Free(tree->_prod_rght); #endif - - for(i=0;i<3;i++) Free(tree->log_lks_aLRT[i]); - Free(tree->log_lks_aLRT); - - Free(tree->div_post_pred_extra_0); - Free(tree->sum_scale_cat_extra_0); - Free(tree->sum_scale_extra_0); - Free(tree->patt_id_extra_0); - Free(tree->p_lk_extra_0); - Free(tree->p_lk_tip_extra_0); - - Free(tree->div_post_pred_extra_1); - Free(tree->sum_scale_cat_extra_1); - Free(tree->sum_scale_extra_1); - Free(tree->patt_id_extra_1); - Free(tree->p_lk_extra_1); - Free(tree->p_lk_tip_extra_1); - - for(i=0;i<2*tree->n_otu-1;++i) Free_NNI(tree->a_edges[i]->nni); - for(i=0;i<2*tree->n_otu-3;++i) Free_Edge_Lk(tree->a_edges[i]); - for(i=0;i<2*tree->n_otu-3;++i) Free_Edge_Loc(tree->a_edges[i]); - - if(tree->n_root != NULL) - { - Free_Edge_Lk_Left(tree->n_root->b[1]); - Free_Edge_Lk_Left(tree->n_root->b[2]); - Free_Edge_Loc_Left(tree->n_root->b[1]); - Free_Edge_Loc_Left(tree->n_root->b[2]); - } - else - { - Free_Edge_Lk(tree->a_edges[2*tree->n_otu-3]); - Free_Edge_Lk(tree->a_edges[2*tree->n_otu-2]); - Free_Edge_Loc(tree->a_edges[2*tree->n_otu-3]); - Free_Edge_Loc(tree->a_edges[2*tree->n_otu-2]); - } - } + + Free(tree->div_post_pred_extra_0); + Free(tree->sum_scale_cat_extra_0); + Free(tree->sum_scale_extra_0); + Free(tree->patt_id_extra_0); + Free(tree->p_lk_extra_0); + Free(tree->p_lk_tip_extra_0); - tree = tree->next; + Free(tree->div_post_pred_extra_1); + Free(tree->sum_scale_cat_extra_1); + Free(tree->sum_scale_extra_1); + Free(tree->patt_id_extra_1); + Free(tree->p_lk_extra_1); + Free(tree->p_lk_tip_extra_1); + + if(tree->n_root != NULL) + { + Free_Edge_Lk_Left(tree->n_root->b[1]); + Free_Edge_Lk_Left(tree->n_root->b[2]); + Free_Edge_Loc_Left(tree->n_root->b[1]); + Free_Edge_Loc_Left(tree->n_root->b[2]); + } + else + { + Free_Edge_Lk(tree->a_edges[2*tree->n_otu-3]); + Free_Edge_Lk(tree->a_edges[2*tree->n_otu-2]); + Free_Edge_Loc(tree->a_edges[2*tree->n_otu-3]); + Free_Edge_Loc(tree->a_edges[2*tree->n_otu-2]); + } } - while(tree); + + if(tree->is_mixt_tree == YES) MIXT_Repeat_Task(Free_Tree_Lk,tree); } @@ -984,7 +930,6 @@ void Free_Tree_List(t_treelist *list) ////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// - void Free_St(supert_tree *st) { int i; @@ -1045,44 +990,40 @@ void Free_One_Spr(t_spr *this_spr) ////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// -void Free_Spr_List_One_Edge(t_tree *mixt_tree) +void Free_Spr_List_One_Edge(t_tree *tree) { int i; - t_tree *tree; - - tree = mixt_tree; - do - { - for(i=0;isize_spr_list_one_edge+1;++i) Free_One_Spr(tree->spr_list_one_edge[i]); - Free(tree->spr_list_one_edge); - tree = tree->next; - } - while(tree); + for(i=0;isize_spr_list_one_edge+1;++i) Free_One_Spr(tree->spr_list_one_edge[i]); + if(tree->is_mixt_tree == YES) MIXT_Repeat_Task(Free_Spr_List_One_Edge,tree); + Free(tree->spr_list_one_edge); } ////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// -void Free_Spr_List_All_Edge(t_tree *mixt_tree) +void Free_Spr_List_All_Edge(t_tree *tree) { int i; - t_tree *tree; - tree = mixt_tree; - do - { - for(i=0;isize_spr_list_all_edge+1;++i) Free_One_Spr(tree->spr_list_all_edge[i]); - Free(tree->spr_list_all_edge); - tree = tree->next; - } - while(tree); + for(i=0;isize_spr_list_all_edge+1;++i) Free_One_Spr(tree->spr_list_all_edge[i]); + if(tree->is_mixt_tree == YES) MIXT_Repeat_Task(Free_Spr_List_All_Edge,tree); + Free(tree->spr_list_all_edge); +} +////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////// + +void Free_Best_Spr(t_tree *tree) +{ + Free_One_Spr(tree->best_spr); + if(tree->is_mixt_tree == YES) MIXT_Repeat_Task(Free_Best_Spr,tree); } ////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// + void Free_Actual_CSeq(calign *data) { int i; diff --git a/src/free.h b/src/free.h index f9501af7..9685c9fc 100644 --- a/src/free.h +++ b/src/free.h @@ -99,5 +99,8 @@ void Free_Clade(t_clad *this); void Free_Label(t_label *lab); void Free_Contrasts(t_ctrst *ctrst); void TIMES_Free_Times(t_time *times); +void Free_Best_Spr(t_tree *tree); +void Free_All_Edges_Light(t_tree *tree); +void Free_All_Edges_Lens(t_tree *tree); #endif diff --git a/src/io.c b/src/io.c index bb00c0d3..151ecc70 100644 --- a/src/io.c +++ b/src/io.c @@ -4510,7 +4510,9 @@ option *PhyML_XML(char *xml_filename) MIXT_Init_T_End(mixt_tree); Print_Data_Structure(YES,mixt_tree->io->fp_out_stats,mixt_tree); + Free_Best_Spr(mixt_tree); Free_Spr_List_One_Edge(mixt_tree); + Free_Spr_List_All_Edge(mixt_tree); Free_Tree_Pars(mixt_tree); Free_Tree_Lk(mixt_tree); } diff --git a/src/main.c b/src/main.c index cdad1b08..5ddf38bb 100644 --- a/src/main.c +++ b/src/main.c @@ -324,7 +324,7 @@ int main(int argc, char **argv) #ifdef BEAGLE finalize_beagle_instance(tree); #endif - Free_One_Spr(tree->best_spr); + Free_Best_Spr(tree); Free_Spr_List_One_Edge(tree); Free_Spr_List_All_Edge(tree); Free_Tree_Pars(tree); diff --git a/src/make.c b/src/make.c index a2141961..ec2fa581 100644 --- a/src/make.c +++ b/src/make.c @@ -763,7 +763,6 @@ t_tree *Make_Tree(int n_otu) ////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// - void Make_Tree_Path(t_tree *tree) { tree->curr_path = (t_node **)mCalloc(tree->n_otu,sizeof(t_node *)); @@ -1293,7 +1292,7 @@ void Make_Spr(t_tree *tree) Make_Spr_List_One_Edge(tree); Make_Spr_List_All_Edge(tree); Make_Best_Spr(tree); - if(tree->is_mixt_tree == YES) MIXT_Make_Spr(tree); + if(tree->is_mixt_tree == YES) MIXT_Repeat_Task(Make_Spr,tree); } ////////////////////////////////////////////////////////////// diff --git a/src/mixt.c b/src/mixt.c index 0ef0228c..1580273d 100644 --- a/src/mixt.c +++ b/src/mixt.c @@ -1640,7 +1640,6 @@ void MIXT_Multiply_Scalar_Dbl(scalar_dbl *this, phydbl scalar) void MIXT_Br_Len_Opt(t_edge *mixt_b, t_tree *mixt_tree) { t_edge *b; - t_tree *tree; scalar_dbl *l; MIXT_Turn_Branches_OnOff_In_All_Elem(ON,mixt_tree); @@ -1648,33 +1647,14 @@ void MIXT_Br_Len_Opt(t_edge *mixt_b, t_tree *mixt_tree) mixt_tree->ignore_mixt_info = YES; b = mixt_b; - tree = mixt_tree; - l = NULL; + l = b->l; do { - while(tree->is_mixt_tree == YES) - { - tree = tree->next; - b = b->next; - } - - l = b->l; - do - { - if(l->onoff == ON) - { - Br_Len_Opt(&(l->v),mixt_b,mixt_tree); - l->onoff = OFF; - } - l = l->next; - } - while(l); - - tree = tree->next; - b = b->next; + if(l->onoff == ON) Br_Len_Opt(&(l->v),mixt_b,mixt_tree); + l = l->next; } - while(tree); + while(l); mixt_tree->ignore_mixt_info = NO; } @@ -2281,7 +2261,7 @@ void MIXT_RATES_Update_Edge_Lengths(t_tree *mixt_tree) #if (defined PHYREX || PHYTIME) RATES_Update_Edge_Lengths(tree); #endif - + if(tree->is_mixt_tree == YES) return; tree = tree->next; } while(tree); @@ -2336,6 +2316,7 @@ void MIXT_Init_Model(t_mod *mod) do { Init_Model(mod->io->cdata,mod,mod->io); + if(mod->is_mixt_mod == YES) return; mod = mod->next; } while(mod); @@ -2511,7 +2492,7 @@ void MIXT_Prepare_All(int num_rand_tree, t_tree *mixt_tree) MIXT_Make_Tree_For_Pars(mixt_tree); Make_Tree_For_Lk(mixt_tree); - MIXT_Make_Spr(mixt_tree); + Make_Spr(mixt_tree); MIXT_Chain_All(mixt_tree); MIXT_Check_Edge_Lens_In_All_Elem(mixt_tree); @@ -2523,26 +2504,6 @@ void MIXT_Prepare_All(int num_rand_tree, t_tree *mixt_tree) ////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// -void MIXT_Make_Spr(t_tree *mixt_tree) -{ - t_tree *tree; - - tree = mixt_tree->next; - - if(tree != NULL) - { - do - { - Make_Spr(tree); - tree = tree->next; - } - while(tree); - } -} - -////////////////////////////////////////////////////////////// -////////////////////////////////////////////////////////////// - void MIXT_Make_Tree_For_Pars(t_tree *mixt_tree) { t_tree *tree; @@ -3283,16 +3244,18 @@ void MIXT_Set_Both_Sides(int yesno, t_tree *mixt_tree) assert(mixt_tree->is_mixt_tree == YES); - tree = mixt_tree; - - do + tree = mixt_tree->next; + + if(tree != NULL) { - if(tree->is_mixt_tree == YES) tree = tree->next; - Set_Both_Sides(yesno,tree); - tree = tree->next; + do + { + Set_Both_Sides(yesno,tree); + if(tree->is_mixt_tree == YES) return; + tree = tree->next; + } + while(tree); } - while(tree); - } ////////////////////////////////////////////////////////////// @@ -3307,6 +3270,7 @@ void MIXT_Set_Model_Parameters(t_mod *mixt_mod) do { Set_Model_Parameters(mod); + if(mod->is_mixt_mod == YES) return; mod = mod->next; } while(mod); @@ -3341,23 +3305,21 @@ void MIXT_Update_Eigen_Lr(t_edge *mixt_b, t_tree *mixt_tree) t_edge *b; t_tree *tree; - tree = mixt_tree; - b = mixt_b; + tree = mixt_tree->next; + b = mixt_b->next; - do + if(tree != NULL) { - if(tree->is_mixt_tree == YES) - { + do + { + Update_Eigen_Lr(b,tree); + if(tree->is_mixt_tree == YES) return; + tree = tree->next; b = b->next; } - - Update_Eigen_Lr(b,tree); - - tree = tree->next; - b = b->next; + while(tree); } - while(tree); } @@ -3575,6 +3537,7 @@ void MIXT_Set_Bl_From_Rt(int yn, t_tree *mixt_tree) do { Set_Bl_From_Rt(yn,tree); + if(tree->is_mixt_tree == YES) return; tree = tree->next; } while(tree); @@ -3647,13 +3610,8 @@ void MIXT_Init_NNI_Score(phydbl val, t_edge *mixt_b, t_tree *mixt_tree) b = mixt_b->next; do { - if(tree->is_mixt_tree) - { - tree = tree->next; - b = b->next; - } - Init_NNI_Score(val,b,tree); + if(tree->is_mixt_tree == YES) return; tree = tree->next; b = b->next; } @@ -3666,39 +3624,87 @@ void MIXT_Init_NNI_Score(phydbl val, t_edge *mixt_b, t_tree *mixt_tree) t_tree *MIXT_Duplicate_Tree(t_tree *ori) { - t_tree *cpy,*first,*prev; - int dum; + t_tree *tree,*cpy; + int i; + + assert(ori->is_mixt_tree == YES); ori->is_mixt_tree = NO; - first = Duplicate_Tree(ori); - first->prev = NULL; - first->is_mixt_tree = YES; + cpy = Duplicate_Tree(ori); ori->is_mixt_tree = YES; - - ori = ori->next; - prev = cpy = first; + cpy->is_mixt_tree = YES; + cpy->prev = NULL; + tree = ori; + do { - dum = ori->is_mixt_tree; - ori->is_mixt_tree = NO; - - prev = cpy; + if(tree->next != NULL) + { + i = tree->next->is_mixt_tree; + tree->next->is_mixt_tree = NO; + cpy->next = Duplicate_Tree(tree->next); + tree->next->is_mixt_tree = i; - cpy = Duplicate_Tree(ori); + cpy->next->is_mixt_tree = tree->next->is_mixt_tree; + cpy->next->prev = cpy; + } + else break; + + tree = tree->next; + cpy = cpy->next; + } + while(tree != NULL); - ori->is_mixt_tree = dum; - cpy->is_mixt_tree = ori->is_mixt_tree; + do cpy = cpy->prev; while(cpy->prev); - cpy->prev = prev; - prev->next = cpy; + do + { + for(i=0;i<2*tree->n_otu-1;++i) + { + Free_Scalar_Dbl(cpy->a_edges[i]->l); + Free_Scalar_Dbl(cpy->a_edges[i]->l_old); + Free_Scalar_Dbl(cpy->a_edges[i]->l_var); + Free_Scalar_Dbl(cpy->a_edges[i]->l_var_old); + } - ori = ori->next; + if(cpy->next == NULL) break; + + cpy = cpy->next; } - while(ori); + while(1); + + do cpy = cpy->prev; while(cpy->prev); + + for(i=0;i<2*tree->n_otu-1;++i) + { + cpy->a_edges[i]->l = Duplicate_Scalar_Dbl(ori->a_edges[i]->l); + cpy->a_edges[i]->l_old = Duplicate_Scalar_Dbl(ori->a_edges[i]->l_old); + cpy->a_edges[i]->l_var = Duplicate_Scalar_Dbl(ori->a_edges[i]->l_var); + cpy->a_edges[i]->l_var_old = Duplicate_Scalar_Dbl(ori->a_edges[i]->l_var_old); + } + - cpy->next = NULL; + tree = cpy; - return(first); + do + { + for(i=0;i<2*tree->n_otu-1;++i) + { + cpy->a_edges[i]->l = tree->a_edges[i]->l; + cpy->a_edges[i]->l_old = tree->a_edges[i]->l_old; + cpy->a_edges[i]->l_var = tree->a_edges[i]->l_var; + cpy->a_edges[i]->l_var_old = tree->a_edges[i]->l_var_old; + } + + if(cpy->next == NULL) break; + + cpy = cpy->next; + } + while(1); + + do cpy = cpy->prev; while(cpy->prev); + + return(cpy); } ////////////////////////////////////////////////////////////// @@ -3864,6 +3870,7 @@ void MIXT_Exchange_Nodes(t_node *a, t_node *d, t_node *w, t_node *v, t_tree *mix tree->a_nodes[w->num], tree->a_nodes[v->num], tree); + if(tree->is_mixt_tree == YES) return; i++; tree = tree->next; } @@ -3872,8 +3879,69 @@ void MIXT_Exchange_Nodes(t_node *a, t_node *d, t_node *w, t_node *v, t_tree *mix ////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// + +void MIXT_Free_Tree(t_tree *mixt_tree) +{ + t_tree *tree; + t_tree *next; + + Free_All_Edges_Lens(mixt_tree); + + tree = mixt_tree; + do + { + if(tree->mat) Free_Mat(tree->mat); + if(tree->t_dir) Free(tree->t_dir); + if(tree->short_l) Free(tree->short_l); + if(tree->mutmap) Free(tree->mutmap); + Free_Bip(tree); + Free(tree->curr_path); + Free_All_Nodes_Light(tree); + Free_All_Edges_Light(tree); + tree = tree->next; + } + while(tree); + + + tree = mixt_tree; + next = mixt_tree->next; + do + { + Free(tree); + tree = next; + if(!tree) break; + next = next->next; + } + while(tree); +} + ////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// +void MIXT_Repeat_Task(void (*Task_Function)(), t_tree *mixt_tree) +{ + t_tree *tree,*next; + + tree = mixt_tree->next; + + if(tree != NULL) + { + do + { + next = tree->next; + (*Task_Function)(tree); + if(tree->is_mixt_tree == YES) return; + tree = next; + } + while(tree != NULL); + } +} + +////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////// diff --git a/src/mixt.h b/src/mixt.h index 044ba41a..12fc7845 100644 --- a/src/mixt.h +++ b/src/mixt.h @@ -119,5 +119,7 @@ void MIXT_Set_Model_Parameters(t_mod *mixt_mod); void MIXT_Print_Site_Lk(t_tree *mixt_tree, FILE *fp); void MIXT_Exchange_Nodes(t_node *a, t_node *d, t_node *w, t_node *v, t_tree *mixt_tree); void MIXT_Chain_Models(t_tree *mixt_tree); +void MIXT_Repeat_Task(void (*Task_Function)(),t_tree *mixt_tree); +void MIXT_Free_Tree(t_tree *mixt_tree); #endif diff --git a/src/optimiz.c b/src/optimiz.c index bd88a214..04587405 100644 --- a/src/optimiz.c +++ b/src/optimiz.c @@ -612,7 +612,6 @@ phydbl Br_Len_Opt(phydbl *l, t_edge *b, t_tree *tree) { phydbl lk_begin, lk_end; - if(tree->is_mixt_tree == YES && tree->ignore_mixt_info == NO) { MIXT_Br_Len_Opt(b,tree); @@ -718,16 +717,6 @@ void Optimize_Br_Len_Serie(int n_max_iter, t_tree *tree) { phydbl lk_init,lk_end; int iter; - - - /* if(tree->n_tot_bl_opt == 0) */ - /* { */ - /* for(int i=0;i<2*tree->n_otu-3;++i) Set_Scalar_Dbl(1.E-6,tree->a_edges[i]->l); */ - /* for(int i=0;i<2*tree->n_otu-3;++i) Set_Scalar_Dbl_Min_Thresh(tree->mod->l_min,tree->a_edges[i]->l); */ - /* for(int i=0;i<2*tree->n_otu-3;++i) Set_Scalar_Dbl_Max_Thresh(tree->mod->l_max,tree->a_edges[i]->l); */ - /* } */ - - Set_Both_Sides(NO,tree); Lk(NULL,tree); @@ -795,12 +784,7 @@ void Optimize_Br_Len_Serie(int n_max_iter, t_tree *tree) } iter++; - - - /* PhyML_Printf("\n. lnL: %f %f %f %d",tree->c_lnL,Rgamma((phydbl)(iter+1),(phydbl)(1./(iter+1))),(phydbl)(iter+1)*(phydbl)(1./(iter+1))*(phydbl)(1./(iter+1)),iter); */ - } - /* while(lk_end - lk_init > tree->mod->s_opt->min_diff_lk_local && iter < n_max_iter); */ while(iter < 1); } diff --git a/src/spr.c b/src/spr.c index aab90364..a55343b2 100644 --- a/src/spr.c +++ b/src/spr.c @@ -128,8 +128,6 @@ int Spr(phydbl init_lnL, phydbl prop_spr, t_tree *tree) Set_Both_Sides(YES,tree); Lk(NULL,tree); tree->best_lnL = tree->c_lnL; - - /* PhyML_Printf("\n. init: %f",tree->c_lnL); */ for(i=0;in_otu-3)*prop_spr));++i) { @@ -145,7 +143,6 @@ int Spr(phydbl init_lnL, phydbl prop_spr, t_tree *tree) Spr_Subtree(b,b->rght,tree); } } - Free(br_idx); @@ -750,7 +747,6 @@ void Global_Spr_Search(t_tree *tree) if(tree->io->print_json_trace == YES) JSON_Tree_Io(tree,tree->io->fp_out_json_trace); if(tree->verbose > VL0 && tree->io->quiet == NO) PhyML_Printf("\n\n. Score of initial tree: %.2f",tree->c_lnL); - tree->mod->s_opt->min_diff_lk_move = 1.E-1; tree->mod->s_opt->min_diff_lk_local = 1.E-1; @@ -781,7 +777,6 @@ void Global_Spr_Search(t_tree *tree) iter = 0; do { - if(!(iter%freq)) { for(int i=0;i<2*tree->n_otu-3;++i) MIXT_Multiply_Scalar_Dbl(tree->a_edges[i]->l,Rgamma((phydbl)(tune_l_mult*iter+1),(phydbl)(1./(tune_l_mult*iter+1)))); @@ -864,8 +859,6 @@ void Global_Spr_Search(t_tree *tree) tree->mod->s_opt->spr_pars = NO; tree->mod->s_opt->min_diff_lk_move = 1.E-2; tree->mod->s_opt->min_diff_lk_local = 1.E-2; - tree->mod->s_opt->apply_spr_right_away = YES; - tree->mod->s_opt->apply_spr = YES; tree->mod->s_opt->eval_list_regraft = NO; tree->mod->s_opt->max_delta_lnL_spr_current = 0.0; tree->mod->s_opt->min_n_triple_moves = 1; @@ -881,7 +874,7 @@ void Global_Spr_Search(t_tree *tree) { for(int i=0;i<2*tree->n_otu-3;++i) MIXT_Multiply_Scalar_Dbl(tree->a_edges[i]->l,Rgamma((phydbl)(tune_l_mult*iter+1),(phydbl)(1./(tune_l_mult*iter+1)))); for(int i=0;i<2*tree->n_otu-3;++i) Set_Scalar_Dbl_Min_Thresh(tree->mod->l_min,tree->a_edges[i]->l); - for(int i=0;i<2*tree->n_otu-3;++i) Set_Scalar_Dbl_Max_Thresh(tree->mod->l_max,tree->a_edges[i]->l); + for(int i=0;i<2*tree->n_otu-3;++i) Set_Scalar_Dbl_Max_Thresh(tree->mod->l_max,tree->a_edges[i]->l); freq--; if(freq < 1) freq=1; } @@ -890,7 +883,7 @@ void Global_Spr_Search(t_tree *tree) Optimize_Br_Len_Serie(2,tree); if(!(iter%round_freq) && iter > 0) Round_Optimize(tree,1000); - + if(tree->verbose > VL0 && tree->io->quiet == NO) { time(&t_cur); @@ -952,8 +945,6 @@ void Global_Spr_Search(t_tree *tree) tree->mod->s_opt->l_min_spr = 1.E-4; tree->mod->s_opt->min_diff_lk_move = 1.E-2; tree->mod->s_opt->min_diff_lk_local = 1.E-2; - tree->mod->s_opt->apply_spr_right_away = YES; - tree->mod->s_opt->apply_spr = YES; tree->mod->s_opt->eval_list_regraft = YES; tree->mod->s_opt->max_delta_lnL_spr_current = 0.0; tree->mod->s_opt->min_n_triple_moves = 5; @@ -964,7 +955,7 @@ void Global_Spr_Search(t_tree *tree) { Spr(tree->c_lnL,1.0,tree); Optimize_Br_Len_Serie(2,tree); - + if(!(iter%round_freq)) Round_Optimize(tree,1000); if(tree->verbose > VL0 && tree->io->quiet == NO) diff --git a/src/utilities.c b/src/utilities.c index af4ba584..6548e55d 100644 --- a/src/utilities.c +++ b/src/utilities.c @@ -4948,102 +4948,106 @@ void Copy_Tree(t_tree *ori, t_tree *cpy) MIXT_Copy_Tree(ori,cpy); return; } - - for(i=0;i<2*ori->n_otu-1;++i) - { - if(ori->a_nodes[i] != NULL) - { - cpy->a_nodes[i]->anc = - (ori->a_nodes[i]->anc != NULL) ? - cpy->a_nodes[ori->a_nodes[i]->anc->num] : - NULL; - - for(j=0;j<3;++j) + else + { + for(i=0;i<2*ori->n_otu-1;++i) + { + if(ori->a_nodes[i] != NULL) { - if(ori->a_nodes[i]->v[j] != NULL) - { - cpy->a_nodes[i]->v[j] = cpy->a_nodes[ori->a_nodes[i]->v[j]->num]; - cpy->a_nodes[i]->b[j] = cpy->a_edges[ori->a_nodes[i]->b[j]->num]; - } - else + cpy->a_nodes[i]->anc = + (ori->a_nodes[i]->anc != NULL) ? + cpy->a_nodes[ori->a_nodes[i]->anc->num] : + NULL; + + for(j=0;j<3;++j) { - cpy->a_nodes[i]->v[j] = NULL; - cpy->a_nodes[i]->b[j] = NULL; + if(ori->a_nodes[i]->v[j] != NULL) + { + cpy->a_nodes[i]->v[j] = cpy->a_nodes[ori->a_nodes[i]->v[j]->num]; + cpy->a_nodes[i]->b[j] = cpy->a_edges[ori->a_nodes[i]->b[j]->num]; + } + else + { + cpy->a_nodes[i]->v[j] = NULL; + cpy->a_nodes[i]->b[j] = NULL; + } } } + cpy->a_nodes[i]->c_seq = ori->a_nodes[i]->c_seq; } - cpy->a_nodes[i]->c_seq = ori->a_nodes[i]->c_seq; - } - - for(i=0;i<2*ori->n_otu-1;++i) - { - if(ori->a_edges[i] != NULL) + + for(i=0;i<2*ori->n_otu-1;++i) { - cpy->a_edges[i]->l->v = ori->a_edges[i]->l->v; - cpy->a_edges[i]->l_old->v = ori->a_edges[i]->l_old->v; - cpy->a_edges[i]->l_var->v = ori->a_edges[i]->l_var->v; - cpy->a_edges[i]->l_var_old->v = ori->a_edges[i]->l_var_old->v; - cpy->a_edges[i]->left = ori->a_edges[i]->left ? cpy->a_nodes[ori->a_edges[i]->left->num] : NULL; - cpy->a_edges[i]->rght = ori->a_edges[i]->rght ? cpy->a_nodes[ori->a_edges[i]->rght->num] : NULL; - cpy->a_edges[i]->l_v1 = ori->a_edges[i]->l_v1; - cpy->a_edges[i]->l_v2 = ori->a_edges[i]->l_v2; - cpy->a_edges[i]->r_v1 = ori->a_edges[i]->r_v1; - cpy->a_edges[i]->r_v2 = ori->a_edges[i]->r_v2; - cpy->a_edges[i]->l_r = ori->a_edges[i]->l_r; - cpy->a_edges[i]->r_l = ori->a_edges[i]->r_l; - cpy->a_edges[i]->does_exist = ori->a_edges[i]->does_exist; - cpy->a_edges[i]->support_val = ori->a_edges[i]->support_val; - + if(ori->a_edges[i] != NULL) + { + cpy->a_edges[i]->l->v = ori->a_edges[i]->l->v; + cpy->a_edges[i]->l_old->v = ori->a_edges[i]->l_old->v; + cpy->a_edges[i]->l_var->v = ori->a_edges[i]->l_var->v; + cpy->a_edges[i]->l_var_old->v = ori->a_edges[i]->l_var_old->v; + cpy->a_edges[i]->left = ori->a_edges[i]->left ? cpy->a_nodes[ori->a_edges[i]->left->num] : NULL; + cpy->a_edges[i]->rght = ori->a_edges[i]->rght ? cpy->a_nodes[ori->a_edges[i]->rght->num] : NULL; + cpy->a_edges[i]->l_v1 = ori->a_edges[i]->l_v1; + cpy->a_edges[i]->l_v2 = ori->a_edges[i]->l_v2; + cpy->a_edges[i]->r_v1 = ori->a_edges[i]->r_v1; + cpy->a_edges[i]->r_v2 = ori->a_edges[i]->r_v2; + cpy->a_edges[i]->l_r = ori->a_edges[i]->l_r; + cpy->a_edges[i]->r_l = ori->a_edges[i]->r_l; + cpy->a_edges[i]->does_exist = ori->a_edges[i]->does_exist; + cpy->a_edges[i]->support_val = ori->a_edges[i]->support_val; + #ifdef BEAGLE - cpy->a_edges[i]->p_lk_left_idx = ori->a_edges[i]->p_lk_left_idx; - cpy->a_edges[i]->p_lk_rght_idx = ori->a_edges[i]->p_lk_rght_idx; - cpy->a_edges[i]->p_lk_tip_idx = ori->a_edges[i]->p_lk_tip_idx; + cpy->a_edges[i]->p_lk_left_idx = ori->a_edges[i]->p_lk_left_idx; + cpy->a_edges[i]->p_lk_rght_idx = ori->a_edges[i]->p_lk_rght_idx; + cpy->a_edges[i]->p_lk_tip_idx = ori->a_edges[i]->p_lk_tip_idx; #endif + } } - } - for(i=0;in_otu;++i) - { - cpy->a_nodes[i]->tax = YES; - - Free(cpy->a_nodes[i]->name); - - cpy->a_nodes[i]->name = (char *)mCalloc(strlen(ori->a_nodes[i]->name)+1,sizeof(char)); - cpy->a_nodes[i]->ori_name = cpy->a_nodes[i]->name ; - - strcpy(cpy->a_nodes[i]->name,ori->a_nodes[i]->name); - } - - - if(ori->n_root) - { - cpy->e_root = cpy->a_edges[ori->e_root->num]; - cpy->n_root = cpy->a_nodes[ori->n_root->num]; - cpy->n_root_pos = ori->n_root_pos; + for(i=0;in_otu;++i) + { + cpy->a_nodes[i]->tax = YES; + + Free(cpy->a_nodes[i]->name); + + cpy->a_nodes[i]->name = (char *)mCalloc(strlen(ori->a_nodes[i]->name)+1,sizeof(char)); + cpy->a_nodes[i]->ori_name = cpy->a_nodes[i]->name ; + + strcpy(cpy->a_nodes[i]->name,ori->a_nodes[i]->name); + } + + + if(ori->n_root) + { + cpy->e_root = cpy->a_edges[ori->e_root->num]; + cpy->n_root = cpy->a_nodes[ori->n_root->num]; + cpy->n_root_pos = ori->n_root_pos; + + cpy->n_root->b[1] = cpy->a_edges[ori->n_root->b[1]->num]; + cpy->n_root->b[2] = cpy->a_edges[ori->n_root->b[2]->num]; + } + + cpy->num_curr_branch_available = 0; + cpy->t_beg = ori->t_beg; + cpy->verbose = ori->verbose; - cpy->n_root->b[1] = cpy->a_edges[ori->n_root->b[1]->num]; - cpy->n_root->b[2] = cpy->a_edges[ori->n_root->b[2]->num]; - } - - cpy->num_curr_branch_available = 0; - cpy->t_beg = ori->t_beg; - cpy->verbose = ori->verbose; - #ifdef BEAGLE - cpy->b_inst = ori->b_inst; + cpy->b_inst = ori->b_inst; #endif + } } - ////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// t_tree *Duplicate_Tree(t_tree *ori) { if(ori->is_mixt_tree == YES) return MIXT_Duplicate_Tree(ori); - t_tree *cpy = Make_Tree_From_Scratch(ori->n_otu,ori->data); - Copy_Tree(ori,cpy); - return(cpy); + else + { + t_tree *cpy = Make_Tree_From_Scratch(ori->n_otu,ori->data); + Copy_Tree(ori,cpy); + return(cpy); + } } ////////////////////////////////////////////////////////////// @@ -11248,7 +11252,7 @@ void Set_Scalar_Dbl_Min_Thresh(phydbl thresh, scalar_dbl *from) f = from; do { - if(f->v < thresh) f->v = thresh;; + if(f->v < thresh) f->v = thresh; f = f->next; } while(f); @@ -11264,7 +11268,7 @@ void Set_Scalar_Dbl_Max_Thresh(phydbl thresh, scalar_dbl *from) f = from; do { - if(f->v > thresh) f->v = thresh;; + if(f->v > thresh) f->v = thresh; f = f->next; } while(f); diff --git a/src/xml.c b/src/xml.c index af9750b4..9dcd2697 100644 --- a/src/xml.c +++ b/src/xml.c @@ -1028,6 +1028,7 @@ t_tree *XML_Process_Base(char *xml_filename) } else { + /* Free existing edge lengths, which will be replaced with those read from !*/ for(i=0;i<2*tree->n_otu-1;++i) { Free_Scalar_Dbl(tree->a_edges[i]->l); @@ -1060,7 +1061,7 @@ t_tree *XML_Process_Base(char *xml_filename) Exit("\n"); } - For(i,2*tree->n_otu-1) + for(i=0;i<2*tree->n_otu-1;++i) { tree->a_edges[i]->l = lens[i]; mixt_tree->a_edges[i]->l = lens[i]; @@ -1081,7 +1082,7 @@ t_tree *XML_Process_Base(char *xml_filename) if(first_m_elem > 1) // Done with this component, move to the next tree and model { if(tree->next) tree = tree->next; - if(mod->next) mod = mod->next; + if(mod->next) mod = mod->next; } } else if(list[j] != ' ')