sercol¶
sercol provides Collection, a sequence that filters and groups while keeping
its collection type and source metadata, and saves to JSON through
serializable. It is the base class
for varcode's VariantCollection and
EffectCollection and mhctools'
BindingPredictionCollection, so their filtering and grouping behave the same.
Install¶
python -m pip install sercol
Python 3.11 or later is required; pip installs serializable and pandas 3.
Filter and group values¶
from sercol import Collection
values = Collection([5, 2, 3, 2])
selected = values.filter(lambda value: value >= 3)
groups = values.groupby(lambda value: value % 2)
print(list(selected))
print({key: list(group) for key, group in groups.items()})
[5, 3]
{1: [5, 3], 0: [2, 2]}
filter() returns a collection; groupby() returns a dictionary of
collections. Both keep the original collection class and its distinct,
sort_key and sources. The original collection is unchanged.
Save and reload¶
Continuing with values above:
restored = Collection.from_json(values.to_json())
print(list(restored))
print(restored == values)
[5, 2, 3, 2]
True
Serialization uses serializable.
Elements and constructor state must be supported by that format. A sort_key
must be a function defined at the top level of a module, so that it can be
imported when reconstructing the collection; to_json() raises ValueError
for a lambda or a function defined inside another function.
See select, order and track values for thresholds, multiple grouping keys and source metadata. The API reference lists all collection operations.