mudata.concat

Contents

mudata.concat#

mudata.concat(mdatas, *, join='inner', merge=None, uns_merge=None, label=None, keys=None, index_unique=None, fill_value=None, pairwise=False)#

Concatenates MuData objects.

All mdatas should have the same axis 0 or 1, which defines concatenation axis:

  • concatenate along obs when obs are shared in each mdata (multimodal),

  • concatenate along vars when vars are shared in each mdata (multi-dataset).

The intersection of modalities is taken. Nested MuData objects cannot be concatenated.

This implementation follows anndata.concat() original implementation. The arguments are propagated to anndata.concat() for concatenating modalities. See anndata.concat() documentation for more details.

Parameters:
  • mdatas (Collection[AnnData] | Mapping[str, AnnData]) – The objects to be concatenated. If a Mapping is passed, keys are used for the keys argument and values are concatenated.

  • join (Literal['inner', 'outer'] (default: 'inner')) – How to align values when concatenating. If “outer”, the union of the other axis is taken. If “inner”, the intersection.

  • merge (Literal['same', 'unique', 'first', 'only'] | Callable | None (default: None)) –

    How elements not aligned to the axis being concatenated along are selected. Currently implemented strategies include:

    • None: No elements are kept.

    • "same": Elements that are the same in each of the objects.

    • "unique": Elements for which there is only one possible value.

    • "first": The first element seen at each from each position.

    • "only": Elements that show up in only one of the objects.

  • uns_merge (Literal['same', 'unique', 'first', 'only'] | Callable | None (default: None)) – How the elements of uns are selected. Uses the same set of strategies as the merge argument, except applied recursively.

  • label (str | None (default: None)) – Column in axis annotation (i.e. obs or var) to place batch information in. If None, no column is added.

  • keys (Collection | None (default: None)) – Names for each object being added. These values are used for column values for label or appended to the index if index_unique is not None. Defaults to incrementing integer labels.

  • index_unique (str | None (default: None)) – Whether to make the index unique by using the keys. If provided, this is the delimiter between “{orig_idx}{index_unique}{key}”. When None, the original indices are kept.

  • fill_value (Any | None (default: None)) – When join="outer", this is the value that will be used to fill the introduced indices. By default, sparse arrays are padded with zeros, while dense arrays and DataFrames are padded with missing values.

  • pairwise (bool (default: False)) – Whether pairwise elements along the concatenated dimension should be included. This is False by default, since the resulting arrays are often not meaningful.

Return type:

MuData

Examples

Preparing example objects

>>> import mudata as md, anndata as ad, pandas as pd, numpy as np
>>> from scipy import sparse
>>> a = ad.AnnData(
...     X=sparse.csr_matrix(np.array([[0, 1], [2, 3]])),
...     obs=pd.DataFrame({"group": ["a", "b"]}, index=["s1", "s2"]),
...     var=pd.DataFrame(index=["var1", "var2"]),
...     varm={"ones": np.ones((2, 5)), "rand": np.random.randn(2, 3), "zeros": np.zeros((2, 5))},
...     uns={"a": 1, "b": 2, "c": {"c.a": 3, "c.b": 4}},
... )
>>> b = ad.AnnData(
...     X=sparse.csr_matrix(np.array([[4, 5, 6], [7, 8, 9]])),
...     obs=pd.DataFrame({"group": ["b", "c"], "measure": [1.2, 4.3]}, index=["s3", "s4"]),
...     var=pd.DataFrame(index=["var1", "var2", "var3"]),
...     varm={"ones": np.ones((3, 5)), "rand": np.random.randn(3, 5)},
...     uns={"a": 1, "b": 3, "c": {"c.b": 4}},
... )
>>> c = ad.AnnData(
...     X=sparse.csr_matrix(np.array([[10, 11], [12, 13]])),
...     obs=pd.DataFrame({"group": ["a", "b"]}, index=["s1", "s2"]),
...     var=pd.DataFrame(index=["var3", "var4"]),
...     uns={"a": 1, "b": 4, "c": {"c.a": 3, "c.b": 4, "c.c": 5}},
... )
>>> m = md.MuData({"a": a, "b": b, "c": c})