ndex2

ndex2.create_nice_cx_from_cx(cx)

Create a NiceCXNetwork from a CX list.

Parameters:cx – a list in CX format
Returns:NiceCXNetwork
ndex2.create_nice_cx_from_file(path)

Create a NiceCXNetwork based on CX JSON from a file.

Parameters:path – the path from which the CX will be loaded
Returns:NiceCXNetwork
ndex2.create_nice_cx_from_networkx(G)

Create a NiceCXNetwork based on a networkx graph. The resulting NiceCXNetwork contains the nodes edges and their attributes from the networkx graph and also preserves the graph ‘pos’ attribute as a CX cartesian coordinates aspect.

Parameters:G (networkx graph) – networkx graph
Returns:NiceCXNetwork
Return type:NiceCXNetwork
ndex2.create_nice_cx_from_pandas(df, source_field=None, target_field=None, source_node_attr=[], target_node_attr=[], edge_attr=[], edge_interaction=None)

Create a NiceCXNetwork from a pandas dataframe in which each row specifies one edge in the network.

If only the df argument is provided the 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, the those and any other arguments refer to headers in the dataframe, controlling the mapping of columns to the attributes of nodes, and edges in the resulting NiceCXNetwork. If a header is not mapped the corresponding column is ignored. If the edge_interaction is not specified it defaults to “interacts-with”

Parameters:
  • df – pandas dataframe to process
  • source_field – header name specifying the name of the source node.
  • target_field – header name specifying the name of the target node.
  • source_node_attr – list of header names specifying attributes of the source node.
  • target_node_attr – list of header names specifying attributes of the target node.
  • edge_attr – list of header names specifying attributes of the edge.
  • edge_interaction – the relationship between the source node and the target node, defaulting to “interacts-with”
Returns:

NiceCXNetwork

ndex2.create_nice_cx_from_server(server=None, username=None, password=None, uuid=None)

Create a NiceCXNetwork based on a network retrieved from NDEx, specified by its UUID. If the network is not public, then username and password arguments for an account on the server with permission to access the network must be supplied.

Parameters:
  • server – the URL of the NDEx server hosting the network.
  • username – the user name of an account with permission to access the network.
  • password – the password of an account with permission to access the network.
  • uuid – the UUID of the network.
Returns:

NiceCXNetwork

ndex2.client

class ndex2.client.Ndex2(host=None, username=None, password=None, update_status=False, debug=False)

A class to facilitate communication with an NDEx server.

add_networks_to_networkset(set_id, networks)

Add networks to a network set. User must have visibility of all networks being added

Parameters:
  • set_id (basestring) – network set id
  • networks (list of strings) – networks that will be added to the set
Returns:

Return type:

create_networkset(name, description)

Creates a new network set

Parameters:
  • name (string) – Network set name
  • description (string) – Network set description
Returns:

URI of the newly created network set

Return type:

string

delete_network(network_id, retry=5)

Deletes the specified network from the server

Parameters:
  • network_id (string) – Network id
  • retry (int) – Number of times to retry if deleting fails
Returns:

Error json if there is an error. Blank

Return type:

string

delete_networks_from_networkset(set_id, networks, retry=5)

Removes network(s) from a network set.

Parameters:
  • set_id (basestring) – network set id
  • networks (list of strings) – networks that will be removed from the set
  • retry (int) – Number of times to retry
Returns:

Return type:

get_neighborhood(network_id, search_string, search_depth=1, edge_limit=2500)

Get the CX for a subnetwork of the network specified by UUID network_id and a traversal of search_depth steps around the nodes found by search_string.

Parameters:
  • network_id (str) – The UUID of the network.
  • search_string (str) – The search string used to identify the network neighborhood.
  • search_depth (int) – The depth of the neighborhood from the core nodes identified.
  • edge_limit (int) – The maximum size of the neighborhood.
Returns:

The CX json object.

Return type:

response object

get_neighborhood_as_cx_stream(network_id, search_string, search_depth=1, edge_limit=2500)

Get a CX stream for a subnetwork of the network specified by UUID network_id and a traversal of search_depth steps around the nodes found by search_string.

