Legacy Quick Tutorial

Below are some small, fully runnable, code blocks that show how to download, edit, and upload networks in NDEx using the deprecated NiceCXNetwork data model class

Warning

This tutorial has been deprecated in favor of CX2 format and new network data model CX2Network.

Click here for new Quick Tutorial

Note

For examples below, it is assumed that NDEx2 Python client is installed

Legacy download network from NDEx

The code block below uses the NDEx2 Python client to download BioGRID: Protein-Protein Interactions (SARS-CoV) network from NDEx as a NiceCXNetwork.

The number of nodes and edges are then printed out and the network is converted to Networkx object.

import json
import ndex2


# Create NDEx2 python client
client = ndex2.client.Ndex2()

# Download BioGRID: Protein-Protein Interactions (SARS-CoV) from NDEx
# https://www.ndexbio.org/viewer/networks/669f30a3-cee6-11ea-aaef-0ac135e8bacf
client_resp = client.get_network_as_cx_stream('669f30a3-cee6-11ea-aaef-0ac135e8bacf')

# Convert downloaded network to NiceCXNetwork object
net_cx = ndex2.create_nice_cx_from_raw_cx(json.loads(client_resp.content))

# Display information about network and output 1st 100 characters of CX
print('Name: ' + net_cx.get_name())
print('Number of nodes: ' + str(len(list(net_cx.get_nodes()))))
print('Number of nodes: ' + str(len(list(net_cx.get_edges()))))
print(json.dumps(net_cx.to_cx())[0:100])

# Create Networkx network
g = net_cx.to_networkx(mode='default')

print('Name: ' + str(g))
print('Number of nodes: ' + str(g.number_of_nodes()))
print('Number of edges: ' + str(g.number_of_edges()))
print('Network annotations: ' + str(g.graph))

Legacy upload new network to NDEx

The code block below shows how to upload a network that is a NiceCXNetwork object to NDEx.

import ndex2

# Create a test network
net_cx = ndex2.nice_cx_network.NiceCXNetwork()

# Set name of network
net_cx.set_name('Upload new network to NDEx')

# Create two nodes and one edge
node_one_id = net_cx.create_node(node_name='foo', node_represents='representing foo')
node_two_id = net_cx.create_node(node_name='bar', node_represents='representing bar')
net_cx.create_edge(edge_source=node_one_id, edge_target=node_two_id, edge_interaction='interacts')

# Create client, be sure to replace <USERNAME> and <PASSWORD> with NDEx username & password
client = ndex2.client.Ndex2(username='<USERNAME>', password='<PASSWORD>')

# Save network to NDEx, value returned is link to raw CX data on server.
client.save_new_network(net_cx.to_cx(), visibility='PRIVATE')

# Example return value: https://www.ndexbio.org/v2/network/4027bead-89f2-11ec-b3be-0ac135e8bacf
# To view network in NDEx replace 'v2' with 'viewer' and add 's' to 'network' like so:
# https://www.ndexbio.org/viewer/networks/4027bead-89f2-11ec-b3be-0ac135e8bacf

Note

To update an existing network replace save_new_network() in code block above with update_cx_network() and set first argument to net_cx.to_cx_stream() and the second argument to str UUID of network

Legacy add nodes, edges, and attributes to network

The code block below shows how to add nodes, edges and attributes to a NiceCXNetwork object

import ndex2

# create an empty NiceCXNetwork object
# a NiceCXNetwork could also be downloaded from NDEx or created from CX data
net_cx = ndex2.nice_cx_network.NiceCXNetwork()

# create a node, id of node is returned
node_one_id = net_cx.create_node(node_name='foo', node_represents='representing foo')

# create another node
node_two_id = net_cx.create_node(node_name='bar', node_represents='representing bar')

# create an edge connecting the nodes, id of edge is returned
edge_id = net_cx.create_edge(edge_source=node_one_id, edge_target=node_two_id, edge_interaction='interacts')

# add attribute named 'altname' to 'foo' node, nothing is returned
net_cx.set_node_attribute(node_one_id, 'altname', 'alternate name for foo', type='string')

# add attribute to 'bar' node
net_cx.set_node_attribute(node_two_id, 'altname', 'alternate name for bar', type='string')

# add an edge attribute named 'weight' with value of 0.5. Set as string
# value and then set type.
net_cx.set_edge_attribute(edge_id, 'weight', '0.5', type='double')

# Create Networkx network
g = net_cx.to_networkx(mode='default')

print('Name: ' + str(g))
print('Number of nodes: ' + str(g.number_of_nodes()))
print('Number of edges: ' + str(g.number_of_edges()))
print('Node annotations: ' + str(g.nodes.data()))
print('Edge annotations: ' + str(g.edges.data()))

Legacy build a lookup table for node names to node ids

The code block below shows how to iterate through nodes in a NiceCXNetwork object and build a dict of node names to node ids. The network downloaded below is Multi-Scale Integrated Cell (MuSIC) v1

import ndex2
import json

# Create NDEx2 python client
client = ndex2.client.Ndex2()

# Download MuSIC network from NDEx
client_resp = client.get_network_as_cx_stream('7fc70ab6-9fb1-11ea-aaef-0ac135e8bacf')

# Convert downloaded network to NiceCXNetwork object
net_cx = ndex2.create_nice_cx_from_raw_cx(json.loads(client_resp.content))

node_name_dict = {}

# Build dictionary and print out all the nodes
for node_id, node_obj in net_cx.get_nodes():
    print('node_id: ' + str(node_id) + ' node_obj: ' + str(node_obj))
    node_name_dict[node_obj['n']] = node_id


# Print out dictionary
print(str(node_name_dict))

Legacy More Tutorials and Examples

To use these tutorials or if Github isn’t showing the above notebooks in the browser, clone the ndex-jupyter-notebooks repository to your local machine and start Jupyter Notebooks in the project directory.

For information on installing and using Jupyter Notebooks, go to jupyter.org

Warning

The preferred network data model is via the newer CX2Network. For tutorial visit :doc:’quicktutorial’