0

I am currently working on creating a tanglegram where species reared are on one side and substrate reared on is on the other side. However, I need the output to also be a tanglegram, having each side of the chart be the tips of a dendogram. I currently have phylogenies of both the species and substrates in Newick formats.

I successfully used the 'ape' package and the plot.phylo() function to generate the 2 phylogenies. I then used the following code from this post to export the order of tips:

tree <- ladderize(tree, right = FALSE)
is_tip <- tree$edge[,2] <= length(tree$tip.label)
ordered_tips <- tree$edge[is_tip, 2]
x<-tree$tip.label[ordered_tips]
write.csv(x,file="test.csv",sep ="\t")

I then used the order of the tips to generate a matrix for use in the 'bipartite' package.

However, of course, the spacing on the bipartite graph doesn't match up with the spacing on the dengdrograms, so I can't just copy/paste them next to one another in an outside image manipulation program. I am wondering if there is a way that I can generate a chart that combines the dendrograms and bipartite graph to make a tanglegram in rstudio?

Here is a simplified visual example of what I am hoping to do:

I am looking to combine 2 phylogenetic trees that, for example, look like this

tree1<-read.tree(text="((C,B),A);")
plot(tree1)

Output: tree1

tree2<-read.tree(text="((G,F),(E,D));")
plot(tree2)

Output: tree2

With a bipartite graph

web = matrix(
c(0, 5, 0, 10, 10, 0, 0, 3, 0, 0, 0, 1),
nrow=4,
ncol=3,
byrow = TRUE,
dimnames = list(c("D","E","F","G"),c("A","B","C"))) 
plotweb(web,method="normal",empty="false",text.rot="90")

Output: bipartite plot

To instead generate a plot that looks like this (I just did the following in an image editor, the expansive dataset I am actually using is far larger)

Desired output: tanglegram

talker90
  • 79
  • 8

2 Answers2

2

A way to draw a tanglegram with trees where the tip labels do not match is to use an association matrix - a matrix that lists corresponding taxa in the respective trees. Using the provided data, the following code will extract the names from columns and rows and the respective weight.

 assoc = data.frame()
 temp = matrix(NA, ncol = 3, nrow = ncol(web))

 for(i in 1:nrow(web)){
     for(j in 1:ncol(web)){
         temp[j,] = c(rownames(web)[i], colnames(web)[j], web[i,j])
     }
     assoc = rbind(assoc, temp)
 }
head(assoc)
  V1 V2 V3
 1  D  A  0
 2  D  B  5
 3  D  C  0
 4  E  A 10
 5  E  B 10

The last column can be used to set weight of the connecting lines between taxa.

library(ape)
cophyloplot(tree2, tree1, assoc[,1:2], lwd = as.numeric(as.character(assoc[,3])), space = 15)

enter image description here

nya
  • 2,138
  • 15
  • 29
  • Thanks very much! Beautiful job! That's very nice. For my actual dataset I ended up needing to add these two lines as well `tree1<-collapse.singles(tree1)` `tree2<-collapse.singles(tree2)` I didn't need to in this case, but [this reference](https://bitsaying.wordpress.com/2015/09/22/tired-of-what-apes-collapse-singles-does-heres-a-way-to-put-those-nodes-back/) might be useful for others if they are trying to conserve branch lengths after collapsing singles – talker90 Oct 26 '18 at 00:25
0

You can use the function cophylo from the phytools package:

require(phytools)
## Your two trees
tree1 <- ape::rcoal(10)
tree2 <- ape::rcoal(10)

If both trees have matching tip labels, you can pass them to the cophylo functions in phytools to create a "cophylo" object:

## Creating the tanglegram
tanglegram <- phytools::cophylo(tree1, tree2)
# Rotating nodes to optimize matching...
# Done.

And plotting using the S3 "cophylo" method:

## Plotting the tanglegram
plot(tanglegram)

Have a look at this excellent blog post containing more details on how to use these "cophylo" objects.

Thomas Guillerme
  • 1,747
  • 4
  • 16
  • 23
  • Hello! Thanks for your response and for the blog post! My problem is that I am trying to pass through 2 trees with different tip labels (one tree of species reared and one tree of substrates these species were reared on). I edited my post to include images for you that might show what I am trying to accomplish better than I could explain. – talker90 Oct 16 '18 at 20:19