HowTo/InputtingTrees: Difference between revisions

From Comparative Phylogenetics in R
Jump to navigation Jump to search
No edit summary
 
mNo edit summary
Line 1: Line 1:
The commands referenced below are all part of special phylogenetic packages in R, not the basic R install. Be sure that you have [[GettingStarted|installed and loaded the packages]] containing the commands referenced below before continuing. For example:
The commands referenced below are all part of special phylogenetic packages in R, not the basic R install. Be sure that you have [[HowTo/GettingStarted|installed and loaded the packages]] containing the commands referenced below before continuing. For example:


   library(ape)
   library(ape)

Revision as of 15:01, 6 February 2008

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. There are several different phylogeny representations used in various R packages, but the most common is "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.

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.tree(MyTrees, file="MyNewickTreefile.tre")#Currently not possible
  

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

  write.tree(MyTree1, MyTree2, MyTree3, file="MyNewickTreefile.tre")#Currently not possible
  write.nexus(MyTree1, MyTree2, MyTree3, file="MyNexusTreefile.nex")#Currently possible

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 paraphrased from the book Analysis of Phylogenetics and Evolution with R (Paradis, 2006).

Test stuff here

Example file attachments

  • rda doesn't work yet because the format is not: rdafile