===================== Cytoscape automation ===================== :mod:`skm_tools.cytoscape_utils` sends networks and analysis results to `Cytoscape `_, to look at, style and export them. It uses `py4cytoscape `_, which controls a running Cytoscape through its REST interface (CyREST). Getting started =============== You need: - the ``cytoscape`` extra: ``pip install "skm-tools[cytoscape]"`` (see :doc:`installation`), - Cytoscape 3.10 or later, open on the same computer. Python talks to it while it runs, so leave it open. Check the connection: .. code-block:: python import py4cytoscape as p4c from skm_tools import cytoscape_utils as cu p4c.cytoscape_ping() # "You are connected to Cytoscape!" A few Cytoscape terms used below: - **SUID**: Cytoscape's id for a network (and for each node and edge). Functions here return the SUID of the networks they create, and take a network as ``network`` (its SUID or its name; without it, the network currently selected in Cytoscape). SUIDs are not kept: they change when a session is saved and opened again. To find a network again, use its name, e.g. ``p4c.get_network_suid("PSS")``. - **Collection**: a group of networks, e.g. a network and its subnetworks; shown as a folder in Cytoscape's *Network* panel. The networks of a collection share their node and edge tables: a column added in one network is there in all of them. - **Visual style**: how a network is drawn (shapes, colours, labels), from the values in the node and edge tables. Several networks can share a style. - **Node and edge tables**: the node and edge attributes, one column per attribute. Loading data into a new column, and using that column in a style, is how data is shown on the network. Loading a network ================= :func:`~skm_tools.cytoscape_utils.load_network` sends a networkx graph to Cytoscape. The node and edge attributes become table columns, typed from their values (Double, Integer, Boolean, String, or List of these for list attributes), also when only some nodes have a value, e.g. data added with :func:`~skm_tools.experimental_data.add_node_data`. ``style`` is optional: it applies one of the SKM styles; without it, Cytoscape uses its default style. .. code-block:: python suid = cu.load_network(pss, title="PSS", style="skm") Cytoscape groups columns named ``namespace::name`` under their namespace in the table browser. ``namespaces`` puts attributes in a namespace by their name's prefix, only in Cytoscape (the networkx attributes keep their names). Style mappings then use the Cytoscape column names: .. code-block:: python # "gwas maxLog10P" -> "gwas::maxLog10P", "gwas Rust maxLog10P" -> "gwas::Rust maxLog10P" suid = cu.load_network(ckn_gwas, title="GWAS", style="skm", namespaces={"gwas ": "gwas"}) p4c.set_node_color_mapping("gwas::Rust maxLog10P", [5, 18], ["#fdd0a2", "#a63603"], network=suid) Cytoscape namespaces have one level, and column names ignore case. The SKM styles are bundled with skm-tools, and imported into Cytoscape the first time they are used (also with :func:`~skm_tools.cytoscape_utils.apply_builtin_style`). CKN and PSS use the same node classes (``node_type``) and edge attributes, so they share a style: - ``"skm"`` (*SKM*): for CKN, the PSS interaction network and the PSS gene networks. Nodes are coloured and shaped by ``node_type``, in the colours of the PSS Explorer, and labelled with ``display_label``. Edges are coloured, and get their arrow, by ``interaction`` (positive: green, negative: red, unknown: grey); mutual (``directed`` False) edges are dashed, and better supported edges (lower ``rank``) are thicker. - ``"skm-reactions"`` (*SKM-reactions*): for the PSS reaction graph. The same nodes, plus the reactions (blue); edges as in the PSS Explorer, by ``edge_type`` (activation: green, inhibition: red, substrate and product: grey), labelled with the participant's ``role``. To change a style for one analysis, copy it first (see :func:`~skm_tools.cytoscape_utils.copy_style`), so the original stays as it is: the SKM styles are shared by every network they are applied to. Marking nodes with a style mapping ================================== Instead of bypasses (see Highlighting), nodes can be marked through a column and a style mapping, which stays with the data: :func:`~skm_tools.cytoscape_utils.flag_column` sets a node attribute to a flag for some nodes and to another attribute (default ``node_type``) for the rest, and :func:`~skm_tools.cytoscape_utils.extend_discrete_mapping` maps a visual property on that column, keeping the entries of an existing mapping (e.g. the SKM shapes per ``node_type``) and adding the flags. .. code-block:: python cu.flag_column(g, "shape_key", {tf: "TF" for tf in tfs}) cu.flag_column(g, "border_flag", {n: "robust DEG" for n in robust}) suid = cu.load_network(g, "DEGs", style="skm", significant_digits=3) style = cu.copy_style("SKM", "SKM-flags", networks=[suid]) cu.extend_discrete_mapping(style, "NODE_SHAPE", "shape_key", {"TF": "DIAMOND"}, network=suid) cu.extend_discrete_mapping(style, "NODE_BORDER_PAINT", "border_flag", {"robust DEG": "#e0a100"}, network=suid) cu.extend_discrete_mapping(style, "NODE_BORDER_WIDTH", "border_flag", {"robust DEG": 6}, network=suid) ``significant_digits`` in :func:`~skm_tools.cytoscape_utils.load_network` rounds the float values loaded into the tables (here to 3 significant digits), for smaller sessions. Highlighting ============ Highlighting changes the look of single nodes and edges, on top of the style (Cytoscape calls these *bypasses*: they stay when the style changes, until cleared in Cytoscape). - :func:`~skm_tools.cytoscape_utils.highlight_nodes`: fill, label and border colour, border width, size. - :func:`~skm_tools.cytoscape_utils.highlight_edges`: colour and line width. - :func:`~skm_tools.cytoscape_utils.highlight_path`: the nodes and edges of a path. For paths from ``get_paths(..., directed=False)``, which can use an edge against its direction, pass ``directed=False`` to highlight the edges either way. Each returns what it highlighted; :func:`~skm_tools.cytoscape_utils.clear_highlights` removes the highlights of the nodes and edges given (or of all, which is slow for large networks). To highlight several paths in different colours, pass what was already highlighted as ``skip_nodes`` and ``skip_edges``, so later paths don't paint over earlier ones. The example below highlights the shortest paths from JA to SA in red, and then those from ABA to SA in blue; nodes and edges on both (e.g. SA itself) stay red: .. code-block:: python from skm_tools.paths import get_paths done_nodes, done_edges = [], [] for paths, color in [(get_paths(pss, "JA", "SA"), "#E41A1C"), (get_paths(pss, "ABA", "SA"), "#377EB8")]: for p in paths: nodes, edges = cu.highlight_path(p, color, skip_nodes=done_nodes, skip_edges=done_edges, network=suid) done_nodes += nodes done_edges += edges :func:`~skm_tools.cytoscape_utils.apply_shortest_paths_style` shows the results of several path searches (e.g. one per source) in one go, through a style instead of highlights. It adds three columns to the tables: for the nodes on the paths, their number of steps to the end of the path (``distance-to-target``; on several paths, the smallest) and the source whose paths they are on (``node-path-source``; the first source, if on several), and for the edges on the paths, ``edge-priority`` (``"direct path ()"``). It then makes a copy of the network's style, ``