Reading & plotting phylogenies
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
This is a basic procedure, but could come handy. I have been reading and doing a basic manipulations with phylogenetic trees a lot lately, so there is a chunk of code for this.
> library (ape) # ape is a basic R package for phylogenetics
> setwd (“C:/Users/Jana/Desktop/primates”) # if you want to read your trees, you should set a name and the path for the directory first.
> getwd () # you can assure yourself that the path to the directory is set right
[1] “C:/Users/Jana/Desktop/primates”
> Lemuri <- read.nexus (“Lemuriformes_10k.nex”) #you can read trees into R in Newick or Nexus format. When reading Newick trees, just use: “read.tree” function instead and don’t forget using .tre suffix
> summary (Lemuri) # look at the data
Length Class Mode
1 4 phylo list
2 4 phylo list
3 4 phylo list
There are multiple trees stored in an object Lemuri right now, so the class of the object Lemuri is of a multi.phylo format and each tree is in the class phylo.
> Lemuri1 <- Lemuri [[1]] # now we picked the first tree and stored it as an object Lemuri1
> summary (Lemuri1)
Phylogenetic tree: Lemuri1
Number of tips: 76
Number of nodes: 75
Branch lengths:
mean: 4.011496
variance: 36.88848
distribution summary:
Min. 1st Qu. Median 3rd Qu. Max.
0.01254 0.89660 2.15400 4.61200 51.27000
No root edge.
First ten tip labels: Allocebus_trichotis
Archaeolemur_majori
Avahi_cleesei
Avahi_laniger
Avahi_occidentalis
Avahi_unicolor
Cheirogaleus_crossleyi
Cheirogaleus_major
Cheirogaleus_medius
Daubentonia_madagascariensis
No node labels.
The phylogeny is dated, so there are some statistics of dispersion, i.e. quantiles and variance, and the statistics of central tendency (mean) for branch length. Tree has no polytomies, because there is one internal node less than the number of external nodes (tips). You can ask if the tree is ultrametric, that is if all branches have the same length:
> is. ultrametric ()
TRUE
Tree was calibrated with fossils, therefore length of all branches from the root is the same.
> plot (Lemuri1, cex = 0.6) # plotting the tree with a smaller tip labels. Command cex sets scaling of text and plotting symbols. 1 = default, 0.6 = 60% smaller
> plot (Lemuri.out1)
> nodelabels () # shows all labels for internal nodes
> plot (Lemuri1)
> nodelabels (Lemuri.out2$node.label [[64, 82]]) # you can show labels for some nodes only
> Lemuri2 <- drop.tip (Lemuri1, c (“Eulemur_fulvus_sanfordi”))
We have pruned one branch from the tree and then saved modified tree as a different object Lemuri2.
Instead of tedious writing down of taxa for subsequent pruning, we can extract whole subtree starting with a given internal node.
> Lemuri3 <- extract.clade (Lemuri1, node = 60)
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.