Skip to content

rename

Short Description

sm.hl.rename: This function offers a straightforward way to rename specific categories within a chosen column of an AnnData object, with the new names being stored in a separate column. It streamlines the process of updating or consolidating category labels for enhanced data clarity and analysis.

Function

rename(adata, rename, from_column='phenotype', to_column='phenotype_renamed', verbose=True)

Parameters:

Name Type Description Default
adata AnnData

Annotated data matrix or path to an AnnData object, containing spatial gene expression data.

required
rename dict

A dictionary mapping existing category names (values) to new category names (keys). Each key corresponds to the new name, and its value is a list of existing names to be consolidated under this new name.

required
from_column str

The name of the column in adata.obs where the categories to be renamed are located. Defaults to 'phenotype'.

'phenotype'
to_column str

The name of the new column in adata.obs where the renamed categories will be stored. Defaults to 'phenotype_renamed'.

'phenotype_renamed'
verbose bool

If True, prints messages about the renaming process.

True

Returns:

Name Type Description
adata AnnData

The AnnData object after applying the renaming operation, with the newly named categories stored in the specified adata.obs[to_column].

Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Example 1: Simplify phenotype labels
rename_dict = {'tumor': ['cd45 neg tumor', 'cd8 tumor', 'cd4 tumor'],
               'macrophages': ['m1 macrophages', 'm2 macrophages']}
adata = sm.hl.rename(adata, rename=rename_dict, from_column='phenotype', to_column='simplified_phenotype')

# Example 2: Merge similar phenotypes under a common name
merge_dict = {'immune cells': ['cd45+', 't-cells', 'b-cells']}
adata = sm.hl.rename(adata, rename=merge_dict, from_column='cell_type', to_column='merged_cell_type')

# Example 3: Rename and create a new column for easier identification
new_names = {'activated': ['activated_tcells', 'activated_bcells'],
             'resting': ['resting_tcells', 'resting_bcells']}
adata = sm.hl.rename(adata, rename=new_names, from_column='status', to_column='status_simplified')
Source code in scimap/helpers/rename.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def rename (adata, 
            rename, 
            from_column='phenotype', 
            to_column='phenotype_renamed',
            verbose=True):
    """
Parameters:
        adata (anndata.AnnData):  
            Annotated data matrix or path to an AnnData object, containing spatial gene expression data.

        rename (dict):  
            A dictionary mapping existing category names (values) to new category names (keys). 
            Each key corresponds to the new name, and its value is a list of existing names to be consolidated under this new name.

        from_column (str, optional):  
            The name of the column in `adata.obs` where the categories to be renamed are located. Defaults to 'phenotype'.

        to_column (str, optional):  
            The name of the new column in `adata.obs` where the renamed categories will be stored. Defaults to 'phenotype_renamed'.

        verbose (bool, optional):  
            If True, prints messages about the renaming process. 

Returns:
        adata (anndata.AnnData):  
            The AnnData object after applying the renaming operation, with the newly named categories stored in the specified `adata.obs[to_column]`.

Example:
    ```python

    # Example 1: Simplify phenotype labels
    rename_dict = {'tumor': ['cd45 neg tumor', 'cd8 tumor', 'cd4 tumor'],
                   'macrophages': ['m1 macrophages', 'm2 macrophages']}
    adata = sm.hl.rename(adata, rename=rename_dict, from_column='phenotype', to_column='simplified_phenotype')

    # Example 2: Merge similar phenotypes under a common name
    merge_dict = {'immune cells': ['cd45+', 't-cells', 'b-cells']}
    adata = sm.hl.rename(adata, rename=merge_dict, from_column='cell_type', to_column='merged_cell_type')

    # Example 3: Rename and create a new column for easier identification
    new_names = {'activated': ['activated_tcells', 'activated_bcells'],
                 'resting': ['resting_tcells', 'resting_bcells']}
    adata = sm.hl.rename(adata, rename=new_names, from_column='status', to_column='status_simplified')

    ```
    """

    # Sanity check: if the values are not list convert them into list
    for i in rename:
        if isinstance(rename[i], str):
            rename[i] = [rename[i]]

    # Get the from_column
    rename_from = list(adata.obs[from_column].values)

    # Split multiple renaming events into independent events
    name = functools.reduce( lambda x,y: dict(x, **y), (dict(map(lambda x: (x,i), rename[i])) for i in rename))

    # Rename
    for i in name:
        if verbose:
            print ('Renaming ' + str(i) + ' to ' + str(name[i]))
        #rename_from = [x.replace(i, name[i]) for x in rename_from]
        s = str(i)
        s = s.replace('+', '\+')
        #rename_from = [re.sub(r'^\b%s\b$' % s,  name[i], j) for j in rename_from]
        rename_from = [re.sub(r'^\b%s$' % s,  name[i], j) for j in rename_from]


    # Append to adata as a new column
    adata.obs[to_column] = rename_from

    # Return
    return adata