@vitessce/sets-utils 2.0.3 → 3.0.1

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 (53) hide show
  1. package/dist/{index.mjs → index.js} +9301 -7998
  2. package/dist-tsc/CellSetsZarrLoader.d.ts +2 -0
  3. package/dist-tsc/CellSetsZarrLoader.d.ts.map +1 -0
  4. package/dist-tsc/CellSetsZarrLoader.js +91 -0
  5. package/dist-tsc/CellSetsZarrLoader.test.d.ts +2 -0
  6. package/dist-tsc/CellSetsZarrLoader.test.d.ts.map +1 -0
  7. package/dist-tsc/CellSetsZarrLoader.test.js +155 -0
  8. package/dist-tsc/cell-set-utils.d.ts +204 -0
  9. package/dist-tsc/cell-set-utils.d.ts.map +1 -0
  10. package/dist-tsc/cell-set-utils.js +630 -0
  11. package/dist-tsc/cell-set-utils.test.d.ts +2 -0
  12. package/dist-tsc/cell-set-utils.test.d.ts.map +1 -0
  13. package/dist-tsc/cell-set-utils.test.fixtures.d.ts +47 -0
  14. package/dist-tsc/cell-set-utils.test.fixtures.d.ts.map +1 -0
  15. package/dist-tsc/cell-set-utils.test.fixtures.js +68 -0
  16. package/dist-tsc/cell-set-utils.test.js +186 -0
  17. package/dist-tsc/constants.d.ts +17 -0
  18. package/dist-tsc/constants.d.ts.map +1 -0
  19. package/dist-tsc/constants.js +17 -0
  20. package/dist-tsc/index.d.ts +7 -0
  21. package/dist-tsc/index.d.ts.map +1 -0
  22. package/dist-tsc/index.js +6 -5
  23. package/dist-tsc/io.d.ts +47 -0
  24. package/dist-tsc/io.d.ts.map +1 -0
  25. package/dist-tsc/io.js +159 -0
  26. package/dist-tsc/io.test.d.ts +2 -0
  27. package/dist-tsc/io.test.d.ts.map +1 -0
  28. package/dist-tsc/io.test.js +125 -0
  29. package/dist-tsc/set-path-utils.d.ts +28 -0
  30. package/dist-tsc/set-path-utils.d.ts.map +1 -0
  31. package/dist-tsc/set-path-utils.js +133 -0
  32. package/dist-tsc/set-path-utils.test.d.ts +2 -0
  33. package/dist-tsc/set-path-utils.test.d.ts.map +1 -0
  34. package/dist-tsc/set-path-utils.test.js +211 -0
  35. package/dist-tsc/utils.d.ts +45 -0
  36. package/dist-tsc/utils.d.ts.map +1 -0
  37. package/dist-tsc/utils.js +139 -0
  38. package/dist-tsc/utils.test.d.ts +2 -0
  39. package/dist-tsc/utils.test.d.ts.map +1 -0
  40. package/dist-tsc/utils.test.js +40 -0
  41. package/package.json +17 -9
  42. package/src/CellSetsZarrLoader.js +2 -2
  43. package/src/CellSetsZarrLoader.test.js +2 -1
  44. package/src/cell-set-utils.js +33 -16
  45. package/src/cell-set-utils.test.js +106 -4
  46. package/src/constants.js +4 -15
  47. package/src/index.js +11 -5
  48. package/src/io.js +25 -35
  49. package/src/io.test.js +2 -4
  50. package/src/set-path-utils.js +155 -0
  51. package/src/set-path-utils.test.js +258 -0
  52. package/src/utils.js +20 -9
  53. package/src/utils.test.js +49 -0
