Converting NiceCXNetwork objects to other formats

Note

Using the newer data model CX2Network is encouraged since all networks on NDEx can be retrieved in newer CX2 format via the NDEx REST Service

Below are converters that facilitate conversion of NiceCXNetwork object to other types

Networkx

class ndex2.nice_cx_network.DefaultNetworkXFactory(legacymode=False)[source]

Converts NiceCXNetwork to networkx.Graph object or one of its subtypes

For details on implementation see get_graph()

Constructor

Note: the parameters in the constructor change behavior of get_graph()

Parameters:

legacymode (bool) – If set to True then get_graph() behaves like NDEx2 Python client version 3.1 and earlier in that this method returns a networkx.Graph object. see get_graph() for more information

Raises:

NDExError – If invalid value is set in legacymode parameter

get_graph(nice_cx_network, networkx_graph=None)[source]

Creates a networkx.Graph, or a subtype, object from nice_cx_network passed in.

Warning

Converting large networks (10,000+ edges or nodes) may take a long time and consume lots of memory.

The conversion is done as follows:

Any network attributes are copied to the networkx.Graph in manner described here: add_network_attributes_from_nice_cx_network()

For nodes:

All nodes are added with the node id set to the id or NODE_ID of input network nodes.

A node attribute named ‘name’ is set for each node with its value set to the value of the ‘name’ attribute from the input network.

If ‘r’ exists on node, the value is added as a node attribute named ‘represents’ (unless legacymode is set to True in constructor)

All other node attributes are added using the same attribute name as found in the input network. The value is directly set as it was found in input network (could be single object or list)

For edges:

Each edge is added setting the source to the value of EDGE_SOURCE attribute and target set as EDGE_TARGET attribute of input network.

Any edge attributes named EDGE_INTERACTION are renamed ‘interaction’ and stored as an attribute for the edge

Changed in version 3.5.0: If the value of an edge attribute is a list then the value is set directly in the graph as is as opposed to being converted into a comma delimited string

Coordinates are copied in manner described here: copy_cartesian_coords_into_graph()

Warning

If legacymode is set to True in constructor then:

  • networkx.Graph created by this method does NOT support multiple edges between the same nodes. Extra edges encountered are ignored and not converted.

  • In addition, the ‘r’ attribute in the node dict is NOT copied to the resulting networkx.Graph object.

  • networkx_graph parameter is ignored

Parameters:
  • nice_cx_network (NiceCXNetwork) – Network to extract graph from

  • networkx_graph (networkx.Graph or subtype) – Empty networkx graph to populate which is IGNORED if legacymode is set to True in constructor. If unset and legacymode is False in constructor then a networkx.MultiDiGraph is created

Raises:

NDExError – if input network is None

Returns:

Input network converted to networkx Graph

Return type:

networkx.Graph if legacymode is set to True in constructor otherwise networkx.MultiDiGraph unless networkx_graph is set in which case networkx_graph is returned

This networkx converter is still callable, but has been deprecated

class ndex2.nice_cx_network.LegacyNetworkXVersionTwoPlusFactory[source]

Deprecated since version 3.2.0: This implementation contains errors, but is left for backwards compatibility of NiceCXNetwork.to_networkx()

Converts NiceCXNetwork to networkx.Graph object following logic in legacy NDEx2 Python client when networkx 2.0+ is installed.

Warning

This implementation assumes networkx 2.0+ is installed and will fail with older versions.

For conversion details see get_graph()

Constructor

get_graph(nice_cx_network, networkx_graph=None)[source]

Creates a networkx.Graph object from nice_cx_network passed in.

Deprecated since version 3.2.0: This implementation contains errors, but is left for backwards compatibility of NiceCXNetwork.to_networkx()

Warning

Converting large networks (10,000+ edges or nodes) may take a long time and consume lots of memory.

This implementation uses node name as ID for nodes, which is problematic if multiple nodes share the same name and results in invalid mapping of node positions

networkx.Graph created by this method does NOT support multiple edges between the same nodes. Extra edges encountered are ignored and not converted.

The conversion is done as follows:

Any network attributes are copied to the networkx.Graph in manner described here: add_network_attributes_from_nice_cx_network()

For nodes:

All nodes are added with the node id set to value of ‘n’ on node. For multiple nodes with same ‘n’ value behavior is unknown

A node attribute named ‘name’ is set for each node with its value set to the value of the ‘name’ attribute from the input network.

If ‘r’ exists on node, the value is added as a node attribute named ‘represents’

All other node attributes are added using the same attribute name as found in the input network. The value is directly set name as found in the input network. The value is directly set as it was found in input network (could be single object or list)

For edges:

Each edge is added setting the source to the value of ‘s’ attribute and target set as ‘t’ attribute of input network.

