skm_tools.utils
Shared helpers.
Attributes
Functions
|
A single value or an iterable of values, as a list. |
|
Read an SKM network export table (PSS or CKN) to a DataFrame. |
|
Split nodes into those in g and those not, logging a warning for the missing ones. |
Remove nodes without edges from g, in place. |
|
|
The value to keep for an edge attribute when merging edges. |
|
Download url to path, compressed or not as the file name says. |
|
Download url to path (see |
|
The complementary colour of a hex colour (e.g. for a label on that colour). |
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.
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:
- 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:
- 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:
- 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
.gzis saved gzipped, any other name uncompressed: the download is decompressed or compressed as needed (gzipped downloads are recognised by theirContent-Type,application/gzip). The file is written under a temporary name and renamed once complete, so an interrupted download leaves no partial file.- Parameters:
url (str)
path (str or pathlib.Path)
- Returns:
path.
- Return type:
- 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).
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
idholding the networkx node id as a string.vertices (dict) – networkx node -> graph-tool vertex.