Skip to content

Serialize KmerCountTable

Adam Taranto edited this page Sep 24, 2024 · 1 revision

Serialize KmerCountTable objects to JSON format, save and load serialized objects.

from oxli import KmerCountTable

kct = KmerCountTable(ksize=4)
kct.count("AAAA")  # Count 'AAAA'
kct.count("AATT")  # Count 'AATT'
kct.count("GGGG")  # Count 'GGGG'
kct.count("GGGG")  # Count again.

Serialize the object to a JSON string:

kct.serialize_json()
>>> '{"counts":{"17832910516274425539":1,"382727017318141683":1,"73459868045630124":2},"ksize":4,"version":"0.3.0","consumed":16}'

Serialize and save as GZipped JSON:

kct.save(filepath='kct.json.gz')

Load a saved KmerCountTable object:

# Load returns a new KmerCountTable object
new_kct = KmerCountTable.load('kct.json.gz')

# Inspect the loaded object
new_kct.serialize_json()
>>> '{"counts":{"73459868045630124":2,"17832910516274425539":1,"382727017318141683":1},"ksize":4,"version":"0.3.0","consumed":16}'