R Hackathon 1/InputtingTrees
The commands referenced below are all part of special phylogenetic packages in R, not the basic R install. Be sure that you have installed and loaded the packages containing the commands referenced below before continuing. For example:
This loads the package ape and its required packages gee, lattice and ade4 into your R session.
How do I input a phylogeny into R?
Okay, so you've just completed a phylogenetic analysis or otherwise obtained a file containing a phylogeny, and you'd like to use that phylogeny as a framework for comparative analysis in R.
The package APE can read phylogenies in either Newick or NEXUS format using the read.tree and read.nexus commands, respectively.
MyTree <- read.tree("MyNewickTreefile.tre") MyTree <- read.nexus("MyNexusTreefile.nex")
Either of these commands will create an identical object called "MyTree" in R containing your phylogeny. This object is in "phylo" format (more specifically, it is an object of class "phylo" and it can be used as input to various other functions in R.
Note that when you construct the original Newick or NEXUS files your taxon names should appear without quotes. R and APE support the use of translation tables for taxon names in NEXUS format, but not in Newick format.
How do I export a phylogeny out of R?
The package APE can export phylogenies in either Newick or Nexus format using the write.tree and write.nexus commands, respectivly.
write.tree(MyTree, file="MyNewickTreefile.tre" write.nexus(Mytree, file="MyNexusTreefile.nex"
What if I'm working with multiple trees?
No problem! If the Newick or NEXUS file that you read into R contains multiple trees, a object called a list will be created. Each element of the list will be one individual tree from the original datafile.
You can also write multiple trees to a single file from R. If the trees are already in a list, the syntax is exactly the same as above.