treehierarchyphylogeny

Reorder newick tree for specific two samples to be closest


I have Newick tree ((a:1, b:1):1, (c:1, d:1):1):1;

I want specific two samples (a or b or c or d) to be as close to each other as possible

(of course without breaking the lineage of the tree)

For example, if I choose b and d

the result will be((a:1, b:1):1, (d:1, c:1):1):1;

It has to work on more complicated samples like

((a:1, b:1):1, ((c:1, d:1):1, (e:1, f:1, g:1):1):1):1;

If I choose b and f, the result has to be

((a:1, b:1):1, ((f:1, g:1, e:1):1, (c:1, d:1):1):1):1;

Order of g, e, c, d doesn't matter


Solution

  • Function rotateConstr from package ape mimics the intended behaviour with a bit of work. In theory, it should place the tips close to one another with a constraint of a partial list of the tip labels:

    tr2 <- rotateConstr(tr, constraint = c("b", "f"))
    

    In practice, the function performs best on a full list of ordered tip labels.

    tr3 <- rotateConstr(tr, constraint = c("a", "b", "f", "e", "g", "c", "d"))