Here’s a detailed documentation for the Layers class in the cassiopeia/data/Layers.py file:

High-level description

The Layers class implements a data structure for storing multiple versions of mutation data for each cell in a CassiopeiaTree object. It behaves like a dictionary, allowing users to store, retrieve, and delete entries using standard dictionary operations.

Code Structure

The Layers class is a subclass of dict and is closely tied to the CassiopeiaTree class. It maintains a reference to its parent CassiopeiaTree object and stores layer data in an internal dictionary.

Symbols

Layers

Description

A class that represents a collection of character matrices, each storing a version of mutation data for cells in a CassiopeiaTree.

Inputs

NameTypeDescription
parentCassiopeiaTreeThe parent CassiopeiaTree object
layersOptional[Mapping]Initial layers to populate the object

Internal Logic

  • Initializes with a reference to the parent CassiopeiaTree and an empty internal dictionary.
  • If initial layers are provided, they are added to the internal dictionary.
  • Implements dictionary-like methods for accessing, setting, and deleting layers.
  • Provides custom string representation and iPython key completion.

repr

Description

Returns a string representation of the Layers object.

Outputs

NameTypeDescription
returnstrString representation of the Layers object

ipython_key_completions

Description

Returns a list of keys for iPython autocompletion.

Outputs

NameTypeDescription
returnList[str]List of layer keys

copy

Description

Creates a shallow copy of the Layers object.

Outputs

NameTypeDescription
returnLayersA new Layers object with copied data

getitem

Description

Retrieves a layer by key.

Inputs

NameTypeDescription
keystrThe key of the layer to retrieve

Outputs

NameTypeDescription
returnpd.DataFrameThe requested layer data

setitem

Description

Sets a layer with the given key and value.

Inputs

NameTypeDescription
keystrThe key for the layer
valuepd.DataFrameThe layer data to set

Internal Logic

  • Validates the input value using the _validate_value method.
  • Stores the validated value in the internal dictionary.

delitem

Description

Deletes a layer with the given key.

Inputs

NameTypeDescription
keystrThe key of the layer to delete

contains

Description

Checks if a layer with the given key exists.

Inputs

NameTypeDescription
keystrThe key to check for

Outputs

NameTypeDescription
returnboolTrue if the key exists, False otherwise

iter

Description

Returns an iterator over the layer keys.

Outputs

NameTypeDescription
returnIterator[str]Iterator over layer keys

len

Description

Returns the number of layers.

Outputs

NameTypeDescription
returnintNumber of layers

_validate_value

Description

Validates the input value for a layer.

Inputs

NameTypeDescription
valpd.DataFrameThe value to validate
keystrThe key associated with the value

Outputs

NameTypeDescription
returnpd.DataFrameThe validated value

Internal Logic

  • Checks if the input value has the correct number of samples (rows) compared to the parent CassiopeiaTree.
  • Raises a ValueError if the shape is incorrect.

Dependencies

DependencyPurpose
typingType hinting
pandasDataFrame operations
cassiopeia.data.CassiopeiaTreeParent class reference

This Layers class provides a flexible way to store and manage multiple versions of mutation data for cells in a CassiopeiaTree object, allowing for easy experimentation with different data representations or imputations.