Skip to content

Commit

Permalink
Corrected reroot outgroup function
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericlemoine committed Mar 26, 2018
1 parent 07cf645 commit 3c8ec62
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
14 changes: 8 additions & 6 deletions tree/algo.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,8 @@ func (t *Tree) RerootOutGroup(removeoutgroup bool, tips ...string) error {
if len(n.br)-len(edges) != 1 {
return errors.New("Reroot error: Several possible branches for root placement (multifurcated node)")
}
/**
We search the unique branch connecting "n" and that is not part of the outgroup
If there were several branches, there should have been an error above
*/
// We search the unique branch connecting "n" and that is not part of the outgroup
// If there were several branches, there should have been an error above
for _, e := range n.br {
found := false
for _, e2 := range edges {
Expand All @@ -402,6 +400,8 @@ func (t *Tree) RerootOutGroup(removeoutgroup bool, tips ...string) error {
break
}
}
// That branch (e) is not part of the ougroup
// => OK
if !found {
rootedge = e
break
Expand All @@ -423,7 +423,7 @@ func (t *Tree) RerootOutGroup(removeoutgroup bool, tips ...string) error {
// We retrieve all nodes of the
// subtree we want to remove
nodes := make([]*Node, 0, len(tips))
t.nodesRecur(&nodes, n, nil)
t.nodesRecur(&nodes, n, root)
for _, no := range nodes {
t.delNode(no)
}
Expand All @@ -449,7 +449,9 @@ func (t *Tree) RerootOutGroup(removeoutgroup bool, tips ...string) error {
ne2.SetSupport(support)
}
}
t.Reroot(root)
if err = t.reroot_nocheck(root); err != nil {
return err
}
if removeoutgroup {
t.UpdateTipIndex()
}
Expand Down
17 changes: 17 additions & 0 deletions tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,23 @@ func (t *Tree) Reroot(n *Node) error {
return err
}

// This function takes a node and reroots the tree on that node.
//
// It reorients edges left-edge-right : see ReorderEdges()
//
// It does not check wether the node is part of tree or not
//
// Returns an error If the given node is a tip or if it can
// not reorder edges
func (t *Tree) reroot_nocheck(n *Node) error {
if n.Nneigh() < 2 {
return errors.New("Cannot reroot on a tip node")
}
t.root = n
err := t.ReorderEdges(n, nil, nil)
return err
}

// This function reorders the edges of a tree in order to always have
// left-edge-right with left node being parent of right node with respect
// to the given root node.
Expand Down

0 comments on commit 3c8ec62

Please sign in to comment.