spac.utils.check_label(adata, annotation, labels=None, should_exist=True, warning=False)[source]

Check if specified labels exist in a given annotation column in adata.obs.

This function verifies whether all or none of the specified labels exist in the provided annotation column of an AnnData object. It ensures that the input labels align with the expected categories present in adata.obs[annotation].

Parameters:
  • adata (anndata.AnnData) – The AnnData object containing the annotation column.

  • annotation (str) – The name of the annotation column in adata.obs to check against.

  • labels (str or list of str, optional) – The label or list of labels to check for existence in the specified annotation column. If None, no validation will be performed.

  • should_exist (bool, optional (default=True)) – Determines whether to check if elements exist in the target column (True), or if they should not exist (False).

  • warning (bool, optional (default=False)) – If True, generate a warning instead of raising an exception if the specified condition is not met.

Raises:
  • TypeError – If adata is not an instance of anndata.AnnData.

  • ValueError – If the specified annotation does not exist in adata.obs. If should_exist is True and any label does not exist in the annotation column. If should_exist is False and any label already exists in the annotation column.

Warns:

UserWarning – If the specified condition is not met and warning is True.

Example

>>> check_label(adata, "cell_type", "B_cell")
>>> check_label(
...     adata, "cluster", ["Cluster1", "Cluster2"], should_exist=True
... )