您可以使用该ape::read.tree()
功能来读取您的 newick 格式树
tree = "(A(C(H(I(Andrew))),D(O(J(Kevin)))),B(E(Charlie),F(K(M(Naomi))),G(L(N(Sam)))));"
my_tree <- read.tree(text = tree)
plot(my_tree)
然后,您可以使用ape::write.tree
将树保存到 newick 文件中:
write.tree(my_tree, file = "my_file_name.tre")
要将表格转换为"phylo"
对象,ape
您可以使用此功能(可能需要一些调整):
## The function
data.frame.to.phylo <- function(sample){
## Making an edge table
edge_table <- rbind(
## The root connecting A to B
rbind(c("root", "A"),c("root", "B")),
## All the nodes connecting to the tips
cbind(sample$stock, sample$name)
)
## Translating the values in the edge table into edge IDs
## The order must be tips, root, nodes
element_names <- c(unique(sample$name), "root", unique(sample$stock))
element_ids <- seq(1:length(element_names))
## Looping through each ID and name
for(element in element_ids) {
edge_table <- ifelse(edge_table == element_names[element], element_ids[element], edge_table)
}
## Make numeric
edge_table <- apply(edge_table, 2, as.numeric)
## Build the phylo object
phylo_object <- list()
phylo_object$edge <- edge_table
phylo_object$tip.label <- unique(sample$name)
phylo_object$node.label <- c("root", unique(sample$stock))
phylo_object$Nnode <- length(phylo_object$node.label)
## Forcing the class to be "phylo"
class(phylo_object) <- "phylo"
return(phylo_object)
}
## The data
sample = data.frame("stock" = c("A", "A", "B", "B", "B"), "mbranch" = c("C", "D", "E", "F", "G"), "sbranch" = c("H", "O", NA, "K", "L"), "lsbranch" = c("I", "J", NA, "M", "N"), "name" = c("Andrea", "Kevin", "Charlie", "Naomi", "Sam"))
## Plotting the data.frame for testing the function
plot(data.frame.to.phylo(sample))
干杯,托马斯