Serializers¶
JSON serialization for Tracks and Regions. Uses only the public Track
protocol — no private attribute access. NaN and Inf values are sanitized
to null for JSON safety.
region_to_dict
¶
Convert a Region to a JSON-safe dictionary.
Reads public fields only. Sanitizes NaN/Inf on score.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
r
|
Region
|
A Region instance. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with chrom, start, end, strand, score, name, tags. |
Examples:
>>> region_to_dict(Region("chr1", 10, 20, score=5.0))
{'chrom': 'chr1', 'start': 10, 'end': 20, 'strand': '.', 'score': 5.0, 'name': '', 'tags': {}}
Source code in src/seqchain/api/serializers.py
serialize_interval_track
¶
Serialize an IntervalTrack to a list of region dicts.
When chroms is populated, queries each chromosome via
track.regions(chrom, 0, size). When empty (e.g. tracks
loaded from BED upload without a reference), falls back to
direct iteration over the track.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track
|
Any
|
An IntervalTrack instance. |
required |
chroms
|
dict[str, int]
|
Chromosome name → size mapping (may be empty). |
required |
Returns:
| Type | Description |
|---|---|
list[dict]
|
List of region dictionaries. |
Examples:
>>> from seqchain.region import Region
>>> from seqchain.track import IntervalTrack, TrackLabel
>>> t = IntervalTrack(TrackLabel("t"), [Region("chr1", 0, 10, score=1.0)])
>>> serialize_interval_track(t, {"chr1": 100})
[{'chrom': 'chr1', 'start': 0, 'end': 10, 'strand': '.', 'score': 1.0, 'name': '', 'tags': {}}]
Source code in src/seqchain/api/serializers.py
serialize_table_track
¶
Serialize a TableTrack to a key-value dictionary.
Calls track.keys() and track.get(key) for each key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track
|
Any
|
A TableTrack instance. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float | None]
|
Dictionary of key → sanitized float value. |
Examples:
>>> from seqchain.track import TableTrack, TrackLabel
>>> serialize_table_track(TableTrack(TrackLabel("t"), {"a": 1.0, "b": 2.0}))
{'a': 1.0, 'b': 2.0}
Source code in src/seqchain/api/serializers.py
serialize_signal_summary
¶
Get a signal summary value for a region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track
|
Any
|
A SignalTrack instance. |
required |
chrom
|
str
|
Chromosome name. |
required |
start
|
int
|
Start coordinate. |
required |
end
|
int
|
End coordinate. |
required |
Returns:
| Type | Description |
|---|---|
float | None
|
Mean signal value, or |
Examples:
>>> from seqchain.region import Region
>>> from seqchain.track import IntervalTrack, TrackLabel
>>> t = IntervalTrack(TrackLabel("t"), [Region("chr1", 0, 100, score=5.0)])
>>> serialize_signal_summary(t, "chr1", 0, 100)
5.0
Source code in src/seqchain/api/serializers.py
genome_to_dict
¶
Convert a Genome to a JSON-safe dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
genome
|
Genome
|
A Genome instance. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with name, chroms (name → size), and topologies. |
Examples:
>>> from seqchain.io.genome import Genome
>>> genome_to_dict(Genome("test", {}, {"chr1": "ATCG"}, (), {}))
{'name': 'test', 'chroms': {'chr1': 4}, 'topologies': {}}