skm_tools.experimental_data =========================== .. py:module:: skm_tools.experimental_data .. autoapi-nested-parse:: Add experimental data (e.g. logFC and p-values, or any table) to network nodes. Attributes ---------- .. autoapisummary:: skm_tools.experimental_data.logger Functions --------- .. autoapisummary:: skm_tools.experimental_data.add_experimental_data skm_tools.experimental_data.add_node_data Module Contents --------------- .. py:data:: logger .. py:function:: add_experimental_data(g, df, logfc_col, pvalue_col=None, cutoff=0.05, match_attribute=None, prefix=None, copy=False) Add experimental values to the nodes of `g` as node attributes. Like the filter functions, this changes `g` in place (and returns it). With ``copy=True`` it annotates a copy and leaves `g` unchanged, e.g. to keep the unannotated graph, or to add several experiments to separate copies. :param g: Network to annotate. :type g: networkx.Graph :param df: Experimental results, indexed by identifier (e.g. gene id), with a logFC column and optionally a p-value column. :type df: pandas.DataFrame :param logfc_col: Column of `df` with the (log) fold change. :type logfc_col: str :param pvalue_col: Column of `df` with the (adjusted) p-value. :type pvalue_col: str, optional :param cutoff: p-value at or below which a value is marked significant (default 0.05). Only used with `pvalue_col`. :type cutoff: float :param match_attribute: Match `df` index values against this node attribute instead of the node id. The attribute can be a single value or a list, e.g. ``"ath_homologues"`` to put gene-level data on PSS functional cluster nodes, or ``"TAIR"`` for CKN. :type match_attribute: str, optional :param prefix: Prefix for the new attribute names, to keep several experiments apart, e.g. ``"H2O2 10min "``. Default: no prefix. :type prefix: str, optional :param copy: Annotate a copy of `g` instead of `g` itself (default False). :type copy: bool :returns: `g` (with ``copy=True``: a copy of `g`). Each node with at least one matching row gets: - ``{prefix}logFC``: the logFC, - ``{prefix}pvalue``: the p-value (with `pvalue_col`), - ``{prefix}significant``: p-value <= `cutoff` (with `pvalue_col`), - ``{prefix}matched_ids``: the `df` identifiers that matched the node. If several rows match a node (e.g. several genes of one functional cluster, or an identifier that is in `df` more than once), the values are those of the most significant row: lowest p-value, then largest absolute logFC. Nodes without a match get none of these attributes: values from an earlier call with the same `prefix` are removed. Missing (NaN) values in `df` are stored as None. :rtype: networkx.Graph .. rubric:: Notes Identifiers are matched exactly. Normalise the index of `df` first if it differs from the node ids, e.g. for CKN (upper-case TAIR loci, ``AT1G01010``) and a table with ``At1g01010`` or isoforms (``AT1G01010.1``):: df.index = df.index.str.upper().str.replace(r"\.\d+$", "", regex=True) A warning is logged if no node matches. .. rubric:: Examples >>> import networkx as nx, pandas as pd >>> g = nx.DiGraph([("AT1G01010", "AT1G01020")]) >>> df = pd.DataFrame({"logFC": [1.5], "padj": [0.01]}, index=["AT1G01010"]) >>> h = add_experimental_data(g, df, "logFC", "padj") >>> h.nodes["AT1G01010"]["significant"] True .. py:function:: add_node_data(g, df, columns=None, match_attribute=None, prefix=None, rank_by=None, ascending=False, copy=False) Add the columns of a table to the nodes of `g` as node attributes. The general version of :func:`add_experimental_data`, for data other than logFC and p-values, e.g. GWAS results summarised per gene. Matching works the same way: on the node id, or on a node attribute (a single value or a list). :param g: Network to annotate. :type g: networkx.Graph :param df: Data indexed by identifier (e.g. gene id), one row per identifier. :type df: pandas.DataFrame :param columns: Columns of `df` to add. Default: all columns. :type columns: list of str, optional :param match_attribute: Match `df` index values against this node attribute instead of the node id, e.g. ``"translation"`` for a CKN translated with :func:`~skm_tools.translate.integrate_translation_ckn` (the target-species gene). Several nodes can match the same identifier; each gets the same values. :type match_attribute: str, optional :param prefix: Prefix for the new attribute names, e.g. ``"gwas "``. Default: no prefix. :type prefix: str, optional :param rank_by: If a node matches several identifiers (a list attribute), use the row with the highest value in this column (lowest with ``ascending=True``). Default: the first matching row in `df` order. :type rank_by: str, optional :param ascending: See `rank_by` (default False). :type ascending: bool :param copy: Annotate a copy of `g` instead of `g` itself (default False). :type copy: bool :returns: `g` (with ``copy=True``: a copy of `g`). Each node with a matching row gets ``{prefix}{column}`` for each column, and ``{prefix}matched_ids``: the `df` identifiers that matched the node. Nodes without a match get none of these attributes: values from an earlier call with the same `prefix` and columns are removed. Missing (NaN) values are stored as None. :rtype: networkx.Graph :raises KeyError: If a column (or `rank_by`) is not in `df`. :raises ValueError: If the index of `df` has duplicates: aggregate to one row per identifier first. .. rubric:: Examples >>> import networkx as nx, pandas as pd >>> g = nx.DiGraph([("AT1G01010_PruarM.1G000100", "AT1G01020")]) >>> g.nodes["AT1G01010_PruarM.1G000100"]["translation"] = "PruarM.1G000100" >>> df = pd.DataFrame({"maxLog10P": [5.2], "trait": ["Rust"]}, index=["PruarM.1G000100"]) >>> h = add_node_data(g, df, match_attribute="translation", prefix="gwas ") >>> h.nodes["AT1G01010_PruarM.1G000100"]["gwas trait"] 'Rust'