Bipartite graph in python
1
0
Entering edit mode
8.5 years ago
kaybio • 0

I have a dictionary data structure. I will like to draw a bipartite graph to visualise the data. Am new to python. I have checked online but dint find any useful help in this regard. I know I will be using the network module in python for this. My data is in the format [Targets:drugs]

Where targets are keys and drugs are values.

python • 6.0k views
ADD COMMENT
1
Entering edit mode
8.5 years ago

I am not a python programmer so I'll do this as an exercise:

import networkx as nx
import matplotlib.pyplot as plt # For drawing

 # Dictionary of target:drug
dictionary = { 'ALK': 'Crizotinib', 'EGFR': 'Lapatinib', 'VEGF':'Regorafenib', 'HER2': 'Lapatinib', 'BCR-ABL': 'Ponatinib', 'RET':'Regorafenib' }

G=nx.Graph() # Create a graph

for target in dictionary.keys():
    G.add_edge(target, dictionary[target]) # Add an edge for each dictionary entry
                                           # Nodes are automatically created

pos = nx.spring_layout(G) # Layout algorithm
nx.draw(G, with_labels = True) # Draw the graph
plt.savefig("graph.png") # Save to a PNG file

Note that a dictionary doesn't allow duplicate keys so it can't represent targets with more than one drug unless the values are lists of drugs.

ADD COMMENT

Login before adding your answer.

Traffic: 2671 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6