From 14349dadd7c49fff4a05dc70ae758326ced04ec7 Mon Sep 17 00:00:00 2001 From: Zuolin Cheng Date: Wed, 2 Dec 2026 22:31:29 -0800 Subject: [PATCH 1/3] Document adding variables to existing Zarr datasets --- docs/how_do_i.rst | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docs/how_do_i.rst b/docs/how_do_i.rst index 0aa9c1d9e..dc28d83a2 100644 --- a/docs/how_do_i.rst +++ b/docs/how_do_i.rst @@ -165,6 +165,22 @@ Call :py:func:`save_dataset`: Zarr datasets must have equal-sized chunks (except for the final chunk, which may be smaller), so you may have to `rechunk the dataset `_ first. +Add a variable to an existing Zarr dataset? +------------------------------------------- + +Load the existing dataset and compute only the new variable. Pass that variable +to :py:func:`save_dataset` with ``mode="a"`` to add it to the existing store: + +.. code-block:: python + + ds = sg.load_dataset("ds.zarr") + counts = sg.count_variant_alleles(ds, merge=False).assign_attrs(ds.attrs) + sg.save_dataset(counts, "ds.zarr", mode="a") + +Include only the variables you intend to write. Append mode can also overwrite +variables with the same names, and ``assign_attrs(ds.attrs)`` retains the +existing dataset attributes. + Load a dataset from Zarr? ------------------------- From f8eb8893eee7e6f54c0d0a965663d97df959a22d Mon Sep 17 00:00:00 2001 From: Zuolin Cheng Date: Wed, 2 Dec 2026 22:47:23 -0800 Subject: [PATCH 2/3] Explain variable selection and replacement when appending Zarr data --- docs/how_do_i.rst | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/how_do_i.rst b/docs/how_do_i.rst index dc28d83a2..bb20be626 100644 --- a/docs/how_do_i.rst +++ b/docs/how_do_i.rst @@ -177,9 +177,11 @@ to :py:func:`save_dataset` with ``mode="a"`` to add it to the existing store: counts = sg.count_variant_alleles(ds, merge=False).assign_attrs(ds.attrs) sg.save_dataset(counts, "ds.zarr", mode="a") -Include only the variables you intend to write. Append mode can also overwrite -variables with the same names, and ``assign_attrs(ds.attrs)`` retains the -existing dataset attributes. +If you create the new variable inside a larger dataset, select it with +``ds[["new_variable"]]`` before saving. Append mode can also overwrite variables +with the same names. If the replacement depends on values loaded lazily from +the variable being overwritten, call ``.compute()`` on the replacement before +saving. Use ``assign_attrs(ds.attrs)`` to retain the existing dataset attributes. Load a dataset from Zarr? ------------------------- From 0523a7f143ed418f920d2aa7b6b42942c360682d Mon Sep 17 00:00:00 2001 From: Zuolin Cheng Date: Wed, 2 Dec 2026 22:47:23 -0800 Subject: [PATCH 3/3] docs: add the required changelog entry --- docs/changelog.rst | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 7f7a29eb0..e6c796220 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -83,8 +83,11 @@ Bug fixes matches the computed result, and correlation distances are no longer slightly negative. -.. Documentation -.. ~~~~~~~~~~~~~ +Documentation +~~~~~~~~~~~~~ + +- Document adding derived variables to an existing Zarr dataset and safely + replacing an existing variable after materializing its data. .. _changelog.0.10.0: