YuLab-SMU / treeio

:seedling: Base Classes and Functions for Phylogenetic Tree Input and Output
https://yulab-smu.top/treedata-book/
96 stars 26 forks source link

Added read.beast.tree and exported write_beast_newick #50

Closed brj1 closed 3 years ago

brj1 commented 3 years ago

Description

Added a method read.beast.tree to read untranslated newick BEAST trees with metadata. Also exported the write_beast_newick method in R/write-beast.R.

Recently, I tried to create starting trees for a BEAST2 run that uses the MultiTypeTree package. I found this quite challenging with available methods as read.tree removes tree metadata ([] after tip/node labels). Also writing newick trees with meta data for BEAST2 is annoying because you have to manually put the [] in the labels.

I also refactored read.beast (and internal functions) a bit so that it reads the file less and reruns less code.

Example

tree <- read.beast.tree(text = '(a[&rate=1]:2,(b[&rate=1.1]:1,c[&rate=0.9]:1)[&rate=1]:1);')
write_beast_newick(tree)
GuangchuangYu commented 3 years ago

I think we should rename write_beast_newick to write.beast.newick and read.beast.tree to read.beast.newick, so that the style of function names is consistent.

GuangchuangYu commented 3 years ago

to consistent with other parser functions in treeio, please only use file as input.

PS: user can use file = textConnection(text)

brj1 commented 3 years ago

I've made all 3 of the requested changes.