opendvp.pp.rescale

Contents

opendvp.pp.rescale#

opendvp.pp.rescale(adata, gate=None, log=True, imageid='imageid', failed_markers=None, method='all', verbose=True, random_state=0, gmm_components=3)#
Parameters:
  • adata (AnnData Object, required) – An annotated data object that contains single-cell expression data.

  • gate (DataFrame, optional) –

    A pandas DataFrame where the first column lists markers, and subsequent columns contain gate values for each image in the dataset. Column names must correspond to unique imageid identifiers, and the marker column must be named “markers”. If a single column of gate values is provided for a dataset with multiple images, the same gate will be uniformly applied to all images. In this case, ensure that the columns are named exactly “markers” and “gates”. If no gates are provided for specific markers, the function attempts to automatically determine gates using a Gaussian Mixture Model (GMM).

    Note: If you have used napariGater(), the gates are stored within adata.uns['gates']. You can directly pass adata.uns['gates'] to use these pre-defined gates.

  • log (bool, optional) – If True, the data in adata.raw.X will be log-transformed (using log1p) before gate application. This transformation is recommended when automatic gate identification through GMM is performed, as it helps in normalizing data distributions.

  • imageid (str, optional) – The name of the column in adata that contains Image IDs. This is necessary for matching manual gates specified in the gate DataFrame to their respective images.

  • failed_markers (dict, optional) – A dictionary mapping imageid to markers that failed quality control. This allows for the exclusion of specific markers from the analysis based on prior visual inspection or other criteria. The dictionary can use all as a key to specify markers that failed across all images.

  • method (str, optional) – Specifies the gating strategy: all to pool data from all images for GMM application, or by_image to apply GMM separately for each image. all may introduce batch effects, while by_image requires sufficient variation within each image to distinguish negative from positive populations effectively.

  • random_state (int, optional) – The seed used by the random number generator for GMM. Ensures reproducibility of results.

  • verbose (bool, optional) – If True, detailed progress updates and diagnostic messages will be printed during the function’s execution.

  • gmm_components (int, optional) – Number of components to use in the Gaussian Mixture Model for automatic gating. Must be at least 2. Gate will be placed between the highest two components. Default is 3.

Returns:

Returns the input adata object with updated expression data (adata.X) after rescaling. The gates applied, either provided manually or determined automatically, are stored within adata.uns['gates'].

Return type:

Modified AnnData Object (AnnData)

Example

```python

# Example with manual gates manual_gate = pd.DataFrame({‘marker’: [‘CD3D’, ‘KI67’], ‘gate’: [7, 8]}) adata = sm.pp.rescale(adata, gate=manual_gate, failed_markers={‘all’: [‘CD20’, ‘CD21’]})

# Importing gates from a CSV manual_gate = pd.read_csv(‘manual_gates.csv’) adata = sm.pp.rescale(adata, gate=manual_gate, failed_markers={‘all’: [‘CD20’, ‘CD21’]})

# Running without manual gates to use GMM for automatic gate determination adata = sm.pp.rescale(adata, gate=None, failed_markers={‘all’: [‘CD20’, ‘CD21’]})

```