Skip to content

Commit

Permalink
Allow retention of internal nodes with only one outbound connection a…
Browse files Browse the repository at this point in the history
…fter droptips!().
  • Loading branch information
richardreeve committed Oct 14, 2024
1 parent c688663 commit 9830223
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"applicationCategory": "ecology",
"codeRepository": "https://github.com/EcoJulia/Phylo.jl",
"dateCreated": "2017-04-21",
"dateModified": "2024-10-13",
"dateModified": "2024-10-14",
"datePublished": "2018-08-11",
"description": "Package for creating and manipulating phylogenies in Julia",
"downloadUrl": "https://github.com/EcoJulia/Phylo.jl/archive/refs/tags/v0.6.0.tar.gz",
Expand Down
54 changes: 30 additions & 24 deletions src/trim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,26 @@ include tips or root.
getinternalnodes(t::AbstractTree) = collect(nodenamefilter(isinternal, t))

"""
droptips!(tree::AbstractTree{OneTree}, tips)
droptips!(tree::AbstractTree{OneTree}, tips; keep = false)
Function to drop tips from a phylogenetic tree `tree`, which are found in
the vector of tips or tip names, `tips`.
the vector of tips or tip names, `tips`. `keep` determines whether to keep
internal and root nodes that now only have one child (default is `false`).
Internal nodes with no children will always be removed.
"""
function droptips! end
@traitfn function droptips!(tree::T,
tips::AbstractVector{N}) where
{N, RT,
T <: AbstractTree{OneTree, RT, N}; !MatchNodeType{T, N}}
return isempty(tips) ? N[] :
droptips!(tree, [getnode(tree, tip) for tip in tips])
tips::AbstractVector{N};
keep = false) where
{N, RT, NL,
T <: AbstractTree{OneTree, RT, NL}; !MatchNodeType{T, N}}
return isempty(tips) ? NL[] :
droptips!(tree, [getnode(tree, tip) for tip in tips], keep = keep)
end

@traitfn function droptips!(tree::T,
tips::AbstractVector{N}) where
tips::AbstractVector{N};
keep = false) where
{N, RT, NL,
T <: AbstractTree{OneTree, RT, NL}; MatchNodeType{T, N}}
keep_tips = N[tip for tip in getleaves(tree) if tip tips]
Expand All @@ -45,26 +49,28 @@ end
map(x -> deletenode!(tree, x), nodes)
end

# Merge internal nodes that no longer have multiple children
while any(map(x -> length(getchildren(tree, x)) .< 2,
getinternalnodes(tree)))
inner_nodes = getinternalnodes(tree)
remove_nodes = findall(map(x -> length(getchildren(tree, x)) .< 2,
inner_nodes))
for i in remove_nodes
parent = getparent(tree, inner_nodes[i])
parentbranch = getinbound(tree, inner_nodes[i])
if !keep
# Merge internal nodes that no longer have multiple children
while any(map(x -> length(getchildren(tree, x)) .< 2,
getinternalnodes(tree)))
inner_nodes = getinternalnodes(tree)
remove_nodes = findall(map(x -> length(getchildren(tree, x)) .< 2,
inner_nodes))
for i in remove_nodes
parent = getparent(tree, inner_nodes[i])
parentbranch = getinbound(tree, inner_nodes[i])

child = getchildren(tree, inner_nodes[i])[1]
childbranch = getoutbounds(tree, getnode(tree, inner_nodes[i]))[1]
child = getchildren(tree, inner_nodes[i])[1]
childbranch = getoutbounds(tree, getnode(tree, inner_nodes[i]))[1]

len = distance(tree, parent, child)
len = distance(tree, parent, child)

deletebranch!(tree, parentbranch)
deletebranch!(tree, childbranch)
deletenode!(tree, inner_nodes[i])
deletebranch!(tree, parentbranch)
deletebranch!(tree, childbranch)
deletenode!(tree, inner_nodes[i])

createbranch!(tree, parent, child, len)
createbranch!(tree, parent, child, len)
end
end
end

Expand Down

0 comments on commit 9830223

Please sign in to comment.