skm_tools.utils

Shared helpers.

Attributes

logger

Functions

as_list(x)

A single value or an iterable of values, as a list.

read_skm_table(path[, list_columns, bool_columns, ...])

Read an SKM network export table (PSS or CKN) to a DataFrame.

resolve_nodes(g, nodes[, what])

Split nodes into those in g and those not, logging a warning for the missing ones.

remove_isolate_nodes(g)

Remove nodes without edges from g, in place.

merge_values(attribute, values)

The value to keep for an edge attribute when merging edges.

download(url, path)

Download url to path, compressed or not as the file name says.

download_if_missing(path, url)

Download url to path (see download()) unless the file exists.

contrast_color(color)

The complementary colour of a hex colour (e.g. for a label on that colour).

to_graph_tool(g)

Convert a networkx graph to a graph-tool graph (structure only, no attributes).

Module Contents

skm_tools.utils.logger
skm_tools.utils.as_list(x)

A single value or an iterable of values, as a list.

Strings (and other non-iterables) are a single value; lists, sets, tuples, generators etc. are several. None stays None (e.g. “no filter”). Tuples are several values, so pass a tuple node id in a list.

Parameters:

x (object or iterable or None)

Return type:

list or None

Examples

>>> as_list("ath")
['ath']
>>> as_list(("ath", "stu"))
['ath', 'stu']
>>> as_list(None) is None
True
skm_tools.utils.read_skm_table(path, list_columns=(), bool_columns=(), int_columns=())

Read an SKM network export table (PSS or CKN) to a DataFrame.

The format: tab-separated (optionally gzipped), a header, no quoting, an empty cell for no value, lists joined with | (as in TAIR’s files and GAF: names contain ,, e.g. AHK2,3,4, and gene symbols ;, e.g. PIP1;3).

Parameters:
  • path (str or pathlib.Path)

  • list_columns (collection of str) – Columns to split into lists (empty entries become None, so lists of different columns stay aligned), to read as True/False, and as integers.

  • bool_columns (collection of str) – Columns to split into lists (empty entries become None, so lists of different columns stay aligned), to read as True/False, and as integers.

  • int_columns (collection of str) – Columns to split into lists (empty entries become None, so lists of different columns stay aligned), to read as True/False, and as integers.

Returns:

Of str, list, bool and int values, with None for empty cells.

Return type:

pandas.DataFrame

skm_tools.utils.resolve_nodes(g, nodes, what='nodes')

Split nodes into those in g and those not, logging a warning for the missing ones.

Parameters:
  • g (networkx.Graph)

  • nodes (node or iterable of nodes) – As for as_list().

  • what (str) – What the nodes are, for the warning (e.g. "sources").

Returns:

  • found (list) – The nodes in g, in the given order.

  • missing (list) – The nodes not in g.

skm_tools.utils.remove_isolate_nodes(g)

Remove nodes without edges from g, in place.

All nodes without edges are removed, including those that had none to begin with.

Parameters:

g (networkx.Graph)

Returns:

Removed node -> "isolate" (the removal reason, as in the filter functions).

Return type:

dict

skm_tools.utils.merge_values(attribute, values)

The value to keep for an edge attribute when merging edges.

Missing values (None) are ignored. The rule, for the other values:

  • all equal (==): that value;

  • interaction: "unknown-influence" if they differ;

  • directed: True if any is True;

  • rank: the lowest (best supported) value;

  • anything else: the first value.

Parameters:
  • attribute (str) – The attribute name.

  • values (list) – One value per merged edge, in a fixed order (e.g. by reaction id).

Returns:

  • value (object) – The value to keep (None if all are None).

  • differ (bool) – Whether the values (other than None) differed.

Examples

>>> merge_values("interaction", ["positive-influence", "negative-influence"])
('unknown-influence', True)
>>> merge_values("rank", [2, 0])
(0, True)
skm_tools.utils.download(url, path)

Download url to path, compressed or not as the file name says.

A name ending in .gz is saved gzipped, any other name uncompressed: the download is decompressed or compressed as needed (gzipped downloads are recognised by their Content-Type, application/gzip). The file is written under a temporary name and renamed once complete, so an interrupted download leaves no partial file.

Parameters:
Returns:

path.

Return type:

pathlib.Path

skm_tools.utils.download_if_missing(path, url)

Download url to path (see download()) unless the file exists.

skm_tools.utils.contrast_color(color)

The complementary colour of a hex colour (e.g. for a label on that colour).

Parameters:

color (str) – "#RRGGBB".

Returns:

"#RRGGBB", each channel inverted.

Return type:

str

Examples

>>> contrast_color("#000000")
'#FFFFFF'
skm_tools.utils.to_graph_tool(g)

Convert a networkx graph to a graph-tool graph (structure only, no attributes).

For algorithms that are too slow in networkx on CKN-sized graphs. Requires graph-tool (https://graph-tool.skewed.de), which is not a dependency of skm-tools. Based on https://bbengfort.github.io/2016/06/graph-tool-from-networkx/.

Parameters:

g (networkx.Graph)

Returns:

  • gtG (graph_tool.Graph) – With a vertex property id holding the networkx node id as a string.

  • vertices (dict) – networkx node -> graph-tool vertex.