Skip to main content

High-level description

The local.py file in the cassiopeia/plotting module provides functions for plotting phylogenetic trees locally using either Matplotlib (for static plots) or Plotly (for dynamic plots). These functions offer an alternative to using iTOL, a cloud-based tree plotting service, for visualizing CassiopeiaTree objects.

Code Structure

The code defines several helper functions for placing tree elements and annotations on a coordinate system, creating colorstrips for categorical and continuous variables, and generating an indel heatmap. These helper functions are then used by the main plotting functions, plot_matplotlib and plot_plotly, to generate the final tree visualizations.

References

This file references symbols from the cassiopeia.data module, specifically the CassiopeiaTree class, and the cassiopeia.plotting.utilities module for tree placement and coordinate transformations.

Symbols

compute_colorstrip_size

Description

This function calculates the width and height of colorstrip boxes based on the node coordinates, anchor coordinates, and the desired location of the colorstrip relative to the anchor. The height is determined to ensure tight arrangement of boxes, while the width is set to 5% of the tree depth.

Inputs

Outputs

Internal Logic

The function iterates through the node coordinates to determine the minimum and maximum positions and depths based on the specified location. The width is calculated as 5% of the difference between the maximum and minimum depths. The height is calculated by dividing the difference between the maximum and minimum positions by the number of anchor coordinates.

create_categorical_colorstrip

Description

This function creates a colorstrip for a categorical variable, assigning a unique color from a specified colormap to each unique value.

Inputs

Outputs

Internal Logic

The function first determines the unique categorical values and assigns integer indices to them, either using the provided value_mapping or generating a pseudo-random mapping. It then uses the utilities.place_colorstrip function to calculate the box coordinates and next anchor coordinates. Finally, it iterates through the input values, assigns a color from the colormap based on the integer index, and populates the colorstrip dictionary with the box coordinates, color, and text label for each node.

create_continuous_colorstrip

Description

This function creates a colorstrip for a continuous variable, assigning colors from a specified colormap based on the normalized values within a specified range.

Inputs

Outputs

Internal Logic

The function first determines the minimum and maximum values for the color scale, using the provided vmin and vmax or the minimum and maximum values from the input values. It then uses the utilities.place_colorstrip function to calculate the box coordinates and next anchor coordinates. Finally, it iterates through the input values, normalizes them to the range [0, 1], assigns a color from the colormap based on the normalized value, and populates the colorstrip dictionary with the box coordinates, color, and text label for each node.

create_indel_heatmap

Description

This function creates an indel heatmap, visualizing the indels present in an allele table using a color mapping.

Inputs

Outputs

Internal Logic

The function first prepares the allele table and indel colors using the utilities.prepare_alleletable function. It then iterates through each site in the allele table, calculates the box coordinates and next anchor coordinates using the utilities.place_colorstrip function, and assigns a color to each indel based on the provided indel_colors or a randomly generated color mapping. The resulting colorstrip for each site is appended to the heatmap list.

create_clade_colors

Description

This function assigns colors to nodes and branches based on clade membership, using a provided dictionary mapping clade root nodes to colors.

Inputs

Outputs

Internal Logic

The function first identifies all descendants of each clade root node. It then iterates through the clade root nodes in descending order of their descendant set size, assigning the corresponding color to all nodes and branches within each clade.

place_tree_and_annotations

Description

This helper function places the tree and all requested annotations on a coordinate system, preparing the data for plotting.

Inputs

Outputs

Internal Logic

The function first places the tree on the coordinate system using the utilities.place_tree function. It then calculates the initial anchor coordinates for the colorstrips, which are the coordinates of the leaf nodes. Next, it creates the indel heatmap, if an allele table is provided, and iterates through the metadata columns, creating colorstrips for each column based on its data type. Finally, it assigns colors to nodes and branches based on clade membership, if clade_colors is provided.

plot_matplotlib

Description

This function generates a static plot of a CassiopeiaTree using Matplotlib.

Inputs