Python code to create trees and its graphical model representation using pydot

In this post, I learned the post from the python heaven using pydot to create graphs.

The following code creates a phylogenetic tree first and then creates its corresponding graphical model representation. The two figures are here:

Phylogenetic tree: treetopology.png
Graphical Model Representation: DGMtree.png


@author: Tingting zhao
import pydot

# specify a directed graph
graph = pydot.Dot(graph_type='digraph')

# specify a node X_5, where label is used to incorporate the subscript 5, shape specifies the shape of the node
# X_5 is the root of the tree
# creating nodes is as simple as creating edges!
node_a = pydot.Node("X5",  texlbl='X_5', label='<X<SUB>5</SUB>>',shape ="circle", style="filled", fillcolor="white")
# similary we specify the children of this tree
node_b = pydot.Node("X4",texlbl='X_4', label='<X<SUB>4</SUB>>', shape="circle", style="filled", fillcolor="grey")
node_c = pydot.Node("X3", texlbl='X_3', label='<X<SUB>3</SUB>>', shape="circle", style="filled", fillcolor="white")
node_d = pydot.Node("X1",texlbl='X_1', label='<X<SUB>1</SUB>>',shape="circle", style="filled", fillcolor="grey")
node_e = pydot.Node("X2", texlbl='X_2', label='<X<SUB>2</SUB>>',shape="circle", style="filled", fillcolor="grey")

#add all nodes to the graph

# add edges between nodes and label the name of the edge, which is the branch length of the tree
graph.add_edge(pydot.Edge(node_a, node_b, texlbl='t_4', label='<t<SUB>4</SUB>>'))
graph.add_edge(pydot.Edge(node_a, node_c, texlbl='t_3', label='<t<SUB>3</SUB>>'))
graph.add_edge(pydot.Edge(node_c, node_d,texlbl='t_1', label='<t<SUB>1</SUB>>'))
graph.add_edge(pydot.Edge(node_c, node_e, texlbl='t_2', label='<t<SUB>2</SUB>>'))

# save this tree to a png file

# create another graph which denotes the graphical representation of this phylogenetic tree coded in "graph" in previous code
graph1 = pydot.Dot(graph_type='digraph')

# the key is that we need to add a box denoted as M outside the tree,
# the reason is that we assume there are k sites in the sequences evolving according to the same tree
# we add a box with M to denotes the independence of those sites and also they evolve under the same tree.

cluster_foo.add_node(pydot.Node('M_var_1',texlbl='X_1', label='<X<SUB>1</SUB>>',shape="circle", style="filled", fillcolor="grey"))
cluster_foo.add_node(pydot.Node('M_var_2',texlbl='X_2', label='<X<SUB>2</SUB>>', shape="circle", style="filled", fillcolor="grey"))
cluster_foo.add_node(pydot.Node('M_var_3',texlbl='X_3', label='<X<SUB>3</SUB>>',shape="circle", style="filled", fillcolor="white"))
cluster_foo.add_node(pydot.Node('M_var_4',texlbl='X_4', label='<X<SUB>4</SUB>>', shape="circle", style="filled", fillcolor="grey"))
cluster_foo.add_node(pydot.Node('M_var_5',texlbl='X_5', label='<X<SUB>5</SUB>>', shape="circle", style="filled", fillcolor="white"))


graph1.add_edge(pydot.Edge("M_var_5", "M_var_3"))
graph1.add_edge(pydot.Edge("M_var_5", "M_var_4"))
graph1.add_edge(pydot.Edge("M_var_3", "M_var_1"))
graph1.add_edge(pydot.Edge("M_var_3", "M_var_2"))


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s