Any edge attributes named ‘i’ are renamed ‘interaction’ and stored as an attribute for the edge

If the value of an edge attribute is a list then the list values are turned into a string separated by a comma and then enclosed by double quotes.

Coordinates are copied in manner described here: copy_cartesian_coords_into_graph()

Parameters:
  • nice_cx_network (NiceCXNetwork) – Network to extract graph from

  • networkx_graph (networkx.Graph or subtype) – ignored by this implementation

Returns:

Input network converted to networkx Graph

Return type:

networkx.Graph

Base class for Networkx converters above

class ndex2.nice_cx_network.NetworkXFactory[source]

Base class for subclasses that implement a factory that creates networkx.Graph objects and contains a couple utility methods used by implementing factory classes

add_edge(networkx_graph, source_node, target_node, attribute_dict)[source]

Adds edge to graph dealing with differences between networkx 1.x and 2.x+

Parameters:
  • networkx_graph (networkx.Graph or one of its subtypes) – networkx graph to add node to

  • source_node – id of source node

  • target_node – id of target node

  • attribute_dict (dict) – dictionary of edge attributes

Returns:

None

add_network_attributes_from_nice_cx_network(nice_cx_network, networkx_graph)[source]

Iterates through network attributes of input nice_cx_network appending the attributes to the graph object passed in setting the values like so:

networkx_graph.graph[attribute_name] = attribute_value

If the value of a network attribute is of type list then the values are converted to strings and concatenated into a single string separated by commas.

Parameters:
  • nice_cx_network (NiceCXNetwork) – Network to extract network attributes from

  • networkx_graph (networkx.Graph) – networkx Graph object, should work with any of the types of Graphs ie MultiGraph etc..

Raises:

NDExError – If either input parameter is None

Returns:

None

add_node(networkx_graph, nodeid, node_attributes, name=None, represents=None)[source]

Adds node to graph dealing with differences between networkx 1.x and 2.x+

Parameters:
  • networkx_graph (networkx.Graph or one of its subtypes) – networkx graph to add node to

  • nodeid – node identifier can be string, int etc.

  • node_attributes (dict) – dictionary of key => value data to set set node attributes with

  • name (string) – name of node that is set as attribute with key ‘name’ on node

  • represents – represents value for node that is set as attribute with key ‘represents’ on node

Returns:

None

copy_cartesian_coords_into_graph(nice_cx_network, networkx_graph)[source]

Examines the nice_cx_network extracting the content of the opaque aspect CARTESIAN_LAYOUT_ASPECT

Changed in version 3.5.0: code now inverts value of y coordinate so position is correct in networkx

If data is found in above aspect, then this method iterates through the list of values which is assumed to be a dictionary of node ids with coordinates as seen here:

[
 { 'node': <id>, 'x': <x coord>, 'y': <y coord},
 { 'node': <id>, 'x': <x coord>, 'y': <y coord},
 .
 .
]

These values (as seen in example above) are stored in the networkx_graph object as tuples with id of node set as key like so:

networkx_graph.pos[<id from above>] = (<x coord>, <y coord>)
Parameters:
Raises:

NDExError – If either input parameter is None

Returns:

None

Pandas

For conversion to Pandas see to_pandas_dataframe()

CX2Network

The NoStyleCXToCX2NetworkFactory class provides a straightforward way to convert an existing NiceCXNetwork object into a CX2Network. It intentionally omits the style of the original network. It is useful in scenarios where only the network’s structure and data are needed without the style information.

from ndex2.nice_cx_network import NiceCXNetwork
from ndex2.cx2 import NoStyleCXToCX2NetworkFactory

# Create a NiceCXNetwork object
nice_cx_network = NiceCXNetwork()

# Your code to populate nice_cx_network...

# Creating an instance of NoStyleCXToCX2NetworkFactory
factory = NoStyleCXToCX2NetworkFactory()

# Converting NiceCXNetwork to CX2Network without style
cx2_network = factory.get_cx2network(nice_cx_network)

# The resulting cx2_network is now a CX2Network object ready for further use

Note

The conversion preserves the network’s data, data attributes and structure.

Warning

Be aware that the visual style from the NiceCXNetwork will not be preserved in the CX2Network. This includes any node or edge styles, layouts, or color schemes.

Why Convert to CX2Network?

  • Performance: Efficient conversion to CX2 format for improved performance in data processing.

  • Compatibility: Ensures compatibility with tools and libraries designed for CX2 format.

    It allows to generate hierarchy in HCX format which is compatible with Cytoscape Web.

  • New Features: Leverage new features and functionalities available in the CX2 format.

Note

CX version 2 is commonly referred to as CX2. In the Cytoscape ecosystem, CX2 files typically carry the .cx2 file extension. This distinguishes them from CX version 1 networks, which usually use the .cx suffix.