1
+ """Class for dumping a LinkML model to a Zarr directory store."""
2
+
1
3
from typing import Union
2
4
3
5
import zarr
4
- from pydantic import BaseModel
5
-
6
+ from linkml_runtime import SchemaView
6
7
from linkml_runtime .dumpers .dumper_root import Dumper
7
8
from linkml_runtime .utils .yamlutils import YAMLRoot
8
- from linkml_runtime import SchemaView
9
+ from pydantic import BaseModel
9
10
10
11
11
- def iterate_element (
12
- element : Union [YAMLRoot , BaseModel ],
13
- schemaview : SchemaView ,
14
- group : zarr .hierarchy .Group = None
12
+ def _iterate_element (
13
+ element : Union [YAMLRoot , BaseModel ], schemaview : SchemaView , group : zarr .hierarchy .Group = None
15
14
):
15
+ """Recursively iterate through the elements of a LinkML model and save them.
16
+
17
+ Writes Pydantic BaseModel objects as groups, slots that implement "linkml:elements"
18
+ as datasets, and other slots as attributes.
19
+ """
16
20
# get the type of the element
17
21
element_type = type (element ).__name__
18
22
@@ -25,24 +29,27 @@ def iterate_element(
25
29
if isinstance (v , BaseModel ):
26
30
# create a subgroup and recurse
27
31
subgroup = group .create_group (k )
28
- iterate_element (v , schemaview , subgroup )
32
+ _iterate_element (v , schemaview , subgroup )
29
33
else :
30
34
# create an attribute on the group
31
35
group .attrs [k ] = v
32
36
33
37
34
38
class ZarrDirectoryStoreDumper (Dumper ):
39
+ """Dumper class for LinkML models to Zarr directory stores."""
40
+
41
+ # TODO is this the right method to overwrite? it does not dump a string
42
+ def dumps (self , element : Union [YAMLRoot , BaseModel ], schemaview : SchemaView , ** kwargs ):
43
+ """Dump the element to a Zarr directory store.
35
44
36
- def dumps (self , element : Union [YAMLRoot , BaseModel ], schemaview : SchemaView , ** kwargs ) -> str :
37
- """ Return element formatted as a YAML string with paths to HDF5 files containing the arrays as datasets"""
45
+ Raises:
46
+ ValueError: If the class requires an identifier and it is not provided.
47
+ """
38
48
id_slot = schemaview .get_identifier_slot (element .__class__ .__name__ )
39
49
if id_slot is None :
40
50
raise ValueError ("The class requires an identifier." )
41
51
id_value = getattr (element , id_slot .name )
42
52
output_file_path = f"{ id_value } .zarr"
43
53
store = zarr .DirectoryStore (output_file_path )
44
54
root = zarr .group (store = store , overwrite = True )
45
- iterate_element (element , schemaview , root )
46
-
47
-
48
-
55
+ _iterate_element (element , schemaview , root )
0 commit comments