@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,38 @@
1
+ import { z } from 'zod';
2
+ import { requestInit } from './shared.js';
3
+
4
+ const image = z.object({
5
+ name: z.string(),
6
+ url: z.string(),
7
+ type: z.string(),
8
+ metadata: z.object({
9
+ dimensions: z.array(z.object({
10
+ field: z.string(),
11
+ type: z.enum(['quantitative', 'nominal', 'ordinal', 'temporal']),
12
+ values: z.array(z.string()).nullable(),
13
+ })).optional(),
14
+ isPyramid: z.boolean().optional(),
15
+ transform: z.union([
16
+ z.object({
17
+ scale: z.number(),
18
+ translate: z.object({
19
+ y: z.number(),
20
+ x: z.number(),
21
+ }),
22
+ }),
23
+ z.object({
24
+ matrix: z.array(z.number()).length(16),
25
+ }),
26
+ ]).optional(),
27
+ isBitmask: z.boolean().optional(),
28
+ omeTiffOffsetsUrl: z.string().optional(),
29
+ }).optional(),
30
+ requestInit: requestInit.optional(),
31
+ });
32
+
33
+ export const rasterJsonSchema = z.object({
34
+ schemaVersion: z.literal('0.0.2'),
35
+ usePhysicalSizeScaling: z.boolean().optional(),
36
+ renderLayers: z.array(z.string()).optional(),
37
+ images: z.array(image),
38
+ });
@@ -0,0 +1,179 @@
1
+ import { z } from 'zod';
2
+ import { fromEntries } from '@vitessce/utils';
3
+ import {
4
+ PluginViewType,
5
+ PluginCoordinationType,
6
+ PluginFileType,
7
+ PluginJointFileType,
8
+ } from '@vitessce/plugins';
9
+ import {
10
+ requestInit,
11
+ coordinationScopeName,
12
+ componentCoordinationScopes,
13
+ componentCoordinationScopesBy,
14
+ } from './shared.js';
15
+ import { latestConfigSchema } from './previous-config-meta.js';
16
+ import { configSchemaToVersion } from './view-config-versions.js';
17
+
18
+ /**
19
+ * Convert an array of strings into a Zod enum schema,
20
+ * bypassing the need for the input array to have
21
+ * at least two elements.
22
+ * @param schemaArr Array of strings.
23
+ * @returns Zod enum schema.
24
+ */
25
+ function toEnum(schemaArr: string[]) {
26
+ if (schemaArr.length === 0) return z.null();
27
+ if (schemaArr.length === 1) return z.literal(schemaArr[0]);
28
+ return z.enum([schemaArr[0], ...schemaArr.slice(1, schemaArr.length)]);
29
+ }
30
+
31
+ /**
32
+ * Build a Zod schema for a file definition.
33
+ * The builder pattern allows the returned
34
+ * Zod schema to be typed despite not knowing
35
+ * the file type name or options sub-schema in advance,
36
+ * which is the case for plugin file types in particular.
37
+ * @param fileType The file type name.
38
+ * @param options Zod schema for the file definition options.
39
+ * @returns Zod object schema for the file definition.
40
+ */
41
+ function buildFileDefSchema<T extends z.ZodTypeAny>(fileType: string, options: T) {
42
+ return z.object({
43
+ fileType: z.literal(fileType),
44
+ options: options.optional(),
45
+ url: z.string()
46
+ .optional(),
47
+ requestInit: requestInit
48
+ .describe(
49
+ 'The properties of this object correspond to the parameters of the JavaScript fetch() function.',
50
+ )
51
+ .optional(),
52
+ coordinationValues: z.record(z.string())
53
+ .describe(
54
+ 'Keys are coordination types. Values are coordination values. Used for matching views to files.',
55
+ )
56
+ .optional(),
57
+ });
58
+ }
59
+
60
+ type FileDefSchema = ReturnType<typeof buildFileDefSchema>;
61
+
62
+ /**
63
+ * Convert an array of Zod schemas into a Zod union schema,
64
+ * bypassing the need for the input array to have
65
+ * at least two elements.
66
+ * @param schemaArr Array of Zod schemas.
67
+ * @returns Zod union schema.
68
+ */
69
+ function toFileDefUnion<T extends FileDefSchema>(schemaArr: T[]) {
70
+ if (schemaArr.length === 0) return z.null();
71
+ if (schemaArr.length === 1) return schemaArr[0];
72
+ return z.discriminatedUnion('fileType', [
73
+ schemaArr[0],
74
+ schemaArr[1],
75
+ ...schemaArr.slice(2, schemaArr.length),
76
+ ]);
77
+ }
78
+
79
+ /**
80
+ * Build a Zod schema for the latest Vitessce config,
81
+ * which is specific to any registered plugins.
82
+ * The builder pattern allows the returned
83
+ * Zod schema to be typed despite not knowing
84
+ * the plugin names or sub-schemas in advance.
85
+ * @param pluginFileTypes
86
+ * @param pluginJointFileTypes
87
+ * @param pluginCoordinationTypes
88
+ * @param pluginViewTypes
89
+ * @returns The Zod schema.
90
+ */
91
+ export function buildConfigSchema<
92
+ T1 extends PluginFileType<any, any, z.ZodTypeAny>,
93
+ T2 extends PluginJointFileType<z.ZodTypeAny>,
94
+ T3 extends PluginCoordinationType<z.ZodTypeAny>,
95
+ >(
96
+ pluginFileTypes: Array<T1>,
97
+ pluginJointFileTypes: Array<T2>,
98
+ pluginCoordinationTypes: Array<T3>,
99
+ pluginViewTypes: Array<PluginViewType>,
100
+ ) {
101
+ const fileTypeSchemas = [...pluginFileTypes, ...pluginJointFileTypes]
102
+ .map(ft => buildFileDefSchema(ft.name, ft.optionsSchema));
103
+
104
+ const fileDefs = toFileDefUnion(fileTypeSchemas);
105
+
106
+ // TODO: make this less redundant with latestSchema from ./previous-base-schemas
107
+ return z.object({
108
+ // eslint-disable-next-line no-underscore-dangle
109
+ version: z.literal(configSchemaToVersion(latestConfigSchema))
110
+ .describe('The schema version for the view config.'),
111
+ uid: z.string().optional(),
112
+ name: z.string(),
113
+ public: z.boolean().optional(),
114
+ description: z.string().optional(),
115
+ datasets: z.array(
116
+ z.object({
117
+ uid: z.string(),
118
+ name: z.string().optional(),
119
+ description: z.string().optional(),
120
+ files: z.array(
121
+ fileDefs,
122
+ ),
123
+ }),
124
+ )
125
+ .describe(
126
+ 'The datasets array defines groups of files, where the files within each dataset reference the same entities (cells, genes, cell sets, etc).',
127
+ ),
128
+ // Merge with coordination type schemas.
129
+ coordinationSpace: z.object(
130
+ // Wrap each value schema in z.record()
131
+ fromEntries(
132
+ pluginCoordinationTypes
133
+ .map(ct => ([
134
+ ct.name,
135
+ z.record(
136
+ coordinationScopeName,
137
+ ct.valueSchema.optional(),
138
+ ).optional(),
139
+ ])),
140
+ ),
141
+ )
142
+ .strict()
143
+ .describe(
144
+ 'The coordination space stores the values for each scope of each coordination object.',
145
+ )
146
+ .optional(),
147
+ layout: z.array(
148
+ z.object({
149
+ uid: z.string()
150
+ .describe(
151
+ 'A unique identifier for the view, to refer to it in getter and setter functions in object-oriented contexts.',
152
+ )
153
+ .optional(),
154
+ component: toEnum(pluginViewTypes.map(vt => vt.name))
155
+ .describe(
156
+ 'Specify a component using a name defined in the component registry.',
157
+ ),
158
+ props: z.record(z.any())
159
+ .describe('Extra prop values for the component.')
160
+ .optional(),
161
+ x: z.number().int(),
162
+ y: z.number().int(),
163
+ w: z.number().int().optional(),
164
+ h: z.number().int().optional(),
165
+ coordinationScopes: componentCoordinationScopes
166
+ .optional(),
167
+ coordinationScopesBy: componentCoordinationScopesBy
168
+ .optional(),
169
+ }),
170
+ )
171
+ .describe(
172
+ 'The layout array defines the views, or components, rendered in the grid.',
173
+ ),
174
+ initStrategy: z.enum(['none', 'auto'])
175
+ .describe(
176
+ 'The initialization strategy determines how missing coordination objects and coordination scope mappings are initially filled in.',
177
+ ),
178
+ });
179
+ }
package/src/shared.ts ADDED
@@ -0,0 +1,158 @@
1
+ import { z } from 'zod';
2
+
3
+ export const coordinationTypeName = z.string();
4
+ export const coordinationScopeName = z.string();
5
+
6
+ const stringOrStringArray = z.union([
7
+ z.string(),
8
+ z.array(z.string()),
9
+ ]);
10
+
11
+ const oneOrMoreCoordinationScopeNames = stringOrStringArray;
12
+
13
+ export const componentCoordinationScopes = z.record(
14
+ coordinationTypeName,
15
+ oneOrMoreCoordinationScopeNames,
16
+ );
17
+
18
+ export const componentCoordinationScopesBy = z.record(
19
+ coordinationTypeName,
20
+ z.record(
21
+ coordinationTypeName,
22
+ z.record(coordinationScopeName, oneOrMoreCoordinationScopeNames),
23
+ ),
24
+ );
25
+
26
+ export const rgbArray = z.array(z.number())
27
+ .length(3);
28
+
29
+ const treeNodeBase = z.object({
30
+ name: z.string(),
31
+ color: rgbArray.optional(),
32
+ });
33
+
34
+ const treeNodeLeaf = treeNodeBase.extend({
35
+ set: z.array(z.string()),
36
+ });
37
+
38
+ type TreeNodeLeaf = z.input<typeof treeNodeLeaf>;
39
+
40
+ type TreeNodeNonLeaf = z.input<typeof treeNodeBase> & {
41
+ children: Array<TreeNodeNonLeaf | TreeNodeLeaf>;
42
+ };
43
+
44
+ const treeNodeNonLeaf: z.ZodType<TreeNodeNonLeaf> = treeNodeBase.extend({
45
+ children: z.lazy(() => z.array(z.union([treeNodeNonLeaf, treeNodeLeaf]))),
46
+ });
47
+
48
+ const cellSets2 = z.object({
49
+ version: z.literal('0.1.2'),
50
+ tree: z.array(treeNodeNonLeaf),
51
+ });
52
+
53
+ const treeNodeLeafProbabilistic = treeNodeBase.extend({
54
+ set: z.array(z.tuple([z.string(), z.number().nullable()])),
55
+ });
56
+
57
+ type TreeNodeLeafProbabilistic = z.infer<typeof treeNodeLeafProbabilistic>;
58
+
59
+ type TreeNodeNonLeafProbabilistic = (z.infer<typeof treeNodeBase> & {
60
+ children: Array<TreeNodeNonLeafProbabilistic | TreeNodeLeafProbabilistic>;
61
+ });
62
+
63
+ const treeNodeNonLeafProbabilistic: z.ZodType<TreeNodeNonLeafProbabilistic> = treeNodeBase
64
+ .extend({
65
+ children: z.lazy(() => z.array(
66
+ z.union([treeNodeNonLeafProbabilistic, treeNodeLeafProbabilistic]),
67
+ )),
68
+ });
69
+
70
+ const cellSets3 = z.object({
71
+ version: z.literal('0.1.3'),
72
+ tree: z.array(treeNodeNonLeafProbabilistic),
73
+ });
74
+
75
+ type TreeNode =
76
+ TreeNodeLeaf
77
+ | TreeNodeNonLeaf
78
+ | TreeNodeLeafProbabilistic
79
+ | TreeNodeNonLeafProbabilistic;
80
+
81
+ /**
82
+ * Transform a node object using a transform function.
83
+ * @param {object} node A node object.
84
+ * @param {function} predicate Returns true if a node matches a condition of interest.
85
+ * @param {function} transform Takes the node matching the predicate as input, returns
86
+ * a transformed version of the node.
87
+ * @param {array} transformedPaths This array parameter is mutated. The path of
88
+ * each transformed node is appended to this array.
89
+ * @param {string[]} The current path of the node being updated, used internally
90
+ * during recursion.
91
+ * @returns {object} The updated node.
92
+ */
93
+ export function nodeTransform(
94
+ node: TreeNode,
95
+ predicate: (a: TreeNode, b?: any) => boolean,
96
+ transform: (a: any, b?: any) => any,
97
+ transformedPaths: Array<string[]>,
98
+ currPath: string[] | null = null,
99
+ ): TreeNode {
100
+ let newPath: string[];
101
+ if (!currPath) {
102
+ newPath = [node.name];
103
+ } else {
104
+ newPath = [...currPath];
105
+ }
106
+ if (predicate(node, newPath)) {
107
+ transformedPaths.push(newPath);
108
+ return transform(node, newPath);
109
+ }
110
+ if ('children' in node) {
111
+ return {
112
+ ...node,
113
+ children: node.children.map((child: TreeNode): TreeNode => nodeTransform(
114
+ child, predicate, transform, transformedPaths, newPath.concat([child.name]),
115
+ )),
116
+ } as TreeNodeNonLeafProbabilistic;
117
+ }
118
+ return node;
119
+ }
120
+
121
+ export const obsSetsSchema = z.union([cellSets3, cellSets2])
122
+ .transform((v): z.infer<typeof cellSets3> => {
123
+ if (v.version === '0.1.3') return v;
124
+ // To upgrade from cell-sets schema 0.1.2 to 0.1.3,
125
+ // add a confidence value of null for each cell ID.
126
+ return {
127
+ ...v,
128
+ version: '0.1.3',
129
+ tree: v.tree.map(levelZeroNode => nodeTransform(
130
+ levelZeroNode,
131
+ (n: TreeNode) => !('children' in n) && Array.isArray(n.set),
132
+ (n: TreeNodeLeaf) => ({ ...n, set: n.set.map((itemId: string) => ([itemId, null])) }),
133
+ [],
134
+ ) as TreeNodeNonLeafProbabilistic),
135
+ };
136
+ });
137
+
138
+ export const obsSetsTabularSchema = z.array(z.object({
139
+ groupName: z.string(),
140
+ setName: z.string(),
141
+ setColor: rgbArray.optional(),
142
+ obsId: z.string(),
143
+ predictionScore: z.number().nullable().optional(),
144
+ }));
145
+
146
+ export const obsSetPath = z.array(z.string());
147
+
148
+ export const requestInit = z.object({
149
+ method: z.string().optional(),
150
+ headers: z.record(z.any()).optional(),
151
+ body: z.string().optional(),
152
+ mode: z.string().optional(),
153
+ credentials: z.string().optional(),
154
+ cache: z.string().optional(),
155
+ redirect: z.string().optional(),
156
+ referrer: z.string().optional(),
157
+ integrity: z.string().optional(),
158
+ });
@@ -0,0 +1,103 @@
1
+ import { z } from 'zod';
2
+
3
+ export const imageLayerObj = z.array(
4
+ z.object({
5
+ channels: z.array(
6
+ z.object({
7
+ color: z.array(z.number())
8
+ .describe(
9
+ 'The color to use when rendering this channel under the null colormap.',
10
+ )
11
+ .optional(),
12
+ selection: z.record(z.any())
13
+ .describe(
14
+ 'Determines the channel selection, e.g. some Z and time slice.',
15
+ ),
16
+ slider: z.array(z.number())
17
+ .describe('Determines the range for color mapping.')
18
+ .optional(),
19
+ visible: z.boolean()
20
+ .describe(
21
+ 'Determines whether this channel of the layer will be rendered in the spatial component.',
22
+ )
23
+ .optional(),
24
+ })
25
+ .strict(),
26
+ ),
27
+ colormap: z.string()
28
+ .nullable(),
29
+ transparentColor: z.array(z.number().describe('One of R G or B (0 - 255).'))
30
+ .length(3)
31
+ .describe('Determines the color to be set to opacity 0')
32
+ .nullable()
33
+ .optional(),
34
+ index: z
35
+ .number()
36
+ .describe(
37
+ 'The index of the layer among the array of layers available in the image file.',
38
+ ),
39
+ opacity: z.number(),
40
+ modelMatrix: z.array(z.number())
41
+ .length(16)
42
+ .describe('transformation matrix for this layer')
43
+ .optional(),
44
+ domainType: z.enum(['Full', 'Min/Max'])
45
+ .describe(
46
+ 'Determines the extent of the channel slider input element in the layer controller.',
47
+ )
48
+ .optional(),
49
+ resolution: z.number()
50
+ .describe('Resolution of 3D volumetric rendering')
51
+ .optional(),
52
+ xSlice: z.array(z.any())
53
+ .length(2)
54
+ .describe('Slice bounds')
55
+ .nullable()
56
+ .optional(),
57
+ renderingMode: z
58
+ .string()
59
+ .describe('Rendering mode of 3D volumetric rendering')
60
+ .optional(),
61
+ ySlice: z.array(z.any())
62
+ .length(2)
63
+ .describe('Slice bounds')
64
+ .nullable()
65
+ .optional(),
66
+ zSlice: z.array(z.any())
67
+ .length(2)
68
+ .describe('Slice bounds')
69
+ .nullable()
70
+ .optional(),
71
+ type: z.enum(['raster', 'bitmask'])
72
+ .optional(),
73
+ use3d: z.boolean()
74
+ .optional(),
75
+ visible: z
76
+ .boolean()
77
+ .describe(
78
+ 'Determines whether this entire layer will be rendered in the spatial component.',
79
+ )
80
+ .optional(),
81
+ })
82
+ .strict()
83
+ .describe(
84
+ 'The properties of this object are the rendering settings for the raster layer.',
85
+ ),
86
+ );
87
+
88
+ export const cellsLayerObj = z.object({
89
+ visible: z.boolean(),
90
+ stroked: z.boolean(),
91
+ radius: z.number(),
92
+ opacity: z.number(),
93
+ });
94
+
95
+ export const neighborhoodsLayerObj = z.object({
96
+ visible: z.boolean(),
97
+ });
98
+
99
+ export const moleculesLayerObj = z.object({
100
+ visible: z.boolean(),
101
+ radius: z.number(),
102
+ opacity: z.number(),
103
+ });