Skip to content

Iterating Records

Adam Taranto edited this page Sep 24, 2024 · 3 revisions

This section explains how to iterate over records in a KmerCountTable object.

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.

KmerCountTable objects are iterable in Python.

Remember that count tables are unsorted by default and order will vary between runs. Use dump() to get sorted records.

# Convert to list
list(kct)
>>> [(73459868045630124, 2), (17832910516274425539, 1), (382727017318141683, 1)]

# Use with generators
[x for x in kct]
>>> [(73459868045630124, 2), (17832910516274425539, 1), (382727017318141683, 1)]

# Make an iterator
table_iterator = iter(kct)

print(next(table_iterator))
>>> (73459868045630124, 2)

print(next(table_iterator))
>>> (17832910516274425539, 1)

print(next(table_iterator))
>>> (382727017318141683, 1)