High-level description

This file contains utility functions for plotting phylogenetic trees locally using libraries like Matplotlib and Plotly. It provides functions for placing tree nodes and branches on a coordinate system, generating color strips for annotations, and creating indel heatmaps.

Code Structure

The code consists of several independent utility functions that can be used for plotting phylogenetic trees. These functions include: degrees_to_radians, polar_to_cartesian, polars_to_cartesians, place_tree, place_colorstrip, generate_random_color, get_random_indel_colors, get_indel_colors, hex_to_rgb, rgb_to_hex, and prepare_alleletable.

References

This file references the CassiopeiaTree class from cassiopeia.data and the convert_alleletable_to_lineage_profile function from cassiopeia.preprocess.utilities.

Symbols

degrees_to_radians

Description

Converts an angle from degrees to radians.

Inputs

NameTypeDescription
degreesfloatAngle in degrees.

Outputs

NameTypeDescription
floatfloatAngle in radians.

polar_to_cartesian

Description

Converts polar coordinates (angle and radius) to Cartesian coordinates (x and y).

Inputs

NameTypeDescription
degreesfloatAngle in degrees.
rfloatRadius.

Outputs

NameTypeDescription
Tuple[float, float]Tuple[float, float]A tuple containing the x and y coordinates.

polars_to_cartesians

Description

Converts a list of polar coordinates to a list of Cartesian coordinates.

Inputs

NameTypeDescription
degreesList[float]List of angles in degrees.
rsList[float]List of radii.

Outputs

NameTypeDescription
Tuple[List[float], List[float]]Tuple[List[float], List[float]]Two lists, containing the x and y coordinates respectively.

place_tree

Description

Computes the coordinates of nodes and branches in a CassiopeiaTree for visualization. It supports both rectangular and polar coordinate systems and offers options for customizing the tree layout.

Inputs

NameTypeDescription
treeCassiopeiaTreeThe tree to place on the coordinate grid.
depth_keyOptional[str]The node attribute to use as the depth of the nodes. If not provided, the distances from the root are used.
orientUnion[Literal[“down”, “up”, “left”, “right”], float]The orientation of the tree. Can be “down”, “up”, “left”, “right” for rectangular plots, or a number for polar plots.
depth_scalefloatScale the depth of the tree by this amount.
width_scalefloatScale the width of the tree by this amount.
extend_branchesboolExtend branch lengths to equalize distances from the root to all nodes.
angled_branchesboolDisplay branches as angled lines instead of straight lines.
polar_interpolation_thresholdfloatThreshold for adding interpolation points in polar plots to smooth out large angles.
polar_interpolation_stepfloatInterpolation step for polar plots.
add_rootboolAdd a synthetic root node to the tree.

Outputs

NameTypeDescription
Tuple[Dict[str, Tuple[float, float]], Dict[Tuple[str, str], Tuple[List[float], List[float]]]]Tuple[Dict[str, Tuple[float, float]], Dict[Tuple[str, str], Tuple[List[float], List[float]]]]Two dictionaries: the first contains node coordinates, and the second contains branch coordinates.

Internal Logic

The function first places the leaves of the tree, then iteratively places internal nodes based on the positions of their children. It supports both rectangular and polar coordinate systems and offers options for customizing the tree layout, such as extending branches, angling branches, and adding a synthetic root node.

place_colorstrip

Description

Computes the coordinates of boxes for a color strip annotation, which can be used to visualize categorical or continuous data alongside the tree.

Inputs

NameTypeDescription
anchor_coordsDict[str, Tuple[float, float]]Dictionary of anchor coordinates for the color strip boxes.
widthfloatWidth of the box.
heightfloatHeight of the box.
spacingfloatSpace between consecutive color strips.
locLiteral[“left”, “right”, “up”, “down”, “polar”]Location of the boxes relative to the anchors.

Outputs

NameTypeDescription
Tuple[Dict[str, Tuple[List[float], List[float]]], Dict[str, Tuple[float, float]]]Tuple[Dict[str, Tuple[List[float], List[float]]], Dict[str, Tuple[float, float]]]A dictionary of box coordinates and a dictionary of new anchor coordinates.

generate_random_color

Description

Generates a random RGB color within specified ranges for each color channel.

Inputs

NameTypeDescription
r_rangeTuple[float, float]Range of values for the red channel.
g_rangeTuple[float, float]Range of values for the green channel.
b_rangeTuple[float, float]Range of values for the blue channel.
random_stateOptional[np.random.RandomState]Random state for reproducibility.

Outputs

NameTypeDescription
Tuple[int, int, int]Tuple[int, int, int]A tuple containing the randomly generated RGB color values.

get_random_indel_colors

Description

Assigns random HSV colors to each unique indel in a lineage profile.

Inputs

NameTypeDescription
lineage_profilepd.DataFrameLineage profile containing indel observations.
random_stateOptional[np.random.RandomState]Random state for reproducibility.

Outputs

NameTypeDescription
pd.DataFramepd.DataFrameA DataFrame mapping indels to HSV colors.

get_indel_colors

Description

Maps indels to HSV colors based on their prior probabilities, with rarer indels receiving brighter colors.

Inputs

NameTypeDescription
indel_priorspd.DataFrameDataFrame mapping indels to probabilities.
random_stateOptional[np.random.RandomState]Random state for reproducibility.

Outputs

NameTypeDescription
pd.DataFramepd.DataFrameA DataFrame mapping indels to HSV colors.

hex_to_rgb

Description

Converts a hexadecimal color code to an RGB tuple.

Inputs

NameTypeDescription
valuestrHexadecimal color code (e.g., “#FF0000”).

Outputs

NameTypeDescription
Tuple[int, int, int]Tuple[int, int, int]A tuple containing the RGB color values.

rgb_to_hex

Description

Converts an RGB tuple to a hexadecimal color code.

Inputs

NameTypeDescription
rgbTuple[int, int, int]A tuple containing the RGB color values.

Outputs

NameTypeDescription
strstrHexadecimal color code (e.g., “#FF0000”).

prepare_alleletable

Description

Prepares an allele table for plotting by generating indel colors and creating a lineage profile.

Inputs

NameTypeDescription
allele_tablepd.DataFrameAllele table containing indel observations.
leavesList[str]List of leaf node names in the tree.
indel_priorsOptional[pd.DataFrame]DataFrame mapping indels to probabilities.
random_stateOptional[np.random.RandomState]Random state for reproducibility.

Outputs

NameTypeDescription
Tuple[pd.DataFrame, pd.DataFrame]Tuple[pd.DataFrame, pd.DataFrame]A tuple containing the lineage profile and indel colors as DataFrames.

Internal Logic

The function first converts the allele table to a lineage profile using the convert_alleletable_to_lineage_profile function. Then, it generates indel colors either randomly or based on prior probabilities, depending on whether indel_priors is provided.