Skip to content

Commit

Permalink
gotree ltt takes node dates from the tree if present, otherwise in mu…
Browse files Browse the repository at this point in the history
…tations
  • Loading branch information
fredericlemoine committed Nov 30, 2023
1 parent 6e5bc94 commit 8ba9125
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
14 changes: 14 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2166,3 +2166,17 @@ EOF
echo "(((A:1,B:1):1,C:2):3,(D:4,(E:3,((F:1.5,G:1.5):0.5,H:2):1):1):1);" | $GOTREE ltt > result
diff -q -b expected result
rm -f expected result

echo "->gotree ltt with dates"
cat > expected <<EOF
0 2019.000000 2
0 2020.000000 3
0 2021.000000 4
0 2022.000000 6
0 2022.500000 7
0 2023.000000 8
0 2024.000000 0
EOF
echo '(((A[&date="2024"]:1,B[&date="2024"]:1)[&date="2023"]:1,C[&date="2024"]:2)[&date="2022"]:3,(D[&date="2024"]:4,(E[&date="2024"]:3,((F[&date="2024"]:1.5,G[&date="2024"]:1.5)[&date="2022.5"]:0.5,H[&date="2024"]:2)[&date="2022"]:1)[&date="2021"]:1)[&date="2020"]:1)[&date="2019"];' | $GOTREE ltt > result
diff -q -b expected result
rm -f expected result
3 changes: 2 additions & 1 deletion tree/algo.go
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,8 @@ func (t *Tree) LTT() (lttdata []LTTData) {
// If the field [&date=] exists, then takes it
// Otherwise, computes the distance to the root
if dists, err = t.NodeDates(); err != nil {
io.LogError(err)
io.LogWarning(err)
io.LogWarning(fmt.Errorf("using mutations instead of dates"))
dists = t.NodeRootDistance()
}

Expand Down
22 changes: 13 additions & 9 deletions tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -966,26 +966,30 @@ func (t *Tree) NodeDates() (ndates []float64, err error) {
pattern = regexp.MustCompile(`(?i)&date="(.+)"`)
nnodes := 0
t.PreOrder(func(cur *Node, prev *Node, e *Edge) (keep bool) {
stop := false
keep = true
if cur.Id() != nnodes {
err = fmt.Errorf("node id does not correspond to postorder traversal: %d vs %d", cur.Id(), nnodes)
stop = true
} else {
keep = false
} else if len(cur.Comments()) > 0 {
keep = false
for _, c := range cur.Comments() {
matches = pattern.FindStringSubmatch(c)
if len(matches) < 2 {
err = fmt.Errorf("no date found: %s", c)
stop = true
}
if date, err = strconv.ParseFloat(matches[1], 64); err != nil {
} else if date, err = strconv.ParseFloat(matches[1], 64); err != nil {
err = fmt.Errorf("one of the node date is malformed: %s", c)
stop = true
} else {
ndates = append(ndates, date)
err = nil
keep = true
}
ndates = append(ndates, date)
}
} else {
err = fmt.Errorf("a node with no date found")
keep = false
}
nnodes += 1
return !stop
return
})
return
}
Expand Down

0 comments on commit 8ba9125

Please sign in to comment.