Skip to content

rename

rename

rename(
    data,
    rename,
    fromColumn="phenotype",
    toColumn="phenotype_renamed",
    verbose=True,
    outputDir=None,
    sdataTable=None,
)

Relabel or merge the categories of an obs column.

Writes a new column rather than editing in place, so the original labels stay available. That matters when you want to plot broad classes and fine phenotypes side by side.

Parameters:

Name Type Description Default
data (AnnData | SpatialData | str, required)

The cell table. An AnnData, a path to an .h5ad file, a SpatialData object, or a path to a .zarr SpatialData store.

required
rename (dict, required)

The mapping, keyed by the new label: {"Tumour": ["ECAD+", "KI67+"], "Stroma": "SMA+"}. Values may be a single label or a list of them. Categories that are not mentioned keep their existing label.

required
fromColumn str

Column in obs to read.

'phenotype'
toColumn str

Column in obs to write. Set this — the default 'phenotype_renamed' is rarely what you want to plot from.

'phenotype_renamed'
verbose bool

Print progress messages.

True
outputDir str

Directory to write the updated object to. With None the object is returned instead.

None
sdataTable str

Which SpatialData table to work on. Ignored for AnnData input, and optional when the store has exactly one table.

None

Returns:

Name Type Description
adata AnnData | SpatialData | None

The updated object with obs[toColumn] set. None when outputDir is given.

Example
# Merge fine phenotypes into broad classes.
adata = sp.tl.rename(
    adata,
    rename={"Tumour": "ECAD+",
            "Immune": ["Treg", "NK cells", "Dendritic cells"]},
    fromColumn="phenotype",
    toColumn="broad",
)

# Give cluster numbers names.
adata = sp.tl.rename(
    adata,
    rename={"Tumour core": "0", "Stroma": "1", "Immune infiltrate": "2"},
    fromColumn="kmeans",
    toColumn="neighbourhood",
)