Plots

class NetExplorer.models.plots.BarPlot

Class for Plotly bar plots where each bar (group) consists of only one value. Inherits from GenExpPlot.

plot()

Converts plot to a JSON string ready to be drawn by Plotly.js.

Returns

JSON string with plot data for Plotly.js.

Return type

str

class NetExplorer.models.plots.GeneExpPlot

Parent class for Plotly barplots and violins.

traces

List of plotly traces that define the data to be plotted.

Type

list of PlotlyTrace

title

Title for plot.

Type

str

xlab

Label for plot x-axis.

Type

str

ylab

Label for plot y-axis.

Type

str

trace_names

Name for traces, if any.

Type

list of str

is_empty()

Checks if plot is empty by looking at its traces.

Returns

True if empty, False otherwise.

Return type

bool

plot()

Method to be provided by child classes to convert plot to JSON string for plotting.

class NetExplorer.models.plots.HeatmapPlot

Class for plotly Heatmaps. They are not GeneExpPlot because they function very differently.

x

x-values for data (condition names).

Type

list of str

y

y-values for data (gene names).

Type

list of str

z

z-values for data (expression that will be mapped to color). Each element of z corresponds to a list with expression values for one gene. z is a 2-D matrix where rows are genes (same order as y) and columns are conditions (same order as x).

Type

list of list

type

Defines type for Plotly (“heatmap”).

Type

str

Warning

add_gene and add_gene_expression should be used syncronously. Each time a gene is added, all gene_expressions will be added for THAT gene. Those should be added in the same order as the provided conditions in the add_condition call previously. See the example for more information.

Example

# Data to be added
genes = ["A", "B"]
expressions = [
    [2.5, 3.5, 4.5], # for A
    [1, 3, 1] # for B
]
conditions = ["c1", "c2", "c3"]

# Initialize the plot
the_plot = HeatMapPlot()
theplot.add_conditions(conditions)

# Add genes and expression
for gene, expression in zip(genes, expressions):
    theplot.add_gene(gene)
    theplot.add_gene_expression(expression)
add_conditions(conditions)

Adds conditions to x-axis.

Parameters

conditions (list of Condition or str) – Conditions to be added.

add_gene(gene_symbol)

Adds gene symbol to y-axis.

Parameters

gene_symbol (str) – Gene symbol to be added to y-axis.

add_gene_expression(expression)

Adds gene expression to z-axis.

Parameters

expression (float) – Expression value to be added to the LAST gene.

is_empty()

Checks if plot is empty by looking at its traces.

Returns

True if empty, False otherwise.

Return type

bool

plot()

Converts plot to a JSON string ready to be drawn by Plotly.js.

Returns

JSON string with plot data for Plotly.js.

Return type

str

class NetExplorer.models.plots.LinePlot

Class for Plotly linecharts. Inherits from GenExpPlot.

type

Type for Plotlyjs (scattergl).

Type

str

plot()

Converts plot to a JSON string ready to be drawn by Plotly.js.

Returns

JSON string with plot data for Plotly.js.

Return type

str

class NetExplorer.models.plots.PlotCreator

Class that handles the creation of plots for PlanExp.

_PlotCreator__create_bar(**kwargs)

Creates a bar plot.

Parameters

**kwargs – Arbitrary keyword arguments passed from create_plot.

Returns

Bar plot instance.

Return type

BarPlot

_PlotCreator__create_coexpression(**kwargs)

Creates a co-expression plot, a plot where x and y axis are expression values for two genes, and each point is a cell.

Parameters

**kwargs – Arbitrary keyword arguments passed from create_plot.

Returns

ScatterPlot plot instance with co-expression plot.

Return type

ScatterPlot

_PlotCreator__create_heatmap(**kwargs)

Creates a heatmap plot.

Parameters

**kwargs – Arbitrary keyword arguments passed from create_plot.

Returns

Heatmap plot instance.

Return type

HeatmapPlot

_PlotCreator__create_linechart(**kwargs)

Creates a linechart plot.

Parameters

**kwargs – Arbitrary keyword arguments passed from create_plot.

Returns

Line chart plot instance.

Return type

LinePlot

_PlotCreator__create_tsne(**kwargs)

Creates either a simple t-SNE (zero or one gene) or a multiple t-SNE (>1 gene).

Parameters

**kwargs – Arbitrary keyword arguments passed from create_plot.

Returns

Scatter plot t-SNE visualization.

Return type

ScatterPlot

_PlotCreator__create_tsne_multiple(cell_positions, sample_names, sample_condition, sample_mean_expression)

Creates a t-SNE plot withmore than one gene expression mapped onto it.

