We often don’t root phylogenetic trees. This reduces the number of possible trees and is described by the equation:
Text: (2n-5)!/[2n-3*(n-3)!]
Rendered:
\(\frac{(2*n-5)!}{2^{n-3} * (n-3)!}\)
Modify the function used in the “number of phylogenetic trees” tutorial to work for unrooted trees.
Compare your results to http://carrot.mcb.uconn.edu/mcb396_41/tree_number.html
You can use the simplest form of the function which doesn’t have any additional argument, eg
The function is here; make the necessary changeas
# change the code below to work for an un-rooted tree
tree_count_unrooted <- function(n = 3){
# change the denominator if need
numerator <- factorial(2*n-5)
# change the denominator if needed
denominator <- 2^(n-3)*factorial(n-3)
trees <- numerator / denominator
print(trees)
if(n<3) {warning("This function is only valid for n>2")}
}
Create a function that will work for rooted OR unrooted trees. Do this by adding an additional argument like
type = “rooted”
and conditional additional statements like
if(type == "rooted){ #do this }
if(type == "unrooted){ #do something else }
Again, you can use the simplest form of the argument.
tree_count_varying <- function(n = 3, style = "rooted, unrooted"){
if(style %in% "unrooted") {numerator <- factorial(2*n-5)
denominator <- 2^(n-3)*factorial(n-3) }
if(style %in% "rooted") {numerator <- factorial(2*n-3)
denominator <- 2^(n-2)*factorial(n-2)}
trees <- numerator / denominator
print(trees)
if(n<3) {warning("This function is only valid for unrooted trees: n > 2 and for rooted trees: n > 1")}}