IMP  2.0.1
The Integrative Modeling Platform
graph.py
1 ## \example kernel/graph.py
2 ## A simple example showing how to use the graph interface for in python.
3 
4 import IMP
5 
6 m= IMP.Model()
7 # An undirected graph with an IMP::Object for each node
9 vs=[]
10 ps=[]
11 for i in range(0,10):
12  ps.append(IMP.Particle(m))
13  vs.append(g.add_vertex(ps[-1]));
14 g.add_edge(vs[0], vs[1])
15 g.add_edge(vs[1], vs[2])
16 
17 #try to use the altgraph package to visualize
18 IMP.show_graphviz(g)
19 try:
20  IMP.show_graphviz(g)
21 except:
22  print "Oh well, no altgraph"
23 
24 try:
25  import matplotlib
26  # the engine to be used must be selected before pyplot is imported
27  matplotlib.use("macosx")
28  import matplotlib.pyplot as plt
29 
30  # the method below requires the altgraph python package
31  xg=IMP.get_networkx_graph(g)
32 
33  # the networkx visualization tools suck, so skip them
34  #import networkx
35  #networkx.draw(xg)
36  #networkx.draw_shell(xg)
37  #plt.show()
38 except:
39  print "networkx not fully installed"
40 
41 g.remove_vertex(0)
42 
43 # we can also try another show method
44 try:
45  IMP.show_graphviz(g)
46 except:
47  print "oh well, something not working with graphviz"
48 
49 # finally, we can
50 # in and out neighbors are the same
51 for n in g.get_in_neighbors(8):
52  print g.get_vertex_name(n).get_name()