Skip to content

Should X become a layer internally, as in Python anndata >= 0.13? #562

Open

Description

@rcannood

Related to #514.

Python anndata 0.13 moved X into layers under the None key (scverse/anndata#1707, closing scverse/anndata#244 from 2019): adata.X is now a shortcut for adata.layers[None] (anndata.py#L615, #L658-L661), None shows up in .layers.keys() and in the repr, and reassigning .layers without a None key warns that a future release may drop .X (aligned_mapping.py#L559-L570).

>>> import numpy as np, anndata as ad
>>> a = ad.AnnData(X=np.ones((2, 3)), layers={"counts": np.zeros((2, 3))})
>>> list(a.layers.keys())
['counts', None]
>>> a.X is a.layers[None]
True
>>> a
AnnData object with n_obs × n_vars = 2 × 3
    layers: 'counts', None (.X)

The on-disk format did not change (yet?). X is still written at the root and layers/ is written without the None key (h5ad.py#L100-L109, #L134), also on current main.

On our side, X and layers are still separate slots in AbstractAnnData and in every implementation (InMemoryAnnData.R#L55-L89). I'm working on a PR hides the None key in ReticulateAnnData$layers and py_to_r(), so the R API is unchanged for now. The question is whether we should follow Python further and store X as a layer internally too.

Possible solution

Options:

  • Keep X a separate slot (status quo) and translate at the Python boundary only.
  • Store X in layers internally and keep adata$X as a shortcut:
    • under a reserved name (e.g. ".X"): shows up in names(adata$layers) and layers_keys(), clashes with user layers of that name, and layers_mapping in as_Seurat() / as_SingleCellExperiment() has to filter it out
    • under an attribute or a wrapper object instead of a name: invisible to users, but adata$layers is no longer a plain named list
  • Go all the way and deprecate adata$X in favour of a default layer.

Activity

  1. added
    design
    on Oct 30, 2026
  2. lazappi commented on Nov 2, 2026

    @lazappi
    Collaborator

    I think we should try to match Python, which is currently something like:

    • X is stored in layers
    • None is the key
    • adata$X is kept as a shortcut

    How it's done in {anndatat.claude} might be useful as a reference https://github.com/lazappi/R-anndata-claude/blob/main/R/anndata.R.

  3. rcannood commented on Nov 2, 2026

    @rcannood
    MemberAuthor

    I'd also prefer option 2. What do we use as the layer name for X, though. I suppose we could use NA as a layer name to align with Python anndata, though it feels a bit weird to me.

  4. rcannood commented on Nov 2, 2026

    @rcannood
    MemberAuthor

    I'm noticing a lot of changes are needed to make this happen -- the to SCE and Seurat converters, the different disk formats (which still have the layers + X encoding), the inmemory format. I'll first focus on getting everything to work by just acknowledging that the reticulate interface needs to account for this, and then adapt the anndataR inner workings later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    design

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions