Skip to content

Commit

Permalink
fix: avoid negative branch lengths
Browse files Browse the repository at this point in the history
  • Loading branch information
rneher committed Sep 18, 2023
1 parent 57c3f47 commit 1187b3c
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages_rs/nextclade/src/tree/tree_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,18 @@ pub fn knit_into_graph(
muts_new_node,
}
};
// if the node is a leaf or if there are shared mutations, need to split the branch above and insert aux node
// if the node is a leaf or if there are non-shared mutations, need to split the branch above and insert aux node
if target_node.is_leaf() || !muts_target_node.nuc_muts.is_empty() {
// determine divergence of new internal node by subtracting shared reversions from target_node
let divergence_middle_node =
target_node_div - calculate_branch_length(&muts_target_node.nuc_muts, divergence_units, ref_seq_len);
let divergence_middle_node = if target_node.is_root() {
target_node_div - calculate_branch_length(&muts_target_node.nuc_muts, divergence_units, ref_seq_len)
} else {
let parent_node = graph.parent_of(target_node).unwrap();
let parent_node_auspice = parent_node.payload();
let parent_node_div = &parent_node_auspice.node_attrs.div.unwrap_or(0.0);
target_node_div
.min(parent_node_div + calculate_branch_length(&muts_common_branch.nuc_muts, divergence_units, ref_seq_len))
};

// generate new internal node
// add private mutations, divergence, name and branch attrs to new internal node
Expand Down

0 comments on commit 1187b3c

Please sign in to comment.