alphapepttools.pp.normalize

Contents

alphapepttools.pp.normalize#

alphapepttools.pp.normalize(adata, layer=None, strategy='total_mean', group_column=None, key_added=None, *, copy=False)#

Normalize measured counts per sample

Parameters:
  • adata (AnnData) – Count data

  • layer (str | None (default: None)) – Layer that will be normalized. If None uses anndata.AnnData.X

  • strategy (Literal['total_mean', 'total_median'] (default: 'total_mean')) –

    Normalization strategy

    • total_mean The intensity of each feature is adjusted by a normalizing factor so that the

    total sample intensity is equal to the mean of the total sample intensities across all samples - total_median The intensity of each feature is adjusted by a normalizing factor so that the total sample intensity is equal to the median of the total sample intensities across all samples

  • group_column (str | None (default: None)) – Column name in adata.obs defining groups for group-wise normalization. If None (default), computes statistics across all samples. If specified, computes statistics separately for each group. This is useful when working with data from different batches with different intensity distributions.

  • key_added (str | None (default: None)) – If not None, adds normalization factors to column in adata.obs

  • copy (bool (default: False)) – Whether to return a modified copy (True) of the anndata object. If False (default) modifies the object inplace

Return type:

AnnData

Returns:

None | anndata.AnnData AnnData object with normalized measurement layer. If copy=False modifies the anndata object at layer inplace and returns None. If copy=True, returns a modified copy.

Examples

Create an AnnData object with intensity data:

adata = ad.AnnData(X=np.array([[0.8, 1.0], [2.0, 0.0], [0.0, 2.0]]))
adata.X
> array([[0.8, 1.0],
        [2.0, 0.0],
        [0.0, 2.0]])

The anndata object gets normalized in place. Per default, the .X attribute will be modified:

normalize(adata)
adata.X
> array([[0.85925926, 1.07407407],
        [1.93333333, 0.        ],
        [0.        , 1.93333333]])

Alternatively, we can normalize a different layer:

adata.layers["normalized"] = adata.X.copy()
normalize(adata, strategy="total_mean", layer="normalized")
adata.X
> array([[0.8, 1.0],
        [2.0, 0.0],
        [0.0, 2.0]])
adata.layers["normalized"]
> array([[0.85925926, 1.07407407],
        [1.93333333, 0.        ],
        [0.        , 1.93333333]])

Or we return a copy of the object:

new_adata = normalize(adata, copy=True)