Advanced Tutorial

Create HCX for CX2 network

Hierarchical Network Schema (HCX) can be obtained by adding specific annotation to a network in CX2 format. HCX format allows to link the hierarchical network to its interactome. Here we describe how to annotate CX2 network to use the Multi-file model for HCX.

  1. Add following attributes to networkAttributes

  • ndexSchema

    with value hierarchy_v0.1

  • HCX::modelFileCount

    with value of the actual count of all the files that this hierarchy references, including the hierarchy itself.

    If you link only full interactome (parent network) the value will be 2. If each node in the hierarchy points to its own interaction network, it will be the number of all the interactomes plus 1 for the hierarchy itself.

  • one of the following attributes:

    (optional in case each node points to its own interactome, in this case a node attribute will be set, see step 2)

    • HCX::interactionNetworkName - filename of file containing parent network located in the same directory of the hierarchy

    • HCX::interactionNetworkUUID - uuid of parent network in NDEx

    • HCX::interactionNetworkURL - url of parent network in NDEx

  1. Add following attributes to nodes

  • HCX::isRoot

    true for root node and false for the rest

  • HCX::members

    list of IDs of nodes in the interaction network.

  • HCX::interactionNetworkUUID, HCX::interactionNetworkName or HCX::interactionNetworkURL

    that allow to specify individual interaction networks for each node, it overwrites the value declared in the networkAttributes aspect. This attributes are optional if the general interaction network was specified in networkAttributes.

    When specifying individual interaction networks for each node, HCX::memberNames attribute can be used instead of HCX::members. HCX::memberNames should have a list of node (gene) names, as in CD_memberList attribute in the hierarchy network generated by the CDAPS app in Cytoscape.

Other node attributes are optional, more information available in HCX specification.


import ndex2
from ndex2.cx2 import RawCX2NetworkFactory

# Create NDEx2 python client
client = client = ndex2.client.Ndex2(username='<USERNAME>', password='<PASSWORD>')

# Create CX2Network factory
factory = RawCX2NetworkFactory()

parent_net = factory.get_cx2network('path_to_your_interactome_in_cx2')
hier_net = factory.get_cx2network('path_to_your_hierarchy_in_cx2')

# Save interactome (parent network) to NDEx to get the url with uuid
parent_url = client.save_new_cx2_network(parent_net.to_cx2(), visibility='PUBLIC')

# Add HCX related attributes to networkAttributes
hier_net.add_network_attribute('ndexSchema', 'hierarchy_v0.1', datatype='string')
hier_net.add_network_attribute('HCX::modelFileCount', '2', datatype='integer')
hier_net.add_network_attribute('HCX::interactionNetworkUUID', parent_url.split('/')[-1], datatype='string')

# Look for roots and add HCX::isRoot attribute to nodes
all_nodes = set(hier_net.get_nodes().keys())
targets = set()
for edge_id, edge_obj in hier_net.get_edges().items():
# Source node is not a target of any edge
root_nodes = all_nodes.difference(targets)

attr_name = 'HCX::isRoot'
for node_id in hier_net.get_nodes().keys():
    hier_net.add_node_attribute(node_id, attr_name, str(node_id in root_nodes).lower(), datatype='boolean')

# Add HCX::members attribute to nodes
for node_id, node_obj in hier_net.get_nodes().items():
    memberlist = hier_net.get_node(node_id).get('v', {}).get('CD_MemberList', []).split(' ')
    membersids = []
    for member in memberlist:
    hier_net.add_node_attribute(node_id, 'HCX::members', membersids, datatype='list_of_integer')


# Save hierarchy to NDEx
client.save_new_cx2_network(hier_net.to_cx2(), visibility='PUBLIC')