Tree format conversion in R

From the life of a biologist/bioinformatician

In my Inbox on Monday morning:

Hi Michael, do you know how can I convert a tree file in nexus format to a phylip format file, using R? Best, A.

Sure. Here’s how you do it:

> library(phybase)
> tree.string.example = "#NEXUS\nBegin trees;\n\tTranslate\n\t\t1 a,\n\t\t2 b,\n\t\t3 c,\n\t\t4 d,\n\t\t5 e,\n\t\t6 f,\n\t\t7 g\n;\ntree TEST =((4:0.733,((7:0.004,1:0.004):0.287,2:0.292):0.440):0.0596,(3:0.509,(6:0.112,5:0.112):0.396):0.283);\nEnd;"
> cat(tree.string.example, file="test.nex")
> = phybase::read.tree.string("test.nex", format="nexus")
[1] "((4:0.733,((7:0.004,1:0.004):0.287,2:0.292):0.440):0.0596,(3:0.509,(6:0.112,5:0.112):0.396):0.283);"

[1] "a" "b" "c" "d" "e" "f" "g"

[1] TRUE

> phybase::write.tree.string($tree, file="test.phy", format="phylip")

One thought on “Tree format conversion in R

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>