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


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);
h = plot(mytree,'Type','square')
set(h.BranchLines(1),'Color',[1 1 0])
set(h.BranchLines(4),'Color',[0 1 1])


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 = 


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 =

j =

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

Need Your Help

How to extract only one kind of file from the archive?

shell terminal unzip unrar

Given a .zip or .rar archive containing 10 files, each with different extensions.

PHP Split Problem

php split preg-split

I am trying to use (and I've tried both) preg_split() and split() and neither of these have worked for me. Here are the attempts and outputs.