# coloring dendogram object in matlab

I have a dendogram looking like object. (in this example, for simplisity, I used phytreeread from Bioinforamtics toolbox)

tree.newick = '(A1:7,(B1:5,((G1:3, H1:3)C1:1, D1:4)F1:1)E1:2)O1:0'; mytree = phytreeread(tree.newick); h = plot(mytree,'Type','square')

I also have of terminal nodes as well as internal nodes.

I can list the edges with

h.BranchLines

and change its color with

set(h.BranchLines(1),'Color',[1 1 0])

but, to make it automatic, I need to know, what are the nodes, at two ends of h.BranchLines(1), and I can't figure it out.

Basically, I am after making a function color_edge(A, B, color), to color the edge between node *A* and node *B*.

In other words, I would like to know either how,
1) for a given h.BranchLines(1) - how to figure out two ending nodes id
2) for a given two nodes, say *A1* and *O1* figure out, what is the which h.Branchlines is connecting them.

Code to reproduce the plot - you would need to have the bioinformatics toolbox

tree.newick = '(A1:7,(B1:5,((G1:3, H1:3)C1:1, D1:4)F1:1)E1:2)O1:0'; tree.root = {'O1'}; mytree = phytreeread(tree.newick); phytreeviewer(mytree) h = plot(mytree,'Type','square') set(h.BranchLines(1),'Color',[1 1 0]) set(h.BranchLines(4),'Color',[0 1 1])

## Answers

You can use getmatrix function to extract the relationship, e.g. in your case:

>> [matrix, id] = getmatrix(mytree) matrix = (9,1) 1 (8,2) 1 (6,3) 1 (6,4) 1 (7,5) 1 (7,6) 1 (8,7) 1 (9,8) 1 id = 'A1' 'B1' 'G1' 'H1' 'D1' 'C1' 'F1' 'E1' 'O1'

It is not documented anywhere that the order of non-zero entries in the sparse matrix corresponds to the order of line handles in BranchLines, but it does seem to be the case (apparently, by construction). So e.g. the first handle would connect nodes 9 and 1, i.e. O1 and A1, according to id mapping.

Worth noting that plot and getmatrix are not built-in functions, so you can just examine the source code to verify this assumption (though they are not the easiest to read). Sources are located under \toolbox\bioinfo\bioinfo\@phytree\.

Alternatively, we can do a quick check by showing branch labels on the chart, e.g. for the first line:

h = plot(mytree, 'Type', 'square', 'BranchLabels', true); set(h.BranchLines(1), 'Color', 'r')

Finally, this is how you can read those non-zero node indices programmatically and in the right order:

[i,j] = ind2sub(size(matrix), find(matrix)) i = 9 8 6 6 7 7 8 9 j = 1 2 3 4 5 6 7 8

P.S. There is obviously absolutely no guarantee it will not break in the future release.