skm_tools.node_images
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; skm_tools.cytoscape_utils shows the results on the
nodes (skm_tools.cytoscape_utils.load_node_images(),
skm_tools.cytoscape_utils.show_node_images()).
Attributes
Functions
|
Default key for matching file names to node names (see |
|
Match image files (e.g. one plot per gene or metabolite) to nodes by name. |
|
A small heatmap image per node, e.g. log2FC per time point x dose, for Cytoscape node images. |
|
Copy images to new, unique file names, optionally converting SVG to PNG. |
|
Write a small round badge with a number for each of values, e.g. to show a count |
|
Chart definitions for each row of df, to show as node charts in Cytoscape. |
Module Contents
- skm_tools.node_images.logger
- skm_tools.node_images.node_file_key(name)
Default key for matching file names to node names (see
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.Examples
>>> node_file_key("WRKY33[fc00166]") 'WRKY33_fc00166_'
- skm_tools.node_images.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.
- Parameters:
folder (str or pathlib.Path) – Folder with the files.
nodes (iterable) – Node names, e.g.
g.nodes()orpy4cytoscape.get_all_nodes().key (callable) – Applied to node names and file names (without extension); they match if the keys are equal. Default
node_file_key().aliases (dict, optional) – File name (without extension) -> node name, for files not named after their node (e.g.
{"Pro": "Proline accumulation"}).extensions (iterable of str) – File extensions to use (default png and svg).
- Returns:
Node -> file path. Nodes with the same key get the same file. Files that match no node are logged (INFO).
- Return type:
- skm_tools.node_images.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:
layoutgives 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.- Parameters:
values (pandas.DataFrame) – One row per node (the index), with the columns named in layout.
folder (str or pathlib.Path) – Folder for the PNG files, named by
node_file_key()(somatch_files_to_nodes()finds them).layout (list of list of str) – Columns of values per grid row, e.g.
[["4h IC50", "4h IC99"], ["24h IC50", "24h IC99"]].marks (pandas.DataFrame, optional) – Booleans with the same index and columns: cells to mark with a star.
vmin (float) – Colour scale limits (default -4, 4), centred at 0.
vmax (float) – Colour scale limits (default -4, 4), centred at 0.
colors (tuple of 3 colours) – Low, centre and high colours.
missing_color (str) – Colour of missing values.
overwrite (bool) – Redraw existing files (default False: keep them).
- Returns:
Node -> image path, for every node of values.
- Return type:
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)
- skm_tools.node_images.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 (
<name>_<random id>), so Cytoscape has to load them again. Old copies are not deleted.- Parameters:
images (dict) – Node -> image path.
folder (str or pathlib.Path) – Folder for the copies (created if needed).
to_png (bool) – Convert SVG images to PNG. Needs
cairosvg(pip install cairosvg).
- Returns:
Node -> path of the copy. Nodes with the same image share one copy.
- Return type:
- skm_tools.node_images.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_<value>.svgin folder: a filled circle with a white outline and the value in its centre. Show them per node withskm_tools.cytoscape_utils.load_node_images()(node ->badges[count]) andskm_tools.cytoscape_utils.show_node_images(), e.g. withposition="top-right".- Parameters:
values (iterable) – Values to make badges for (numbers, or short strings).
folder (str or pathlib.Path) – Folder for the badges (created if needed). Existing badges are overwritten; as Cytoscape caches images, use a new folder (or
unique_dirofload_node_images()) when changing their look.size (int) – Width and height in pixels (default 40); the size shown in Cytoscape is set by
show_node_images().color (str) – Badge and number colours (default dark grey and white).
text_color (str) – Badge and number colours (default dark grey and white).
- Returns:
Value -> path of its badge.
- Return type:
- skm_tools.node_images.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, withskm_tools.cytoscape_utils.show_node_images(). Needs the enhancedGraphics app in Cytoscape.- Parameters:
df (pandas.DataFrame) – One row per node.
colors (str or list of str) – Hex colour per column, or one for all.
chart (str) – enhancedGraphics chart type, e.g.
"barchart"(default),"linechart","heatstripchart","piechart".value_range (tuple of float, optional) – (min, max) of the value axis (default: per chart, from its values).
labels (list of str, optional) – Label per column (default: the column names). enhancedGraphics splits lists on commas and can’t escape them, so labels can’t contain
,or".na_value (float) – Value for missing data (default 0).
**options – Further enhancedGraphics options, e.g.
separation=2,ybase=1.
- Returns:
Chart string per row of df.
- Return type:
- Raises:
ValueError – If a label contains
,or".
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"'