matUtils extract --reroot: retain rerooted leaf, rename leaf from former internal node #380
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This addresses two corner cases that we hadn't thought through yet for --reroot:
First, when rerooting to a leaf node, that node was converted into an internal node (the new root), losing its leaf status, so when the result was written out, the tree no longer had that leaf. To fix this, when rerooting to a leaf, we add a new leaf with the same name on the new root node.
Second, when the old root had no leaf children and only one internal node child, after rerooting it became a new leaf node, keeping its original ID of
node_1
. However, the saved protobuf could not be read back in by matUtils because when parsing the Newick string,node_1
is assigned to the first node encountered, and it is an error for any subsequent node to have the namenode_1
(even a leaf) becausenode_1
is already in the tree. To prevent that error, when the old root becomes a leaf node, we change the old-root-new-leaf's name toformer_root
-- unless there is already aformer_root
in the tree, in which case we make a newformer_root_
serialNumber that does not already exist in the tree.