IMP  2.0.0
The Integrative Modeling Platform
_graph_show.py
1 
2 def get_networkx_graph(ig):
3  import networkx
4  g= networkx.DiGraph()
5  if len(ig.get_vertices())==0:
6  return g
7  class NodeWrapper:
8  def __init__(self, p):
9  self.p=p
10  def __str__(self):
11  return self.p.get_name()
12  def __call__(self, name):
13  return self.p.__call__(name)
14 
15  for vi in ig.get_vertices():
16  n= ig.get_vertex_name(vi)
17  g.add_node(NodeWrapper(n))
18  for vi in ig.get_vertices():
19  n= ig.get_vertex_name(vi)
20  for ni in ig.get_out_neighbors(vi):
21  nn= ig.get_vertex_name(ni)
22  g.add_edge(NodeWrapper(n), NodeWrapper(nn))
23  return g
24 
25 
26 def show_altgraph(g):
27  def clean(name):
28  try:
29  n0=name.get_name()
30  except:
31  n0=str(name)
32  n1= str(n0).replace('"','')
33  n2= n1.replace("\n",'')
34  return n2
35  import altgraph
36  from altgraph import Graph, Dot
37  graph= Graph.Graph()
38  for i,v in enumerate(g.get_vertices()):
39  graph.add_node(i) #, node_data=g.get_vertex_name(v)
40  for i,v in enumerate(g.get_vertices()):
41  for n in g.get_out_neighbors(v):
42  graph.add_edge(v, n)
43  dot = Dot.Dot(graph) #, graph_type="digraph"
44  for i,v in enumerate(g.get_vertices()):
45  dot.node_style(i, label=clean(g.get_vertex_name(v)))
46  dot.display()
47 
48 def show_graphviz(g):
49  import IMP.base
50  tfn= IMP.base.create_temporary_file_name("graph", ".dot")
51  tfon= IMP.base.create_temporary_file_name("graph", ".pdf")
52  st= g.get_graphviz_string()
53  open(tfn, "w").write(st)
54  try:
55  import subprocess
56  except ImportError:
57  import sys
58  print >> sys.stderr, "Cannot run dot on Python 2.3 systems."
59  return
60  try:
61  print "running dot"
62  sp=subprocess.Popen(["dot", "-Tpdf", tfn, "-o"+tfon])
63  sp.wait()
64  except:
65  import sys
66  print >> sys.stderr, "The dot command from the graphviz package was not found. Please make sure it is in the PATH passed to IMP."
67  return
68  try:
69  import platform
70  if platform.system() =="Darwin":
71  cmd="open"
72  else:
73  cmd="acroread"
74  print "launching viewer", cmd
75  subprocess.Popen([cmd, tfon])
76  except:
77  print "Could not display file. It is saved at", tfon
78  return tfon