# Plot phylogenetic tree from list of edges

I have a dataset that I wish to convert to tree or phylo format like in the ape package, in order to plot the phylogenetic tree. It is formatted like a list of edges and species names for the leaves. The dataset is quite large, but I have made an example dataset that represents it:

df <- data.frame(c(8,8,7,6,6,7,9,9,9),
c(1,2,3,4,5,6,7,8,9),
c(9, 9, 12, 14, 17, 19, 21, 23, 9),
c("a","b","c","d","e", "node1", "node2", "node3", "node4"))
names(df) <- c("parent","node","branch.length", "label")


I wish to plot it as a rooted phylogenetic tree using the ape package, but I can't figure out how to convert it to for example a newick string. I have tried to look at solutions both in R and Python, but I can't figure it out. Does anyone have any idea how this can be done?

Additional information: I also have two other vectors, one contains the length of each edge, and one containing the age of the child node, equal to the sum of all edge length from that child to any leaf of the tree. It would be great if this could be included, but not neccessary for the plotting as it is only illustrative.

Thanks!

Update

I am using R version 4.1.2. One of my attempts at translating my data to Newick code (as i understand this could include the branch lengths), using one of [these answers]. I don't understand what goes wrong, but I have not worked with Newick code before so I am not very experienced with this. (https://biology.stackexchange.com/questions/37057/construct-phylogeny-form-edgelist):

df <- data.frame(c(8,8,7,6,6,7,9,9,9),
c(1,2,3,4,5,6,7,8,9),
c(9, 9, 12, 14, 17, 19, 21, 23, 9),
c("a","b","c","d","e", "node1", "node2", "node3", "node4"))
names(df) <- c("parent","node","branch.length", "label")
tree_data <- df
str(tree_data)

tree_data$$newick = paste(tree_data$$label,':',signif(tree_data$$branch.length,3),sep="") nparents = 2 while(nparents > 1){ leaf = setdiff(tree_datanode,tree_dataparent) parent_leaf = unique(tree_dataparent[!is.na(match(tree_datanode,leaf))]) index_remove = integer(0) for(i in 1:length(parent_leaf)){ parent_children = tree_datanode[which(tree_dataparent == parent_leaf[i])] all_children_in_leaf = all(parent_children %in% leaf) if(all_children_in_leaf==T){ index_remove = c(index_remove,match(parent_children,tree_datanode)) tree_datanewick[which(tree_datanode==parent_leaf[i])] = paste("(",paste(tree_datanewick[match(parent_children,tree_datanode)],collapse=" , "),")",tree_datanewick[which(tree_datanode==parent_leaf[i])] ,sep = " ") } } tree_data = tree_data[-index_remove,] parent_next = unique(tree_dataparent[!is.na(match(tree_datanode,leaf))]) nparents = length(parent_next) if (nparents==1) newick_code = paste("(",paste(tree_data$$newick,collapse=","),")",colnames(tree_data)[parent_next],";",sep = " ")
}

plot.phylo(tree)



I don't get an error any more, but I get some unexpected output: This is what I would expect, all nodes that are not single letters are inner nodes, not leaves: I really appreciate any help!

• There is an answer to more or less the same question in biology.stackexchange.com. Mar 7 at 22:22
• Thank you for the answer, I have taken a look at the answers but none of them seem to work for me. (all return errors after reformatting to the data format they used) Nikunj Goel's answer of writing a code to translate to Newick code and include the branch lengths is what I have tried to do myself, but it is challenging as I am not very familiar with it. Mar 10 at 7:22
• Hi @Helle, can you provide a few lines of codes as well as the errors you are getting. Include the version of R you are using. Those answers were published 7 yrs ago most likely you will have to adjust your code a bit to make it compatible with current standards. Mar 10 at 9:09
• @Macintosh I have updated it now :-) Mar 10 at 10:07
• the error comes from colnames(Adj)[parent_next]: Adj is not defined and should refer to the correct table. Mar 10 at 12:42

The following example uses python to generate the newick string. You can than import that in R for further analysis.

Please keep in mind that you can not use self reference for node4 (9,9,9), so I changed this to 'Root'.

from ete3 import Tree
import pandas as pd

df = pd.DataFrame({'parent': [8, 8, 7, 6, 6, 7, 9, 9, 'Root'],
'node': [1, 2, 3, 4, 5, 6, 7, 8, 9],
'branch.length': [9, 9, 12, 14, 17, 19, 21, 23, 9],
'label': ['a', 'b', 'c', 'd', 'e', 'node1', 'node2', 'node3', 'node4']})

parent_child_list = df[['parent', 'node', 'branch.length']].apply(tuple, axis=1).tolist()
tree = Tree.from_parent_child_table(parent_child_list)
newick_str = tree.write(format=1)



newick_str:

'(((3:12,(4:14,5:17)6:19)7:21,(1:9,2:9)8:23)9:9);'

• Thank you! Exactly what I was looking for. Mar 11 at 11:46