@@ -0,0 +1,139 @@
1
+ import tinycolor from 'tinycolor2';
2
+ import { isEqual } from 'lodash-es';
3
+ import { PALETTE } from '@vitessce/utils';
4
+ import { SETS_DATATYPE_OBS, HIERARCHICAL_SCHEMAS, } from './constants.js';
5
+ /**
6
+ * Execute a callback function based on a keypress event.
7
+ * @param {object} event The event from onKeyPress
8
+ * @param {string} key The key identifier to match.
9
+ * @param {Function} callback The function to execute.
10
+ */
11
+ export function callbackOnKeyPress(event, key, callback) {
12
+ if (event.key === key) {
13
+ event.preventDefault();
14
+ callback();
15
+ }
16
+ }
17
+ /**
18
+ * Convert an array of [r, g, b] numbers to a hex color.
19
+ * @param {number[]} rgbArray The color [r, g, b] array.
20
+ * @returns {string} The hex color as a string.
21
+ */
22
+ export function colorArrayToString(rgbArray) {
23
+ return tinycolor({ r: rgbArray[0], g: rgbArray[1], b: rgbArray[2] }).toHexString();
24
+ }
25
+ /**
26
+ * Convert a string color representation to an array of [r,g,b].
27
+ * @param {string} colorString The color as a string.
28
+ * @returns {number[]} The color as an array.
29
+ */
30
+ export function colorStringToArray(colorString) {
31
+ const colorObj = tinycolor(colorString).toRgb();
32
+ return [colorObj.r, colorObj.g, colorObj.b];
33
+ }
34
+ /**
35
+ * Get a string of help text for coloring a particular hierarchy level.
36
+ * @param {integer} i The level. 1 for cluster, 2 for subcluster, etc.
37
+ * @returns {string} The tooltip text for coloring the level.
38
+ */
39
+ export function getLevelTooltipText(i) {
40
+ if (i === 0)
41
+ return 'Color by hierarchy';
42
+ if (i <= 2) {
43
+ const subs = j => ('sub'.repeat(j));
44
+ return `Color by ${subs(i - 1)}cluster`;
45
+ }
46
+ return `Color by cluster level ${i}`;
47
+ }
48
+ export function isEqualOrPrefix(targetPath, testPath) {
49
+ if (targetPath.length <= testPath.length) {
50
+ return isEqual(targetPath, testPath.slice(0, targetPath.length));
51
+ }
52
+ return false;
53
+ }
54
+ export function tryRenamePath(targetPath, testPath, nextTargetPath) {
55
+ if (isEqualOrPrefix(targetPath, testPath)) {
56
+ return [...nextTargetPath, ...testPath.slice(nextTargetPath.length)];
57
+ }
58
+ return testPath;
59
+ }
60
+ export const PATH_SEP = '___';
61
+ export function pathToKey(path) {
62
+ return path.join(PATH_SEP);
63
+ }
64
+ // Moved from src/components/utils.js
65
+ export function getNextNumberedNodeName(nodes, prefix, suffix) {
66
+ let i = 1;
67
+ if (nodes) {
68
+ // eslint-disable-next-line no-loop-func
69
+ while (nodes.find(n => n.name.includes(`${prefix}${i}`))) {
70
+ // eslint-disable-next-line no-plusplus
71
+ i++;
72
+ }
73
+ }
74
+ return `${prefix}${i}${suffix}`;
75
+ }
76
+ /**
77
+ * Create a new selected cell set based on a cell selection.
78
+ * @param {string[]} cellSelection An array of cell IDs.
79
+ * @param {object[]} additionalCellSets The previous array of user-defined cell sets.
80
+ * @param {function} setCellSetSelection The setter function for cell set selections.
81
+ * @param {function} setAdditionalCellSets The setter function for user-defined cell sets.
82
+ */
83
+ export function setObsSelection(cellSelection, additionalCellSets, cellSetColor, setCellSetSelection, setAdditionalCellSets, setCellSetColor, setCellColorEncoding, prefix = 'Selection ', suffix = '') {
84
+ const CELL_SELECTIONS_LEVEL_ZERO_NAME = 'My Selections';
85
+ const selectionsLevelZeroNode = additionalCellSets?.tree.find(n => n.name === CELL_SELECTIONS_LEVEL_ZERO_NAME);
86
+ const nextAdditionalCellSets = {
87
+ version: HIERARCHICAL_SCHEMAS.latestVersion,
88
+ datatype: SETS_DATATYPE_OBS,
89
+ tree: [...(additionalCellSets ? additionalCellSets.tree : [])],
90
+ };
91
+ const nextName = getNextNumberedNodeName(selectionsLevelZeroNode?.children, prefix, suffix);
92
+ let colorIndex = 0;
93
+ if (selectionsLevelZeroNode) {
94
+ colorIndex = selectionsLevelZeroNode.children.length;
95
+ selectionsLevelZeroNode.children.push({
96
+ name: nextName,
97
+ set: cellSelection.map(d => [d, null]),
98
+ });
99
+ }
100
+ else {
101
+ nextAdditionalCellSets.tree.push({
102
+ name: CELL_SELECTIONS_LEVEL_ZERO_NAME,
103
+ children: [
104
+ {
105
+ name: nextName,
106
+ set: cellSelection.map(d => [d, null]),
107
+ },
108
+ ],
109
+ });
110
+ }
111
+ setAdditionalCellSets(nextAdditionalCellSets);
112
+ const nextPath = ['My Selections', nextName];
113
+ setCellSetColor([
114
+ ...(cellSetColor || []),
115
+ {
116
+ path: nextPath,
117
+ color: PALETTE[colorIndex % PALETTE.length],
118
+ },
119
+ ]);
120
+ setCellSetSelection([nextPath]);
121
+ setCellColorEncoding('cellSetSelection');
122
+ }
123
+ export function mergeObsSets(cellSets, additionalCellSets) {
124
+ return {
125
+ version: HIERARCHICAL_SCHEMAS.latestVersion,
126
+ datatype: SETS_DATATYPE_OBS,
127
+ tree: [
128
+ ...(cellSets ? cellSets.tree : []),
129
+ ...(additionalCellSets ? additionalCellSets.tree : []),
130
+ ],
131
+ };
132
+ }
133
+ export function getObsInfoFromDataWithinRange(range, data) {
134
+ const [lowerBound, upperBound] = range;
135
+ const cellIds = data
136
+ .filter(item => item.value >= lowerBound && item.value <= upperBound)
137
+ .map(item => item.cellId);
138
+ return cellIds;
139
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=utils.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.test.d.ts","sourceRoot":"","sources":["../src/utils.test.js"],"names":[],"mappings":""}
@@ -0,0 +1,40 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { getObsInfoFromDataWithinRange } from './utils.js';
3
+ describe('getObsInfoFromDataWithinRange', () => {
4
+ it('should return the correct obs info with no gene defined', () => {
5
+ const data = [
6
+ { value: 107.84313725490196, gene: null, cellId: '778' },
7
+ { value: 75.29411764705883, gene: null, cellId: '1409' },
8
+ { value: 111.76470588235294, gene: null, cellId: '3642' },
9
+ { value: 34.11764705882353, gene: null, cellId: '1302' },
10
+ { value: 75.68627450980392, gene: null, cellId: '1285' },
11
+ ];
12
+ const range = [0, 100];
13
+ const expectedObsInfo = ['1409', '1302', '1285'];
14
+ const actualObsInfo = getObsInfoFromDataWithinRange(range, data);
15
+ actualObsInfo.forEach((item) => {
16
+ expect(expectedObsInfo).toContain(item);
17
+ });
18
+ expectedObsInfo.forEach((item) => {
19
+ expect(actualObsInfo).toContain(item);
20
+ });
21
+ });
22
+ it('should return the correct obs info when gene is defined', () => {
23
+ const data = [
24
+ { value: 107.84313725490196, gene: 'ABC', cellId: '778' },
25
+ { value: 75.29411764705883, gene: 'ABC', cellId: '1409' },
26
+ { value: 111.76470588235294, gene: 'ABC', cellId: '3642' },
27
+ { value: 34.11764705882353, gene: 'ABC', cellId: '1302' },
28
+ { value: 75.68627450980392, gene: 'ABC', cellId: '1285' },
29
+ ];
30
+ const range = [0, 100];
31
+ const expectedObsInfo = ['1409', '1302', '1285'];
32
+ const actualObsInfo = getObsInfoFromDataWithinRange(range, data);
33
+ actualObsInfo.forEach((item) => {
34
+ expect(expectedObsInfo).toContain(item);
35
+ });
36
+ expectedObsInfo.forEach((item) => {
37
+ expect(actualObsInfo).toContain(item);
38
+ });
39
+ });
40
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitessce/sets-utils",
3
- "version": "2.0.3",
3
+ "version": "3.0.1",
4
4
  "author": "Gehlenborg Lab",
5
5
  "homepage": "http://vitessce.io",
6
6
  "repository": {
@@ -8,27 +8,35 @@
8
8
  "url": "git+https://github.com/vitessce/vitessce.git"
9
9
  },
10
10
  "license": "MIT",
11
- "main": "dist/index.mjs",
11
+ "type": "module",
12
+ "main": "dist/index.js",
12
13
  "files": [
14
+ "src",
13
15
  "dist",
14
- "src"
16
+ "dist-tsc"
15
17
  ],
16
18
  "dependencies": {
17
19
  "@turf/centroid": "^6.5.0",
18
20
  "@turf/helpers": "^6.5.0",
19
- "ajv": "^6.10.0",
20
21
  "concaveman": "^1.2.1",
21
22
  "d3-dsv": "^1.1.1",
22
23
  "internmap": "^2.0.3",
23
24
  "json2csv": "^5.0.0",
24
- "lodash": "^4.17.21",
25
+ "lodash-es": "^4.17.21",
25
26
  "tinycolor2": "^1.4.1",
26
- "uuid": "^3.3.2",
27
- "@vitessce/utils": "2.0.3",
28
- "@vitessce/vit-s": "2.0.3"
27
+ "uuid": "^9.0.0",
28
+ "@vitessce/schemas": "3.0.1",
29
+ "@vitessce/utils": "3.0.1"
29
30
  },
30
31
  "scripts": {
31
32
  "bundle": "pnpm exec vite build -c ../../../scripts/vite.config.js",
32
- "test": "pnpm exec vitest --run -r ../../../ --dir ."
33
+ "test": "pnpm exec vitest --run"
34
+ },
35
+ "module": "dist/index.js",
36
+ "exports": {
37
+ ".": {
38
+ "types": "./dist-tsc/index.d.ts",
39
+ "import": "./dist/index.js"
40
+ }
33
41
  }
34
42
  }
@@ -2,10 +2,10 @@ import { InternMap } from 'internmap';
2
2
  import {
3
3
  treeInitialize,
4
4
  nodeAppendChild,
5
- } from './cell-set-utils';
5
+ } from './cell-set-utils.js';
6
6
  import {
7
7
  SETS_DATATYPE_OBS,
8
- } from './constants';
8
+ } from './constants.js';
9
9
 
