ETE2 - One Child Node with Multiple Parents?

2.1k views Asked by At

I'm trying to create a tree using the ETE2 module in Python. I'd like to add 1 child node to 2 parent nodes so that they both connect to the child when the tree is displayed. I'm a complete novice with ETE, so please forgive me if this is an easy question. Code:

from ete2 import Tree, TreeNode, TextFace


classes = Tree()

#adding 1
course1 = TreeNode(name="Course 1")
face1 = TextFace(text="Course 1")
course1.add_face(face1, column=1)
classes.add_child(child=course1)

#adding 2
course2 = TreeNode(name="Course 2")
face2 = TextFace(text="Course 2")
course2.add_face(face2, column=1)
course1.add_child(child=course2)

#adding 3
course3 = TreeNode(name="Course 3")
face3 = TextFace(text="Course 3")
course3.add_face(face3, column=1)
#adding course3 to 2 parents
course1.add_child(child=course3)
course2.add_child(child=course3)

classes.show()

Output is here

Am I fundamentally going against the idea of a tree here? Does ETE have a simple(r) way to do what I'm proposing?

Let me know if you need more info to help.

1

There are 1 answers

0
user2740614 On BEST ANSWER

I essentially figured out the answer to my own question: the data structure I should be using is not a tree. @boardrider pointed out that a tree inherently is a "one to many" structure, mapping one parent node to one or more children.

The solution was a Directed Acyclic Graph. This is a variation of a graph data structure, which maps "many to many" to continue with the phrasing from above.

Resources:

Graph data structure basics

Python specific graph structures

I will edit this as I progress.