skm_tools.node_images ===================== .. py:module:: skm_tools.node_images .. autoapi-nested-parse:: Images and charts for network nodes: match image files to nodes, make unique copies, make number badges, and build chart definitions. These helpers don't need Cytoscape; :mod:`skm_tools.cytoscape_utils` shows the results on the nodes (:func:`skm_tools.cytoscape_utils.load_node_images`, :func:`skm_tools.cytoscape_utils.show_node_images`). Attributes ---------- .. autoapisummary:: skm_tools.node_images.logger Functions --------- .. autoapisummary:: skm_tools.node_images.node_file_key skm_tools.node_images.match_files_to_nodes skm_tools.node_images.heatmap_images skm_tools.node_images.unique_image_copies skm_tools.node_images.number_badges skm_tools.node_images.chart_column Module Contents --------------- .. py:data:: logger .. py:function:: node_file_key(name) Default key for matching file names to node names (see :func:`match_files_to_nodes`). Replaces the characters that can't (or shouldn't) be in file names with ``_``, e.g. ``"WRKY33[fc00166]"`` -> ``"WRKY33_fc00166_"`` and ``"11-/12-OH-JA"`` -> ``"11-_12-OH-JA"``; gene ids stay as they are. :param name: Node name, or file name without extension. :type name: str :rtype: str .. rubric:: Examples >>> node_file_key("WRKY33[fc00166]") 'WRKY33_fc00166_' .. py:function:: match_files_to_nodes(folder, nodes, key=node_file_key, aliases=None, extensions=('png', 'svg')) Match image files (e.g. one plot per gene or metabolite) to nodes by name. :param folder: Folder with the files. :type folder: str or pathlib.Path :param nodes: Node names, e.g. ``g.nodes()`` or ``py4cytoscape.get_all_nodes()``. :type nodes: iterable :param key: Applied to node names and file names (without extension); they match if the keys are equal. Default :func:`node_file_key`. :type key: callable :param aliases: File name (without extension) -> node name, for files not named after their node (e.g. ``{"Pro": "Proline accumulation"}``). :type aliases: dict, optional :param extensions: File extensions to use (default png and svg). :type extensions: iterable of str :returns: Node -> file path. Nodes with the same key get the same file. Files that match no node are logged (INFO). :rtype: dict .. py:function:: heatmap_images(values, folder, layout, marks=None, vmin=-4, vmax=4, colors=('#2357c4', '#ffffff', '#c4361b'), missing_color='lightgrey', overwrite=False) A small heatmap image per node, e.g. log2FC per time point x dose, for Cytoscape node images. Each image is a grid of cells: ``layout`` gives the columns of `values` per grid row. Values are coloured on a diverging scale centred at 0; missing values are grey; cells where `marks` is True get a star (e.g. DE). Needs matplotlib. :param values: One row per node (the index), with the columns named in `layout`. :type values: pandas.DataFrame :param folder: Folder for the PNG files, named by :func:`node_file_key` (so :func:`match_files_to_nodes` finds them). :type folder: str or pathlib.Path :param layout: Columns of `values` per grid row, e.g. ``[["4h IC50", "4h IC99"], ["24h IC50", "24h IC99"]]``. :type layout: list of list of str :param marks: Booleans with the same index and columns: cells to mark with a star. :type marks: pandas.DataFrame, optional :param vmin: Colour scale limits (default -4, 4), centred at 0. :type vmin: float :param vmax: Colour scale limits (default -4, 4), centred at 0. :type vmax: float :param colors: Low, centre and high colours. :type colors: tuple of 3 colours :param missing_color: Colour of missing values. :type missing_color: str :param overwrite: Redraw existing files (default False: keep them). :type overwrite: bool :returns: Node -> image path, for every node of `values`. :rtype: dict .. rubric:: Examples >>> import pandas as pd >>> lfc = pd.DataFrame({"4h": [1.5, -2.0], "24h": [0.2, None]}, index=["AT1G01010", "AT1G01020"]) >>> images = heatmap_images(lfc, "heatmaps", [["4h"], ["24h"]], marks=lfc.abs() >= 1) # doctest: +SKIP .. py:function:: unique_image_copies(images, folder, to_png=False) Copy images to new, unique file names, optionally converting SVG to PNG. Cytoscape caches images, and doesn't always notice that a file is a different one (e.g. the same file name in another folder) or has changed: it may show an old image instead. Every call makes new copies, with names that no other file has had (``_``), so Cytoscape has to load them again. Old copies are not deleted. :param images: Node -> image path. :type images: dict :param folder: Folder for the copies (created if needed). :type folder: str or pathlib.Path :param to_png: Convert SVG images to PNG. Needs ``cairosvg`` (``pip install cairosvg``). :type to_png: bool :returns: Node -> path of the copy. Nodes with the same image share one copy. :rtype: dict .. py:function:: number_badges(values, folder, size=40, color='#333333', text_color='white') Write a small round badge with a number for each of `values`, e.g. to show a count (such as the number of SNPs of a gene) in a node's corner. Each badge is an SVG file ``badge_.svg`` in `folder`: a filled circle with a white outline and the value in its centre. Show them per node with :func:`skm_tools.cytoscape_utils.load_node_images` (node -> ``badges[count]``) and :func:`skm_tools.cytoscape_utils.show_node_images`, e.g. with ``position="top-right"``. :param values: Values to make badges for (numbers, or short strings). :type values: iterable :param folder: Folder for the badges (created if needed). Existing badges are overwritten; as Cytoscape caches images, use a new folder (or ``unique_dir`` of :func:`~skm_tools.cytoscape_utils.load_node_images`) when changing their look. :type folder: str or pathlib.Path :param size: Width and height in pixels (default 40); the size shown in Cytoscape is set by :func:`~skm_tools.cytoscape_utils.show_node_images`. :type size: int :param color: Badge and number colours (default dark grey and white). :type color: str :param text_color: Badge and number colours (default dark grey and white). :type text_color: str :returns: Value -> path of its badge. :rtype: dict .. py:function:: chart_column(df, columns, colors, chart='barchart', value_range=None, labels=None, na_value=0, **options) Chart definitions for each row of `df`, to show as node charts in Cytoscape. Builds `enhancedGraphics `_ chart strings (e.g. ``barchart: colorlist="..." valuelist="..."``); load them as a node table column and show them like images, with :func:`skm_tools.cytoscape_utils.show_node_images`. Needs the enhancedGraphics app in Cytoscape. :param df: One row per node. :type df: pandas.DataFrame :param columns: Columns of `df` to chart, in order. :type columns: list of str :param colors: Hex colour per column, or one for all. :type colors: str or list of str :param chart: enhancedGraphics chart type, e.g. ``"barchart"`` (default), ``"linechart"``, ``"heatstripchart"``, ``"piechart"``. :type chart: str :param value_range: (min, max) of the value axis (default: per chart, from its values). :type value_range: tuple of float, optional :param labels: Label per column (default: the column names). enhancedGraphics splits lists on commas and can't escape them, so labels can't contain ``,`` or ``"``. :type labels: list of str, optional :param na_value: Value for missing data (default 0). :type na_value: float :param \*\*options: Further enhancedGraphics options, e.g. ``separation=2``, ``ybase=1``. :returns: Chart string per row of `df`. :rtype: pandas.Series :raises ValueError: If a label contains ``,`` or ``"``. .. rubric:: Examples >>> import pandas as pd >>> df = pd.DataFrame({"t1": [1.5], "t2": [-0.5]}, index=["AT2G38470"]) >>> chart_column(df, ["t1", "t2"], "#E41A1C", value_range=(-2, 2)).iloc[0] 'barchart: colorlist="#E41A1C,#E41A1C" valuelist="1.5,-0.5" labellist="t1,t2" range="-2,2"'