skm_tools.experimental_data
Add experimental data (e.g. logFC and p-values, or any table) to network nodes.
Attributes
Functions
|
Add experimental values to the nodes of g as node attributes. |
|
Add the columns of a table to the nodes of g as node attributes. |
Module Contents
- skm_tools.experimental_data.logger
- skm_tools.experimental_data.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=Trueit annotates a copy and leaves g unchanged, e.g. to keep the unannotated graph, or to add several experiments to separate copies.- Parameters:
g (networkx.Graph) – Network to annotate.
df (pandas.DataFrame) – Experimental results, indexed by identifier (e.g. gene id), with a logFC column and optionally a p-value column.
logfc_col (str) – Column of df with the (log) fold change.
pvalue_col (str, optional) – Column of df with the (adjusted) p-value.
cutoff (float) – p-value at or below which a value is marked significant (default 0.05). Only used with pvalue_col.
match_attribute (str, optional) – 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.prefix (str, optional) – Prefix for the new attribute names, to keep several experiments apart, e.g.
"H2O2 10min ". Default: no prefix.copy (bool) – Annotate a copy of g instead of g itself (default False).
- 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.
- Return type:
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 withAt1g01010or isoforms (AT1G01010.1):df.index = df.index.str.upper().str.replace(r"\.\d+$", "", regex=True)
A warning is logged if no node matches.
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
- skm_tools.experimental_data.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
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).- Parameters:
g (networkx.Graph) – Network to annotate.
df (pandas.DataFrame) – Data indexed by identifier (e.g. gene id), one row per identifier.
columns (list of str, optional) – Columns of df to add. Default: all columns.
match_attribute (str, optional) – Match df index values against this node attribute instead of the node id, e.g.
"translation"for a CKN translated withintegrate_translation_ckn()(the target-species gene). Several nodes can match the same identifier; each gets the same values.prefix (str, optional) – Prefix for the new attribute names, e.g.
"gwas ". Default: no prefix.rank_by (str, optional) – 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.ascending (bool) – See rank_by (default False).
copy (bool) – Annotate a copy of g instead of g itself (default False).
- 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.- Return type:
- Raises:
KeyError – If a column (or rank_by) is not in df.
ValueError – If the index of df has duplicates: aggregate to one row per identifier first.
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'