Skip to content

Commit 140e35f

Browse files
committed
reset blengths if it fails to optimize
1 parent f6ed49e commit 140e35f

File tree

2 files changed

+83
-11
lines changed

2 files changed

+83
-11
lines changed

tree/tree.cpp

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3232,9 +3232,9 @@ bool cmaple::Tree::examineSubtreePlacementMidBranch(
32323232

32333233
// if bottom_regions is null (inconsistent) -> we can't optimize blengths
32343234
if (!bottom_regions) {
3235-
best_appending_blength = removed_blength;
3236-
best_mid_top_blength = at_node.getUpperLength() * 0.5;
3237-
best_mid_bottom_blength = best_mid_top_blength;
3235+
resetOptBlengths(best_appending_blength, best_mid_top_blength,
3236+
best_mid_bottom_blength, removed_blength,
3237+
at_node.getUpperLength() * 0.5);
32383238
}
32393239
// otherwise, optimize blengths
32403240
else
@@ -3257,20 +3257,46 @@ bool cmaple::Tree::examineSubtreePlacementMidBranch(
32573257
// 3. create a reference from that pointer
32583258
auto& upper_lr_regions = *upper_lr_regions_ptr;
32593259

3260+
bool reset_blengths = false;
32603261
std::unique_ptr<SeqRegions> two_lower_regions = nullptr;
32613262
const RealNumType mid_branch_length = at_node.getUpperLength() * 0.5;
32623263
bottom_regions->mergeTwoLowers<num_states>(two_lower_regions,
32633264
mid_branch_length, *subtree_regions, best_appending_blength,
32643265
aln, model, cumulative_rate, threshold_prob);
3265-
best_mid_top_blength = estimateBranchLength<num_states>(upper_lr_regions, two_lower_regions);
3266+
// optimize best_mid_top_blength
3267+
if (two_lower_regions) {
3268+
best_mid_top_blength = estimateBranchLength<num_states>
3269+
(upper_lr_regions, two_lower_regions);
3270+
}
3271+
// otherwise, reset blengths
3272+
else
3273+
{
3274+
reset_blengths = true;
3275+
}
32663276

32673277
// optimize the mid_bottom blength
32683278
std::unique_ptr<SeqRegions> tmp_upper_lr_regions = nullptr;
32693279
upper_lr_regions->mergeUpperLower<num_states>(
32703280
tmp_upper_lr_regions, best_mid_top_blength, *subtree_regions,
32713281
best_appending_blength, aln, model, threshold_prob);
3272-
best_mid_bottom_blength = estimateBranchLength<num_states>
3273-
(tmp_upper_lr_regions, bottom_regions);
3282+
// optimize best_mid_bottom_blength
3283+
if (tmp_upper_lr_regions) {
3284+
best_mid_bottom_blength = estimateBranchLength<num_states>
3285+
(tmp_upper_lr_regions, bottom_regions);
3286+
}
3287+
// otherwise, reset blengths
3288+
else
3289+
{
3290+
reset_blengths = true;
3291+
}
3292+
3293+
// reset all blengths (if needed)
3294+
if (reset_blengths)
3295+
{
3296+
resetOptBlengths(best_appending_blength, best_mid_top_blength,
3297+
best_mid_bottom_blength, removed_blength,
3298+
mid_branch_length);
3299+
}
32743300

32753301
// re-compute the mid-branch regions
32763302
upper_lr_regions->mergeUpperLower<num_states>(
@@ -3295,24 +3321,49 @@ bool cmaple::Tree::examineSubtreePlacementMidBranch(
32953321
}
32963322
// case we are moving from a parent to a child
32973323
else {
3298-
3324+
bool reset_blengths = false;
32993325
// optimize the mid_top blength
33003326
const std::unique_ptr<SeqRegions>& lower_regions =
33013327
current_node.getPartialLh(TOP);
33023328
std::unique_ptr<SeqRegions> two_lower_regions = nullptr;
33033329
const RealNumType mid_branch_length =
33043330
updating_node->getBranchLength() * 0.5;
33053331
lower_regions->mergeTwoLowers<num_states>(two_lower_regions, mid_branch_length,
3306-
*subtree_regions, best_appending_blength, aln, model, cumulative_rate, threshold_prob);
3307-
best_mid_top_blength = estimateBranchLength<num_states>(updating_node->getIncomingRegions(), two_lower_regions);
3332+
*subtree_regions, best_appending_blength, aln, model, cumulative_rate, threshold_prob);
3333+
// optimize best_mid_top_blength
3334+
if (two_lower_regions) {
3335+
best_mid_top_blength = estimateBranchLength<num_states>
3336+
(updating_node->getIncomingRegions(), two_lower_regions);
3337+
}
3338+
// otherwise, reset blengths
3339+
else
3340+
{
3341+
reset_blengths = true;
3342+
}
33083343

33093344
// optimize the mid_bottom blength
33103345
std::unique_ptr<SeqRegions> tmp_upper_lr_regions = nullptr;
33113346
updating_node->getIncomingRegions()->mergeUpperLower<num_states>(
33123347
tmp_upper_lr_regions, best_mid_top_blength, *subtree_regions,
33133348
best_appending_blength, aln, model, threshold_prob);
3314-
best_mid_bottom_blength = estimateBranchLength<num_states>(
3315-
tmp_upper_lr_regions, lower_regions);
3349+
// optimize best_mid_bottom_blength
3350+
if (tmp_upper_lr_regions) {
3351+
best_mid_bottom_blength = estimateBranchLength<num_states>(
3352+
tmp_upper_lr_regions, lower_regions);
3353+
}
3354+
// otherwise, reset blengths
3355+
else
3356+
{
3357+
reset_blengths = true;
3358+
}
3359+
3360+
// reset all blengths (if needed)
3361+
if (reset_blengths)
3362+
{
3363+
resetOptBlengths(best_appending_blength, best_mid_top_blength,
3364+
best_mid_bottom_blength, removed_blength,
3365+
mid_branch_length);
3366+
}
33163367

33173368
// re-compute the mid-branch regions
33183369
updating_node->getIncomingRegions()->mergeUpperLower<num_states>(
@@ -3395,6 +3446,17 @@ bool cmaple::Tree::examineSubtreePlacementMidBranch(
33953446
return true;
33963447
}
33973448

3449+
void cmaple::Tree::resetOptBlengths(RealNumType& best_appending_blength,
3450+
RealNumType& best_mid_top_blength,
3451+
RealNumType& best_mid_bottom_blength,
3452+
const RealNumType removed_blength,
3453+
const RealNumType mid_branch_length)
3454+
{
3455+
best_appending_blength = removed_blength;
3456+
best_mid_top_blength = mid_branch_length;
3457+
best_mid_bottom_blength = best_mid_top_blength;
3458+
}
3459+
33983460
// NOTE: top_node != null <=> case when crawling up from child to parent
33993461
// otherwise, top_node == null <=> case we are moving from a parent to a child
34003462
/*template <const StateType num_states>

tree/tree.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,16 @@ bool isDiffFromOrigPlacement(
799799
RealNumType& opt_mid_bottom_blength,
800800
std::vector<AltBranch>& alt_branches,
801801
bool& is_root_considered);
802+
803+
/**
804+
Reset blengths when we fail to optimize them
805+
*/
806+
void resetOptBlengths(
807+
RealNumType& best_appending_blength,
808+
RealNumType& best_mid_top_blength,
809+
RealNumType& best_mid_bottom_blength,
810+
const RealNumType removed_blength,
811+
const RealNumType mid_branch_length);
802812

803813
/**
804814
Examine placing a subtree as a descendant of an existing node

0 commit comments

Comments
 (0)