IMP  2.4.0
The Integrative Modeling Platform
kernel/graph.py

A simple example showing how to use the graph interface for in python.

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