skm_tools.ckn ============= .. py:module:: skm_tools.ckn .. autoapi-nested-parse:: CKN (Comprehensive Knowledge Network): load and filter. Attributes ---------- .. autoapisummary:: skm_tools.ckn.logger skm_tools.ckn.CKN_RANKS skm_tools.ckn.TF_REGULATION Functions --------- .. autoapisummary:: skm_tools.ckn.ckn_to_networkx skm_tools.ckn.rank_counts skm_tools.ckn.transcription_factors skm_tools.ckn.tf_target_enrichment skm_tools.ckn.filter_ckn_edges skm_tools.ckn.filter_ckn_nodes Module Contents --------------- .. py:data:: logger .. py:data:: CKN_RANKS :value: (0, 1, 2, 3, 4) The CKN edge ranks, from 0 (curated, from PSS) to 4 (only predicted). .. py:function:: ckn_to_networkx(edge_path=None, node_path=None, as_directed=True, create_using=nx.DiGraph, data_dir='.') Load CKN (v2.1) to a networkx directed graph, including node attributes. Downloads the CKN files from skm.nib.si if they don't exist yet. :param edge_path: Path to the edge file (default ``/AtCKN-v2.1-2026.10.tsv.gz``); if the file does not exist, it is downloaded from skm.nib.si, gzipped if the name ends in ``.gz``. :type edge_path: str or pathlib.Path, optional :param node_path: Path to the node file (default ``/AtCKN-v2.1-2026.10_node-annot.tsv.gz``); downloaded as for `edge_path`. :type node_path: str or pathlib.Path, optional :param as_directed: Add undirected edges (``directed`` False, e.g. binding) in both directions, so directed path searches can use them either way (default True): A -> B (undirected) becomes A -> B and B -> A. If False, every edge is added once, as listed in the file. To leave out the undirected edges, use ``filter_ckn_edges(g, filter_function=lambda d: d["directed"])``. :type as_directed: bool :param create_using: Graph class to create (default ``networkx.DiGraph``; ``networkx.MultiDiGraph`` keeps parallel edges). CKN can have two edges between the same nodes, of different types (e.g. ABI4 binds RGL2, and activates its transcription): a DiGraph keeps the last one of each direction, a MultiDiGraph all of them. :type create_using: networkx graph class :param data_dir: Folder for the default file names (default: the current folder). :type data_dir: str or pathlib.Path :returns: CKN (or the `create_using` type): node attributes ``node_type`` (the PSS class, e.g. ``PlantCoding``, ``Metabolite``), ``locus_type`` (genes and RNAs: the TAIR locus type, e.g. ``protein_coding``, ``mirna``), ``species``, ``TAIR``, ``display_label``, ``short_name``, ``synonyms``, ``description``, ``mapman``, ``note`` and ``tissue``; edge attributes ``interaction`` (``positive-influence``, ``negative-influence`` or ``unknown-influence``), ``directed``, ``rank``, ``effect``, ``type``, ``species``, ``isTFregulation`` and ``interactionSources``. ``synonyms``, ``mapman``, ``tissue`` and ``interactionSources`` are lists; empty values are None. Nodes of the node file without edges are left out. :rtype: networkx.DiGraph .. py:function:: rank_counts(g) The number of edges of each CKN rank. :param g: CKN, e.g. from :func:`ckn_to_networkx`. :type g: networkx.Graph :returns: Rank -> number of edges, for every rank in :data:`CKN_RANKS` (and any other rank in `g`). Edges without a ``rank`` are not counted. :rtype: dict .. py:data:: TF_REGULATION :value: 'transcription factor regulation' The CKN edge ``type`` of a transcription factor regulating a gene. .. py:function:: transcription_factors(g, mapman_bins=None) Transcription factors in CKN. The sources of the network's transcription factor regulation edges (``type`` ``"transcription factor regulation"``), and optionally the nodes annotated with MapMan bins, e.g. ``"14.5"`` (RNA biosynthesis.DNA-binding transcriptional regulation in MapMan4), for TFs without regulation edges in CKN. Only the edges in `g` count: filter the edges first (:func:`filter_ckn_edges`) to restrict to some ranks. Load CKN as a MultiDiGraph (``ckn_to_networkx(create_using=nx.MultiDiGraph)``): a DiGraph keeps one edge per node pair and direction, so a binding edge can hide a regulation edge between the same genes. :param g: CKN, e.g. from :func:`ckn_to_networkx`. :type g: networkx.Graph :param mapman_bins: MapMan bins whose nodes (and those of their sub-bins) also count as TFs; see :func:`skm_tools.annotations.get_nodes_by_mapman`. :type mapman_bins: str or list of str, optional :rtype: set .. py:function:: tf_target_enrichment(g, degs, universe=None, min_targets=5) Transcription factors whose CKN targets are over-represented among differentially expressed genes. For each TF (source of transcription factor regulation edges in `g`), a one-sided hypergeometric test of its targets among the DEGs. The background is the universe genes with at least one incoming transcription factor regulation edge in `g`: genes no TF is known to regulate can't be hits, and would make every TF look enriched. Only the edges in `g` count: filter the ranks first (:func:`filter_ckn_edges`) if needed. Load CKN as a MultiDiGraph (``ckn_to_networkx(create_using=nx.MultiDiGraph)``): a DiGraph keeps one edge per node pair and direction, so a binding edge can hide a regulation edge between the same genes. The activity score is computed over the TF's DE targets with a signed edge (``interaction`` positive-influence / negative-influence; all edges from the TF to the target must agree) and a direction: the mean of sign(edge) x direction(target), +1 if all are consistent with a more active TF, -1 with a less active one. :param g: CKN, e.g. from :func:`ckn_to_networkx` (DiGraph or MultiDiGraph). :type g: networkx.Graph :param degs: The DEGs: node -> direction (+1 up, -1 down, 0 unknown or mixed), or just the nodes (no direction). :type degs: dict or iterable :param universe: The genes that could be DE, e.g. those measured. Default: every node of `g`. :type universe: iterable, optional :param min_targets: Test only TFs with at least this many targets in the background (default 5). :type min_targets: int :returns: One row per tested TF, sorted by p: ``tf``, ``targets`` (in the background), ``hits`` (DE targets), ``up_hits``, ``down_hits``, ``expected``, ``fold``, ``p``, ``padj`` (Benjamini-Hochberg over the TFs), ``n_signed``, ``activity_score`` (NaN without signed hits), ``hit_genes`` (list). The background size and the number of DEGs in it are in ``attrs["background"]`` and ``attrs["n_degs"]``. :rtype: pandas.DataFrame .. py:function:: filter_ckn_edges(g, keep_edge_ranks=None, keep_edge_types=None, filter_function=None, remove_isolates=True) Remove CKN edges, in place. The filters are combined: an edge is kept only if it passes all of them. :param g: CKN, e.g. from :func:`ckn_to_networkx`. Changed in place. :type g: networkx.Graph :param keep_edge_ranks: Keep only edges of these ranks (0: best supported, to 4). Edges without a ``rank`` attribute are kept. :type keep_edge_ranks: int or iterable of int, optional :param keep_edge_types: Keep only edges of these ``type`` values (e.g. ``"binding"``). :type keep_edge_types: str or iterable of str, optional :param filter_function: Called with each edge's attribute dict; return True to keep the edge. :type filter_function: callable, optional :param remove_isolates: Also remove nodes without edges afterwards (default True); this includes nodes that had no edges before. :type remove_isolates: bool :returns: * **removed_edges** (*list*) -- The removed edges, ``(u, v)`` or, for a multigraph, ``(u, v, key)``. * **removed_nodes** (*dict*) -- Removed node -> ``"isolate"``. .. py:function:: filter_ckn_nodes(g, node_types=None, species=None, tissues=None, remove_isolates=True) Remove CKN nodes, in place. Complexes with a removed component are removed too: if a component is removed, the complex can't exist. :param g: CKN, e.g. from :func:`ckn_to_networkx`. Changed in place. :type g: networkx.Graph :param node_types: Keep only nodes of these ``node_type`` values (the PSS classes, e.g. ``"PlantCoding"``, ``"Metabolite"``). :type node_types: str or iterable of str, optional :param species: Keep only nodes of these species (e.g. ``"ath"``); nodes without a species (e.g. metabolites) are kept. Complexes of an Arabidopsis protein and a pathogen (species ``"ath/foreign"``, e.g. ``RISC - vsiRNA``) are removed with ``species="ath"``, as their foreign component is. :type species: str or iterable of str, optional :param tissues: Keep only nodes annotated with at least one of these tissues. :type tissues: str or iterable of str, optional :param remove_isolates: Also remove nodes without edges afterwards (default True); this includes nodes that had no edges before. :type remove_isolates: bool :returns: Removed node -> reason (``"wrong species"``, ``"wrong node type"``, ``"wrong tissue type"``, ``"complex component removed"`` or ``"isolate"``). :rtype: dict