skm_tools.utils =============== .. py:module:: skm_tools.utils .. autoapi-nested-parse:: Shared helpers. Attributes ---------- .. autoapisummary:: skm_tools.utils.logger Functions --------- .. autoapisummary:: skm_tools.utils.as_list skm_tools.utils.read_skm_table skm_tools.utils.resolve_nodes skm_tools.utils.remove_isolate_nodes skm_tools.utils.merge_values skm_tools.utils.download skm_tools.utils.download_if_missing skm_tools.utils.contrast_color skm_tools.utils.to_graph_tool Module Contents --------------- .. py:data:: logger .. py:function:: 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. :param x: :type x: object or iterable or None :rtype: list or None .. rubric:: Examples >>> as_list("ath") ['ath'] >>> as_list(("ath", "stu")) ['ath', 'stu'] >>> as_list(None) is None True .. py:function:: 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). :param path: :type path: str or pathlib.Path :param list_columns: Columns to split into lists (empty entries become None, so lists of different columns stay aligned), to read as ``True``/``False``, and as integers. :type list_columns: collection of str :param bool_columns: Columns to split into lists (empty entries become None, so lists of different columns stay aligned), to read as ``True``/``False``, and as integers. :type bool_columns: collection of str :param int_columns: Columns to split into lists (empty entries become None, so lists of different columns stay aligned), to read as ``True``/``False``, and as integers. :type int_columns: collection of str :returns: Of str, list, bool and int values, with None for empty cells. :rtype: pandas.DataFrame .. py:function:: resolve_nodes(g, nodes, what='nodes') Split `nodes` into those in `g` and those not, logging a warning for the missing ones. :param g: :type g: networkx.Graph :param nodes: As for :func:`as_list`. :type nodes: node or iterable of nodes :param what: What the nodes are, for the warning (e.g. ``"sources"``). :type what: str :returns: * **found** (*list*) -- The nodes in `g`, in the given order. * **missing** (*list*) -- The nodes not in `g`. .. py:function:: 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. :param g: :type g: networkx.Graph :returns: Removed node -> ``"isolate"`` (the removal reason, as in the filter functions). :rtype: dict .. py:function:: 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. :param attribute: The attribute name. :type attribute: str :param values: One value per merged edge, in a fixed order (e.g. by reaction id). :type values: list :returns: * **value** (*object*) -- The value to keep (None if all are None). * **differ** (*bool*) -- Whether the values (other than None) differed. .. rubric:: Examples >>> merge_values("interaction", ["positive-influence", "negative-influence"]) ('unknown-influence', True) >>> merge_values("rank", [2, 0]) (0, True) .. py:function:: 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. :param url: :type url: str :param path: :type path: str or pathlib.Path :returns: `path`. :rtype: pathlib.Path .. py:function:: download_if_missing(path, url) Download `url` to `path` (see :func:`download`) unless the file exists. .. py:function:: contrast_color(color) The complementary colour of a hex colour (e.g. for a label on that colour). :param color: ``"#RRGGBB"``. :type color: str :returns: ``"#RRGGBB"``, each channel inverted. :rtype: str .. rubric:: Examples >>> contrast_color("#000000") '#FFFFFF' .. py:function:: 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/. :param g: :type 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.