@vitessce/schemas 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +19 -0
  3. package/dist/index.js +16360 -0
  4. package/dist-tsc/file-def-options-legacy.d.ts +72 -0
  5. package/dist-tsc/file-def-options-legacy.d.ts.map +1 -0
  6. package/dist-tsc/file-def-options-legacy.js +28 -0
  7. package/dist-tsc/file-def-options.d.ts +417 -0
  8. package/dist-tsc/file-def-options.d.ts.map +1 -0
  9. package/dist-tsc/file-def-options.js +139 -0
  10. package/dist-tsc/index.d.ts +11 -0
  11. package/dist-tsc/index.d.ts.map +1 -0
  12. package/dist-tsc/index.js +10 -0
  13. package/dist-tsc/previous-config-meta.d.ts +251 -0
  14. package/dist-tsc/previous-config-meta.d.ts.map +1 -0
  15. package/dist-tsc/previous-config-meta.js +22 -0
  16. package/dist-tsc/previous-config-schemas.d.ts +4053 -0
  17. package/dist-tsc/previous-config-schemas.d.ts.map +1 -0
  18. package/dist-tsc/previous-config-schemas.js +204 -0
  19. package/dist-tsc/previous-config-upgraders.d.ts +25 -0
  20. package/dist-tsc/previous-config-upgraders.d.ts.map +1 -0
  21. package/dist-tsc/previous-config-upgraders.js +589 -0
  22. package/dist-tsc/raster-json.d.ts +274 -0
  23. package/dist-tsc/raster-json.d.ts.map +1 -0
  24. package/dist-tsc/raster-json.js +36 -0
  25. package/dist-tsc/schema-builders.d.ts +465 -0
  26. package/dist-tsc/schema-builders.d.ts.map +1 -0
  27. package/dist-tsc/schema-builders.js +127 -0
  28. package/dist-tsc/shared.d.ts +145 -0
  29. package/dist-tsc/shared.d.ts.map +1 -0
  30. package/dist-tsc/shared.js +100 -0
  31. package/dist-tsc/spatial-layers.d.ts +112 -0
  32. package/dist-tsc/spatial-layers.d.ts.map +1 -0
  33. package/dist-tsc/spatial-layers.js +81 -0
  34. package/dist-tsc/view-config-utils.test.d.ts +2 -0
  35. package/dist-tsc/view-config-utils.test.d.ts.map +1 -0
  36. package/dist-tsc/view-config-utils.test.fixtures.d.ts +495 -0
  37. package/dist-tsc/view-config-utils.test.fixtures.d.ts.map +1 -0
  38. package/dist-tsc/view-config-utils.test.fixtures.js +881 -0
  39. package/dist-tsc/view-config-utils.test.js +23 -0
  40. package/dist-tsc/view-config-versions.d.ts +6 -0
  41. package/dist-tsc/view-config-versions.d.ts.map +1 -0
  42. package/dist-tsc/view-config-versions.js +78 -0
  43. package/package.json +49 -0
  44. package/src/file-def-options-legacy.js +34 -0
  45. package/src/file-def-options.ts +158 -0
  46. package/src/index.ts +41 -0
  47. package/src/missing-types.d.ts +1 -0
  48. package/src/previous-config-meta.ts +85 -0
  49. package/src/previous-config-schemas.ts +259 -0
  50. package/src/previous-config-upgraders.ts +739 -0
  51. package/src/raster-json.ts +38 -0
  52. package/src/schema-builders.ts +179 -0
  53. package/src/shared.ts +158 -0
  54. package/src/spatial-layers.ts +103 -0
  55. package/src/view-config-utils.test.fixtures.ts +901 -0
  56. package/src/view-config-utils.test.ts +39 -0
  57. package/src/view-config-versions.ts +87 -0
