@@ -11371,9 +11371,26 @@ NumSeqsType cmaple::Tree::seekBestRoot()
1137111371 const RealNumType half_blength = candidate_node.getUpperLength () >= 0 ?
1137211372 (candidate_node.getUpperLength () * 0.5 ) : -1 ;
1137311373
11374+ // 0. extract the mutations at the candidate node
11375+ std::unique_ptr<SeqRegions>& candidate_node_mutations =
11376+ node_mutations[candidate_vec_id];
11377+ // 1. create a new regions that de-integrate the mutations, if any
11378+ std::unique_ptr<SeqRegions> mut_integrated_candidate_regions =
11379+ (candidate_node_mutations && candidate_node_mutations->size ())
11380+ ? candidate_node.getPartialLh (TOP)
11381+ ->integrateMutations <num_states>(candidate_node_mutations, aln, true )
11382+ : nullptr ;
11383+ // 2. create the pointer that points to the appropriate regions
11384+ const std::unique_ptr<SeqRegions>* candidate_regions_ptr =
11385+ (candidate_node_mutations && candidate_node_mutations->size ())
11386+ ? &(mut_integrated_candidate_regions)
11387+ : &(candidate_node.getPartialLh (TOP));
11388+ // 3. create a reference from that pointer
11389+ auto & candidate_lower_regions = *candidate_regions_ptr;
11390+
1137411391 // compute the likelihood contribution when merging this node and the passing subtree
1137511392 std::unique_ptr<SeqRegions> lower_regions_merged = nullptr ;
11376- const RealNumType lh_contribution_by_merging = candidate_node. getPartialLh (TOP)
11393+ const RealNumType lh_contribution_by_merging = candidate_lower_regions
1137711394 ->mergeTwoLowers <num_states>(lower_regions_merged, half_blength,
1137811395 *(root_candidate->getIncomingRegions ()), half_blength, aln,
1137911396 model, cumulative_rate, threshold_prob, true );
@@ -11424,7 +11441,8 @@ NumSeqsType cmaple::Tree::seekBestRoot()
1142411441 {
1142511442 addChildrenAsRootCandidate<num_states>(root_candidate->getIncomingRegions (),
1142611443 candidate_node.getUpperLength (), root_candidate->getLhDeducted (),
11427- score, root_candidate->getFailureCount (), candidate_node, node_stack);
11444+ score, root_candidate->getFailureCount (), candidate_node,
11445+ candidate_vec_id, node_stack);
1142811446 }
1142911447
1143011448 // Show log every 1000 nodes
@@ -11464,13 +11482,44 @@ void cmaple::Tree::addStartingRootCandidate(
1146411482 const Index child_2_index = node.getNeighborIndex (RIGHT);
1146511483 PhyloNode& child_1 = nodes[child_1_index.getVectorIndex ()];
1146611484 PhyloNode& child_2 = nodes[child_2_index.getVectorIndex ()];
11467- std::unique_ptr<SeqRegions>& lower_regions_child_1 =
11468- child_1.getPartialLh (TOP);
11469- std::unique_ptr<SeqRegions>& lower_regions_child_2 =
11470- child_2.getPartialLh (TOP);
1147111485 const RealNumType total_blength = child_1.getUpperLength ()
1147211486 + child_2.getUpperLength ();
1147311487
11488+ // 0. extract the mutations at child_1
11489+ std::unique_ptr<SeqRegions>& child_1_mutations =
11490+ node_mutations[child_1_index.getVectorIndex ()];
11491+ // 1. create a new regions that de-integrate the mutations, if any
11492+ std::unique_ptr<SeqRegions> mut_integrated_child_1_regions =
11493+ (child_1_mutations && child_1_mutations->size ())
11494+ ? child_1.getPartialLh (TOP)
11495+ ->integrateMutations <num_states>(child_1_mutations, aln, true )
11496+ : nullptr ;
11497+ // 2. create the pointer that points to the appropriate regions
11498+ const std::unique_ptr<SeqRegions>* child_1_regions_ptr =
11499+ (child_1_mutations && child_1_mutations->size ())
11500+ ? &(mut_integrated_child_1_regions)
11501+ : &(child_1.getPartialLh (TOP));
11502+ // 3. create a reference from that pointer
11503+ auto & lower_regions_child_1 = *child_1_regions_ptr;
11504+
11505+
11506+ // 0. extract the mutations at child_2
11507+ std::unique_ptr<SeqRegions>& child_2_mutations =
11508+ node_mutations[child_2_index.getVectorIndex ()];
11509+ // 1. create a new regions that de-integrate the mutations, if any
11510+ std::unique_ptr<SeqRegions> mut_integrated_child_2_regions =
11511+ (child_2_mutations && child_2_mutations->size ())
11512+ ? child_2.getPartialLh (TOP)
11513+ ->integrateMutations <num_states>(child_2_mutations, aln, true )
11514+ : nullptr ;
11515+ // 2. create the pointer that points to the appropriate regions
11516+ const std::unique_ptr<SeqRegions>* child_2_regions_ptr =
11517+ (child_2_mutations && child_2_mutations->size ())
11518+ ? &(mut_integrated_child_2_regions)
11519+ : &(child_2.getPartialLh (TOP));
11520+ // 3. create a reference from that pointer
11521+ auto & lower_regions_child_2 = *child_2_regions_ptr;
11522+
1147411523 // compute the current likelihood contribution by merging likelihood
1147511524 // at root with the state frequencies
1147611525 /* RealNumType lh_contribution_at_root =
@@ -11492,40 +11541,91 @@ void cmaple::Tree::addStartingRootCandidate(
1149211541 {
1149311542 addChildrenAsRootCandidate<num_states>(lower_regions_child_2,
1149411543 total_blength, lh_contribution_at_root,
11495- 0 , 0 , child_1, node_stack);
11544+ 0 , 0 , child_1, child_1_index. getVectorIndex (), node_stack);
1149611545 }
1149711546
1149811547 // add child 2 (if it's an internal node)
1149911548 if (child_2.isInternal ())
1150011549 {
1150111550 addChildrenAsRootCandidate<num_states>(lower_regions_child_1,
1150211551 total_blength, lh_contribution_at_root,
11503- 0 , 0 , child_2, node_stack);
11552+ 0 , 0 , child_2, child_2_index. getVectorIndex () , node_stack);
1150411553 }
1150511554}
1150611555
1150711556template <const StateType num_states>
1150811557void cmaple::Tree::addChildrenAsRootCandidate (
11509- const std::unique_ptr<SeqRegions>& incoming_regions_ref ,
11558+ const std::unique_ptr<SeqRegions>& ori_incoming_regions_ref ,
1151011559 const cmaple::RealNumType branch_length,
1151111560 const cmaple::RealNumType lh_deducted,
1151211561 const cmaple::RealNumType last_lh,
1151311562 const short int failure_count,
1151411563 PhyloNode& parent_node,
11564+ const NumSeqsType parent_vec_index,
1151511565 std::stack<std::unique_ptr<RootCandidate>>& node_stack)
1151611566{
1151711567 // only consider adding children if the current parent node is an internal
1151811568 if (parent_node.isInternal ())
1151911569 {
11570+ // integrate the mutations at the parent node (if any)
11571+ // into the incoming_regions_ref
11572+ // 0. extract the mutations at the parent node
11573+ std::unique_ptr<SeqRegions>& parent_node_mutations =
11574+ node_mutations[parent_vec_index];
11575+ // 1. create a new regions that integrate the mutations, if any
11576+ std::unique_ptr<SeqRegions> mut_integrated_incoming_regions =
11577+ (parent_node_mutations && parent_node_mutations->size ())
11578+ ? ori_incoming_regions_ref
11579+ ->integrateMutations <num_states>(parent_node_mutations, aln)
11580+ : nullptr ;
11581+ // 2. create the pointer that points to the appropriate regions
11582+ const std::unique_ptr<SeqRegions>* incoming_regions_ptr =
11583+ (parent_node_mutations && parent_node_mutations->size ())
11584+ ? &(mut_integrated_incoming_regions)
11585+ : &(ori_incoming_regions_ref);
11586+ // 3. create a reference from that pointer
11587+ auto & incoming_regions_ref = *incoming_regions_ptr;
11588+
1152011589 // extract the two children
1152111590 const Index child_1_index = parent_node.getNeighborIndex (LEFT);
1152211591 const Index child_2_index = parent_node.getNeighborIndex (RIGHT);
1152311592 PhyloNode& child_1 = nodes[child_1_index.getVectorIndex ()];
1152411593 PhyloNode& child_2 = nodes[child_2_index.getVectorIndex ()];
11525- std::unique_ptr<SeqRegions>& lower_regions_child_1 =
11526- child_1.getPartialLh (TOP);
11527- std::unique_ptr<SeqRegions>& lower_regions_child_2 =
11528- child_2.getPartialLh (TOP);
11594+
11595+ // 0. extract the mutations at child_1
11596+ std::unique_ptr<SeqRegions>& child_1_mutations =
11597+ node_mutations[child_1_index.getVectorIndex ()];
11598+ // 1. create a new regions that de-integrate the mutations, if any
11599+ std::unique_ptr<SeqRegions> mut_integrated_child_1_regions =
11600+ (child_1_mutations && child_1_mutations->size ())
11601+ ? child_1.getPartialLh (TOP)
11602+ ->integrateMutations <num_states>(child_1_mutations, aln, true )
11603+ : nullptr ;
11604+ // 2. create the pointer that points to the appropriate regions
11605+ const std::unique_ptr<SeqRegions>* child_1_regions_ptr =
11606+ (child_1_mutations && child_1_mutations->size ())
11607+ ? &(mut_integrated_child_1_regions)
11608+ : &(child_1.getPartialLh (TOP));
11609+ // 3. create a reference from that pointer
11610+ auto & lower_regions_child_1 = *child_1_regions_ptr;
11611+
11612+
11613+ // 0. extract the mutations at child_2
11614+ std::unique_ptr<SeqRegions>& child_2_mutations =
11615+ node_mutations[child_2_index.getVectorIndex ()];
11616+ // 1. create a new regions that de-integrate the mutations, if any
11617+ std::unique_ptr<SeqRegions> mut_integrated_child_2_regions =
11618+ (child_2_mutations && child_2_mutations->size ())
11619+ ? child_2.getPartialLh (TOP)
11620+ ->integrateMutations <num_states>(child_2_mutations, aln, true )
11621+ : nullptr ;
11622+ // 2. create the pointer that points to the appropriate regions
11623+ const std::unique_ptr<SeqRegions>* child_2_regions_ptr =
11624+ (child_2_mutations && child_2_mutations->size ())
11625+ ? &(mut_integrated_child_2_regions)
11626+ : &(child_2.getPartialLh (TOP));
11627+ // 3. create a reference from that pointer
11628+ auto & lower_regions_child_2 = *child_2_regions_ptr;
1152911629
1153011630 // compute the likelihood we need to deduct when we un-merge the two children
1153111631 std::unique_ptr<SeqRegions> lower_regions_merged = nullptr ;
0 commit comments