skm_tools.pss ============= .. py:module:: skm_tools.pss .. autoapi-nested-parse:: PSS (Plant Stress Signalling): load the network exports, filter, simplify and rewire. The PSS exports (reaction graph, interaction network, per-species gene networks) are made by skm-pss-export (https://github.com/NIB-SI/skm-pss-export). The loaders record which network a graph is in ``g.graph["pss_network"]`` (``"reaction_graph"``, ``"interaction_network"`` or ``"gene_network"``, with the species of a gene network in ``g.graph["species"]``); the functions here use it to check that they are applied to a network they work on. Attributes ---------- .. autoapisummary:: skm_tools.pss.logger skm_tools.pss.REACTION_GRAPH skm_tools.pss.INTERACTION_NETWORK skm_tools.pss.GENE_NETWORK Functions --------- .. autoapisummary:: skm_tools.pss.pss_reaction_graph_to_networkx skm_tools.pss.pss_interaction_network_to_networkx skm_tools.pss.pss_gene_network_to_networkx skm_tools.pss.remove_reactions skm_tools.pss.remove_deadend_complexes skm_tools.pss.filter_pss_nodes skm_tools.pss.simplify_pss skm_tools.pss.remove_and_rewire skm_tools.pss.remove_duplicated_binding_edges skm_tools.pss.metabolite_enzymes skm_tools.pss.pathway_gene_sets Module Contents --------------- .. py:data:: logger .. py:data:: REACTION_GRAPH :value: 'reaction_graph' .. py:data:: INTERACTION_NETWORK :value: 'interaction_network' .. py:data:: GENE_NETWORK :value: 'gene_network' .. py:function:: pss_reaction_graph_to_networkx(edge_path=None, node_path=None, data_dir='.') .. py:function:: pss_interaction_network_to_networkx(edge_path=None, node_path=None, data_dir='.') .. py:function:: pss_gene_network_to_networkx(edge_path=None, node_path=None, species='ath', data_dir='.') .. py:function:: remove_reactions(g, reaction_ids, remove_isolates=True) Remove PSS reactions, in place. Works on all three PSS networks: in the reaction graph, the reaction nodes are removed (with their participant edges); in the interaction network and the gene networks, the edges with these ``reaction_id`` values. Merged edges (e.g. from :func:`simplify_pss`) that also come from other reactions are kept, with only those reaction ids. :param g: PSS network. Changed in place. :type g: networkx.MultiDiGraph or networkx.DiGraph :param reaction_ids: Reactions to remove (e.g. ``["rx00001"]``); reactions not in `g` are ignored. :type reaction_ids: str or iterable of str :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 (``"reaction removed"`` for reaction nodes of the reaction graph, ``"isolate"``). :rtype: dict .. rubric:: Examples >>> import networkx as nx >>> g = nx.MultiDiGraph() >>> _ = g.add_edge("A", "B", key="rx1", reaction_id="rx1") >>> _ = g.add_edge("B", "C", key="rx2", reaction_id="rx2") >>> remove_reactions(g, "rx1") {'A': 'isolate'} .. py:function:: remove_deadend_complexes(g) Remove complexes without outgoing edges, in place. A complex that influences nothing (is not an input or modifier of any reaction) is a dead end in directed analyses. Repeated until no dead-end complexes are left, since removing one can leave another without outgoing edges. In the interaction network and the gene networks, only the complexes are removed: the binding partners' mutual edges from the same reactions are kept. In the reaction graph, the reactions producing the complexes are removed (see :func:`remove_reactions`), and then the nodes left without edges. :param g: PSS network. Changed in place. :type g: networkx.DiGraph or networkx.MultiDiGraph :returns: Removed node -> reason (``"dead-end complex"``; in the reaction graph also ``"reaction removed"`` and ``"isolate"``). :rtype: dict .. py:function:: filter_pss_nodes(g, node_types=None, species=None, remove_isolates=True) Remove PSS nodes, in place. With `species`, the functional clusters without genes in the species, and the complexes they are components of, are removed with their reactions (see :func:`remove_reactions`), as in the species' gene network: applied to the interaction network, the result has the same reactions and edges as the gene network, with clusters instead of genes. With `node_types`, complexes with a removed component are removed too (using the ``components`` and ``component_cluster_ids`` node attributes; components not in `g` are ignored). :param g: PSS network. Changed in place. :type g: networkx.Graph :param node_types: Keep only nodes of these ``node_type`` values (e.g. ``"PlantCoding"``, ``"Complex"``; in a gene network, genes have their cluster's class, e.g. ``"PlantCoding"``). Not for the reaction graph, where it would leave reactions with missing participants. :type node_types: str or list of str, optional :param species: Interaction network or reaction graph: remove the functional clusters without genes in any of these species (``_homologues`` attributes, e.g. ``["stu"]``), the complexes with such a cluster among their components (``component_cluster_ids``), and their reactions. Abstract clusters (``PlantAbstract``) have no genes and are kept, as metabolites. Merged edges (e.g. from :func:`simplify_pss`) are kept if they also come from other reactions. A gene network is already for one species. :type species: str or list 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 (``"species missing"``, ``"wrong node type"``, ``"complex component removed"``, ``"reaction removed"`` or ``"isolate"``). :rtype: dict :raises ValueError: If `node_types` is given for the reaction graph, `species` for a gene network, or a species without a ``_homologues`` attribute in `g` (e.g. a misspelt code, or an export for some species only, which has ``genes`` instead). .. py:function:: simplify_pss(g, split_on_attrs=None, verbose=False) Merge parallel edges (one per reaction) into one edge per node pair. Returns a new graph; `g` is unchanged. For the interaction network or a gene network, not the reaction graph (raises ValueError). Each edge gets the ``reaction_id`` values of the edges merged into it, as a sorted list, and their other attributes per reaction: lists aligned with ``reaction_id`` (e.g. ``reaction_type``, ``source_role``, ``evidence_sentence``, ``influence_sbo``). Edges that aren't merged get lists too, so each attribute has one type. Three attributes are summarised in one value instead (:func:`skm_tools.utils.merge_values`), ignoring missing values (None): - all values equal: that value; - ``interaction``: ``"unknown-influence"`` if they differ (e.g. a positive and a negative influence; ``influence_sbo`` has the influence of each reaction); - ``directed``: True if any edge is directed; - ``rank``: the lowest. For example, the edge of reactions rx1 (catalysis) and rx2 (binding/oligomerisation): ``reaction_id`` ``["rx1", "rx2"]``, ``reaction_type`` ``["catalysis", "binding/oligomerisation"]``. To compare an attribute with a value, compare its elements: ``"catalysis" in d["reaction_type"]``. :param g: PSS interaction network or gene network. :type g: networkx.MultiDiGraph :param split_on_attrs: Edge attributes (e.g. ``["interaction"]``) that must not be merged away: parallel edges are only merged with others that have the same values for all of them. These keep their one value (not a list). :type split_on_attrs: str or list of str, optional :param verbose: Log every merged edge whose summary attributes differed, with the value kept (INFO; otherwise DEBUG). A summary, with the number of merged edges per differing attribute, is always logged (INFO). :type verbose: bool :returns: A DiGraph, or a MultiDiGraph with `split_on_attrs` (as edges that differ on them stay separate). Graph attributes (e.g. ``pss_network``) are copied. :rtype: networkx.DiGraph or networkx.MultiDiGraph :raises ValueError: If `g` is the reaction graph, or not a multigraph (e.g. already simplified). .. py:function:: remove_and_rewire(g, nodes, dry_run=False) Remove nodes, connecting each of their upstream nodes to each of their downstream nodes. Changes `g` in place. The nodes are removed one at a time, each against the graph as rewired so far, so chains of removed nodes (A -> B -> C -> D, removing B and C) are bridged (A -> D). They are removed upstream first (in a topological order of the network, nodes on a cycle by name), whatever the order they are given in, so the result doesn't depend on it. Rewiring doesn't go through the mutual edges between binding partners (partner <-> partner), in either direction, only through the complex-forming ones (partner -> complex): A -> B and B <-> D (binding) give no A -> D, but A -> B and B -> B|D give A -> B|D. Catalysis: the catalyst -| substrate edges (the enzyme uses up its substrate) are not rewired through as they are. Removing a substrate connects the nodes upstream of it to its catalysts instead, as substrate -> catalyst (positive); and not to the product of a catalysed reaction, which it reaches through the catalyst (the substrate -> product edge of a reaction with a catalyst is not rewired through, from the substrate's side; nor from the product's, when the substrate is removed too, so the result doesn't depend on the order). So removing the metabolites of a pathway connects its enzymes, in order: E1 catalysing A -> B and E2 catalysing B -> C give E1 -> E2 (through B; positive), without shortcuts past E2. A reaction without a catalyst keeps its substrate -> product route (E1 -> B, B -> C uncatalysed, C -> D by E3: E1 -> E3). The edges are recognised by their ``reaction_type``, roles and reaction ids, per reaction: each reaction of a merged edge (e.g. from :func:`simplify_pss`) is rewired as its own. A new edge A -> C, replacing A -> B -> C: - ``reaction_id``: the reaction ids of both edges (a sorted list), and the other attributes per reaction, as in :func:`simplify_pss`: ``source_*`` attributes from A -> B (None for the reactions of B -> C only, in which A takes no part), ``target_*`` attributes from B -> C (None for those of A -> B only), and the others from the edge of the reaction; - ``note``: ``"rewired through B"`` (for each reaction); - ``interaction``: the sign of the chain (positive and negative: negative; two negatives: positive; anything with an unknown influence: unknown); - ``directed``: True; ``rank``: the lower. If A -> C already exists, the new edge is merged into it, as in :func:`simplify_pss` (a reaction of both keeps the attributes of the existing edge). :param g: A simple directed graph, e.g. from :func:`simplify_pss`. Multigraphs are not supported. :type g: networkx.DiGraph :param nodes: Nodes to remove (nodes not in `g` are ignored, with a warning). :type nodes: node or iterable of nodes :param dry_run: Don't change `g`; return the edges that would be added instead. :type dry_run: bool :returns: Removed node -> reason (``"rewired"``, or ``"nothing to rewire"`` if no edge replaced it); with `dry_run`, the edges ``(u, v, data)`` that would be added or changed. :rtype: dict or list :raises NotImplementedError: If `g` is a multigraph or undirected. :raises ValueError: If `g` is the reaction graph. .. py:function:: remove_duplicated_binding_edges(g) Keep one direction of each mutual binding edge, in place. Binding partners are linked in both directions (A -> B and B -> A, same reaction); this removes one of the two (which one is arbitrary), e.g. for a less cluttered drawing. A merged edge (e.g. from :func:`simplify_pss`) is removed only if all its reactions are bindings that the opposite edge has too. Don't use it before directed path or neighbourhood analysis. :param g: PSS interaction network or gene network (not the reaction graph; each parallel edge of a multigraph is considered separately). Changed in place. :type g: networkx.DiGraph or networkx.MultiDiGraph :returns: The removed edges, ``(u, v)`` or, for a multigraph, ``(u, v, key)``. :rtype: list .. py:function:: metabolite_enzymes(g) The enzymes of the reactions that produce and that consume each metabolite. From the catalysis reactions: a catalyst's edge to a reaction's product (``target_role`` ``"product"``) and to its substrate (``"substrate"``), for each reaction of an edge (also of merged edges, e.g. from :func:`simplify_pss`). :param g: PSS interaction network or gene network. :type g: networkx.MultiDiGraph or networkx.DiGraph :returns: One row per enzyme, metabolite and reaction: ``enzyme``, ``metabolite``, ``role`` (``"producer"`` or ``"consumer"``), ``reaction_id``, ``metabolite_type`` (``Metabolite`` or ``MetaboliteFamily``). :rtype: pandas.DataFrame .. py:function:: pathway_gene_sets(g, attribute='all_pathways', genes_only=True) PSS pathways as node sets, e.g. for an enrichment analysis. :param g: PSS gene network (or interaction network). :type g: networkx.Graph :param attribute: Node attribute with the pathways (default ``"all_pathways"``; ``"pathway"`` for the main ones). :type attribute: str :param genes_only: Only genes (nodes with a ``species``; default), not metabolites or complexes. :type genes_only: bool :returns: Pathway -> sorted list of nodes. :rtype: dict