dendextend icon indicating copy to clipboard operation
dendextend copied to clipboard

Tanglegram and color_lines

Open TreeT2 opened this issue 7 years ago • 16 comments

Hello Tal and All,

I am looking to produce a tanglegram of two phylogenies of the same taxa with different datasets. I have already converted the phylogenies to dendrograms using DECIPHER.

I create an ordered character vector of colours based on tip label order on the left side phylogeny (I also reverse this as color_lines works from bottom to top). My problem is that the colours are not mapping accurately. I do think this is either due to the topology of one phylogeny/dendrogram or using the original phylogeny to generate the ordered vector (although I suspect not).

(note I have also tried ape cophyloplot and experience the same issue)

command : tanglegram(dendA, dendC, color_lines = x1)

Alternatively is there a method to colour associations lines similar to common_subtrees_color_lines, but involves specifying the subtrees/ clade in the left phylogeny (via the scale bar)??

Hope this makes sense.

TreeT2 avatar Jun 28 '17 13:06 TreeT2

Hi there, For future people - how did you solve this?

talgalili avatar Jul 01 '17 18:07 talgalili

Hi Tal,

I originally posted on stack exchange

The issue was in taxa names, some were labeled as name_101, the underscore did translate in the vector so I was left with name 101. So when I went forth to create a colour vector using extracted taxa names numerous rows were missing. I didn't get an error message so it took me a while to work it back.

TreeT2 avatar Jul 03 '17 11:07 TreeT2

Thanks. What type of test in the code would have been able to pick it up? (If you have an idea for one then we could add it)

talgalili avatar Jul 03 '17 11:07 talgalili

Im just referring to info in the help page:

a vector of colors for the lines connected the labels. If the colors are shorter than the number of labels, they are recycled (and a warning is issued). The colors in the vector are applied on the lines from the bottom up.

Im afraid Im only a novice in R.

TreeT2 avatar Jul 03 '17 11:07 TreeT2

So are you saying that you do not see a warning message? Which function is this the help file of?

talgalili avatar Jul 03 '17 11:07 talgalili

No warning message. The quote above is from the tanglegram function and color_line argument.

TreeT2 avatar Jul 03 '17 11:07 TreeT2

Hi, Tal I have the same issue. Here is the code which could reproduce the color issue.

rm(list=ls())
library(dendextend)
library(dplyr)
library(DECIPHER)
library(ggplot2)

#load source data
dend1 <- ReadDendrogram('https://raw.githubusercontent.com/virologist/Flu/master/tree1.newick', internalLabels = FALSE)
dend2 <- ReadDendrogram('https://raw.githubusercontent.com/virologist/Flu/master/tree2.newick', internalLabels = FALSE)

#read the clade/color data
tip_metadata <- read.csv('https://raw.githubusercontent.com/virologist/Flu/master/Cluster.csv', sep=",", header=TRUE,check.names=FALSE, stringsAsFactor=F)

#set label colors trees
virus_lab <- labels(dend1)
virus_lab <- tip_metadata$Clade
virus_lab <- as.factor(virus_lab)
levels(virus_lab) <- 1:length(levels(virus_lab))
labels_colors(dend1) <- virus_lab

# Stepwise untangle one tree compared to another
dend2_corrected <- untangle_step_rotate_1side(dend1, dend2)[[1]]

#Create a dendrogram with color branches) 
dl <- dendlist(color_branches(dend1, clusters = as.numeric(virus_lab),col=levels(virus_lab)), 
               color_branches(dend2_corrected, clusters = as.numeric(virus_lab), col=levels(virus_lab)))


#Print entanglement
dl %>% entanglement # lower is bette

dend <- tanglegram(dl, 
           main_left = "HA", 
           main_right = "NA",
           color_lines=as.numeric(virus_lab),
           margin_inner = 0.5,  ## remove tip labels from tanglegram
           lwd = .5, 
           axes = TRUE,
           type = "r",
           remove_nodePar = TRUE, 
           highlight_distinct_edges = FALSE, 
           highlight_branches_lwd=FALSE, 
           common_subtrees_color_lines = FALSE, 
           edge.lwd = .8,
           lab.cex = .1, 
           faster = FALSE)

