问题
The answer and question is related to this post: Change tip labels in phylogeny in R. I have old tip labels I want to replace with new tip labels because of taxonomic changes. How would I do this with a multiPhylo object in R? I have a random distribution of trees from the posterior I would like to update with new taxonomic names.
回答1:
multiPhylo
objects are simply lists of phylo
objects so the best way to modify them is to use lapply
.
First you should define the function allowing to rename tips
## Function for renaming tips
rename.tips.phylo <- function(tree, names) {
tree$tip.label <- names
return(tree)
}
You can then apply it to as many trees as you'd like:
## Generating 50 random 5 tips trees
random_trees <- rmtree(50, 5)
## Renaming all the tips
random_trees_renamed <- lapply(random_trees, rename.tips.phylo,
names = c("A", "B", "C", "D", "E"))
However, the output will be a list so you'll have to manually switch the class back to multiPhylo
:
## Class of the original object
class(random_trees)
#[1] "multiPhylo"
## Class of the renamed trees
class(random_trees_renamed)
#[1] "list"
## Changing the class
class(random_trees_renamed) <- "multiPhylo"
class(random_trees_renamed)
#[1] "multiPhylo"
And now all your tip labels have been renamed:
## First tree
random_trees_renamed[[1]]$tip.label
#[1] "A" "B" "C" "D" "E"
## 42nd tree
random_trees_renamed[[42]]$tip.label
#[1] "A" "B" "C" "D" "E"
来源:https://stackoverflow.com/questions/57680924/changing-tip-labels-for-a-multiphylo-object