Creating NiceCXNetwork objects¶
-
ndex2.
create_nice_cx_from_raw_cx
(cx)[source]¶ Create a
NiceCXNetwork()
from a as a list of dict objects in CX formatExample:
import json import ndex2 # cx_as_str is a str containing JSON in CX format above net_cx = ndex2.create_nice_cx_from_raw_cx(json.loads(cx_as_str))
Parameters: cx (list) – CX as a list of dict objects Returns: NiceCXNetwork Return type: NiceCXNetwork()
-
ndex2.
create_nice_cx_from_file
(path)[source]¶ Create a
NiceCXNetwork()
from a file that is in the CX formatParameters: path (str) – the path of the CX file
Raises: - Exception – if path is not a file
- OSError – if there is an error opening the path file
- JSONDecodeError – if there is an error parsing the path file with json.load()
Returns: NiceCXNetwork
Return type:
-
ndex2.
create_nice_cx_from_server
(server, username=None, password=None, uuid=None, ndex_client=None)[source]¶ Create a
NiceCXNetwork()
based on a network retrieved from NDEx, specified by its UUID.Changed in version 3.5.0: Code refactor and ndex_client parameter has been added
If the network is not public, then username and password arguments (or ndex_client with username and password set) for an account on the server with permission to access the network must be supplied.
Example usage:
import ndex2 # Download BioGRID: Protein-Protein Interactions (SARS-CoV) from NDEx # https://www.ndexbio.org/viewer/networks/669f30a3-cee6-11ea-aaef-0ac135e8bacf net_cx = ndex2.create_nice_cx_from_server(None, uuid='669f30a3-cee6-11ea-aaef-0ac135e8bacf')
Note
If ndex_client is not passed in, this function internally creates
Ndex2
using values from parameters passed into this function.Parameters: - server (str) – the URL of the NDEx server hosting the network
- username (str) – the user name of an account with permission to access the network
- password (str) – the password of an account with permission to access the network
- uuid (str) – the UUID of the network
- ndex_client (
Ndex2
) – Used as NDEx REST client overriding server, username and password parameters if set
Raises: NDExError – If uuid is not specified
Returns: NiceCXNetwork
Return type:
Networkx¶
-
ndex2.
create_nice_cx_from_networkx
(G)[source] Creates a
NiceCXNetwork
based on anetworkx.Graph
graph.Changed in version 3.5.0: Major refactor to fix multiple bugs #83, #84, #90
import ndex2 import networkx as nx G = nx.Graph() G.add_node(1, someval=1.5, name='node 1') G.add_node(2, someval=2.5, name='node 2') G.add_edge(1, 2, weight=5) print(ndex2.create_nice_cx_from_networkx(G).to_cx())
The resulting
NiceCXNetwork
contains the nodes, edges and their attributes from thenetworkx.Graph
graph and also preserves the graph ‘pos’ attribute as a CX cartesian coordinates aspectCARTESIAN_LAYOUT_ASPECT
with the values of Y invertedDescription of how conversion is performed:
Network:
- Network name is set value of
G.graph.get('name')
or tocreated from networkx by ndex2.create_nice_cx_networkx()
if name isNone
or not present
Nodes:
- Node id is value of
n
from this for loop:for n, d G.nodes(data=True):
ifn
is NOT anint
, new ids starting from0
are used - Node name is value of name attribute on the node or is set to id of node if name is not present.
- Node represents is value of represents attribute on the
node or set is to node name if
None
or not present
Edges:
- Interaction is value of interaction attribute on the edge
or is set to
neighbor-of
ifNone
or not present
Note
Data types are inferred by using
isinstance()
and converted to corresponding CX data types. For list items, only the 1st item is examined to determine typeParameters: G ( networkx.Graph
) – Graph to convertRaises: Exception – if G parameter is None
or there is another error in conversionReturns: Converted network Return type: NiceCXNetwork
- Network name is set value of
Pandas¶
-
ndex2.
create_nice_cx_from_pandas
(df, source_field=None, target_field=None, source_node_attr=[], target_node_attr=[], edge_attr=[], edge_interaction=None, source_represents=None, target_represents=None)[source] Create a
NiceCXNetwork()
from apandas.DataFrame
in which each row specifies one edge in the network.Changed in version 3.5.0: Removed print statements showing progress and network name is now being set
If only the df argument is provided the
pandas.DataFrame
is treated as ‘SIF’ format, where the first two columns specify the source and target node ids of the edge and all other columns are ignored. The edge interaction is defaulted to “interacts-with”If both the source_field and target_field arguments are provided, then those and any other arguments refer to headers in the
pandas.DataFrame
, controlling the mapping of columns to the attributes of nodes, and edges in the resultingNiceCXNetwork()
.If a header is not mapped, the corresponding column is ignored.
If the edge_interaction is not specified, interaction is set to “interacts-with”
import ndex2 import pandas as pd data = {'source': ['Node 1','Node 2'], 'target': ['Node 2','Node 3'], 'interaction': ['helps', 'hurts']} df = pd.DataFrame.from_dict(data) net = ndex2.create_nice_cx_from_pandas(df, source_field='source', target_field='target', edge_interaction='interaction') print(net.get_nodes()) print(net.get_edges())
Note
The datatype for everything added to the network is the CX string type
Parameters: - df (
pandas.DataFrame
) – Pandas dataframe to process - source_field (str) – header name specifying the name of the source node.
- target_field (str) – header name specifying the name of the target node.
- source_node_attr (list) – list of header names specifying attributes of the source node.
- target_node_attr (list) – list of header names specifying attributes of the target node.
- edge_attr (list) – list of header names specifying attributes of the edge.
- edge_interaction (str) – the relationship between the source node and the target node, defaulting to “interacts-with”
- source_represents (str) –
- target_represents (str) –
Returns: NiceCXNetwork
Return type: - df (