Parameters:
  • network_id (str) – The UUID of the network.
  • search_string (str) – The search string used to identify the network neighborhood.
  • search_depth (int) – The depth of the neighborhood from the core nodes identified.
  • edge_limit (int) – The maximum size of the neighborhood.
Returns:

The response.

Return type:

response object

get_network_as_cx_stream(network_id)

Get the existing network with UUID network_id from the NDEx connection as a CX stream.

Parameters:network_id (str) – The UUID of the network.
Returns:The response.
Return type:response object
get_network_aspect_as_cx_stream(network_id, aspect_name)

Get the specified aspect of the existing network with UUID network_id from the NDEx connection as a CX stream.

Parameters:
  • network_id (str) – The UUID of the network.
  • aspect_name – The aspect NAME.
Returns:

The response.

Return type:

response object

get_network_ids_for_user(username)

Get the network uuids owned by the user

Parameters:username (str) – users NDEx username
Returns:list of uuids
get_network_set(set_id)

Gets the network set information including the list of networks

Parameters:set_id (basestring) – network set id
Returns:network set information
Return type:dict
get_network_summary(network_id)

Gets information about a network.

Parameters:network_id (str) – The UUID of the network.
Returns:Summary
Return type:dict
get_provenance(network_id)

Gets the network provenance

Parameters:network_id (string) – Network id
Returns:Provenance
Return type:dict
get_sample_network(network_id)

Gets the sample network

Parameters:network_id (string) – Network id
Returns:Sample network
Return type:list of dicts in cx format
get_task_by_id(task_id)

Retrieves a task by id

Parameters:task_id (string) – Task id
Returns:Task
Return type:dict
get_user_by_username(username)

Gets the user id by user name

Parameters:username (string) – User name
Returns:User id
Return type:string
get_user_network_summaries(username, offset=0, limit=1000)

Get a list of network summaries for networks owned by specified user. It returns not only the networks that the user owns but also the networks that are shared with them directly.

Parameters:
  • username (str) – the username of the network owner
  • offset (int) – the starting position of the network search
  • limit
Returns:

list of uuids

Return type:

list

grant_network_to_user_by_username(username, network_id, permission)

Grants permission to network for the given user name

Parameters:
  • username (string) – User name
  • network_id (string) – Network id
  • permission (string) – Network permission
Returns:

Result

Return type:

dict

grant_networks_to_group(groupid, networkids, permission='READ')

Set group permission for a set of networks

Parameters:
  • groupid (string) – Group id
  • networkids (list) – List of network ids
  • permission (string) – Network permission
Returns:

Result

Return type:

dict

grant_networks_to_user(userid, networkids, permission='READ')

Gives read permission to specified networks for the provided user

Parameters:
  • userid (string) – User id
  • networkids (list of strings) – Network ids
  • permission (string (default is READ)) – Network permissions
Returns:

none

Return type:

none

make_network_private(network_id)

Makes the network specified by the network_id private.

Parameters:network_id (str) – The UUID of the network.
Returns:The response.
Return type:response object
make_network_public(network_id)

Makes the network specified by the network_id public.

Parameters:network_id (str) – The UUID of the network.
Returns:The response.
Return type:response object
save_cx_stream_as_new_network(cx_stream, visibility=None, indexed_fields=None)

Create a new network from a CX stream.

Parameters:
  • cx_stream (BytesIO) – IO stream of cx
  • visibility (string) – Sets the visibility (PUBLIC or PRIVATE)
  • indexed_fields (list of strings) – Fields to be indexed
Returns:

Response data

Return type:

string or dict

save_new_network(cx, visibility=None, indexed_fields=None)

Create a new network (cx) on the server

Parameters:
  • cx (list of dicts) – Network cx
  • visibility (string) – Sets the visibility (PUBLIC or PRIVATE)
  • indexed_fields (list of strings) – Fields to be indexed
Returns:

Response data

Return type:

string or dict

search_networks(search_string='', account_name=None, start=0, size=100, include_groups=False)

Search for networks based on the search_text, optionally limited to networks owned by the specified account_name.