Parameters
  • genes (list of str) – List of gene symbols.

  • cell_positions (dict) – Dictionary with cell positions in t-SNE space. Key is cell/sample name, value is tuple with (x, y) positions (float, float).

  • sample_names (list of str) – List of sample names.

  • sample_condition (dict) – Dictionary with sample-condition relationships. Key is cell/sample name, value is condition identifier (int).

  • sample_mean_expression (dict) – Dictionary with mean expession for condition. Key is sample name (str), value is mean expression (float) for each gene in genes in that cell.

Returns

ScatterPlot plot instance.

Return type

ScatterPlot

_PlotCreator__create_tsne_simple(genes, cell_positions, sample_names, sample_condition, sample_expression)

Creates a t-SNE plot with one (or none) gene expression mapped onto it.

Parameters
  • genes (list of str) – List of gene symbols.

  • cell_positions (dict) – Dictionary with cell positions in t-SNE space. Key is cell/sample name, value is tuple with (x, y) positions (float, float).

  • sample_names (list of str) – List of sample names.

  • sample_condition (dict) – Dictionary with sample-condition relationships. Key is cell/sample name, value is condition identifier (int).

  • sample_expression (dict) – Key is gene symbol (str), value is vector of expression in each sample (list of float), sorted by condition.

Warning

sample_expression values have to be in the same order as sample_names! We sort them by sample identifier using order(‘sample’) in Django models.

Returns

ScatterPlot plot instance.

Return type

ScatterPlot

_PlotCreator__create_violin(**kwargs)

Creates a violin plot.

Parameters

**kwargs – Arbitrary keyword arguments passed from create_plot.

Returns

Violin plot instance.

Return type

ViolinPlot

create_plot(plot_name, **kwargs)

Method for creating plots.

Parameters
  • plot_name (str) – Name of the plot. Can be (violin, tsne, coexpression, bar, heatmap or line)

  • experiment (str) – Experiment in PlanExp.

  • dataset (str) – Dataset in PlanExp.

  • conditions (list of Condition) – Conditions (previously sorted) to use for the plot.

  • genes (list of str) – Genes to plot. Can be empty depending on the plot.

  • ctype (str) – Condition type of conditions.

  • only_toggle (bool, optional) – Plot only samples with expression > 0. Used only for plot violin.

Returns

Can be of subclass (ViolinPlot, ScatterPlot,

BarPlot, HeatmapPlot, or LinePlot).

Return type

plot (GeneExpPlot)

class NetExplorer.models.plots.PlotlyTrace(name)

Class for plotly traces.

name
Type

str

class NetExplorer.models.plots.ScatterPlot

Class for scatter plots. Inherits from GenExpPlot.

type

Type for Plotlyjs (scattergl).

Type

str

cmax

Maximum value to compute color gradient. Defaults to None.

Type

float

cmin

Minimum value to compute color gradient. Defaults to 0.

Type

int

compute_color_limits()

Computes color limits according to stored trace values and fills cmax attribute.

Raises

EmptyPlotError – raised when no values are found in traces.

plot()

Converts plot to a JSON string ready to be drawn by Plotly.js.

Returns

JSON string with plot data for Plotly.js.

Return type

str

class NetExplorer.models.plots.ViolinPlot

Class for Plotly violinplots. Each violin (group) is made of multiple values. Inherits from GenExpPlot.

jitter_and_round(values, jitter=0.01)

Jitters the y-values of the Violin plot so that Plotly can plot them because Plotly.js is buggy as hell, and if the standard deviation is equal to zero it will REFUSE to plot the violin.

Parameters
  • values (list of float) – Values to be jittered.

  • jitter (float) – How much to jitter the y-values.

plot()

Converts plot to a JSON string ready to be drawn by Plotly.js.

Returns

JSON string with plot data for Plotly.js.

Return type

str

class NetExplorer.models.plots.VolcanoPlot

Class for Plotly volcano scatterplots. Legacy code that should be converted to ScatterPlot at some point.

var data = [

{

z: [[1, 20, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, -10, 20]], x: [‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’], y: [‘Morning’, ‘Afternoon’, ‘Evening’], type: ‘heatmap’

} ];

Plotly.newPlot(‘myDiv’, data);

add_color_to_trace(trace_name, colors)
add_name(trace_name, name)
add_trace(name)
add_units(axis, units)

Adds units to one axis of the plot.

Parameters
  • axis – string cointaining ‘x’ or ‘y’.

  • units – string for units.

Returns

nothing

add_x(trace_name, x)
add_y(trace_name, y)
plot()
set_limits(axis, start, end)