@@ -0,0 +1,739 @@
1
+ /* eslint-disable camelcase */
2
+ import { z } from 'zod';
3
+ import { v4 as uuidv4 } from 'uuid';
4
+ import { cloneDeep } from 'lodash-es';
5
+ import { getNextScope, capitalize } from '@vitessce/utils';
6
+ import { componentCoordinationScopes, componentCoordinationScopesBy } from './shared.js';
7
+ import {
8
+ configSchema0_1_0,
9
+ configSchema1_0_0,
10
+ configSchema1_0_1,
11
+ configSchema1_0_2,
12
+ configSchema1_0_3,
13
+ configSchema1_0_4,
14
+ configSchema1_0_5,
15
+ configSchema1_0_6,
16
+ configSchema1_0_7,
17
+ configSchema1_0_8,
18
+ configSchema1_0_9,
19
+ configSchema1_0_10,
20
+ configSchema1_0_11,
21
+ configSchema1_0_12,
22
+ configSchema1_0_13,
23
+ configSchema1_0_14,
24
+ configSchema1_0_15,
25
+ configSchema1_0_16,
26
+ } from './previous-config-schemas.js';
27
+
28
+
29
+ interface ViewProps {
30
+ target: [number, number];
31
+ zoom: number;
32
+ }
33
+
34
+ const coordinationSpaceSchema1_0_0 = configSchema1_0_0
35
+ .shape.coordinationSpace.unwrap();
36
+ const viewCoordinationScopes1_0_0 = configSchema1_0_0
37
+ .shape.layout.element.shape.coordinationScopes.unwrap();
38
+
39
+ /**
40
+ * A helper function for the `upgrade()` function,
41
+ * which helps convert `props.view` (for scatterplot and spatial),
42
+ * into new coordination scopes, setting their values
43
+ * in the coordination space and returning the new scope mappings.
44
+ * This function does mutate the `coordinationSpace` parameter.
45
+ * @param {string} prefix The coordination type prefix,
46
+ * either 'embedding' or 'spatial'.
47
+ * @param {object} view The view prop object containing
48
+ * the properties `.target` and `.zoom`.
49
+ * @param {object} coordinationSpace The coordination space.
50
+ * @returns {object} The new coordination scope names.
51
+ */
52
+ function upgradeReplaceViewProp(
53
+ prefix: string,
54
+ view: ViewProps,
55
+ coordinationSpace: z.infer<typeof coordinationSpaceSchema1_0_0>,
56
+ ) {
57
+ const prevZScopes = Object.keys(coordinationSpace[`${prefix}Zoom`]);
58
+ const prevTXScopes = Object.keys(coordinationSpace[`${prefix}TargetX`]);
59
+ const prevTYScopes = Object.keys(coordinationSpace[`${prefix}TargetY`]);
60
+
61
+ const nextZScope = getNextScope(prevZScopes);
62
+ const nextTXScope = getNextScope(prevTXScopes);
63
+ const nextTYScope = getNextScope(prevTYScopes);
64
+
65
+ const { zoom, target: [targetX, targetY] } = view;
66
+ // eslint-disable-next-line no-param-reassign
67
+ coordinationSpace[`${prefix}Zoom`][nextZScope] = zoom;
68
+ // eslint-disable-next-line no-param-reassign
69
+ coordinationSpace[`${prefix}TargetX`][nextTXScope] = targetX;
70
+ // eslint-disable-next-line no-param-reassign
71
+ coordinationSpace[`${prefix}TargetY`][nextTYScope] = targetY;
72
+ return {
73
+ [`${prefix}Zoom`]: nextZScope,
74
+ [`${prefix}TargetX`]: nextTXScope,
75
+ [`${prefix}TargetY`]: nextTYScope,
76
+ };
77
+ }
78
+
79
+ /**
80
+ * Convert an older view config to a newer view config.
81
+ * @param {object} config A v0.1.0 "legacy" view config.
82
+ * @returns {object} A v1.0.0 "upgraded" view config.
83
+ */
84
+ export function upgradeFrom0_1_0(
85
+ config: z.infer<typeof configSchema0_1_0>,
86
+ datasetUid: string | null = null,
87
+ ): z.infer<typeof configSchema1_0_0> {
88
+ const coordinationSpace: z.infer<typeof configSchema1_0_0.shape.coordinationSpace> = {
89
+ embeddingType: {},
90
+ embeddingZoom: {},
91
+ embeddingTargetX: {},
92
+ embeddingTargetY: {},
93
+ spatialZoom: {},
94
+ spatialTargetX: {},
95
+ spatialTargetY: {},
96
+ };
97
+
98
+ const layout: z.infer<typeof configSchema1_0_0.shape.layout> = [];
99
+ config.staticLayout.forEach((componentDef) => {
100
+ let newComponentDef = {
101
+ ...componentDef,
102
+ coordinationScopes: {},
103
+ };
104
+ if (componentDef.component === 'scatterplot') {
105
+ // Need to set up the coordinationSpace
106
+ // with embeddingType to replace scatterplot
107
+ // component prop "mapping".
108
+ if (componentDef.props?.mapping && typeof componentDef.props.mapping === 'string') {
109
+ coordinationSpace.embeddingType[componentDef.props.mapping] = componentDef.props.mapping;
110
+ newComponentDef = {
111
+ ...newComponentDef,
112
+ coordinationScopes: {
113
+ ...newComponentDef.coordinationScopes,
114
+ embeddingType: componentDef.props.mapping,
115
+ },
116
+ };
117
+ }
118
+ // Need to set up the coordinationSpace
119
+ // with embeddingZoom / embeddingTargetX/Y to replace scatterplot
120
+ // component prop "view" ({ zoom, target }).
121
+ if (componentDef.props?.view) {
122
+ // Note that the below function does mutate the coordinationSpace param.
123
+ const newScopeValues = upgradeReplaceViewProp(
124
+ 'embedding', componentDef.props.view, coordinationSpace,
125
+ );
126
+ newComponentDef = {
127
+ ...newComponentDef,
128
+ coordinationScopes: {
129
+ ...newComponentDef.coordinationScopes,
130
+ ...newScopeValues,
131
+ },
132
+ };
133
+ }
134
+ }
135
+ if (componentDef.component === 'spatial') {
136
+ // Need to set up the coordinationSpace
137
+ // with spatialZoom / spatialTargetX/Y to replace spatial
138
+ // component prop "view" ({ zoom, target }).
139
+ if (componentDef?.props?.view) {
140
+ // Note that the below function does mutate the coordinationSpace param.
141
+ const newScopeValues = upgradeReplaceViewProp(
142
+ 'spatial', componentDef.props.view, coordinationSpace,
143
+ );
144
+ newComponentDef = {
145
+ ...newComponentDef,
146
+ coordinationScopes: {
147
+ ...newComponentDef.coordinationScopes,
148
+ ...newScopeValues,
149
+ },
150
+ };
151
+ }
152
+ }
153
+ layout.push(newComponentDef);
154
+ });
155
+
156
+ const lcDef = layout.find(c => c.component === 'layerController');
157
+ const spatialDef = layout.find(c => c.component === 'spatial');
158
+ if (lcDef && spatialDef && 'coordinationScopes' in spatialDef) {
159
+ lcDef.coordinationScopes = spatialDef.coordinationScopes;
160
+ }
161
+
162
+ // Use a random dataset ID when initializing automatically,
163
+ // so that it changes with each new v0.1.0 view config.
164
+ // However, check if the `datasetUid` parameter was passed,
165
+ // which allows for unit testing.
166
+ const newDatasetUid = datasetUid || uuidv4();
167
+
168
+ return {
169
+ version: '1.0.0',
170
+ name: config.name,
171
+ description: config.description,
172
+ public: config.public,
173
+ datasets: [
174
+ {
175
+ uid: newDatasetUid,
176
+ name: newDatasetUid,
177
+ files: config.layers.map(layer => ({
178
+ fileType: layer.fileType,
179
+ url: layer.url,
180
+ })),
181
+ },
182
+ ],
183
+ initStrategy: 'auto',
184
+ coordinationSpace,
185
+ layout,
186
+ };
187
+ }
188
+
189
+ export function upgradeFrom1_0_0(
190
+ config: z.infer<typeof configSchema1_0_0>,
191
+ ): z.infer<typeof configSchema1_0_1> {
192
+ const newConfig = cloneDeep(config);
193
+ const { coordinationSpace } = newConfig;
194
+
195
+ function replaceLayerType(
196
+ layerType: string,
197
+ cSpace: z.infer<typeof coordinationSpaceSchema1_0_0>,
198
+ ) {
199
+ // Layer type could be one of a few things, bitmask or raster at the moment.
200
+ const isRaster = layerType === 'raster';
201
+ // eslint-disable-next-line no-param-reassign
202
+ cSpace[`spatial${capitalize(layerType)}Layer${isRaster ? 's' : ''}`] = {};
203
+ Object.entries(cSpace.spatialLayers).forEach(([scope, layers]) => {
204
+ if (Array.isArray(layers) && layers.find(layer => layer.type === layerType)) {
205
+ const typedLayers = layers
206
+ .filter(layer => layer.type === layerType)
207
+ .map((layer) => {
208
+ const newLayer = { ...layer };
209
+ delete newLayer.type;
210
+ return newLayer;
211
+ });
212
+ // eslint-disable-next-line no-param-reassign
213
+ cSpace[`spatial${capitalize(layerType)}Layer${isRaster ? 's' : ''}`][scope] = isRaster
214
+ ? typedLayers
215
+ : typedLayers[0];
216
+ } else {
217
+ // eslint-disable-next-line no-param-reassign
218
+ cSpace[`spatial${capitalize(layerType)}Layer${isRaster ? 's' : ''}`][scope] = null;
219
+ }
220
+ });
221
+ }
222
+
223
+ if (coordinationSpace && 'spatialLayers' in coordinationSpace) {
224
+ replaceLayerType('raster', coordinationSpace);
225
+ replaceLayerType('cells', coordinationSpace);
226
+ replaceLayerType('molecules', coordinationSpace);
227
+ replaceLayerType('neighborhoods', coordinationSpace);
228
+ delete coordinationSpace.spatialLayers;
229
+ }
230
+
231
+ const layout = newConfig.layout.map((component) => {
232
+ const newComponent = { ...component };
233
+
234
+ function replaceCoordinationScope(
235
+ layerType: string,
236
+ cScopes: z.infer<typeof viewCoordinationScopes1_0_0>,
237
+ ) {
238
+ const isRaster = layerType === 'raster';
239
+ if (
240
+ ['spatial', 'layerController'].includes(newComponent.component)
241
+ || (newComponent.component === 'description' && isRaster)
242
+ ) {
243
+ // eslint-disable-next-line no-param-reassign
244
+ cScopes[`spatial${capitalize(layerType)}Layer${isRaster ? 's' : ''}`] = cScopes.spatialLayers;
245
+ }
246
+ }
247
+
248
+ if (newComponent.coordinationScopes && newComponent.coordinationScopes.spatialLayers) {
249
+ replaceCoordinationScope('raster', newComponent.coordinationScopes);
250
+ replaceCoordinationScope('cells', newComponent.coordinationScopes);
251
+ replaceCoordinationScope('molecules', newComponent.coordinationScopes);
252
+ replaceCoordinationScope('neighborhoods', newComponent.coordinationScopes);
253
+ delete newComponent.coordinationScopes.spatialLayers;
254
+ }
255
+ return newComponent;
256
+ });
257
+
258
+ return {
259
+ ...newConfig,
260
+ coordinationSpace,
261
+ layout,
262
+ version: '1.0.1',
263
+ };
264
+ }
265
+
266
+ interface SpatialRasterLayer1_0_0 {
267
+ type: 'bitmask' | 'raster';
268
+ }
269
+
270
+ export function upgradeFrom1_0_1(
271
+ config: z.infer<typeof configSchema1_0_1>,
272
+ ): z.infer<typeof configSchema1_0_2> {
273
+ // Need to add the globalDisable3d prop to any layer controller views,
274
+ // to match the previous lack of 3D auto-detection behavior.
275
+
276
+ const layout = config.layout.map((component) => {
277
+ const newComponent = { ...component };
278
+ if (newComponent.component === 'layerController') {
279
+ newComponent.props = {
280
+ ...newComponent.props,
281
+ globalDisable3d: true,
282
+ };
283
+ }
284
+ return newComponent;
285
+ });
286
+
287
+ // Enforce bitmask or raster as spatial raster layer type, defaulting
288
+ // to raster layer if it is not one of bitmask or raster from the old config.
289
+
290
+ const newConfig = cloneDeep(config);
291
+ Object.keys((newConfig?.coordinationSpace?.spatialRasterLayers || {})).forEach((key) => {
292
+ if (newConfig.coordinationSpace?.spatialRasterLayers?.[key]) {
293
+ newConfig.coordinationSpace.spatialRasterLayers[key]
294
+ .forEach((layer: SpatialRasterLayer1_0_0, index: number) => {
295
+ if (newConfig.coordinationSpace) {
296
+ newConfig.coordinationSpace.spatialRasterLayers[key][index].type = ['bitmask', 'raster'].includes(layer.type) ? layer.type : 'raster';
297
+ }
298
+ });
299
+ }
300
+ });
301
+
302
+ return {
303
+ ...newConfig,
304
+ layout,
305
+ version: '1.0.2',
306
+ };
307
+ }
308
+
309
+ export function upgradeFrom1_0_2(
310
+ config: z.infer<typeof configSchema1_0_2>,
311
+ ): z.infer<typeof configSchema1_0_3> {
312
+ // Need to add the globalDisable3d prop to any layer controller views,
313
+ // to match the previous lack of 3D auto-detection behavior.
314
+
315
+ const layout = config.layout.map((component) => {
316
+ const newComponent = { ...component };
317
+ if (newComponent.component === 'layerController') {
318
+ newComponent.props = {
319
+ ...newComponent.props,
320
+ disableChannelsIfRgbDetected: true,
321
+ };
322
+ }
323
+ return newComponent;
324
+ });
325
+
326
+ // Enforce bitmask or raster as spatial raster layer type, defaulting
327
+ // to raster layer if it is not one of bitmask or raster from the old config.
328
+
329
+ const newConfig = cloneDeep(config);
330
+
331
+ return {
332
+ ...newConfig,
333
+ layout,
334
+ version: '1.0.3',
335
+ };
336
+ }
337
+
338
+ export function upgradeFrom1_0_3(
339
+ config: z.infer<typeof configSchema1_0_3>,
340
+ ): z.infer<typeof configSchema1_0_4> {
341
+ const newConfig = cloneDeep(config);
342
+
343
+ return {
344
+ ...newConfig,
345
+ version: '1.0.4',
346
+ };
347
+ }
348
+
349
+ // Added in version 1.0.5:
350
+ // - Support for an array of strings in the setName property within options array items
351
+ // for the anndata-cell-sets.zarr file type.
352
+ export function upgradeFrom1_0_4(
353
+ config: z.infer<typeof configSchema1_0_4>,
354
+ ): z.infer<typeof configSchema1_0_5> {
355
+ const newConfig = cloneDeep(config);
356
+
357
+ return {
358
+ ...newConfig,
359
+ version: '1.0.5',
360
+ };
361
+ }
362
+
363
+
364
+ // Added in version 1.0.6:
365
+ // - Support for the scoreName property within options array items
366
+ // for the anndata-cell-sets.zarr file type.
367
+ export function upgradeFrom1_0_5(
368
+ config: z.infer<typeof configSchema1_0_5>,
369
+ ): z.infer<typeof configSchema1_0_6> {
370
+ const newConfig = cloneDeep(config);
371
+
372
+ return {
373
+ ...newConfig,
374
+ version: '1.0.6',
375
+ };
376
+ }
377
+
378
+ // Added in version 1.0.7:
379
+ // - Support for aliasing the gene identifiers using a different var dataframe column
380
+ // via a new `geneAlias` option for the `anndata-expression-matrix.zarr` fileType.
381
+ export function upgradeFrom1_0_6(
382
+ config: z.infer<typeof configSchema1_0_6>,
383
+ ): z.infer<typeof configSchema1_0_7> {
384
+ const newConfig = cloneDeep(config);
385
+
386
+ return {
387
+ ...newConfig,
388
+ version: '1.0.7',
389
+ };
390
+ }
391
+
392
+ // Added in version 1.0.8:
393
+ // - Support for multiple `dataset` coordination scopes and
394
+ // dataset-specific coordination scope mappings for all
395
+ // other coordination types.
396
+ export function upgradeFrom1_0_7(
397
+ config: z.infer<typeof configSchema1_0_7>,
398
+ ): z.infer<typeof configSchema1_0_8> {
399
+ const newConfig = cloneDeep(config);
400
+
401
+ return {
402
+ ...newConfig,
403
+ version: '1.0.8',
404
+ };
405
+ }
406
+
407
+ // Added in version 1.0.9:
408
+ // - Support for plugin coordination types.
409
+ export function upgradeFrom1_0_8(
410
+ config: z.infer<typeof configSchema1_0_8>,
411
+ ): z.infer<typeof configSchema1_0_9> {
412
+ const newConfig = cloneDeep(config);
413
+
414
+ return {
415
+ ...newConfig,
416
+ version: '1.0.9',
417
+ };
418
+ }
419
+
420
+ // Added in version 1.0.10:
421
+ // - Support for the optional 'uid' property for views.
422
+ export function upgradeFrom1_0_9(
423
+ config: z.infer<typeof configSchema1_0_9>,
424
+ ): z.infer<typeof configSchema1_0_10> {
425
+ const newConfig = cloneDeep(config);
426
+
427
+ return {
428
+ ...newConfig,
429
+ version: '1.0.10',
430
+ };
431
+ }
432
+
433
+ // Added in version 1.0.11:
434
+ // - Changes to spatial layer coordination type names.
435
+ // - Cell -> Obs, Gene -> Feature in coordination type names.
436
+ export function upgradeFrom1_0_10(
437
+ config: z.infer<typeof configSchema1_0_10>,
438
+ ): z.infer<typeof configSchema1_0_11> {
439
+ const coordinationSpace = { ...config.coordinationSpace };
440
+
441
+ const scopeAnalogies = {
442
+ // Spatial layer types
443
+ spatialRasterLayers: 'spatialImageLayer',
444
+ spatialCellsLayer: 'spatialSegmentationLayer',
445
+ spatialMoleculesLayer: 'spatialPointLayer',
446
+ spatialNeighborhoodsLayer: 'spatialNeighborhoodLayer',
447
+ // Other types
448
+ cellFilter: 'obsFilter',
449
+ cellHighlight: 'obsHighlight',
450
+ cellSelection: 'obsSelection',
451
+ cellSetSelection: 'obsSetSelection',
452
+ cellSetHighlight: 'obsSetHighlight',
453
+ cellSetColor: 'obsSetColor',
454
+ geneFilter: 'featureFilter',
455
+ geneHighlight: 'featureHighlight',
456
+ geneSelection: 'featureSelection',
457
+ geneExpressionColormap: 'featureValueColormap',
458
+ geneExpressionColormapRange: 'featureValueColormapRange',
459
+ cellColorEncoding: 'obsColorEncoding',
460
+ additionalCellSets: 'additionalObsSets',
461
+ embeddingCellSetPolygonsVisible: 'embeddingObsSetPolygonsVisible',
462
+ embeddingCellSetLabelsVisible: 'embeddingObsSetLabelsVisible',
463
+ embeddingCellSetLabelSize: 'embeddingObsSetLabelSize',
464
+ embeddingCellRadius: 'embeddingObsRadius',
465
+ embeddingCellRadiusMode: 'embeddingObsRadiusMode',
466
+ embeddingCellOpacity: 'embeddingObsOpacity',
467
+ embeddingCellOpacityMode: 'embeddingObsOpacityMode',
468
+ };
469
+
470
+ Object.entries(scopeAnalogies).forEach(([oldKey, newKey]) => {
471
+ if (coordinationSpace[oldKey]) {
472
+ coordinationSpace[newKey] = coordinationSpace[oldKey];
473
+ delete coordinationSpace[oldKey];
474
+ }
475
+ });
476
+
477
+ const layout = config.layout.map((component) => {
478
+ const newComponent = { ...component };
479
+ const { coordinationScopes = {} } = newComponent;
480
+
481
+ Object.entries(scopeAnalogies).forEach(([oldKey, newKey]) => {
482
+ if (coordinationScopes[oldKey]) {
483
+ coordinationScopes[newKey] = coordinationScopes[oldKey];
484
+ delete coordinationScopes[oldKey];
485
+ }
486
+ });
487
+
488
+ return {
489
+ ...newComponent,
490
+ coordinationScopes,
491
+ };
492
+ });
493
+
494
+ return {
495
+ ...config,
496
+ coordinationSpace,
497
+ layout,
498
+ version: '1.0.11',
499
+ };
500
+ }
501
+
502
+ // Added in version 1.0.12:
503
+ // - Added a fileType-to-dataType mapping
504
+ // so that datasets[].files[].type is no longer required.
505
+ export function upgradeFrom1_0_11(
506
+ config: z.infer<typeof configSchema1_0_11>,
507
+ ): z.infer<typeof configSchema1_0_12> {
508
+ const newConfig = cloneDeep(config);
509
+
510
+ const {
511
+ datasets,
512
+ coordinationSpace,
513
+ } = newConfig;
514
+
515
+ if (coordinationSpace?.embeddingType) {
516
+ // This array may contain more embedding types than
517
+ // the cells.json file actually contains, but the tradeoff is that
518
+ // we do not have to load the cells.json file to double check what
519
+ // embedding types are actually present. CellsJsonAsObsEmbedding
520
+ // will just load the embedding as null if it is not present.
521
+ const embeddingTypes = Object.values(coordinationSpace.embeddingType);
522
+ datasets.forEach((dataset, i) => {
523
+ const { files } = dataset;
524
+ files.forEach((fileDef, j) => {
525
+ const { fileType } = fileDef;
526
+ if (fileType === 'cells.json') {
527
+ datasets[i].files[j].options = {
528
+ embeddingTypes,
529
+ };
530
+ }
531
+ });
532
+ });
533
+ }
534
+
535
+ return {
536
+ ...newConfig,
537
+ datasets,
538
+ version: '1.0.12',
539
+ };
540
+ }
541
+
542
+ // Added in version 1.0.13:
543
+ // - Adds the property `coordinationValues` for
544
+ // view config file definitions but is not yet
545
+ // used to do file matching/lookups.
546
+ export function upgradeFrom1_0_12(
547
+ config: z.infer<typeof configSchema1_0_12>,
548
+ ): z.infer<typeof configSchema1_0_13> {
549
+ const newConfig = cloneDeep(config);
550
+
551
+ // Set up coordination scopes for anndata-cells.zarr's options.factors
552
+ // in the coordination space, and create a new coordinationScopes.obsLabelsType array
553
+ // for each component in the layout.
554
+ const { datasets, coordinationSpace, layout } = newConfig;
555
+ const newCoordinationSpace: z.infer<typeof coordinationSpaceSchema1_0_0> = (
556
+ coordinationSpace || {}
557
+ );
558
+ const datasetUidToObsLabelsTypeScopes: Record<string, string[]> = {};
559
+ datasets.forEach((dataset) => {
560
+ const { files, uid } = dataset;
561
+ files.forEach((fileDef) => {
562
+ const { fileType, options } = fileDef;
563
+ if (fileType === 'anndata-cells.zarr') {
564
+ if (options && 'factors' in options && Array.isArray(options.factors)) {
565
+ const obsLabelsTypeScopes: string[] = [];
566
+ options.factors.forEach((olt: string) => {
567
+ const nextScope = getNextScope(Object.keys(coordinationSpace?.obsLabelsType || {}));
568
+ newCoordinationSpace.obsLabelsType = {
569
+ ...newCoordinationSpace.obsLabelsType,
570
+ // Need to remove the obs/ prefix.
571
+ [nextScope]: olt.split('/').at(-1),
572
+ };
573
+ obsLabelsTypeScopes.push(nextScope);
574
+ });
575
+ datasetUidToObsLabelsTypeScopes[uid] = obsLabelsTypeScopes;
576
+ }
577
+ }
578
+ });
579
+ });
580
+ function getDatasetUidForView(
581
+ viewDef: z.infer<typeof configSchema1_0_12.shape.layout.element>,
582
+ ): string|null {
583
+ if (viewDef.coordinationScopes?.dataset && typeof viewDef.coordinationScopes?.dataset === 'string') {
584
+ return newCoordinationSpace.dataset[viewDef.coordinationScopes.dataset];
585
+ }
586
+ if (datasets.length > 0) {
587
+ return datasets[0].uid;
588
+ }
589
+ return null;
590
+ }
591
+ const newLayout = layout.map((viewDef) => {
592
+ const viewDatasetUid = getDatasetUidForView(viewDef);
593
+ if (typeof viewDatasetUid === 'string') {
594
+ const datasetObsLabelsTypeScopes = datasetUidToObsLabelsTypeScopes[viewDatasetUid];
595
+ if (datasetObsLabelsTypeScopes) {
596
+ return {
597
+ ...viewDef,
598
+ coordinationScopes: {
599
+ ...viewDef.coordinationScopes,
600
+ obsLabelsType: datasetObsLabelsTypeScopes,
601
+ },
602
+ };
603
+ }
604
+ }
605
+ return viewDef;
606
+ });
607
+
608
+ return {
609
+ ...newConfig,
610
+ coordinationSpace: newCoordinationSpace,
611
+ layout: newLayout,
612
+ version: '1.0.13',
613
+ };
614
+ }
615
+
616
+ // Added in version 1.0.14:
617
+ // - Adds the coordination types
618
+ // gatingFeatureSelectionX,
619
+ // gatingFeatureSelectionY,
620
+ // featureValueTransformCoefficient.
621
+ export function upgradeFrom1_0_13(
622
+ config: z.infer<typeof configSchema1_0_13>,
623
+ ): z.infer<typeof configSchema1_0_14> {
624
+ const newConfig = cloneDeep(config);
625
+
626
+ return {
627
+ ...newConfig,
628
+ version: '1.0.14',
629
+ };
630
+ }
631
+
632
+ // Added in version 1.0.15:
633
+ // - Changes the following view names:
634
+ // - genes -> featureList
635
+ // - cellSets -> obsSets
636
+ // - cellSetSizes -> obsSetSizes
637
+ // - cellSetExpression -> obsSetFeatureValueDistribution
638
+ // - expressionHistogram -> featureValueHistogram
639
+ // - Deprecates the props:
640
+ // - variablesLabelOverride
641
+ // - observationsLabelOverride
642
+ export function upgradeFrom1_0_14(
643
+ config: z.infer<typeof configSchema1_0_14>,
644
+ ): z.infer<typeof configSchema1_0_15> {
645
+ const newConfig = cloneDeep(config);
646
+ const { layout } = newConfig;
647
+
648
+ const viewTypeAnalogies: Record<string, string> = {
649
+ genes: 'featureList',
650
+ cellSets: 'obsSets',
651
+ cellSetSizes: 'obsSetSizes',
652
+ cellSetExpression: 'obsSetFeatureValueDistribution',
653
+ expressionHistogram: 'featureValueHistogram',
654
+ };
655
+ // Handle the view type renaming.
656
+ const newLayout = layout.map((viewDef) => {
657
+ // Replace the old component name with the new one.
658
+ if (viewTypeAnalogies[viewDef.component]) {
659
+ return {
660
+ ...viewDef,
661
+ component: viewTypeAnalogies[viewDef.component],
662
+ };
663
+ }
664
+ return viewDef;
665
+ });
666
+ const propAnalogies = {
667
+ variablesLabelOverride: 'featureType',
668
+ observationsLabelOverride: 'obsType',
669
+ };
670
+ // Warn about the prop usage.
671
+ newLayout.forEach((viewDef) => {
672
+ // Iterate over each old prop key.
673
+ Object.entries(propAnalogies).forEach(([oldProp, newType]) => {
674
+ if (viewDef.props?.[oldProp]) {
675
+ console.warn(`Warning: the '${oldProp}' prop on the ${viewDef.component} view is deprecated. Please use the '${newType}' coordination type instead.`);
676
+ }
677
+ });
678
+ });
679
+ return {
680
+ ...newConfig,
681
+ version: '1.0.15',
682
+ layout: newLayout,
683
+ };
684
+ }
685
+
686
+ // Added in version 1.0.16:
687
+ // - Explict coordinationScopesBy property for view definitions,
688
+ // to replace the previous implicit mapping of per-dataset coordination
689
+ // scopes.
690
+ export function upgradeFrom1_0_15(
691
+ config: z.infer<typeof configSchema1_0_15>,
692
+ ): z.infer<typeof configSchema1_0_16> {
693
+ const newConfig = cloneDeep(config);
694
+
695
+ const { layout } = newConfig;
696
+ const newLayout = layout.map((view): z.infer<typeof configSchema1_0_16.shape.layout.element> => {
697
+ const { coordinationScopes } = view;
698
+ // Create a new coordinationScopes property that conforms to v1.0.16.
699
+ const newCoordinationScopes: z.infer<typeof componentCoordinationScopes> = {};
700
+ // Update coordinationScopes and coordinationScopesBy when required.
701
+ if (coordinationScopes?.dataset && Array.isArray(coordinationScopes.dataset)) {
702
+ const coordinationScopesBy: z.infer<typeof componentCoordinationScopesBy> = {
703
+ dataset: {},
704
+ };
705
+ Object.entries(coordinationScopes).forEach(([coordinationType, coordinationScope]) => {
706
+ if (!Array.isArray(coordinationScope) && typeof coordinationScope === 'object') {
707
+ if (coordinationType === 'dataset') {
708
+ console.error('Expected coordinationScopes.dataset value to be either string or string[], but got object.');
709
+ }
710
+ coordinationScopesBy.dataset[coordinationType] = coordinationScope;
711
+ } else if (Array.isArray(coordinationScope) || typeof coordinationScope === 'string') {
712
+ newCoordinationScopes[coordinationType] = coordinationScope;
713
+ }
714
+ });
715
+ return {
716
+ ...view,
717
+ coordinationScopes: newCoordinationScopes,
718
+ coordinationScopesBy,
719
+ };
720
+ }
721
+ if (coordinationScopes) {
722
+ Object.entries(coordinationScopes).forEach(([coordinationType, coordinationScope]) => {
723
+ if (Array.isArray(coordinationScope) || typeof coordinationScope === 'string') {
724
+ newCoordinationScopes[coordinationType] = coordinationScope;
725
+ }
726
+ });
727
+ }
728
+ return {
729
+ ...view,
730
+ coordinationScopes: newCoordinationScopes,
731
+ };
732
+ });
733
+
734
+ return {
735
+ ...newConfig,
736
+ layout: newLayout,
737
+ version: '1.0.16',
738
+ };
739
+ }