Best, Yan

virologist avatar Jun 14 '20 04:06 virologist

Any chance for a smaller example that is self contained and that can reproduce the issue? Maybe use dput to extract the objects.

Thanks!

On Sun, Jun 14, 2020, 07:27 Biopig [email protected] wrote:

Hi, Tal I think I meet the similar problem. I demo code is fine. The branch and tanglegram link lines have the as same color as I want. [image: image] https://user-images.githubusercontent.com/54756814/84584642-63dcb900-ae39-11ea-83bc-3e6d1a2ecbbf.png Here is the link of all code and files https://github.com/virologist/Flu Here is the code `#if (!require(dendextend)) install.packages(dendextend); rm(list=ls()) setwd("C:/Users/Administrator/Desktop/dendextend_demo")

library(dendextend) library(dplyr) library(DECIPHER) library(ggplot2)

#load source data dend1 <- ReadDendrogram("dend1.newick", internalLabels = FALSE) dend2 <- ReadDendrogram("dend2.newick", internalLabels = FALSE)

#read the clade/color data tip_metadata <- read.csv("taxon_color.csv", sep="\t", header=TRUE,check.names=FALSE, stringsAsFactor=F)

#set label colors trees virus_lab <- labels(dend1) virus_lab <- tip_metadata$clade virus_lab <- as.factor(virus_lab) levels(virus_lab) <- 1:length(levels(virus_lab)) labels_colors(dend1) <- virus_lab Stepwise untangle one tree compared to another

dend2_corrected <- untangle_step_rotate_1side(dend1, dend2)[[1]]

#Create a dendrogram with color branches) dl <- dendlist(color_branches(dend1, clusters = as.numeric(virus_lab),col=levels(virus_lab)), color_branches(dend2_corrected, clusters = as.numeric(virus_lab), col=levels(virus_lab)))

#Print entanglement dl %>% entanglement # lower is bette

tanglegram(dl, main_left = "Tree_1", main_right = "Tree_2", color_lines = as.numeric(virus_lab), lwd = .5, axes = TRUE, type = "r", remove_nodePar = TRUE, highlight_distinct_edges = FALSE, highlight_branches_lwd=FALSE, common_subtrees_color_lines = FALSE, edge.lwd = .8, lab.cex = .1, faster = FALSE) `

BUT, not with my own data.

  1. I have 11 clades. But, it looks like the dendextend only can offer 9 colors. The colors of Green and Black have been recycled without any warming. [image: image] https://user-images.githubusercontent.com/54756814/84584517-0136ed80-ae38-11ea-8192-cedd75cdfca0.png
  2. The branch color and color_line are not consistent even I designated them coloring based on same taxon.

Could you please give some help? Best Regards Yan

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/talgalili/dendextend/issues/47#issuecomment-643716880, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHOJBWQTWCTRFSRPHUT5D3RWRGUTANCNFSM4DQ76FJA .

talgalili avatar Jun 14 '20 05:06 talgalili

Yes, It's reproducible.

virologist avatar Jun 14 '20 05:06 virologist

In order to help me. Could you please replace: "Here is the link of all code and files https://github.com/virologist/Flu Here is the code `#if (!require(dendextend)) install.packages(dendextend); rm(list=ls()) setwd("C:/Users/Administrator/Desktop/dendextend_demo")"

With something I can just copy paste to my console, run, and see the same results? (i.e.: without sending me to download files to somewhere, change the path, etc.?)

On Sun, Jun 14, 2020 at 8:22 AM Biopig [email protected] wrote:

Yes, It's reproducible.

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/talgalili/dendextend/issues/47#issuecomment-643720414, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHOJBRBXS53GZ3KGS6QMALRWRNDFANCNFSM4DQ76FJA .

talgalili avatar Jun 14 '20 08:06 talgalili

