IMP  2.2.1
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 
8  class NodeWrapper:
9 
10  def __init__(self, p):
11  self.p = p
12 
13  def __str__(self):
14  return self.p.get_name()
15 
16  def __call__(self, name):
17  return self.p.__call__(name)
18 
19  for vi in ig.get_vertices():
20  n = ig.get_vertex_name(vi)
21  g.add_node(NodeWrapper(n))
22  for vi in ig.get_vertices():
23  n = ig.get_vertex_name(vi)
24  for ni in ig.get_out_neighbors(vi):
25  nn = ig.get_vertex_name(ni)
26  g.add_edge(NodeWrapper(n), NodeWrapper(nn))
27  return g
28 
29 
30 def show_altgraph(g):
31  def clean(name):
32  try:
33  n0 = name.get_name()
34  except:
35  n0 = str(name)
36  n1 = str(n0).replace('"', '')
37  n2 = n1.replace("\n", '')
38  return n2
39  import altgraph
40  from altgraph import Graph, Dot
41  graph = Graph.Graph()
42  for i, v in enumerate(g.get_vertices()):
43  graph.add_node(i) # , node_data=g.get_vertex_name(v)
44  for i, v in enumerate(g.get_vertices()):
45  for n in g.get_out_neighbors(v):
46  graph.add_edge(v, n)
47  dot = Dot.Dot(graph) # , graph_type="digraph"
48  for i, v in enumerate(g.get_vertices()):
49  dot.node_style(i, label=clean(g.get_vertex_name(v)))
50  dot.display()
51 
52 
53 def show_graphviz(g):
54  import IMP.base
55  tfn = IMP.base.create_temporary_file_name("graph", ".dot")
56  tfon = IMP.base.create_temporary_file_name("graph", ".pdf")
57  st = g.get_graphviz_string()
58  open(tfn, "w").write(st)
59  try:
60  import subprocess
61  except ImportError:
62  import sys
63  print >> sys.stderr, "Cannot run dot on Python 2.3 systems."
64  return
65  try:
66  print "running dot"
67  sp = subprocess.Popen(["dot", "-Tpdf", tfn, "-o" + tfon])
68  sp.wait()
69  except:
70  import sys
71  print >> sys.stderr, "The dot command from the graphviz package was not found. Please make sure it is in the PATH passed to IMP."
72  return
73  import platform
74  if platform.system() == "Darwin":
75  cmd = ["open"]
76  else:
77  cmd = ["gv", "acroread", "xpdf"]
78  success = False
79  for c in cmd:
80  print "launching viewer", c
81  try:
82  subprocess.check_call([c, tfon])
83  success = True
84  break
85  except:
86  pass
87  if not success:
88  print "Could not display file. It is saved at", tfon
89  return tfon
std::string create_temporary_file_name(std::string prefix="imp_temp", std::string suffix="")
See IMP.base for more information.
Definition: base/Array.h:20
std::string show_graphviz(Graph g)
networkx::DiGraph get_networkx_graph(Graph g)
void show_altgraph(Graph g)