Description: This script provides examples of the new functions available in the phangorn library to ‘intertwine’ trees and networks, i.e. compare trees and networks and data transferrance. It also provides a step-by-step guide for users new to R.

Methodological advancement: The major advancement in this phangorn update is the introduction of a generic network object with a wide range of related transfer and analysis functions. These new functions provide the first means to directly transfer information amongst a wide range of phylogenetic trees and networks, as well as means to visualise and further analyse this information. This should provide a platform for individuals to easily conduct tree-network comparisons and stimulate further function development by the community.

What next?: By implementing full network handling compatibility in R, and providing exemplar scripts (such as this) and support, the scientific community now has an easy means to analyse and compare the results of phylogenetic trees vs. network approaches. We hope this will open a largely unexplored world to the general phylogenetic audience.

Installing R

  1. Download R
    Select the nearest mirror to your location at
  2. Select your operating system and download the relevant installation file.
  3. Install R following the instructions.

Installing the phangorn library

Open R and run the two lines of code below in the command line.
(You will need to select a region from which to download the library)

install.packages("phangorn", dependencies=TRUE)
# install latest development version needs devtools
install.packages("devtools", dependencies=TRUE)

Getting started

library(phangorn)    # load the phangorn library
## Loading required package: ape

Set the working directory

This is often a major stumbling block for new R users. You need to specify where in your folder structure you wish to work. i.e, where are the files stored that you wish to input?

This is done using the setwd() function, e.g. setwd(“C:/TreesNetworks/Example Files”).

We now set it to the folder of the phangorn package, which contains the files we want to load for this example.

Read in the example file datasets:

This example files are based on the woodmouse dataset available in the ape library. Ultimately, this dataset is from this study: Michaux, J. R., Magnanou, E., Paradis, E., Nieberding, C. and Libois, R. (2003) Mitochondrial phylogeography of the Woodmouse (Apodemus sylvaticus) in the Western Palearctic region. Molecular Ecology, 12, 685-697.)

## automatically set the correct working directory for the examples below
setwd(system.file("extdata/trees", package = "phangorn"))
## in your case it may look something like this...
# setwd("C:/TreesNetworks/Example Files")

##DNA Matrix, maybe not needed 
woodmouse <- read.phyDat("woodmouse.fasta",format="fasta") 
## RAxML best-known tree with bipartition support (from previous analysis)
raxml.tree <- read.tree("RAxML_bipartitions.woodmouse")
## RAxML bootstrap trees (from previous analysis)
raxml.bootstrap <- read.tree("RAxML_bootstrap.woodmouse")
## MrBayes consensus tree (50% majority rule) (from previous analysis)
mrbayes.tree <-"woodmouse.mrbayes.nex.con")
## MrBayes sample runs 1 and 2 (from previous analysis)
run1 <-"woodmouse.mrbayes.nex.run1.t")
run2 <-"woodmouse.mrbayes.nex.run2.t")
## How many trees are in the MrBayes tree sample?
## 1001 phylogenetic trees
## 1001 phylogenetic trees
## Combining the two runs and removing 25% burn-in
mrbayes.trees <- c(run1[251:1001],run2[251:1001])
## NeigbourNet Nexus file generated by SplitsTree (from previous analysis)
Nnet <-"woodmouse.nxs")

All example files read into R.

Viewing the data

par(mfrow=c(1,2), mar=c(2,2,2,2)) # Setting plot parameters
### Plotting trees with support values:
##  RAxML
nodelabels(raxml.tree$node.label, adj = c(1, 0), frame = "none")
##  MrBayes
nodelabels(mrbayes.tree$node.label, adj = c(1, 0), frame = "none")

par(mfrow=c(1,1)) # Setting plot parameters
# NeighbourNet

## alternatively,
# plot(Nnet,"2D")

Interwining trees and network functions


Identification of edge bundles (in black) in a neighbour-net (NN) network that correspond to branches (labelled 1-12) in a tree (a maximum likelihood tree, in this case).

# create a vector of labels for the network corresponding to edges in the tree
edge.lab <- createLabel(Nnet, raxml.tree, raxml.tree$edge[,2], "edge")
# could be also 1:27 instead of raxml.tree$edge[,2]

# Show the correspondingly labelled tree and network in R
par(mfrow=c(1,2), mar=c(2,2,2,2))
#plotBS(raxml.tree, rotate.tree = 180) 
plot(raxml.tree, "u", rotate.tree = 180) 
edgelabels(raxml.tree$edge[,2],col="blue", frame="none")

# find edges that are in the network but not in the tree
edge.col <- rep("black", nrow(Nnet$edge))
edge.col[ ] <- "red"
# or a simpler alternative...
edge.col <- createLabel(Nnet, raxml.tree, "black", nomatch="red")

x <- plot.networx(Nnet, edge.label = edge.lab, show.edge.label = T, "2D", edge.color = edge.col,
                  col.edge.label = "blue")