[BioC] tree manipulation

Wolfgang Huber whuber at embl.de
Wed May 12 00:34:11 CEST 2010


On 11/05/10 14:49, Anthony Ferrari wrote:
> Hi all,
>
> Does exist a package  that allows one to recursively apply a custom function
> to nodes in a binary tree (from hclust) from leaves to the root ?
> A kind of "tree_apply" ?
> Ideally, after that, we could easily retrieve the value of the function at
> each node and the leaves IDs under that node.
>
> Thank you.
>
> AF
>
>

Dear Anthony,

/* this would be better for R-help.... */

perhaps there is already a solution out there, but the task is so simple 
that you may not need a package.

For trees that are represented as lists of lists, something like the 
below code example would work.

For the representation used by hclust, you could write a similar 
function that loops over the rows of the "merge" matrix (see the hclust 
man page).


tree = list(a = list( A=1,  B=2),
             b = list( A=list(x=3, y=4, z=5),
                       B=list(x=6, y=7),
                       C=8),
             c = 9)

traverse = function(x, f)
   if (is.list(x)) lapply(x, traverse, f=f) else f(x)

traverse(tree, f=function(x) x+1)




Wolfgang Huber
EMBL
http://www.embl.de/research/units/genome_biology/huber



More information about the Bioconductor mailing list