Skip to content

Commit 76b2957

Browse files
authored
fix: apply negative branch length correction iteratively (#45)
1 parent fb50bc6 commit 76b2957

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

workflow/scripts/report/allele_freq_tree_data.R

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ fix_negative_edge_length <- function(nj.tree) {
1919
) %>%
2020
as.data.table()
2121
colnames(edge_infos) <- c("from", "to", "length")
22-
nega_froms <- edge_infos[length < 0, sort(unique(from))]
23-
for (nega_from in nega_froms) {
24-
minus_length <- edge_infos[from == nega_from, ][order(length)][1, length]
25-
edge_infos[from == nega_from, length := length - minus_length]
26-
edge_infos[to == nega_from, length := length + minus_length]
22+
while (any(edge_infos$length < 0)) {
23+
nega_froms <- edge_infos[length < 0, sort(unique(from))]
24+
for (nega_from in nega_froms) {
25+
minus_length <- edge_infos[from == nega_from, ][order(length)][1, length]
26+
edge_infos[from == nega_from, length := length - minus_length]
27+
edge_infos[to == nega_from, length := length + minus_length]
28+
}
2729
}
2830
nj.tree$edge.length <- edge_infos$length
2931
nj.tree

0 commit comments

Comments
 (0)