Parameters:
  • search_string (str) – The text to search for.
  • account_name (str) – The account to search
  • start (int) – The number of blocks to skip. Usually zero, but may be used to page results.
  • size (int) – The size of the block.
  • include_groups
Returns:

The response.

Return type:

response object

set_network_properties(network_id, network_properties)

Sets network properties

Parameters:
  • network_id (string) – Network id
  • network_properties (list) – List of NDEx property value pairs
Returns:

Return type:

set_network_system_properties(network_id, network_properties)

Set network system properties

Parameters:
  • network_id (string) – Network id
  • network_properties (dict of NDEx network property value pairs) – Network properties
Returns:

Result

Return type:

dict

set_provenance(network_id, provenance)

Sets the network provenance

Parameters:
  • network_id (string) – Network id
  • provenance (dict) – Network provcenance
Returns:

Result

Return type:

dict

set_read_only(network_id, value)

Sets the read only flag on the specified network

Parameters:
  • network_id (string) – Network id
  • value (bool) – Read only value
Returns:

Result

Return type:

dict

update_cx_network(cx_stream, network_id)

Update the network specified by UUID network_id using the CX stream cx_stream.

Parameters:
  • cx_stream – The network stream.
  • network_id (str) – The UUID of the network.
Returns:

The response.

Return type:

response object

update_network_group_permission(groupid, networkid, permission)

Updated group permissions

Parameters:
  • groupid (string) – Group id
  • networkid (string) – Network id
  • permission (string) – Network permission
Returns:

Result

Return type:

dict

update_network_profile(network_id, network_profile)

Updates the network profile Any profile attributes specified will be updated but attributes that are not specified will have no effect - omission of an attribute does not mean deletion of that attribute. The network profile attributes that can be updated by this method are: ‘name’, ‘description’ and ‘version’.

Parameters:
  • network_id (string) – Network id
  • network_profile (dict) – Network profile
Returns:

Return type:

update_network_user_permission(userid, networkid, permission)

Updated network user permission

Parameters:
  • userid (string) – User id
  • networkid (string) – Network id
  • permission (string) – Network permission
Returns:

Result

Return type:

dict

update_status()

Updates the admin status

Returns:None (however the status is stored in the client object self.status)
Return type:

ndex2.niceCXNetwork

class ndex2.niceCXNetwork.NiceCXNetwork(cx=None, server=None, username=None, password=None, uuid=None, networkx_G=None, pandas_df=None, filename=None, data=None, **attr)
add_edge(edge_element)

Add an edge object to the network. (For an easier method for adding edges use create_edge() )

Parameters:edge_element (cx.aspects.EdgesElement) – An edge object
Returns:Edge ID
Return type:
add_node(node_element)

Add a node object to the network. (For an easier method for adding nodes use create_node() )

Parameters:node_element (cx.aspects.NodesElement) – A node object
Returns:Node ID
Return type:int
apply_template(server, uuid, username=None, password=None)

Get a network from NDEx, copy its cytoscapeVisualProperties aspect to this network.

Parameters:
  • server (string) – server host name (i.e. public.ndexbio.org)
  • username (string) – username (optional - used when accessing private networks)
  • password (string) – password (optional - used when accessing private networks)
  • uuid (string) – uuid of the styled network
Returns:

None

Return type:

none

create_edge(id=None, edge_source=None, edge_target=None, edge_interaction=None, cx_fragment=None)

Create a new edge in the network by specifying source-interaction-target

Parameters:
  • id
  • edge_source (int) – The source node this edge, either its id or the node object itself.
  • edge_target (int) – The target node this edge, either its id or the node object itself.
  • edge_interaction (string) – The interaction that describes the relationship between the source and target nodes
  • cx_fragment (json) – CX Fragment
Returns:

Edge ID

Return type:

int

create_from_pandas(df, source_field=None, target_field=None, source_node_attr=[], target_node_attr=[], edge_attr=[], edge_interaction=None)

Constructor that uses a pandas dataframe to build niceCX

Parameters:
  • df (Pandas Dataframe) – dataframe
  • headers
Returns:

none

Return type:

n/a

getProvenance()

Get the network provenance as a Python dictionary having the CX provenance schema.

Returns:List of provenance
Return type:list of json objects
get_context()