In order to help me. Could you please replace: "Here is the link of all code and files https://github.com/virologist/Flu Here is the code `#if (!require(dendextend)) install.packages(dendextend); rm(list=ls()) setwd("C:/Users/Administrator/Desktop/dendextend_demo")" With something I can just copy paste to my console, run, and see the same results? (i.e.: without sending me to download files to somewhere, change the path, etc.?) On Sun, Jun 14, 2020 at 8:22 AM Biopig @.***> wrote: Yes, It's reproducible. — You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub <#47 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHOJBRBXS53GZ3KGS6QMALRWRNDFANCNFSM4DQ76FJA .

Yes, for sure. I updated the code so that you can copy, paste and run. Yan

virologist avatar Jan 16 '21 05:01 virologist

Old thread, but was struggling with this today and figured it out.

The colour order shouldn't be the reverse of tree$tip.label, but the reverse of tree$tip.label[order.dendrogram(x = as.dendrogram(tree))]

ghoresh11 avatar Jun 03 '21 10:06 ghoresh11

Cool, thanks for letting us know! Is there a code change you think we should make in the package?

On Thu, Jun 3, 2021 at 1:44 PM Gal Horesh @.***> wrote:

Old thread, but was struggling with this today and figured it out.

The colour order shouldn't be the reverse of tree$tip.label, but the reverse of tree$tip.label[order.dendrogram(x = as.dendrogram(tree))]

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/talgalili/dendextend/issues/47#issuecomment-853774529, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHOJBXH553VAW3WMAILWO3TQ5MI3ANCNFSM4DQ76FJA .

talgalili avatar Jun 03 '21 10:06 talgalili

The details of the code are not so clear to me on a quick look, but I realised that the order of the lines came down to this line.

  ord_arrow <- cbind((1:l)[order(order.dendrogram(dend1))], (1:l)[order(order.dendrogram(dend2))])

Perhaps if this line is changed to the order is based on a match between dend1$tip.label and dend2$tip.label it will solve the problem.

ghoresh11 avatar Jun 03 '21 10:06 ghoresh11

Any chance you could make a simple small reproducible example of the issues so I can debug it?

On Thu, 3 Jun 2021, 13:57 Gal Horesh, @.***> wrote:

The details of the code are not so clear to me on a quick look, but I realised that the order of the lines came down to this line.

ord_arrow <- cbind((1:l)[order(order.dendrogram(dend1))], (1:l)[order(order.dendrogram(dend2))])

Perhaps if this line is changed to the order is based on a match between dend1$tip.label and dend2$tip.label it will solve the problem.

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/talgalili/dendextend/issues/47#issuecomment-853781359, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHOJBTXXAOB5WGLE2UPD2TTQ5NZNANCNFSM4DQ76FJA .

talgalili avatar Jun 03 '21 11:06 talgalili

This should help:

library(ape)
library(dendextend)
library(phytools)

tree1 = rtree(n = 20)
tree2 = rtree(n = 20)

## the problem arises when you reroot the tree and the order changes
tree1 = chronos(root(tree1, "t17"))
tree2 = chronos(root(tree2, "t17"))

## order based on tips
colours = data.frame(tip = tree1$tip.label, colours = rep("", length(tree1$tip.label)))
colours$colours[colours$tip %in% c("t1","t2","t3","t4","t5")] = "red"
colours$colours[colours$tip %in% c("t6","t7","t8","t9","t10")] = "blue"
colours$colours[colours$tip %in% c("t11","t12","t13","t14","t15")] = "purple"
colours$colours[colours$tip %in% c("t16","t17","t18","t19","t20")] = "orange"



## this is wrong, you expect 1-5 to have the same colout, 6-10, 11-15 and 16-20, but this isn't the case
tanglegram(tree1, tree2, intersecting  = F,
           axes = F,highlight_branches_lwd = F, color_lines = colours$colours,
           dLeaf = 0.01)

correct_order = order.dendrogram(x = as.dendrogram(tree1))
colours = colours[correct_order,]


tanglegram(tree1, tree2, intersecting  = F,
           axes = F,highlight_branches_lwd = F, color_lines = colours$colours,
           dLeaf = 0.01)

ghoresh11 avatar Jun 03 '21 12:06 ghoresh11