22from os import PathLike
33
44from anndata import AnnData # type: ignore
5+ from anndata import __version__ as ad_version
56import pandas as pd
67from scipy .sparse import csc_matrix
78import h5py # type: ignore
89from typing import List
910import logging
1011from numpy import ndarray
12+ import platform
1113from .utils import _validate_anndata , _get_loupe_path , get_obs , get_obsm , get_count_matrix
14+ from . import __version__
15+
1216
1317
1418def _create_string_dataset (obj : h5py .Group , key : str , strings : List [str ]| pd .Series | str | pd .Index ) -> None :
@@ -34,6 +38,26 @@ def _create_string_dataset(obj: h5py.Group, key: str, strings: List[str]|pd.Seri
3438 else :
3539 obj .create_dataset (name = key , data = strings , dtype = dtype )
3640
41+ def _write_metadata (f : h5py .File ) -> None :
42+ '''
43+ Writes the metadata to the h5 file
44+ '''
45+ metadata = f .create_group ('metadata' )
46+ meta = {}
47+ h5_version = h5py .h5 .HDF5_VERSION_COMPILED_AGAINST
48+ h5_version = f"{ h5_version [0 ]} .{ h5_version [1 ]} .{ h5_version [2 ]} "
49+ meta ["tool" ] = "LoupePy"
50+ meta ["tool_version" ] = __version__
51+ meta ["os" ] = platform .system ()
52+ meta ["platform" ] = platform .platform ()
53+ meta ["language" ] = f"Python -- { platform .python_version ()} "
54+ meta ["h5py_version" ] = h5py .__version__
55+ meta ["anndata_version" ] = ad_version
56+ meta ["hdf5_version" ] = h5_version
57+ for k ,v in meta .items ():
58+ v = str (v )
59+ _create_string_dataset (metadata , k , v )
60+
3761def _write_matrix (f : h5py .File , matrix : csc_matrix ,
3862 features : pd .Series | pd .Index , barcodes : pd .Index | pd .Series ,
3963 feature_ids : list [str ]| pd .Series | None = None ) -> None :
@@ -202,6 +226,7 @@ def _write_hdf5(mat: csc_matrix,
202226 projections = f .create_group ('projections' )
203227 for n in obsm .keys ():
204228 _write_projection (projections , obsm [n ], n )
229+ _write_metadata (f )
205230 f .close ()
206231 except ValueError :
207232 logging .error ("Something went wrong while writing the h5 file. Please check the input data." )
0 commit comments