skm_tools.dinar =============== .. py:module:: skm_tools.dinar .. autoapi-nested-parse:: Export networks as DiNAR node and edge tables. `DiNAR `_ (Differential Network Analysis in R) shows experimental data on a knowledge network, one cluster of nodes at a time. Its custom networks are tab-separated node and edge tables with fixed column names (as DiNAR's ``Networks/PSS/Ath-based_*.txt``); missing values are ``-``. DiNAR shows one cluster at a time (cluster 0 not at all), and can't show clusters with more than 2000 edges. Attributes ---------- .. autoapisummary:: skm_tools.dinar.MAX_CLUSTER_EDGES skm_tools.dinar.DINAR_NODE_COLUMNS skm_tools.dinar.DINAR_EDGE_COLUMNS Functions --------- .. autoapisummary:: skm_tools.dinar.to_dinar skm_tools.dinar.write_dinar Module Contents --------------- .. py:data:: MAX_CLUSTER_EDGES :value: 2000 .. py:data:: DINAR_NODE_COLUMNS :value: ['geneID', 'shortDescription', 'shortName', 'MapManBin', 'clusterID', 'x', 'y',... .. py:data:: DINAR_EDGE_COLUMNS :value: ['geneID1', 'geneID2', 'reactionType', 'clusterID_geneID1', 'clusterID_geneID2',... .. py:function:: to_dinar(g, clusters=None, positions=None, seed=0) Node and edge tables of `g` in DiNAR's format. :param g: PSS or CKN network (any networkx graph type), e.g. a PSS gene network. :type g: networkx.Graph :param clusters: The clusters DiNAR shows one at a time: a node attribute (e.g. ``"pathway"``; for list attributes, the first value is used), or a ``{node: cluster}`` mapping (e.g. from community detection). Nodes without a cluster (None or NaN) are put together in one more cluster. Default: all nodes in one cluster (fine for small networks, e.g. PSS). Nodes without edges get ``clusterID`` 0, which DiNAR doesn't show. :type clusters: str or dict, optional :param positions: Node coordinates: a ``{node: (x, y)}`` mapping (as returned by the networkx layout functions), or a node attribute holding them. Default: the ``pos`` node attribute (the networkx convention, ``nx.set_node_attributes(g, pos, "pos")``) if every node has one; otherwise a spring layout of each cluster, with the clusters on a grid. :type positions: str or dict, optional :param seed: Random seed for the default layout (default 0, so the layout is the same each time, with the same networkx and numpy versions). :type seed: int, optional :returns: * **nodes** (*pandas.DataFrame*) -- One row per node: ``geneID`` (the node id), ``shortDescription`` (``description``), ``shortName`` (``display_label``, or the node id), ``MapManBin`` (the ``mapman`` bins joined with ``" | "``), ``clusterID`` (1, 2, ..., largest cluster first; 0 for nodes without edges), ``x``, ``y``, ``clusterSimplifiedNodeDegree`` (number of neighbours in the same cluster) and ``expressed`` (1). * **edges** (*pandas.DataFrame*) -- ``geneID1``, ``geneID2``, ``reactionType``, the clusters and cluster degrees of both nodes, and ``exists`` (1). ``reactionType`` is ``act_``, ``inh_`` or ``unk_`` (from the ``interaction``: positive, negative or unknown influence; DiNAR draws edges by it), followed by the PSS ``reaction_type`` (e.g. ``act_protein activation``), or ``influence`` for networks without one (CKN). Parallel edges with the same ``reactionType`` are one row. * Missing values are ``-``. :raises ValueError: If `positions` has no coordinates for some nodes, two node ids are the same as text (e.g. ``1`` and ``"1"``), or a cluster has more than 2000 edges (DiNAR can't show it: use smaller clusters). .. rubric:: Examples >>> import networkx as nx >>> g = nx.MultiDiGraph() >>> g.add_node("AT2G38470", display_label="WRKY33", mapman=["26.11.3.2.1_External stimuli"]) >>> g.add_node("camalexin") >>> _ = g.add_edge("AT2G38470", "camalexin", interaction="positive-influence") >>> nodes, edges = to_dinar(g) >>> nodes.loc[0, ["geneID", "shortName", "MapManBin"]].tolist() ['AT2G38470', 'WRKY33', '26.11.3.2.1_External stimuli'] .. py:function:: write_dinar(g, nodes_path, edges_path, clusters=None, positions=None, seed=0) Write `g` as DiNAR node and edge tables (tab-separated), see :func:`to_dinar`. :param g: Network to write. :type g: networkx.Graph :param nodes_path: Files to write (overwritten if they exist). :type nodes_path: str or pathlib.Path :param edges_path: Files to write (overwritten if they exist). :type edges_path: str or pathlib.Path :param clusters: As for :func:`to_dinar`. :param positions: As for :func:`to_dinar`. :param seed: As for :func:`to_dinar`.