10
10
  export function dataToCellSetsTree(data, options) {
11
11
  // obsIndex is an array of all cell IDs, for the purposes of set complement operations only.
@@ -1,4 +1,5 @@
1
- import { dataToCellSetsTree } from './CellSetsZarrLoader';
1
+ import { describe, it, expect } from 'vitest';
2
+ import { dataToCellSetsTree } from './CellSetsZarrLoader.js';
2
3
 
3
4
  describe('loaders/CellSetsZarrLoader', () => {
4
5
  it('dataToCellSetsTree constructs a hierarchy from an array of columns', async () => {
@@ -1,16 +1,14 @@
1
1
  /* eslint-disable no-underscore-dangle */
2
- import uuidv4 from 'uuid/v4';
3
- import isNil from 'lodash/isNil';
4
- import isEqual from 'lodash/isEqual';
5
- import range from 'lodash/range';
2
+ import { v4 as uuidv4 } from 'uuid';
3
+ import { isNil, isEqual, range } from 'lodash-es';
6
4
  import { featureCollection as turfFeatureCollection, point as turfPoint } from '@turf/helpers';
7
5
  import centroid from '@turf/centroid';
8
6
  import concaveman from 'concaveman';
9
7
  import { getDefaultColor, PALETTE } from '@vitessce/utils';
10
8
  import {
11
9
  HIERARCHICAL_SCHEMAS,
12
- } from './constants';
13
- import { pathToKey } from './utils';
10
+ } from './constants.js';
11
+ import { pathToKey } from './utils.js';
14
12
 
15
13
  /**
16
14
  * Alias for the uuidv4 function to make code more readable.
@@ -291,7 +289,7 @@ export function nodeToLevelDescendantNamePaths(node, level, prevPath, stopEarly
291
289
  */
292
290
  export function treeExport(currTree, datatype) {
293
291
  return {
294
- version: HIERARCHICAL_SCHEMAS[datatype].latestVersion,
292
+ version: HIERARCHICAL_SCHEMAS.latestVersion,
295
293
  datatype,
296
294
  tree: currTree.tree,
297
295
  };
@@ -346,7 +344,7 @@ export function treeExportSet(currTree, nodePath) {
346
344
  */
347
345
  export function treeInitialize(datatype) {
348
346
  return {
349
- version: HIERARCHICAL_SCHEMAS[datatype].latestVersion,
347
+ version: HIERARCHICAL_SCHEMAS.latestVersion,
350
348
  datatype,
351
349
  tree: [],
352
350
  };
@@ -509,8 +507,9 @@ export function treeToCellPolygonsBySetNames(
509
507
  * Given a tree with state, get the sizes of the
510
508
  * sets currently marked as "visible".
511
509
  * @param {object} currTree A tree object.
510
+ * @param {array} allNamePaths Array of all paths.
512
511
  * @param {array} selectedNamePaths Array of arrays of strings,
513
- * representing set "paths".
512
+ * representing selected paths.
514
513
  * @param {object[]} setColor Array of objects with the
515
514
  * properties `path` and `color`.
516
515
  * @param {string} theme "light" or "dark" for the vitessce theme
@@ -518,20 +517,38 @@ export function treeToCellPolygonsBySetNames(
518
517
  * with the properties `name`, `size`, `key`,
519
518
  * and `color`.
520
519
  */
521
- export function treeToSetSizesBySetNames(currTree, selectedNamePaths, setColor, theme) {
520
+ export function treeToSetSizesBySetNames(
521
+ currTree, allNamePaths, selectedNamePaths, setColor, theme,
522
+ ) {
522
523
  const sizes = [];
523
- selectedNamePaths.forEach((setNamePath) => {
524
- const node = treeFindNodeByNamePath(currTree, setNamePath);
524
+
525
+ /**
526
+ * Checks if a path is contained in an array of paths.
527
+ * @param {array} path Array of strings, which compose the path.
528
+ * @param {array} paths Array of arrays of strings, which compose paths.
529
+ * */
530
+ const contains = (path, paths) => paths.some(p => isEqual(p, path));
531
+
532
+ allNamePaths.forEach((clusterPath) => {
533
+ const node = treeFindNodeByNamePath(currTree, clusterPath);
525
534
  if (node) {
526
535
  const nodeSet = nodeToSet(node);
527
- const nodeColor = setColor?.find(d => isEqual(d.path, setNamePath))?.color
528
- || getDefaultColor(theme);
529
- sizes.push({
536
+ const nodeColor = setColor?.find(d => isEqual(d.path, clusterPath))?.color
537
+ || getDefaultColor(theme);
538
+ const nodeProps = {
530
539
  key: generateKey(),
531
540
  name: node.name,
532
541
  size: nodeSet.length,
533
542
  color: nodeColor,
534
- });
543
+ setNamePath: clusterPath,
544
+ // used by the CellSetSizesPlot to determine if the bar should be grayed out
545
+ isGrayedOut: true,
546
+ };
547
+ // if the current path is selected, we need to show it
548
+ if (contains(clusterPath, selectedNamePaths)) {
549
+ nodeProps.isGrayedOut = false;
550
+ }
551
+ sizes.push(nodeProps);
535
552
  }
536
553
  });
537
554
  return sizes;
@@ -1,5 +1,6 @@
1
1
  /* eslint-disable no-underscore-dangle */
2
- import { cloneDeep } from 'lodash';
2
+ import { describe, it, expect } from 'vitest';
3
+ import { cloneDeep } from 'lodash-es';
3
4
 
4
5
  import {
5
6
  nodeToRenderProps,
@@ -13,14 +14,14 @@ import {
13
14
  nodeToLevelDescendantNamePaths,
14
15
  treeExport,
15
16
  filterNode,
16
- } from './cell-set-utils';
17
+ treeToSetSizesBySetNames,
18
+ } from './cell-set-utils.js';
17
19
 
18
20
  import {
19
21
  levelTwoNodeLeaf,
20
22
  levelZeroNode,
21
23
  tree,
22
- } from './cell-set-utils.test.fixtures';
23
-
24
+ } from './cell-set-utils.test.fixtures.js';
24
25
 
25
26
  describe('Hierarchical sets cell-set-utils', () => {
26
27
  describe('Node rendering', () => {
@@ -107,6 +108,7 @@ describe('Hierarchical sets cell-set-utils', () => {
107
108
  [],
108
109
  ['Cell Type Annotations'],
109
110
  );
111
+
110
112
  // No nodes transformed for fals-y predicate.
111
113
  expect(nodeTransformedWithoutPredicate.name).toEqual('Cell Type Annotations');
112
114
  expect(
@@ -115,6 +117,106 @@ describe('Hierarchical sets cell-set-utils', () => {
115
117
  });
116
118
  });
117
119
 
120
+ describe('Generate properties from paths', () => {
121
+ it('treeToSetSizesBySetNames where one bar is selected', () => {
122
+ const setColor = [
123
+ {
124
+ path: ['Cell Type Annotations', 'Vasculature', 'Endothelial'],
125
+ color: [255, 0, 0],
126
+ },
127
+ {
128
+ path: ['Cell Type Annotations', 'Vasculature'],
129
+ color: [68, 119, 170],
130
+ },
131
+ ];
132
+ const setSizes = treeToSetSizesBySetNames(
133
+ tree,
134
+ [['Cell Type Annotations', 'Vasculature', 'Endothelial'], ['Cell Type Annotations', 'Vasculature']],
135
+ [['Cell Type Annotations', 'Vasculature', 'Endothelial']],
136
+ setColor,
137
+ 'dark',
138
+ );
139
+
140
+ expect(setSizes.length).toEqual(2);
141
+ expect(setSizes[0].name).toEqual('Endothelial');
142
+ expect(setSizes[0].size).toEqual(3);
143
+ expect(setSizes[0].color).toEqual([255, 0, 0]);
144
+ expect(setSizes[0].isGrayedOut).toEqual(false);
145
+ expect(setSizes[0].setNamePath).toEqual(['Cell Type Annotations', 'Vasculature', 'Endothelial']);
146
+
147
+ expect(setSizes[1].name).toEqual('Vasculature');
148
+ expect(setSizes[1].size).toEqual(9);
149
+ expect(setSizes[1].color).toEqual([68, 119, 170]);
150
+ expect(setSizes[1].isGrayedOut).toEqual(true);
151
+ });
152
+
153
+ it('treeToSetSizesBySetNames where nothing is selected', () => {
154
+ const setColor = [
155
+ {
156
+ path: ['Cell Type Annotations', 'Vasculature', 'Endothelial'],
157
+ color: [255, 0, 0],
158
+ },
159
+ {
160
+ path: ['Cell Type Annotations', 'Vasculature'],
161
+ color: [68, 119, 170],
162
+ },
163
+ ];
164
+ const setSizes = treeToSetSizesBySetNames(
165
+ tree,
166
+ [['Cell Type Annotations', 'Vasculature', 'Endothelial'], ['Cell Type Annotations', 'Vasculature']],
167
+ [],
168
+ setColor,
169
+ 'dark',
170
+ );
171
+
172
+ expect(setSizes.length).toEqual(2);
173
+ expect(setSizes[0].name).toEqual('Endothelial');
174
+ expect(setSizes[0].size).toEqual(3);
175
+ expect(setSizes[0].color).toEqual([255, 0, 0]);
176
+ expect(setSizes[0].isGrayedOut).toEqual(true);
177
+ expect(setSizes[0].setNamePath).toEqual(['Cell Type Annotations', 'Vasculature', 'Endothelial']);
178
+
179
+ expect(setSizes[1].name).toEqual('Vasculature');
180
+ expect(setSizes[1].size).toEqual(9);
181
+ expect(setSizes[1].color).toEqual([68, 119, 170]);
182
+ expect(setSizes[1].isGrayedOut).toEqual(true);
183
+ expect(setSizes[1].setNamePath).toEqual(['Cell Type Annotations', 'Vasculature']);
184
+ });
185
+
186
+ it('treeToSetSizesBySetNames where selected paths are not part of all paths', () => {
187
+ const setColor = [
188
+ {
189
+ path: ['Cell Type Annotations', 'Vasculature', 'Endothelial'],
190
+ color: [255, 0, 0],
191
+ },
192
+ {
193
+ path: ['Cell Type Annotations', 'Vasculature'],
194
+ color: [68, 119, 170],
195
+ },
196
+ ];
197
+ const setSizes = treeToSetSizesBySetNames(
198
+ tree,
199
+ [['Cell Type Annotations', 'Vasculature', 'Endothelial'], ['Cell Type Annotations', 'Vasculature']],
200
+ [['Louvain Clusters', '0'], ['Louvain Clusters', '1']],
201
+ setColor,
202
+ 'dark',
203
+ );
204
+
205
+ expect(setSizes.length).toEqual(2);
206
+ expect(setSizes[0].name).toEqual('Endothelial');
207
+ expect(setSizes[0].size).toEqual(3);
208
+ expect(setSizes[0].color).toEqual([255, 0, 0]);
209
+ expect(setSizes[0].isGrayedOut).toEqual(true);
210
+ expect(setSizes[0].setNamePath).toEqual(['Cell Type Annotations', 'Vasculature', 'Endothelial']);
211
+
212
+ expect(setSizes[1].name).toEqual('Vasculature');
213
+ expect(setSizes[1].size).toEqual(9);
214
+ expect(setSizes[1].color).toEqual([68, 119, 170]);
215
+ expect(setSizes[1].isGrayedOut).toEqual(true);
216
+ expect(setSizes[1].setNamePath).toEqual(['Cell Type Annotations', 'Vasculature']);
217
+ });
218
+ });
219
+
118
220
  describe('Get from tree', () => {
119
221
  it('Tree Union', () => {
120
222
  const union = treeToUnion(
package/src/constants.js CHANGED
@@ -1,4 +1,4 @@
1
- import { obsSetsSchema, obsSetsTabularSchema } from '@vitessce/vit-s';
1
+ import { obsSetsSchema, obsSetsTabularSchema } from '@vitessce/schemas';
2
2
 
3
3
  export const FILE_EXTENSION_JSON = 'json';
4
4
  export const MIME_TYPE_JSON = 'application/json';
@@ -12,21 +12,10 @@ export const NA_VALUE_TABULAR = 'NA';
12
12
 
13
13
  export const SETS_DATATYPE_OBS = 'obs';
14
14
  export const HIERARCHICAL_SCHEMAS = {
15
- cell: {
16
- latestVersion: '0.1.3',
17
- schema: obsSetsSchema,
18
- },
19
- obs: {
20
- latestVersion: '0.1.3',
21
- schema: obsSetsSchema,
22
- },
15
+ latestVersion: '0.1.3',
16
+ schema: obsSetsSchema,
23
17
  };
24
18
 
25
19
  export const TABULAR_SCHEMAS = {
26
- cell: {
27
- schema: obsSetsTabularSchema,
28
- },
29
- obs: {
30
- schema: obsSetsTabularSchema,
31
- },
20
+ schema: obsSetsTabularSchema,
32
21
  };
package/src/index.js CHANGED
@@ -22,7 +22,12 @@ export {
22
22
  getCellSetPolygons,
23
23
  treeToMembershipMap,
24
24
  nodeToSet,
25
- } from './cell-set-utils';
25
+ } from './cell-set-utils.js';
26
+ export {
27
+ findLongestCommonPath,
28
+ filterPathsByExpansionAndSelection,
29
+ findChangedHierarchy,
30
+ } from './set-path-utils.js';
26
31
  export {
27
32
  isEqualOrPrefix,
28
33
  tryRenamePath,
@@ -34,7 +39,8 @@ export {
34
39
  callbackOnKeyPress,
35
40
  getLevelTooltipText,
36
41
  pathToKey,
37
- } from './utils';
42
+ getObsInfoFromDataWithinRange,
43
+ } from './utils.js';
38
44
  export {
39
45
  downloadForUser,
40
46
  handleExportJSON,
@@ -42,14 +48,14 @@ export {
42
48
  tryUpgradeTreeToLatestSchema,
43
49
  handleImportJSON,
44
50
  handleImportTabular,
45
- } from './io';
51
+ } from './io.js';
46
52
  export {
47
53
  FILE_EXTENSION_JSON,
48
54
  FILE_EXTENSION_TABULAR,
49
55
  SETS_DATATYPE_OBS,
50
56
  MIME_TYPE_JSON,
51
57
  MIME_TYPE_TABULAR,
52
- } from './constants';
58
+ } from './constants.js';
53
59
  export {
54
60
  dataToCellSetsTree,
55
- } from './CellSetsZarrLoader';
61
+ } from './CellSetsZarrLoader.js';
package/src/io.js CHANGED
@@ -1,17 +1,15 @@
1
- import Ajv from 'ajv';
2
- import isNil from 'lodash/isNil';
1
+ import { isNil } from 'lodash-es';
3
2
  import { dsvFormat } from 'd3-dsv';
4
3
  // TODO(monorepo): try to find a different package for this.
5
4
  // Reference: https://github.com/zemirco/json2csv/issues/539
6
- import { Parser } from 'json2csv/dist/json2csv.umd';
5
+ import { Parser } from 'json2csv/dist/json2csv.umd.js';
7
6
  import { getDefaultColor } from '@vitessce/utils';
8
- import { colorArrayToString, colorStringToArray } from './utils';
9
- import { nodeTransform } from './cell-set-utils';
7
+ import { colorArrayToString, colorStringToArray } from './utils.js';
10
8
  import {
11
9
  HIERARCHICAL_SCHEMAS, TABULAR_SCHEMAS,
12
10
  MIME_TYPE_JSON, MIME_TYPE_TABULAR,
13
11
  SEPARATOR_TABULAR, NA_VALUE_TABULAR,
14
- } from './constants';
12
+ } from './constants.js';
15
13
 
16
14
  /**
17
15
  * Check if an imported tree has an old schema version that we know how to
@@ -19,32 +17,23 @@ import {
19
17
  * @param {object} currTree A hierarchical tree object with a .version property,
20
18
  * which has already passed schema validation, but may not have the latest schema version.
21
19
  * @param {string} datatype The data type of the items in the schema.
20
+ * @returns {[object, boolean]} The upgraded tree object and a boolean indicating
21
+ * whether the tree was upgraded.
22
22
  */
23
+ // eslint-disable-next-line no-unused-vars
23
24
  export function tryUpgradeTreeToLatestSchema(currTree, datatype) {
24
- const validate = new Ajv().compile(HIERARCHICAL_SCHEMAS[datatype].schema);
25
- const valid = validate(currTree);
25
+ const zodSchema = HIERARCHICAL_SCHEMAS.schema;
26
+ const latestSchemaVersion = HIERARCHICAL_SCHEMAS.latestVersion;
27
+ const parseResult = zodSchema.safeParse(currTree);
28
+ const valid = parseResult.success;
26
29
  if (!valid) {
27
- const failureReason = JSON.stringify(validate.errors, null, 2);
30
+ const failureReason = JSON.stringify(parseResult.error.message, null, 2);
28
31
  throw new Error(`Tree validation failed: ${failureReason}`);
29
- } else if (currTree.datatype !== datatype) {
30
- throw new Error(
31
- `The data type does not match the expected data type of '${datatype}'.`,
32
- );
33
- }
34
- if (currTree.version === '0.1.2') {
35
- // To upgrade from cell-sets schema 0.1.2 to 0.1.3,
36
- // add a confidence value of null for each cell ID.
37
- return {
38
- ...currTree,
39
- version: HIERARCHICAL_SCHEMAS[datatype].latestVersion,
40
- tree: currTree.tree.map(levelZeroNode => nodeTransform(
41
- levelZeroNode,
42
- n => !n.children && Array.isArray(n.set),
43
- n => ({ ...n, set: n.set.map(itemId => ([itemId, null])) }), [],
44
- )),
45
- };
46
32
  }
47
- return currTree;
33
+ // Zod will not only validate, but also will upgrade from v0.1.2 to v0.1.3 via transform.
34
+ // We also want to return a boolean indicating whether the tree was upgraded.
35
+ const didUpgrade = (currTree.version !== latestSchemaVersion);
36
+ return [parseResult.data, didUpgrade];
48
37
  }
49
38
 
50
39
  /**
@@ -59,7 +48,7 @@ export function tryUpgradeTreeToLatestSchema(currTree, datatype) {
59
48
  export function handleImportJSON(result, datatype, theme) {
60
49
  let importData = JSON.parse(result);
61
50
  // Validate the imported file.
62
- importData = tryUpgradeTreeToLatestSchema(importData, datatype);
51
+ [importData] = tryUpgradeTreeToLatestSchema(importData, datatype);
63
52
  return importData;
64
53
  }
65
54
 
@@ -88,25 +77,26 @@ export function handleImportTabular(result, datatype, theme) {
88
77
  ),
89
78
  }));
90
79
  // Validate the imported file.
91
- const validate = new Ajv().compile(TABULAR_SCHEMAS[datatype].schema);
92
- const valid = validate(importData);
80
+ const zodSchema = TABULAR_SCHEMAS.schema;
81
+ const parseResult = zodSchema.safeParse(importData);
82
+ const valid = parseResult.success;
93
83
  if (!valid) {
94
- const failureReason = JSON.stringify(validate.errors, null, 2);
84
+ const failureReason = JSON.stringify(parseResult.error.message, null, 2);
95
85
  throw new Error(`Import validation failed: ${failureReason}`);
96
86
  } else {
87
+ const parsedData = parseResult.data;
97
88
  // Convert the validated array to a tree representation.
98
89
  const treeToImport = {
99
- version: HIERARCHICAL_SCHEMAS[datatype].latestVersion,
100
- datatype,
90
+ version: HIERARCHICAL_SCHEMAS.latestVersion,
101
91
  tree: [],
102
92
  };
103
- const uniqueGroupNames = Array.from(new Set(importData.map(d => d.groupName)));
93
+ const uniqueGroupNames = Array.from(new Set(parsedData.map(d => d.groupName)));
104
94
  uniqueGroupNames.forEach((groupName) => {
105
95
  const levelZeroNode = {
106
96
  name: groupName,
107
97
  children: [],
108
98
  };
109
- const groupRows = importData.filter(d => d.groupName === groupName);
99
+ const groupRows = parsedData.filter(d => d.groupName === groupName);
110
100
  const uniqueSetNames = Array.from(new Set(groupRows.map(d => d.setName)));
111
101
  uniqueSetNames.forEach((setName) => {
112
102
  const setRows = groupRows.filter(d => d.setName === setName);