Skip to content

Commit

Permalink
added functions for getting edge sets
Browse files Browse the repository at this point in the history
  • Loading branch information
Casey Dunn committed Apr 12, 2017
1 parent 26f9066 commit 0585b48
Show file tree
Hide file tree
Showing 28 changed files with 126 additions and 23 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ Suggests:
roxygen2,
knitr
VignetteBuilder: knitr
RoxygenNote: 5.0.1
RoxygenNote: 6.0.1
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Generated by roxygen2: do not edit by hand

export(ancestral_edges)
export(are_bipartitions_compatible)
export(bipartition_for_edge)
export(bipartition_for_edge_by_label)
export(compatible_edges)
export(connecting_edges)
export(cut_tree)
export(decompose_tree)
export(descendants)
Expand Down
50 changes: 50 additions & 0 deletions R/utility_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,53 @@ distance_from_tip = function(phy){

return(ages)
}

#' For a node in a tree, get a vector of the edges between the
#' node and the root
#'
#' @param phy A phylo object
#' @param node A node number
#' @return A vector of edge numbers
#' @export
ancestral_edges = function(phy, node){
edges = c()
focal_node = node

# While there is an edge with child focal node,
# add the edge and then sqitch the focal node to its
#parent.
while( focal_node %in% phy$edge[ ,2 ] ){
new_edge = which( phy$edge[ ,2 ] == focal_node )
edges = c( edges, new_edge )
focal_node = phy$edge[ new_edge, 1 ]
}

return(edges)
}

#' For two nodes in a tree, get a vector of the edges that
#' connect them
#'
#' @param phy A phylo object
#' @param node_a Number of the first node
#' @param node_b Number of the second node
#' @return A vector of edge numbers
#' @export
connecting_edges = function(phy, node_a, node_b){

# For each node, get the set of edges that connect
# it to the root
edges_a = ancestral_edges( phy, node_a )
edges_b = ancestral_edges( phy, node_b )

# The path between the nodes is the set of edges that
# are present in one but not the other, ie xor. This
# can be obtained by taking the union and then removing
# the intersection
edges = setdiff(
union( edges_a, edges_b ),
intersect( edges_a, edges_b )
)

return(edges)
}
21 changes: 21 additions & 0 deletions man/ancestral_edges.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/are_bipartitions_compatible.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/bipartition_for_edge.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/bipartition_for_edge_by_label.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/compatible_edges.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/connecting_edges.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/cut_tree.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/decompose_tree.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/descendants.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/difference_from_calibrated.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/distance_from_tip.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/flip_bipartition.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/get_bipartitions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/get_corresponding_nodes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/hutan.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/is_compatible_with_set.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/is_monophyletic.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/safe.drop.tip.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/siphonophore_constraint.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/siphonophore_ml.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/slide_root_edges.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/tip_descendants.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/tips.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/zero_constrained.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions tests/testthat/test_general.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,32 @@ test_that("the correct number of depths are produced",{
expect_equal( length(depths), 11 )
})

test_that("can get the edges that connect a tip to the root",{
edges = ancestral_edges( siphonophore_ml, 2 )
expect_true( setequal( edges, c( 3, 2 ) ) )
})

test_that("can get the edges that connect a tip to the root",{
edges = ancestral_edges( siphonophore_ml, 10 )
expect_true( setequal( edges, c( 30, 29, 28, 26, 24, 23, 22, 12, 11, 10, 9, 8, 7, 6, 5, 4, 2 ) ) )
})

test_that("can get the edges that connect an internal node to the root",{
edges = ancestral_edges( siphonophore_ml, 65 )
expect_true( setequal( edges, c( 10, 9, 8, 7, 6, 5, 4, 2 ) ) )
})

test_that("can get the connecting edges for sister species",{
edges = connecting_edges( siphonophore_ml, 31, 32)
expect_true( setequal( edges, c( 71, 72 ) ) )
})

test_that("can get the connecting edges for tips that span root",{
edges = connecting_edges( siphonophore_ml, 1, 32)
expect_true( setequal( edges, c( 1, 72, 70, 69, 67, 66, 58, 57, 11, 10, 9, 8, 7, 6, 5, 4, 2 ) ) )
})

test_that("can get the connecting edges for ancestor and descendent nodes",{
edges = connecting_edges( siphonophore_ml, 65, 67)
expect_true( setequal( edges, c( 11, 12 ) ) )
})

0 comments on commit 0585b48

Please sign in to comment.