Question

I've built a tree image, see question.
Now I've some major groups.
One group has nodes with color green and brown, and has a 'B' and a 'A'. The second group has only pink nodes and 'T' and the last group has yellow, orange and blue, and the letters 'L', 'X' and 'H'. the colors refer to the colors of the nodes and the letters belong to the name. So I want to color the edge of the different groups.

#taken from draw_graphviz
def get_label_mapping(G, selection): 
    for node in G.nodes(): 
        if (selection is None) or (node in selection): 
            try: 
                label = str(node) 
                if label not in (None, node.__class__.__name__): 
                    yield (node, label) 
            except (LookupError, AttributeError, ValueError): 
                pass


labels = dict(get_label_mapping(G, None))
for label in labels.keys():
if str(label) != "Clade":
        num = label.name.split('-')
        if 'T' in num[0]:
            node_colors.append('#CC6699')
        elif 'X' in num[0]:
            node_colors.append('r')
        else:
            node_colors.append('y')

so I've done a similar function to the above, instead of node, I changed to get_edge. and try this:

  for edge in edges.keys():
        if str(edge) != "Clade":
            if 'T' in edge:
                edge_colors.append('b')

where edge is:

(Clade(branch_length=-0.00193, name='T-7199-8'), Clade(branch_length=0.00494))

perhaps there's a way to say if T is in name, then color the edge. What to you think?

anyone knows how to do this?

Thank you

Was it helpful?

Solution

I'm guessing (since I don't know how that snippet fits in to the rest of the code) that you're iterating over the nodes, and adding one colour to the list for each node. Like the error message suggests, you need to work out the colour required for each edge. That's going to be trickier.

Alright, got it! The code could be tidied up a bit, but this works.

#Define your centre node: you need to pull this out of the graph. Call it b.
# The number changes each time: look for a Clade(branch_length=0.03297)
# Its neighbors have branch lengths .00177, .01972, .00774.
b = G.nodes()[112]

# Recursively paint edges below a certain point, ignoring ones we've already seen
def paintedges(graph, startnode, colour):
    for node in graph.neighbors(startnode):
        if node not in alreadyseen: # alreadyseen is in global scope
            graph[startnode][node]["colour"] = colour
            alreadyseen.add(node)
            paintedges(graph, node, colour)

alreadyseen = set([b])
G[b][G.neighbors(b)[0]]["colour"] = "red"
paintedges(G, G.neighbors(b)[0], "red")
G[b][G.neighbors(b)[1]]["colour"] = "blue"
paintedges(G, G.neighbors(b)[1], "blue")
G[b][G.neighbors(b)[2]]["colour"] = "green"
paintedges(G, G.neighbors(b)[2], "green")

# Now make a list of all the colours, in the order networkx keeps the edges
edgecolours = [G[f][t]["colour"] for f,t in G.edges()]
kwargs["edge_color"] = edgecolours

Tree with colours

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top