Sure sorry
The network is generated using the following script starting from a clusterw alignment file:
import networkx, pylab
from networkx.drawing.nx_agraph import graphviz_layout
from Bio import Phylo
from Bio.Phylo.TreeConstruction import DistanceCalculator
from Bio.Phylo.TreeConstruction import DistanceTreeConstructor
from Bio import AlignIO
import plotly.plotly as py
from plotly.graph_objs import *
import networkx as nx
What color to give to the edges?
e_color = ‘#ccccff’
What colors to give to the nodes with similar labels?
color_scheme = {‘RSK’: ‘#e60000’, ‘SGK’: ‘#ffff00’, ‘PKC’: ‘#32cd32’, ‘DMPK’: ‘#e600e6’, ‘NDR’: ‘#3366ff’,
‘GRK’: ‘#8080ff’, ‘PKA’: ‘magenta’, ‘MAST’: ‘green’, ‘YANK’: ‘pink’}
What sizes to give to the nodes with similar labels?
size_scheme = {‘RSK’: 200, ‘SGK’: 150, ‘PKC’: 350, ‘DMPK’: 400, ‘NDR’: 280, ‘GRK’: 370, ‘PKA’: 325, ‘MAST’: 40,
‘YANK’: 200}
Edit this to produce a custom label to color mapping
def label_colors(label):
color_to_set = 'blue’
for label_subname in color_scheme:
if label_subname in label:
color_to_set = color_scheme[label_subname]
return color_to_set
Edit this to produce a custom label to size mapping
def label_sizes(label):
# Default size
size_to_set = 20
for label_subname in size_scheme:
if label_subname in label:
size_to_set = size_scheme[label_subname]
return size_to_set
Draw a tree whose alignment is stored in msa.phy
def draw_tree():
# This loads the default kinase alignment that should be in the same directory as this script
aln = AlignIO.read(‘agc.aln’, ‘clustal’)
# This will construct the unrooted tree.
calculator = DistanceCalculator(‘identity’)
dm = calculator.get_distance(aln)
constructor = DistanceTreeConstructor()
tree = constructor.nj(dm)
G = Phylo.to_networkx(tree)
node_sizes = []
labels = {}
node_colors = []
for n in G:
label = str(n)
if 'Inner' in label:
# These are the inner tree nodes -- leave them blank and with very small sizes.
node_sizes.append(1)
labels[n] = ''
node_colors.append(e_color)
else:
# Size of the node depends on the labels!
node_sizes.append(label_sizes(label))
# Set colors depending on our color scheme and label names
node_colors.append(label_colors(label))
# set the label that will appear in each node
labels[n] = label
# Draw the tree given the info we provided!
pos = graphviz_layout(G)
networkx.draw(G, pos, edge_color=e_color, node_size=node_sizes, labels=labels, with_labels=False,node_color=node_colors)
pylab.show()
return G
G = draw_tree()
Next I try to follow to the recipe that you suggested:
V=G.nodes()
E=G.edges()
print E
here is the output
[(Clade(branch_length=0.0776315789474, name=‘PKG__PKG1’), Clade(branch_length=0.0745911347787, name=‘Inner24’)), ###(Clade(branch_length=0.00330756013746, name=‘Inner34’), Clade(branch_length=0.0585685808634, name=‘Inner35’)), #(Clade(branch_length=0.00330756013746, name=‘Inner34’), Clade(branch_length=0.0588549337261, name=‘SGK__SGK’)), (Clade(branch_length=0.00330756013746, name=‘Inner34’), Clade(branch_length=0.0607326951399, name=‘SGK__SGK3’)), (Clade(branch_length=0.0265329682131, name=‘Inner9’),…]
I follow the script you linked to only modifying the def position(g) from N=len(g.nodes()) to N=g.nodes():
def position(g):
if not isinstance(g, pgv.AGraph):
raise ValueError(‘The graph g must be a pygraphviz AGraph’)
# N=len(g.nodes())
N=g.nodes()
pos=[]
for k in N:
s=g.get_node(k).attr[‘pos’]
t=s.split(",")
pos.append(map(float, t))
return pos
print H.get_node(0)
#H.nodes()
H.get_node(‘Inner7’).attr[‘pos’]
pos=position(H)
#print pos
[[317.97, 680.49], [252.39, 723.04], [242.76, 659.16], [384.17, 569.51], [168.44, 558.75], [95.542, 594.64], [99.477, 515.78], [242.76, 530.12],
which looks correctly to me.
import plotly.plotly as py
from plotly.graph_objs import *
def plotly_graph(E, pos):
# E is the list of tuples representing the graph edges
# pos is the list of node coordinates
N=len(pos)
Xn=[pos[k][0] for k in range(N)]# x-coordinates of nodes
Yn=[pos[k][1] for k in range(N)]# y-coordnates of nodes
Xe=[]
Ye=[]
for e in E:
Xe+=[pos[e[0]][0],pos[e[1]][0], None]# x coordinates of the nodes defining the edge e
Ye+=[pos[e[0]][1],pos[e[1]][1], None]# y - " -
return Xn, Yn, Xe, Ye
Xn, Yn, Xe, Ye=plotly_graph(E, pos)
and here the program crashes.