0

I want to draw a single networkX graph with items inside the tuple 'new'. My graph nodes will have labels such as "AXIN", and color will be green or yellow.

I want the node to be green color, if 'UNREPORTED'. If item[2] within tuple 'reported', I want this node to be yellow color.

> new = (('AXIN', 37, 'reported'),  ('LGR', 30, 'reported'),  ('NKD',
> 24, 'reported'),  ('TNFRSF', 23, 'UNREPORTED'),  ('CCND', 19,
> 'reported'),  ('APCDD', 18, 'reported'),  ('TR', 16, 'UNREPORTED'), 
> ('TOX', 15, 'UNREPORTED'),   ('LEF', 15, 'reported'),  ('MME', 13,
> 'reported'))
> 
> X, Y, _ = zip(*new) import seaborn as sns sns.set() import
> matplotlib.pyplot as plt  %matplotlib inline plt.figure(figsize = (20,
> 10)) mytitle = "Most common genes coexpressed with {gene1}, {gene2},
> {gene3}, {gene4}".format(gene1="axin2", gene2="lef", gene3="nkd1",
> gene4="lgr5") plt.title(mytitle, fontsize=40) plt.ylabel('Number of
> same gene encounters across studies', fontsize=20) ax =
> plt.bar(range(len(X)), Y, 0.6, align='center', tick_label = X,
> color="red")  ax = plt.xticks(rotation=90) new = tuple(new)
> 
> import networkx as nx children = sorted(new, key=lambda x: x[1])
> parent = children.pop()[0]
> 
> G = nx.Graph() for child, weight, _ in children: G.add_edge(parent,
> child, weight=weight) width = list(nx.get_edge_attributes(G,
> 'weight').values()) colors = [] for i in new:
>         if i[2] == 'UNREPORTED':
>                 colors.append('green')
>         elif i[2] == 'reported':
>                 colors.append('yellow') print(colors) plt.savefig("plt.gene-expression.pdf") plt.figure(figsize = (20, 10))
> mytitle = "Most common genes coexpressed with {gene1}, {gene2},
> {gene3}, {gene4}".format(gene1="axin2", gene2="lef", gene3="nkd1",
> gene4="lgr5") plt.title(mytitle, fontsize=40)
> 
> nx.draw_networkx(G, font_size=10, node_size=2000, alpha=0.6,
> node_color=colors) plt.savefig("gene-expression-graph.pdf")

With this code, I get two different graphs, with yellow nodes, other with green nodes.

1 Answer 1

0

You could use a dictionary comprehension with the ternary operator to build a mapping of the nodes to colors. This can be used to color the nodes when drawing the graph.

import networkx as nx

new = (('AXIN', 37, 'reported'),  ('LGR', 30, 'reported'),  ('NKD', 24, 'reported'),  ('TNFRSF', 23, 'UNREPORTED'),  ('CCND', 19, 'reported'),  ('APCDD', 18, 'reported'),  ('TR', 16, 'UNREPORTED'), ('TOX', 15, 'UNREPORTED'),   ('LEF', 15, 'reported'),  ('MME', 13, 'reported'))
children = sorted(new, key=lambda x: x[1])
node_color = {node[0]: 'y' if node[2] == 'reported' else 'g' for node in children}
parent = children.pop()[0]
G = nx.Graph()
for child in children: G.add_edge(parent, child[0])
nx.draw_networkx(G, node_color=[node_color[i] for i in G.nodes()])

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @h_s. unfortunately, it assigns the green color to any of the nodes. Not specifically to the "UNREPORTED" nodes only. I expected TNFRSF, TOX & TR to be green nodes. But I got APCDD as a green node, which is wrong because APCDD is reported.
@J.A I've now provided a fuller answer with a minimal working example. Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.