Reputation: 3343
I am working on to create a newick tree out of list of childs. I have a list of lists where the list name is the parent name and list element are the childs. Here is an example:
$`825`
[1] 824
$`824`
[1] 823
$`823`
[1] 822
$`822`
[1] 821
$`821`
[1] 820 777
$`820`
[1] 819 816 789 787 785 783
$`789`
[1] 788
$`787`
[1] 786
$`785`
[1] 784
$`783`
[1] 782
$`777`
[1] 776
Hence the output I want is phylo tree in newick format as follows:
825(824(823(822(821(820(819,816,789(788),787,785(784),783(782)),777(776)))))
What is the best way to do this? One way is to write a recursive function that traverses in depth first order and creates the tree. But in R recursions are known to be bad.
Thanks.
Upvotes: 3
Views: 810
Reputation: 1978
If you are looking for a pre-canned solution I believe the bioconductor/ape library has a list -> newick converter
http://www.r-phylo.org/wiki/HowTo/InputtingTrees
Otherwise, I wrote some code a while ago that did just this for the neighbour-joining algorithm; it's extremely terse (written in a tight deadline), but perhaps it will help.
https://github.com/rgrannell1/NJ/blob/master/main.R
hope that helps somewhat
Upvotes: 1