HowTo/InputtingTrees (R language)

http://www.r-phylo.org/wiki/HowTo/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:

  library(ape)

This loads the package ape and its required packages, gee, nlme and lattice, 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. R packages for phylogenetics may differ in how they represent phylogenies. The most commonly used is the “phylo” (more specifically, objects of class “phylo”). Luckily, converting your phylogeny to “phylo” representation is easy.

The package APE can read phylogenies in either Newick or NEXUS format using the read.tree and read.nexus commands, respectively. (Note: we assume you have already placed these input trees in your working directory, see the basics)

  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” representation and it can be input to various other functions in R.

If you have the sample Geospiza phylogeny saved to your working directory, this command will load it into memory.

  geotree <- read.nexus("geospiza.nex")

Note that when you construct the original Newick or NEXUS files you should avoid enclosing your taxon names within quotes or using blank space within taxon names. A taxon named Genus_species will work fine, one named “Genus species” will not. 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?

APE can export phylogenies in phylo representation into either Newick or Nexus format using the write.tree and write.nexus commands, respectively.

  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, an 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 (perhaps one called MyTrees), the syntax is the same as above.

  write.nexus(MyTrees, file="MyNexusTreefile.nex")

If trees are not in a list but exist instead as separate objects, you can type them as sequential arguments in write.nexus.

  write.nexus(MyTree1, MyTree2, MyTree3, file="MyNexusTreefile.nex")

Write.tree currently accepts a only single tree as input.

However, be aware that many functions in R are designed to accept only a single tree as input, not a a list of trees. You can apply a single function to all the elements of a list sequentially using the lapply and sapply commands. For more information on the usage of lapply and sapply, type either of the following into the R command line.

  help(lapply)
  help(sapply)

 


Credit: Most of the information on this page is based on the book Analysis of Phylogenetics and Evolution with R (Paradis, 2006).

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>