Skip to content

ad.merge function #658

Open

Description

@ivirshup

I would like to add a anndata.merge function, with similar functionality to xarray.merge.

Example

>>> expr_adata
AnnData object with n_obs × n_vars = 3000 × 15000
    layers: 'counts'
>>> pca_adata
AnnData object with n_obs × n_vars = 3000 × 15000
    uns: 'pca'
    obsm: 'X_pca'
    varm: 'PCs'
>>> ad.merge([expr_adata, pca_adata])
AnnData object with n_obs × n_vars = 3000 × 15000
    layers: 'counts'
    uns: 'pca'
    obsm: 'X_pca'
    varm: 'PCs'

Use cases

Partial-AnnDatas returned from functions

Many scanpy function take an anndata object, produce a number of elements, and add them back to the original anndata object. We could instead produce a new object which only holds the new elements, then ad.merge the results together. By itself, this is the exact same thing, but this refactoring would allow a few new uses.

Instead of updating the original, we could keep the results seperate. This would be useful for generating multiple parameterizations, or having a lightweight object to pass to further objects – as opposed to mutating or copying the whole original object.

Seperating parts of analyses

We could want to keep elements from annotation or analysis seperate until we need them. We could avoid keeping the large arrays in layers for a velocity analysis, until we actually want them.

scirpy

Scirpy has a function for doing this specifically with immense receptor data: scirpy.pp.merge_with_ir. This would be a more general case. The IR AnnData here is a bit like the "partial-AnnDatas" discussed above.

(please let me know if this isn't the case @grst)

Previous discussion

This has been suggested and discussed a number of places.

Requirements

This would require full support for adata.X = None #467

Implimenting this would fit well with an anndata.align (#531) function (e.g. pass multiple anndata objects, return them with axes aligned). As the updates and reindexing are orthogonal.

Activity

  1. added this to the 0.9.0 milestone on Dec 6, 2021
  2. ivirshup commented on Jan 27, 2022

    @ivirshup
    MemberAuthor

    sgkit does something similar to the "Partial-AnnDatas returned from functions", and controls whether a partial or updated object is returned with the merge kwarg

  3. modified the milestones: 0.9, 0.10.0 on Jan 23, 2023
  4. grst commented on Jan 29, 2023

    @grst
    Contributor

    Just noting that the scirpy use-case is obsolete in favor of MuData after scverse/scirpy#356 gets merged.

  5. modified the milestones: 0.10.0, 0.11.0 on Sep 5, 2023
  6. pcm32 commented on May 8, 2024

    @pcm32

    This would be very nice to have, we produce a lot of heavy objects redundantly due to parametrisation of runs.

  7. modified the milestones: 0.11.0, 0.12.0 on Aug 8, 2024
  8. modified the milestones: 0.12.0, 0.13.0 on Jul 16, 2025
  9. modified the milestones: 0.13.0, 0.14.0 on Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions