CX2
The CX2Network
class represents the CX2 (Cytoscape Exchange) network format.
The class provides structured access, manipulation, reading, processing, and writing of network data elements including nodes, edges, attributes, visual properties, and other features of the network.
New in version 3.6.0.
CX2Network methods
- class ndex2.cx2.CX2Network[source]
A representation of the CX2 (Cytoscape Exchange) network format.
This class provides functionality to read, process, and write data in the CX2 format. It facilitates the structured access and manipulation of network data elements such as nodes, edges, attributes, and visual properties.
The class maintains internal data structures that hold network data and provides methods to:
Load data from raw CX2 files.
Generate the CX2 representation of the current state.
Write the current state to a CX2 formatted file.
- Attributes:
attribute_declarations
A dictionary representing the declarations of attributes for network elements.
network_attribute
A dictionary storing global attributes of the network.
nodes
A dictionary of nodes.
edges
A dictionary of edges.
aliases
A dictionary that maps aspect names (like “nodes” or “edges”) to their alias declarations.
default_values
A dictionary that maps aspect names to their default attribute values.
visual_properties
A list storing visual properties of the network.
node_bypasses
A dictionary of node-specific visual properties that bypass default styles.
edge_bypasses
A dictionary of edge-specific visual properties that bypass default styles.
opaque_aspects
A list of other aspects in the CX2 format which don’t have a defined structure in this class.
status
A dictionary representing the network’s status.
New in version 3.6.0.
- add_edge(edge_id=None, source=None, target=None, attributes=None)[source]
Adds an edge to the network.
- add_edge_bypass(edge_id, value)[source]
Adds an edge-specific visual property bypass.
- Parameters:
edge_id (str) – ID of the edge.
value (Any) – Visual property bypass value.
- add_node(node_id=None, attributes=None, x=None, y=None, z=None)[source]
Adds a node to the network.
- Parameters:
- Raises:
NDExAlreadyExists – If node with node_id already exists
- add_node_bypass(node_id, value)[source]
Adds a node-specific visual property bypass.
- Parameters:
node_id (str) – ID of the node.
value (Any) – Visual property bypass value.
- add_opaque_aspect(aspect)[source]
Adds an opaque aspect to the list of opaque aspects.
- Parameters:
aspect (dict) – The opaque aspect to add.
- create_from_raw_cx2(cx2_data)[source]
Loads and processes a raw CX2 data into structured data within the instance.
- get_attribute_declarations()[source]
Retrieves the attribute declarations.
- Returns:
The attribute declarations.
- Return type:
- get_declared_type(aspect_name, attribute_name, attribute_value=None)[source]
Retrieves the declared data type for a given aspect’s attribute.
- Parameters:
aspect_name (str) – The name of the aspect (e.g., ‘nodes’, ‘edges’).
attribute_name (str) – The attribute whose declared data type needs to be retrieved.
attribute_value (str, int, bool, float, list) – Actual value that will be used to infer data type if data type has not yet been defined for attribute
- Returns:
The declared data type or ‘string’ if not found.
- Return type:
- get_default_value(aspect_name, attribute_name)[source]
Retrieves default value for a given aspect’s attribute.
- get_edge_bypasses()[source]
Retrieves the edge-specific visual property bypasses.
- Returns:
The edge-specific visual property bypasses.
- Return type:
- get_edges()[source]
Retrieves the edges in the network.
- Returns:
Edges in the network.
- Return type:
- get_network_attributes()[source]
Retrieves the network attributes.
- Returns:
The network attributes.
- Return type:
- get_node_bypasses()[source]
Retrieves the node-specific visual property bypasses.
- Returns:
The node-specific visual property bypasses.
- Return type:
- get_nodes()[source]
Retrieves the nodes in the network.
- Returns:
Nodes in the network.
- Return type:
- get_opaque_aspects()[source]
Retrieves the opaque aspects of the network.
- Returns:
The opaque aspects of the network.
- Return type:
- get_status()[source]
Retrieves the status of the network.
- Returns:
The status of the network.
- Return type:
- get_visual_properties()[source]
Retrieves the visual properties of the network.
- Returns:
The visual properties of the network.
- Return type:
- remove_node(node_id)[source]
Removes a node and checks for dangling edges (edges without both source and target).
- set_attribute_declarations(value)[source]
Sets the attribute declarations.
- Parameters:
value (dict) – The attribute declarations to set.
- set_network_attributes(network_attrs)[source]
Sets the network attributes after processing them using declared types in attribute declarations.
- Parameters:
network_attrs (dict) – The network attributes to set.
- set_opaque_aspects(value)[source]
Sets the opaque aspects for the network.
- Parameters:
value (list) – New opaque aspects for the network.
- set_status(value)[source]
Sets the status for the network.
- Parameters:
value (dict) – New status for the network.
- set_visual_properties(value)[source]
Sets the visual properties for the network.
- Parameters:
value (dict) – New visual properties for the network.
- to_cx2()[source]
Generates the CX2 representation of the current state of the instance.
This method constructs a list structure representing the current state of the network in the CX2 format.
- update_node(node_id, attributes=None, x=None, y=None, z=None)[source]
Updates the attributes of a node.
- Parameters:
- Raises:
NDExError – if node with node_id passed in does not exist
- write_as_raw_cx2(output_path)[source]
Writes data from CX2Network object to a raw CX2 formatted JSON file.
- Parameters:
output_path (str) – Destination file path for the CX2 formatted output.
CX2NetworkFactory classes and methods
- class ndex2.cx2.CX2NetworkFactory[source]
Base class for Factory classes that create
CX2Network
objectsNew in version 3.6.0.
- get_cx2network(input_data=None) CX2Network [source]
Defines method that creates
CX2Network
Warning
Subclasses should implement, this method always raises
NotImplementedError
- Parameters:
input_data – Optional input data for used to generate network
- Raises:
NotImplementedError – Always raised. Subclasses should implement
- Returns:
Generated network
- Return type:
- class ndex2.cx2.NoStyleCXToCX2NetworkFactory[source]
Creates
CX2Network
network from CX data orNiceCXNetwork
New in version 3.6.0.
- get_cx2network(input_data=None) CX2Network [source]
Creates
CX2Network
from CX data orNiceCXNetwork
but does NOT convert the style associated with input networkNote
Style is NOT converted by this call
- Parameters:
input_data (str, list or
NiceCXNetwork
) – Optional input data used to generate network- Returns:
Generated network
- Return type:
- class ndex2.cx2.RawCX2NetworkFactory[source]
Factory class responsible for creating
CX2Network
instances directly from raw CX2.New in version 3.6.0.
- get_cx2network(input_data=None) CX2Network [source]
Converts the provided raw CX2 into a
CX2Network
object.- Parameters:
input_data (dict or similar mapping type) – Raw CX2 to be converted.
- Returns:
A constructed
CX2Network
object from the input data.- Return type:
Supported data types for attributes
string
double
boolean
integer
long
list_of_string
list_of_double
list_of_boolean
list_of_integer
list_of_long
Example
import json
from ndex2.cx2 import CX2Network
net = CX2Network()
net.create_from_raw_cx2('path_to_your_cx2_file.cx2')
net.write_as_raw_cx2('output_path.cx2')