Search code examples
matlabplottreedata-visualizationmatlab-figure

Remove leafs from phytree if all the branches have same value


I'm using MATLAB to generate 'phytrees' and I need to simplify them.

The way I thought is by removing subtrees where all the node has the same value and keep only this value + a number that represent how many nodes were deleted.

For example, here is one of the trees:

ma

and I want to replace the subtrees that have the same values like here:

ma_zoom

Is there a way to do so?


Solution

  • I did not find a programmatic way to do it, but from the picture you attached I see that you use plot to view your figure. If instead, you'll use the phytreeviewer (just type view(your_phylotree)) you'll get a different figure window, with other related tools.

    Specifically, you'll see the Collapse branch button Collapse, and the Rename branch button enter image description here, which will together get you exactly what you want. The first "removing subtrees" (actually hiding them), and the second lets you change the branch name to "value + a number".

    You can do all this also by simply right-clicking in the relevant brunch:

    right-click

    Here is an example with data from the docs:

    % bulding some tree:
    seqs = fastaread('pf00002.fa');
    distances = seqpdist(seqs,'method','jukes-cantor','indels','pair');
    phylotree = seqneighjoin(distances,'equivar',seqs);
    view(phylotree)
    

    After some collapsing and renaming on this tree, and printing it to a figure (with right click on the most top branch, or the root, that you want to include in the figure), I got:

    example tree

    Also, note that every time you hover with the mouse on a branch (even if collapsed) you get a list of the Leafs in that branch and their count:

    tooltip