Get the @context aspect of the network, the aspect that maps namespace prefixes to their defining URIs

Returns:List of context objects
Return type:list of json objects
get_edge_attribute(edge, attribute_name, subnetwork=None)

Get the value(s) of an attribute of an edge, where the edge may be specified by its id or passed in as an object.

Parameters:
  • edge (cx.aspects.EdgesElement or int) – Edge object or edge id
  • attribute_name – Attribute name
  • subnetwork (int) – The id of the subnetwork (if any) to which this attribute was applied.
Returns:

Edge attribute value(s)

Return type:

list, string, int or float

get_edge_attribute_objects(edge, attribute_name)

Get the attribute objects for an edge attribute name, where the edge may be specified by its id or passed in as an object. The edge attribute objects include datatype and subnetwork information. An example of networks that include subnetworks are Cytoscape collections stored in NDEx.

Parameters:
  • edge (cx.aspects.EdgesElement or int) – Edge object or edge id
  • attribute_name – Attribute name
Returns:

Edge attribute object

Return type:

cx.aspects.EdgesAttributesElement

get_edge_attributes(edge)

Get the attribute objects of an edge, where the edge may be specified by its id or passed in as an object.

Parameters:edge (cx.aspects.EdgeElement or int) – Edge object or edge id
Returns:Edge attribute objects
Return type:list of cx.aspects.EdgeAttributesElement
get_edges()

Returns an iterator over edge ids as keys and edge objects as values.

Returns:Edge iterator
Return type:iterator
get_metadata()

Get the network metadata

Returns:Network metadata
Return type:Iterator of metadata.MetaDataElement
get_name()

Get the network name

Returns:Network name
Return type:string
get_network_attribute_objects(attribute_name)

Get the value of a network attribute

Parameters:attribute_name (string) – Attribute name
Returns:Network attribute object
Return type:cx.aspects.NetworkAttributesElement
get_network_attributes()

Get the attribute objects of the network.

Returns:List of NetworkAttributesElement objects
Return type:cx.aspects.NetworkAttributesElement
get_node_attribute(node, attribute_name)

Get the value(s) of an attribute of a node, where the node may be specified by its id or passed in as an object.

Parameters:
  • node (cx.aspects.NodesElement or int) – node object or node id
  • attribute_name – attribute name
Returns:

string

Return type:

get_node_attribute_objects(node, attribute_name)

Get the attribute objects for a node attribute name, where the node may be specified by its id or passed in as an object. The node attribute objects include datatype and subnetwork information. An example of networks that include subnetworks are Cytoscape collections stored in NDEx.

Parameters:
  • node (cx.aspects.NodesElement or int) – node object or node id
  • attribute_name (string) – attribute name
Returns:

Return type:

get_node_attributes(node)

Get the attribute objects of a node, where the node may be specified by its id or passed in as an object.

Parameters:node (cx.aspects.NodesElement or int) – node object or node id
Returns:
Return type:
get_nodes()

Returns an iterator over node ids as keys and node objects as values.

Returns:iterator over nodes
Return type:iterator
get_opaque_aspect(aspect_name)

Get the elements of the aspect specified by aspect_name

Parameters:aspect_name (string) – the name of the aspect to retrieve.
Returns:Opaque aspect
Return type:cx.aspects.AspectElement
get_opaque_aspect_names()

Get the names of all opaque aspects

Returns:List of opaque aspect names
Return type:list of strings
get_summary()

Get a network summary

Returns:Network summary
Return type:string
merge_node_attributes(source_attribute1, source_attribute2, target_attribute)

Checks 2 attribute fields for values and merges them into one attribute. The best use is when one attribute is empty which occurs when loading from an edge file. Use with caution

Parameters:
  • source_attribute1 (basestring) – The name of the first attribute
  • source_attribute2 (basestring) – The name of the second attribute
  • target_attribute (basestring) – The desired name for the merged data
Returns:

Return type:

set_context(context)

Set the @context aspect of the network, the aspect that maps namespace prefixes to their defining URIs

Parameters:context (List of dict (namespace string: URI)) – List of context objects
Returns:None
Return type:none
set_edge_attribute(edge, attribute_name, values, type=None, subnetwork=None, cx_fragment=None)

