Skip to content

msmu.pp.collapse_obs

Collapse obs rows by sample_key, aggregating duplicates into one row per group.

Common use cases (with recommended agg_method):

  • LFQ fractions (sample × LC fraction → sample): agg_method="sum". Standard LFQ rollup since each fraction holds a different portion of the sample's peptides.
  • Technical replicates (sample × injection/process repeats → sample): agg_method="median" or "mean". Replicates measure the same analyte, so the central tendency is the right estimate ("sum" would inflate).
  • Other obs-axis duplicates (plate replicates, etc.): pick agg_method that matches the meaning of the duplication.

The effect on a given modality depends on its var structure:

  • When var rows are file-specific (e.g. DIA-NN psm where the var index is filename.Precursor.Id), each (sample, var) cell still holds at most one non-NaN value, so this function only reduces the obs dimension; the actual cross-fraction precursor aggregation happens later in to_peptide.
  • When var rows are identity-based (e.g. DDA-LFQ peptide modality where the var index is the peptide identity), a single (sample, var) cell may collect values from multiple obs rows, and agg_method performs the real rollup.
Workflow contexts
  • DIA-NN fractionated: read → psm/precursor → collapse_obsto_peptideto_protein.
  • DDA-LFQ fractionated: read → psm + peptide → collapse_obs (peptide gets real sum) → to_protein.
  • Any workflow with technical replicates: collapse_obs(agg_method="median") early in the pipeline (typically before normalisation).

Applied uniformly across all modalities so the obs axis stays consistent.

Parameters:

Name Type Description Default
mdata MuData

MuData whose obs rows contain duplicates to be collapsed.

required
sample_key str

Column in obs identifying the group each row belongs to.

required
agg_method Literal['sum', 'max', 'median', 'mean']

Aggregation across rows of the same group for each (group, feature) pair. One of "sum", "max", "median", "mean". NaN values are skipped; an all-NaN group yields NaN (not 0) for "sum" as well.

'sum'
layer str | None

Layer to aggregate. If None, .X is used.

None
log_transformed bool

Whether the input quantification is in log2-space. Defaults to False because collapse_obs is typically called before log2_transform (read → collapse → log2 → normalise → ...). When log_transformed=True is combined with agg_method="sum", the function internally converts back to linear space, sums, and re-applies log2 (i.e. log2(sum(2^x))) — the correct LFQ rollup for log2 input. Other agg_method choices (max, median, mean) operate directly in the given space; mean on log2 data corresponds to the geometric mean of linear intensities.

False

Returns:

Type Description
MuData

New MuData with collapsed obs. Obs columns uniform within a group retain

MuData

their scalar value; non-uniform columns become lists preserving the original

MuData

row order, so per-row metadata (e.g. filenames) is not lost.

Notes
  • obsm/obsp are not propagated through the collapse.
  • var and modality uns are preserved.