Viz

viz.py

wgcnaFigures.py

get_module_color_annotation(map_list, col_annotation=False, row_annotation=False, bygene=False, module_colors=[], dendrogram=[])[source]

This function takes a list of values, converts them into colors, and creates a new plotly object to be used as an annotation. Options module_colors and dendrogram only apply when map_list is a list of experimental features used in module eigenegenes calculation.

Parameters
  • map_list (list) – dendrogram leaf labels.

  • col_annotation (bool) – if True, adds color annotations as a row.

  • row_annotation (bool) – if True, adds color annotations as a column.

  • bygene (bool) – determines wether annotation colors have to be reordered to match dendrogram leaf labels.

  • module_colors (list) – dendrogram leaf module color.

  • dendrogram (dict) – dendrogram represented as a plotly object figure.

Returns

Plotly object figure.

Note

map_list and module_colors must have the same length.

get_heatmap(df, colorscale=None, color_missing=True)[source]

This function plots a simple Plotly heatmap.

Parameters
  • df – pandas dataframe containing experimental data, with samples/subjects as rows and features as columns.

  • colorscale (list[list]) – heatmap colorscale (e.g. [[0,’#67a9cf’],[0.5,’#f7f7f7’],[1,’#ef8a62’]]). If colorscale is not defined, will take [[0, ‘rgb(255,255,255)’], [1, ‘rgb(255,51,0)’]] as default.

  • color_missing (bool) – if set to True, plots missing values as grey in the heatmap.

Returns

Plotly object figure.

plot_labeled_heatmap(df, textmatrix, title, colorscale=[[0, 'rgb(0,255,0)'], [0.5, 'rgb(255,255,255)'], [1, 'rgb(255,0,0)']], width=1200, height=800, row_annotation=False, col_annotation=False)[source]

This function plots a simple Plotly heatmap with column and/or row annotations and heatmap annotations.

Parameters
  • df – pandas dataframe containing data to be plotted in the heatmap.

  • textmatrix – pandas dataframe with heatmap annotations as values.

  • title (str) – the title of the figure.

  • colorscale (list[list]) – heatmap colorscale (e.g. [[0,’rgb(0,255,0)’],[0.5,’rgb(255,255,255)’],[1,’rgb(255,0,0)’]])

  • width (int) – the width of the figure.

  • height (int) – the height of the figure.

  • row_annotation (bool) – if True, adds a color-coded column at the left of the heatmap.

  • col_annotation (bool) – if True, adds a color-coded row at the bottom of the heatmap.

Returns

Plotly object figure.

plot_dendrogram_guidelines(Z_tree, dendrogram)[source]

This function takes a dendrogram tree dictionary and its plotly object and creates shapes to be plotted as vertical dashed lines in the dendrogram.

Parameters
  • Z_tree (dict) – dictionary of data structures computed to render the dendrogram. Keys: ‘icoords’, ‘dcoords’, ‘ivl’ and ‘leaves’.

  • dendrogram – dendrogram represented as a plotly object figure.

Returns

List of dictionaries.

plot_intramodular_correlation(MM, FS, feature_module_df, title, width=1000, height=800)[source]

This function uses the Feature significance and Module Membership measures, and plots a multi-scatter plot of all modules against all clinical traits.

Parameters
  • MM – pandas dataframe with module membership data

  • FS – pandas dataframe with feature significance data

  • feature_module_df – pandas DataFrame of experimental features and module colors (use mode=’dataframe’ in get_FeaturesPerModule)

  • title (str) – plot title

  • width (int) – plot width

  • height (int) – plot height

Returns

Plotly object figure.

Example:

plot = plot_intramodular_correlation(MM, FS, feature_module_df, title='Plot', width=1000, height=800):

Note

There is a limit in the number of subplots one can make in Plotly. This function limits the number of modules shown to 5.

plot_complex_dendrogram(dendro_df, subplot_df, title, dendro_labels=[], distfun='euclidean', linkagefun='average', hang=0.04, subplot='module colors', subplot_colorscale=[], color_missingvals=True, row_annotation=False, col_annotation=False, width=1000, height=800)[source]

This function plots a dendrogram with a subplot below that can be a heatmap (annotated or not) or module colors.

Parameters
  • dendro_df – pandas dataframe containing data used to generate dendrogram, columns will result in dendrogram leaves.

  • subplot_df – pandas dataframe containing data used to generate plot below dendrogram.

  • title (str) – the title of the figure.

  • dendro_labels (list) – list of strings for dendrogram leaf nodes labels.

  • distfun (str) – distance measure to be used (‘euclidean‘, ‘maximum‘, ‘manhattan‘, ‘canberra‘, ‘binary‘, ‘minkowski‘ or ‘jaccard‘).

  • linkagefun (str) – hierarchical/agglomeration method to be used (‘single‘, ‘complete‘, ‘average‘, ‘weighted‘, ‘centroid‘, ‘median‘ or ‘ward‘).

  • hang (float) – height at which the dendrogram leaves should be placed.

  • subplot (str) – type of plot to be shown below the dendrogram (´module colors´ or ´heatmap´).

  • subplot_colorscale (list) – colorscale to be used in the subplot.

  • color_missingvals (bool) – if set to True, plots missing values as grey in the heatmap.

  • row_annotation (bool) – if True, adds a color-coded column at the left of the heatmap.

  • col_annotation (bool) – if True, adds a color-coded row at the bottom of the heatmap.

  • width (int) – the width of the figure.

  • height (int) – the height of the figure.

Returns

Plotly object figure.

Dendrogram.py

plot_dendrogram(Z_dendrogram, cutoff_line=True, value=15, orientation='bottom', hang=30, hide_labels=False, labels=None, colorscale=None, hovertext=None, color_threshold=None)[source]

Modified version of Plotly _dendrogram.py that returns a dendrogram Plotly figure object with cutoff line.

Parameters
  • Z_dendrogram (ndarray) – Matrix of observations as array of arrays

  • cutoff_line (boolean) – plot distance cutoff line

  • value (float or int) – dendrogram distance for cutoff line

  • orientation (str) – ‘top’, ‘right’, ‘bottom’, or ‘left’

  • hang (float) – dendrogram distance of leaf lines

  • hide_labels (boolean) – show leaf labels

  • labels (list) – List of axis category labels(observation labels)

  • colorscale (list) – Optional colorscale for dendrogram tree

  • hovertext (list[list]) – List of hovertext for constituent traces of dendrogram clusters

  • color_threshold (double) – Value at which the separation of clusters will be made

Returns

Plotly figure object

Example:

figure = plot_dendrogram(dendro_tree, hang=0.9, cutoff_line=False)
class Dendrogram(Z_dendrogram, orientation='bottom', hang=1, hide_labels=False, labels=None, colorscale=None, hovertext=None, color_threshold=None, width=inf, height=inf, xaxis='xaxis', yaxis='yaxis')[source]

Bases: object

Refer to plot_dendrogram() for docstring.

get_color_dict(colorscale)[source]

Returns colorscale used for dendrogram tree clusters.

Parameters

colorscale (list) – colors to use for the plot in rgb format

Return (dict)

default colors mapped to the user colorscale

set_axis_layout(axis_key, hide_labels)[source]

Sets and returns default axis object for dendrogram figure.

Parameters

axis_key (str) – E.g., ‘xaxis’, ‘xaxis1’, ‘yaxis’, yaxis1’, etc.

Return (dict)

An axis_key dictionary with set parameters.

set_figure_layout(width, height, hide_labels)[source]

Sets and returns default layout object for dendrogram figure.

Parameters
  • width (int) – plot width

  • height (int) – plot height

  • hide_labels (boolean) – show leaf labels

Returns

Plotly layout

get_dendrogram_traces(Z_dendrogram, hang, colorscale, hovertext, color_threshold)[source]

Calculates all the elements needed for plotting a dendrogram.

Parameters
  • Z_dendrogram (ndarray) – Matrix of observations as array of arrays

  • hang (float) – dendrogram distance of leaf lines

  • colorscale (list) – Color scale for dendrogram tree clusters

  • hovertext (list) – List of hovertext for constituent traces of dendrogram

Return (tuple)

Contains all the traces in the following order:

  1. trace_list: List of Plotly trace objects for dendrogram tree

  2. icoord: All X points of the dendrogram tree as array of arrays with length 4

  3. dcoord: All Y points of the dendrogram tree as array of arrays with length 4

  4. ordered_labels: leaf labels in the order they are going to appear on the plot

  5. Z_dendrogram[‘leaves’]: left-to-right traversal of the leaves

color_list.py

Code for handling color names and RGB codes.

This module is part of Swampy, and used in Think Python and Think Complexity, by Allen Downey.

http://greenteapress.com

Copyright 2013 Allen B. Downey. Distributed under the GNU General Public License at gnu.org/licenses/gpl.html.

make_color_dict(colors='\n141 211 199\t\tturquoise\n31 120 180\t\tblue\n139 69 19\t\tsaddlebrown\n177 89 40\t\tbrown\n51 160 44\t\tgreen\n255 237 111\t\tyellow\n173 255 47\t\tgreenyellow\n255 0 0\t\tred\n255 255 255\t\twhite\n0 0 0\t\tblack\n255 192 203\t\tpink\n255 0 255\t\tmagenta\n160 32 240\t\tpurple\n210 180 140\t\ttan\n250 128 114\t\tsalmon\n166 206 227\t\tcyan\n25 25 112\t\tmidnightblue\n224 255 255\t\tlightcyan\n153 153 153 \t\tgrey60\n144 238 144\t\tlightgreen\n255 255 224\t\tlightyellow\n65 105 225\t\troyalblue\n139 0 0\t\tdarkred\n0 100 0\t\tdarkgreen\n0 206 209\t\tdarkturquoise\n169 169 169\t\tdarkgrey\n255 165 0\t\torange\n255 140 0\t\tdarkorange\n135 206 235\t\tskyblue\n70 130 180\t\tsteelblue\n175 238 238\t\tpaleturquoise\n238 130 238\t\tviolet\n85 107 47\t\tdarkolivegreen\n139 0 139\t\tdarkmagenta\n190 190 190\t\tgray\n190 190 190\t\tgrey\n')[source]

Returns a dictionary that maps color names to RGB strings.

The format of RGB strings is ‘#RRGGBB’.

read_colors()[source]

Returns color information in two data structures.

The format of RGB strings is ‘#RRGGBB’.

color_dict: map from color name to RGB string rgbs: list of (rgb, names) pairs, where rgb is an RGB code and names is a sorted list of color names

invert_dict(d)[source]

Returns a dictionary that maps from values to lists of keys.

d: dict

returns: dict