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: diff --git a/docs/how_do_i.rst b/docs/how_do_i.rst index 0aa9c1d9e..bb20be626 100644 --- a/docs/how_do_i.rst +++ b/docs/how_do_i.rst @@ -165,6 +165,24 @@ 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") + +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? -------------------------