code: def cm_json_to_graph(im_json):
      
      cmap_data = im_json['contact map']['map']
  
      # Initialize the graph
      graph = AGraph()
  
      # In this loop we add sites as nodes and clusters around sites to the
      # graph. We also collect edges to be added between sites later.
      edges = []
      for node_idx, node in enumerate(cmap_data):
          sites_in_node = []
          for site_idx, site in enumerate(node['node_sites']):
              # We map the unique ID of the site to its name
              site_key = (node_idx, site_idx)
              sites_in_node.append(site_key)
              graph.add_node(site_key, label=site['site_name'], style='filled',
                             shape='ellipse')
              # Each port link is an edge from the current site to the
              # specified site
              if not site['site_type'] or not site['site_type'][0] == 'port':
                  continue
              for port_link in site['site_type'][1]['port_links']:
                  edge = (site_key, tuple(port_link))
                  edges.append(edge)
          graph.add_subgraph(sites_in_node,
                             name='cluster_%s' % node['node_type'],
                             label=node['node_type'])
  
      # Finally we add the edges between the sites
      for source, target in edges:
          graph.add_edge(source, target)
  
      return graph
docs: Return pygraphviz Agraph from Kappy's contact map JSON.
  
      Parameters
      ----------
      im_json : dict
          A JSON dict which contains a contact map generated by Kappy.
  
      Returns
      -------
      graph : pygraphviz.Agraph
          A graph representing the contact map.