-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschema.py
512 lines (398 loc) · 14.5 KB
/
schema.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
import json
from typing import (
Any,
Dict,
Generator,
Generic,
List,
Optional,
Tuple,
TypeVar,
Union,
)
from pydantic import BaseModel as PydanticBaseModel
from pydantic import ConfigDict, Field, RootModel, model_validator
from typing_extensions import Annotated, Literal, TypedDict
from .utils import _GenerateJsonSchema, get_schema_of
# Override Basemodel
class BaseModel(PydanticBaseModel):
model_config = ConfigDict(validate_assignment=True)
# nice repr if printing with rich
def __rich_repr__(self):
return iter(self)
def json(self, exclude_none: bool = True, **kwargs):
return super().json(exclude_none=exclude_none, **kwargs)
def dict(self, exclude_none: bool = True, **kwargs):
return super().dict(exclude_none=exclude_none, **kwargs)
def model_dump(self, exclude_none: bool = True, **kwargs):
return super().model_dump(exclude_none=exclude_none, **kwargs)
def model_dump_json(self, exclude_none: bool = True, **kwargs):
return super().model_dump_json(exclude_none=exclude_none, **kwargs)
##################################################
# General #
##################################################
Domain = Tuple[float, float]
class OverlayOptions(BaseModel):
extent: Optional[List[List[int]]] = None
minWidth: Optional[float] = None
fill: Optional[str] = None
fillOpacity: Optional[float] = None
stroke: Optional[str] = None
strokeOpacity: Optional[float] = None
strokeWidth: Optional[float] = None
strokePos: Optional[Union[str, List[str]]] = None
outline: Optional[str] = None
outlineOpacity: Optional[float] = None
outlineWidth: Optional[float] = None
outlinePos: Optional[Union[str, List[str]]] = None
class Overlay(BaseModel):
type: Optional[str] = None
uid: Optional[str] = None
chromInfoPath: Optional[str] = None
includes: Optional[List[str]] = None
options: Optional[OverlayOptions] = None
##################################################
# Locks #
##################################################
# Locks are tricky to describe with python's type system
# because _some_ keys are static (e.g., the lock `uid`) while
# the rest of the keys are dynamic (the view uids) and
# satisfy a different type constraint.
#
# In JSON schema, this is type can be described using an "object"
# "type" with "additionalProperties" or "patternProperties" field.
#
# ```json
# {
# "type": "object",
# "properties": {
# "uid": { "type: "string" }
# },
# "additionalProperties": {
# "type": "array",
# "minLength": 3,
# "maxLength": 3,
# "items": [
# { "type": "number" },
# { "type": "number" },
# { "type": "number" }
# ]
# }
# }
# ```
#
# The lock classes implement pydantic Models which:
#
# (1) Performs the appropriate validation/serde for this object
#
# (2) Exports the appropriate JSON schema using "additionalProperties"
# field via a custom `schema_extra` extension.
#
# This could probably be implemented generally with
# pydantic.generics.Generic/typing.Generic, but we implement
# concretely for the different lock types.
LockEntry = Tuple[float, float, float]
# We'd rather have tuples in our final model, because a
# RootModel is clunky from a python user perspective.
# We create this class to get validation for free in `root_validator`
class _LockEntryModel(RootModel[LockEntry]):
pass
def _lock_schema_extra(schema: Dict[str, Any], _: Any) -> None:
schema["additionalProperties"] = get_schema_of(LockEntry)
class Lock(BaseModel):
uid: Optional[str] = None
model_config = ConfigDict(extra="allow", json_schema_extra=_lock_schema_extra)
def __iter__(self) -> Generator[Tuple[str, LockEntry], None, None]:
for key, val in super().__iter__():
if key not in self.model_fields:
yield key, val
# can only validate on creation for "extra" fields
@model_validator(mode="before")
@classmethod
def validate_locks(cls, values: Dict[str, Any]):
for k in values:
if k not in cls.model_fields:
# validate using our custom validator
model = _LockEntryModel.model_validate(values[k])
# get back the root type
values[k] = model.model_dump()
return values
class ValueScaleLockEntry(TypedDict):
view: str
track: str
class _ValueScaleLockEntryModel(RootModel[ValueScaleLockEntry]):
pass
def _value_scale_lock_schema_extra(schema: Dict[str, Any], _: Any) -> None:
schema["additionalProperties"] = get_schema_of(ValueScaleLockEntry)
class ValueScaleLock(BaseModel):
uid: Optional[str] = None
ignoreOffScreenValues: Optional[bool] = None
model_config = ConfigDict(
extra="allow",
json_schema_extra=_value_scale_lock_schema_extra,
)
def __iter__(self) -> Generator[Tuple[str, ValueScaleLockEntry], None, None]:
for key, val in super().__iter__():
if key not in self.model_fields:
yield key, val
# can only validate on creation for "extra" fields
@model_validator(mode="before")
@classmethod
def validate_locks(cls, values: Dict[str, Any]):
for k in values:
if k not in cls.model_fields:
# validate using our custom validator
model = _ValueScaleLockEntryModel.model_validate(values[k])
# read back as a regular dict
values[k] = model.model_dump()
return values
class AxisSpecificLock(BaseModel):
axis: Literal["x", "y"]
lock: str
class AxisSpecificLocks(BaseModel):
x: Optional[AxisSpecificLock] = None
y: Optional[AxisSpecificLock] = None
class LocationLocks(BaseModel):
locksByViewUid: Dict[str, Union[str, AxisSpecificLocks]] = Field(
default_factory=dict
)
locksDict: Dict[str, Lock] = Field(default_factory=dict)
class ZoomLocks(BaseModel):
model_config = ConfigDict(extra="forbid")
locksByViewUid: Dict[str, str] = Field(default_factory=dict)
locksDict: Dict[str, Lock] = Field(default_factory=dict)
class ValueScaleLocks(BaseModel):
model_config = ConfigDict(extra="forbid")
locksByViewUid: Dict[str, str] = Field(default_factory=dict)
locksDict: Dict[str, ValueScaleLock] = Field(default_factory=dict)
##################################################
# Tracks #
##################################################
TrackTypeT = TypeVar("TrackTypeT", bound=str)
TrackOptions = Dict[str, Any]
TilesetInfo = Dict[str, Any]
Tile = Dict[str, Any]
class Data(BaseModel):
type: Optional[str] = None
url: Optional[str] = None
server: Optional[str] = None
filetype: Optional[str] = None
children: Optional[List] = None
tilesetInfo: Optional[TilesetInfo] = None
tiles: Optional[Tile] = None
class BaseTrack(BaseModel, Generic[TrackTypeT]):
model_config = ConfigDict(extra="allow")
type: TrackTypeT
uid: Optional[str] = None
width: Optional[int] = None
height: Optional[int] = None
options: Optional[TrackOptions] = None
class Tileset(BaseModel):
tilesetUid: Optional[str] = None
server: Optional[str] = None
ViewportProjectionTrackType = Literal[
"viewport-projection-center",
"viewport-projection-vertical",
"viewport-projection-horizontal",
]
EnumTrackType = Union[
ViewportProjectionTrackType,
Literal[
"multivec",
"1d-heatmap",
"line",
"point",
"bar",
"divergent-bar",
"stacked-interval",
"gene-annotations",
"linear-2d-rectangle-domains",
"chromosome-labels",
"linear-heatmap",
"1d-value-interval",
"2d-annotations",
"2d-chromosome-annotations",
"2d-chromosome-grid",
"2d-chromosome-labels",
"2d-rectangle-domains",
"2d-tiles",
"arrowhead-domains",
"bedlike",
"cross-rule",
"dummy",
"horizontal-1d-annotations",
"horizontal-1d-heatmap",
"horizontal-1d-tiles",
"horizontal-1d-value-interval",
"horizontal-2d-rectangle-domains",
"horizontal-bar",
"horizontal-chromosome-grid",
"horizontal-chromosome-labels",
"horizontal-divergent-bar",
"horizontal-gene-annotations",
"horizontal-heatmap",
"horizontal-line",
"horizontal-multivec",
"horizontal-point",
"horizontal-rule",
"horizontal-vector-heatmap",
"image-tiles",
"left-axis",
"left-stacked-interval",
"mapbox-tiles",
"osm-2d-tile-ids",
"osm-tiles",
"raster-tiles",
"simple-svg",
"square-markers",
"top-axis",
"top-stacked-interval",
"vertical-1d-annotations",
"vertical-1d-heatmap",
"vertical-1d-tiles",
"vertical-1d-value-interval",
"vertical-2d-rectangle-domains",
"vertical-bar",
"vertical-bedlike",
"vertical-chromosome-grid",
"vertical-chromosome-labels",
"vertical-gene-annotations",
"vertical-heatmap",
"vertical-line",
"vertical-multivec",
"vertical-point",
"vertical-rule",
"vertical-vector-heatmap",
],
]
class EnumTrack(BaseTrack[EnumTrackType], Tileset):
model_config = ConfigDict(extra="ignore")
data: Optional[Data] = None
chromInfoPath: Optional[str] = None
fromViewUid: Optional[str] = None
x: Optional[float] = None
y: Optional[float] = None
class HeatmapTrack(BaseTrack[Literal["heatmap"]], Tileset):
model_config = ConfigDict(extra="ignore")
data: Optional[Data] = None
position: Optional[str] = None
transforms: Optional[List] = None
class IndependentViewportProjectionTrack(BaseTrack[ViewportProjectionTrackType]):
model_config = ConfigDict(extra="ignore")
fromViewUid: None = None
projectionXDomain: Optional[Domain] = None
projectionYDomain: Optional[Domain] = None
transforms: Optional[List] = None
x: Optional[float] = None
y: Optional[float] = None
class CombinedTrack(BaseTrack[Literal["combined"]]):
model_config = ConfigDict(extra="ignore")
contents: List["Track"]
position: Optional[str] = None
Track = Union[
EnumTrack,
CombinedTrack,
HeatmapTrack,
IndependentViewportProjectionTrack,
BaseTrack,
]
# CombinedTrack is recursive and needs delayed evaluation of annoations
CombinedTrack.model_rebuild()
##################################################
# View #
##################################################
TrackT = TypeVar("TrackT", bound=Track)
TrackPosition = Literal["left", "right", "top", "bottom", "center", "whole", "gallery"]
class Tracks(BaseModel, Generic[TrackT]):
"""Track layout within a View."""
model_config = ConfigDict(extra="ignore")
left: Optional[List[TrackT]] = None
right: Optional[List[TrackT]] = None
top: Optional[List[TrackT]] = None
bottom: Optional[List[TrackT]] = None
center: Optional[List[TrackT]] = None
whole: Optional[List[TrackT]] = None
gallery: Optional[List[TrackT]] = None
def __iter__(self) -> Generator[Tuple[TrackPosition, TrackT], None, None]:
for pos, tlist in super().__iter__():
if tlist is None:
continue
for track in tlist:
yield pos, track # type: ignore
class Layout(BaseModel):
"""Size and position of a View."""
model_config = ConfigDict(extra="ignore")
x: int = Field(default=0, description="The X Position")
y: int = Field(default=0, description="The Y Position")
w: int = Field(default=12, description="Width")
h: int = Field(default=12, description="Height")
moved: Optional[bool] = None
static: Optional[bool] = None
class GenomePositionSearchBox(BaseModel):
"""Locations to search within a View."""
autocompleteServer: Optional[str] = Field(
default=None,
examples=["//higlass.io/api/v1"],
description="The Autocomplete Server URL",
)
autocompleteId: Optional[str] = Field(
default=None,
examples=["OHJakQICQD6gTD7skx4EWA"],
description="The Autocomplete ID",
)
chromInfoServer: Optional[str] = Field(
default=None,
examples=["//higlass.io/api/v1"],
description="The Chrominfo Server URL",
)
chromInfoId: Optional[str] = Field(
default=None,
examples=["hg19"],
description="The Chromosome Info ID",
)
visible: Optional[bool] = Field(
default=None,
description="The Visible Schema",
)
class View(BaseModel, Generic[TrackT]):
"""An arrangment of Tracks to display within a given Layout."""
model_config = ConfigDict(extra="ignore")
layout: Layout
tracks: Tracks[TrackT]
uid: Optional[str] = None
autocompleteSource: Optional[str] = None
chromInfoPath: Optional[str] = None
genomePositionSearchBox: Optional[GenomePositionSearchBox] = None
genomePositionSearchBoxVisible: Optional[bool] = None
initialXDomain: Optional[Domain] = None
initialYDomain: Optional[Domain] = None
overlays: Optional[List[Overlay]] = None
selectionView: Optional[bool] = None
zoomFixed: Optional[bool] = None
zoomLimits: Tuple[float, Optional[float]] = (1, None)
##################################################
# Viewconf #
##################################################
ViewT = TypeVar("ViewT", bound=View)
class Viewconf(BaseModel, Generic[ViewT]):
"""Root object describing a HiGlass visualization."""
model_config = ConfigDict(extra="forbid")
editable: Optional[bool] = True
viewEditable: Optional[bool] = True
tracksEditable: Optional[bool] = True
zoomFixed: Optional[bool] = None
compactLayout: Optional[bool] = None
exportViewUrl: Optional[str] = None
trackSourceServers: Optional[List[str]] = None
views: Optional[Annotated[List[ViewT], Field(min_length=1)]] = None
zoomLocks: Optional[ZoomLocks] = None
locationLocks: Optional[LocationLocks] = None
valueScaleLocks: Optional[ValueScaleLocks] = None
chromInfoPath: Optional[str] = None
def schema():
json_schema = Viewconf.model_json_schema(schema_generator=_GenerateJsonSchema)
json_schema["$schema"] = _GenerateJsonSchema.schema_dialect
json_schema["title"] = "HiGlass viewconf"
return json_schema
def schema_json(**kwargs):
return json.dumps(schema(), **kwargs)