def __init__(self, cx=None, server=None, username=None, password=None, uuid=None, data=None, **attr): '''There are generally four ways to create a graph. 1. An empty graph. G = NdexGraph() 2. Using a cx dictionary. G = NdexGraph(cx) 3. Loading it from an NDEx server. G = NdexGraph(server='http://test.ndexbio.org' uuid='983a2b93-2c55-11e6-a7c5-0630eb0972a1') 4. Just like any other NetworkX MultiDiGraph(). ''' MultiDiGraph.__init__(self, data, **attr) self.subnetwork_id = None self.view_id = None self.max_node_id = None self.max_edge_id = None self.pos = {} self.unknown_cx = [] # Maps edge ids to node ids. e.g. { edge1: (source_node, target_node), edge2: (source_node, target_node) } self.edgemap = {} if not cx and server and uuid: ndex = nc.Ndex(server, username, password) cx = ndex.get_network_as_cx_stream(uuid).json() if not cx: raise RuntimeError("Failed to retrieve network with uuid " + uuid + " from " + server) # If there is no CX to process, just return. if cx == None: return # First pass, get information about subnetworks. for aspect in cx: if 'subNetworks' in aspect: for subnetwork in aspect['subNetworks']: id = subnetwork['@id'] if self.subnetwork_id != None: raise ValueError( "networkn does not support collections!") self.subnetwork_id = id elif 'cyViews' in aspect: for cyViews in aspect['cyViews']: id = cyViews['@id'] if self.view_id != None: raise ValueError( "networkn does not support more than one view!") self.view_id = id elif 'metaData' in aspect: # Strip metaData continue else: self.unknown_cx.append(aspect) cx = self.unknown_cx # Second pass, just build basic graph. self.unknown_cx = [] for aspect in cx: if 'nodes' in aspect: for node in aspect['nodes']: id = node['@id'] name = node['n'] if 'n' in node else None if name: self.add_node(id, name=name) else: self.add_node(id) represents = node['r'] if 'r' in node else None if represents: self.node[id]['represents'] = represents elif 'edges' in aspect: for edge in aspect['edges']: id = edge['@id'] interaction = edge['i'] if 'i' in edge else None s = edge['s'] t = edge['t'] self.edgemap[id] = (s, t) if interaction: self.add_edge(s, t, key=id, interaction=interaction) else: self.add_edge(s, t, key=id) else: self.unknown_cx.append(aspect) cx = self.unknown_cx # Third pass, handle attributes # Notes. Not handled, datatypes. self.unknown_cx = [] for aspect in cx: if 'networkAttributes' in aspect: for networkAttribute in aspect['networkAttributes']: name = networkAttribute['n'] # special: ignore selected if name == 'selected': continue value = networkAttribute['v'] if 'd' in networkAttribute: d = networkAttribute['d'] if d == 'boolean': value = value.lower() == 'true' if 's' in networkAttribute or name not in self.graph: self.graph[name] = value elif 'nodeAttributes' in aspect: for nodeAttribute in aspect['nodeAttributes']: id = nodeAttribute['po'] name = nodeAttribute['n'] # special: ignore selected if name == 'selected': continue value = nodeAttribute['v'] if 'd' in nodeAttribute: d = nodeAttribute['d'] if d == 'boolean': value = value.lower() == 'true' if 's' in nodeAttribute or name not in self.node[id]: self.node[id][name] = value elif 'edgeAttributes' in aspect: for edgeAttribute in aspect['edgeAttributes']: id = edgeAttribute['po'] s, t = self.edgemap[id] name = edgeAttribute['n'] # special: ignore selected and shared_name columns if name == 'selected' or name == 'shared name': continue value = edgeAttribute['v'] if 'd' in edgeAttribute: d = edgeAttribute['d'] if d == 'boolean': value = value.lower() == 'true' if 's' in edgeAttribute or name not in self[s][t][id]: self[s][t][id][name] = value else: self.unknown_cx.append(aspect) cx = self.unknown_cx # Fourth pass, node locations self.pos = {} self.unknown_cx = [] for aspect in cx: if 'cartesianLayout' in aspect: for nodeLayout in aspect['cartesianLayout']: id = nodeLayout['node'] x = nodeLayout['x'] y = nodeLayout['y'] self.pos[id] = np.array([x, y], dtype='float32') else: self.unknown_cx.append(aspect)
def __init__(self, cx=None, server=None, username=None, password=None, uuid=None, data=None, **attr): '''There are generally four ways to create a graph. 1. An empty graph. G = NdexGraph() 2. Using a cx dictionary. G = NdexGraph(cx) 3. Loading it from an NDEx server. G = NdexGraph(server='http://test.ndexbio.org' uuid='983a2b93-2c55-11e6-a7c5-0630eb0972a1') 4. Just like any other NetworkX MultiDiGraph(). ''' MultiDiGraph.__init__(self, data, **attr) self.subnetwork_id = None self.view_id = None self.max_node_id = None self.max_edge_id = None self.pos = {} self.unknown_cx = [] # Maps edge ids to node ids. e.g. { edge1: (source_node, target_node), edge2: (source_node, target_node) } self.edgemap = {} if not cx and server and uuid: ndex = nc.Ndex(server,username,password) cx = ndex.get_network_as_cx_stream(uuid).json() if not cx: raise RuntimeError("Failed to retrieve network with uuid " + uuid + " from " + server) # If there is no CX to process, just return. if cx == None: return # First pass, get information about subnetworks. for aspect in cx: if 'subNetworks' in aspect: for subnetwork in aspect['subNetworks']: id = subnetwork['@id'] if self.subnetwork_id != None: raise ValueError("networkn does not support collections!") self.subnetwork_id = id elif 'cyViews' in aspect: for cyViews in aspect['cyViews']: id = cyViews['@id'] if self.view_id != None: raise ValueError("networkn does not support more than one view!") self.view_id = id elif 'metaData' in aspect: # Strip metaData continue else: self.unknown_cx.append(aspect) cx = self.unknown_cx # Second pass, just build basic graph. self.unknown_cx = [] for aspect in cx: if 'nodes' in aspect: for node in aspect['nodes']: id = node['@id'] name = node['n'] if 'n' in node else None if name: self.add_node(id, name=name) else: self.add_node(id) represents = node['r'] if 'r' in node else None if represents: self.node[id]['represents'] = represents elif 'edges' in aspect: for edge in aspect['edges']: id = edge['@id'] interaction = edge['i'] if 'i' in edge else None s = edge['s'] t = edge['t'] self.edgemap[id] = (s, t) if interaction: self.add_edge(s, t, key=id, interaction=interaction) else: self.add_edge(s, t, key=id) else: self.unknown_cx.append(aspect) cx = self.unknown_cx # Third pass, handle attributes # Notes. Not handled, datatypes. self.unknown_cx = [] for aspect in cx: if 'networkAttributes' in aspect: for networkAttribute in aspect['networkAttributes']: name = networkAttribute['n'] # special: ignore selected if name == 'selected': continue value = networkAttribute['v'] if 'd' in networkAttribute: d = networkAttribute['d'] if d == 'boolean': value = value.lower() == 'true' if 's' in networkAttribute or name not in self.graph: self.graph[name] = value elif 'nodeAttributes' in aspect: for nodeAttribute in aspect['nodeAttributes']: id = nodeAttribute['po'] name = nodeAttribute['n'] # special: ignore selected if name == 'selected': continue value = nodeAttribute['v'] if 'd' in nodeAttribute: d = nodeAttribute['d'] if d == 'boolean': value = value.lower() == 'true' if 's' in nodeAttribute or name not in self.node[id]: self.node[id][name] = value elif 'edgeAttributes' in aspect: for edgeAttribute in aspect['edgeAttributes']: id = edgeAttribute['po'] s, t = self.edgemap[id] name = edgeAttribute['n'] # special: ignore selected and shared_name columns if name == 'selected' or name == 'shared name': continue value = edgeAttribute['v'] if 'd' in edgeAttribute: d = edgeAttribute['d'] if d == 'boolean': value = value.lower() == 'true' if 's' in edgeAttribute or name not in self[s][t][id]: self[s][t][id][name] = value else: self.unknown_cx.append(aspect) cx = self.unknown_cx # Fourth pass, node locations self.pos = {} self.unknown_cx = [] for aspect in cx: if 'cartesianLayout' in aspect: for nodeLayout in aspect['cartesianLayout']: id = nodeLayout['node'] x = nodeLayout['x'] y = nodeLayout['y'] self.pos[id] = np.array([x,y], dtype='float32') else: self.unknown_cx.append(aspect)