Skip to content

Commit 339d506

Browse files
pkerpedjievnvictus
authored andcommitted
Changed to use a tileset rather than track constructor for local tile data
1 parent fe8491c commit 339d506

4 files changed

Lines changed: 66 additions & 61 deletions

File tree

src/higlass/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
)
3232
from higlass.server import HiGlassServer
3333
from higlass.tilesets import (
34+
LocalDataTileset,
3435
Tileset,
3536
bed2ddb,
3637
beddb,

src/higlass/api.py

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -120,61 +120,16 @@ def opts(
120120
return track
121121

122122

123-
class _LocalDataMixin:
124-
def local_data(
125-
self: TrackT, # type: ignore
126-
tsinfo,
127-
data,
128-
inplace: bool = False,
129-
) -> TrackT: # type: ignore
130-
"""Configures local data for a Track.
131-
132-
Parameters
133-
----------
134-
tsinfo : dict
135-
Tileset info to be placed under ["tilesetInfo"]["x"].
136-
data : list
137-
Tile data to be placed under ["tiles"].
138-
inplace : bool, optional
139-
Whether to modify the existing track in place or return
140-
a new track with the data applied (default: `False`).
141-
142-
Returns
143-
-------
144-
track : A track with local data configured.
145-
"""
146-
min_pos = tsinfo.get("min_pos", [])
147-
max_pos = tsinfo.get("max_pos", [])
148-
149-
if len(min_pos) != len(max_pos):
150-
raise ValueError("min_pos and max_pos must have equal lengths")
151-
152-
if len(min_pos) == 2:
153-
tile_key = "x.0.0.0"
154-
elif len(min_pos) == 1:
155-
tile_key = "x.0.0"
156-
else:
157-
raise ValueError("min_pos must be a one or two element array")
158-
159-
track = self if inplace else utils.copy_unique(self)
160-
track.data = {
161-
"type": "local-tiles",
162-
"tilesetInfo": {"x": tsinfo},
163-
"tiles": {tile_key: data},
164-
}
165-
return track
166-
167-
168123
## Extend higlass-schema classes
169124

170125

171-
class EnumTrack(hgs.EnumTrack, _LocalDataMixin, _OptionsMixin, _PropertiesMixin):
126+
class EnumTrack(hgs.EnumTrack, _OptionsMixin, _PropertiesMixin):
172127
"""Represents a generic track."""
173128

174129
...
175130

176131

177-
class HeatmapTrack(hgs.HeatmapTrack, _LocalDataMixin, _OptionsMixin, _PropertiesMixin):
132+
class HeatmapTrack(hgs.HeatmapTrack, _OptionsMixin, _PropertiesMixin):
178133
"""Represets a specialized heatmap track."""
179134

180135
...
@@ -196,7 +151,7 @@ class CombinedTrack(hgs.CombinedTrack, _OptionsMixin, _PropertiesMixin):
196151
...
197152

198153

199-
class PluginTrack(hgs.BaseTrack, _OptionsMixin, _PropertiesMixin, _LocalDataMixin):
154+
class PluginTrack(hgs.BaseTrack, _OptionsMixin, _PropertiesMixin):
200155
"""Represents an unknown plugin track."""
201156

202157
plugin_url: ClassVar[str]

src/higlass/tilesets.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from higlass._utils import TrackType, datatype_default_track
1212

1313
__all__ = [
14+
"LocalDataTileset",
1415
"Tileset",
1516
"bed2ddb",
1617
"bigwig",
@@ -71,6 +72,59 @@ def remote(
7172
return RemoteTileset(uid, server, name)
7273

7374

75+
@dataclass
76+
class LocalDataTileset:
77+
"""A tileset that serves data locally without a server.
78+
79+
Parameters
80+
----------
81+
tsinfo : dict
82+
Tileset info dict (must include ``min_pos`` and ``max_pos``).
83+
data : list
84+
Tile data for the tileset.
85+
"""
86+
87+
tsinfo: dict
88+
data: list
89+
90+
def __post_init__(self):
91+
min_pos = self.tsinfo.get("min_pos", [])
92+
max_pos = self.tsinfo.get("max_pos", [])
93+
94+
if len(min_pos) != len(max_pos):
95+
raise ValueError("min_pos and max_pos must have equal lengths")
96+
97+
if len(min_pos) == 2:
98+
self._tile_key = "x.0.0.0"
99+
elif len(min_pos) == 1:
100+
self._tile_key = "x.0.0"
101+
else:
102+
raise ValueError("min_pos must be a one or two element array")
103+
104+
def track(self, type_: TrackType, **kwargs) -> higlass.api.Track:
105+
"""Create a HiGlass track with local data embedded.
106+
107+
Parameters
108+
----------
109+
type_ : TrackType
110+
The track type to create.
111+
**kwargs : dict
112+
Additional top-level track properties.
113+
114+
Returns
115+
-------
116+
higlass.api.Track
117+
A track with the ``data`` section populated for local-tiles.
118+
"""
119+
trk = higlass.api.track(type_=type_, **kwargs)
120+
trk.data = {
121+
"type": "local-tiles",
122+
"tilesetInfo": {"x": self.tsinfo},
123+
"tiles": {self._tile_key: self.data},
124+
}
125+
return trk
126+
127+
74128
class Tileset(abc.ABC):
75129
"""Base class for defining custom tilesets in `higlass`.
76130

test/test_api.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -209,31 +209,26 @@ def test_options_mixin():
209209
assert track.options and track.options["foo"] == "bar"
210210

211211

212-
def test_local_data_mixin():
213-
track = hg.track("heatmap")
212+
def test_local_data_tileset():
214213
tsinfo = {"min_pos": [0, 0], "max_pos": [100, 100]}
215214
data = [{"x": 1, "y": 2}]
216215

217-
other = track.local_data(tsinfo, data)
218-
assert track.uid != other.uid
219-
assert track.data is None
216+
tileset = hg.LocalDataTileset(tsinfo, data)
217+
other = tileset.track("heatmap")
220218
assert other.data.type == "local-tiles"
221219
assert other.data.tilesetInfo["x"] == tsinfo
222220
assert other.data.tiles["x.0.0.0"] == data
223221

224-
track2 = hg.track("heatmap")
225222
tsinfo_1d = {"min_pos": [0], "max_pos": [100]}
226-
other2 = track2.local_data(tsinfo_1d, data, inplace=True)
227-
assert track2 is other2
228-
assert track2.data.tiles["x.0.0"] == data
223+
tileset_1d = hg.LocalDataTileset(tsinfo_1d, data)
224+
other_1d = tileset_1d.track("heatmap")
225+
assert other_1d.data.tiles["x.0.0"] == data
229226

230227
with pytest.raises(ValueError, match="min_pos and max_pos must have equal lengths"):
231-
hg.track("heatmap").local_data({"min_pos": [0], "max_pos": [0, 0]}, data)
228+
hg.LocalDataTileset({"min_pos": [0], "max_pos": [0, 0]}, data)
232229

233230
with pytest.raises(ValueError, match="min_pos must be a one or two element array"):
234-
hg.track("heatmap").local_data(
235-
{"min_pos": [0, 0, 0], "max_pos": [0, 0, 0]}, data
236-
)
231+
hg.LocalDataTileset({"min_pos": [0, 0, 0], "max_pos": [0, 0, 0]}, data)
237232

238233

239234
def test_plugin_track():

0 commit comments

Comments
 (0)