Skip to content

Commit bdebd92

Browse files
Add JsonDataclass for asdict/asjson
1 parent 7520af5 commit bdebd92

File tree

2 files changed

+16
-43
lines changed

2 files changed

+16
-43
lines changed

bio2zarr/core.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import concurrent.futures as cf
22
import contextlib
33
import dataclasses
4+
import json
45
import logging
56
import multiprocessing
67
import os
@@ -277,3 +278,11 @@ def __exit__(self, exc_type, exc_val, exc_tb):
277278
self._update_progress()
278279
self.progress_bar.close()
279280
return False
281+
282+
283+
class JsonDataclass:
284+
def asdict(self):
285+
return dataclasses.asdict(self)
286+
287+
def asjson(self):
288+
return json.dumps(self.asdict(), indent=4)

bio2zarr/vcf.py

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def display_size(n):
5050

5151

5252
@dataclasses.dataclass
53-
class VcfFieldSummary:
53+
class VcfFieldSummary(core.JsonDataclass):
5454
num_chunks: int = 0
5555
compressed_size: int = 0
5656
uncompressed_size: int = 0
@@ -67,9 +67,6 @@ def update(self, other):
6767
self.min_value = min(self.min_value, other.min_value)
6868
self.max_value = max(self.max_value, other.max_value)
6969

70-
def asdict(self):
71-
return dataclasses.asdict(self)
72-
7370
@staticmethod
7471
def fromdict(d):
7572
return VcfFieldSummary(**d)
@@ -168,7 +165,7 @@ class Filter:
168165

169166

170167
@dataclasses.dataclass
171-
class IcfMetadata:
168+
class IcfMetadata(core.JsonDataclass):
172169
samples: list
173170
contigs: list
174171
filters: list
@@ -226,12 +223,6 @@ def fromdict(d):
226223
d["contigs"] = [Contig(**cd) for cd in d["contigs"]]
227224
return IcfMetadata(**d)
228225

229-
def asdict(self):
230-
return dataclasses.asdict(self)
231-
232-
def asjson(self):
233-
return json.dumps(self.asdict(), indent=4)
234-
235226

236227
def fixed_vcf_field_definitions():
237228
def make_field_def(name, vcf_type, vcf_number):
@@ -933,17 +924,11 @@ def num_fields(self):
933924

934925

935926
@dataclasses.dataclass
936-
class IcfPartitionMetadata:
927+
class IcfPartitionMetadata(core.JsonDataclass):
937928
num_records: int
938929
last_position: int
939930
field_summaries: dict
940931

941-
def asdict(self):
942-
return dataclasses.asdict(self)
943-
944-
def asjson(self):
945-
return json.dumps(self.asdict(), indent=4)
946-
947932
@staticmethod
948933
def fromdict(d):
949934
md = IcfPartitionMetadata(**d)
@@ -987,17 +972,11 @@ def check_field_clobbering(icf_metadata):
987972

988973

989974
@dataclasses.dataclass
990-
class IcfWriteSummary:
975+
class IcfWriteSummary(core.JsonDataclass):
991976
num_partitions: int
992977
num_samples: int
993978
num_variants: int
994979

995-
def asdict(self):
996-
return dataclasses.asdict(self)
997-
998-
def asjson(self):
999-
return json.dumps(self.asdict(), indent=4)
1000-
1001980

1002981
class IntermediateColumnarFormatWriter:
1003982
def __init__(self, path):
@@ -1409,7 +1388,7 @@ def variant_chunk_nbytes(self):
14091388

14101389

14111390
@dataclasses.dataclass
1412-
class VcfZarrSchema:
1391+
class VcfZarrSchema(core.JsonDataclass):
14131392
format_version: str
14141393
samples_chunk_size: int
14151394
variants_chunk_size: int
@@ -1421,12 +1400,6 @@ class VcfZarrSchema:
14211400
def field_map(self):
14221401
return {field.name: field for field in self.fields}
14231402

1424-
def asdict(self):
1425-
return dataclasses.asdict(self)
1426-
1427-
def asjson(self):
1428-
return json.dumps(self.asdict(), indent=4)
1429-
14301403
@staticmethod
14311404
def fromdict(d):
14321405
if d["format_version"] != ZARR_SCHEMA_FORMAT_VERSION:
@@ -1645,17 +1618,14 @@ def generate_partitions(num_records, chunk_size, num_partitions, max_chunks=None
16451618

16461619

16471620
@dataclasses.dataclass
1648-
class VcfZarrWriterMetadata:
1621+
class VcfZarrWriterMetadata(core.JsonDataclass):
16491622
format_version: str
16501623
icf_path: str
16511624
schema: VcfZarrSchema
16521625
dimension_separator: str
16531626
partitions: list
16541627
provenance: dict
16551628

1656-
def asdict(self):
1657-
return dataclasses.asdict(self)
1658-
16591629
@staticmethod
16601630
def fromdict(d):
16611631
if d["format_version"] != VZW_METADATA_FORMAT_VERSION:
@@ -1670,19 +1640,13 @@ def fromdict(d):
16701640

16711641

16721642
@dataclasses.dataclass
1673-
class VcfZarrWriteSummary:
1643+
class VcfZarrWriteSummary(core.JsonDataclass):
16741644
num_partitions: int
16751645
num_samples: int
16761646
num_variants: int
16771647
num_chunks: int
16781648
max_encoding_memory: str
16791649

1680-
def asdict(self):
1681-
return dataclasses.asdict(self)
1682-
1683-
def asjson(self):
1684-
return json.dumps(self.asdict(), indent=4)
1685-
16861650

16871651
class VcfZarrWriter:
16881652
def __init__(self, path):

0 commit comments

Comments
 (0)