.var_consistency

proteopy.pp.var_consistency(adata, batch_key, group_by, min_groups=4, layer=None, key_added=None, inplace=True, verbose=False)[source]

Compute the consistency of each variable’s profile across batches.

Batches (e.g. replicates) are expected to reproduce the same profile across groups (e.g. conditions and fractions). For every variable:

  1. construct its profile across the group_by groups for each batch

  2. compute the Pearson correlation between the profiles of every batch pair

  3. summarize the pairwise correlations by their median, minimum and mean

The resulting scores can be used to remove variables with poorly reproducible profiles via filter_by_var_consistency().

Parameters:
  • adata (AnnData) – AnnData object in proteodata format.

  • batch_key (str) – Column in adata.obs identifying the batches whose profiles are compared, e.g. replicates.

  • group_by (str | list[str]) – Column(s) in adata.obs defining the groups that make up a profile. With several columns, each combination of their values is one group, e.g. ["condition", "fraction"].

  • min_groups (int) – Minimum number of groups with a value in both batches required to compute a correlation. Must be at least 2.

  • layer (str | None) – Layer to use for the computation. If None, uses adata.X.

  • key_added (str | None) – Prefix of the keys under which results are stored. Defaults to '<batch_key>_corr'. Stored are, in adata.var, '<key_added>_median', '<key_added>_min', '<key_added>_mean' and '<key_added>_n_valid' (the number of batch pairs with a defined correlation), and, in adata.varm, '<key_added>_pairs': a DataFrame with the correlation ('corr_<batch1>_vs_<batch2>') and the number of shared groups ('n_<batch1>_vs_<batch2>') of every batch pair.

  • inplace (bool) – If True, modify adata in place. If False, return a copy of adata with the added keys.

  • verbose (bool) – If True, print the matrix used and where results are stored.

Returns:

The annotated copy of adata if inplace=False, otherwise None.

Return type:

AnnData | None

Raises:
  • KeyError – If batch_key or a group_by column is not in adata.obs, or layer is not in adata.layers.

  • ValueError – If group_by is empty, has duplicates or contains batch_key, the batch_key / group_by columns contain missing values, min_groups is smaller than 2, or fewer than two batches are present.

  • TypeError – If an argument has the wrong type.

Warns:

UserWarning – If the selected matrix is sparse and is densified, or if an output key already exists and is overwritten.

Notes

If a batch holds more than one sample for the same group, their values are averaged. A correlation is undefined (NaN) when the two batches share fewer than min_groups non-missing groups or either profile is constant. Zeros are used as values, not treated as missing; convert them to NaN beforehand if they denote missing measurements.

Examples

>>> import proteopy as pr
>>> pr.pp.var_consistency(
...     adata,
...     batch_key="replicate",
...     group_by=["condition", "fraction"],
... )
>>> adata.var["replicate_corr_min"].describe()