Set the value(s) of attribute of an edge, where the edge may be specified by its id or passed in an object.

Parameters:
  • edge
  • attribute_name – Attribute name
  • values – The values of the attribute
  • type (cx.aspects.ATTRIBUTE_DATA_TYPE) – The datatype of the attribute values, defaults to the python datatype of the values.
  • subnetwork (string or int) – The id of the subnetwork to which this attribute applies.
  • cx_fragment (json) – CX Fragment (optional)
Returns:

none

Return type:

set_metadata(metadata_obj)

Set the network metadata

Parameters:metadata_obj (dict of metadata.MetaDataElement) – Dict of metadata objects
Returns:None
Return type:none
set_name(network_name)

Set the network name

Parameters:network_name (string) – Network name
Returns:None

:rtype:none

set_network_attribute(name=None, values=None, type=None, subnetwork=None, cx_fragment=None)

Set an attribute of the network

Parameters:
  • name (string) – Attribute name
  • values (list, string, float or int) – The values of the attribute
  • type (cx.aspects.ATTRIBUTE_DATA_TYPE) – The datatype of the attribute values
  • subnetwork (int) – The id of the subnetwork (if any) to which this attribute applies.
  • cx_fragment (json) – CX fragment
Returns:

None

Return type:

none

set_node_attribute(node, attribute_name, values, type=None, subnetwork=None, cx_fragment=None)

Set the value(s) of an attribute of a node, where the node may be specified by its id or passed in as an object.

Parameters:
  • node (cx.aspects.NodesElement or int) – node object or node id
  • attribute_name (string) – attribute name
  • values (list, string, int or float) – A value or list of values of the attribute
  • type (cx.aspects.ATTRIBUTE_DATA_TYPE) – the datatype of the attribute values, defaults to the python datatype of the values.
  • subnetwork (int or string) – the id of the subnetwork to which this attribute applies.
  • cx_fragment (json) – CX fragment
Returns:

none

Return type:

set_opaque_aspect(aspect_name, aspect_elements)

Set the aspect specified by aspect_name to the list of aspect elements. If aspect_elements is None, the aspect is removed.

Parameters:
  • aspect_name (string) – Name of the aspect
  • aspect_elements (cx.aspects.AspectElement) – Aspect element
Returns:

None

Return type:

none

set_provenance(provenance)

Set the network provenance

Parameters:provenance (list) – List of provenance objects
Returns:None
Return type:none
to_cx()

Return the CX corresponding to the network.

Returns:CX representation of the network
Return type:CX (list of dict aspects)
to_cx_stream()

Returns a stream of the CX corresponding to the network. Can be used to post to endpoints that can accept streaming inputs

Returns:The CX stream representation of this network.
Return type:io.BytesIO
to_networkx()

Return a NetworkX graph based on the network. Elements in the CartesianCoordinates aspect of the network are transformed to the NetworkX pos attribute.

Returns:Networkx graph
Return type:networkx Graph()
to_pandas_dataframe()

Export the network as a Pandas DataFrame. Note: This method only processed nodes, edges, node attributes and edge attributes, but not network attributes or other aspects

Returns:Pandas dataframe
Return type:Pandas dataframe
update_to(uuid, server, username, password)

Upload this network to the specified server to the account specified by username and password.

Parameters:
  • server (str) – The NDEx server to upload the network to.
  • username (str) – The username of the account to store the network.
  • password (str) – The password for the account.
Returns:

The UUID of the network on NDEx.

Return type:

str

Example:
ndexGraph.upload_to(‘http://test.ndexbio.org‘, ‘myusername’, ‘mypassword’)
upload_to(server, username, password)

Upload this network to the specified server to the account specified by username and password. Example: ndexGraph.upload_to(‘http://test.ndexbio.org‘, ‘myusername’, ‘mypassword’)

Parameters:
  • server (string) – The NDEx server to upload the network to.
  • username (string) – The username of the account to store the network.
  • password (string) – The password for the account.
Returns:

The UUID of the network on NDEx.

Return type:

string

Table Of Contents

This Page